|
| 1 | +/* Copyright 2025 The OpenXLA Authors. |
| 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 "xla/service/gpu/launch_dimensions.h" |
| 17 | + |
| 18 | +#include <gmock/gmock.h> |
| 19 | +#include <gtest/gtest.h> |
| 20 | +#include "xla/stream_executor/launch_dim.h" |
| 21 | +#include "xla/tsl/platform/status_matchers.h" |
| 22 | +#include "xla/tsl/platform/statusor.h" |
| 23 | +#include "xla/tsl/util/proto/proto_matchers.h" |
| 24 | +#include "tsl/platform/protobuf.h" |
| 25 | + |
| 26 | +namespace xla::gpu { |
| 27 | +namespace { |
| 28 | +using ::tsl::proto_testing::EqualsProto; |
| 29 | +using ::tsl::testing::IsOkAndHolds; |
| 30 | + |
| 31 | +TEST(LaunchDimensionsTest, DefaultConstruction) { |
| 32 | + LaunchDimensions dimensions{}; |
| 33 | + EXPECT_EQ(dimensions.block_counts().x, 1); |
| 34 | + EXPECT_EQ(dimensions.block_counts().y, 1); |
| 35 | + EXPECT_EQ(dimensions.block_counts().z, 1); |
| 36 | + EXPECT_EQ(dimensions.thread_counts_per_block().x, 1); |
| 37 | + EXPECT_EQ(dimensions.thread_counts_per_block().y, 1); |
| 38 | + EXPECT_EQ(dimensions.thread_counts_per_block().z, 1); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(LaunchDimensionsTest, LinearConstruction) { |
| 42 | + constexpr int kNumBlocks = 42; |
| 43 | + constexpr int kNumThreads = 123; |
| 44 | + LaunchDimensions dimensions{kNumBlocks, kNumThreads}; |
| 45 | + EXPECT_EQ(dimensions.block_counts().x, kNumBlocks); |
| 46 | + EXPECT_EQ(dimensions.block_counts().y, 1); |
| 47 | + EXPECT_EQ(dimensions.block_counts().z, 1); |
| 48 | + EXPECT_EQ(dimensions.thread_counts_per_block().x, kNumThreads); |
| 49 | + EXPECT_EQ(dimensions.thread_counts_per_block().y, 1); |
| 50 | + EXPECT_EQ(dimensions.thread_counts_per_block().z, 1); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(LaunchDimensionsTest, ToProto) { |
| 54 | + se::BlockDim block_dimensions{1, 2, 3}; |
| 55 | + se::ThreadDim thread_dimensions{4, 5, 6}; |
| 56 | + LaunchDimensions dimensions{block_dimensions, thread_dimensions}; |
| 57 | + |
| 58 | + LaunchDimensionsProto proto = dimensions.ToProto(); |
| 59 | + EXPECT_THAT(proto, EqualsProto(R"pb( |
| 60 | + block_counts { coordinates { x: 1, y: 2, z: 3 } } |
| 61 | + thread_counts_per_block { coordinates { x: 4 y: 5 z: 6 } } |
| 62 | + )pb")); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(LaunchDimensionsTest, ToAndFromProto) { |
| 66 | + se::BlockDim block_dimensions{10, 20, 30}; |
| 67 | + se::ThreadDim thread_dimensions{44, 55, 66}; |
| 68 | + LaunchDimensions dimensions{block_dimensions, thread_dimensions}; |
| 69 | + |
| 70 | + EXPECT_THAT(LaunchDimensions::FromProto(dimensions.ToProto()), |
| 71 | + IsOkAndHolds(dimensions)); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(LaunchDimensionsTest, FromAndToProto) { |
| 75 | + LaunchDimensionsProto proto; |
| 76 | + ASSERT_TRUE(tsl::protobuf::TextFormat::ParseFromString( |
| 77 | + R"pb( |
| 78 | + block_counts { coordinates { x: 10, y: 20, z: 30 } } |
| 79 | + thread_counts_per_block { coordinates { x: 44 y: 55 z: 66 } } |
| 80 | + )pb", |
| 81 | + &proto)); |
| 82 | + |
| 83 | + TF_ASSERT_OK_AND_ASSIGN(LaunchDimensions launch_dimensions, |
| 84 | + LaunchDimensions::FromProto(proto)); |
| 85 | + EXPECT_THAT(launch_dimensions.ToProto(), EqualsProto(proto)); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace |
| 89 | +} // namespace xla::gpu |
0 commit comments