Caffe2 - C++ API
A deep learning, cross platform ML framework
blob_serializer_base.h
1 #pragma once
2 
3 #include <string>
4 #include <functional>
5 
6 #include "c10/util/Registry.h"
7 #include "caffe2/core/common.h"
8 #include "caffe2/proto/caffe2_pb.h"
9 
10 namespace caffe2 {
11 
12 class Blob;
13 
14 constexpr int kDefaultChunkSize = -1;
15 constexpr int kNoChunking = 0;
16 
28  public:
29  virtual ~BlobSerializerBase() {}
30  using SerializationAcceptor =
31  std::function<void(const std::string& blobName, const std::string& data)>;
46  virtual void Serialize(
47  const void* pointer,
48  TypeMeta typeMeta,
49  const std::string& name,
50  SerializationAcceptor acceptor) = 0;
51 
52  virtual void SerializeWithChunkSize(
53  const void* pointer,
54  TypeMeta typeMeta,
55  const std::string& name,
56  SerializationAcceptor acceptor,
57  int /*chunk_size*/) {
58  // Base implementation.
59  Serialize(pointer, typeMeta, name, acceptor);
60  }
61 };
62 
63 // The Blob serialization registry and serializer creator functions.
64 C10_DECLARE_TYPED_REGISTRY(
65  BlobSerializerRegistry,
68  std::unique_ptr);
69 #define REGISTER_BLOB_SERIALIZER(id, ...) \
70  C10_REGISTER_TYPED_CLASS(BlobSerializerRegistry, id, __VA_ARGS__)
71 // Creates an operator with the given operator definition.
72 inline unique_ptr<BlobSerializerBase> CreateSerializer(TypeIdentifier id) {
73  return BlobSerializerRegistry()->Create(id);
74 }
75 
76 
81 class CAFFE2_API BlobDeserializerBase {
82  public:
83  virtual ~BlobDeserializerBase() {}
84 
85  // Deserializes from a BlobProto object.
86  virtual void Deserialize(const BlobProto& proto, Blob* blob) = 0;
87 };
88 
89 C10_DECLARE_REGISTRY(BlobDeserializerRegistry, BlobDeserializerBase);
90 #define REGISTER_BLOB_DESERIALIZER(name, ...) \
91  C10_REGISTER_CLASS(BlobDeserializerRegistry, name, __VA_ARGS__)
92 // Creates an operator with the given operator definition.
93 inline unique_ptr<BlobDeserializerBase> CreateDeserializer(const string& type) {
94  return BlobDeserializerRegistry()->Create(type);
95 }
96 
97 
98 } // namespace caffe2
Blob is a general container that hosts a typed pointer.
Definition: blob.h:24
BlobDeserializerBase is an abstract class that deserializes a blob from a BlobProto or a TensorProto...
A type id is a unique id for a given C++ type.
Definition: typeid.h:60
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:324
BlobSerializerBase is an abstract class that serializes a blob to a string.
virtual void Serialize(const void *pointer, TypeMeta typeMeta, const std::string &name, SerializationAcceptor acceptor)=0
The virtual function that returns a serialized string for the input blob.