|
| 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_lib.h" |
| 17 | + |
| 18 | +#include <cstdint> |
| 19 | +#include <functional> |
| 20 | +#include <numeric> |
| 21 | +#include <utility> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "absl/algorithm/container.h" |
| 25 | +#include "absl/container/inlined_vector.h" |
| 26 | +#include "absl/status/statusor.h" |
| 27 | +#include "absl/strings/str_join.h" |
| 28 | +#include "xla/layout_util.h" |
| 29 | +#include "xla/runtime/buffer_use.h" |
| 30 | +#include "xla/shape.h" |
| 31 | +#include "xla/shape_util.h" |
| 32 | +#include "xla/util.h" |
| 33 | + |
| 34 | +namespace xla::cpu { |
| 35 | + |
| 36 | +absl::InlinedVector<BufferUse, 4> DotBufferUses(const DotSlices& slices) { |
| 37 | + return {BufferUse::Read(slices.lhs_buffer), |
| 38 | + BufferUse::Read(slices.rhs_buffer), |
| 39 | + BufferUse::Write(slices.out_buffer)}; |
| 40 | +} |
| 41 | + |
| 42 | +absl::StatusOr<DotShape> GetDotShape(DotDimensionNumbers dot_dimensions, |
| 43 | + const Shape& lhs_shape, |
| 44 | + const Shape& rhs_shape, |
| 45 | + const Shape& out_shape) { |
| 46 | + // All shapes must be in dim0-major layout. |
| 47 | + if (!LayoutUtil::IsMonotonicWithDim0Major(lhs_shape.layout()) || |
| 48 | + !LayoutUtil::IsMonotonicWithDim0Major(rhs_shape.layout()) || |
| 49 | + !LayoutUtil::IsMonotonicWithDim0Major(out_shape.layout())) { |
| 50 | + return InvalidArgument( |
| 51 | + "DotThunk requires all operands and outputs to be in " |
| 52 | + "dim0-major layout: lhs_shape=[%s], rhs_shape=[%s], out_shape=[%s]", |
| 53 | + lhs_shape.ToString(true), rhs_shape.ToString(true), |
| 54 | + out_shape.ToString(true)); |
| 55 | + } |
| 56 | + |
| 57 | + // Batch dimensions must be contiguous and start at 0. |
| 58 | + std::vector<int64_t> batch_dims(dot_dimensions.lhs_batch_dimensions().size()); |
| 59 | + absl::c_iota(batch_dims, 0); |
| 60 | + |
| 61 | + if (!absl::c_equal(dot_dimensions.lhs_batch_dimensions(), batch_dims) || |
| 62 | + !absl::c_equal(dot_dimensions.rhs_batch_dimensions(), batch_dims)) { |
| 63 | + return InvalidArgument( |
| 64 | + "Batch dimensions must be contiguous and start at 0: " |
| 65 | + "lhs_batch_dims=[%s], rhs_batch_dims=[%s]", |
| 66 | + absl::StrJoin(dot_dimensions.lhs_batch_dimensions(), ","), |
| 67 | + absl::StrJoin(dot_dimensions.rhs_batch_dimensions(), ",")); |
| 68 | + } |
| 69 | + |
| 70 | + int64_t num_batch_dims = batch_dims.size(); |
| 71 | + int64_t batch_size = |
| 72 | + std::accumulate(out_shape.dimensions().begin(), |
| 73 | + out_shape.dimensions().begin() + num_batch_dims, 1LL, |
| 74 | + std::multiplies<int64_t>()); |
| 75 | + |
| 76 | + Shape lhs_matmul_shape = ShapeUtil::DeleteDimensions(batch_dims, lhs_shape); |
| 77 | + Shape rhs_matmul_shape = ShapeUtil::DeleteDimensions(batch_dims, rhs_shape); |
| 78 | + Shape out_matmul_shape = ShapeUtil::DeleteDimensions(batch_dims, out_shape); |
| 79 | + |
| 80 | + // Check that matmul shapes are rank 2 or less and can be represented as |
| 81 | + // Eigen 2D contraction. |
| 82 | + if (lhs_matmul_shape.rank() > 2 || rhs_matmul_shape.rank() > 2 || |
| 83 | + out_matmul_shape.rank() > 2) { |
| 84 | + return InvalidArgument( |
| 85 | + "MatMul shape must be rank 2 or less: lhs=%s, rhs=%s, out=%s", |
| 86 | + lhs_matmul_shape.ToString(true), rhs_matmul_shape.ToString(true), |
| 87 | + out_matmul_shape.ToString(true)); |
| 88 | + } |
| 89 | + |
| 90 | + return DotShape{ |
| 91 | + batch_size, |
| 92 | + std::move(lhs_matmul_shape), |
| 93 | + std::move(rhs_matmul_shape), |
| 94 | + std::move(out_matmul_shape), |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace xla::cpu |
0 commit comments