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 #include "tensorflow/stream_executor/tpu/tpu_executable.h"
17
18 #include "tensorflow/compiler/xla/xla_data.pb.h"
19 #include "tensorflow/core/platform/casts.h"
20 #include "tensorflow/core/tpu/tpu_api.h"
21 #include "tensorflow/core/tpu/tpu_ops_c_api.h"
22 #include "tensorflow/stream_executor/tpu/c_api_conversions.h"
23 #include "tensorflow/stream_executor/tpu/proto_helper.h"
24 #include "tensorflow/stream_executor/tpu/status_helper.h"
25 #include "tensorflow/stream_executor/tpu/tpu_platform.h"
26 #include "tensorflow/stream_executor/tpu/tpu_platform_interface.h"
27
28 namespace xla {
29
TpuExecutable(const XLA_TpuProgram * core_program,std::unique_ptr<HloModule> hlo_module,HostCommandHandler host_command_handler)30 TpuExecutable::TpuExecutable(const XLA_TpuProgram* core_program,
31 std::unique_ptr<HloModule> hlo_module,
32 HostCommandHandler host_command_handler)
33 : TpuExecutableInterface(std::move(hlo_module)),
34 core_program_(core_program),
35 host_command_handler_(std::move(host_command_handler)) {}
36
LoadProgramAndEnqueueToStream(const ServiceExecutableRunOptions & run_options,absl::Span<const se::DeviceMemoryBase> arguments,se::DeviceMemoryBase result,absl::optional<se::DeviceMemoryBase> cross_program_prefetch_addr)37 Status TpuExecutable::LoadProgramAndEnqueueToStream(
38 const ServiceExecutableRunOptions& run_options,
39 absl::Span<const se::DeviceMemoryBase> arguments,
40 se::DeviceMemoryBase result,
41 absl::optional<se::DeviceMemoryBase> cross_program_prefetch_addr) {
42 SE_DeviceMemoryBase* arguments_bases = nullptr;
43 if (!arguments.empty()) {
44 arguments_bases = new SE_DeviceMemoryBase[arguments.size()];
45 for (int i = 0; i < arguments.size(); i++) {
46 arguments_bases[i] =
47 SE_DeviceMemoryBase{const_cast<void*>(arguments[i].opaque()),
48 arguments[i].size(), arguments[i].payload()};
49 }
50 }
51
52 SE_DeviceMemoryBase result_base{result.opaque(), result.size(),
53 result.payload()};
54 SE_DeviceMemoryBase prefetch_base;
55 if (cross_program_prefetch_addr.has_value()) {
56 prefetch_base = SE_DeviceMemoryBase{cross_program_prefetch_addr->opaque(),
57 cross_program_prefetch_addr->size(),
58 cross_program_prefetch_addr->payload()};
59 }
60 int32 rng_seed = run_options.run_options().rng_seed();
61
62 XLA_DeviceAssignment c_dev_assign{/*bytes=*/nullptr, /*size=*/0};
63 auto dev_assign = run_options.run_options().device_assignment();
64 stream_executor::tpu::SerializedProto dev_assign_serialized;
65 if (dev_assign != nullptr) {
66 DeviceAssignmentProto dev_assign_proto;
67 TF_RETURN_IF_ERROR(dev_assign->Serialize(&dev_assign_proto));
68 dev_assign_serialized =
69 stream_executor::tpu::SerializeProto(dev_assign_proto);
70 c_dev_assign.bytes = dev_assign_serialized.bytes;
71 c_dev_assign.size = dev_assign_serialized.size;
72 }
73
74 auto platform = tensorflow::down_cast<tensorflow::tpu::TpuPlatform*>(
75 tensorflow::tpu::TpuPlatformInterface::GetRegisteredPlatform());
76 auto stream = platform->stream_map()->at(
77 run_options.run_options().stream()->implementation());
78 StatusHelper status;
79
80 TpuExecutable_LoadProgramAndEnqueueToStream_Params params;
81 params.struct_size = TpuExecutable_LoadProgramAndEnqueueToStream_Params_SIZE;
82 params.priv = nullptr;
83 params.program = core_program_;
84 params.arguments = arguments_bases;
85 params.arguments_len = arguments.size();
86 params.result = &result_base;
87 params.has_cross_program_prefetch_addr =
88 cross_program_prefetch_addr.has_value();
89 params.cross_program_prefetch_addr =
90 cross_program_prefetch_addr.has_value() ? &prefetch_base : nullptr;
91 params.rng_seed = rng_seed;
92 params.device_assignment = &c_dev_assign;
93 params.stream = stream;
94 params.status = status.c_status;
95
96 tensorflow::tpu::OpsApiFn()->TpuExecutable_LoadProgramAndEnqueueToStreamFn(
97 ¶ms);
98
99 if (dev_assign != nullptr) {
100 stream_executor::tpu::SerializedProto_Free(dev_assign_serialized);
101 }
102 delete[] arguments_bases;
103 return status.status();
104 }
105
HostShapeToDeviceShape(const Shape & host_shape)106 Shape TpuExecutable::HostShapeToDeviceShape(const Shape& host_shape) {
107 XLA_Shape c_host_shape;
108 XLA_Shape c_device_shape;
109 ApiConverter::ToC(host_shape, &c_host_shape);
110 tensorflow::tpu::OpsApiFn()->HardwareLayout_HostShapeToDeviceShapeFn(
111 &c_host_shape, &c_device_shape);
112 Shape device_shape = ApiConverter::FromC(&c_device_shape);
113 ApiConverter::Free(&c_host_shape);
114 ApiConverter::Free(&c_device_shape);
115 return device_shape;
116 }
117
ShapeSize(const Shape & shape)118 int64 TpuExecutable::ShapeSize(const Shape& shape) {
119 XLA_Shape c_shape;
120 ApiConverter::ToC(shape, &c_shape);
121 int64 size =
122 tensorflow::tpu::OpsApiFn()->HardwareLayout_ShapeSizeFn(&c_shape);
123 ApiConverter::Free(&c_shape);
124 return size;
125 }
126
fingerprint() const127 absl::string_view TpuExecutable::fingerprint() const {
128 // TODO(skye): the fingerprint can be plumbed through via core_program_
129 LOG(FATAL) << "TpuExecutable::fingerprint() unimplemented";
130 }
131
132 } // namespace xla
133