1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_STREAM_EXECUTOR_TPU_PROTO_HELPER_H_
17 #define TENSORFLOW_STREAM_EXECUTOR_TPU_PROTO_HELPER_H_
18
19 #include <cstddef>
20
21 #include "tensorflow/core/platform/logging.h"
22 #include "tensorflow/stream_executor/tpu/c_api_decl.h"
23
24 extern "C" {
25
26 void StreamExecutor_Tpu_FreeSerializedProto(const TpuSerializedProto* proto);
27
28 } // extern "C"
29
30 namespace stream_executor {
31 namespace tpu {
32
33 using SerializedProto = TpuSerializedProto;
34
35 // Serializes a `proto` and put the result in the given `SerializedProtoType*`
36 // argument.
37 //
38 // Users should call SerializedProto_Free on `serialized_proto` afterwards.
39 template <class ProtoType, class SerializedProtoType>
SerializeProto(const ProtoType & proto,SerializedProtoType * serialized_proto)40 inline void SerializeProto(const ProtoType& proto,
41 SerializedProtoType* serialized_proto) {
42 auto size = proto.ByteSizeLong();
43 auto bytes = new char[size];
44 CHECK(proto.SerializeToArray(bytes, size));
45 serialized_proto->size = size;
46 serialized_proto->bytes = bytes;
47 }
48
49 // Serializes a proto and return the result as a SerializedProto value.
50 //
51 // Users should call SerializedProto_Free on the return value afterwards.
52 template <class ProtoType>
SerializeProto(const ProtoType & proto)53 inline SerializedProto SerializeProto(const ProtoType& proto) {
54 SerializedProto serialized_proto;
55 SerializeProto(proto, &serialized_proto);
56 return serialized_proto;
57 }
58
59 // Deserializes a buffer and return the corresponding proto. If the buffer is
60 // empty, return an empty proto.
61 template <class ProtoType, class SerializedProtoType>
DeserializeProto(const SerializedProtoType & serialized_proto)62 inline ProtoType DeserializeProto(const SerializedProtoType& serialized_proto) {
63 ProtoType proto;
64 if (serialized_proto.bytes != nullptr) {
65 CHECK_GT(serialized_proto.size, 0);
66 CHECK(proto.ParseFromArray(serialized_proto.bytes, serialized_proto.size))
67 << "Invalid buffer, failed to deserialize buffer.";
68 }
69 return proto;
70 }
71
72 // Releases the memory allocated for serialized protos.
73 template <class SerializedProtoType>
SerializedProto_Free(const SerializedProtoType & serialized_proto)74 inline void SerializedProto_Free(const SerializedProtoType& serialized_proto) {
75 CHECK_NE(serialized_proto.bytes, nullptr);
76 CHECK_GT(serialized_proto.size, 0);
77 delete[] serialized_proto.bytes;
78 }
79
80 } // namespace tpu
81 } // namespace stream_executor
82
83 #endif // TENSORFLOW_STREAM_EXECUTOR_TPU_PROTO_HELPER_H_
84