Repository navigation
Support Mojo kernels in Python submissions - #536
Merged
Merged
Conversation
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>
Coverage reportClick to see where and how coverage changed
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( |
Contributor
There was a problem hiding this comment.
Shouldn't we avoid using any word having "stream" 🤔?
kernelbot/src/kernelbot/api/api_utils.py
Lines 271 to 275 in 7f720fd
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #533 by @tolgacangoz. The approach is theirs: Mojo lives inside a normal
submission.pyand is compiled withmojo.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.pyembeds the Mojo source, imports it throughmojo.importer, and exposescustom_kernel. The extension check,#!POPCORNdirectives, the "stream" filter, KernelGuard, CPU precompile (skipped because there is noload_inline) andlang: pyrouting are all unchanged.Changes
max==26.7.0is pinned in the Modal image, with amojo.importer/mojo --versionsmoke check. Without it, a submission has to pip install at runtime, which gets flagged ashacked. It's pinned because Mojo syntax still changes between releases.examples/vectoradd_py/submission_mojo.pycontains no "stream" text, so it passes the existing filter. It launches on Mojo's defaultDeviceContextqueue. It uses a plainimportinstead ofimportlib, so KernelGuard classifies it asvalid(Propose to add Mojo🔥-based kernels #533's version wassuspiciousunder--profile strict).mojo.importerwrites the.mojosource and__mojocache__next tosubmission.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.Mojois accepted as a template language intask.ymland 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:
Evals that run
custom_kernelon 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 withtype(torch.cuda.current_stream())()and enter it withwith. On H100 that reported 0.06 ms for a 0.54 ms add and still passed checks. The filter is unchanged frommain.Tests
test_api_utils.py: the example passes the API's submission filters.test_run_eval.py: the private directory (fails onmain) and reuse of the CPU-artifact directory.test_task.py: the Mojo template.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
pytestenvironment,KERNELGUARD_ENABLED=1, submissions sent withcurlas the CLI does. The submission was this solution forvectoradd_v2:Earlier runner-level runs also passed every mode on T4, L4, A100, H100 and B200, including NCU profile mode on H100. No
.mojofiles were left in the container afterwards.Not covered
max..mojofile submissions are not supported.tests/test_api_utils.pybeforetests/test_admin_api.pymakes the admin tests fail with 401, because they patchADMIN_TOKENat import time.tests/test_profiling.pyalready triggers the same failure, and the default file order is fine.