Skip to content

Support Mojo kernels in Python submissions - #536

Open
msaroufim wants to merge 2 commits into
mainfrom
mojo-support
Open

msaroufim wants to merge 2 commits into
mainfrom
mojo-support

Conversation

@msaroufim

Copy link
Copy Markdown
Member

Supersedes #533 by @tolgacangoz. The approach is theirs: Mojo lives inside a normal submission.py and is compiled with mojo.importer. The commits credit them as co-author. This PR adds fixes found by running it end to end on Modal.

Approach

Mojo submissions are still Python submissions. submission.py embeds the Mojo source, imports it through mojo.importer, and exposes custom_kernel. Nothing between the CLI and the Modal function had to change: the .py extension check, #!POPCORN directives, KernelGuard, CPU precompile (skipped because there is no load_inline), and lang: py routing all work as they are.

Changes

  • Image: max==26.7.0 is pinned in the Modal image, with a mojo.importer / mojo --version smoke check. Without it, a submission has to pip install at runtime, which gets flagged as hacked. It is pinned because Mojo syntax still changes between releases.
  • Stream fix: the example launches on PyTorch's current stream via DeviceContext.create_external_stream. The version in Propose to add Mojo🔥-based kernels #533 launches on Mojo's own stream. That only works because the PMPP eval uses the legacy default stream. Under a side stream it races, and under CUDA graph capture the graph comes out empty ("The CUDA Graph is empty"). The Helion eval uses both. The new example also uses a plain import instead of importlib, so KernelGuard now classifies it as valid (Propose to add Mojo🔥-based kernels #533's version was suspicious under --profile strict).
  • Private working directory (separate commit): mojo.importer needs the .mojo file on disk and writes __mojocache__ next to it. run_pytorch_script used to remove only the files it created itself, and GPU containers are reused across users. So the next submission in a warm container could read the previous user's Mojo source. Each run now gets a temporary working directory that is deleted afterwards.
  • Templates: Mojo is accepted as a template language in task.yml and in the Discord template command (from Propose to add Mojo🔥-based kernels #533).

Tests

  • Unit: tests/test_run_eval.py, which fails on main, and a Mojo template case in tests/test_task.py.
  • Modal integration:
    • Mojo added to test_modal_launcher_python_script (T4, H100).
    • test_modal_mojo_leaderboard: H100 ranked run. Its timing must be above a lower bound set by memory bandwidth.
    • test_modal_mojo_uses_torch_current_stream: side-stream and CUDA-graph-replay check.

Verified before opening

I ran modal_run_config on the real cuda_image from this branch (KERNELBOT_NVCC_TOOLCHAIN=0, ephemeral app, separate Modal workspace):

GPU test benchmark leaderboard profile (NCU) stream/graph check
T4 pass pass pass (6.40 ms) – pass
L4 pass pass pass (6.83 ms) – pass
A100 pass pass pass (1.39 ms) – pass
H100 pass pass pass (0.84 ms) pass, 5 reports pass (#533's version: fail)
B200 pass pass pass (0.82 ms) – pass
  • H100 baselines, same run: Triton 0.66 ms, inline CUDA 0.84 ms. Both still pass with the new working directory, including NCU profile mode.
  • Leftover files: no .mojo files remained under /root or /tmp after any run.
  • Compile and call cost: the first Mojo import in a fresh directory takes about 7 s. Each run compiles once, and that build is reused by the eval worker process. Host overhead per custom_kernel call is about 11 µs, so no DLPack conversion is involved.

Not covered

  • The GitHub-runner images (AMD, self-hosted NVIDIA) don't install max.
  • Native .mojo file submissions are not supported. That would break the Python-only assumption in the extension check, KernelGuard, CPU precompile, the website editor, and the CLI.

msaroufim and others added 2 commits November 23, 2026 01:18
Submissions can leave files next to themselves, and warm Modal containers
serve many users. Mojo submissions in particular write their .mojo source
and a __mojocache__ build next to submission.py, which the next submission
in the same container could read. Each run_pytorch_script call now runs in
a temporary directory that is removed afterwards.
Mojo submissions stay ordinary Python submissions: submission.py embeds the
Mojo source, compiles it with mojo.importer, and exposes custom_kernel. The
platform changes are:

- Install a pinned max package (Mojo compiler, mojo.importer, MAX GPU libs)
  in the Modal image, so submissions no longer need a runtime pip install.
- Accept Mojo starter templates in task definitions and the Discord
  template command.
- Add a vectoradd example that launches on PyTorch's current stream via
  DeviceContext.create_external_stream. Mojo's default stream only works
  with evaluators that use the legacy default stream; side streams and CUDA
  graph capture miss the kernel otherwise.
- Cover the example in Modal integration tests: test mode on T4 and H100,
  a ranked run with a bandwidth lower bound on timings, and a side-stream
  and CUDA-graph check.

Co-authored-by: M. Tolga Cangöz <mtcangoz@gmail.com>
@github-actions

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/libkernelbot
  task.py
  utils.py
Project Total  

This report was generated by python-coverage-comment-action

Comment on lines +69 to +74
# Launch on PyTorch's current stream so ordering and CUDA-event timing
# match the evaluator.
var stream = self_ptr[].ctx.create_external_stream(
Pointer[NoneType, MutUntrackedOrigin](unsafe_from_address=Int(py=stream_py))
)
stream.enqueue_function(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't we avoid using any word having "stream" 🤔?

if "stream" in submission_code.lower():
raise HTTPException(
status_code=500,
detail="Your code contains work on another stream. This is not allowed and may result in your disqualification. If you think this is a mistake, please contact us.", # noqa: E501
)

@tolgacangoz

Copy link
Copy Markdown

Thanks a bunch for getting this over to the completion, Mark!

This branch has not been deployed

No deployments
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.

2 participants