Caffe2 - C++ API
A deep learning, cross platform ML framework
blob_serialization.cc
1 #include "caffe2/core/blob_serialization.h"
2 
3 #include <sstream>
4 #include <mutex>
5 
6 #include "caffe2/core/blob.h"
7 #include "caffe2/utils/proto_utils.h"
8 
9 CAFFE2_DEFINE_int(
10  caffe2_tensor_chunk_size,
11  1000000,
12  "Chunk size to split tensor data into");
13 
14 namespace caffe2 {
22  public:
23  StringSerializer() {}
24  ~StringSerializer() {}
29  void Serialize(
30  const Blob& blob,
31  const string& name,
32  SerializationAcceptor acceptor) override {
33  CAFFE_ENFORCE(blob.IsType<std::string>());
34 
35  BlobProto blob_proto;
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());
40  }
41 };
42 
48  public:
49  void Deserialize(const BlobProto& proto, Blob* blob) override {
50  *blob->GetMutable<std::string>() = proto.content();
51  }
52 };
53 
54 // The blob serialization member function implementation.
56  const string& name,
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);
62 }
63 
64 // The blob serialization member function implementation.
65 std::string Blob::Serialize(const string& name) const {
66  std::string data;
67  BlobSerializerBase::SerializationAcceptor acceptor = [&data](
68  const std::string&, const std::string& blob) {
69  DCHECK(data.empty()); // should be called once with kNoChunking
70  data = blob;
71  };
72  this->Serialize(name, acceptor, kNoChunking);
73  return data;
74 }
75 
76 // Specialization for StoreDeviceDetail for CPU - nothing needs to be done.
77 template <>
79  const Tensor<CPUContext>& input, TensorProto* proto) {}
80 
81 // The actual serialization registry objects.
82 CAFFE_DEFINE_TYPED_REGISTRY(
83  BlobSerializerRegistry,
84  CaffeTypeId,
86 
87 CAFFE_DEFINE_REGISTRY(BlobDeserializerRegistry, BlobDeserializerBase);
88 
89 void Blob::Deserialize(const string& content) {
90  BlobProto blob_proto;
91  CAFFE_ENFORCE(
92  blob_proto.ParseFromString(content),
93  "Cannot parse content into a BlobProto.");
94  Deserialize(blob_proto);
95 }
96 
97 void Blob::Deserialize(const BlobProto& blob_proto) {
98  if (blob_proto.type() == kTensorBlobType) {
99  // This is a tensor object. Depending on the device type, we will
100  // use the corresponding TensorDeserializer.
101  auto deserializer = CreateDeserializer(
102  "Tensor" +
103  DeviceTypeName(blob_proto.tensor().device_detail().device_type()));
104  // Tensor's deserializer should always be registered, but we will double
105  // check if it is not null anyway.
106  CAFFE_ENFORCE(deserializer.get());
107  deserializer->Deserialize(blob_proto, this);
108  } else {
109  auto deserializer = CreateDeserializer(blob_proto.type());
110  CAFFE_ENFORCE(
111  deserializer.get(),
112  "No registered deserializer for type ",
113  blob_proto.type());
114  deserializer->Deserialize(blob_proto, this);
115  }
116 }
117 
118 namespace {
119 // Serialize TensorCPU.
120 REGISTER_BLOB_SERIALIZER(
121  (TypeMeta::Id<TensorCPU>()),
123 REGISTER_BLOB_DESERIALIZER(TensorCPU, TensorDeserializer<CPUContext>);
124 // Serialize std::string
125 REGISTER_BLOB_SERIALIZER((TypeMeta::Id<std::string>()), StringSerializer);
126 REGISTER_BLOB_DESERIALIZER(std::string, StringDeserializer);
127 } // namespace
128 } // namespace caffe2
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...
Definition: tensor.h:73
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.
Definition: blob.h:25
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.
Definition: blob.h:96
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.
Definition: blob.h:56
TensorDeserializer is the deserializer for Tensors.