Skip to content

Commit 33c9b15

Browse files
committed
Code clean up
Signed-off-by: intelgaoxiong <xiong.gao@intel.com>
1 parent 7ad5df9 commit 33c9b15

3 files changed

Lines changed: 33 additions & 60 deletions

File tree

src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,21 +1205,6 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
12051205
}
12061206

12071207
// Compile multiple generate model variants with different sizes
1208-
// DBG: dump final effective config keys relevant to online partitioning / attention isolation
1209-
{
1210-
auto dump_key = [](const ov::AnyMap& cfg, const std::string& key, const std::string& label) {
1211-
auto it = cfg.find(key);
1212-
std::cout << "[LLM_CFG] " << label << " " << key << " = "
1213-
<< (it != cfg.end() ? it->second.as<std::string>() : "(not set)") << std::endl;
1214-
};
1215-
dump_key(prefill_config, "NPUW_ONLINE_PIPELINE", "PREFILL");
1216-
dump_key(prefill_config, "NPUW_ONLINE_ISOLATE", "PREFILL");
1217-
dump_key(prefill_config, "NPUW_ATTN", "PREFILL");
1218-
dump_key(generate_config, "NPUW_ONLINE_PIPELINE", "GENERATE");
1219-
dump_key(generate_config, "NPUW_ONLINE_ISOLATE", "GENERATE");
1220-
dump_key(generate_config, "NPUW_ATTN", "GENERATE");
1221-
dump_key(generate_config, "NPUW_UNFOLD_IREQS", "GENERATE");
1222-
}
12231208
compile_generate_model_variants(generate_model_variants, plugin, generate_config);
12241209

12251210
m_prefill_compiled = m_compiled_model_factory(prefill_model, plugin, prefill_config);

src/plugins/intel_npu/src/plugin/npuw/moe/moe_infer_utils.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,20 @@ ov::Tensor slice_expert_weight(const ov::Tensor& batched_weight, size_t expert_i
6666
}
6767

6868
namespace {
69-
// Returns the token-count dimension from a 4-D router output shape.
70-
// Two layout variants exist depending on the opset version used during model export:
71-
// Layout A: [num_experts, 1, token_num, 1] → returns shape[2]
72-
// Layout B: [num_experts, token_num, 1, 1] → returns shape[1]
73-
// ASSERTs if neither layout is recognised.
69+
// Returns the token-count dimension from a router output shape.
70+
// All supported layouts have a trailing singleton dim:
71+
// [N, token, 1] (3-D) → token at dim 1
72+
// [N, 1, token, 1] (4-D, dim1=1) → token at dim 2
73+
// [N, token, 1, 1] (4-D, dim1≠1) → token at dim 1
74+
// ASSERTs if rank ∉ {3, 4} or trailing dim ≠ 1.
7475
static size_t get_router_token_count(const ov::Shape& router_shape) {
75-
if (router_shape.size() == 3) {
76-
// 3-D [num_experts, token_count, 1] (Qwen3 prefill)
77-
NPUW_ASSERT(router_shape[2] == 1 && "Unexpected 3-D router shape: trailing dim must be 1");
78-
return router_shape[1];
79-
}
80-
NPUW_ASSERT(router_shape.size() == 4);
81-
if (router_shape[1] == 1 && router_shape[3] == 1)
82-
return router_shape[2]; // Layout A: [num_experts, 1, token_count, 1]
83-
if (router_shape[2] == 1 && router_shape[3] == 1)
84-
return router_shape[1]; // Layout B: [num_experts, token_count, 1, 1]
85-
NPUW_ASSERT(false && "Unexpected router output shape - cannot determine token dimension!");
86-
return 0; // unreachable, suppress warning
76+
const auto rank = router_shape.size();
77+
NPUW_ASSERT((rank == 3 || rank == 4) && router_shape.back() == 1 &&
78+
"Router shape must be 3-D or 4-D with trailing singleton");
79+
// [N, 1, token, 1] is the only layout where dim 1 is a singleton rather than the token dim.
80+
if (rank == 4 && router_shape[1] == 1)
81+
return router_shape[2]; // [N, 1, token, 1]
82+
return router_shape[1]; // [N, token, 1, 1] or [N, token, 1]
8783
}
8884
} // namespace
8985

