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 | |
16 | #include "tensorflow/core/platform/enable_tf2_utils.h" |
17 | |
18 | #include <atomic> |
19 | |
20 | #include "tensorflow/core/util/env_var.h" |
21 | |
22 | namespace tensorflow { |
23 | |
24 | enum Enablement : uint8 { kFalse = 0, kTrue = 1, undefined = 2 }; |
25 | |
26 | // If this flag is set, we will use it as a signal to decide on whether to |
27 | // use the MLIR based TF-XLA bridge. |
28 | static std::atomic<Enablement> tf2_enabled{undefined}; |
29 | |
30 | // Determine whether or not the user has explicitly asked for tf2 execution. |
31 | // Will be used to determine whether to use the MLIR based bridge. |
32 | void set_tf2_execution(bool enabled) { |
33 | tf2_enabled = (enabled) ? Enablement::kTrue : Enablement::kFalse; |
34 | } |
35 | |
36 | bool tf2_execution_enabled() { |
37 | if (tf2_enabled == Enablement::undefined) { |
38 | static bool tf2_behavior_env_enabled = [] { |
39 | string tf2_env; |
40 | TF_CHECK_OK(ReadStringFromEnvVar("TF2_BEHAVIOR", "0", &tf2_env)); |
41 | return tf2_env != "0"; |
42 | }(); |
43 | tf2_enabled = |
44 | (tf2_behavior_env_enabled) ? Enablement::kTrue : Enablement::kFalse; |
45 | } |
46 | return tf2_enabled; |
47 | } |
48 | |
49 | } // namespace tensorflow |
50 |