diff options
author | 2018-07-10 15:01:20 -0700 | |
---|---|---|
committer | 2018-07-10 15:14:00 -0700 | |
commit | 0e75ab944932e562c387ffca2e19bd347ca63882 (patch) | |
tree | d37177512db666d4eea9fa1c6088bb684f31dbf4 /tensorflow | |
parent | d4cca3613d771b336966bb9e72b2debeb8fcc17f (diff) |
Add tf_enable_prng_ops_gpu flag to allow optionally enabling RandomUniform op on GPU.
PiperOrigin-RevId: 204018791
Diffstat (limited to 'tensorflow')
4 files changed, 123 insertions, 2 deletions
diff --git a/tensorflow/compiler/tf2xla/BUILD b/tensorflow/compiler/tf2xla/BUILD index fd31c26544..ff002d15b0 100644 --- a/tensorflow/compiler/tf2xla/BUILD +++ b/tensorflow/compiler/tf2xla/BUILD @@ -139,12 +139,14 @@ cc_library( "xla_op_registry.cc", "xla_resource.cc", "xla_cpu_backend.cc", + "legacy_flags/backend_registration_flags.cc", ] + if_cuda_is_configured([ "xla_gpu_backend.cc", ]), hdrs = [ "const_analysis.h", "graph_compiler.h", + "legacy_flags/backend_registration_flags.h", "xla_compilation_device.h", "xla_compiler.h", "xla_context.h", @@ -175,9 +177,11 @@ cc_library( "//tensorflow/compiler/xla/client/lib:numeric", "//tensorflow/compiler/xla/client/xla_client:xla_builder", "//tensorflow/compiler/xla/client/xla_client:xla_computation", + "//tensorflow/compiler/xla/legacy_flags:parse_flags_from_env", "//tensorflow/core:core_cpu", "//tensorflow/core:core_cpu_internal", "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", "//tensorflow/core:lib", "//tensorflow/core:lib_internal", "//tensorflow/core:protos_all_cc", diff --git a/tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.cc b/tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.cc new file mode 100644 index 0000000000..661505021f --- /dev/null +++ b/tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.cc @@ -0,0 +1,63 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +// Legacy flags for the XLA bridge's backend registration modules. + +#include <mutex> // NOLINT +#include <vector> + +#include "tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.h" +#include "tensorflow/compiler/xla/legacy_flags/parse_flags_from_env.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { +namespace legacy_flags { + +// Pointers to the parsed value of the flags and flag descriptors, initialized +// via flags_init. +static BackendRegistrationFlags* flags; +static std::vector<Flag>* flag_list; +static std::once_flag flags_init; + +// Allocate *flags. Called via call_once(&flags_init,...). +static void AllocateFlags() { + flags = new BackendRegistrationFlags; + flags->tf_enable_prng_ops_gpu = false; + flag_list = new std::vector<Flag>({ + Flag("tf_enable_prng_ops_gpu", &flags->tf_enable_prng_ops_gpu, + "Whether to enable PRNG ops: [RandomStandardNormal | RandomUniform " + "| RandomUniformInt | TruncatedNormal] on GPU."), + }); + xla::legacy_flags::ParseFlagsFromEnv(*flag_list); +} + +// Append to *append_to flag definitions associated with the XLA bridge's +// backend registration modules. +void AppendBackendRegistrationFlags(std::vector<Flag>* append_to) { + std::call_once(flags_init, &AllocateFlags); + append_to->insert(append_to->end(), flag_list->begin(), flag_list->end()); +} + +// Return a pointer to the BackendRegistrationFlags struct; +// repeated calls return the same pointer. +// This should be called only after Flags::Parse() has returned. +BackendRegistrationFlags* GetBackendRegistrationFlags() { + std::call_once(flags_init, &AllocateFlags); + return flags; +} + +} // namespace legacy_flags +} // namespace tensorflow diff --git a/tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.h b/tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.h new file mode 100644 index 0000000000..861c923dd5 --- /dev/null +++ b/tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.h @@ -0,0 +1,49 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_TF2XLA_LEGACY_FLAGS_BACKEND_REGISTRATION_FLAGS_H_ +#define TENSORFLOW_COMPILER_TF2XLA_LEGACY_FLAGS_BACKEND_REGISTRATION_FLAGS_H_ + +// Legacy flags for the XLA bridge's backend registration modules. + +#include <vector> + +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { +namespace legacy_flags { + +// Append to *flag_list flag definitions associated with the XLA bridge's +// backend registration modules. +void AppendBackendRegistrationFlags(std::vector<tensorflow::Flag>* append_to); + +// The values of flags associated with the XLA bridge's backend registration +// module. +typedef struct { + // Whether to enable RandomUniform op on GPU backend. + // TODO (b/32333178): Remove this flag or set its default to true. + bool tf_enable_prng_ops_gpu; +} BackendRegistrationFlags; + +// Return a pointer to the BackendRegistrationFlags struct; +// repeated calls return the same pointer. +// This should be called only after Flags::Parse() has returned. +BackendRegistrationFlags* GetBackendRegistrationFlags(); + +} // namespace legacy_flags +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_TF2XLA_LEGACY_FLAGS_BACKEND_REGISTRATION_FLAGS_H_ diff --git a/tensorflow/compiler/tf2xla/xla_gpu_backend.cc b/tensorflow/compiler/tf2xla/xla_gpu_backend.cc index 62168b6483..3217db3e73 100644 --- a/tensorflow/compiler/tf2xla/xla_gpu_backend.cc +++ b/tensorflow/compiler/tf2xla/xla_gpu_backend.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ +#include "tensorflow/compiler/tf2xla/legacy_flags/backend_registration_flags.h" #include "tensorflow/compiler/tf2xla/tf2xla_util.h" #include "tensorflow/compiler/tf2xla/xla_op_registry.h" #include "tensorflow/core/framework/kernel_def.pb.h" @@ -22,8 +23,12 @@ namespace tensorflow { bool GpuOpFilter(KernelDef* kdef) { // TODO(b/31361304): The GPU backend does not parallelize PRNG ops, leading to // slow code. - if (kdef->op() == "RandomStandardNormal" || kdef->op() == "RandomUniform" || - kdef->op() == "RandomUniformInt" || kdef->op() == "TruncatedNormal") { + legacy_flags::BackendRegistrationFlags* flags = + legacy_flags::GetBackendRegistrationFlags(); + VLOG(2) << "flags->tf_enable_prng_ops_gpu: " << flags->tf_enable_prng_ops_gpu; + if (!flags->tf_enable_prng_ops_gpu && + (kdef->op() == "RandomStandardNormal" || kdef->op() == "RandomUniform" || + kdef->op() == "RandomUniformInt" || kdef->op() == "TruncatedNormal")) { return false; } if (kdef->op() == "Const") { |