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/service/gpu/alias_passthrough_params.h"
17
18 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
19 #include "tensorflow/compiler/xla/tests/test_utils.h"
20 #include "tensorflow/core/lib/core/status_test_util.h"
21 #include "tensorflow/core/platform/test.h"
22
23 namespace xla {
24 namespace gpu {
25
26 class AliasPassthroughParamsTest : public HloTestBase {};
27
TEST_F(AliasPassthroughParamsTest,AliasPassThroughParams)28 TEST_F(AliasPassthroughParamsTest, AliasPassThroughParams) {
29 auto module = ParseAndReturnVerifiedModule(R"(
30 HloModule TestModule
31
32 ENTRY TestComputation {
33 p0 = f16[2048,1024] parameter(0)
34 p1 = f16[2048,1024] parameter(1)
35 sum = f16[2048,1024] add(p0, p1)
36 ROOT root = (f16[2048,1024], f16[2048,1024], f16[2048,1024]) tuple(p0, sum, p1)
37 })")
38 .ValueOrDie();
39 EXPECT_TRUE(AliasPassthroughParams().Run(module.get()).ValueOrDie());
40 const auto& alias_config = module->input_output_alias_config();
41 EXPECT_EQ(0, alias_config.GetAliasedParameter({0})->parameter_number);
42 EXPECT_FALSE(alias_config.OutputHasAlias({1}));
43 EXPECT_EQ(1, alias_config.GetAliasedParameter({2})->parameter_number);
44 }
45
TEST_F(AliasPassthroughParamsTest,DoNotAliasPassThroughParamsMoreThanOnce)46 TEST_F(AliasPassthroughParamsTest, DoNotAliasPassThroughParamsMoreThanOnce) {
47 auto module = ParseAndReturnVerifiedModule(R"(
48 HloModule TestModule
49
50 ENTRY TestComputation {
51 p0 = f16[2048,1024] parameter(0)
52 ROOT root = (f16[2048,1024], f16[2048,1024]) tuple(p0, p0)
53 })")
54 .ValueOrDie();
55 EXPECT_TRUE(AliasPassthroughParams().Run(module.get()).ValueOrDie());
56 const auto& alias_config = module->input_output_alias_config();
57 EXPECT_EQ(0, alias_config.GetAliasedParameter({0})->parameter_number);
58 EXPECT_FALSE(alias_config.OutputHasAlias({1}));
59 }
60
TEST_F(AliasPassthroughParamsTest,PresetAliases)61 TEST_F(AliasPassthroughParamsTest, PresetAliases) {
62 auto module = ParseAndReturnVerifiedModule(R"(
63 HloModule TestModule
64
65 ENTRY TestComputation {
66 p0 = f16[2048,1024] parameter(0)
67 p1 = f16[2048,1024] parameter(1)
68 sum = f16[2048,1024] add(p0, p1)
69 ROOT root = (f16[2048,1024], f16[2048,1024], f16[2048,1024]) tuple(p0, sum, p1)
70 })")
71 .ValueOrDie();
72
73 // Presetting an alias for p0 -> Sum. This could happen in a case of
74 // `alias_resource_update`.
75 auto& preset_alias = module->input_output_alias_config();
76 TF_EXPECT_OK(preset_alias.SetUpAlias(/*output_index=*/{1},
77 /*param_number=*/0,
78 /*param_index=*/{}));
79
80 EXPECT_TRUE(AliasPassthroughParams().Run(module.get()).ValueOrDie());
81 const auto& alias_result = module->input_output_alias_config();
82 // Assert that an alias p1 -> p1 is established by `AliasPassthroughParams`.
83 EXPECT_EQ(1, alias_result.GetAliasedParameter({2})->parameter_number);
84 EXPECT_FALSE(alias_result.OutputHasAlias({0}));
85 }
86
87 } // namespace gpu
88 } // namespace xla
89