• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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_COMPILER_XLA_CPU_FUNCTION_RUNTIME_H_
17 #define TENSORFLOW_COMPILER_XLA_CPU_FUNCTION_RUNTIME_H_
18 
19 #include "tensorflow/core/platform/types.h"
20 
21 #include <cassert>
22 
23 namespace xla {
24 namespace cpu_function_runtime {
25 // Stores information about one buffer used by an XLA:CPU compiled function.
26 // These buffers are used for holding inputs to the computation, outputs from
27 // the computation and as temporary scratch space.
28 class BufferInfo {
29  public:
30   // Creates a BufferInfo from a serialized encoding generated by `Encode`.
BufferInfo(std::pair<tensorflow::uint64,tensorflow::uint64> encoding)31   explicit BufferInfo(
32       std::pair<tensorflow::uint64, tensorflow::uint64> encoding)
33       : entry_param_number_(encoding.second) {
34     Kind kind;
35     tensorflow::uint64 size;
36     Unpack(encoding.first, &kind, &size);
37     kind_ = kind;
38     size_ = size;
39   }
40 
41   // Returns true if this buffer stores a constant.  These never need to be
42   // allocated by the runtime.
is_constant()43   bool is_constant() const { return kind() == Kind::kConstant; }
44 
45   // Returns true if this buffer stores an entry parameter.  These may or may
46   // not need to be allocated by the runtime, depending on
47   // XlaCompiledCpuFunction::AllocMode.
is_entry_parameter()48   bool is_entry_parameter() const { return kind() == Kind::kEntryParameter; }
49 
50   // Returns the entry parameter number of this buffer.
entry_parameter_number()51   tensorflow::uint64 entry_parameter_number() const {
52     assert(is_entry_parameter());
53     return entry_param_number_;
54   }
55 
56   // Returns true if this buffer is temporary scratch space required by the XLA
57   // computations.  These are always allocated by the runtime.
is_temp_buffer()58   bool is_temp_buffer() const { return kind() == Kind::kTempBuffer; }
59 
60   // Returns true if this buffer is allocated on the C stack or into registers.
61   // These buffers are never allocated by the runtime.
is_on_stack_buffer()62   bool is_on_stack_buffer() const { return kind() == Kind::kOnStackBuffer; }
63 
64   // Returns the size for this buffer.
size()65   tensorflow::uint64 size() const { return size_; }
66 
67   // Encodes this BufferInfo into two 64 bit integers that can be used to
68   // reconstruct the BufferInfo later using the constructor.  We need this
69   // because we use BufferInfo in places where using protocol buffers would
70   // negatively impact binary size.
Encode()71   std::pair<tensorflow::uint64, tensorflow::uint64> Encode() const {
72     static_assert(sizeof(*this) == 16, "");
73     tensorflow::uint64 upper = Pack(kind(), size_);
74     tensorflow::uint64 lower = entry_param_number_;
75     return {upper, lower};
76   }
77 
78   bool operator==(const BufferInfo& buffer_info) const {
79     if (kind() != buffer_info.kind() || size() != buffer_info.size()) {
80       return false;
81     }
82     return !is_entry_parameter() ||
83            entry_parameter_number() == buffer_info.entry_parameter_number();
84   }
85 
86   // Factory methods:
87 
MakeTempBuffer(tensorflow::uint64 size)88   static BufferInfo MakeTempBuffer(tensorflow::uint64 size) {
89     return BufferInfo(Kind::kTempBuffer, /*size=*/size,
90                       /*entry_param_number=*/-1);
91   }
MakeConstant(tensorflow::uint64 size)92   static BufferInfo MakeConstant(tensorflow::uint64 size) {
93     return BufferInfo(Kind::kConstant, /*size=*/size,
94                       /*entry_param_number=*/-1);
95   }
MakeEntryParameter(tensorflow::uint64 size,tensorflow::uint64 param_number)96   static BufferInfo MakeEntryParameter(tensorflow::uint64 size,
97                                        tensorflow::uint64 param_number) {
98     return BufferInfo(Kind::kEntryParameter, /*size=*/size,
99                       /*entry_param_number=*/param_number);
100   }
MakeOnStackBuffer(tensorflow::uint64 size)101   static BufferInfo MakeOnStackBuffer(tensorflow::uint64 size) {
102     return BufferInfo(Kind::kOnStackBuffer, /*size=*/size,
103                       /*entry_param_number=*/-1);
104   }
105 
106  private:
107   BufferInfo() = default;
108 
109   enum class Kind : tensorflow::uint64 {
110     kConstant,
111     kTempBuffer,
112     kEntryParameter,
113     kOnStackBuffer
114   };
115 
kind()116   Kind kind() const { return static_cast<Kind>(kind_); }
117 
BufferInfo(Kind kind,tensorflow::uint64 size,tensorflow::uint64 entry_param_number)118   explicit BufferInfo(Kind kind, tensorflow::uint64 size,
119                       tensorflow::uint64 entry_param_number)
120       : kind_(kind), size_(size), entry_param_number_(entry_param_number) {}
121 
Pack(Kind kind,tensorflow::uint64 size)122   static tensorflow::uint64 Pack(Kind kind, tensorflow::uint64 size) {
123     return (static_cast<tensorflow::uint64>(size) << 2) |
124            static_cast<tensorflow::uint64>(kind);
125   }
126 
Unpack(tensorflow::uint64 packed,Kind * kind,tensorflow::uint64 * size)127   static void Unpack(tensorflow::uint64 packed, Kind* kind,
128                      tensorflow::uint64* size) {
129     *size = packed >> 2;
130     *kind = static_cast<Kind>((packed << 62) >> 62);
131   }
132 
133   Kind kind_ : 2;
134   tensorflow::uint64 size_ : 62;
135   tensorflow::int64 entry_param_number_;
136 };
137 
138 // Align to 64-bytes, to mimic tensorflow::Allocator::kAllocatorAlignment.
139 constexpr size_t kAlign = 64;
140 
141 // When declaring variables that will be passed to an XLA instance as input via
142 // set_arg_data(), be it a regular input or a resource variable in the graph,
143 // the C++ variables must be aligned.
144 //
145 // Example usage:
146 //   XLA_ALIGN std::array<float, 4> arg_x;
147 //   XLA_ALIGN float arg_y;
148 //   xla_instance.set_arg_data(0, arg_x.date());
149 //   xla_instance.set_arg_data(0, &arg_y);
150 #define XLA_ALIGN alignas(xla::cpu_function_runtime::kAlign)
151 
152 // AlignedBufferBytes returns the sum of the size of each buffer in
153 // `buffer_infos`, skipping constants, on-stack buffers and, if
154 // allocate_entry_params is false, entry parameters.  There are `n` entries in
155 // `buffer_infos`.  Each buffer is aligned to kAlign byte boundaries.
156 size_t AlignedBufferBytes(const BufferInfo* buffer_infos, size_t n,
157                           bool allocate_entry_params);
158 
159 // MallocContiguousBuffers allocates buffers for use by the entry point
160 // generated by tfcompile.  There are `n` entries in `buffer_infos`.  If
161 // `annotate_initialized` is set, the allocated memory will be annotated as
162 // having been initialized - this is useful when allocating temporary buffers.
163 // If allocate_entry_params is true then allocates temp buffers and entry
164 // parameters, otherwise allocated only temp buffers.  Slots in `bufs`
165 // corresponding to unallocated buffers are set to nullptr.
166 //
167 // A single contiguous block of memory is allocated, and portions of it are
168 // parceled out into `bufs`, which must have space for `n` entries.  Returns
169 // the head of the allocated contiguous block, which should be passed to
170 // FreeContiguous when the buffers are no longer in use.
171 void* MallocContiguousBuffers(const BufferInfo* buffer_infos, size_t n,
172                               bool allocate_entry_params, void** bufs,
173                               bool annotate_initialized);
174 
175 // FreeContiguous frees the contiguous block of memory allocated by
176 // MallocContiguousBuffers.
177 void FreeContiguous(void* contiguous);
178 }  // namespace cpu_function_runtime
179 }  // namespace xla
180 
181 #endif  // TENSORFLOW_COMPILER_XLA_CPU_FUNCTION_RUNTIME_H_
182