Skip to content

[Fix] Keep Buffer input validation active under python -O - #11

Open
morluto wants to merge 1 commit into
MoonshotAI:masterfrom
morluto:codex/public-api-validation
Open

[Fix] Keep Buffer input validation active under python -O#11
morluto wants to merge 1 commit into
MoonshotAI:masterfrom
morluto:codex/public-api-validation

Conversation

@morluto

@morluto morluto commented Jul 28, 2026

Copy link
Copy Markdown

Part of #1.

Summary

Replace correctness-critical assertions at the public Buffer boundary with
typed exceptions that remain active under python -O.

caller tensors and configuration
                |
                v
     +------------------------+
     | public Buffer boundary |
     | type, shape, layout    |
     | CUDA and exact device  |
     +-----------+------------+
                 |
        valid ---+---> planning / CUDA launch
                 |
      invalid ---+---> typed Python exception

Problem

Python removes assert statements when optimization is enabled. Public checks
for tensor dtype, shape, contiguity, device placement, required arguments, and
zero-copy aliasing could therefore disappear before malformed inputs reached
CUDA-facing code.

Change

Add a shared public tensor validator covering:

  • object type;
  • dtype;
  • shape;
  • contiguity;
  • CUDA placement;
  • exact Buffer device.

Apply explicit validation to:

  • dispatch hidden states, route weights, top-k indices, and per-expert counts;
  • prefetch weight tensors;
  • combine inputs and zero-copy aliases;
  • full and intermediate gradient-reduction tensors;
  • Buffer construction and context constraints;
  • destroyed or uninitialized Buffer use.

The exception types distinguish the failure category:

  • TypeError for incorrect Python types and tensor dtypes;
  • ValueError for incompatible shapes, layouts, devices, arguments, and
    aliases;
  • RuntimeError for destroyed or uninitialized buffers.

Scope

This PR covers the top-level Buffer API in api.py. Internal launch-time
assertions in planning, dispatch, combine, buffer allocation, and
gradient-reduction modules remain separate work under #1.

It does not include the separate num_sms policy or communication-plan
provenance changes.

Validation

  • Focused validation under python -O — passed.
  • Python compilation and formatting checks — passed.
  • Added or updated invalid-input coverage for dispatch, combine, prefetch,
    gradient reduction, and zero-copy aliasing.

@morluto morluto changed the title Validate Buffer inputs with explicit exceptions [Fix] Keep Buffer input validation active under python -O Jul 28, 2026
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.

1 participant