forked from microsoft/onnxruntime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49a80df
commit 755e2d3
Showing
6 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
onnxruntime/contrib_ops/cpu/quantization/firefox_matmul_integer.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "firefox_matmul_integer.h" | ||
#include "core/providers/cpu/math/matmul_helper.h" | ||
|
||
namespace onnxruntime { | ||
namespace contrib { | ||
|
||
ONNX_OPERATOR_KERNEL_EX( | ||
FirefoxMatMulInteger8, | ||
kMSDomain, | ||
1, | ||
kCpuExecutionProvider, | ||
KernelDefBuilder() | ||
.TypeConstraint("T1", DataTypeImpl::GetTensorType<int8_t>()) | ||
.TypeConstraint("T2", DataTypeImpl::GetTensorType<int8_t>()) | ||
.TypeConstraint("T3", DataTypeImpl::GetTensorType<int16_t>()), | ||
FirefoxMatMulInteger8<int8_t, int8_t, int16_t>); | ||
|
||
template <> | ||
Status FirefoxMatMulInteger8<int8_t, int8_t, int16_t>::Compute(OpKernelContext* ctx) const { | ||
auto A = ctx->Input<Tensor>(0); | ||
auto B = ctx->Input<Tensor>(1); | ||
ORT_ENFORCE(A != nullptr && B != nullptr); | ||
|
||
MatMulComputeHelper helper; | ||
ORT_RETURN_IF_ERROR(helper.Compute(A->Shape(), B->Shape())); | ||
Tensor* Y = ctx->Output(0, helper.OutputShape()); | ||
|
||
// Bail out early if the output is going to be empty | ||
if (Y->Shape().Size() == 0) | ||
return Status::OK(); | ||
|
||
for (int i = 0; i < static_cast<int>(helper.OutputOffsets().size()); i++) { | ||
EigenCastGEMM<int8_t, int8_t, int16_t>( | ||
A->Data<int8_t>() + helper.LeftOffsets()[i], | ||
B->Data<int8_t>() + helper.RightOffsets()[i], | ||
Y->MutableData<int16_t>() + helper.OutputOffsets()[i], | ||
static_cast<int>(helper.M()), | ||
static_cast<int>(helper.N()), | ||
static_cast<int>(helper.K())); | ||
} | ||
|
||
return Status::OK(); | ||
} | ||
|
||
} // namespace contrib | ||
} // namespace onnxruntime |
22 changes: 22 additions & 0 deletions
22
onnxruntime/contrib_ops/cpu/quantization/firefox_matmul_integer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
|
||
#include "core/common/common.h" | ||
#include "core/framework/op_kernel.h" | ||
#include "core/util/math_cpuonly.h" | ||
|
||
namespace onnxruntime { | ||
namespace contrib { | ||
|
||
template <typename T1, typename T2, typename T3> | ||
class FirefoxMatMulInteger8 final : public OpKernel { | ||
public: | ||
FirefoxMatMulInteger8(const OpKernelInfo& info) : OpKernel(info) { | ||
} | ||
|
||
Status Compute(OpKernelContext* context) const override; | ||
}; | ||
} // namespace contrib | ||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
onnxruntime/test/contrib_ops/firefox_matmul_integer_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "gtest/gtest.h" | ||
#include "test/providers/provider_test_utils.h" | ||
|
||
#include "core/common/common.h" | ||
#include "core/framework/op_kernel.h" | ||
#include "core/util/math_cpuonly.h" | ||
|
||
namespace onnxruntime { | ||
namespace test { | ||
|
||
TEST(FirefoxMatMulIntegerOpTest, FirefoxMatMulInteger_1) { | ||
OpTester test("FirefoxMatMulInteger", 1, onnxruntime::kMSDomain); | ||
test.AddInput<int8_t>("T1", {1, 1}, {15}); | ||
test.AddInput<int8_t>("T2", {1, 1}, {8}); | ||
test.AddOutput<int32_t>("T3", {1, 1}, {120}); // Result is 15 * 8 | ||
test.Run(); | ||
} | ||
|
||
TEST(FirefoxMatMulIntegerOpTest, FirefoxMatMulInteger_2) { | ||
OpTester test("FirefoxMatMulInteger", 1, onnxruntime::kMSDomain); | ||
test.AddInput<int8_t>("T1", {1, 2}, {-7, 10}); | ||
test.AddInput<int8_t>("T2", {2, 1}, {-8, -11}); | ||
test.AddOutput<int32_t>("T3", {1, 1}, {8}); // Result is (-7 * -8) + (10 * -11) | ||
test.Run(); | ||
} | ||
|
||
TEST(FirefoxMatMulIntegerOpTest, FirefoxMatMulInteger_Empty_input) { | ||
OpTester test("FirefoxMatMulInteger", 1, onnxruntime::kMSDomain); | ||
test.AddInput<int8_t>("T1", {0, 2}, {}); | ||
test.AddInput<int8_t>("T2", {2, 1}, {-8, -11}); | ||
test.AddOutput<int32_t>("T3", {0, 1}, {}); // Empty input produces an empty output | ||
test.Run(); | ||
} | ||
|
||
TEST(FirefoxMatMulIntegerOpTest, FirefoxMatMulInteger_3) { | ||
OpTester test("FirefoxMatMulInteger", 1, onnxruntime::kMSDomain); | ||
test.AddInput<int8_t>("T1", {3, 2}, {-7, 10, 10, -113, 22, -36}); | ||
test.AddInput<int8_t>("T2", {2, 4}, {-8, -11, 13, 14, -9, 12, 3, -6}); | ||
test.AddOutput<int32_t>("T3", {3, 4}, | ||
{-158, 97, -61, -2, // First row results | ||
989, -1426, 1693, 1682, // Second row results | ||
282, -518, 280, -372}); // Third row results | ||
test.Run(); | ||
} | ||
|
||
} // namespace test | ||
} // namespace onnxruntime |