Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CoreML MLProgram] Support Float16 (1/N) #22068

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open

Conversation

wejoncy
Copy link
Contributor

@wejoncy wejoncy commented Sep 12, 2024

Description

Support Float16 for CoreML MLProgram EP.
Operations:
Unary/Binary/Act/pool/shape/gemm/conv

Motivation and Context

@@ -257,6 +257,98 @@ TEST(CoreMLExecutionProviderTest, TestNameSanitization) {
// TensorRT does not support Clip opset 11 yet.
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
}

TEST(CoreMLExecutionProviderTest, TestBinaryFp16) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add separate tests here or could we update onnxruntime\test\providers\cpu\math\element_wise_ops_test.cc to run the MLFloat16 tests it has for CoreML?

We're also going to add some xnnpack fp16 kernels so the more common test code that is used the better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Refactored some of elemenwise UTs. It's still a bit messy for FP16/Bf16 test.

@wejoncy wejoncy marked this pull request as ready for review September 20, 2024 09:07
onnxruntime/test/providers/cpu/math/gemm_test.cc Outdated Show resolved Hide resolved
test.AddAttribute("beta", 1.0f);
test.AddInput<MLFloat16>("A", {2, 4}, f_A);
test.AddInput<MLFloat16>("B", {4, 3}, f_B, true);
f_C.resize(3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: create a new local variable so that a developer doesn't have to track the changes made to f_C across multiple different tests.

would also be good to capture in a comment what exactly is being tested by using different input sizes for f_C and why.

may also be good to use something other than all 1's for the bias input as that could potentially hide an issue in the handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -37,6 +37,12 @@ void TestConvFp16Op(const ConvOpAndTestAttributes& attributes,
OpTester::ExpectResult expect_result = OpTester::ExpectResult::kExpectSuccess,
const std::string& err_str = "",
int opset = 11) {
#if !defined(MLAS_F16VEC_INTRINSICS_SUPPORTED)
// a `return` after tester will make binary crash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason that if you do not have MLAS_F16VEC_INTRINSICS_SUPPORTED there's no support for the custom NhwcFusedConv attribute? And now that we hit this code when the CoreML EP is enabled we need to do an early exit in that case as no EP can handle the node?

Copy link
Contributor Author

@wejoncy wejoncy Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I misunderstand it. I just need to exclude coremlEP when it has activation fused.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My question was more about making the comment explain 'why' rather than 'what' will happen. If you say 'it will crash' the next developer to look at the code has to start from scratch to figure out the reason in order to decide if this applies to their potential changes not.

I think it comes down to the activation attribute being something that the FusedConv/NhwcFusedConv operators have that the ONNX Conv doesn't. As the CoreML EP doesn't support those contrib ops, if the CPU EP can't handle it due to the lack of MLAS support the OpTester won't find any EPs that can run and fails.

i.e. if attributes.activation is not empty we're using a contrib op that only a small number of EPs support.

I'm surprised this check isn't also required for the QNN EP as I don't see FusedConv or NhwcFusedConv in the ops it supports.

Copy link
Contributor Author

@wejoncy wejoncy Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.
Comments added

onnxruntime/core/providers/coreml/builders/model_builder.h Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants