• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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_CORE_COMMON_RUNTIME_PLUGGABLE_DEVICE_PLUGGABLE_DEVICE_CONTEXT_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_PLUGGABLE_DEVICE_PLUGGABLE_DEVICE_CONTEXT_H_
18 
19 #include "tensorflow/core/common_runtime/device.h"
20 #include "tensorflow/core/framework/device_base.h"
21 #include "tensorflow/core/lib/gtl/inlined_vector.h"
22 
23 namespace stream_executor {
24 class Stream;
25 }  // namespace stream_executor
26 
27 namespace tensorflow {
28 
29 class PluggableDeviceContext : public DeviceContext {
30  public:
31   // Does not take ownership of streams.
PluggableDeviceContext(int stream_id,se::Stream * stream,se::Stream * host_to_device_stream,se::Stream * device_to_host_stream,gtl::InlinedVector<se::Stream *,4> device_to_device_stream)32   PluggableDeviceContext(
33       int stream_id, se::Stream* stream, se::Stream* host_to_device_stream,
34       se::Stream* device_to_host_stream,
35       gtl::InlinedVector<se::Stream*, 4> device_to_device_stream)
36       : stream_id_(stream_id),
37         stream_(stream),
38         host_to_device_stream_(host_to_device_stream),
39         device_to_host_stream_(device_to_host_stream),
40         device_to_device_stream_(device_to_device_stream) {}
41 
~PluggableDeviceContext()42   ~PluggableDeviceContext() override {}
43 
stream()44   se::Stream* stream() const override { return stream_; }
host_to_device_stream()45   se::Stream* host_to_device_stream() const { return host_to_device_stream_; }
device_to_host_stream()46   se::Stream* device_to_host_stream() const { return device_to_host_stream_; }
device_to_device_stream(int index)47   se::Stream* device_to_device_stream(int index) const {
48     return device_to_device_stream_[index % device_to_device_stream_.size()];
49   }
stream_id()50   int stream_id() const { return stream_id_; }
51 
52   void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device,
53                              Tensor* device_tensor, StatusCallback done,
54                              bool sync_dst_compute) const override;
55 
56   void CopyDeviceTensorToCPU(const Tensor* device_tensor,
57                              StringPiece tensor_name, Device* device,
58                              Tensor* cpu_tensor, StatusCallback done) override;
59 
60   void CopyTensorInSameDevice(const Tensor* input_tensor, Device* device,
61                               Tensor* output_tensor,
62                               StatusCallback done) const override;
63 
MaintainLifetimeOnStream(const Tensor * t,se::Stream * stream)64   void MaintainLifetimeOnStream(const Tensor* t,
65                                 se::Stream* stream) const override {}
66 
67   Status ThenExecute(Device* device, se::Stream* stream,
68                      std::function<void()> func) override;
69 
70   bool IsPluggableDevice() override;
71 
72  private:
73   int stream_id_;
74   // The default primary stream to use for this context.
75   // All the memory belongs to this stream.
76   se::Stream* stream_;
77   // The stream to use for copying data from host into PluggableDevice.
78   se::Stream* host_to_device_stream_;
79   // The stream to use for copying data from PluggableDevice to host.
80   se::Stream* device_to_host_stream_;
81   // Streams to use for copying data between PluggableDevices.
82   gtl::InlinedVector<se::Stream*, 4> device_to_device_stream_;
83 };
84 
85 }  // namespace tensorflow
86 
87 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_PLUGGABLE_DEVICE_PLUGGABLE_DEVICE_CONTEXT_H_
88