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_EMBEDDING_ENGINE_STATE_INTERFACE_H_ 16 #define TENSORFLOW_CORE_TPU_KERNELS_TPU_EMBEDDING_ENGINE_STATE_INTERFACE_H_ 17 18 #include <string> 19 20 #include "tensorflow/core/framework/resource_mgr.h" 21 #include "tensorflow/core/tpu/tpu_api.h" 22 #include "tensorflow/core/tpu/tpu_ops_c_api.h" 23 24 namespace tensorflow { 25 26 class TpuEmbeddingEngineState; 27 28 namespace tpu { 29 30 const char kTpuEmbeddingEngineStateInterfaceResourceName[] = 31 "tpu_embedding_engine_state"; 32 33 class TpuEmbeddingEngineStateInterface : public ResourceBase { 34 public: TpuEmbeddingEngineStateInterface(XLA_TpuEmbeddingEngineState * handle)35 explicit TpuEmbeddingEngineStateInterface(XLA_TpuEmbeddingEngineState* handle) 36 : engine_state_(handle) {} 37 ~TpuEmbeddingEngineStateInterface()38 ~TpuEmbeddingEngineStateInterface() override { 39 if (engine_state_ != nullptr) { 40 OpsApiFn()->TpuEmbeddingEngineState_FreeFn(engine_state_); 41 } 42 } 43 GetState()44 tensorflow::TpuEmbeddingEngineState* GetState() const { 45 if (engine_state_ == nullptr) { 46 return nullptr; 47 } 48 return static_cast<tensorflow::TpuEmbeddingEngineState*>( 49 OpsApiFn()->TpuEmbeddingEngineState_GetStateFn(engine_state_)); 50 } 51 Create()52 static TpuEmbeddingEngineStateInterface* Create() { 53 XLA_TpuEmbeddingEngineState* state = nullptr; 54 if (OpsApiFn()->TpuEmbeddingEngineState_CreateFn != nullptr) { 55 state = OpsApiFn()->TpuEmbeddingEngineState_CreateFn(); 56 } 57 return new TpuEmbeddingEngineStateInterface(state); 58 } 59 DebugString()60 string DebugString() const override { 61 return "TpuEmbeddingEngineStateInterface"; 62 } 63 64 private: 65 XLA_TpuEmbeddingEngineState* engine_state_; 66 }; 67 68 } // namespace tpu 69 } // namespace tensorflow 70 71 #endif // TENSORFLOW_CORE_TPU_KERNELS_TPU_EMBEDDING_ENGINE_STATE_INTERFACE_H_ 72