| 62 | } // namespace |
| 63 | |
| 64 | Status Encode(const Tensor& tensor, TensorProto* proto) { |
| 65 | if (!tensor.Initialized()) { |
| 66 | return Status::FailedPrecondition("Tensor should be properly initialized"); |
| 67 | } |
| 68 | |
| 69 | proto->set_dtype(static_cast<DataTypeProto>(tensor.Type())); |
| 70 | for (auto& dim : tensor.Shape().Dims()) { |
| 71 | proto->mutable_tensor_shape()->mutable_dims()->Add(dim); |
| 72 | } |
| 73 | |
| 74 | if (tensor.Type() == DataType::kString) { |
| 75 | return EncodeString(tensor, proto); |
| 76 | } |
| 77 | |
| 78 | auto ptr = tensor.Raw<char>(); |
| 79 | auto bytes = tensor.TotalBytes(); |
| 80 | auto content = proto->mutable_tensor_content(); |
| 81 | content->assign(ptr, bytes); |
| 82 | return Status::OK(); |
| 83 | } |
| 84 | |
| 85 | Status Decode(const TensorProto& proto, Tensor* tensor) { |
| 86 | if (!tensor->Initialized()) { |