Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Types | Public Member Functions
caffe2::Blob Class Reference

Blob is a general container that hosts a typed pointer. More...

#include <blob.h>

Public Types

typedef void(* DestroyCall) (void *)
 

Public Member Functions

 Blob ()
 Initializes an empty Blob.
 
 Blob (Blob &&other) noexcept
 
Bloboperator= (Blob &&other) noexcept
 
template<class T >
bool IsType () const
 Checks if the content stored in the blob is of type T.
 
const TypeMetameta () const
 Returns the meta info of the blob.
 
const char * TypeName () const
 Returns a printable typename of the blob.
 
template<class T >
const T & Get () const
 Gets the const reference of the stored object. More...
 
const void * GetRaw () const
 
void * GetRaw ()
 
template<class T >
T * GetMutable (bool *is_new_object=nullptr)
 Gets a mutable pointer to the stored object. More...
 
template<class T >
T * Reset (T *allocated)
 Sets the underlying object to the allocated one. More...
 
void * Reset (void *allocated, const TypeMeta &meta, const DestroyCall &destroy)
 
DestroyCall Release ()
 Releases the ownership, if any, this Blob has on the underlying pointer. More...
 
template<class T >
std::remove_const< T >::type * ShareExternal (typename std::remove_const< T >::type *allocated)
 Sets the underlying object to the allocated one, but does not take over the ownership of the passed in pointer. More...
 
void * ShareExternal (void *allocated, const TypeMeta &meta)
 
void Reset ()
 Resets the Blob to an empty one.
 
void Serialize (const string &name, BlobSerializerBase::SerializationAcceptor acceptor, int chunk_size=kDefaultChunkSize) const
 Serializes the current blob, if possible. More...
 
string Serialize (const string &name) const
 Convenience function to serialize a blob to a string. More...
 
void swap (Blob &rhs)
 Swaps the underlying storage of two blobs.
 
void Deserialize (const string &content)
 Deserializes from a string containing either BlobProto or TensorProto. More...
 
void Deserialize (const BlobProto &proto)
 

Detailed Description

Blob is a general container that hosts a typed pointer.

A Blob hosts a pointer as well as its type, and takes charge of deleting it properly when the blob is deallocated or re-allocated with a new type. A blob could contain anything, although the most common case is to contain a Tensor.

Definition at line 41 of file blob.h.

Member Function Documentation

void caffe2::Blob::Deserialize ( const string &  content)

Deserializes from a string containing either BlobProto or TensorProto.

If the deserialization fails, the content in the blob should no longer be trusted.

Definition at line 117 of file blob_serialization.cc.

template<class T >
const T& caffe2::Blob::Get ( ) const
inline

Gets the const reference of the stored object.

The code checks if the stored object is of the desired type.

Definition at line 91 of file blob.h.

template<class T >
T* caffe2::Blob::GetMutable ( bool *  is_new_object = nullptr)
inline

Gets a mutable pointer to the stored object.

If the current object is not of the right type, a new object is created and the old object is freed. Note that type T should have a default constructor. Otherwise, create the object yourself first, and use Reset().

Definition at line 117 of file blob.h.

DestroyCall caffe2::Blob::Release ( )
inline

Releases the ownership, if any, this Blob has on the underlying pointer.

The user is then responsible for freeing the data if needed

Definition at line 162 of file blob.h.

template<class T >
T* caffe2::Blob::Reset ( T *  allocated)
inline

Sets the underlying object to the allocated one.

The Blob then takes over the ownership of the passed in pointer. If there is already an object in the Blob, the old object is freed.

This is used when the underlying class T does not have a default ctor, or complex initializations needs to be done outside the blob.

Definition at line 137 of file blob.h.

void caffe2::Blob::Serialize ( const string &  name,
BlobSerializerBase::SerializationAcceptor  acceptor,
int  chunk_size = kDefaultChunkSize 
) const

Serializes the current blob, if possible.

Note that this serialization uses the registration mechanism and one has to implement specific serialization approaches for specific classes. Acceptor should take care of writing data to the actual storage.

Definition at line 81 of file blob_serialization.cc.

std::string caffe2::Blob::Serialize ( const string &  name) const

Convenience function to serialize a blob to a string.

This is a conveinence function to serialize small Blobs that produce manageable serialized strings. To serialize big blobs such as large sparse tensors, use the fully-functional interface in blob_serializer_base.h.

NOTE: this function doesn't do chunking and might break with big tensors.

Definition at line 91 of file blob_serialization.cc.

template<class T >
std::remove_const<T>::type* caffe2::Blob::ShareExternal ( typename std::remove_const< T >::type *  allocated)
inline

Sets the underlying object to the allocated one, but does not take over the ownership of the passed in pointer.

If there is already an object in the Blob, the old object is freed.

Unlike Reset, this does not take over the ownership of the pointer and the caller is responsible for making sure that the lifetime of the allocated blob outlasts the lifetime of any access to this blob, until another Reset call is made or the blob is destructed.

Definition at line 179 of file blob.h.


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