@@ -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