MoonshotAI / MoonshotAI/MoonEP

Use proper exceptions instead of assert for user-facing validation

Open
#1 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
134
PR merge metrics
No merged PRs in 30d

Description

Problem

The codebase uses assert statements extensively for input validation and invariant checking (in api.py, buffer.py, planning.py, dispatch.py, combine.py, etc.). In Python, assert statements are stripped when running with -O (optimize) flag, which is common in production environments. This means all safety checks silently disappear in production.

Example

# buffer.py — currently
assert src_rank != self.rank, "Cannot map local tensor"

# Should be
if src_rank == self.rank:
    raise ValueError("Cannot map local tensor: src_rank equals current rank")

Scope

Affected files:

  • moonep/api.py — context creation, dispatch/combine validation
  • moonep/buffer.py — VMM allocation, IPC fd exchange
  • moonep/planning.py — planning kernel validation
  • moonep/dispatch.py — dispatch kernel validation
  • moonep/combine.py — combine kernel validation
  • moonep/grad_reduce.py — grad reduce validation

Recommendation

Replace all user-facing assert with ValueError / RuntimeError / TypeError. Keep assert only for truly internal invariants that should never fire regardless of input.

Priority order:

  1. buffer.py — IPC fd exchange and VMM allocation (resource leak risk)
  2. api.py — top-level API validation (user-facing)
  3. Kernel launch functions — shape/dtype validation

Impact

Production safety. Without this fix, running with python -O silently disables all validation, potentially causing undefined behavior (wrong results, GPU crashes, or silent memory corruption) instead of clear error messages.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by auditing the assertions in moonep/buffer.py and moonep/api.py, then review planning.py, dispatch.py, combine.py, and grad_reduce.py for user-facing validation. Run the relevant entry points with and without Python's -O flag to compare behavior. Done means user-facing checks raise suitable exceptions under optimization while truly internal invariants remain assertions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, distributed-systems
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.