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_UTIL_H_ 16 #define TENSORFLOW_CORE_TPU_KERNELS_TPU_UTIL_H_ 17 18 #include <memory> 19 #include <string> 20 #include <vector> 21 22 #include "grpcpp/server_builder.h" 23 #include "absl/strings/str_cat.h" 24 #include "tensorflow/cc/framework/ops.h" 25 #include "tensorflow/compiler/tf2xla/xla_compiler.h" 26 #include "tensorflow/compiler/xla/client/compile_only_client.h" 27 #include "tensorflow/compiler/xla/statusor.h" 28 #include "tensorflow/core/protobuf/config.pb.h" 29 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_key.h" 30 31 namespace tensorflow { 32 namespace tpu { 33 34 // Utility to get session_name from `SessionMetadata`. `SessionMetadata` may 35 // be null. 36 std::string SessionNameFromMetadata(const SessionMetadata* session_metadata); 37 38 // Generates cache proto key for a given computation on a TPU core. 39 std::string ProtoKeyForComputation(const std::string& key, int core); 40 41 // Returns a TpuCompilationCacheKey parsed from given key or an error. 42 xla::StatusOr<TpuCompilationCacheKey> ParseCompilationCacheKey( 43 const std::string& key); 44 45 xla::CompileOnlyClient::AotXlaComputationInstance 46 BuildAotXlaComputationInstance( 47 const XlaCompiler::CompilationResult& compilation_result); 48 49 // Returns true if TPU compilation is enabled. 50 bool IsTpuCompilationEnabled(); 51 52 // Converts an int64 host memory `tensor` to a `shape`. 53 Status ShapeTensorToTensorShape(const Tensor& tensor, TensorShape* shape); 54 55 Status DynamicShapesToTensorShapes(const OpInputList& dynamic_shapes, 56 std::vector<TensorShape>* shapes); 57 Status DynamicShapesToTensorShapes(const InputList& dynamic_shapes, 58 std::vector<TensorShape>* shapes); 59 60 // Creates gRPC ServerBuilder. 61 xla::StatusOr<std::unique_ptr<::grpc::ServerBuilder>> CreateServerBuilder( 62 int serving_port); 63 } // namespace tpu 64 } // namespace tensorflow 65 66 #endif // TENSORFLOW_CORE_TPU_KERNELS_TPU_UTIL_H_ 67