xref: /aosp_15_r20/external/executorch/kernels/portable/cpu/op_mm.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 
mm_out(KernelRuntimeContext & ctx,const Tensor & in,const Tensor & mat2,Tensor & out)19 Tensor& mm_out(
20     KernelRuntimeContext& ctx,
21     const Tensor& in,
22     const Tensor& mat2,
23     Tensor& out) {
24   ET_KERNEL_CHECK(ctx, check_mm_args(in, mat2, out), InvalidArgument, out);
25 
26   size_t output_ndim = 0;
27   exec_aten::SizesType output_sizes[kTensorDimensionLimit];
28   get_mm_out_target_size(in, mat2, output_sizes, &output_ndim);
29   ET_KERNEL_CHECK(
30       ctx,
31       resize_tensor(out, {output_sizes, output_ndim}) == Error::Ok,
32       InvalidArgument,
33       out);
34 
35   ET_KERNEL_CHECK(
36       ctx, tensors_have_same_dim_order(in, mat2, out), InvalidArgument, out);
37 
38   ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);
39 
40   ET_SWITCH_REAL_TYPES_AND2(
41       Half, BFloat16, in.scalar_type(), ctx, "mm.out", CTYPE, [&]() {
42         size_t m = in.size(0);
43         size_t n = in.size(1);
44         size_t p = mat2.size(1);
45 
46         vec_matmul<CTYPE>(
47             out.mutable_data_ptr<CTYPE>(),
48             in.const_data_ptr<CTYPE>(),
49             mat2.const_data_ptr<CTYPE>(),
50             m,
51             n,
52             p);
53       });
54 
55   return out;
56 }
57 
58 } // namespace native
59 } // namespace executor
60 } // namespace torch
61