1 #ifndef CAFFE2_CORE_BLOB_SERIALIZATION_H_ 2 #define CAFFE2_CORE_BLOB_SERIALIZATION_H_ 7 #include <google/protobuf/repeated_field.h> 9 #include "caffe2/core/blob.h" 10 #include "caffe2/core/blob_serializer_base.h" 11 #include "caffe2/core/tensor.h" 12 #include <c10/util/typeid.h> 13 #include "caffe2/core/types.h" 14 #include "caffe2/utils/simple_queue.h" 16 C10_DECLARE_int(caffe2_tensor_chunk_size);
17 C10_DECLARE_int(caffe2_max_tensor_serializer_threads);
18 C10_DECLARE_bool(caffe2_serialize_fp16_as_bytes);
22 constexpr
auto kTensorBlobType =
"Tensor";
24 constexpr
auto kChunkIdSeparator =
"#%";
35 BlobSerializerBase::SerializationAcceptor acceptor,
36 int chunk_size = kDefaultChunkSize);
48 CAFFE2_API
string SerializeBlob(
const Blob& blob,
const string& name);
78 CAFFE2_API
Tensor EmptyTensorFromProto(
const TensorProto& proto);
98 SerializationAcceptor acceptor)
override;
99 void SerializeWithChunkSize(
103 SerializationAcceptor acceptor,
104 int chunk_size)
override;
115 void StoreDeviceDetail(
const Tensor& input, TensorProto* proto);
116 unique_ptr<BaseContext> context_;
130 void Deserialize(
const BlobProto& proto,
Blob* blob)
override;
140 void DeserializeToTensor(
const TensorProto& proto,
Tensor* tensor);
146 Tensor Deserialize(
const TensorProto& proto);
154 template <
typename SrcType,
typename DstType>
155 inline void CopyToProtoAsIs(
158 google::protobuf::RepeatedField<DstType>* field,
161 sizeof(SrcType) ==
sizeof(DstType),
162 "The source type and dest type cannot be copied as-is. Did " 163 "you mean CopyToProtoWithCast?");
164 field->Reserve(size);
165 for (
size_t i = 0; i < size; ++i) {
168 context->template CopyToCPU<SrcType>(
169 size, src,
reinterpret_cast<SrcType*
>(field->mutable_data()));
171 context->FinishDeviceComputation();
174 template <
typename SrcType,
typename DstType>
175 inline void CopyToProtoWithCast(
178 google::protobuf::RepeatedField<DstType>* field,
182 unique_ptr<SrcType[]> buffer(
new SrcType[size]);
183 context->template CopyToCPU<SrcType>(size, src, buffer.get());
184 context->FinishDeviceComputation();
185 field->Reserve(size);
186 for (
size_t i = 0; i < size; ++i) {
187 field->Add(static_cast<DstType>(buffer[i]));
191 template <
typename SrcType,
typename DstType>
192 inline void CopyFromProtoAsIs(
194 const google::protobuf::RepeatedField<SrcType>& field,
198 sizeof(SrcType) ==
sizeof(DstType),
199 "The source type and dest type cannot be copied as-is. Did " 200 "you mean CopyFromProtoWithCast?");
201 CAFFE_ENFORCE_EQ(size, field.size(),
"Incorrect proto field size.");
202 context->template CopyFromCPU<DstType>(
203 size,
reinterpret_cast<const DstType*
>(field.data()), dst);
206 template <
typename SrcType,
typename DstType>
207 inline void CopyFromProtoWithCast(
209 const google::protobuf::RepeatedField<SrcType>& field,
212 CAFFE_ENFORCE_EQ(size, field.size(),
"Incorrect proto field size.");
215 unique_ptr<DstType[]> buffer(
new DstType[size]);
216 const SrcType* src = field.data();
217 for (
size_t i = 0; i < size; ++i) {
218 buffer[i] =
static_cast<DstType
>(src[i]);
220 context->template CopyFromCPU<DstType>(size, buffer.get(), dst);
232 CAFFE2_API std::string SerializeAsString_EnforceCheck(
233 const google::protobuf::MessageLite&,
234 const char* error_location =
nullptr);
237 inline std::string SerializeBlobProtoAsString_EnforceCheck(
238 const BlobProto& blob) {
239 return SerializeAsString_EnforceCheck(blob, blob.name().c_str());
244 #endif // CAFFE2_CORE_BLOB_SERIALIZATION_H_ 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...
void DeserializeBlob(const string &content, Blob *result)
Deserializes from a string containing either BlobProto or TensorProto.
Virtual interface for the Context class in Caffe2.
TensorDeserializer is the deserializer for Tensors.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void SerializeBlob(const Blob &blob, const string &name, BlobSerializerBase::SerializationAcceptor acceptor, int chunk_size)
Serializes the given blob, if possible.
BlobSerializerBase is an abstract class that serializes a blob to a string.