Skip to content

Commit

Permalink
POC for a combined compiled and export call to seed a cache
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-intel committed Nov 19, 2024
1 parent 236a854 commit 0b969a9
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/inference/dev_api/openvino/runtime/icore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ class OPENVINO_RUNTIME_API ICore {
const std::string& device_name,
const ov::AnyMap& config) const = 0;

/**
* @brief Creates a compiled model from a source model object and saves it to a file for future use.
*
* This can be more efficient than compile_mofrl(Model) + export_model(fstream).
*
* @param model Model object acquired from Core::read_model.
* @param device_name Name of a device to load a model to.
* @param filepath Name of the file and path where the compile model object is saved.
* @param properties Optional map of pairs: (property name, property value) relevant only for this load
* operation.
* @return A pointer to compiled model
*/
virtual ov::SoPtr<ov::ICompiledModel> compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& device_name,
const std::string& filepath,
const AnyMap& properties = {}) const = 0;

/**
* @brief Creates a compiled model from a previously exported model
* @param model model stream
Expand Down Expand Up @@ -241,7 +258,9 @@ class OPENVINO_RUNTIME_API ICore {
* @param keep_core_property Whether to return core-level properties
* @return map of properties that are supported by device
*/
virtual AnyMap get_supported_property(const std::string& full_device_name, const AnyMap& properties, const bool keep_core_property = true) const = 0;
virtual AnyMap get_supported_property(const std::string& full_device_name,
const AnyMap& properties,
const bool keep_core_property = true) const = 0;

virtual bool device_supports_model_caching(const std::string& device_name) const = 0;

Expand Down
10 changes: 10 additions & 0 deletions src/inference/dev_api/openvino/runtime/iplugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ class OPENVINO_RUNTIME_API IPlugin : public std::enable_shared_from_this<IPlugin
const ov::AnyMap& properties,
const ov::SoPtr<ov::IRemoteContext>& context) const = 0;

/**
* @brief Compiles model from ov::Model object
* @param model A model object acquired from ov::Core::read_model or source construction
* @param properties A ov::AnyMap of properties relevant only for this load operation
* @return Created Compiled Model object
*/
virtual std::shared_ptr<ov::ICompiledModel> compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& filepath,
const ov::AnyMap& properties) const;

/**
* @brief Sets properties for plugin, acceptable keys can be found in openvino/runtime/properties.hpp
* @param properties ov::AnyMap of properties
Expand Down
15 changes: 15 additions & 0 deletions src/inference/include/openvino/runtime/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,21 @@ class OPENVINO_RUNTIME_API Core {
return compile_model(model, context, AnyMap{std::forward<Properties>(properties)...});
}

/**
* @brief Creates a compiled model from a source model object and saves it to a file for future use.
*
* This can be more efficient than compile_mofrl(Model) + export_model(fstream).
*
* @param model Model object acquired from Core::read_model.
* @param device_name Name of a device to load a model to.
* @param filepath Name of the file and path where the compile model object is saved.
* @param properties Optional map of pairs: (property name, property value) relevant only for this load
* operation.
*/
CompiledModel compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& device_name, const std::string &filepath,
const AnyMap& properties = {}) const;

