• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_op_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 tensorflow {
29 
TpuOpExecutable(const XLA_TpuProgram * core_program,std::unique_ptr<xla::HloModule> hlo_module,HostCommandHandler host_command_handler)30 TpuOpExecutable::TpuOpExecutable(const XLA_TpuProgram* core_program,
31                                  std::unique_ptr<xla::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 xla::ServiceExecutableRunOptions & run_options,absl::Span<const se::DeviceMemoryBase> arguments,se::DeviceMemoryBase result,absl::optional<se::DeviceMemoryBase> cross_program_prefetch_addr)37 Status TpuOpExecutable::LoadProgramAndEnqueueToStream(
38     const xla::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_t 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     xla::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 = down_cast<tpu::TpuPlatform*>(
75       tpu::TpuPlatformInterface::GetRegisteredPlatform());
76   auto stream = platform->LookupStream(
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   tpu::OpsApiFn()->TpuExecutable_LoadProgramAndEnqueueToStreamFn(&params);
97 
98   if (dev_assign != nullptr) {
99     stream_executor::tpu::SerializedProto_Free(dev_assign_serialized);
100   }
101   delete[] arguments_bases;
102   return status.status();
103 }
104 
HostShapeToDeviceShape(const xla::Shape & host_shape)105 xla::Shape TpuOpExecutable::HostShapeToDeviceShape(
106     const xla::Shape& host_shape) {
107   XLA_Shape c_host_shape;
108   XLA_Shape c_device_shape;
109   ApiConverter::ToC(host_shape, &c_host_shape);
110   tpu::OpsApiFn()->HardwareLayout_HostShapeToDeviceShapeFn(&c_host_shape,
111                                                            &c_device_shape);
112   xla::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 xla::Shape & shape)118 int64 TpuOpExecutable::ShapeSize(const xla::Shape& shape) {
119   XLA_Shape c_shape;
120   ApiConverter::ToC(shape, &c_shape);
121   int64_t size = tpu::OpsApiFn()->HardwareLayout_ShapeSizeFn(&c_shape);
122   ApiConverter::Free(&c_shape);
123   return size;
124 }
125 
fingerprint() const126 absl::string_view TpuOpExecutable::fingerprint() const {
127   // TODO(skye): the fingerprint can be plumbed through via core_program_
128   LOG(FATAL) << "TpuOpExecutable::fingerprint() unimplemented";
129 }
130 
131 }  // namespace tensorflow
132