Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions kernels/optimized/cpu/op_mm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/kernels/optimized/blas/CPUBlas.h>
#include <executorch/kernels/portable/cpu/util/matmul_ops_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>

namespace torch {
namespace executor {
namespace native {

using Tensor = exec_aten::Tensor;

Tensor& opt_mm_out(
RuntimeContext& ctx,
const Tensor& in,
const Tensor& mat2,
Tensor& out) {
ET_KERNEL_CHECK(ctx, check_mm_args(in, mat2, out), InvalidArgument, out);

size_t output_ndim = 0;
exec_aten::SizesType output_sizes[kTensorDimensionLimit];
get_mm_out_target_size(in, mat2, output_sizes, &output_ndim);
ET_KERNEL_CHECK(
ctx,
resize_tensor(out, {output_sizes, output_ndim}) == Error::Ok,
InvalidArgument,
out);

ET_SWITCH_REAL_TYPES_AND2(
Half, BFloat16, in.scalar_type(), ctx, "mm.out", CTYPE, [&]() {
size_t n = in.size(0);
size_t k = in.size(1);
size_t m = mat2.size(1);

// gemm expects column-major inputs and produces column-major
// output. So, we take advantage of the identity (A @ B).t()
// = B.t() @ A.t() here; row-major B is B.t() from gemm's
// column-major perspective, etc.
executorch::cpublas::gemm(
executorch::cpublas::TransposeType::NoTranspose,
executorch::cpublas::TransposeType::NoTranspose,
m,
n,
k,
static_cast<CTYPE>(1),
mat2.const_data_ptr<CTYPE>(),
m,
in.const_data_ptr<CTYPE>(),
k,
static_cast<CTYPE>(0),
out.mutable_data_ptr<CTYPE>(),
m);
});

return out;
}

} // namespace native
} // namespace executor
} // namespace torch
7 changes: 7 additions & 0 deletions kernels/optimized/cpu/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ _OPTIMIZED_ATEN_OPS = (
],
}),
),
op_target(
name = "op_mm",
deps = [
"//executorch/kernels/optimized:libblas",
"//executorch/kernels/portable/cpu/util:matmul_ops_util",
],
),
op_target(
name = "op_mul",
deps = [
Expand Down
5 changes: 5 additions & 0 deletions kernels/optimized/optimized.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
- arg_meta: null
kernel_name: torch::executor::opt_le_tensor_out

- op: mm.out
kernels:
- arg_meta: null
kernel_name: torch::executor::opt_mm_out

- op: mul.out
kernels:
- arg_meta: null
Expand Down
2 changes: 1 addition & 1 deletion kernels/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def define_common_targets():
_common_op_test("op_mean_test", ["aten", "portable"])
_common_op_test("op_min_test", ["aten", "portable"])
_common_op_test("op_minimum_test", ["aten", "portable"])
_common_op_test("op_mm_test", ["aten", "portable"])
_common_op_test("op_mm_test", ["aten", "portable", "optimized"])
_common_op_test("op_mul_test", ["aten", "portable", "optimized"])
_common_op_test("op_narrow_copy_test", ["aten", "portable"])
_common_op_test("op_native_batch_norm_test", ["aten", "portable"])
Expand Down
Loading