1 /* Copyright 2022 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 <memory>
17 #include <string>
18 #include <utility>
19
20 #include "tensorflow/compiler/xla/service/cpu/cpu_compiler.h"
21 #include "tensorflow/compiler/xla/service/cpu/test_target_triple_helper.h"
22 #include "tensorflow/compiler/xla/service/cpu/tests/cpu_codegen_test.h"
23 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
24 #include "tensorflow/compiler/xla/service/hlo_parser.h"
25 #include "tensorflow/compiler/xla/service/hlo_query.h"
26 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
27 #include "tensorflow/core/lib/core/status_test_util.h"
28 #include "tensorflow/core/platform/test.h"
29
30 namespace xla {
31 namespace cpu {
32 namespace {
33
34 class CpuSpmdCompileTest : public CpuCodegenTest {};
35
TEST_F(CpuSpmdCompileTest,SinglePartition)36 TEST_F(CpuSpmdCompileTest, SinglePartition) {
37 // Module with "Sharding" custom call and use_spmd_partitioning enabled.
38 const char *const hlo_string = R"(
39 HloModule module
40
41 ENTRY entry {
42 %parameter.3379 = f32[1,1]{1,0} parameter(0)
43 %custom-call.3380 = f32[1,1]{1,0} custom-call(f32[1,1]{1,0} %parameter.3379),
44 custom_call_target="Sharding", sharding={replicated}
45 ROOT %reshape.6032 = f32[] reshape(f32[1,1]{1,0} %custom-call.3380)
46 })";
47
48 HloModuleConfig config;
49 config.set_use_spmd_partitioning(true);
50 auto hlo_module =
51 ParseAndReturnVerifiedModule(hlo_string, config).ValueOrDie();
52
53 // Verify that compilation succeeded.
54 StatusOr<std::unique_ptr<Executable>> executable =
55 CompileToExecutable(std::move(hlo_module));
56 TF_EXPECT_OK(executable.status());
57 }
58
TEST_F(CpuSpmdCompileTest,DotSharding)59 TEST_F(CpuSpmdCompileTest, DotSharding) {
60 const char *const hlo_string = R"(
61 HloModule test
62
63 ENTRY main {
64 %Arg_0.1 = s64[8,6,4]{2,1,0} parameter(0), sharding={devices=[1,2,2]0,1,2,3}
65 %Arg_1.2 = s64[4,2]{1,0} parameter(1), sharding={devices=[2,1,2]0,2,1,3 last_tile_dim_replicate}
66 %dot.3 = s64[8,6,2]{2,1,0} dot(s64[8,6,4]{2,1,0} %Arg_0.1, s64[4,2]{1,0} %Arg_1.2), lhs_contracting_dims={2}, rhs_contracting_dims={0}
67 %tuple.4 = (s64[8,6,2]{2,1,0}) tuple(s64[8,6,2]{2,1,0} %dot.3)
68 ROOT %get-tuple-element.5 = s64[8,6,2]{2,1,0} get-tuple-element((s64[8,6,2]{2,1,0}) %tuple.4), index=0, sharding={devices=[2,1,1,2]0,1,2,3 last_tile_dim_replicate}
69 })";
70
71 HloModuleConfig config;
72 config.set_use_spmd_partitioning(true);
73 config.set_replica_count(4);
74 config.set_num_partitions(4);
75 config.set_debug_options(GetDebugOptionsFromFlags());
76 auto module = ParseAndReturnVerifiedModule(hlo_string, config).ValueOrDie();
77
78 CpuAotCompilationOptions options{
79 /*triple=*/kTargetTripleForHost, /*cpu_name=*/kTargetCpuForHost,
80 /*features=*/"",
81 /*entry_point_name=*/"main",
82 /*relocation_model=*/CpuAotCompilationOptions::RelocationModel::Static};
83
84 std::string filecheck_pattern = R"(
85 CHECK: call void @__xla_cpu_runtime_AllToAll
86 CHECK: call void @__xla_cpu_runtime_AllReduce
87 )";
88
89 CompileAheadOfTimeAndVerifyIr(std::move(module), options, filecheck_pattern,
90 /*match_optimized_ir=*/true);
91 }
92
93 } // namespace
94 } // namespace cpu
95 } // namespace xla
96