xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tpu/kernels/tpu_execute_op.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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 #ifndef TENSORFLOW_CORE_TPU_KERNELS_TPU_EXECUTE_OP_H_
16 #define TENSORFLOW_CORE_TPU_KERNELS_TPU_EXECUTE_OP_H_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "tensorflow/core/framework/op.h"
22 #include "tensorflow/core/framework/op_kernel.h"
23 #include "tensorflow/core/platform/macros.h"
24 #include "tensorflow/core/platform/mutex.h"
25 
26 namespace tensorflow {
27 
28 // Op that executes a precompiled TPU computation.
29 class TPUExecuteOp : public AsyncOpKernel {
30  public:
31   explicit TPUExecuteOp(OpKernelConstruction* context);
32   ~TPUExecuteOp() override;
33 
34   AsyncOpKernel* AsAsync() override;
35 
36   void Compute(OpKernelContext* context) override;
37   void ComputeAsync(OpKernelContext* context, DoneCallback done) override;
38 
39  protected:
40   // Used by TPUExecuteAndUpdateVariablesOp to set the fused variable reads and
41   // updates indices in the XLA computation. The two vectors must have the same
42   // size, and a pair of read index and write index represents a variable's
43   // input to the program and its updated value from the program. If the
44   // variable is not updated, use -1 as the output index.
45   std::vector<int> fused_device_var_reads_in_computation_inputs_;
46   std::vector<int> fused_device_var_updates_in_computation_outputs_;
47 
48  private:
49   Status DoWork(OpKernelContext* context);
50 
51   TF_DISALLOW_COPY_AND_ASSIGN(TPUExecuteOp);
52 };
53 
54 // A variant of TPUExecuteOp that contains fused device variable reads and
55 // updates.
56 class TPUExecuteAndUpdateVariablesOp : public TPUExecuteOp {
57  public:
58   explicit TPUExecuteAndUpdateVariablesOp(OpKernelConstruction* context);
59   ~TPUExecuteAndUpdateVariablesOp() override = default;
60 
61  private:
62   TF_DISALLOW_COPY_AND_ASSIGN(TPUExecuteAndUpdateVariablesOp);
63 };
64 
65 }  // namespace tensorflow
66 
67 #endif  // TENSORFLOW_CORE_TPU_KERNELS_TPU_EXECUTE_OP_H_
68