Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

#include <system_error>
#include <unordered_map>
#include <vector>

namespace executorchcoreml {
Expand Down
1 change: 1 addition & 0 deletions backends/apple/coreml/runtime/kvstore/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <bitset>
#include <functional>
#include <memory>
#include <string>
#include <system_error>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#import <functional>
#include <optional>
#include <memory>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#import <executorch/runtime/executor/method.h>
#import <executorch/runtime/executor/program.h>
#import <executorch/runtime/platform/runtime.h>
#import <executorch/util/util.h>

static constexpr size_t kRuntimeMemorySize = 10 * 1024U * 1024U; // 10 MB

Expand Down Expand Up @@ -95,6 +94,33 @@

return result;
}

Result<std::vector<Buffer>> prepare_input_tensors(Method& method) {
MethodMeta method_meta = method.method_meta();
size_t num_inputs = method_meta.num_inputs();
std::vector<std::vector<uint8_t>> buffers;
for (size_t i = 0; i < num_inputs; i++) {
Result<TensorInfo> tensor_meta = method_meta.input_tensor_meta(i);
if (!tensor_meta.ok()) {
ET_LOG(Info, "Skipping non-tensor input %zu", i);
continue;
}
Buffer buffer(tensor_meta->nbytes(), 1);
auto sizes = tensor_meta->sizes();
exec_aten::TensorImpl tensor_impl(tensor_meta->scalar_type(), std::size(sizes), const_cast<int *>(sizes.data()), buffer.data());
exec_aten::Tensor tensor(&tensor_impl);
EValue input_value(std::move(tensor));
Error err = method.set_input(input_value, i);
if (err != Error::Ok) {
ET_LOG(Error, "Failed to prepare input %zu: 0x%" PRIx32, i, (uint32_t)err);
return err;
}
buffers.emplace_back(std::move(buffer));
}

return buffers;
}

}

@interface CoreMLBackendDelegateTests : XCTestCase
Expand Down Expand Up @@ -145,15 +171,12 @@ - (void)executeModelAtURL:(NSURL *)modelURL nTimes:(NSUInteger)nTimes {
MemoryManager memoryManger(&methodAllocator, &plannedAllocator);
auto method = program->load_method(methodName.get().c_str(), &memoryManger);
XCTAssert(method.ok());
auto inputs = util::PrepareInputTensors(method.get());

auto inputBuffers = prepare_input_tensors(method.get());
auto status = method->execute();
XCTAssertEqual(status, Error::Ok);
auto outputs = methodAllocator.allocateList<EValue>(method->outputs_size());
status = method->get_outputs(outputs, method->outputs_size());
XCTAssertEqual(status, Error::Ok);

util::FreeInputs(inputs);
}
}

Expand Down
31 changes: 28 additions & 3 deletions examples/apple/coreml/executor_runner/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ Args parse_command_line_args(NSArray<NSString *> *args) {
return result;
}

Result<std::vector<Buffer>> prepare_input_tensors(Method& method) {
MethodMeta method_meta = method.method_meta();
size_t num_inputs = method_meta.num_inputs();
std::vector<std::vector<uint8_t>> buffers;
for (size_t i = 0; i < num_inputs; i++) {
Result<TensorInfo> tensor_meta = method_meta.input_tensor_meta(i);
if (!tensor_meta.ok()) {
ET_LOG(Info, "Skipping non-tensor input %zu", i);
continue;
}
Buffer buffer(tensor_meta->nbytes(), 1);
auto sizes = tensor_meta->sizes();
exec_aten::TensorImpl tensor_impl(tensor_meta->scalar_type(), std::size(sizes), const_cast<int *>(sizes.data()), buffer.data());
exec_aten::Tensor tensor(&tensor_impl);
EValue input_value(std::move(tensor));
Error err = method.set_input(input_value, i);
if (err != Error::Ok) {
ET_LOG(Error, "Failed to prepare input %zu: 0x%" PRIx32, i, (uint32_t)err);
return err;
}
buffers.emplace_back(std::move(buffer));
}

return buffers;
}

double calculate_mean(const std::vector<double>& durations) {
if (durations.size() == 0) {
return 0.0;
Expand Down Expand Up @@ -293,7 +319,7 @@ int main(int argc, char * argv[]) {
ET_CHECK_MSG(method_name.ok(), "Failed to load method with name=%s from program=%p", method_name.get().c_str(), program.get());
ET_LOG(Info, "Running method = %s", method_name.get().c_str());

auto inputs = util::PrepareInputTensors(*method);
auto inputs = prepare_input_tensors(*method);
ET_LOG(Info, "Inputs prepared.");

// Run the model.
Expand Down Expand Up @@ -322,7 +348,6 @@ int main(int argc, char * argv[]) {
}
}

util::FreeInputs(inputs);
return 0;
return EXIT_SUCCESS;
}
}