• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JIT_XLA_DEVICE_CONTEXT_H_
17 #define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_
18 
19 #include <memory>
20 
21 #include "absl/synchronization/mutex.h"
22 #include "tensorflow/compiler/jit/xla_tensor.h"
23 #include "tensorflow/compiler/tf2xla/xla_compiler.h"
24 #include "tensorflow/compiler/xla/client/global_data.h"
25 #include "tensorflow/compiler/xla/client/local_client.h"
26 #include "tensorflow/core/framework/allocator.h"
27 #include "tensorflow/core/framework/device_base.h"
28 #include "tensorflow/core/lib/core/status.h"
29 
30 namespace tensorflow {
31 
32 // The allocator used for Tensors assigned to the XLA device. The allocator
33 // ignores the alignment and size of the request and always returns a new,
34 // empty, XlaTensor.
35 class XlaDeviceAllocator : public Allocator {
36  public:
37   XlaDeviceAllocator(se::StreamExecutor* stream_executor);
38   ~XlaDeviceAllocator() override;
39 
40   string Name() override;
41 
42   void* AllocateRaw(size_t alignment, size_t num_bytes) override;
43   void DeallocateRaw(void* ptr) override;
44   absl::optional<AllocatorStats> GetStats() override;
45   bool ClearStats() override;
46 
47  private:
48   // The stream executor of the device.
49   se::StreamExecutor* stream_executor_;
50 };
51 
52 // Helper class for managing data transfers between host and XLA devices.
53 class XlaDeviceContext : public DeviceContext {
54  public:
55   explicit XlaDeviceContext(
56       std::shared_ptr<se::Stream> compute_stream,
57       std::shared_ptr<se::Stream> host_to_device_stream,
58       std::shared_ptr<se::Stream> device_to_host_stream,
59       std::vector<std::shared_ptr<se::Stream>> device_to_device_streams,
60       xla::LocalClient* client,
61       XlaCompiler::ShapeRepresentationFn shape_representation_fn,
62       thread::ThreadPool* thread_pool, bool use_fast_mem = false);
63 
64   void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device,
65                              Tensor* device_tensor, StatusCallback done,
66                              bool sync_dst_compute) const override;
67   void CopyDeviceTensorToCPU(const Tensor* device_tensor,
68                              absl::string_view tensor_name, Device* device,
69                              Tensor* cpu_tensor, StatusCallback done) override;
70   void CopyTensorInSameDevice(const Tensor* input_tensor, Device* device,
71                               Tensor* output_tensor,
72                               StatusCallback done) const override;
73 
client()74   xla::LocalClient* client() const { return client_; }
stream()75   se::Stream* stream() const override { return stream_.get(); }
host_to_device_stream()76   se::Stream* host_to_device_stream() const {
77     return host_to_device_stream_.get();
78   }
device_to_device_stream(int index)79   se::Stream* device_to_device_stream(int index) const {
80     return device_to_device_streams_.at(index).get();
81   }
transfer_manager()82   xla::TransferManager* transfer_manager() const { return transfer_manager_; }
shape_representation_fn()83   const XlaCompiler::ShapeRepresentationFn& shape_representation_fn() const {
84     return shape_representation_fn_;
85   }
86 
87   // Returns a device-to-device stream, in round-robin fashion.
88   se::Stream* GetDeviceToDeviceStream();
89 
90   Status ThenExecute(Device* device, stream_executor::Stream* stream,
91                      std::function<void()> func) override;
92 
93  private:
UseMultipleStreams()94   bool UseMultipleStreams() const { return stream_ != host_to_device_stream_; }
95 
96   // The main compute stream of the device, used to synchronize the transfer
97   // streams if they are set.
98   std::shared_ptr<se::Stream> stream_;
99   // The stream to use for transferring data from host to device. Can be
100   // idential to stream_, but must not be nullptr.
101   std::shared_ptr<se::Stream> host_to_device_stream_;
102   // The stream to use for transferring data from device to host. Can be
103   // idential to stream_. If nullptr, borrow a stream from backend for each
104   // transfer request to support out-of-order requests.
105   std::shared_ptr<se::Stream> device_to_host_stream_;
106   // Streams to use for transferring data directly between different devices,
107   // e.g., over NVLINK.
108   std::vector<std::shared_ptr<se::Stream>> device_to_device_streams_;
109 
110   // For the underlying memory allocator and XLA's TransferManager.
111   xla::LocalClient* client_;
112   // Transfer manager, for marshalling data to and from the device.
113   xla::TransferManager* transfer_manager_;
114 
115   XlaCompiler::ShapeRepresentationFn shape_representation_fn_;
116 
117   // Thread pool used for running closures
118   thread::ThreadPool* thread_pool_;
119 
120   // Whether uses TPU fast mem or not.
121   bool use_fast_mem_;
122 
123   absl::Mutex mu_;
124   int next_stream_ TF_GUARDED_BY(mu_) = 0;
125 };
126 
127 }  // namespace tensorflow
128 
129 #endif  // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_
130