• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tensorflow/compiler/xla/service/gpu/matmul_utils.h"
17 
18 #include <vector>
19 
20 #include "absl/strings/string_view.h"
21 #include "tensorflow/compiler/xla/service/hlo_parser.h"
22 #include "tensorflow/compiler/xla/test.h"
23 #include "tensorflow/core/platform/status_matchers.h"
24 
25 namespace xla {
26 namespace gpu {
27 namespace {
28 
29 using ::tensorflow::testing::IsOkAndHolds;
30 using ::testing::ElementsAre;
31 
TEST(GetNonContractingDimsTest,Valid)32 TEST(GetNonContractingDimsTest, Valid) {
33   Shape shape = ParseShape("f32[1,2,3,4,5,6]").ValueOrDie();
34   EXPECT_THAT(GetNonContractingDims(shape, /*batch_dims=*/{4},
35                                     /*contracting_dims=*/{1, 5}),
36               IsOkAndHolds(ElementsAre(0, 2, 3)));
37 }
38 
39 struct GetBatchRowColumnShapeTestParams {
40   absl::string_view shape;
41   std::vector<int64_t> batch_dims;
42   std::vector<int64_t> row_dims;
43   std::vector<int64_t> col_dims;
44   absl::string_view expected_shape;
45 };
46 
47 using GetBatchRowColumnShapeTest =
48     ::testing::TestWithParam<GetBatchRowColumnShapeTestParams>;
49 
TEST_P(GetBatchRowColumnShapeTest,ValidShape)50 TEST_P(GetBatchRowColumnShapeTest, ValidShape) {
51   const GetBatchRowColumnShapeTestParams& params = GetParam();
52 
53   Shape shape = ParseShape(params.shape).ValueOrDie();
54   EXPECT_THAT(GetBatchRowColumnShape(shape, params.batch_dims, params.row_dims,
55                                      params.col_dims),
56               IsOkAndHolds(ParseShape(params.expected_shape).ValueOrDie()));
57 }
58 
59 INSTANTIATE_TEST_SUITE_P(
60     GetBatchRowColumnShapeTests, GetBatchRowColumnShapeTest,
61     ::testing::ValuesIn<GetBatchRowColumnShapeTestParams>({
62         {"f32[3,4]{1,0}", /*batch_dims=*/{}, /*row_dims=*/{0}, /*col_dims=*/{1},
63          "f32[1,3,4]{2,1,0}"},
64         {"f32[3,4]{0,1}", {}, {0}, {1}, "f32[1,3,4]{1,2,0}"},
65         {"f32[3,4]{1,0}", {}, {1}, {0}, "f32[1,4,3]{1,2,0}"},
66         {"f32[3,4,5]{2,1,0}", {0}, {1}, {2}, "f32[3,4,5]{2,1,0}"},
67         {"f32[3,4,5]{2,1,0}", {2}, {1}, {0}, "f32[5,4,3]{0,1,2}"},
68         {"f32[3,4,5,6,7,8]{5,2,4,1,3,0}",
69          {0, 3},
70          {1, 4},
71          {2, 5},
72          "f32[18,28,40]{2,1,0}"},
73     }));
74 
TEST(GetBatchRowColumnShapeTest,BatchRowsColsInterleaved)75 TEST(GetBatchRowColumnShapeTest, BatchRowsColsInterleaved) {
76   Shape shape = ParseShape("f32[3,4,5,6,7,8]{5,4,3,2,1,0}").ValueOrDie();
77   auto result =
78       GetBatchRowColumnShape(shape, /*batch_dims=*/{0, 3},
79                              /*row_dims=*/{1, 4}, /*col_dims=*/{2, 5});
80   EXPECT_FALSE(result.ok());
81 }
82 
TEST(GetBatchRowColumnShapeTest,WrongPhysicalOrder)83 TEST(GetBatchRowColumnShapeTest, WrongPhysicalOrder) {
84   Shape shape = ParseShape("f32[3,4,5,6]{3,2,0,1}").ValueOrDie();
85   auto result = GetBatchRowColumnShape(shape, /*batch_dims=*/{0, 1},
86                                        /*row_dims=*/{2}, /*col_dims=*/{3});
87   EXPECT_FALSE(result.ok());
88 }
89 
90 using Order = MatrixLayout::Order;
91 
92 struct GetMatrixLayoutTestParams {
93   absl::string_view shape;
94   int64_t batch_size;
95   int64_t num_rows;
96   int64_t num_cols;
97   Order order;
98   int64_t leading_dim_stride;
99   int64_t batch_stride;
100 };
101 
102 using GetMatrixLayoutTest = ::testing::TestWithParam<GetMatrixLayoutTestParams>;
103 
TEST_P(GetMatrixLayoutTest,ValidShape)104 TEST_P(GetMatrixLayoutTest, ValidShape) {
105   const GetMatrixLayoutTestParams& params = GetParam();
106 
107   Shape shape = ParseShape(params.shape).ValueOrDie();
108   MatrixLayout result = MatrixLayout::For(shape).ValueOrDie();
109   EXPECT_EQ(result.batch_size, params.batch_size);
110   EXPECT_EQ(result.num_rows, params.num_rows);
111   EXPECT_EQ(result.num_cols, params.num_cols);
112   EXPECT_EQ(result.order, params.order);
113   EXPECT_EQ(result.leading_dim_stride, params.leading_dim_stride);
114   EXPECT_EQ(result.batch_stride, params.batch_stride);
115 }
116 
117 INSTANTIATE_TEST_SUITE_P(
118     GetMatrixLayoutTests, GetMatrixLayoutTest,
119     ::testing::ValuesIn<GetMatrixLayoutTestParams>({
120         {"f32[3,4,5]{2,1,0}", /*batch_size=*/3, /*num_rows=*/4, /*num_cols=*/5,
121          /*order=*/Order::kRowMajor, /*leading_dim_stride=*/5,
122          /*batch_stride=*/20},
123         {"f32[3,4,5]{1,2,0}", 3, 4, 5, Order::kColumnMajor, 4, 20},
124         {"f32[3,4,5]{2,0,1}", 3, 4, 5, Order::kRowMajor, 15, 5},
125         {"f32[3,4,5]{1,0,2}", 3, 4, 5, Order::kColumnMajor, 12, 4},
126     }));
127 
TEST(GetMatrixLayoutTest,BatchInMostMinorPhysicalDimension)128 TEST(GetMatrixLayoutTest, BatchInMostMinorPhysicalDimension) {
129   Shape shape = ParseShape("f32[3,4,5]{0,2,1}").ValueOrDie();
130   EXPECT_FALSE(MatrixLayout::For(shape).ok());
131 }
132 
133 }  // namespace
134 }  // namespace gpu
135 }  // namespace xla
136