An object representing a Caffe2 workspace. It is a context manager, so you can say 'with workspace:' to use the represented workspace as your global workspace. It also supports every method supported by caffe2.python.workspace, but instead of running these operations in the gl
| 35 | |
| 36 | |
| 37 | class Workspace: |
| 38 | """ |
| 39 | An object representing a Caffe2 workspace. It is a context manager, |
| 40 | so you can say 'with workspace:' to use the represented workspace |
| 41 | as your global workspace. It also supports every method supported |
| 42 | by caffe2.python.workspace, but instead of running these operations |
| 43 | in the global workspace, it runs them in the workspace represented |
| 44 | by this object. When this object goes dead, the workspace (and all |
| 45 | nets and blobs within it) are freed. |
| 46 | |
| 47 | Why do we need this class? Caffe2's workspace model is very "global state" |
| 48 | oriented, in that there is always some ambient global workspace you are |
| 49 | working in which holds on to all of your networks and blobs. This class |
| 50 | makes it possible to work with workspaces more locally, and without |
| 51 | forgetting to deallocate everything in the end. |
| 52 | """ |
| 53 | def __init__(self): |
| 54 | # Caffe2 (apparently) doesn't provide any native method of generating |
| 55 | # a fresh, unused workspace, so we have to fake it by generating |
| 56 | # a unique ID and hoping it's not used already / will not be used |
| 57 | # directly in the future. |
| 58 | self._ctx = _WorkspaceCtx(str(uuid.uuid4())) |
| 59 | |
| 60 | def __getattr__(self, attr): |
| 61 | def f(*args, **kwargs): |
| 62 | with self._ctx: |
| 63 | return getattr(workspace, attr)(*args, **kwargs) |
| 64 | return f |
| 65 | |
| 66 | def __del__(self): |
| 67 | # NB: This is a 'self' call because we need to switch into the workspace |
| 68 | # we want to reset before we actually reset it. A direct call to |
| 69 | # workspace.ResetWorkspace() will reset the ambient workspace, which |
| 70 | # is not want we want. |
| 71 | self.ResetWorkspace() |
no outgoing calls
searching dependent graphs…