src/plugins/intel_npu/src/plugin/npuw/moe_transformations/moe_transformation.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -385,38 +385,32 @@ std::optional<MoEDownstream> detect_and_transform_moe_downstream(const std::shar
385385
LOG_DEBUG("Detecting MoE downstream pattern...");
386386
LOG_BLOCK();
387387

388-
// Pattern to match: Parameter -> [Convert] -> ReduceSum
389-
// Looking for a parameter with shape [N, ..., W] where:
390-
// dim 0 = N (total experts, > 1)
391-
// dim n-1 = W (hidden_dim)
392-
// Accepted layouts:
393-
// 4-D: [N, 1, H, W] (GPT-OSS) or [N, H, 1, W] (Qwen, decoding)
394-
// 3-D: [N, token, W] (Qwen, prefill — token may be > 1)
395-
// The primary discriminator is check_downstream_pattern() (Parameter -> [Convert] -> ReduceSum).
388+
// Pattern: Parameter -> [Convert] -> ReduceSum, shape [N, ..., W] where:
389+
// rank 3: [N, token, W]
390+
// rank 4: [N, 1, H, W] or [N, H, 1, W]
391+
// shape[0] = N > 1 (num_experts).
396392
const auto& params = model->get_parameters();
397393

398394
for (size_t param_idx = 0; param_idx < params.size(); ++param_idx) {
399395
const auto& param = params[param_idx];
400396

401-
// Validate shape: must be 3-D or 4-D with dim 0 > 1
402-
auto param_shape = param->get_partial_shape();
403-
if (!param_shape.rank().is_static()) {
404-
continue;
405-
}
406-
const auto rank = param_shape.rank().get_length();
407-
if (rank < 3 || rank > 4) {
408-
continue;
409-
}
410-
411-
auto shape = param_shape.to_shape();
412-
if (shape[0] <= 1) {
397+
auto shape_opt = [&]() -> std::optional<ov::Shape> {
398+
const auto ps = param->get_partial_shape();
399+
if (!ps.rank().is_static())
400+
return std::nullopt;
401+
const auto rank = ps.rank().get_length();
402+
if (rank < 3 || rank > 4)
403+
return std::nullopt;
404+
const auto s = ps.to_shape();
405+
if (s[0] <= 1)
406+
return std::nullopt;
407+
if (rank == 4 && s[1] != 1 && s[2] != 1)
408+
return std::nullopt;
409+
return s;
410+
}();
411+
if (!shape_opt || !check_downstream_pattern(param))
413412
continue;
414-
}
415-
416-
// Check pattern: Parameter -> Convert -> ReduceSum
417-
if (!check_downstream_pattern(param)) {
418-
continue;
419-
}
413+
const auto& shape = *shape_opt;
420414

421415
LOG_DEBUG(" Found downstream pattern for Parameter: " << param->get_friendly_name());
422416
LOG_DEBUG(" Pattern matched: Parameter -> Convert -> ReduceSum");
@@ -468,7 +462,6 @@ std::optional<MoEDownstream> create_moe_downstream(const std::shared_ptr<ov::Mod
468462
LOG_BLOCK();
469463

470464
LOG_INFO("Using K=" << active_experts_num << " active experts for downstream");
471-
std::cout << "Using K=" << active_experts_num << " active experts for downstream" << std::endl;
472465

473466
auto downstream_info = detect_and_transform_moe_downstream(model, active_experts_num);
474467
if (downstream_info && downstream_info->is_valid()) {
@@ -482,7 +475,6 @@ std::optional<MoEDownstream> create_moe_downstream(const std::shared_ptr<ov::Mod
482475
}
483476

484477
LOG_WARN("Failed to create MoEDownstream - downstream pattern not found");
485-
std::cout << "Failed to create MoEDownstream - downstream pattern not found" << std::endl;
486478
return std::nullopt;
487479
}
488480

0 commit comments

Comments
 (0)