// Copyright (c) MLLM Team. // Licensed under the MIT License. #include #include #include "mllm/core/BaseOp.hpp" #include "mllm/core/DeviceTypes.hpp" #include "mllm/engine/Context.hpp" #include "mllm/mllm.hpp" #include "mllm/backends/qnn/QNNBackend.hpp" #include "mllm/backends/qnn/QNNDispatcher.hpp" #include "CustomLayers.hpp" namespace mllm { // export initQnnBackend function to initialize QNN backend void initQnnBackend(const std::string& context_path, bool prepare_only) { MLLM_RT_ASSERT(isQnnAvailable()); auto& ctx = Context::instance(); // 1. Register backend auto backend = std::make_shared(); if (!context_path.empty() && std::filesystem::exists(context_path)) { if (prepare_only) { MLLM_ERROR_EXIT(1, "Refusing to overwrite existing QNN context in prepare-only mode: {}", context_path); } MLLM_INFO("QNN context path exists: {}", context_path); if (!backend->loadContext(context_path)) { MLLM_ERROR_EXIT(1, "Failed to load QNN context from {}", context_path); } else { MLLM_INFO("QNN context loaded successfully from {}", context_path); } } else { if (!backend->createContext(prepare_only)) { MLLM_ERROR_EXIT(1, "Failed to create QNN context"); } else { MLLM_INFO("QNN context created successfully"); } } ctx.registerBackend(backend); // 2. Initialize memory manager ctx.memoryManager()->registerAllocator(kQNN, backend->allocator(), { .really_large_tensor_threshold = 0, .using_buddy_mem_pool = false, }); MLLM_INFO("QNN memory manager registered"); // 3. Initialize dispatcher manager ctx.dispatcherManager()->registerDispatcher( createQNNDispatcher(ctx.dispatcherManager()->getExecutor(), qnn::QNNDispatcherOptions())); // register QNN custom ops Context::instance().registerCustomizedOp(kQNN, "DequantizeAdd", std::shared_ptr((BaseOpFactory*)(new qnn::DequantizeAddFactory()))); ctx.registerCustomizedOp(kQNN, "ShadowCorrectionPacket", std::make_shared()); } } // namespace mllm