1 /* Copyright 2019 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/xla/tools/prepare_reference_module.h"
17
18 #include <functional>
19 #include <memory>
20
21 #include "tensorflow/compiler/xla/debug_options_flags.h"
22 #include "tensorflow/compiler/xla/service/despecializer.h"
23 #include "tensorflow/compiler/xla/service/hlo_module.h"
24 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
25 #include "tensorflow/compiler/xla/statusor.h"
26 #include "tensorflow/compiler/xla/xla.pb.h"
27 #include "tensorflow/core/platform/errors.h"
28 #include "tensorflow/stream_executor/lib/status.h"
29 #include "tensorflow/stream_executor/platform.h"
30
31 namespace xla {
32
PrepareReferenceModule(const HloModule & test_module,const::stream_executor::Platform::Id & test_platform_id,const std::function<void (HloModuleConfig *)> & config_modifier_hook,const std::function<Status (const HloModule &,const::stream_executor::Platform::Id &,HloModule *)> & module_modifier_hook)33 StatusOr<std::unique_ptr<HloModule>> PrepareReferenceModule(
34 const HloModule& test_module,
35 const ::stream_executor::Platform::Id& test_platform_id,
36 const std::function<void(HloModuleConfig*)>& config_modifier_hook,
37 const std::function<Status(const HloModule&,
38 const ::stream_executor::Platform::Id&,
39 HloModule*)>& module_modifier_hook) {
40 DebugOptions debug_options = GetDebugOptionsFromFlags();
41 // The combination of fast math and optimizations leads to unsound code
42 // transformations (see third_party/tensorflow/compiler/xla/xla.proto for
43 // details). The test platform should not change this from the default.
44 debug_options.set_xla_cpu_enable_fast_math(false);
45 debug_options.set_xla_gpu_enable_fast_min_max(false);
46 HloModuleConfig reference_config = test_module.config();
47 reference_config.set_debug_options(debug_options);
48 if (config_modifier_hook) {
49 config_modifier_hook(&reference_config);
50 }
51 std::unique_ptr<HloModule> reference_module =
52 test_module.Clone(reference_config, "reference");
53 if (module_modifier_hook) {
54 TF_RETURN_IF_ERROR(module_modifier_hook(test_module, test_platform_id,
55 reference_module.get()));
56 } else {
57 TF_RETURN_IF_ERROR(Despecializer().Run(reference_module.get()).status());
58 }
59 return std::move(reference_module);
60 }
61 }; // namespace xla
62