1 #include "caffe2/core/blob_serialization.h" 2 #include "caffe2/core/common.h" 3 #include "caffe2/core/context.h" 4 #include "caffe2/core/tensor_int8.h" 5 #include <c10/util/typeid.h> 6 #include "caffe2/core/types.h" 17 SerializationAcceptor acceptor)
override {
19 const auto& tensor = *
static_cast<const Int8TensorCPU*
>(pointer);
21 blob_proto.set_name(name);
22 blob_proto.set_type(
"Int8TensorCPU");
23 QTensorProto& proto = *blob_proto.mutable_qtensor();
25 for (
int i = 0; i < tensor.t.dim(); ++i) {
26 proto.add_dims(tensor.t.dim32(i));
28 proto.set_precision(8);
29 proto.set_scale(tensor.scale);
30 proto.set_bias(tensor.zero_point);
31 proto.set_is_signed(
false);
33 const TensorProto::DataType data_type =
34 TypeMetaToDataType(tensor.t.dtype());
35 proto.set_data_type(data_type);
37 case TensorProto_DataType_INT32:
38 detail::CopyToProtoAsIs(
40 tensor.t.template data<int32_t>(),
44 case TensorProto_DataType_UINT8:
45 detail::CopyToProtoWithCast(
47 tensor.t.template data<uint8_t>(),
52 CAFFE_ENFORCE(
false,
"Unsupported data type in Int8TensorCPU");
55 acceptor(name, SerializeBlobProtoAsString_EnforceCheck(blob_proto));
64 void Deserialize(
const BlobProto& blob_proto,
Blob* blob)
override {
65 const QTensorProto& proto = blob_proto.qtensor();
66 Int8TensorCPU* tensor = blob->template GetMutable<Int8TensorCPU>();
67 tensor->scale = proto.scale();
68 tensor->zero_point = proto.bias();
70 for (
const int d : proto.dims()) {
73 tensor->t.Resize(dims);
74 switch (proto.data_type()) {
75 case TensorProto_DataType_INT32:
76 detail::CopyFromProtoAsIs(
79 tensor->t.template mutable_data<int32_t>(),
82 case TensorProto_DataType_UINT8:
83 detail::CopyFromProtoWithCast(
86 tensor->t.template mutable_data<uint8_t>(),
90 CAFFE_ENFORCE(
false,
"Unsupported data type in Int8TensorCPU");
101 REGISTER_BLOB_SERIALIZER(
102 (TypeMeta::Id<int8::Int8TensorCPU>()),
Blob is a general container that hosts a typed pointer.
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
TensorDeserializer is the deserializer for Tensors.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
BlobSerializerBase is an abstract class that serializes a blob to a string.