xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tfrt/utils/error_util.cc (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 #include "tensorflow/core/tfrt/utils/error_util.h"
16 
17 #include "tfrt/host_context/async_value.h"  // from @tf_runtime
18 #include "tfrt/host_context/diagnostic.h"  // from @tf_runtime
19 
20 namespace tfrt {
21 
ConvertTfErrorCodeToTfrtErrorCode(const tensorflow::Status & status)22 tfrt::ErrorCode ConvertTfErrorCodeToTfrtErrorCode(
23     const tensorflow::Status& status) {
24   auto tf_error_code = status.code();
25   switch (tf_error_code) {
26     default:
27       LOG(INFO) << "Unsupported TensorFlow error code: " << status.ToString();
28       return tfrt::ErrorCode::kUnknown;
29 #define ERROR_TYPE(TFRT_ERROR, TF_ERROR) \
30   case tensorflow::error::TF_ERROR:      \
31     return tfrt::ErrorCode::TFRT_ERROR;
32 #include "tensorflow/core/tfrt/utils/error_type.def"  // NOLINT
33   }
34 }
35 
CreateTfErrorStatus(const DecodedDiagnostic & error)36 tensorflow::Status CreateTfErrorStatus(const DecodedDiagnostic& error) {
37   auto tf_error_code = tensorflow::error::Code::UNKNOWN;
38   switch (error.code) {
39     default:
40       tf_error_code = tensorflow::error::Code::INTERNAL;
41       LOG(INFO) << "Unsupported TFRT error code "
42                 << ErrorName(error.code).str();
43       break;
44 #define ERROR_TYPE(TFRT_ERROR, TF_ERROR)               \
45   case tfrt::ErrorCode::TFRT_ERROR:                    \
46     tf_error_code = tensorflow::error::Code::TF_ERROR; \
47     break;
48 #include "tensorflow/core/tfrt/utils/error_type.def"  // NOLINT
49   }
50   return tensorflow::Status(tf_error_code, error.message);
51 }
52 
ToTfStatus(const tfrt::AsyncValue * av)53 tensorflow::Status ToTfStatus(const tfrt::AsyncValue* av) {
54   CHECK(av != nullptr && av->IsAvailable())  // Crash OK
55       << "Expected a ready async value.";
56   if (av->IsError()) {
57     return CreateTfErrorStatus(av->GetError());
58   }
59   return ::tensorflow::OkStatus();
60 }
61 
62 }  // namespace tfrt
63