Skip to content

fix(ascend): validate module-pair count and prevent under-allocation for Ascend 910C - #2270

Closed
princexpoddar wants to merge 3 commits into
Project-HAMi:masterfrom
princexpoddar:fix/ascend-910c-module-pairing
Closed

fix(ascend): validate module-pair count and prevent under-allocation for Ascend 910C#2270
princexpoddar wants to merge 3 commits into
Project-HAMi:masterfrom
princexpoddar:fix/ascend-910c-module-pairing

Conversation

@princexpoddar

@princexpoddar princexpoddar commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

Fixes Ascend 910C NPU device allocation in pkg/device/ascend/device.go.

In Fit(), verify that len(combination) == int(originReq) after calling computeBestCombination910C(). If enough complete module pairs cannot be formed, return false with AllocatedCardsInsufficientRequest instead of reporting success with a partial allocation.
Run computeBestCombination910C() for all Ascend910CType multi-device requests so the module-pair validation is not skipped when the raw candidate count already matches the requested count.
Change the sorting in computeBestCombination910C() to descending order so full cards are considered first, matching the intended behavior.
Add regression tests in pkg/device/ascend/device_910c_pairing_test.go covering partial allocation, the exact-count bypass, and a valid full-pair allocation.

Which issue(s) this PR fixes: Fixes #2268

Special notes for your reviewer: Tested with go test -v ./pkg/device/ascend/....

Does this PR introduce a user-facing change? No

AI assistance: Used Claude during development. Verified and tested the final changes.

Summary by CodeRabbit

  • Bug Fixes

    • Improved Ascend 910C device allocation to validate complete physical card pairs.
    • Requests with incomplete pairings now correctly fail instead of allocating fewer NPUs than requested.
    • Device selection now prioritizes fully available cards before partially available cards.
    • Preserved general allocation behavior for other device types.
  • Tests

    • Added regression coverage for incomplete and complete Ascend 910C pairing scenarios.

@hami-robot hami-robot Bot added the kind/bug Something isn't working label Aug 1, 2026
@hami-robot

hami-robot Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: princexpoddar
Once this PR has been reviewed and has the lgtm label, please assign wawa0210 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f3659e88-6816-4e2e-abe4-370eff27b934

📥 Commits

Reviewing files that changed from the base of the PR and between 7516c82 and 5292c57.

📒 Files selected for processing (1)
  • pkg/device/ascend/device_910c_pairing_test.go

📝 Walkthrough

Walkthrough

Ascend910C allocation now validates complete physical card pairs before success. It rejects under-allocation and exact-count candidates that lack valid pairs. It separates general topology selection, prioritizes full cards, and adds five regression tests.

Changes

Ascend 910C allocation

Layer / File(s) Summary
Pair validation and selection
pkg/device/ascend/device.go
Fit validates the full requested count after Ascend910C pairing. Short pairings return an insufficient-allocation reason. General topology requests use the combination selector. Full cards are considered before partial cards.
Pairing regression coverage
pkg/device/ascend/device_910c_pairing_test.go
Tests cover partial allocations, exact-count bypasses, missing NetworkID, no-full-pair results, and successful complete-pair allocations.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • Project-HAMi/HAMi#2238: Both changes modify Ascend Fit allocation-failure handling and add partial-allocation regression tests.

Suggested reviewers: ouyangluwei163, lengrongfu

Poem

A rabbit checks each card in line,
Two NPUs must pair just right.
Short requests now fail with care,
Full modules pass the allocation gate.
Five tests hop into the night.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the Ascend 910C module-pair validation fix and prevention of under-allocation.
Linked Issues check ✅ Passed The changes address all linked issue objectives: complete-pair validation, exact-count bypass prevention, correct sorting, and successful full-pair allocation.
Out of Scope Changes check ✅ Passed The changes are limited to Ascend 910C allocation logic and focused regression tests required by the linked issue.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
pkg/device/ascend/device_910c_pairing_test.go (1)

39-44: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert the required insufficient-allocation reason.

These tests pass for any rejection. Assert that reason contains common.AllocatedCardsInsufficientRequest. This verifies the scheduler-facing failure contract from the PR objective.

Also applies to: 72-77

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/device/ascend/device_910c_pairing_test.go` around lines 39 - 44, Update
the assertions in the Ascend 910C pairing test around dev.Fit to require that
reason contains common.AllocatedCardsInsufficientRequest, while preserving the
existing fit=false and allocation checks. Apply the same assertion to the
additional case noted in the review.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/device/ascend/device_910c_pairing_test.go`:
- Line 1: Add the repository-standard Apache 2.0 license header at the beginning
of device_910c_pairing_test.go, before the package ascend declaration, matching
the format used by other Go files.
- Around line 3-9: Reorder the imports in the device_910c_pairing_test.go import
block so the standard-library testing import remains first, the Kubernetes
imports follow as external dependencies, and the
github.com/Project-HAMi/HAMi/pkg/device import appears last.

In `@pkg/device/ascend/device.go`:
- Around line 579-581: Update the insufficient-allocation branch containing
reason[common.AllocatedCardsInsufficientRequest] to store len(combination)
instead of len(tmpDevs), so the reason reports the selected NPU count. Pass
originReq to common.GenReason for this allocation-specific failure path while
preserving the existing log and return behavior.
- Around line 569-575: Update the Ascend 910C allocation flow around
computeBestCombination910C so pair validation runs for eligible multi-device
requests regardless of whether NetworkID is present or needTopology is enabled.
Ensure requests such as indices 0 and 2 cannot bypass full-pair validation,
while preserving existing behavior for other device types and request sizes. Add
a regression case with CustomInfo["NetworkID"] absent.

