xref: /aosp_15_r20/external/executorch/kernels/portable/cpu/op_bmm.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <executorch/kernels/portable/cpu/util/matmul_ops_util.h>
10 #include <executorch/kernels/portable/cpu/vec_ops.h>
11 #include <executorch/runtime/kernel/kernel_includes.h>
12 
13 namespace torch {
14 namespace executor {
15 namespace native {
16 
17 using Tensor = exec_aten::Tensor;
18 
bmm_out(KernelRuntimeContext & ctx,const Tensor & in,const Tensor & mat2,Tensor & out)19 Tensor& bmm_out(
20     KernelRuntimeContext& ctx,
21     const Tensor& in,
22     const Tensor& mat2,
23     Tensor& out) {
24   ET_KERNEL_CHECK(ctx, check_bmm_args(in, mat2, out), InvalidArgument, out);
25 
26   ET_KERNEL_CHECK(
27       ctx, tensors_have_same_dim_order(in, mat2, out), InvalidArgument, out);
28 
29   ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);
30 
31   size_t output_ndim = 0;
32   exec_aten::SizesType output_sizes[kTensorDimensionLimit];
33   get_bmm_out_target_size(in, mat2, output_sizes, &output_ndim);
34   ET_KERNEL_CHECK(
35       ctx,
36       resize_tensor(out, {output_sizes, output_ndim}) == Error::Ok,
37       InvalidArgument,
38       out);
39 
40   ET_SWITCH_REAL_TYPES_AND(
41       Half, in.scalar_type(), ctx, "bmm.out", CTYPE, [&]() {
42         const CTYPE* in_data = in.const_data_ptr<CTYPE>();
43         const CTYPE* mat2_data = mat2.const_data_ptr<CTYPE>();
44         CTYPE* out_data = out.mutable_data_ptr<CTYPE>();
45 
46         int64_t batch_size = in.size(0);
47         int64_t m = in.size(1);
48         int64_t n = in.size(2);
49         int64_t p = mat2.size(2);
50 
51         for (int i = 0; i < batch_size; ++i) {
52           const CTYPE* in_data_offset = in_data + i * m * n;
53           const CTYPE* mat2_data_offset = mat2_data + i * n * p;
54           CTYPE* out_data_offset = out_data + i * m * p;
55 
56           vec_matmul<CTYPE>(
57               out_data_offset, in_data_offset, mat2_data_offset, m, n, p);
58         }
59       });
60 
61   return out;
62 }
63 
64 } // namespace native
65 } // namespace executor
66 } // namespace torch
67