4 from __future__
import absolute_import
5 from __future__
import division
6 from __future__
import print_function
7 from __future__
import unicode_literals
16 An object representing a Caffe2 workspace. It is a context manager, 17 so you can say 'with workspace:' to use the represented workspace 18 as your global workspace. It also supports every method supported 19 by caffe2.python.workspace, but instead of running these operations 20 in the global workspace, it runs them in the workspace represented 21 by this object. When this object goes dead, the workspace (and all 22 nets and blobs within it) are freed. 24 Why do we need this class? Caffe2's workspace model is very "global state" 25 oriented, in that there is always some ambient global workspace you are 26 working in which holds on to all of your networks and blobs. This class 27 makes it possible to work with workspaces more locally, and without 28 forgetting to deallocate everything in the end. 39 def __getattr__(self, attr):
40 def f(*args, **kwargs):
42 return getattr(workspace, attr)(*args, **kwargs)
46 self.workspace_stack.append(workspace.CurrentWorkspace())
47 workspace.SwitchWorkspace(self.
workspace_id, create_if_missing=
True)
49 def __exit__(self, exc_type, exc_value, traceback):
50 w = self.workspace_stack.pop()
58 workspace.SwitchWorkspace(w, create_if_missing=
True)