Skip to content

Feature - #2412

Closed
im-Toqeer-506 wants to merge 2 commits into
Project-HAMi:masterfrom
im-Toqeer-506:feature
Closed

Feature#2412
im-Toqeer-506 wants to merge 2 commits into
Project-HAMi:masterfrom
im-Toqeer-506:feature

Conversation

@im-Toqeer-506

@im-Toqeer-506 im-Toqeer-506 commented Aug 6, 2026

Copy link
Copy Markdown

What type of PR is this?

/kind feature
/kind failing-test

What this PR does / why we need it:

All 14 HAMi device backends implement the same device.Devices interface
(pkg/device/devices.go), but each is tested in isolation — so the same class of
contract violation keeps being rediscovered and fixed one backend at a time
(int32 overflow in GenerateResourceRequests: #2278/#2284/#2336; nil-map / nil-pointer
panics on the admission and Fit paths: #2254/#2294).

This PR lands phase 1 of a shared, table-driven conformance suite that runs one
set of contract assertions against every constructible backend, so a regression in
any of them fails CI immediately instead of shipping.

Included:

  • A test-only backend registry (conformanceCases()) that constructs 14 backend
    variants from plain in-memory configs mirroring the production resource names wired
    in pkg/scheduler/config.InitDevicesWithConfig.
  • Shared invariants:
    • GetResourceNames() returns at least one non-empty name (else the backend is
      unreachable by the scheduler).
    • A container requesting none of a backend's resources yields Nums == 0.
    • Fit(nil, …) and Fit([]*DeviceUsage{}, …) return false without panicking.
    • MutateAdmission does not panic on a pod that requests none of the resources.
    • No negative Nums/Memreq/Coresreq from GenerateResourceRequests — the
      int32-overflow guard.
  • The overflow invariant surfaced a previously-untracked overflow in metax-sgpu,
    which this PR fixes inline (reject out-of-range memory before the unchecked
    int32(mem) cast) — exactly the value the suite is meant to deliver.
  • Backends with dedicated open issues (cambricon [bug]: cambricon int32 overflow in GenerateResourceRequests silently drops memory request #2278, mthreads bug: int32 overflow in GenerateResourceRequests silently drops memory request (iluvatar, mthreads) #2284) go on an
    explicit, commented skip list linked to their tracking issues, so the gap is visible,
    not silent.

Kept additive and phased per the issue: ascend and iluvatar (slice / enable-flag
constructors) and further invariants (scoring monotonicity, PatchAnnotations
round-trip, lock idempotency) are documented as follow-up.

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

Special notes for your reviewer:

  • The overflow invariant has teeth: with the skip list disabled, cambricon and
    mthreads fail and metax-sgpu passes (confirming the metax fix is exercised).
  • Overflow "case 2" is gated on MemoryFactor > 0, so it targets the
    scaling-multiplication overflow that the bug reports describe; backends that don't
    scale still get the in-range non-negativity check (case 1).
  • The suite lives in the external device_test package on purpose — the backend
    sub-packages import pkg/device, so an internal test importing them back would
    create an import cycle.
  • Validation: go test -race ./pkg/device/... and ./pkg/scheduler/... all pass;
    gofmt, goimports (local-prefix), and go vet are clean.

Does this PR introduce a user-facing change?:

NONE


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **Bug Fixes**
  * Prevented invalid device resource requests when memory values exceed supported limits.
  * Invalid requests now fail safely instead of producing negative or corrupted resource values.

* **Tests**
  * Added comprehensive conformance coverage for registered device backends, including empty devices, unrelated containers, admission safety, resource names, and overflow handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Every hardware backend implements the same device.Devices interface
(pkg/device/devices.go), but each is otherwise tested in isolation, so the
same class of contract violation has repeatedly been fixed one backend at a
time (e.g. nil-map / nil-pointer panics on the admission and Fit paths in
Project-HAMi#2254 and Project-HAMi#2294).

Add pkg/device/conformance_test.go: a backend-agnostic suite that runs one
shared set of contract assertions against every constructible backend, so a
regression in any of them fails here immediately instead of shipping and
being rediscovered vendor-by-vendor.

Invariants asserted for all 14 constructible backends:
- registry guard: every case has a name and a non-nil backend
- GetResourceNames() advertises at least one non-empty resource name
  (a backend with none is unreachable by the scheduler)
- a container requesting none of a backend's resources yields Nums == 0
- Fit against nil and empty candidate lists returns false without panicking
- MutateAdmission on a pod with no accelerator request does not panic

The suite lives in the external device_test package on purpose: the backend
sub-packages import pkg/device, so an internal test importing them back
would create an import cycle.

The ascend and iluvatar backends (slice-returning constructors gated behind
enable flags) and the int32-overflow invariant for GenerateResourceRequests
are intentionally deferred to a follow-up, so this first pass stays green
while the underlying fixes land (Project-HAMi#2278, Project-HAMi#2284).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: M Toqeer Zia <muhammadtoqeerzia586694@gmail.com>
@hami-robot

hami-robot Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: im-Toqeer-506
Once this PR has been reviewed and has the lgtm label, please assign shouren 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

@hami-robot

hami-robot Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Welcome @im-Toqeer-506! It looks like this is your first PR to Project-HAMi/HAMi 🎉

@hami-robot hami-robot Bot added the size/L label Aug 6, 2026
@coderabbitai

coderabbitai Bot commented Aug 6, 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: 68024371-1f54-456b-9835-bb5bffe8981b

📥 Commits

Reviewing files that changed from the base of the PR and between 87d9795 and 7f88e4d.

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

📝 Walkthrough

Walkthrough

Added a backend-agnostic conformance suite for registered device backends. The suite checks construction, resource names, empty inputs, admission mutation, and overflow handling. Metax now rejects memory values outside the int32 range before conversion.

Changes

Device backend conformance

Layer / File(s) Summary
Backend registry and contracts
pkg/device/conformance_test.go
Registers representative NVIDIA, Cambricon, Hygon, Enflame, MThreads, Metax, Kunlun, AWS Neuron, AMD, Vastai, and Biren backends. Validates backend names, implementations, and scheduler resource names.
General backend behavior checks
pkg/device/conformance_test.go
Checks unrelated containers, nil and empty device lists, and admission mutation without accelerator requests.
Overflow request validation
pkg/device/conformance_test.go, pkg/device/metax/sdevice.go
Tests non-negative device, memory, and core requests. Metax rejects negative or over-math.MaxInt32 memory values before converting to int32.

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

Possibly related issues

Possibly related PRs

  • Project-HAMi/HAMi#2285 — Both changes address int32 memory overflow in backend resource request generation.
  • Project-HAMi/HAMi#2338 — Both changes modify Metax GenerateResourceRequests overflow handling.
  • Project-HAMi/HAMi#2388 — Both changes add range validation for GenerateResourceRequests, including the Metax implementation.

Suggested reviewers: archlitchi

Poem

A rabbit checks each backend bright,
Counts stay whole and ranges right.
Metax guards the numbers tall,
Empty pods no longer fall.
Across the registry, tests now run—
Safe requests for everyone.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is generic and does not identify the conformance test suite or the metax overflow fix. Replace "Feature" with a concise title that names the device conformance tests and overflow handling change.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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 requested a review from archlitchi August 6, 2026 15:47

@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/metax/sdevice.go`:
- Around line 259-266: Update the memory-request conversion flow before the
scaling assignment: reject failed AsInt64() conversions, validate the original v
against the allowable range after accounting for MemoryFactor, and only then
compute mem = v * MemoryFactor. Preserve the existing final mem range check in
the surrounding request-validation logic as defense in depth, and ensure invalid
or overflowing inputs do not produce a zero-valued request.
🪄 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: cda5f0ca-a473-481a-9eba-7d4c1ded1a71

📥 Commits

Reviewing files that changed from the base of the PR and between 87d9795 and ff663d0.

📒 Files selected for processing (2)
  • pkg/device/conformance_test.go
  • pkg/device/metax/sdevice.go

Comment on lines +259 to +266
// Reject out-of-range memory requests that would overflow int32.
// MemoryFactor is 1024, so a Gi request without a unit multiplies by that;
// a request above math.MaxInt32 wraps negative on the unchecked cast below.
if mem < 0 || mem > math.MaxInt32 {
klog.Errorf("container<%s> metax-sgpu memory request %d MiB is out of range", ctr.Name, mem)
return device.ContainerDeviceRequest{}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
factor = 1024
value = 1 << 54
wrapped = ((value * factor + (1 << 63)) % (1 << 64)) - (1 << 63)
assert 0 <= wrapped <= (1 << 31) - 1
print(f"wrapped int64 result: {wrapped}")
PY

Repository: Project-HAMi/HAMi

Length of output: 179


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate sdevice.go =="
fd -a 'sdevice\.go$' . | sed 's#^\./##'

echo "== relevant file info =="
file="$(fd 'sdevice\.go$' . | head -n1)"
wc -l "$file"
echo "== outline =="
ast-grep outline "$file" --view compact || true

echo "== relevant lines =="
sed -n '220,285p' "$file" | cat -n | sed 's/^/L/;s/L/ /'

echo "== imports header =="
sed -n '1,40p' "$file" | cat -n

Repository: Project-HAMi/HAMi

Length of output: 4335


Validate the memory quantity before scaling.

Line 262 runs after mem = v * MemoryFactor. With MemoryFactor == 1024, a value such as v == 1<<54 overflows int64 to 0, so this check accepts an out-of-range request and returns Memreq: 0. Failed AsInt64() conversions also leave mem at 0, which is interpreted as MemPercentagereq: 100. Reject failed AsInt64() conversions and validate v before multiplying; keep the final mem check as defense in depth.

🤖 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/metax/sdevice.go` around lines 259 - 266, Update the
memory-request conversion flow before the scaling assignment: reject failed
AsInt64() conversions, validate the original v against the allowable range after
accounting for MemoryFactor, and only then compute mem = v * MemoryFactor.
Preserve the existing final mem range check in the surrounding
request-validation logic as defense in depth, and ensure invalid or overflowing
inputs do not produce a zero-valued request.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

Signed-off-by: M Toqeer Zia <muhammadtoqeerzia586694@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@archlitchi

Copy link
Copy Markdown
Member

You need to edit the title, to reflect the feature you intend to implement

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.

2 participants