Skip to content

Support Mojo kernels in Python submissions - #536

Merged
msaroufim merged 7 commits into
mainfrom
mojo-support
Nov 23, 2026
Merged

msaroufim merged 7 commits into
mainfrom
mojo-support

Conversation

@msaroufim

@msaroufim msaroufim commented Nov 23, 2026 •

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 the fixes found by running it end to end.

Approach

Mojo submissions are still Python submissions. submission.py embeds the Mojo source, imports it through mojo.importer, and exposes custom_kernel. The extension check, #!POPCORN directives, the "stream" filter, KernelGuard, CPU precompile (skipped because there is no load_inline) and lang: py routing are all unchanged.

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's pinned because Mojo syntax still changes between releases.
  • Example: examples/vectoradd_py/submission_mojo.py contains no "stream" text, so it passes the existing filter. It launches on Mojo's default DeviceContext queue. It uses a plain import instead of importlib, so KernelGuard classifies it as valid (Propose to add Mojo🔥-based kernels #533's version was suspicious under --profile strict).
  • Private working directory: mojo.importer writes the .mojo source and __mojocache__ next to submission.py, and Modal reuses GPU containers across users. Before this change, the next submission in a warm container could read the previous user's Mojo source. Each Python run now gets a temporary working directory that is deleted afterwards. When CPU-artifact replay has already set one up (KERNELBOT_INLINE_WORKDIR), the run reuses it, because replay checks header dependencies against it.
  • 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).

Streams

Mojo's default queue is a separate CUDA stream from PyTorch's (a profiler trace shows stream 13 vs 7). It is a blocking stream, so it stays ordered with PyTorch's legacy default stream. That is the stream the PMPP-style evals check and time on, so results and timings are correct there:

  • 0.84 ms on H100 for 16384² fp16, about the same as inline CUDA.
  • 0/20 mismatches when reading results without a sync.

Evals that run custom_kernel on a side stream or inside a CUDA graph (Helion style) don't support Mojo. In that setup the ranked run rejects it on correctness, though benchmark mode reported 0.0 µs.

I tried allowlisting PyTorch's current stream in the filter so Mojo could follow it, and dropped the idea. Allowing the bare torch.cuda.current_stream() lets plain PyTorch code create a side stream with type(torch.cuda.current_stream())() and enter it with with. On H100 that reported 0.06 ms for a 0.54 ms add and still passed checks. The filter is unchanged from main.

Tests

  • Unit:
    • test_api_utils.py: the example passes the API's submission filters.
    • test_run_eval.py: the private directory (fails on main) and reuse of the CPU-artifact directory.
    • test_task.py: the Mojo template.
  • 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_is_ordered_with_torch: a delayed write on PyTorch's default stream must be visible to the Mojo kernel.

Verified on the gpu-mode Modal account, through the API

Setup: local API server and Postgres, runner from this branch in the pytest environment, KERNELGUARD_ENABLED=1, submissions sent with curl as the CLI does. The submission was this solution for vectoradd_v2:

GPU test leaderboard (public / secret)
H100 pass 836 / 835 µs
B200 pass 848 / 833 µs
A100 pass 1194 / 1212 µs
L4 pass 6746 / 6757 µs

Earlier runner-level runs also passed every mode on T4, L4, A100, H100 and B200, including NCU profile mode on H100. No .mojo files were left in the container afterwards.

Not covered

  • Side-stream and CUDA-graph evals (see Streams).
  • The GitHub-runner images (AMD, self-hosted NVIDIA) don't install max.
  • Native .mojo file submissions are not supported.
  • Test ordering: running tests/test_api_utils.py before tests/test_admin_api.py makes the admin tests fail with 401, because they patch ADMIN_TOKEN at import time. tests/test_profiling.py already triggers the same failure, and the default file order is fine.

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

github-actions Bot commented Nov 23, 2026 •

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
Contributor

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
)

@mtolgacangoz

Copy link
Copy Markdown
Contributor

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

The API and Discord reject any submission containing "stream", so the
current-stream version of the example could not be submitted. It now uses
Mojo's default DeviceContext queue. That is a separate CUDA stream, but a
blocking one, so it stays ordered with PyTorch's legacy default stream that
the evaluators time on. Evaluators that run custom_kernel on a side stream or
inside a CUDA graph would not see the kernel; none of the default-stream
problem evals do that.

The side-stream/graph integration test is replaced by one that checks this
ordering, and a unit test runs the example through the API's submission
filters.
Collecting it imported kernelbot.env before test_admin_api patched
ADMIN_TOKEN, which made the admin tests fail with 401.
The stream filter rejected any submission containing "stream", so a Mojo
kernel could not be handed PyTorch's stream and fell back to Mojo's own
queue, which side-stream and CUDA-graph evaluators do not see.

mentions_streams() now strips three exact uses before checking:
torch.cuda.current_stream(), its .cuda_stream handle, and Mojo's
create_external_stream(. Everything else that mentions streams is still
rejected. The API, the Discord submit command and the post-run warning all
use the same check.

The Mojo example launches on PyTorch's current stream again, and its
integration test covers the default stream, a side stream and CUDA graph
replay.
Allowlisting the bare torch.cuda.current_stream() let submissions build a
new stream with type(torch.cuda.current_stream())() or .__class__() and
enter it as a context manager, with no "stream" text left. On H100 that
reported 0.06 ms for a 0.54 ms add and still passed checks. Only the
.cuda_stream handle (an int) and create_external_stream( stay allowed.

run_pytorch_script now reuses KERNELBOT_INLINE_WORKDIR when CPU-artifact
replay set it. That directory is already private, and replay checks header
dependencies against it; a second scratch directory made those checks miss
and silently recompile on the GPU.
Loosening the anti-cheat filter for every submission is not worth it: the
first version allowed a side-stream timing bypass for plain PyTorch code,
and the tightened one is still hard to prove safe. The filter is back to
main's behaviour and the Mojo example uses Mojo's default queue again.

That queue is a blocking stream ordered with PyTorch's default stream, so
default-stream evaluators like PMPP check and time it correctly. Evaluators
that run custom_kernel on side streams or in CUDA graphs do not support
Mojo; their ranked runs reject it on correctness.
@msaroufim
msaroufim merged commit b4ac612 into main Nov 23, 2026
4 checks passed
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