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/compiler/xla/service/gpu/tests/gpu_codegen_test.h"
17 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
18 #include "tensorflow/compiler/xla/service/hlo_parser.h"
19 #include "tensorflow/compiler/xla/service/hlo_query.h"
20 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
21 #include "tensorflow/core/lib/core/status_test_util.h"
22 #include "tensorflow/core/platform/test.h"
23
24 namespace xla {
25 namespace gpu {
26 namespace {
27
28 class GpuSpmdE2ECompileTest : public GpuCodegenTest {};
29
TEST_F(GpuSpmdE2ECompileTest,SinglePartition)30 TEST_F(GpuSpmdE2ECompileTest, SinglePartition) {
31 // Module with "Sharding" custom call and use_spmd_partitioning enabled.
32 const char *const hlo_string = R"(
33 HloModule module
34
35 ENTRY entry {
36 %parameter.3379 = f32[1,1]{1,0} parameter(0)
37 %custom-call.3380 = f32[1,1]{1,0} custom-call(f32[1,1]{1,0} %parameter.3379),
38 custom_call_target="Sharding", sharding={replicated}
39 ROOT %reshape.6032 = f32[] reshape(f32[1,1]{1,0} %custom-call.3380)
40 })";
41
42 HloModuleConfig config;
43 config.set_use_spmd_partitioning(true);
44 auto hlo_module =
45 ParseAndReturnVerifiedModule(hlo_string, config).ValueOrDie();
46
47 // Verify that compilation succeeded.
48 StatusOr<std::unique_ptr<Executable>> executable =
49 CompileToExecutable(std::move(hlo_module));
50 TF_EXPECT_OK(executable.status());
51 }
52
TEST_F(GpuSpmdE2ECompileTest,DotSharding)53 TEST_F(GpuSpmdE2ECompileTest, DotSharding) {
54 const char *const hlo_string = R"(
55 HloModule test
56
57 ENTRY main {
58 %x = bf16[1,1024,12288] parameter(0), sharding={replicated}
59 %W = bf16[3,12288,320,128] parameter(1), sharding={devices=[1,1,2,1]0,1}
60 %T = bf16[3,320,128,1,1024] parameter(2), sharding={devices=[1,2,1,1,1]0,1}
61 // SPMD partitioning should propagate sharding=[1,2,1,1,1] for this dot. And post-partitioning there should not
62 // be any collectives needed to exchange data between the 2 partitions.
63 %dot = bf16[3,320,128,1,1024] dot(bf16[3,12288,320,128] %W, bf16[1,1024,12288] %x), lhs_contracting_dims={1}, rhs_contracting_dims={2}
64 ROOT %r = bf16[3,320,128,1,1024] add(bf16[3,320,128,1,1024] %dot, bf16[3,320,128,1,1024] %T), sharding={devices=[1,2,1,1,1]0,1}
65 })";
66
67 HloModuleConfig config;
68 config.set_use_spmd_partitioning(true);
69 config.set_num_partitions(2);
70 config.set_debug_options(GetDebugOptionsFromFlags());
71 auto hlo_module =
72 ParseAndReturnVerifiedModule(hlo_string, config).ValueOrDie();
73
74 TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> optimized_module,
75 GetOptimizedModule(std::move(hlo_module)));
76
77 // Validate that no collective communication operations are generated in this
78 // module.
79 const bool has_collective_ops = absl::c_any_of(
80 optimized_module->entry_computation()->instructions(),
81 [](const HloInstruction *inst) {
82 return hlo_query::IsCollectiveCommunicationOp(inst->opcode());
83 });
84 EXPECT_FALSE(has_collective_ops);
85 }
86
87 } // namespace
88 } // namespace gpu
89 } // namespace xla
90