diff --git a/.ci/scripts/test_model.sh b/.ci/scripts/test_model.sh index b0725d36eb7..fa922e897d7 100755 --- a/.ci/scripts/test_model.sh +++ b/.ci/scripts/test_model.sh @@ -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 diff --git a/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm b/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm index 028191ce497..9a0b4facc89 100644 --- a/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm +++ b/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm @@ -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 strides(tensor.strides().begin(), tensor.strides().end()); std::vector 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: { @@ -233,6 +241,12 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { std::array 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++) { diff --git a/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm b/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm index c10a6ba63f2..69e3fc09671 100644 --- a/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm +++ b/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm @@ -113,7 +113,7 @@ - (void)testAddModelExecution { XCTAssertNotNil(inputs); MLMultiArray *output = [ETCoreMLTestUtils filledMultiArrayWithShape:inputs[0].shape dataType:inputs[0].dataType repeatedValue:@(0) error:&localError]; NSArray *args = [inputs arrayByAddingObject:output]; - XCTAssertTrue([self.modelManager executeModelWithHandle:handle + XCTAssertTrue([self.modelManager executeModelWithHandle:handle args:args loggingOptions:executorchcoreml::ModelLoggingOptions() eventLogger:nullptr