xref: /aosp_15_r20/external/tensorflow/tensorflow/core/runtime_fallback/util/type_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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_RUNTIME_FALLBACK_UTIL_TYPE_UTIL_H_
16 #define TENSORFLOW_CORE_RUNTIME_FALLBACK_UTIL_TYPE_UTIL_H_
17 
18 #include "llvm/Support/ErrorHandling.h"
19 #include "tensorflow/core/framework/types.pb.h"
20 #include "tensorflow/core/platform/logging.h"
21 #include "tfrt/dtype/dtype.h"  // from @tf_runtime
22 
23 namespace tensorflow {
24 namespace tfd {
25 
26 // Map tfrt::Dtype to TF_DataType.
GetTfDataType(tfrt::DType dtype)27 inline DataType GetTfDataType(tfrt::DType dtype) {
28   switch (dtype) {
29     case tfrt::DType::Invalid:
30     case tfrt::DType::Unsupported:
31     case tfrt::DType::Resource:
32       DCHECK(false) << "invalid dtype";
33       return DataType::DT_INVALID;
34 #define DTYPE(TFRT_ENUM, DT_ENUM) \
35   case tfrt::DType::TFRT_ENUM:    \
36     return DataType::DT_ENUM;
37 #include "tensorflow/core/runtime_fallback/util/dtype.def"  // NOLINT
38   }
39 }
40 
GetTfrtDtype(DataType dtype)41 inline tfrt::DType GetTfrtDtype(DataType dtype) {
42   switch (dtype) {
43     default:
44       return tfrt::DType(tfrt::DType::Unsupported);
45     case DataType::DT_INVALID:
46       return tfrt::DType();
47     case DataType::DT_RESOURCE:
48       return tfrt::DType(tfrt::DType::Resource);
49 #define DTYPE(TFRT_ENUM, DT_ENUM) \
50   case DataType::DT_ENUM:         \
51     return tfrt::DType(tfrt::DType::TFRT_ENUM);
52 #include "tensorflow/core/runtime_fallback/util/dtype.def"  // NOLINT
53   }
54 }
55 
56 }  // namespace tfd
57 }  // namespace tensorflow
58 
59 #endif  // TENSORFLOW_CORE_RUNTIME_FALLBACK_UTIL_TYPE_UTIL_H_
60