/**
* @brief Registers an extension to a Core object.
* @param library_path Path to the library with ov::Extension.
Expand Down
10 changes: 10 additions & 0 deletions src/inference/src/cpp/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ CompiledModel Core::compile_model(const std::shared_ptr<const ov::Model>& model,
});
}

CompiledModel Core::compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& device_name,
const std::string& filepath,
const AnyMap& config) const {
OV_CORE_CALL_STATEMENT({
auto exec = _impl->compile_model_to_disk(model, device_name, filepath, config);
return {exec._ptr, exec._so};
});
}

void Core::add_extension(const std::string& library_path) {
try {
add_extension(ov::detail::load_extensions(library_path));
Expand Down
14 changes: 14 additions & 0 deletions src/inference/src/dev/core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,20 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::string& mod
return compiled_model;
}

ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& device_name,
const std::string& filepath,
const ov::AnyMap& config) const {
OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::compile_model_to_disk");
auto parsed = parseDeviceNameIntoConfig(device_name, config);
auto plugin = get_plugin(parsed._deviceName);
ov::SoPtr<ov::ICompiledModel> compiled_model;

compiled_model = plugin.compile_model_to_disk(model, filepath, parsed._config);

return compiled_model;
}

ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::import_model(std::istream& model,
const std::string& device_name,
const ov::AnyMap& config) const {
Expand Down
11 changes: 9 additions & 2 deletions src/inference/src/dev/core_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#pragma once

#include "cache_guard.hpp"
#include "dev/plugin.hpp"
#include "cache_manager.hpp"
#include "dev/plugin.hpp"
#include "openvino/core/any.hpp"
#include "openvino/core/extension.hpp"
#include "openvino/core/so_extension.hpp"
Expand Down Expand Up @@ -275,6 +275,11 @@ class CoreImpl : public ov::ICore, public std::enable_shared_from_this<ov::ICore
const std::string& device_name,
const ov::AnyMap& config) const override;

ov::SoPtr<ov::ICompiledModel> compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& device_name,
const std::string& filepath,
const ov::AnyMap& config = {}) const override;

ov::SoPtr<ov::ICompiledModel> import_model(std::istream& model,
const std::string& device_name = {},
const ov::AnyMap& config = {}) const override;
Expand All @@ -291,7 +296,9 @@ class CoreImpl : public ov::ICore, public std::enable_shared_from_this<ov::ICore

ov::SoPtr<ov::IRemoteContext> create_context(const std::string& device_name, const AnyMap& args) const override;

ov::AnyMap get_supported_property(const std::string& device_name, const ov::AnyMap& config, const bool keep_core_property = true) const override;
ov::AnyMap get_supported_property(const std::string& device_name,
const ov::AnyMap& config,
const bool keep_core_property = true) const override;

ov::SoPtr<ov::IRemoteContext> get_default_context(const std::string& device_name) const override;

Expand Down
13 changes: 13 additions & 0 deletions src/inference/src/dev/iplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
//

#include <fstream>

#include "openvino/runtime/iplugin.hpp"

#include "openvino/op/convert.hpp"
Expand Down Expand Up @@ -79,6 +81,17 @@ std::shared_ptr<ov::ICompiledModel> ov::IPlugin::compile_model(const std::string
return compile_model(model, properties);
}

std::shared_ptr<ov::ICompiledModel> ov::IPlugin::compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& filepath,
const ov::AnyMap& properties) const {
auto core = get_core();
OPENVINO_ASSERT(core);
auto& compiled_model = compile_model(model, properties);
std::ofstream fs(filepath, std::ios::binary);
compiled_model->export_model(fs);
return compiled_model;
}

std::unordered_set<std::string> ov::get_supported_nodes(
const std::shared_ptr<const ov::Model>& model,
std::function<void(std::shared_ptr<ov::Model>&)> transform,
Expand Down
6 changes: 6 additions & 0 deletions src/inference/src/dev/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ ov::SoPtr<ov::ICompiledModel> ov::Plugin::compile_model(const std::shared_ptr<co
OV_PLUGIN_CALL_STATEMENT(return {m_ptr->compile_model(model, properties, context), m_so});
}

ov::SoPtr<ov::ICompiledModel> ov::Plugin::compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& filepath,
const ov::AnyMap& properties) const {
OV_PLUGIN_CALL_STATEMENT(return {m_ptr->compile_model_to_disk(model, filepath, properties), m_so});
}

ov::SupportedOpsMap ov::Plugin::query_model(const std::shared_ptr<const ov::Model>& model,
const ov::AnyMap& properties) const {
OV_PLUGIN_CALL_STATEMENT(return m_ptr->query_model(model, properties));
Expand Down
5 changes: 4 additions & 1 deletion src/inference/src/dev/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Plugin {
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const;

SoPtr<ov::ICompiledModel> compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& filepath,
const ov::AnyMap& properties) const;

ov::SupportedOpsMap query_model(const std::shared_ptr<const ov::Model>& model, const ov::AnyMap& properties) const;

SoPtr<ov::ICompiledModel> import_model(std::istream& model, const ov::AnyMap& properties) const;
Expand Down Expand Up @@ -78,4 +82,3 @@ class Plugin {
};

} // namespace ov

22 changes: 22 additions & 0 deletions src/plugins/intel_npu/src/al/include/intel_npu/config/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ struct COMPILATION_MODE_PARAMS final : OptionBase<COMPILATION_MODE_PARAMS, std::
}
};

//
// COMPILED_OUTPUT_FILE
//

struct COMPILED_OUTPUT_FILE final : OptionBase<COMPILED_OUTPUT_FILE, std::string> {
static std::string_view key() {
return ov::intel_npu::compiled_output_file.name();
}

static std::string defaultValue() {
return {};
}

static OptionMode mode() {
return OptionMode::CompileTime;
}

static bool isPublic() {
return false;
}
};

//
// DPU_GROUPS
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ inline std::istream& operator>>(std::istream& is, LegacyPriority& priority) {
*/
static constexpr ov::Property<LegacyPriority, ov::PropertyMutability::RO> legacy_model_priority{"MODEL_PRIORITY"};

/**
* @brief [Only for NPU Plugin]
* Type: Arbitrary string.
* This option allows to specify the file path to export the compiled model to directly from the driver.
*/
static constexpr ov::Property<std::string> compiled_output_file{"COMPILED_OUTPUT_FILE"};

/**
* @brief [Only for NPU Plugin]
* Type: Arbitrary string.
Expand Down
1 change: 1 addition & 0 deletions src/plugins/intel_npu/src/al/src/config/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void intel_npu::registerCompilerOptions(OptionsDesc& desc) {
desc.add<DMA_ENGINES>();
desc.add<DYNAMIC_SHAPE_TO_STATIC>();
desc.add<EXECUTION_MODE_HINT>();
desc.add<COMPILED_OUTPUT_FILE>();
}

//
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/intel_npu/src/plugin/include/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Plugin : public ov::IPlugin {
const ov::AnyMap& properties,
const ov::SoPtr<ov::IRemoteContext>& context) const override;

std::shared_ptr<ov::ICompiledModel> compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& filepath,
const ov::AnyMap& properties) const override;

ov::SoPtr<ov::IRemoteContext> create_context(const ov::AnyMap& remote_properties) const override;

ov::SoPtr<ov::IRemoteContext> get_default_context(const ov::AnyMap& remote_properties) const override;
Expand Down
11 changes: 11 additions & 0 deletions src/plugins/intel_npu/src/plugin/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,17 @@ ov::Any Plugin::get_property(const std::string& name, const ov::AnyMap& argument
OPENVINO_THROW("Unsupported configuration key: ", name);
}

std::shared_ptr<ov::ICompiledModel> Plugin::compile_model_to_disk(const std::shared_ptr<const ov::Model>& model,
const std::string& filepath,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::compile_model_to_disk");

ov::AnyMap combined_properties{properties};
combined_properties[ov::intel_npu::compiled_output_file.name()] = filepath;

return compile_model(model, combined_properties);
}

std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<const ov::Model>& model,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::compile_model");
Expand Down

0 comments on commit 0b969a9

Please sign in to comment.