xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/infeed_manager.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/literal.h"
25 #include "tensorflow/compiler/xla/service/gpu/xfeed_queue.h"
26 #include "tensorflow/compiler/xla/shape_tree.h"
27 #include "tensorflow/compiler/xla/types.h"
28 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
29 
30 namespace xla {
31 namespace gpu {
32 
33 // TODO(b/30467474) Once GPU infeed implementation settles, consider
34 // folding back the cpu and gpu infeed implementations into a generic
35 // one if possible.
36 //
37 // Current limitations:
38 // * Does not handle multiple devices/replicas.
39 //
40 // * Buffer space on GPU is allocated on every infeed enqueue request,
41 // and it does not handle the case when it runs out of
42 // memory. Potential solution is to pre-allocate a fixed amount of
43 // memory and block when that memory is full.
44 
45 // Client-side class used to enqueue infeed buffers.
46 class InfeedManager
47     : public BlockingXfeedQueue<ShapeTree<se::ScopedDeviceMemory<uint8_t>>> {
48  public:
49   explicit InfeedManager(se::StreamExecutor* executor);
50 
51   Status TransferLiteralToInfeed(se::StreamExecutor* executor,
52                                  const LiteralSlice& literal);
53 
54  private:
stream()55   se::Stream* stream() const { return stream_.get(); }
56 
57   // Stream used to enqueue infeed device copies.
58   std::unique_ptr<se::Stream> stream_;
59 };
60 
61 // Returns the GPU infeed manager for the given stream executor,
62 InfeedManager* GetOrCreateInfeedManager(se::StreamExecutor* executor);
63 
64 }  // namespace gpu
65 }  // namespace xla
66 
67 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INFEED_MANAGER_H_
68