4 from __future__
import absolute_import
5 from __future__
import division
6 from __future__
import print_function
7 from __future__
import unicode_literals
16 def __init__(self, workspace_id):
22 self.workspace_stack.append(workspace.CurrentWorkspace())
23 workspace.SwitchWorkspace(self.
workspace_id, create_if_missing=
True)
25 def __exit__(self, exc_type, exc_value, traceback):
26 w = self.workspace_stack.pop()
34 workspace.SwitchWorkspace(w, create_if_missing=
True)
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. 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. 60 def __getattr__(self, attr):
61 def f(*args, **kwargs):
63 return getattr(workspace, attr)(*args, **kwargs)