xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tpu/kernels/tpu_mesh_state_interface.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 EXPERIMENTAL_BRAIN_TPU_1VM_MINIEXECUTOR_TPU_MESH_STATE_INTERFACE_H_
16 #define EXPERIMENTAL_BRAIN_TPU_1VM_MINIEXECUTOR_TPU_MESH_STATE_INTERFACE_H_
17 
18 #include <string>
19 
20 #include "tensorflow/core/framework/resource_mgr.h"
21 #include "tensorflow/core/protobuf/tpu/compile_metadata.pb.h"
22 #include "tensorflow/core/tpu/tpu_api.h"
23 #include "tensorflow/core/tpu/tpu_ops_c_api.h"
24 
25 namespace tensorflow {
26 
27 class TpuMeshCommonState;
28 
29 namespace tpu {
30 
31 const char kTpuMeshStateInterfaceResourceName[] = "tpu_mesh_common_state";
32 
33 class TpuMeshStateInterface : public tensorflow::ResourceBase {
34  public:
TpuMeshStateInterface(XLA_TpuMeshState * handle)35   explicit TpuMeshStateInterface(XLA_TpuMeshState* handle)
36       : mesh_state_(handle) {
37   }
38 
~TpuMeshStateInterface()39   ~TpuMeshStateInterface() override {
40     if (mesh_state_ != nullptr) {
41       OpsApiFn()->TpuMeshState_FreeFn(mesh_state_);
42     }
43   }
44 
Create()45   static TpuMeshStateInterface* Create() {
46     XLA_TpuMeshState* state = nullptr;
47     if (OpsApiFn()->TpuMeshState_CreateFn != nullptr) {
48       state = OpsApiFn()->TpuMeshState_CreateFn();
49     }
50     return new TpuMeshStateInterface(state);
51   }
52 
data()53   const XLA_TpuMeshState* data() const { return mesh_state_; }
54 
mesh_common_state()55   tensorflow::TpuMeshCommonState* mesh_common_state() const {
56     if (mesh_state_ == nullptr) {
57       return nullptr;
58     }
59     return static_cast<tensorflow::TpuMeshCommonState*>(
60         OpsApiFn()->TpuMeshState_MeshCommonStateFn(mesh_state_));
61   }
62 
63   // Returns whether we should include the device assignment as a static field
64   // to the TPU program. This also determines whether we should include the
65   // device assignment as part of the compilation cache key.
NeedsStaticDeviceAssignment(const TPUCompileMetadataProto & metadata,TpuCoreTypeEnum tpu_core_type)66   bool NeedsStaticDeviceAssignment(
67       const TPUCompileMetadataProto& metadata,
68       TpuCoreTypeEnum tpu_core_type) const {
69     if (mesh_state_ == nullptr) {
70       return false;
71     }
72     // Static device assignment enables XLA to perform certain optimization when
73     // all cores are used in the replicated computation.
74     return metadata.num_cores_per_replica() * metadata.num_replicas() ==
75            OpsApiFn()->TpuTopology_AvailableCoreCountFn(mesh_state_,
76                                                         tpu_core_type);
77   }
78 
DebugString()79   string DebugString() const override { return "TpuMeshStateInterface"; }
80 
81  private:
82   XLA_TpuMeshState* mesh_state_;
83 };
84 
85 }  // namespace tpu
86 }  // namespace tensorflow
87 
88 #endif  // EXPERIMENTAL_BRAIN_TPU_1VM_MINIEXECUTOR_TPU_MESH_STATE_INTERFACE_H_
89