1 /* Copyright 2017 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_COMPILER_XLA_SERVICE_GENERIC_TRANSFER_MANAGER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GENERIC_TRANSFER_MANAGER_H_ 18 19 #include <vector> 20 21 #include "tensorflow/compiler/xla/service/transfer_manager.h" 22 #include "tensorflow/compiler/xla/xla_data.pb.h" 23 #include "tensorflow/core/platform/macros.h" 24 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 25 #include "tensorflow/core/platform/types.h" 26 27 namespace xla { 28 29 // A generic implementation of the XLA TransferManager interface 30 // that is the base class for both CPU and GPU. For GPU, it transfers 31 // data between host and device (GPU). For CPU, since the "device" 32 // here is the host itself, there's not much for this transfer manager 33 // to do except memcpy the result. There is a CpuTransferManager that 34 // inherits from GenericTransferManager and handles CPU-specific 35 // infeed. 36 class GenericTransferManager : public TransferManager { 37 public: 38 GenericTransferManager(se::Platform::Id platform_id, size_t pointer_size); ~GenericTransferManager()39 ~GenericTransferManager() override {} 40 41 se::Platform::Id PlatformId() const override; 42 43 void TransferLiteralFromDevice( 44 se::Stream* stream, const ShapedBuffer& device_buffer, 45 MutableBorrowingLiteral literal, std::function<void(Status)> done, 46 const TransferMetadata* transfer_metadata) override; 47 48 Status TransferLiteralToDeviceAsync( 49 se::Stream* stream, const LiteralSlice& literal, 50 const ShapedBuffer& device_buffer, 51 const TransferMetadata* transfer_metadata) override; 52 53 Status TransferLiteralToInfeed(se::StreamExecutor* executor, 54 const LiteralSlice& literal) override; 55 Status TransferLiteralFromOutfeed(se::StreamExecutor* executor, 56 const Shape& literal_shape, 57 MutableBorrowingLiteral literal) override; 58 59 Status ResetDevices(absl::Span<se::StreamExecutor* const> executors) override; 60 61 int64 GetByteSizeRequirement(const Shape& shape) const override; 62 63 protected: 64 Status WriteSingleTupleIndexTable( 65 se::Stream* stream, absl::Span<const se::DeviceMemoryBase> elements, 66 const Shape& shape, se::DeviceMemoryBase* region) override; 67 68 private: 69 Status TransferLiteralFromDeviceInternal(se::StreamExecutor* executor, 70 const ShapedBuffer& device_buffer, 71 MutableBorrowingLiteral literal); 72 73 // The platform this transfer manager targets. 74 const se::Platform::Id platform_id_; 75 76 // The size in bytes of pointers on this platform. 77 const size_t pointer_size_; 78 79 TF_DISALLOW_COPY_AND_ASSIGN(GenericTransferManager); 80 }; 81 82 } // namespace xla 83 84 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GENERIC_TRANSFER_MANAGER_H_ 85