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