1 #ifndef CAFFE2_CORE_QTENSOR_SERIALIZATION_H_ 2 #define CAFFE2_CORE_QTENSOR_SERIALIZATION_H_ 4 #include "caffe2/core/blob_serialization.h" 5 #include "caffe2/core/qtensor.h" 9 constexpr
auto kQTensorBlobQType =
"QTensor";
11 template <
class Context>
22 SerializationAcceptor acceptor)
override;
28 template <
class Context>
31 void Deserialize(
const BlobProto& proto,
Blob* blob)
override;
35 template <
class Context>
39 BlobSerializerBase::SerializationAcceptor acceptor) {
40 const auto& qtensor = blob.template Get<QTensor<Context>>();
42 blob_proto.set_name(name);
43 blob_proto.set_type(kQTensorBlobQType);
44 QTensorProto& proto = *blob_proto.mutable_qtensor();
46 for (
int i = 0; i < qtensor.ndim(); ++i) {
47 proto.add_dims(qtensor.dim32(i));
49 proto.set_precision(qtensor.precision());
50 proto.set_scale(qtensor.scale());
51 proto.set_bias(qtensor.bias());
52 proto.set_is_signed(qtensor.is_signed());
53 detail::CopyToProtoWithCast(
54 qtensor.nbytes(), qtensor.data(), proto.mutable_data(), &this->context_);
55 acceptor(name, blob_proto.SerializeAsString());
58 template <
class Context>
60 const BlobProto& blob_proto,
65 template <
class Context>
67 const QTensorProto& proto,
71 for (
const int d : proto.dims()) {
74 qtensor->Resize(dims);
75 qtensor->SetPrecision(proto.precision());
76 qtensor->SetScale(proto.scale());
77 qtensor->SetBias(proto.bias());
78 qtensor->SetSigned(proto.is_signed());
80 detail::CopyFromProtoWithCast(
81 qtensor->nbytes(), proto.data(), qtensor->mutable_data(), &context);
86 #endif // CAFFE2_CORE_QTENSOR_SERIALIZATION_H_ void Serialize(const Blob &blob, const string &name, SerializationAcceptor acceptor) override
Serializes a Blob.
BlobDeserializerBase is an abstract class that deserializes a blob from a BlobProto or a TensorProto...
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.