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");
15 caffe2_max_tensor_serializer_threads,
17 "Maximal number of threads that can be used for tensor serialization");
20 caffe2_serialize_fp16_as_bytes,
22 "Serialize FLOAT16 tensors using byte_data field");
42 SerializationAcceptor acceptor)
override {
43 CAFFE_ENFORCE(blob.
IsType<std::string>());
46 blob_proto.set_name(name);
47 blob_proto.set_type(
"std::string");
48 blob_proto.set_content(blob.template Get<std::string>());
49 acceptor(name, blob_proto.SerializeAsString());
59 void Deserialize(
const BlobProto& proto,
Blob* blob)
override {
60 *blob->
GetMutable<std::string>() = proto.content();
67 BlobSerializerBase::SerializationAcceptor acceptor,
68 int chunk_size)
const {
69 std::unique_ptr<BlobSerializerBase> serializer(CreateSerializer(meta_.id()));
70 CAFFE_ENFORCE(serializer,
"No known serializer for ", meta_.name());
71 serializer->SerializeWithChunkSize(*
this, name, acceptor, chunk_size);
77 BlobSerializerBase::SerializationAcceptor acceptor = [&data](
78 const std::string&,
const std::string& blob) {
82 this->
Serialize(name, acceptor, kNoChunking);
93 CAFFE_DEFINE_TYPED_REGISTRY(
94 BlobSerializerRegistry,
102 BlobProto blob_proto;
104 blob_proto.ParseFromString(content),
105 "Cannot parse content into a BlobProto.");
106 Deserialize(blob_proto);
110 if (blob_proto.type() == kTensorBlobType) {
113 auto deserializer = CreateDeserializer(
115 DeviceTypeName(blob_proto.tensor().device_detail().device_type()));
118 CAFFE_ENFORCE(deserializer.get());
119 deserializer->Deserialize(blob_proto,
this);
121 auto deserializer = CreateDeserializer(blob_proto.type());
124 "No registered deserializer for type ",
126 deserializer->Deserialize(blob_proto,
this);
132 REGISTER_BLOB_SERIALIZER(
133 (TypeMeta::Id<TensorCPU>()),
Blob is a general container that hosts a typed pointer.
TensorSerializer is the serializer for Tensors.
BlobDeserializerBase is an abstract class that deserializes a blob from a BlobProto or a TensorProto...
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.
TensorDeserializer is the deserializer for Tensors.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
T * GetMutable(bool *is_new_object=nullptr)
Gets a mutable pointer to the stored object.
void Deserialize(const string &content)
Deserializes from a string containing either BlobProto or TensorProto.
bool IsType() const
Checks if the content stored in the blob is of type T.
void Serialize(const Blob &blob, const string &name, SerializationAcceptor acceptor) override
Serializes a Blob.
StringDeserializer is the deserializer for Strings.
BlobSerializerBase is an abstract class that serializes a blob to a string.