xref: /aosp_15_r20/external/tensorflow/tensorflow/core/kernels/gpu_device_array.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2016 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 #ifndef TENSORFLOW_CORE_KERNELS_GPU_DEVICE_ARRAY_H_
16 #define TENSORFLOW_CORE_KERNELS_GPU_DEVICE_ARRAY_H_
17 
18 #if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \
19     (defined(TENSORFLOW_USE_ROCM) && TENSORFLOW_USE_ROCM)
20 
21 #include "tensorflow/core/common_runtime/gpu/gpu_event_mgr.h"
22 #include "tensorflow/core/framework/op_kernel.h"
23 #include "tensorflow/core/framework/tensor_reference.h"
24 #include "tensorflow/core/kernels/gpu_device_array_gpu.h"
25 
26 namespace tensorflow {
27 
28 // Create an array of value on the host, to be sent to kernel using
29 // GpuDeviceArrayStruct.
30 //
31 // Usage:
32 //   int size = ...;
33 //   GpuDeviceArrayOnHost ptrs(context, size);
34 //   OP_REQUIRES_OK(ptrs.Init());
35 //   for (int i = 0; i < size; ++i) {
36 //     ptrs.Set(i, ...);
37 //   }
38 //   OP_REQUIRES_OK(ptrs.Finalize());
39 //   launchKernel(..., ptrs.data, ...);
40 //
41 // ValueType must be memcopyable.
42 template <typename ValueType, int MaxInlineValues = 8>
43 class GpuDeviceArrayOnHost {
44  public:
GpuDeviceArrayOnHost(OpKernelContext * context,int32_t size)45   GpuDeviceArrayOnHost(OpKernelContext* context, int32_t size)
46       : context_(context),
47         total_bytes_(static_cast<int64_t>(size) * sizeof(ValueType)) {
48     data_.size = size;
49   }
50 
Init()51   Status Init() {
52     if (inlined()) {
53       values_ = data_.inline_values;
54       return OkStatus();
55     }
56 
57     // Out-of-line: allocate data that will be memcopied.
58     AllocatorAttributes attr;
59     attr.set_on_host(true);
60     attr.set_gpu_compatible(true);
61     TF_RETURN_IF_ERROR(
62         context_->allocate_temp(DT_INT8, TensorShape{total_bytes_},
63                                 &out_of_line_values_on_host_, attr));
64     values_ = reinterpret_cast<ValueType*>(
65         out_of_line_values_on_host_.flat<int8>().data());
66     return OkStatus();
67   }
68 
Set(int index,ValueType val)69   void Set(int index, ValueType val) {
70     DCHECK(values_);  // ensure Init was called.
71     DCHECK_LT(index, data_.size);
72     *(values_ + index) = val;
73   }
74 
Finalize()75   Status Finalize() {
76     if (inlined()) {
77       return OkStatus();
78     }
79 
80     // Out-of-line - copy pointers to device.
81     auto stream = context_->op_device_context()->stream();
82     TensorReference tensor_ref(out_of_line_values_on_host_);
83     TF_RETURN_IF_ERROR(context_->allocate_temp(
84         DT_INT8, TensorShape{total_bytes_}, &out_of_line_values_on_gpu_));
85     se::DeviceMemoryBase output_values_base{
86         out_of_line_values_on_gpu_.flat<int8>().data(),
87         static_cast<uint64>(total_bytes_)};
88     stream->ThenMemcpy(&output_values_base,
89                        out_of_line_values_on_host_.flat<int8>().data(),
90                        total_bytes_);
91     context_->device()
92         ->tensorflow_accelerator_device_info()
93         ->event_mgr->ThenExecute(stream,
94                                  [tensor_ref]() { tensor_ref.Unref(); });
95     data_.out_of_line_values = reinterpret_cast<ValueType*>(
96         out_of_line_values_on_gpu_.flat<int8>().data());
97     return OkStatus();
98   }
99 
data()100   const GpuDeviceArrayStruct<ValueType, MaxInlineValues>& data() const {
101     // Ensure Finalize is called.
102     DCHECK(inlined() || out_of_line_values_on_gpu_.IsInitialized());
103     return data_;
104   }
105 
106  private:
inlined()107   bool inlined() const { return data_.size <= MaxInlineValues; }
108 
109   OpKernelContext* const context_;
110   const int64_t total_bytes_;  // total size of all pointers.
111   ValueType* values_ = nullptr;
112   GpuDeviceArrayStruct<ValueType, MaxInlineValues> data_;
113 
114   Tensor out_of_line_values_on_host_;
115   Tensor out_of_line_values_on_gpu_;
116 
117   TF_DISALLOW_COPY_AND_ASSIGN(GpuDeviceArrayOnHost);
118 };
119 
120 }  // namespace tensorflow
121 
122 #endif  // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
123 
124 #endif  // TENSORFLOW_CORE_KERNELS_GPU_DEVICE_ARRAY_H_
125