---

Nitpick comments:
In `@pkg/device/ascend/device_910c_pairing_test.go`:
- Around line 39-44: Update the assertions in the Ascend 910C pairing test
around dev.Fit to require that reason contains
common.AllocatedCardsInsufficientRequest, while preserving the existing
fit=false and allocation checks. Apply the same assertion to the additional case
noted in the review.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2939ae09-b104-44fb-9680-5052ed6f1691

📥 Commits

Reviewing files that changed from the base of the PR and between c7891de and 6ac01b8.

📒 Files selected for processing (2)
  • pkg/device/ascend/device.go
  • pkg/device/ascend/device_910c_pairing_test.go

Comment thread pkg/device/ascend/device_910c_pairing_test.go
Comment thread pkg/device/ascend/device_910c_pairing_test.go
Comment thread pkg/device/ascend/device.go Outdated
Comment thread pkg/device/ascend/device.go Outdated
…for Ascend 910C

Signed-off-by: Prabal Poddar <prabalpoddar73@gmail.com>
… fix reason reporting

Always run computeBestCombination910C for multi-device Ascend 910C requests even when NetworkID is absent or needTopology is false. Update AllocatedCardsInsufficientRequest reason to report the selected NPU count and pass originReq to GenReason.

Signed-off-by: Prabal Poddar <prabalpoddar73@gmail.com>
@princexpoddar
princexpoddar force-pushed the fix/ascend-910c-module-pairing branch from 6ac01b8 to 7516c82 Compare August 2, 2026 04:36

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/device/ascend/device_910c_pairing_test.go (1)

56-61: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert the required rejection reason.

These tests only assert fit == false. They can pass if Fit rejects for an unrelated reason. Assert that reason reports common.AllocatedCardsInsufficientRequest in each incomplete-pair case.

Also applies to: 89-94, 149-154

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/device/ascend/device_910c_pairing_test.go` around lines 56 - 61, Update
the rejection assertions in the incomplete-pair cases around dev.Fit, including
the sections at the current and referenced locations, to verify that reason
equals common.AllocatedCardsInsufficientRequest in addition to asserting fit is
false. Preserve the existing allocation-count checks and apply the same expected
reason to every incomplete-pair scenario.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/device/ascend/device_910c_pairing_test.go`:
- Around line 56-61: Update the rejection assertions in the incomplete-pair
cases around dev.Fit, including the sections at the current and referenced
locations, to verify that reason equals common.AllocatedCardsInsufficientRequest
in addition to asserting fit is false. Preserve the existing allocation-count
checks and apply the same expected reason to every incomplete-pair scenario.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1be9858e-3bf2-4e2a-b576-4937b3d34beb

📥 Commits

Reviewing files that changed from the base of the PR and between 6ac01b8 and 7516c82.

📒 Files selected for processing (2)
  • pkg/device/ascend/device.go
  • pkg/device/ascend/device_910c_pairing_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/device/ascend/device.go

@mesutoezdil mesutoezdil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

heads up, #2035 refactors the same Fit block and its author owns the ascend area

// NetworkID is absent or needTopology is false, because
// candidates satisfying the count alone may still be spread
// across incomplete/partial modules rather than full pairs.
combination := npu.computeBestCombination910C(nodeInfo, int(originReq), tmpDevs[k.Type])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

forcing the pairing on every 910c multi request changes behavior for nodes w/o module info in CustomInfo, does computeBestCombination910C return empty there and make those pods unschedulable? pls cover that case w/ a test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a test for exactly this - TestComputeBestCombination910C_NoFullPairsReturnsEmpty. Checks both computeBestCombination910C directly (returns an empty slice, no panic, for 4 singletons on 4 separate modules with no NetworkID) and the end-to-end Fit() path (fit=false, clean rejection).

from what I could see in the current device plugin source NetworkID is always set to 0 for 910C, so this exact no-NetworkID scenario may be rare in practice but wanted the test in regardless, since Fit() shouldn't depend on that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added a test for exactly this - TestComputeBestCombination910C_NoFullPairsReturnsEmpty. Checks both computeBestCombination910C directly (returns an empty slice, no panic, for 4 singletons on 4 separate modules with no NetworkID) and the end-to-end Fit() path (fit=false, clean rejection).

from what I could see in the current device plugin source NetworkID is always set to 0 for 910C, so this exact no-NetworkID scenario may be rare in practice but wanted the test in regardless, since Fit() shouldn't depend on that.

You should already know the rule from your previous PRs
a reminder for the future:

Reminder: Answers must be written by human being. You can view the relevant rule here.
https://github.com/Project-HAMi/HAMi/blob/master/CONTRIBUTING.md#contribution-gates
"4. Review replies. The reply you post must be written by you and must address the specific point raised. Verbatim or canned AI replies, or replies that do not engage the comment, lead to the PR being closed."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

apologies for not addressing it directly by me...pls I would appreciate if you could reopen the PR..Also thanks for the heads up on #2035..as i checked it doesn't touch the needTopology branch or 910C pairing block this pr modifies..they can merge with no conflicts..will rebase if it changes

Verify that computeBestCombination910C returns empty slice and Fit returns fit=false when no candidate NPUs share a full module and NetworkID is absent.

Signed-off-by: Prabal Poddar <prabalpoddar73@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

silent under-allocation and topology bypass in Ascend 910C device allocation

2 participants