Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class IGraph : public std::enable_shared_from_this<IGraph> {

virtual void set_argument_value(uint32_t argi, const void* argv) const = 0;

virtual void set_metadata(NetworkMetadata metadata) = 0;

virtual void initialize(const Config& config) = 0;

virtual ~IGraph() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Graph : public IGraph {

void set_argument_value(uint32_t argi, const void* argv) const override;

void set_metadata(NetworkMetadata metadata) override;

void initialize(const Config& config) override;

const NetworkMetadata& get_metadata() const override;
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Graph::Graph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
}
}

void Graph::set_metadata(NetworkMetadata metadata) {
_metadata = metadata;
}

const NetworkMetadata& Graph::get_metadata() const {
return _metadata;
}
Expand Down
53 changes: 51 additions & 2 deletions src/plugins/intel_npu/src/plugin/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,27 @@ ov::Any Plugin::get_property(const std::string& name, const ov::AnyMap& argument
return _properties->get_property(name, arguments);
}

std::shared_ptr<ov::Model> deBatchModel(std::shared_ptr<ov::Model>& model) {
size_t inputIdx = 0;
std::map<std::string, ov::PartialShape> newShapes;
for (auto&& item : model->get_parameters()) {
auto layout = item->get_layout();
auto partShape = item->get_partial_shape();
if (ov::layout::has_batch(layout)) {
partShape[ov::layout::batch_idx(layout)] = 1;
}
newShapes.emplace(item->get_friendly_name(), partShape);
inputIdx++;
}
model->reshape(newShapes);

return model;
}

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");
auto modelForCompilation = model->clone();

// Before going any further: if
// ... 1 - NPUW mode is activated
Expand Down Expand Up @@ -556,6 +574,27 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<
localConfig.update({{ov::intel_npu::batch_mode.name(), strStream.str()}});
}

ov::Dimension originalBatch;
bool modelDeBached = false;
if (localConfig.isAvailable(ov::intel_npu::batch_mode.name()) &&
localConfig.get<BATCH_MODE>() == ov::intel_npu::BatchMode::PLUGIN && model->is_dynamic()) {
try {
originalBatch = ov::get_batch(modelForCompilation);
ov::set_batch(modelForCompilation, 1);
modelDeBached = true;
} catch (const std::exception& ex) {
_logger.warning("The plugin couldn't resize a batched model due to exception: {0}.\nProbably, the "
"model is a dynamic model and layout hasn't been specified. Trying to debatch it...",
ex.what());
modelForCompilation = deBatchModel(modelForCompilation);
if (!modelForCompilation) {
OPENVINO_THROW("Cannot debatch a model");
}
_logger.info("The model has been debatched successfully");
modelDeBached = true;
}
}

if (localConfig.isAvailable(ov::intel_npu::batch_mode.name()) && !model->get_variables().empty()) {
if (localConfig.get<BATCH_MODE>() == ov::intel_npu::BatchMode::PLUGIN) {
OPENVINO_THROW("This model contains states, thus it is not supported when handling batching on the plugin");
Expand Down Expand Up @@ -614,10 +653,10 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<
_logger.debug("performing compile");

if (!localConfig.get<WEIGHTLESS_BLOB>()) {
graph = compiler->compile(model->clone(), localConfig);
graph = compiler->compile(modelForCompilation->clone(), localConfig);
} else {
check_weightless_cache_attribute_occurrence(model);
graph = compiler->compileWS(model->clone(), localConfig);
graph = compiler->compileWS(modelForCompilation->clone(), localConfig);
}
} catch (const std::exception& ex) {
OPENVINO_THROW(ex.what());
Expand All @@ -626,6 +665,16 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<
OPENVINO_THROW("NPU plugin: got an unexpected exception from compiler");
}

if (modelDeBached) {
auto metadata = graph->get_metadata();
for (auto& in : metadata.inputs) {
if (in.shapeFromIRModel.has_value() && originalBatch.get_max_length() != 1) {
in.shapeFromIRModel.value()[0] = originalBatch;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep originalShapesMap to avoid using [0].

}
}
graph->set_metadata(metadata);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose to extend NetworkMetadata class by aggregating additional layout information or better off introducing a new class alike PluginNetworkMetadata which will hold NetworkMetadata and layouts as well.

The purpose of adding this layout is to let user specify it so that we will stick to it instead of speculate with BATCH_AXIS position which is not equal to 0 in the generic case as we had ensured in previous PRs

}
Copy link
Contributor Author

@DariaMityagina DariaMityagina Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is necessary to preserve the original batch information. After reshaping the model in lines 660-676 and compiling it in line 736, the metadata will reflect shapeFromIRModel as the reshaped version, rather than the original.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Points to consider: Is it possible to avoid altering the metadata? Can we eliminate dependence on it when dealing with dynamic batch scenarios?


std::shared_ptr<ov::ICompiledModel> compiledModel;
try {
compiledModel = std::make_shared<CompiledModel>(model, shared_from_this(), device, graph, localConfig);
Expand Down
Loading