Skip to content

Fix coreml rank0 #10534

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

Merged
merged 7 commits into from
Apr 30, 2025
Merged
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
2 changes: 1 addition & 1 deletion .ci/scripts/test_model.sh
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ test_model_with_coreml() {

DTYPE=float16

"${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision "${DTYPE}"
"${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision "${DTYPE}" --use_partitioner
EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*.pte" -print -quit)

if [ -n "$EXPORTED_MODEL" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,17 @@
ET_LOG(Error, "%s: DataType=%d is not supported", ETCoreMLStrings.delegateIdentifier.UTF8String, (int)tensor.scalar_type());
return std::nullopt;
}

std::vector<ssize_t> strides(tensor.strides().begin(), tensor.strides().end());
std::vector<size_t> shape(tensor.sizes().begin(), tensor.sizes().end());

// If tensor is rank 0, wrap in rank 1
// See https://github.com/apple/coremltools/blob/8.2/coremltools/converters/mil/frontend/torch/exir_utils.py#L73
if (shape.size() == 0) {
shape.push_back(1);
strides.push_back(1);
}

MultiArray::MemoryLayout layout(dataType.value(), std::move(shape), std::move(strides));
switch (argType) {
case ArgType::Input: {
Expand Down Expand Up @@ -233,6 +241,12 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) {
std::array<SizesType, kTensorDimensionLimit> new_shape;
for (size_t i = nInputs; i < nInputs + nOutputs; i++) {
Tensor& t = args[i]->toTensor();
// If t has rank 0, do not resize. delegate_args[i] will have rank 1
// because we resized it in get_multi_array
if (t.dim() == 0) {
continue;
}

int rank = delegate_args[i].layout().rank();
assert (rank <= new_shape.size());
for (int d = 0; d < rank; d++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ - (void)testAddModelExecution {
XCTAssertNotNil(inputs);
MLMultiArray *output = [ETCoreMLTestUtils filledMultiArrayWithShape:inputs[0].shape dataType:inputs[0].dataType repeatedValue:@(0) error:&localError];
NSArray<MLMultiArray *> *args = [inputs arrayByAddingObject:output];
XCTAssertTrue([self.modelManager executeModelWithHandle:handle
XCTAssertTrue([self.modelManager executeModelWithHandle:handle
args:args
loggingOptions:executorchcoreml::ModelLoggingOptions()
eventLogger:nullptr
Expand Down
Loading