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 // This header declares classes for the infeed manager and the infeed 17 // buffer that are used by the GPU runtime to transfer buffers into an 18 // executing GPU computation, e.g., to feed data into a while loop. 19 20 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INFEED_MANAGER_H_ 21 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INFEED_MANAGER_H_ 22 23 #include "absl/base/thread_annotations.h" 24 #include "tensorflow/compiler/xla/service/gpu/xfeed_queue.h" 25 #include "tensorflow/compiler/xla/shape_tree.h" 26 #include "tensorflow/compiler/xla/types.h" 27 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 28 29 namespace xla { 30 namespace gpu { 31 32 // TODO(b/30467474) Once GPU infeed implementation settles, consider 33 // folding back the cpu and gpu infeed implementations into a generic 34 // one if possible. 35 // 36 // Current limitations: 37 // * Does not handle multiple devices/replicas. 38 // 39 // * Buffer space on GPU is allocated on every infeed enqueue request, 40 // and it does not handle the case when it runs out of 41 // memory. Potential solution is to pre-allocate a fixed amount of 42 // memory and block when that memory is full. 43 44 // Defines an infeed buffer that is passed to the runtime by 45 // the client. The client manages the memory of the buffer. 46 class InfeedBuffer { 47 public: 48 InfeedBuffer() = default; InfeedBuffer(se::StreamExecutor * executor,int64 length)49 InfeedBuffer(se::StreamExecutor* executor, int64 length) 50 : device_memory_(executor, executor->AllocateArray<uint8>(length)), 51 length_(length) { 52 CHECK(!device_memory_->is_null()); 53 } 54 length()55 int64 length() const { return length_; } 56 device_memory()57 se::DeviceMemoryBase* device_memory() { return device_memory_.ptr(); } 58 59 private: 60 se::ScopedDeviceMemory<uint8> device_memory_; 61 int64 length_; 62 }; 63 64 // Client-side class used to enqueue infeed buffers. 65 class InfeedManager : public XfeedQueue<ShapeTree<InfeedBuffer>> { 66 public: 67 // Returns a cached stream associated with an executor. Allocates a 68 // new stream on the first invocation. On subsequent invocations, if 69 // the cached executor is not the same as the requested executor, 70 // returns null. 71 se::Stream* GetStream(se::StreamExecutor* executor); 72 73 private: 74 // Mutex for serializing the creation of host_to_device_stream_. 75 tensorflow::mutex host_to_device_stream_mu_; 76 77 // Cached host to device stream for queuing infeed data. 78 std::unique_ptr<se::Stream> host_to_device_stream_ 79 ABSL_GUARDED_BY(host_to_device_stream_mu_); 80 81 // Executor that the host_to_device_stream belongs to. Not owned. 82 se::StreamExecutor* host_to_device_executor_ = nullptr; 83 }; 84 85 // Singleton creator-or-accessor: Returns the GPU infeed manager. 86 InfeedManager* GetOrCreateInfeedManager(); 87 88 } // namespace gpu 89 } // namespace xla 90 91 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INFEED_MANAGER_H_ 92