|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#define ORT_API_MANUAL_INIT |
| 5 | +#include "onnxruntime_cxx_api.h" |
| 6 | +#undef ORT_API_MANUAL_INIT |
| 7 | + |
| 8 | +#include <memory> |
| 9 | + |
| 10 | +#include "core/providers/webgpu/ep/factory.h" |
| 11 | + |
| 12 | +// To make symbols visible on macOS/iOS |
| 13 | +#ifdef __APPLE__ |
| 14 | +#define EXPORT_SYMBOL __attribute__((visibility("default"))) |
| 15 | +#else |
| 16 | +#define EXPORT_SYMBOL |
| 17 | +#endif |
| 18 | + |
| 19 | +namespace onnxruntime { |
| 20 | +namespace webgpu { |
| 21 | +void CleanupWebGpuContexts(); |
| 22 | +void CleanupKernelRegistries(); |
| 23 | +} // namespace webgpu |
| 24 | +} // namespace onnxruntime |
| 25 | + |
| 26 | +namespace google { |
| 27 | +namespace protobuf { |
| 28 | +void ShutdownProtobufLibrary(); |
| 29 | +} // namespace protobuf |
| 30 | +} // namespace google |
| 31 | + |
| 32 | +extern "C" { |
| 33 | +// |
| 34 | +// Public symbols |
| 35 | +// |
| 36 | +EXPORT_SYMBOL OrtStatus* CreateEpFactories(const char* registration_name, const OrtApiBase* ort_api_base, |
| 37 | + const OrtLogger* default_logger, |
| 38 | + OrtEpFactory** factories, size_t max_factories, size_t* num_factories) { |
| 39 | + // Manual init for the C++ API |
| 40 | + onnxruntime::ep::ApiInit(ort_api_base); |
| 41 | + |
| 42 | + if (max_factories < 1) { |
| 43 | + return onnxruntime::ep::Api().ort.CreateStatus(ORT_INVALID_ARGUMENT, |
| 44 | + "Not enough space to return EP factory. Need at least one."); |
| 45 | + } |
| 46 | + |
| 47 | + // Initialize the global default logger |
| 48 | + ::onnxruntime::ep::adapter::Logger::CreateDefaultLogger(default_logger); |
| 49 | + |
| 50 | + // Factory could use registration_name or define its own EP name. |
| 51 | + std::unique_ptr<OrtEpFactory> factory = std::make_unique<onnxruntime::webgpu::ep::Factory>(); |
| 52 | + |
| 53 | + factories[0] = factory.release(); |
| 54 | + *num_factories = 1; |
| 55 | + |
| 56 | + return nullptr; |
| 57 | +} |
| 58 | + |
| 59 | +EXPORT_SYMBOL OrtStatus* ReleaseEpFactory(OrtEpFactory* factory) { |
| 60 | + // STEP.1 - Release the factory |
| 61 | + delete static_cast<onnxruntime::webgpu::ep::Factory*>(factory); |
| 62 | + |
| 63 | + // STEP.2 - Clean up cached kernel registries |
| 64 | + onnxruntime::webgpu::CleanupKernelRegistries(); |
| 65 | + |
| 66 | + // STEP.3 - Clean up WebGPU contexts |
| 67 | + onnxruntime::webgpu::CleanupWebGpuContexts(); |
| 68 | + |
| 69 | + // STEP.4 - Destroy the global default logger wrapper |
| 70 | + ::onnxruntime::ep::adapter::Logger::DestroyDefaultLogger(); |
| 71 | + |
| 72 | + // STEP.5 - Shutdown protobuf library |
| 73 | + google::protobuf::ShutdownProtobufLibrary(); |
| 74 | + |
| 75 | + return nullptr; |
| 76 | +} |
| 77 | + |
| 78 | +} // extern "C" |
0 commit comments