1 #include "caffe2/core/blob_serialization.h" 6 #include "caffe2/core/blob.h" 7 #include "caffe2/utils/proto_utils.h" 10 caffe2_tensor_chunk_size,
12 "Chunk size to split tensor data into");
32 SerializationAcceptor acceptor)
override {
33 CAFFE_ENFORCE(blob.
IsType<std::string>());
36 blob_proto.set_name(name);
37 blob_proto.set_type(
"std::string");
38 blob_proto.set_content(blob.template Get<std::string>());
39 acceptor(name, blob_proto.SerializeAsString());
49 void Deserialize(
const BlobProto& proto,
Blob* blob)
override {
50 *blob->
GetMutable<std::string>() = proto.content();
57 BlobSerializerBase::SerializationAcceptor acceptor,
58 int chunk_size)
const {
59 std::unique_ptr<BlobSerializerBase> serializer(CreateSerializer(meta_.id()));
60 CAFFE_ENFORCE(serializer,
"No known serializer for ", meta_.name());
61 serializer->SerializeWithChunkSize(*
this, name, acceptor, chunk_size);
67 BlobSerializerBase::SerializationAcceptor acceptor = [&data](
68 const std::string&,
const std::string& blob) {
72 this->
Serialize(name, acceptor, kNoChunking);
82 CAFFE_DEFINE_TYPED_REGISTRY(
83 BlobSerializerRegistry,
92 blob_proto.ParseFromString(content),
93 "Cannot parse content into a BlobProto.");
94 Deserialize(blob_proto);
98 if (blob_proto.type() == kTensorBlobType) {
101 auto deserializer = CreateDeserializer(
103 DeviceTypeName(blob_proto.tensor().device_detail().device_type()));
106 CAFFE_ENFORCE(deserializer.get());
107 deserializer->Deserialize(blob_proto,
this);
109 auto deserializer = CreateDeserializer(blob_proto.type());
112 "No registered deserializer for type ",
114 deserializer->Deserialize(blob_proto,
this);
120 REGISTER_BLOB_SERIALIZER(
121 (TypeMeta::Id<TensorCPU>()),
void Deserialize(const string &content)
Deserializes from a string containing either BlobProto or TensorProto.
BlobDeserializerBase is an abstract class that deserializes a blob from a BlobProto or a TensorProto...
TensorSerializer is the serializer for Tensors.
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information...
void Serialize(const Blob &blob, const string &name, SerializationAcceptor acceptor) override
Serializes a Blob.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
Blob is a general container that hosts a typed pointer.
BlobSerializerBase is an abstract class that serializes a blob to a string.
T * GetMutable(bool *is_new_object=nullptr)
Gets a mutable pointer to the stored object.
StringDeserializer is the deserializer for Strings.
StringSerializer is the serializer for String.
void Serialize(const string &name, BlobSerializerBase::SerializationAcceptor acceptor, int chunk_size=kDefaultChunkSize) const
Serializes the current blob, if possible.
bool IsType() const
Checks if the content stored in the blob is of type T.
TensorDeserializer is the deserializer for Tensors.