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_TPU_EXECUTABLE_INTERFACE_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_EXECUTABLE_INTERFACE_H_ 18 19 #include <memory> 20 #include <vector> 21 22 #include "absl/types/optional.h" 23 #include "absl/types/span.h" 24 #include "tensorflow/compiler/xla/service/executable.h" 25 #include "tensorflow/compiler/xla/service/hlo_execution_profile.h" 26 #include "tensorflow/compiler/xla/service/hlo_input_output_alias_config.h" 27 #include "tensorflow/compiler/xla/service/hlo_module.h" 28 #include "tensorflow/compiler/xla/service/hlo_profile_printer_data.pb.h" 29 #include "tensorflow/compiler/xla/service/service_executable_run_options.h" 30 #include "tensorflow/compiler/xla/shape.h" 31 #include "tensorflow/compiler/xla/status.h" 32 #include "tensorflow/compiler/xla/statusor.h" 33 #include "tensorflow/compiler/xla/types.h" 34 #include "tensorflow/stream_executor/device_memory.h" 35 #include "tensorflow/stream_executor/device_memory_allocator.h" 36 #include "tensorflow/stream_executor/stream_executor.h" 37 38 namespace xla { 39 40 // An executable capable of being fed to a TPU device. 41 class TpuExecutableInterface : public Executable { 42 public: TpuExecutableInterface(std::shared_ptr<HloModule> hlo_module)43 explicit TpuExecutableInterface(std::shared_ptr<HloModule> hlo_module) 44 : Executable(std::move(hlo_module)) {} 45 ~TpuExecutableInterface() override = default; 46 47 StatusOr<ExecutionOutput> ExecuteAsyncOnStream( 48 const ServiceExecutableRunOptions* run_options, 49 std::vector<ExecutionInput> arguments, 50 HloExecutionProfile* hlo_execution_profile) override; 51 52 // Same as AllocateOutputMemory, except that input buffers can be reused 53 // as output buffers. See UserBufferAlias class comment for more details on 54 // the buffer reuse. 55 // 56 // `alias_config` indicates which input and output buffers can be aliased. 57 // 58 // `arguments` are ExecutionInput containing the input parameters. Currently 59 // only a single input parameter (typically a tuple) is supported on TPU. For 60 // each element in the shape tree, if the element holds the ownership of the 61 // memory, it is considered donated and XLA will potentially reuse it as 62 // output buffers. 63 // 64 // The optional 'transfer_stream' parameter enables transfers (for tuple 65 // tables) to be performed on a separate stream to 'stream'. 66 StatusOr<ExecutionOutput> AllocateOutputMemoryWithInputReuse( 67 const Shape& host_shape, const HloInputOutputAliasConfig& alias_config, 68 se::DeviceMemoryAllocator* allocator, 69 std::vector<ExecutionInput>* arguments, se::Stream* stream, 70 se::Stream* transfer_stream = nullptr); 71 72 virtual Status LoadProgramAndEnqueueToStream( 73 const ServiceExecutableRunOptions& run_options, 74 absl::Span<const stream_executor::DeviceMemoryBase> arguments, 75 stream_executor::DeviceMemoryBase result, 76 absl::optional<stream_executor::DeviceMemoryBase> 77 cross_program_prefetch_addr) = 0; 78 79 virtual absl::string_view fingerprint() const = 0; 80 81 protected: 82 virtual Shape HostShapeToDeviceShape(const Shape& host_shape) = 0; 83 84 virtual int64 ShapeSize(const Shape& shape) = 0; 85 }; 86 87 } // namespace xla 88 89 #endif // TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_EXECUTABLE_INTERFACE_H_ 90