|
| 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/backends/gpu/runtime/select_k_thunk.h" |
| 17 | + |
| 18 | +#include <memory> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include <gmock/gmock.h> |
| 22 | +#include <gtest/gtest.h> |
| 23 | +#include "xla/backends/gpu/runtime/thunk.h" |
| 24 | +#include "xla/backends/gpu/runtime/thunk.pb.h" |
| 25 | +#include "xla/codegen/emitters/kernel_arguments.h" |
| 26 | +#include "xla/hlo/ir/hlo_instruction.h" |
| 27 | +#include "xla/literal_util.h" |
| 28 | +#include "xla/service/buffer_assignment.h" |
| 29 | +#include "xla/shape_util.h" |
| 30 | +#include "xla/tsl/platform/statusor.h" |
| 31 | +#include "xla/tsl/util/proto/proto_matchers.h" |
| 32 | + |
| 33 | +namespace xla::gpu { |
| 34 | +namespace { |
| 35 | + |
| 36 | +using ::tsl::proto_testing::EqualsProto; |
| 37 | + |
| 38 | +TEST(SelectKThunkTest, ToProto) { |
| 39 | + Thunk::ThunkInfo thunk_info; |
| 40 | + thunk_info.profile_annotation = "profile_annotation"; |
| 41 | + thunk_info.execution_stream_id = 123; |
| 42 | + |
| 43 | + BufferAllocation alloc0(/*index=*/0, /*size=*/20, /*color=*/0); |
| 44 | + BufferAllocation::Slice slice0(&alloc0, /*offset=*/0, /*size=*/20); |
| 45 | + |
| 46 | + BufferAllocation alloc1(/*index=*/1, /*size=*/12, /*color=*/0); |
| 47 | + BufferAllocation::Slice slice1(&alloc1, /*offset=*/0, /*size=*/12); |
| 48 | + |
| 49 | + BufferAllocation alloc2(/*index=*/2, /*size=*/12, /*color=*/0); |
| 50 | + BufferAllocation::Slice slice2(&alloc2, /*offset=*/0, /*size=*/12); |
| 51 | + |
| 52 | + emitters::KernelArgument arg0(ShapeUtil::MakeShape(F32, {1, 5}), slice0); |
| 53 | + emitters::KernelArgument arg1(ShapeUtil::MakeShape(F32, {1, 3}), slice1); |
| 54 | + emitters::KernelArgument arg2(ShapeUtil::MakeShape(U32, {1, 3}), slice2); |
| 55 | + arg0.set_written(false); |
| 56 | + arg1.set_written(true); |
| 57 | + arg2.set_written(true); |
| 58 | + |
| 59 | + emitters::KernelArguments kernel_arguments({arg0, arg1, arg2}); |
| 60 | + |
| 61 | + auto c1 = HloInstruction::CreateConstant( |
| 62 | + LiteralUtil::CreateR2<float>({{.125f, 0.875f, .5f, .25f, 0.75f}})); |
| 63 | + auto topKInst = HloInstruction::CreateCustomCall( |
| 64 | + ShapeUtil::MakeShape(F32, {1, 5}), {c1.get()}, "__gpu$TopK"); |
| 65 | + |
| 66 | + SelectKThunk thunk(topKInst.get(), 1, 5, 3, F32, kernel_arguments); |
| 67 | + TF_ASSERT_OK_AND_ASSIGN(ThunkProto proto, thunk.ToProto()); |
| 68 | + EXPECT_THAT(proto, EqualsProto(R"pb( |
| 69 | + thunk_info { profile_annotation: "custom-call" } |
| 70 | + select_k_thunk {} |
| 71 | + )pb")); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
| 75 | +} // namespace xla::gpu |
0 commit comments