Skip to content

Fix bytes_per_key truncation in random kernels (Metal + CUDA)#3432

Merged
angeloskath merged 1 commit into
ml-explore:mainfrom
dogukanveziroglu:fix-random-bytes-per-key-overflow
Apr 21, 2026
Merged

Fix bytes_per_key truncation in random kernels (Metal + CUDA)#3432
angeloskath merged 1 commit into
ml-explore:mainfrom
dogukanveziroglu:fix-random-bytes-per-key-overflow

Conversation

@dogukanveziroglu

Copy link
Copy Markdown
Contributor

C++ passes bytes_per_key as size_t (8 bytes) but the kernels declared it as uint/uint32_t, truncating values above UINT32_MAX. Promote the kernel parameter to ulong/uint64_t so the full 64-bit value is read.

no measurable performance impact.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

C++ passes bytes_per_key as size_t (8 bytes) but the kernels declared
it as uint/uint32_t, truncating values above UINT32_MAX. Promote the
kernel parameter to ulong/uint64_t so the full 64-bit value is read.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for noticing this!

@angeloskath angeloskath merged commit 5f519ef into ml-explore:main Apr 21, 2026
16 checks passed
@dogukanveziroglu dogukanveziroglu deleted the fix-random-bytes-per-key-overflow branch April 22, 2026 12:36
ekryski added a commit to ekryski/mlx that referenced this pull request May 6, 2026
…dLocalStream, linalg.det)

Highlights:
- ThreadLocalStream in C++ (ml-explore#3405) — joins StreamOrDevice variant
- Segmented mm NAX kernel (ml-explore#3419) — new steel/gemm/kernels/steel_gemm_segmented{,_nax}
- mlx.core.linalg additions: determinant, sign-log-determinant (ml-explore#3416)
- NAX split-K tuning + addmm fix (ml-explore#3422)
- gather_mm CUDA + qmm CUDA fixes (ml-explore#3414, ml-explore#3417, ml-explore#3445)
- Scheduler::enqueue thread safety (ml-explore#3423)
- Random kernel bytes_per_key truncation fix (ml-explore#3432)
- Many smaller bug + CI fixes; nanobind 2.12.0 pin

Conflict resolution (5 files; all auto-merged cleanly, no manual edits):
- mlx/utils.h — `ThreadLocalStream` joins StreamOrDevice; our
  metal_retain_bound_buffers() helper preserved.
- mlx/backend/metal/device.cpp — upstream's "no Metal device available"
  error message coexists with our signpost/MLX_METAL_PROFILE perf
  diagnostic + hazard-mode retain logic.
- mlx/backend/metal/CMakeLists.txt — upstream's steel_gemm_segmented{,_nax}
  joins our kernel registrations.
- mlx/backend/metal/kernels/CMakeLists.txt — same: upstream segmented +
  our fused_gate_activation kernel.
- tests/scheduler_tests.cpp — upstream's "test thread local stream"
  joins our scheduler-test alignment for lazy command-encoder init.

No disputed conflicts; the auto-merge produced a coherent result with both
sides' work intact.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants