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
16 #include "tensorflow/compiler/tf2tensorrt/convert/trt_parameters.h"
17
18 #if GOOGLE_CUDA && GOOGLE_TENSORRT
19
20 #include <algorithm>
21 #include <cctype>
22
23 #include "absl/strings/str_cat.h"
24 #include "tensorflow/core/lib/core/errors.h"
25 #include "tensorflow/core/lib/core/status.h"
26 #include "tensorflow/core/platform/errors.h"
27
28 namespace tensorflow {
29 namespace tensorrt {
30
TrtPrecisionModeToName(const TrtPrecisionMode mode,string * name)31 Status TrtPrecisionModeToName(const TrtPrecisionMode mode, string* name) {
32 const char* kUnknown = "UNKNOWN";
33 *name = *kUnknown;
34 switch (mode) {
35 case TrtPrecisionMode::FP32:
36 *name = "FP32";
37 break;
38 case TrtPrecisionMode::FP16:
39 *name = "FP16";
40 break;
41 case TrtPrecisionMode::INT8:
42 *name = "INT8";
43 break;
44 }
45 if (name->compare(kUnknown) == 0)
46 return errors::OutOfRange("Unknown precision mode");
47 return OkStatus();
48 }
49
TrtPrecisionModeFromName(const string & name,TrtPrecisionMode * mode)50 Status TrtPrecisionModeFromName(const string& name, TrtPrecisionMode* mode) {
51 if (name == "FP32") {
52 *mode = TrtPrecisionMode::FP32;
53 } else if (name == "FP16") {
54 *mode = TrtPrecisionMode::FP16;
55 } else if (name == "INT8") {
56 *mode = TrtPrecisionMode::INT8;
57 } else {
58 return errors::InvalidArgument("Invalid precision mode name: ", name);
59 }
60 return OkStatus();
61 }
62
DebugString(const TrtPrecisionMode mode)63 string DebugString(const TrtPrecisionMode mode) {
64 string mode_str;
65 TF_CHECK_OK(TrtPrecisionModeToName(mode, &mode_str));
66 return absl::StrCat("TrtPrecisionMode::", mode_str);
67 }
68
ProfileStrategyToName(const ProfileStrategy strategy)69 string ProfileStrategyToName(const ProfileStrategy strategy) {
70 switch (strategy) {
71 case ProfileStrategy::kRange:
72 return "Range";
73 case ProfileStrategy::kOptimal:
74 return "Optimal";
75 case ProfileStrategy::kRangeOptimal:
76 return "Range+Optimal";
77 case ProfileStrategy::kImplicitBatchModeCompatible:
78 return "ImplicitBatchModeCompatible";
79 }
80 return "Unknown";
81 }
82
ProfileStrategyFromName(const string & name,ProfileStrategy * strategy)83 Status ProfileStrategyFromName(const string& name, ProfileStrategy* strategy) {
84 string name_lowercase(name);
85 std::transform(name.begin(), name.end(), name_lowercase.begin(),
86 [](unsigned char c) { return std::tolower(c); });
87 if (name_lowercase == "range") {
88 *strategy = ProfileStrategy::kRange;
89 } else if (name_lowercase == "optimal") {
90 *strategy = ProfileStrategy::kOptimal;
91 } else if (name_lowercase == "range+optimal") {
92 *strategy = ProfileStrategy::kRangeOptimal;
93 } else if (name_lowercase == "implicitbatchmodecompatible") {
94 *strategy = ProfileStrategy::kImplicitBatchModeCompatible;
95 } else {
96 return errors::InvalidArgument("Invalid profile strategy: ", name);
97 }
98 return OkStatus();
99 }
100
101 } // namespace tensorrt
102 } // namespace tensorflow
103
104 #endif // GOOGLE_CUDA && GOOGLE_TENSORRT
105