• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core/util/determinism.h"
17 
18 #include "tensorflow/core/platform/mutex.h"
19 #include "tensorflow/core/util/env_var.h"
20 
21 namespace tensorflow {
22 
23 namespace {
24 
25 enum class DeterminismState { DISABLED, ENABLED, NOT_SET };
26 mutex* determinism_state_mutex = new mutex;
27 DeterminismState determinism_state = DeterminismState::NOT_SET;
28 
29 }  // namespace
30 
OpDeterminismRequired()31 bool OpDeterminismRequired() {
32   mutex_lock l(*determinism_state_mutex);
33 
34   if (determinism_state == DeterminismState::NOT_SET) {
35     bool env_var_set = false;
36     TF_CHECK_OK(tensorflow::ReadBoolFromEnvVar("TF_DETERMINISTIC_OPS",
37                                                /*default_val=*/false,
38                                                &env_var_set));
39     determinism_state =
40         env_var_set ? DeterminismState::ENABLED : DeterminismState::DISABLED;
41   }
42 
43   return determinism_state == DeterminismState::ENABLED;
44 }
45 
EnableOpDeterminism(bool enabled)46 void EnableOpDeterminism(bool enabled) {
47   mutex_lock l(*determinism_state_mutex);
48   determinism_state =
49       enabled ? DeterminismState::ENABLED : DeterminismState::DISABLED;
50 }
51 
52 }  // namespace tensorflow
53