Skip to content

fix(ascend): guard int32 narrowing in GenerateResourceRequests - #2601

Merged
hami-robot[bot] merged 4 commits into
Project-HAMi:masterfrom
Eshiv-Pandey:fix/ascend-int32-narrowing
Aug 18, 2026
Merged

fix(ascend): guard int32 narrowing in GenerateResourceRequests#2601
hami-robot[bot] merged 4 commits into
Project-HAMi:masterfrom
Eshiv-Pandey:fix/ascend-int32-narrowing

Conversation

@Eshiv-Pandey

@Eshiv-Pandey Eshiv-Pandey commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?
/kind bug

What this PR does / why we need it:

GenerateResourceRequests in the ascend backend converts user-controlled int64 resource limits (device count, memory, cores) into the int32 fields of ContainerDeviceRequest (Nums, Memreq, Coresreq) without bounds checking. A value exceeding math.MaxInt32 silently wraps to a small or negative number that flows into Fit(). A memory request that wraps to 0 makes the Fit() check (Totalmem - Usedmem < memreq) always pass, so the scheduler can place a pod onto an already-full NPU, causing oversubscription and OOM for other workloads on the same card.

This PR adds range guards that return the existing empty ContainerDeviceRequest{} rejection sentinel before every int32() narrowing, matching the pattern from #2388 (hygon, metax-sgpu). Memory is guarded both before and after the MemoryFactor multiplication, covering the soft-partitioning path (raw value) and the trimMemory path.

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

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

Oversized or invalid Ascend device resource requests (for example an ascend memory request such as 16Gi, or values exceeding the int32 range) are now rejected instead of silently wrapping to an incorrect value.

AI assistance disclosure: I used AI assistance (Claude Code) to help audit the backends, implement the guards, and draft the regression tests. I reviewed the change and verified the new tests fail without the guards and pass with them.

Summary by CodeRabbit

  • Bug Fixes

    • Improved validation of device, memory, and core resource requests.
    • Invalid, negative, or oversized values are now rejected safely.
    • Prevented errors caused by memory scaling exceeding supported limits.
  • Tests

    • Added coverage for invalid resource values and overflow scenarios across supported request paths.

GenerateResourceRequests narrowed user-controlled int64 limits (device
count, memory, cores) into the int32 fields of ContainerDeviceRequest
without bounds checking. A value over math.MaxInt32 silently wrapped to a
small or negative number that flowed into Fit(), letting the scheduler
place pods onto overcommitted NPUs. A byte-suffixed memory quantity such
as 16Gi wrapped to 0 on the soft-partitioning path, and MemPercentagereq
then defaulted to 0 as well, so the fit check could never reject it.

Guard every narrowing point before the int32 cast, returning the existing
empty ContainerDeviceRequest{} rejection sentinel, matching the pattern
from Project-HAMi#2388 (hygon, metax-sgpu). Memory guards cover both the pre-factor
value and the value after MemoryFactor scaling, on the soft-partitioning
and trimMemory paths alike. The memory error messages state that Ascend
memory is treated as MB not Byte, since a request like 16Gi is almost
always a wrong-unit mistake.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>
@hami-robot
hami-robot Bot requested a review from archlitchi August 11, 2026 16:46
@hami-robot
hami-robot Bot requested a review from lengrongfu August 11, 2026 16:46
@hami-robot hami-robot Bot added the size/L label Aug 11, 2026
@coderabbitai

coderabbitai Bot commented Aug 11, 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: 1cebad42-83ae-492d-a52f-b0fff0a743cb

📥 Commits

Reviewing files that changed from the base of the PR and between 9cead34 and 352c31f.

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

📝 Walkthrough

Walkthrough

Ascend resource request generation now validates device, memory, and core values against nonnegative int32 bounds. It rejects memory-factor multiplication overflow. Regression tests cover invalid inputs across soft-partitioning and template-trimming paths.

Changes

Ascend resource validation

Layer / File(s) Summary
Validate Ascend resource requests
pkg/device/ascend/device.go
GenerateResourceRequests rejects invalid or overflowing device, memory, and core values before int32 conversion. Memory-factor multiplication is checked for overflow.
Cover invalid request paths
pkg/device/ascend/device_test.go
Regression tests verify empty requests for oversized and negative values across soft-partitioning and template-trimming paths. Tests also cover memory-factor overflow.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Mergeability Score: ⚪ Minimal · up to 352c3

The change is localized to rejecting oversized or invalid Ascend resource requests before narrowing, with no actionable merge-blocking risk remaining beyond normal checks and review.

Possibly related issues

Possibly related PRs

Suggested reviewers: archlitchi

Poem

A rabbit checks each value twice,
No wrapped requests, no risky dice.
Memory scales within its range,
Cores and devices do not change.
Tests hop through each path with cheer.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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 fix for unchecked int32 narrowing in GenerateResourceRequests.
Linked Issues check ✅ Passed The implementation rejects negative and out-of-range device, memory, and core values before int32 conversion, including MemoryFactor paths, and adds regression tests [#2600].
Out of Scope Changes check ✅ Passed All implementation and test changes directly support the linked issue by preventing int32 overflow and validating affected Ascend resource paths.
✨ 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: 1

🤖 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.go`:
- Around line 331-334: Update the memory validation around mem.AsInt64() to
reject negative quantities using mem.Sign() < 0 before converting or evaluating
memnums, ensuring values such as -1m do not produce a valid 100% request.
Preserve the existing out-of-range handling for non-negative quantities and add
a regression case covering -1m.
🪄 Autofix

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: 2807a772-85d2-44e3-a0e7-94ee4ab7efc6

📥 Commits

Reviewing files that changed from the base of the PR and between 634bf2b and 3f9d743.

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

Comment thread pkg/device/ascend/device.go Outdated
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/device/ascend/device.go 91.30% 1 Missing and 1 partial ⚠️
Flag Coverage Δ
unittests 62.62% <91.30%> (+0.26%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
pkg/device/ascend/device.go 85.39% <91.30%> (+0.64%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions Bot added the kind/bug Something isn't working label Aug 11, 2026
A negative fractional quantity such as -1m returns ok=false from
AsInt64, so it bypassed the range guard and silently defaulted to a
100% memory request. Check mem.Sign() before AsInt64 to reject all
negative quantities, and drop the now-redundant memnums < 0 branch.
Add a -1m regression case and shorten the explanatory comments.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>
Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>
Comment thread pkg/device/ascend/device.go
The -1m test only exercised the fractional negative path where AsInt64
returns ok=false. A plain whole -100 passes AsInt64 (ok=true), so the
old int32 overflow guard alone would never catch it. This pins the
Sign() < 0 check to the whole-number case, directly covering the case
the reviewer asked about.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>

@archlitchi archlitchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@hami-robot

hami-robot Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, Eshiv-Pandey

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

The pull request process is described 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ascend: GenerateResourceRequests narrows user-controlled values to int32 without bounds checks

3 participants