Skip to content

Commit

Permalink
[NPU] Create compiler adapter factory (#27962)
Browse files Browse the repository at this point in the history
### Details:
Clean-up code - create compiler adapter factory class to create the
proper compiler adapter.

### Tickets:
 - *CVS-158848*

Signed-off-by: Bogdan Pereanu <[email protected]>
  • Loading branch information
pereanub authored Dec 10, 2024
1 parent 535f807 commit 773c214
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/plugins/intel_npu/src/backend/include/zero_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class ZeroEngineBackend final : public IEngineBackend {
bool isCommandQueueExtSupported() const override;
bool isLUIDExtSupported() const override;

const std::shared_ptr<ZeroInitStructsHolder>& getInitStruct() const;

void* getContext() const override;

void updateInfo(const Config& config) override;

const std::shared_ptr<ZeroInitStructsHolder> getInitStructs() const override;

private:
std::shared_ptr<ZeroInitStructsHolder> _initStruct;

Expand Down
8 changes: 4 additions & 4 deletions src/plugins/intel_npu/src/backend/src/zero_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ void* ZeroEngineBackend::getContext() const {
return _initStruct->getContext();
}

const std::shared_ptr<ZeroInitStructsHolder>& ZeroEngineBackend::getInitStruct() const {
return _initStruct;
}

void ZeroEngineBackend::updateInfo(const Config& config) {
_logger.setLevel(config.get<LOG_LEVEL>());
if (_devices.size() > 0) {
Expand All @@ -85,4 +81,8 @@ void ZeroEngineBackend::updateInfo(const Config& config) {
}
}

const std::shared_ptr<ZeroInitStructsHolder> ZeroEngineBackend::getInitStructs() const {
return _initStruct;
}

} // namespace intel_npu
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "intel_npu/common/igraph.hpp"

namespace intel_npu {

class ICompilerAdapter {
public:
virtual std::shared_ptr<IGraph> compile(const std::shared_ptr<const ov::Model>& model,
const Config& config) const = 0;
virtual std::shared_ptr<IGraph> parse(std::vector<uint8_t> network, const Config& config) const = 0;
virtual ov::SupportedOpsMap query(const std::shared_ptr<const ov::Model>& model, const Config& config) const = 0;

virtual ~ICompilerAdapter() = default;
};

} // namespace intel_npu
13 changes: 3 additions & 10 deletions src/plugins/intel_npu/src/common/include/intel_npu/common/npu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "intel_npu/common/igraph.hpp"
#include "intel_npu/common/sync_infer_request.hpp"
#include "intel_npu/config/config.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
#include "openvino/runtime/intel_npu/remote_properties.hpp"
#include "openvino/runtime/iremote_context.hpp"
#include "openvino/runtime/properties.hpp"
Expand Down Expand Up @@ -47,23 +48,15 @@ class IEngineBackend : public std::enable_shared_from_this<IEngineBackend> {
virtual void* getContext() const;
/** @brief Update backend and device info */
virtual void updateInfo(const Config& config) = 0;
/** @brief Get LevelZero structures */
virtual const std::shared_ptr<ZeroInitStructsHolder> getInitStructs() const;

protected:
virtual ~IEngineBackend() = default;
};

//------------------------------------------------------------------------------

class ICompilerAdapter {
public:
virtual std::shared_ptr<IGraph> compile(const std::shared_ptr<const ov::Model>& model,
const Config& config) const = 0;
virtual std::shared_ptr<IGraph> parse(std::vector<uint8_t> network, const Config& config) const = 0;
virtual ov::SupportedOpsMap query(const std::shared_ptr<const ov::Model>& model, const Config& config) const = 0;

virtual ~ICompilerAdapter() = default;
};

//------------------------------------------------------------------------------

class IDevice : public std::enable_shared_from_this<IDevice> {
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/intel_npu/src/common/src/npu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ IDevice::Uuid IDevice::getUuid() const {
OPENVINO_THROW("Get UUID not supported");
}

const std::shared_ptr<ZeroInitStructsHolder> IEngineBackend::getInitStructs() const {
return nullptr;
}

ov::device::LUID IDevice::getLUID() const {
OPENVINO_THROW("Get LUID not supported");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "driver_compiler_adapter.hpp"
#include "intel_npu/common/icompiler_adapter.hpp"
#include "intel_npu/config/compiler.hpp"
#include "intel_npu/config/config.hpp"
#include "plugin_compiler_adapter.hpp"

namespace intel_npu {

class CompilerAdapterFactory final {
public:
const std::unique_ptr<ICompilerAdapter> getCompiler(const ov::SoPtr<IEngineBackend>& engineBackend,
const Config& config) const {
auto compilerType = config.get<COMPILER_TYPE>();
switch (compilerType) {
case ov::intel_npu::CompilerType::MLIR: {
if (engineBackend->getName() != "LEVEL0") {
return std::make_unique<PluginCompilerAdapter>(nullptr);
}

return std::make_unique<PluginCompilerAdapter>(engineBackend->getInitStructs());
}
case ov::intel_npu::CompilerType::DRIVER: {
if (engineBackend->getName() != "LEVEL0") {
OPENVINO_THROW("NPU Compiler Adapter must be used with LEVEL0 backend");
}

return std::make_unique<DriverCompilerAdapter>(engineBackend->getInitStructs());
}
default:
OPENVINO_THROW("Invalid NPU_COMPILER_TYPE");
}
}
};

} // namespace intel_npu
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

#pragma once

#include <ze_api.h>
#include <ze_graph_ext.h>

#include <type_traits>
#include <utility>

#include "intel_npu/common/npu.hpp"
#include "intel_npu/common/icompiler_adapter.hpp"
#include "intel_npu/config/config.hpp"
#include "intel_npu/utils/logger/logger.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include "intel_npu/common/npu.hpp"
#include "intel_npu/common/icompiler_adapter.hpp"
#include "intel_npu/icompiler.hpp"
#include "intel_npu/utils/logger/logger.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "driver_compiler_adapter.hpp"

#include <ze_graph_ext.h>

#include <regex>
#include <string_view>

Expand All @@ -21,7 +19,6 @@
#include "intel_npu/utils/zero/zero_utils.hpp"
#include "ir_serializer.hpp"
#include "openvino/core/model.hpp"
#include "ze_graph_ext_wrappers.hpp"

namespace {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "plugin_compiler_adapter.hpp"

#include <ze_graph_ext.h>

#include <memory>
#include <string>

Expand All @@ -19,7 +17,6 @@
#include "openvino/util/file_util.hpp"
#include "openvino/util/shared_object.hpp"
#include "plugin_graph.hpp"
#include "ze_graph_ext_wrappers.hpp"

namespace {
std::shared_ptr<void> loadLibrary(const std::string& libpath) {
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/intel_npu/src/plugin/include/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class Plugin : public ov::IPlugin {
const ov::AnyMap& properties) const override;

private:
std::unique_ptr<ICompilerAdapter> getCompiler(const Config& config) const;

std::shared_ptr<NPUBackends> _backends;

std::map<std::string, std::string> _config;
Expand Down
54 changes: 11 additions & 43 deletions src/plugins/intel_npu/src/plugin/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
#include <fstream>

#include "compiled_model.hpp"
#include "npuw/compiled_model.hpp"
#include "driver_compiler_adapter.hpp"
#include "compiler_adapter_factory.hpp"
#include "intel_npu/common/device_helpers.hpp"
#include "intel_npu/common/icompiler_adapter.hpp"
#include "intel_npu/common/igraph.hpp"
#include "intel_npu/common/itt.hpp"
#include "intel_npu/config/common.hpp"
#include "intel_npu/config/compiler.hpp"
#include "intel_npu/config/npuw.hpp"
#include "intel_npu/config/runtime.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
#include "npuw/compiled_model.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/runtime/intel_npu/properties.hpp"
#include "openvino/runtime/properties.hpp"
#include "plugin_compiler_adapter.hpp"
#include "remote_context.hpp"
#include "zero_backend.hpp"

using namespace intel_npu;

Expand Down Expand Up @@ -699,8 +698,9 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<
}
}

auto original_model = model->clone();
auto compiler = getCompiler(localConfig);
auto originalModel = model->clone();
CompilerAdapterFactory compilerAdapterFactory;
auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig);

OV_ITT_TASK_NEXT(PLUGIN_COMPILE_MODEL, "compile");
std::shared_ptr<intel_npu::IGraph> graph;
Expand All @@ -716,7 +716,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<

std::shared_ptr<ov::ICompiledModel> compiledModel;
try {
compiledModel = std::make_shared<CompiledModel>(original_model, shared_from_this(), device, graph, localConfig);
compiledModel = std::make_shared<CompiledModel>(originalModel, shared_from_this(), device, graph, localConfig);
} catch (const std::exception& ex) {
OPENVINO_THROW(ex.what());
} catch (...) {
Expand Down Expand Up @@ -772,7 +772,8 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, c
std::shared_ptr<ov::ICompiledModel> compiledModel;

try {
auto compiler = getCompiler(localConfig);
CompilerAdapterFactory compilerAdapterFactory;
auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig);

auto graphSize = getFileSize(stream);

Expand Down Expand Up @@ -821,7 +822,8 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>&
const auto platform = _backends->getCompilationPlatform(localConfig.get<PLATFORM>(), localConfig.get<DEVICE_ID>());
localConfig.update({{ov::intel_npu::platform.name(), platform}});

auto compiler = getCompiler(localConfig);
CompilerAdapterFactory compilerAdapterFactory;
auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig);
ov::SupportedOpsMap supportedOpsMap;
try {
supportedOpsMap = compiler->query(model, localConfig);
Expand All @@ -834,40 +836,6 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>&
return supportedOpsMap;
}

std::unique_ptr<ICompilerAdapter> Plugin::getCompiler(const Config& config) const {
auto compilerType = config.get<COMPILER_TYPE>();
_logger.debug("performing createCompiler");

switch (compilerType) {
case ov::intel_npu::CompilerType::MLIR: {
if (_backends->getBackendName() != "LEVEL0") {
return std::make_unique<PluginCompilerAdapter>(nullptr);
}

auto zeroBackend = std::dynamic_pointer_cast<ZeroEngineBackend>(_backends->getIEngineBackend()._ptr);
if (zeroBackend == nullptr) {
return std::make_unique<PluginCompilerAdapter>(nullptr);
}

return std::make_unique<PluginCompilerAdapter>(zeroBackend->getInitStruct());
}
case ov::intel_npu::CompilerType::DRIVER: {
if (_backends->getBackendName() != "LEVEL0") {
OPENVINO_THROW("NPU Compiler Adapter must be used with LEVEL0 backend");
}

auto zeroBackend = std::dynamic_pointer_cast<ZeroEngineBackend>(_backends->getIEngineBackend()._ptr);
if (!zeroBackend) {
OPENVINO_THROW("Failed to cast zeroBackend, zeroBackend is a nullptr");
}

return std::make_unique<DriverCompilerAdapter>(zeroBackend->getInitStruct());
}
default:
OPENVINO_THROW("Invalid NPU_COMPILER_TYPE");
}
}

std::atomic<int> Plugin::_compiledModelLoadCounter{1};

static const ov::Version version = {CI_BUILD_NUMBER, NPU_PLUGIN_LIB_NAME};
Expand Down

0 comments on commit 773c214

Please sign in to comment.