• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utility>
17 
18 #include "tensorflow/compiler/xla/service/gpu/gpu_sanitize_constant_names.h"
19 
20 #include "tensorflow/compiler/xla/service/hlo_matchers.h"
21 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
22 #include "tensorflow/compiler/xla/service/hlo_parser.h"
23 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
24 #include "tensorflow/core/platform/test.h"
25 
26 namespace xla {
27 namespace gpu {
28 namespace {
29 
30 namespace op = xla::testing::opcode_matchers;
31 using SanitizeConstantNamesTest = HloTestBase;
32 
TEST_F(SanitizeConstantNamesTest,InstructionNameWithHyphenSanitized)33 TEST_F(SanitizeConstantNamesTest, InstructionNameWithHyphenSanitized) {
34   const char *const kHloString = R"(
35     HloModule HyphenInInstructionName
36       ENTRY kernelEntry {
37         ROOT equal-to = s32[2]{0} constant({42, 73})
38     })";
39 
40   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
41                           ParseAndReturnVerifiedModule(kHloString));
42 
43   EXPECT_TRUE(GpuSanitizeConstantNames().Run(module.get()).ValueOrDie());
44   HloInstruction *root = module->entry_computation()->root_instruction();
45   EXPECT_EQ(root->name(), "equal_to");
46 }
47 
TEST_F(SanitizeConstantNamesTest,InstructionNameWithDotSanitized)48 TEST_F(SanitizeConstantNamesTest, InstructionNameWithDotSanitized) {
49   const char *const kHloString = R"(
50     HloModule HyphenInInstructionName
51       ENTRY kernelEntry {
52         ROOT equal.to = s32[2]{0} constant({42, 73})
53     })";
54 
55   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
56                           ParseAndReturnVerifiedModule(kHloString));
57 
58   EXPECT_TRUE(GpuSanitizeConstantNames().Run(module.get()).ValueOrDie());
59   HloInstruction *root = module->entry_computation()->root_instruction();
60   EXPECT_EQ(root->name(), "equal_to");
61 }
62 
TEST_F(SanitizeConstantNamesTest,BufferSanitizedNameCollisionResolved)63 TEST_F(SanitizeConstantNamesTest, BufferSanitizedNameCollisionResolved) {
64   const char *const kHloString = R"(
65     HloModule BufferSanitizedName
66       ENTRY kernelEntry {
67       equal.to = s32[2]{0} constant({42, 73})
68       equal-to = s32[2]{0} constant({67, 3})
69       ROOT equal_to = s32[2]{0} add(equal.to, equal-to)
70     })";
71 
72   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
73                           ParseAndReturnVerifiedModule(kHloString));
74 
75   EXPECT_TRUE(GpuSanitizeConstantNames().Run(module.get()).ValueOrDie());
76   EXPECT_THAT(FindInstruction(module.get(), "equal_to_1"), op::Constant());
77   EXPECT_THAT(FindInstruction(module.get(), "equal_to_2"), op::Constant());
78 }
79 
80 }  // namespace
81 }  // namespace gpu
82 }  // namespace xla
83