|
| 1 | +/* Copyright 2024 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/cpu/runtime/dot_thunk.h" |
| 17 | + |
| 18 | +#include <cstdint> |
| 19 | + |
| 20 | +#include "xla/backends/cpu/runtime/buffer_allocations.h" |
| 21 | +#include "xla/backends/cpu/runtime/thunk.h" |
| 22 | +#include "xla/backends/cpu/runtime/thunk_testlib.h" |
| 23 | +#include "xla/literal_util.h" |
| 24 | +#include "xla/shape.h" |
| 25 | +#include "xla/shape_util.h" |
| 26 | +#include "xla/tsl/concurrency/async_value_ref.h" |
| 27 | +#include "xla/tsl/platform/statusor.h" |
| 28 | +#include "xla/tsl/platform/test.h" |
| 29 | +#include "xla/tsl/platform/threadpool.h" |
| 30 | + |
| 31 | +#define EIGEN_USE_THREADS |
| 32 | +#include "unsupported/Eigen/CXX11/Tensor" |
| 33 | + |
| 34 | +namespace xla::cpu { |
| 35 | +namespace { |
| 36 | + |
| 37 | +TEST(DotThunkTest, SimpleDot) { |
| 38 | + auto lhs = LiteralUtil::CreateR2<float>({{1.0, 2.0}, {3.0, 4.0}}); |
| 39 | + auto rhs = LiteralUtil::CreateR2<float>({{4.0, 3.0}, {2.0, 1.0}}); |
| 40 | + auto out = LiteralUtil::CreateR2<float>({{0.0, 0.0}, {0.0, 0.0}}); |
| 41 | + |
| 42 | + BufferAllocations allocations = CreateBufferAllocations(lhs, rhs, out); |
| 43 | + |
| 44 | + auto [lhs_alloc, rhs_alloc, out_alloc] = |
| 45 | + CreateBufferAllocation(lhs, rhs, out); |
| 46 | + auto [lhs_slice, rhs_slice, out_slice] = |
| 47 | + CreateBufferAllocationSlice(lhs_alloc, rhs_alloc, out_alloc); |
| 48 | + |
| 49 | + Shape shape = ShapeUtil::MakeShape(F32, {2, 2}); |
| 50 | + |
| 51 | + DotDimensionNumbers dot_dimensions; |
| 52 | + dot_dimensions.add_lhs_contracting_dimensions(1); |
| 53 | + dot_dimensions.add_rhs_contracting_dimensions(0); |
| 54 | + |
| 55 | + TF_ASSERT_OK_AND_ASSIGN( |
| 56 | + auto thunk, DotThunk::Create({"dot"}, dot_dimensions, lhs_slice, shape, |
| 57 | + rhs_slice, shape, out_slice, shape)); |
| 58 | + |
| 59 | + Thunk::ExecuteParams params; |
| 60 | + params.buffer_allocations = &allocations; |
| 61 | + |
| 62 | + auto execute_event = thunk->Execute(params); |
| 63 | + tsl::BlockUntilReady(execute_event); |
| 64 | + ASSERT_FALSE(execute_event.IsError()) << execute_event.GetError(); |
| 65 | + |
| 66 | + EXPECT_EQ(out, LiteralUtil::CreateR2<float>({{8.0, 5.0}, {20.0, 13.0}})); |
| 67 | +} |
| 68 | + |
| 69 | +TEST(DotThunkTest, ThreadedDot) { |
| 70 | + auto shape = ShapeUtil::MakeShape(F32, {1024, 1024}); |
| 71 | + // These aren't very interesting literals, but they should be large enough to |
| 72 | + // trigger multi-threaded execution. |
| 73 | + auto lhs = *LiteralUtil::CreateLiteralWithGenerator<F32, float>( |
| 74 | + shape, [](auto) { return 1.0; }); |
| 75 | + auto rhs = *LiteralUtil::CreateLiteralWithGenerator<F32, float>( |
| 76 | + shape, [](auto) { return 1.0; }); |
| 77 | + auto out = *LiteralUtil::CreateLiteralWithGenerator<F32, float>( |
| 78 | + shape, [](auto) { return 0; }); |
| 79 | + |
| 80 | + BufferAllocations allocations = CreateBufferAllocations(lhs, rhs, out); |
| 81 | + |
| 82 | + auto [lhs_alloc, rhs_alloc, out_alloc] = |
| 83 | + CreateBufferAllocation(lhs, rhs, out); |
| 84 | + auto [lhs_slice, rhs_slice, out_slice] = |
| 85 | + CreateBufferAllocationSlice(lhs_alloc, rhs_alloc, out_alloc); |
| 86 | + |
| 87 | + DotDimensionNumbers dot_dimensions; |
| 88 | + dot_dimensions.add_lhs_contracting_dimensions(1); |
| 89 | + dot_dimensions.add_rhs_contracting_dimensions(0); |
| 90 | + |
| 91 | + TF_ASSERT_OK_AND_ASSIGN( |
| 92 | + auto thunk, DotThunk::Create({"dot"}, dot_dimensions, lhs_slice, shape, |
| 93 | + rhs_slice, shape, out_slice, shape)); |
| 94 | + |
| 95 | + tsl::thread::ThreadPool threads(tsl::Env::Default(), "test", 8); |
| 96 | + Eigen::ThreadPoolDevice device(threads.AsEigenThreadPool(), |
| 97 | + threads.NumThreads()); |
| 98 | + Thunk::ExecuteParams params; |
| 99 | + params.buffer_allocations = &allocations; |
| 100 | + params.intra_op_threadpool = &device; |
| 101 | + |
| 102 | + auto execute_event = thunk->Execute(params); |
| 103 | + tsl::BlockUntilReady(execute_event); |
| 104 | + ASSERT_FALSE(execute_event.IsError()) << execute_event.GetError(); |
| 105 | + |
| 106 | + auto expected = *LiteralUtil::CreateLiteralWithGenerator<F32, float>( |
| 107 | + shape, [&](auto) { return shape.dimensions(0); }); |
| 108 | + EXPECT_EQ(out, expected); |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace |
| 112 | +} // namespace xla::cpu |
0 commit comments