Skip to content

Commit 8a224c6

Browse files
committed
Optimize memory when exporting.
1 parent 0b6add8 commit 8a224c6

21 files changed

+814
-247
lines changed

backends/apple/coreml/compiler/coreml_preprocess.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ def to_bytes(mlmodel):
5050
outfile.write(json_object)
5151

5252
# flatten directory contents and convert it to bytes
53-
flattened_bytes = executorchcoreml.flatten_directory_contents(
53+
contents = executorchcoreml.flatten_directory_contents(
5454
str(model_dir_path.resolve())
5555
)
5656
shutil.rmtree(str(model_dir_path.resolve()))
57-
return flattened_bytes
57+
return contents
5858

5959
@classmethod
6060
# pyre-ignore
@@ -70,7 +70,10 @@ def preprocess(
7070
pass_pipeline=ct.PassPipeline.DEFAULT,
7171
skip_model_load=True,
7272
)
73-
flattened_bytes = CoreMLBackend.to_bytes(mlmodel)
74-
return PreprocessResult(
75-
processed_bytes=flattened_bytes,
73+
74+
bytes_and_handle = CoreMLBackend.to_bytes(mlmodel)
75+
result = PreprocessResult(
76+
processed_bytes=bytes_and_handle[0],
7677
)
78+
79+
return result

backends/apple/coreml/runtime/delegate/ETCoreMLModelManager.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ - (nullable ETCoreMLModel *)loadModelFromData:(NSData *)data
380380
using namespace inmemoryfs;
381381

382382
auto buffer = MemoryBuffer::make_unowned(const_cast<void *>(data.bytes), data.length);
383-
std::unique_ptr<InMemoryFileSystem> inMemoryFS = inmemoryfs::make(buffer);
383+
std::unique_ptr<InMemoryFileSystem> inMemoryFS = inmemoryfs::make_inmemory_filesystem(std::move(buffer));
384384
if (!inMemoryFS) {
385385
ETCoreMLLogErrorAndSetNSError(error,
386386
ETCoreMLErrorCorruptedModel,

backends/apple/coreml/runtime/delegate/backend_delegate.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ MLMultiArrayDataType get_data_type(MultiArray::DataType dataType) {
6464
return MLMultiArrayDataTypeFloat32;
6565
}
6666
case MultiArray::DataType::Double: {
67-
return MLMultiArrayDataTypeFloat64;
67+
return MLMultiArrayDataTypeDouble;
6868
}
6969
case MultiArray::DataType::Int: {
7070
return MLMultiArrayDataTypeInt32;

backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
ET_CHECK_OR_RETURN_ERROR(handle != nullptr,
136136
InvalidProgram,
137137
"%s: Failed to init the model.", ETCoreMLStrings.delegateIdentifier.UTF8String);
138-
138+
processed->Free();
139139
return handle;
140140
}
141141

0 commit comments

Comments
 (0)