diff --git a/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm b/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm index fc3e5a47f9..2d94873ce6 100644 --- a/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm +++ b/backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm @@ -25,9 +25,22 @@ #endif namespace { -using namespace torch::executor; using namespace executorchcoreml; +using executorch::aten::ScalarType; +using executorch::runtime::ArrayRef; +using executorch::runtime::Backend; +using executorch::runtime::BackendExecutionContext; +using executorch::runtime::BackendInitContext; +using executorch::runtime::CompileSpec; +using executorch::runtime::DelegateHandle; +using executorch::runtime::EValue; +using executorch::runtime::Error; +using executorch::runtime::EventTracerDebugLogLevel; +using executorch::runtime::FreeableBuffer; +using executorch::runtime::get_backend_class; +using executorch::runtime::Result; + std::optional get_data_type(ScalarType scalar_type) { switch (scalar_type) { case ScalarType::Bool: @@ -60,14 +73,14 @@ if (!eValue->isTensor()) { return std::nullopt; } - + auto tensor = eValue->toTensor(); auto dataType = get_data_type(tensor.scalar_type()); if (!dataType.has_value()) { 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()); MultiArray::MemoryLayout layout(dataType.value(), std::move(shape), std::move(strides)); @@ -86,7 +99,7 @@ if (!dict) { return std::nullopt; } - + BackendDelegate::Config config; { NSNumber *should_prewarm_model = SAFE_CAST(dict[@"shouldPrewarmModel"], NSNumber); @@ -94,21 +107,21 @@ config.should_prewarm_model = static_cast(should_prewarm_model.boolValue); } } - + { NSNumber *should_prewarm_asset = SAFE_CAST(dict[@"shouldPrewarmAsset"], NSNumber); if (should_prewarm_asset) { config.should_prewarm_asset = static_cast(should_prewarm_asset.boolValue); } } - + { NSNumber *max_models_cache_size_in_bytes = SAFE_CAST(dict[@"maxModelsCacheSizeInBytes"], NSNumber); if (max_models_cache_size_in_bytes) { config.max_models_cache_size = max_models_cache_size_in_bytes.unsignedLongLongValue; } } - + return config; } @@ -127,14 +140,15 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { auto debug_level = event_tracer->event_tracer_debug_level(); options.log_intermediate_tensors = (debug_level >= EventTracerDebugLogLevel::kIntermediateOutputs); } - + return options; } } //namespace -namespace torch { -namespace executor { +namespace executorch { +namespace backends { +namespace coreml { using namespace executorchcoreml; @@ -154,7 +168,7 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { auto buffer = Buffer(spec.value.buffer, spec.value.nbytes); specs_map.emplace(spec.key, std::move(buffer)); } - + auto buffer = Buffer(processed->data(), processed->size()); std::error_code error; auto handle = impl_->init(std::move(buffer), specs_map); @@ -173,7 +187,7 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { size_t nInputs = nArgs.first; size_t nOutputs = nArgs.second; delegate_args.reserve(nInputs + nOutputs); - + // inputs for (size_t i = 0; i < nInputs; i++) { auto multi_array = get_multi_array(args[i], ArgType::Input); @@ -182,7 +196,7 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { "%s: Failed to create multiarray from input at args[%zu]", ETCoreMLStrings.delegateIdentifier.UTF8String, i); delegate_args.emplace_back(std::move(multi_array.value())); } - + // outputs for (size_t i = nInputs; i < nInputs + nOutputs; i++) { auto multi_array = get_multi_array(args[i], ArgType::Output); @@ -191,7 +205,7 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { "%s: Failed to create multiarray from output at args[%zu]", ETCoreMLStrings.delegateIdentifier.UTF8String, i); delegate_args.emplace_back(std::move(multi_array.value())); } - + auto logging_options = get_logging_options(context); std::error_code ec; #ifdef ET_EVENT_TRACER_ENABLED @@ -206,7 +220,7 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { "%s: Failed to run the model.", ETCoreMLStrings.delegateIdentifier.UTF8String); #endif - + return Error::Ok; } @@ -235,5 +249,6 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) { static auto success_with_compiler = register_backend(backend); } -} // namespace executor -} // namespace torch +} // namespace coreml +} // namespace backends +} // namespace executorch diff --git a/backends/apple/coreml/runtime/include/coreml_backend/delegate.h b/backends/apple/coreml/runtime/include/coreml_backend/delegate.h index 1943e0f05b..ec402e8171 100644 --- a/backends/apple/coreml/runtime/include/coreml_backend/delegate.h +++ b/backends/apple/coreml/runtime/include/coreml_backend/delegate.h @@ -17,8 +17,9 @@ namespace executorchcoreml { class BackendDelegate; } -namespace torch { -namespace executor { +namespace executorch { +namespace backends { +namespace coreml { class CoreMLBackendDelegate final : public ::executorch::runtime::BackendInterface { public: @@ -34,8 +35,10 @@ class CoreMLBackendDelegate final : public ::executorch::runtime::BackendInterfa /// produce `processed`. /// @retval On success, an opaque handle representing the loaded model /// otherwise an`Error` case. - Result - init(BackendInitContext& context, FreeableBuffer* processed, ArrayRef compileSpecs) const override; + executorch::runtime::Result + init(executorch::runtime::BackendInitContext& context, + executorch::runtime::FreeableBuffer* processed, + executorch::runtime::ArrayRef compileSpecs) const override; /// Executes the loaded model. /// @@ -43,7 +46,9 @@ class CoreMLBackendDelegate final : public ::executorch::runtime::BackendInterfa /// @param handle The handle returned by an earlier call to `init`. /// @param args The models inputs and outputs. /// @retval On success, `Error::Ok` otherwise any other `Error` case. - Error execute(BackendExecutionContext& context, DelegateHandle* handle, EValue** args) const override; + executorch::runtime::Error execute(executorch::runtime::BackendExecutionContext& context, + executorch::runtime::DelegateHandle* handle, + executorch::runtime::EValue** args) const override; /// Returns `true` if the delegate is available otherwise `false`. bool is_available() const override; @@ -51,7 +56,7 @@ class CoreMLBackendDelegate final : public ::executorch::runtime::BackendInterfa /// Unloads the loaded CoreML model with the specified handle. /// /// @param handle The handle returned by an earlier call to `init`. - void destroy(DelegateHandle* handle) const override; + void destroy(executorch::runtime::DelegateHandle* handle) const override; /// Returns the registered `CoreMLBackendDelegate` instance. static CoreMLBackendDelegate* get_registered_delegate() noexcept; @@ -65,5 +70,7 @@ class CoreMLBackendDelegate final : public ::executorch::runtime::BackendInterfa private: std::shared_ptr impl_; }; -} // namespace executor -} // namespace torch + +} // namespace coreml +} // namespace backends +} // namespace executorch diff --git a/backends/apple/coreml/runtime/sdk/model_event_logger_impl.mm b/backends/apple/coreml/runtime/sdk/model_event_logger_impl.mm index 1d358583a8..12ac8ec15a 100644 --- a/backends/apple/coreml/runtime/sdk/model_event_logger_impl.mm +++ b/backends/apple/coreml/runtime/sdk/model_event_logger_impl.mm @@ -17,7 +17,11 @@ namespace { -using namespace torch::executor; +using namespace executorch::runtime; + +using executorch::aten::ScalarType; +using executorch::aten::Tensor; +using executorch::aten::TensorImpl; uint64_t time_units_to_nano_seconds(uint64_t time_units) { static mach_timebase_info_data_t info; @@ -25,7 +29,7 @@ uint64_t time_units_to_nano_seconds(uint64_t time_units) { dispatch_once(&onceToken, ^{ NSCAssert(mach_timebase_info(&info) == KERN_SUCCESS, @"ModelEventLogger: Failed to get time base."); }); - + return time_units * info.numer / info.denom; } @@ -100,7 +104,7 @@ bool is_packed(NSArray *shape, NSArray *strides) { estimated_execution_end_time_in_ns, metadata.bytes, metadata.length); - + }]; } @@ -109,7 +113,7 @@ bool is_packed(NSArray *shape, NSArray *strides) { [op_path_to_value_map enumerateKeysAndObjectsUsingBlock:^(ETCoreMLModelStructurePath *path, MLMultiArray *intermediate_value, BOOL * _Nonnull __unused stop) { - using namespace torch::executor; + using namespace executorch::runtime; @autoreleasepool { NSString *debug_symbol = op_path_to_debug_symbol_name_map[path]; @@ -123,7 +127,7 @@ bool is_packed(NSArray *shape, NSArray *strides) { } MLMultiArray *supported_value = value; - NSArray *shape = supported_value.shape; + NSArray *shape = supported_value.shape; NSError *local_error = nil; MLMultiArrayDataType data_type = get_supported_data_type(value.dataType); @@ -131,7 +135,7 @@ bool is_packed(NSArray *shape, NSArray *strides) { supported_value = [[MLMultiArray alloc] initWithShape:shape dataType:data_type error:&local_error]; - NSCAssert(supported_value != nil, + NSCAssert(supported_value != nil, @"ModelEventLoggerImpl: Failed to create packed multiarray with shape=%@, dataType=%ld, error=%@.", shape, static_cast(value.dataType), diff --git a/backends/apple/coreml/runtime/test/BackendDelegateTests.mm b/backends/apple/coreml/runtime/test/BackendDelegateTests.mm index 8222349650..78ee33429a 100644 --- a/backends/apple/coreml/runtime/test/BackendDelegateTests.mm +++ b/backends/apple/coreml/runtime/test/BackendDelegateTests.mm @@ -60,7 +60,7 @@ + (nullable NSURL *)bundledResourceWithName:(NSString *)name extension:(NSString } + (void)setUp { - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } - (void)setUp { diff --git a/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm b/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm index 691d4d726e..ef114546fe 100644 --- a/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm +++ b/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm @@ -17,8 +17,8 @@ static constexpr size_t kRuntimeMemorySize = 50 * 1024U * 1024U; // 50 MB -using namespace torch::executor; -using torch::executor::testing::TensorFactory; +using namespace executorch::runtime; +using executorch::runtime::testing::TensorFactory; namespace { // TODO: Move the following methods to a utility class, so that it can be shared with `executor_runner.main.mm` @@ -107,8 +107,8 @@ } Buffer buffer(tensor_meta->nbytes(), 0); auto sizes = tensor_meta->sizes(); - exec_aten::TensorImpl tensor_impl(tensor_meta->scalar_type(), std::size(sizes), const_cast(sizes.data()), buffer.data()); - exec_aten::Tensor tensor(&tensor_impl); + executorch::aten::TensorImpl tensor_impl(tensor_meta->scalar_type(), std::size(sizes), const_cast(sizes.data()), buffer.data()); + executorch::aten::Tensor tensor(&tensor_impl); EValue input_value(std::move(tensor)); Error err = method.set_input(input_value, i); if (err != Error::Ok) { @@ -129,7 +129,7 @@ @interface CoreMLBackendDelegateTests : XCTestCase @implementation CoreMLBackendDelegateTests + (void)setUp { - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } + (nullable NSURL *)bundledResourceWithName:(NSString *)name extension:(NSString *)extension { diff --git a/backends/apple/coreml/runtime/test/ETCoreMLAssetManagerTests.mm b/backends/apple/coreml/runtime/test/ETCoreMLAssetManagerTests.mm index f466899559..def13a96d4 100644 --- a/backends/apple/coreml/runtime/test/ETCoreMLAssetManagerTests.mm +++ b/backends/apple/coreml/runtime/test/ETCoreMLAssetManagerTests.mm @@ -23,7 +23,7 @@ @interface ETCoreMLAssetManagerTests : XCTestCase @implementation ETCoreMLAssetManagerTests + (void)setUp { - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } - (void)setUp { diff --git a/backends/apple/coreml/runtime/test/ETCoreMLModelDebuggerTests.mm b/backends/apple/coreml/runtime/test/ETCoreMLModelDebuggerTests.mm index 2464ec8dbb..495821544a 100644 --- a/backends/apple/coreml/runtime/test/ETCoreMLModelDebuggerTests.mm +++ b/backends/apple/coreml/runtime/test/ETCoreMLModelDebuggerTests.mm @@ -70,7 +70,7 @@ @interface ETCoreMLModelDebuggerTests : XCTestCase @implementation ETCoreMLModelDebuggerTests + (void)setUp { - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } + (nullable NSURL *)bundledResourceWithName:(NSString *)name extension:(NSString *)extension { diff --git a/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm b/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm index 5fceb9ac75..504e7ed336 100644 --- a/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm +++ b/backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm @@ -32,7 +32,7 @@ + (nullable NSURL *)bundledResourceWithName:(NSString *)name extension:(NSString } - (void)setUp { - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); @autoreleasepool { NSError *localError = nil; self.fileManager = [[NSFileManager alloc] init]; diff --git a/backends/apple/coreml/runtime/test/ETCoreMLModelProfilerTests.mm b/backends/apple/coreml/runtime/test/ETCoreMLModelProfilerTests.mm index c243ab5159..3cc6308579 100644 --- a/backends/apple/coreml/runtime/test/ETCoreMLModelProfilerTests.mm +++ b/backends/apple/coreml/runtime/test/ETCoreMLModelProfilerTests.mm @@ -59,7 +59,7 @@ @interface ETCoreMLModelProfilerTests : XCTestCase @implementation ETCoreMLModelProfilerTests + (void)setUp { - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } + (nullable NSURL *)bundledResourceWithName:(NSString *)name extension:(NSString *)extension { diff --git a/examples/apple/coreml/executor_runner/main.mm b/examples/apple/coreml/executor_runner/main.mm index 405bfb9c6c..35608dd092 100644 --- a/examples/apple/coreml/executor_runner/main.mm +++ b/examples/apple/coreml/executor_runner/main.mm @@ -24,12 +24,13 @@ static inline id check_class(id obj, Class cls) { #define SAFE_CAST(Object, Type) ((Type *)check_class(Object, [Type class])) +using executorch::backends::coreml::CoreMLBackendDelegate; using executorch::etdump::ETDumpGen; using executorch::etdump::ETDumpResult; using executorch::extension::FileDataLoader; using executorch::runtime::DataLoader; -using executorch::runtime::EValue; using executorch::runtime::Error; +using executorch::runtime::EValue; using executorch::runtime::EventTracer; using executorch::runtime::EventTracerDebugLogLevel; using executorch::runtime::FreeableBuffer; @@ -42,7 +43,6 @@ static inline id check_class(id obj, Class cls) { using executorch::runtime::Result; using executorch::runtime::Span; using executorch::runtime::TensorInfo; -using torch::executor::CoreMLBackendDelegate; static constexpr size_t kRuntimeMemorySize = 16 * 1024U * 1024U; // 16 MB