Caffe2 - C++ API
A deep learning, cross platform ML framework
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Data Fields
caffe2::Workspace Class Reference

Workspace is a class that holds all the related objects created during runtime: (1) all blobs, and (2) all instantiated networks. More...

#include <workspace.h>

Public Types

typedef std::function< bool(int)> ShouldContinue
 
typedef CaffeMap< string, unique_ptr< Blob > > BlobMap
 
typedef CaffeMap< string, unique_ptr< NetBase > > NetMap
 

Public Member Functions

 Workspace ()
 Initializes an empty workspace.
 
 Workspace (const string &root_folder)
 Initializes an empty workspace with the given root folder. More...
 
 Workspace (const Workspace *shared)
 Initializes a workspace with a shared workspace. More...
 
 Workspace (const Workspace *shared, const std::unordered_map< string, string > &forwarded_blobs)
 Initializes workspace with parent workspace, blob name remapping (new name -> parent blob name), no other blobs are inherited from parent workspace.
 
 Workspace (const string &root_folder, const Workspace *shared)
 Initializes a workspace with a root folder and a shared workspace.
 
void AddBlobMapping (const Workspace *parent, const std::unordered_map< string, string > &forwarded_blobs, bool skip_defined_blobs=false)
 Adds blob mappings from workspace to the blobs from parent workspace. More...
 
template<class Context >
void CopyForwardedTensors (const std::unordered_set< std::string > &blobs)
 Converts previously mapped tensor blobs to local blobs, copies values from parent workspace blobs into new local blobs. More...
 
vector< string > LocalBlobs () const
 Return list of blobs owned by this Workspace, not including blobs shared from parent workspace.
 
vector< string > Blobs () const
 Return a list of blob names. More...
 
const string & RootFolder ()
 Return the root folder of the workspace.
 
bool HasBlob (const string &name) const
 Checks if a blob with the given name is present in the current workspace.
 
void PrintBlobSizes ()
 
BlobCreateBlob (const string &name)
 Creates a blob of the given name. More...
 
BlobCreateLocalBlob (const string &name)
 Similar to CreateBlob(), but it creates a blob in the local workspace even if another blob with the same name already exists in the parent workspace – in such case the new blob hides the blob in parent workspace. More...
 
bool RemoveBlob (const string &name)
 Remove the blob of the given name. More...
 
const BlobGetBlob (const string &name) const
 Gets the blob with the given name as a const pointer. More...
 
BlobGetBlob (const string &name)
 Gets the blob with the given name as a mutable pointer. More...
 
BlobRenameBlob (const string &old_name, const string &new_name)
 Renames a local workspace blob. More...
 
NetBaseCreateNet (const NetDef &net_def, bool overwrite=false)
 Creates a network with the given NetDef, and returns the pointer to the network. More...
 
NetBaseCreateNet (const std::shared_ptr< const NetDef > &net_def, bool overwrite=false)
 
NetBaseGetNet (const string &net_name)
 Gets the pointer to a created net. More...
 
void DeleteNet (const string &net_name)
 Deletes the instantiated network with the given name.
 
bool RunNet (const string &net_name)
 Finds and runs the instantiated network with the given name. More...
 
vector< string > Nets () const
 Returns a list of names of the currently instantiated networks.
 
bool RunPlan (const PlanDef &plan_def, ShouldContinue should_continue=StopOnSignal{})
 Runs a plan that has multiple nets and execution steps.
 
ThreadPoolGetThreadPool ()
 
bool RunOperatorOnce (const OperatorDef &op_def)
 
bool RunNetOnce (const NetDef &net_def)
 

Static Public Member Functions

template<typename F >
static void ForEach (F f)
 Applies a function f on each workspace that currently exists. More...
 

Data Fields

std::atomic< int > last_failed_op_net_position {}
 

Detailed Description

Workspace is a class that holds all the related objects created during runtime: (1) all blobs, and (2) all instantiated networks.

It is the owner of all these objects and deals with the scaffolding logistics.

Definition at line 47 of file workspace.h.

Constructor & Destructor Documentation

caffe2::Workspace::Workspace ( const string &  root_folder)
inlineexplicit

Initializes an empty workspace with the given root folder.

For any operators that are going to interface with the file system, such as load operators, they will write things under this root folder given by the workspace.

Definition at line 64 of file workspace.h.

