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_TRANSFER_MANAGER_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_TRANSFER_MANAGER_H_ 18 19 #include "tensorflow/compiler/xla/literal.h" 20 #include "tensorflow/compiler/xla/service/shaped_buffer.h" 21 #include "tensorflow/compiler/xla/service/transfer_manager.h" 22 #include "tensorflow/compiler/xla/shape.h" 23 #include "tensorflow/stream_executor/stream_executor.h" 24 #include "tensorflow/stream_executor/tpu/tpu_executor_c_api.h" 25 #include "tensorflow/stream_executor/tpu/tpu_transfer_manager_interface.h" 26 27 namespace tensorflow { 28 namespace tpu { 29 30 class TpuTransferManager : public xla::TpuTransferManagerInterface { 31 public: 32 TpuTransferManager(); 33 ~TpuTransferManager() override; 34 35 using Status = stream_executor::port::Status; 36 template <typename T> 37 using StatusOr = stream_executor::port::StatusOr<T>; 38 39 stream_executor::Platform::Id PlatformId() const override; 40 41 xla::Shape HostShapeToDeviceShape( 42 const xla::Shape& host_shape) const override; 43 44 Status TransferLiteralToDeviceAsync( 45 stream_executor::Stream* stream, const xla::LiteralSlice& literal, 46 const xla::ShapedBuffer& device_buffer, 47 const TransferMetadata* transfer_metadata) override; 48 49 void TransferLiteralFromDevice( 50 stream_executor::Stream* stream, const xla::ShapedBuffer& device_buffer, 51 xla::MutableBorrowingLiteral literal, std::function<void(Status)> done, 52 const TransferMetadata* transfer_metadata) override; 53 54 Status TransferLiteralToInfeed(stream_executor::StreamExecutor* executor, 55 const xla::LiteralSlice& literal) override; 56 57 Status TransferLiteralFromOutfeed( 58 stream_executor::StreamExecutor* executor, 59 xla::MutableBorrowingLiteral literal) override; 60 61 Status TransferBuffersToInfeed( 62 se::StreamExecutor* executor, 63 const std::deque<tensorflow::tpu::NoncopyableBuffer>& buffers) override; 64 65 Status ResetDevices( 66 absl::Span<stream_executor::StreamExecutor* const> executor) override; 67 68 int64 GetByteSizeRequirement(const xla::Shape& shape) const override; 69 70 StatusOr<xla::Shape> ChooseCompactLayoutForShape( 71 const xla::Shape& host_shape) const override; 72 73 bool CanShapedBufferBeAccessedNow( 74 stream_executor::StreamExecutor* executor, 75 const xla::ShapedBuffer& device_buffer) const override; 76 77 bool CanBufferBeAccessedNow( 78 se::StreamExecutor* executor, 79 const se::DeviceMemoryBase& device_buffer) const override; 80 81 Status WriteSingleTupleIndexTable( 82 stream_executor::Stream* stream, 83 absl::Span<const stream_executor::DeviceMemoryBase> elements, 84 const xla::Shape& shape, 85 stream_executor::DeviceMemoryBase* region) override; 86 87 Status LinearizeToBuffers( 88 const xla::LiteralSlice& literal, 89 std::deque<tensorflow::tpu::NoncopyableBuffer>* buffers) override; 90 91 private: 92 XLA_TransferManager* manager_; 93 }; 94 95 } // namespace tpu 96 } // namespace tensorflow 97 98 #endif // TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_TRANSFER_MANAGER_H_ 99