caffe2::Workspace::Workspace ( const Workspace shared)
inlineexplicit

Initializes a workspace with a shared workspace.

When we access a Blob, we will first try to access the blob that exists in the local workspace, and if not, access the blob that exists in the shared workspace. The caller keeps the ownership of the shared workspace and is responsible for making sure that its lifetime is longer than the created workspace.

Definition at line 76 of file workspace.h.

Member Function Documentation

void caffe2::Workspace::AddBlobMapping ( const Workspace parent,
const std::unordered_map< string, string > &  forwarded_blobs,
bool  skip_defined_blobs = false 
)

Adds blob mappings from workspace to the blobs from parent workspace.

Creates blobs under possibly new names that redirect read/write operations to the blobs in the parent workspace. Arguments: parent - pointer to parent workspace forwarded_blobs - map from new blob name to blob name in parent's workspace skip_defined_blob - if set skips blobs with names that already exist in the workspace, otherwise throws exception

Definition at line 179 of file workspace.cc.

vector< string > caffe2::Workspace::Blobs ( ) const

Return a list of blob names.

This may be a bit slow since it will involve creation of multiple temp variables. For best performance, simply use HasBlob() and GetBlob().

Definition at line 80 of file workspace.cc.

template<class Context >
void caffe2::Workspace::CopyForwardedTensors ( const std::unordered_set< std::string > &  blobs)
inline

Converts previously mapped tensor blobs to local blobs, copies values from parent workspace blobs into new local blobs.

Ignores undefined blobs.

Definition at line 137 of file workspace.h.

Blob * caffe2::Workspace::CreateBlob ( const string &  name)

Creates a blob of the given name.

The pointer to the blob is returned, but the workspace keeps ownership of the pointer. If a blob of the given name already exists, the creation is skipped and the existing blob is returned.

Definition at line 100 of file workspace.cc.

Blob * caffe2::Workspace::CreateLocalBlob ( const string &  name)

Similar to CreateBlob(), but it creates a blob in the local workspace even if another blob with the same name already exists in the parent workspace – in such case the new blob hides the blob in parent workspace.

If a blob of the given name already exists in the local workspace, the creation is skipped and the existing blob is returned.

Definition at line 114 of file workspace.cc.

NetBase * caffe2::Workspace::CreateNet ( const NetDef &  net_def,
bool  overwrite = false 
)

Creates a network with the given NetDef, and returns the pointer to the network.

If there is anything wrong during the creation of the network, a nullptr is returned. The Workspace keeps ownership of the pointer.

If there is already a net created in the workspace with the given name, CreateNet will overwrite it if overwrite=true is specified. Otherwise, an exception is thrown.

Definition at line 214 of file workspace.cc.

template<typename F >
static void caffe2::Workspace::ForEach ( f)
inlinestatic

Applies a function f on each workspace that currently exists.

This function is thread safe and there is no race condition between workspaces being passed to f in this thread and destroyed in another.

Definition at line 302 of file workspace.h.

const Blob * caffe2::Workspace::GetBlob ( const string &  name) const

Gets the blob with the given name as a const pointer.

If the blob does not exist, a nullptr is returned.

Definition at line 160 of file workspace.cc.

Blob * caffe2::Workspace::GetBlob ( const string &  name)

Gets the blob with the given name as a mutable pointer.

If the blob does not exist, a nullptr is returned.

Definition at line 210 of file workspace.cc.

NetBase * caffe2::Workspace::GetNet ( const string &  net_name)

Gets the pointer to a created net.

The workspace keeps ownership of the network.

Definition at line 251 of file workspace.cc.

bool caffe2::Workspace::RemoveBlob ( const string &  name)

Remove the blob of the given name.

Return true if removed and false if not exist. Will NOT remove from the shared workspace.

Definition at line 147 of file workspace.cc.

Blob * caffe2::Workspace::RenameBlob ( const string &  old_name,
const string &  new_name 
)

Renames a local workspace blob.

If blob is not found in the local blob list or if the target name is already present in local or any parent blob list the function will throw.

Definition at line 124 of file workspace.cc.

bool caffe2::Workspace::RunNet ( const string &  net_name)

Finds and runs the instantiated network with the given name.

If the network does not exist or there are errors running the network, the function returns false.

Definition at line 265 of file workspace.cc.


The documentation for this class was generated from the following files: