capstan-rl 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ name: Deploy GitHub Pages
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ paths:
7
+ - "docs/**"
8
+ - ".github/workflows/pages.yml"
9
+ workflow_dispatch:
10
+
11
+ permissions:
12
+ contents: read
13
+ pages: write
14
+ id-token: write
15
+
16
+ concurrency:
17
+ group: pages
18
+ cancel-in-progress: false
19
+
20
+ jobs:
21
+ deploy:
22
+ runs-on: ubuntu-latest
23
+ environment:
24
+ name: github-pages
25
+ url: ${{ steps.deployment.outputs.page_url }}
26
+ steps:
27
+ - uses: actions/checkout@v4
28
+ - uses: actions/configure-pages@v5
29
+ - uses: actions/upload-pages-artifact@v3
30
+ with:
31
+ path: docs
32
+ - id: deployment
33
+ uses: actions/deploy-pages@v4
@@ -0,0 +1,38 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ push:
5
+ tags: ["v*"]
6
+
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+ permissions:
11
+ contents: read
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: astral-sh/setup-uv@v5
15
+ # A tag that doesn't match pyproject's version would publish the wrong number under the right name.
16
+ - run: |
17
+ version=$(python3 -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
18
+ test "v$version" = "$GITHUB_REF_NAME" || { echo "tag $GITHUB_REF_NAME != v$version"; exit 1; }
19
+ - run: uv build
20
+ - uses: actions/upload-artifact@v4
21
+ with:
22
+ name: dist
23
+ path: dist/
24
+
25
+ # Trusted publishing: PyPI accepts this workflow's OIDC identity, so no API token is stored anywhere.
26
+ # Only this job gets id-token, and the pypi environment can require a manual approval.
27
+ publish:
28
+ needs: build
29
+ runs-on: ubuntu-latest
30
+ environment: pypi
31
+ permissions:
32
+ id-token: write
33
+ steps:
34
+ - uses: actions/download-artifact@v4
35
+ with:
36
+ name: dist
37
+ path: dist/
38
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,226 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ *.lcov
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ # Pipfile.lock
97
+
98
+ # uv
99
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # uv.lock
103
+
104
+ # poetry
105
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
106
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
107
+ # commonly ignored for libraries.
108
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
109
+ # poetry.lock
110
+ # poetry.toml
111
+
112
+ # pdm
113
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
114
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
115
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
116
+ # pdm.lock
117
+ # pdm.toml
118
+ .pdm-python
119
+ .pdm-build/
120
+
121
+ # pixi
122
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
123
+ # pixi.lock
124
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
125
+ # in the .venv directory. It is recommended not to include this directory in version control.
126
+ .pixi/*
127
+ !.pixi/config.toml
128
+
129
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
130
+ __pypackages__/
131
+
132
+ # Celery stuff
133
+ celerybeat-schedule*
134
+ celerybeat.pid
135
+
136
+ # Redis
137
+ *.rdb
138
+ *.aof
139
+ *.pid
140
+
141
+ # RabbitMQ
142
+ mnesia/
143
+ rabbitmq/
144
+ rabbitmq-data/
145
+
146
+ # ActiveMQ
147
+ activemq-data/
148
+
149
+ # SageMath parsed files
150
+ *.sage.py
151
+
152
+ # Environments
153
+ .env
154
+ .envrc
155
+ .venv
156
+ env/
157
+ venv/
158
+ ENV/
159
+ env.bak/
160
+ venv.bak/
161
+
162
+ # Spyder project settings
163
+ .spyderproject
164
+ .spyproject
165
+
166
+ # Rope project settings
167
+ .ropeproject
168
+
169
+ # mkdocs/Zensical documentation
170
+ /site
171
+
172
+ # mypy
173
+ .mypy_cache/
174
+ .dmypy.json
175
+ dmypy.json
176
+
177
+ # Pyre type checker
178
+ .pyre/
179
+
180
+ # pytype static type analyzer
181
+ .pytype/
182
+
183
+ # Cython debug symbols
184
+ cython_debug/
185
+
186
+ # PyCharm
187
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
188
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
189
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
190
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
191
+ # .idea/
192
+
193
+ # Abstra
194
+ # Abstra is an AI-powered process automation framework.
195
+ # Ignore directories containing user credentials, local state, and settings.
196
+ # Learn more at https://abstra.io/docs
197
+ .abstra/
198
+
199
+ # Visual Studio Code
200
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
201
+ # can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
202
+ # and can be added to the global gitignore or merged into this file. However, if you prefer, you
203
+ # could uncomment the following to ignore the entire vscode folder
204
+ # .vscode/
205
+ # Temporary file for partial code execution
206
+ tempCodeRunnerFile.py
207
+
208
+ # Ruff stuff:
209
+ .ruff_cache/
210
+
211
+ # PyPI configuration file
212
+ .pypirc
213
+
214
+ # Marimo
215
+ marimo/_static/
216
+ marimo/_lsp/
217
+ __marimo__/
218
+
219
+ # Streamlit
220
+ .streamlit/secrets.toml
221
+
222
+ capstan.md
223
+ plan.md
224
+ drafts/
225
+ logs/
226
+ checkpoints.md
@@ -0,0 +1,39 @@
1
+ # AGENTS.md
2
+
3
+ Instructions for any coding agent working in this repo.
4
+
5
+ ## What this repo is
6
+
7
+ Capstan measures, verifies, and speeds up the rollout step of RL post-training for flow and diffusion models. Read [README.md](README.md) for scope. The current target is narrow: Qwen-Image OCR LoRA, verl-omni with MixGRPO, one GPU configuration. Do not build for anything outside that scope unless asked.
8
+
9
+ The three deliverables, in order:
10
+
11
+ 1. Profiler: per-phase time for one training step.
12
+ 2. Numerics check: rollout vs trainer log-prob drift, plus batch invariance.
13
+ 3. One optimisation, measured by the profiler and gated by the numerics check.
14
+
15
+ ## Numbers
16
+
17
+ - Every number in README.md or `bench/` comes from a real run. Never estimate, extrapolate, or fill in a placeholder with a plausible value.
18
+ - Each result records the GPU, card count, and verl-omni commit it ran on.
19
+ - Drift is reported per latent element. The importance ratio has the trainer log-prob in the numerator.
20
+ - The drift threshold is fixed before the run. Never change it after seeing results.
21
+ - A speedup is not a result without its drift number next to it.
22
+ - If a result contradicts a claim in README.md, change the claim to fit the result, not the other way round.
23
+
24
+ ## Code
25
+
26
+ - Write the smallest change that works. No speculative abstractions, config for values that never change, or scaffolding for later.
27
+ - No comments that restate the code. No header or docstring blocks at the top of files. Comment only what the code cannot say: a non-obvious reason, a numerical subtlety, a known limitation.
28
+ - Reuse what verl-omni and vllm-omni already provide before writing your own.
29
+ - Non-trivial logic leaves one runnable check behind.
30
+ - Do not add dependencies without asking.
31
+
32
+
33
+ ## Git and PRs
34
+
35
+ - Never commit or push unless the maintainer asks for it in that conversation.
36
+ - No `Co-Authored-By` trailers or any agent attribution in commits or PR bodies.
37
+ - One concern per PR. Keep each PR small enough to review line by line. Split large work into a chain of PRs.
38
+ - Branch from `main`. Never push to `main` directly.
39
+ - The maintainer reviews on GitHub and asks for changes in the agent session. Address each review comment in a new commit on the same branch, then report what changed for each comment.
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,117 @@
1
+ Metadata-Version: 2.5
2
+ Name: capstan-rl
3
+ Version: 0.1.0
4
+ Summary: Measure, verify, and speed up the rollout step of RL post-training for flow and diffusion models.
5
+ Project-URL: Repository, https://github.com/Vishal-Padia/capstan
6
+ License-Expression: Apache-2.0
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.11
9
+ Requires-Dist: numpy
10
+ Description-Content-Type: text/markdown
11
+
12
+ # capstan
13
+
14
+ Measure, verify, and speed up the rollout step of RL post-training for flow and diffusion models.
15
+
16
+ RL post-training for diffusion models spends most of its GPU time sampling trajectories (10–50 denoising steps per sample, doubled with CFG). Speeding up rollout is only safe if the sampler and trainer still compute the same per-step log-probabilities—otherwise training silently goes off-policy. Capstan sits on top of the RL framework you already run, profiles where step time goes, checks rollout-vs-trainer log-prob drift, and gates rollout optimisations on both.
17
+
18
+ ## Results
19
+
20
+ Proof of concept on **Qwen-Image OCR LoRA** with **MixGRPO** (10 denoising steps, 2-step random SDE window, `true_cfg_scale=4.0`), `verl-omni` `81f3b65`, 5-step runs on Modal H100s (timings averaged over steps 3–5):
21
+
22
+ | Check | Result | Hardware & setup | Details |
23
+ | --- | --- | --- | --- |
24
+ | Baseline profile | Rollout is **58.2%** of a 758 s step (update 19.6%, reward 14.3%, recompute 4.8%, sync 3.1%). Transformer forwards are **97.4%** of rollout GPU time. | 4x H100 80 GB, request mode | [bench/profile.md](bench/profile.md) |
25
+ | Baseline drift | **Fails** the 1% bar: **7.2%** of entries (370 / 5120) have \|ratio − 1\| > 1e-4, reaching 44% at `t = 673`. | Same run | [bench/drift.md](bench/drift.md) |
26
+ | Batched rollout | Rollout 294.7 → 104.3 s (**2.83x**); full step 533.0 → 345.2 s (**1.54x**). Drift unchanged (321 → 304 / 5120 over; baseline gate passes, `p = 0.76`). | 4x H100 80 GB HBM3, request vs step mode (`max_num_seqs=16`), same host | [bench/before_after.md](bench/before_after.md) |
27
+ | No `torch.compile` in rollout | No speed cost (rollout 104.7 → 102.8 s; step 363.5 → 358.3 s). **3.2x fewer** drift failures (6.6% → 2.05%, 340 → 105 / 5120; baseline gate passes, still fails 1% bar). | 4x H100 80 GB HBM3, step mode with `enforce_eager=True`, same host | [bench/before_after.md](bench/before_after.md) |
28
+ | Root cause of baseline drift | Reproduces at step 0 on 1 GPU. No single bad kernel: `diffusers` vs itself (unpadded vs padded prompt) diverges as much as vs `vllm-omni` (`1.8e-2` vs `2.1e-2` after CFG) across 60 `bf16` blocks. | 1x H100 80 GB HBM3 | [bench/consistency.md](bench/consistency.md) |
29
+ | Batch invariance | Max per-element sampler drift between batch 1 and 16: **1.19e-07** (one `fp32` ULP). | 1x H100 80 GB, step mode | [bench/drift.md](bench/drift.md#batch-invariance) |
30
+
31
+ **Recommended rollout config:** `step_execution=True`, `max_num_seqs=16`, `enforce_eager=True`.
32
+
33
+ Notes on reading the numbers:
34
+ - **Per-element drift:** `verl-omni`'s per-step log-prob is already a mean over latent elements. Drift is `trainer - rollout` per `(sample, SDE step)`, and the importance ratio is `exp(trainer - rollout)` (trainer in the numerator).
35
+ - **Host variance:** Two Modal 4x H100 hosts ran the identical request-mode recipe at 758 s/step vs 533 s/step. Every before/after comparison runs both halves in one container on one host.
36
+ - **Weight sync:** Sync is ~3–4% here because this is a LoRA run, not full-parameter training.
37
+
38
+ ## What it does
39
+
40
+ - **Profiler (`capstan run`, `capstan report`):** Hooks `verl-omni` timers and `vllm-omni`'s diffusion pipeline (via a `vllm.general_plugins` entry point, no fork) to split each training step into rollout, reward, recompute, update, sync, and idle—and splits rollout GPU time into text encode, ODE steps, SDE-window steps, and VAE decode.
41
+ - **Numerics check (`capstan report`, `capstan gate`):** Records rollout and trainer log-probs per step plus batch invariance across batch sizes.
42
+ - **Absolute threshold** (fixed before the first real run): `|ratio − 1| ≤ 1e-4` for at least 99% of `(sample, SDE step)` entries. `1e-4` is the recipe's own PPO clip window (`diffusion_loss.clip_ratio`).
43
+ - **Baseline gate** (fixed Nov 29, 2026, after the batching and eager runs and checked against them afterwards): because the stock recipe already fails the absolute bar, `capstan gate <before> <after>` fails an optimisation if it produces significantly more entries over `1e-4` than its paired same-host baseline (one-sided two-proportion z-test, `p < 0.05`).
44
+ - **Optimisations:** Driven by the profile, measured in paired same-host runs, and gated by the drift check.
45
+
46
+ ## Quickstart
47
+
48
+ ### Run locally
49
+
50
+ 1. Install `verl-omni` at commit `81f3b65` following its [install guide](https://github.com/verl-project/verl-omni/blob/main/docs/start/install.md), add `uv pip install -e ".[ocr]"`, and build the OCR dataset into `$WORKSPACE/data/ocr/qwen_image/` with its `examples/flowgrpo_trainer/data_process/qwenimage_ocr.py`.
51
+ 2. Clone Capstan and install it into the same active Python ≥ 3.11 venv:
52
+
53
+ ```bash
54
+ git clone https://github.com/Vishal-Padia/capstan
55
+ uv pip install -e capstan
56
+ ```
57
+
58
+ (For the CLI alone, `uv pip install capstan-rl` or `uv pip install git+https://github.com/Vishal-Padia/capstan` works once published.)
59
+
60
+ 3. From the `verl-omni` repo root, run MixGRPO through Capstan. [`bench/run_mixgrpo.sh`](bench/run_mixgrpo.sh) is `verl-omni`'s `run_qwen_image_ocr_lora_mixgrpo.sh` wrapped with `capstan run`, `calculate_log_probs=True`, and no checkpoints/validation/W&B:
61
+
62
+ ```bash
63
+ STEPS=5 bash /path/to/capstan/bench/run_mixgrpo.sh
64
+ ```
65
+
66
+ Recordings are written to `runs/mixgrpo/` (`phases_<pid>.jsonl` per process, `logprobs_step<step>_pid<pid>.npz` per step).
67
+
68
+ 4. Generate tables or gate a paired before/after run:
69
+
70
+ ```bash
71
+ capstan report runs/mixgrpo # writes profile.md and drift.md (--skip 2 default, --out DIR)
72
+ capstan gate runs/before runs/after # baseline z-test gate
73
+ ```
74
+
75
+ ### Run on Modal
76
+
77
+ [`bench/modal_app.py`](bench/modal_app.py) builds the exact `verl-omni` `81f3b65` + `vllm 0.33` image and stores models, data, and runs on a Modal volume (`capstan-models`). Both models are public; no Hugging Face token is needed. Requires `pip install modal && modal setup`.
78
+
79
+ ```bash
80
+ modal run bench/modal_app.py::stage # download models + build OCR dataset (run once first)
81
+ modal run bench/modal_app.py::run --job dry # tiny random-weight check on 1x A10G/A10 (~8 min)
82
+ modal run bench/modal_app.py::run --job batch-invariance # 1x H100, batch 1 vs 16
83
+ modal run bench/modal_app.py::run --job mixgrpo --name baseline # 4x H100 stock recipe
84
+ modal run bench/modal_app.py::run --job mixgrpo-pair --name paired # 4x H100 request vs step mode, same host
85
+ modal run bench/modal_app.py::run --job eager-pair --name eager # 4x H100 compile on vs off, same host
86
+ ```
87
+
88
+ - Every job saves `host.txt`, logs, timings, log-probs, and reports to the volume and downloads them to `logs/<name>/`.
89
+ - `stage` must be run once before any other job (including `dry`, which reads the OCR dataset).
90
+ - `dry` generates reports with `--skip 0`; to regenerate locally, pass `--skip 0 --out <dir>`.
91
+ - `--step-mode` on `dry` and `mixgrpo` enables `step_execution=True, max_num_seqs=16`.
92
+ - For long 4x H100 jobs on a flaky connection, `modal deploy bench/modal_app.py`, spawn via `modal.Function.from_name("capstan", "mixgrpo_pair").spawn(name, 5, "eager")`, and download later with `modal run bench/modal_app.py::fetch --name <name>`.
93
+
94
+ ## Status, limitations, and roadmap
95
+
96
+ **Tested:** Qwen-Image OCR LoRA MixGRPO on 4x H100 (Modal, `verl-omni` `81f3b65`, 5 steps), 1x H100 for batch invariance / consistency / per-block hooks, and tiny random-weight models on 1x A10/A10G.
97
+
98
+ **Untested:** Other models, frameworks, or GPUs; full-parameter training; multi-node; runs longer than 5 steps; held-out reward evaluation. The two optimisations are each measured on one host pair and may not transfer to other recipes or hardware. The run steps have only been verified end-to-end by the author (and a fresh-environment check of the Modal `dry` job).
99
+
100
+ **Roadmap (not built yet):**
101
+ - Generalize beyond `QwenImagePipelineWithLogProb` to other diffusion/flow image and video DiTs
102
+ - FP8 (and experimental FP4) rollout paths gated by the same numerics check
103
+ - Streaming audio / TTS and state-space models
104
+ - Dedicated rollout kernels, engine, and weight-sync path
105
+ - Adapters for `verl`, `prime-rl`, `SkyRL`, and `TRL`
106
+
107
+ **Non-goals:** Not a new RL algorithm, not a replacement for existing RL frameworks, and not an autoregressive text-LLM inference server (vLLM and SGLang already cover that).
108
+
109
+ ## Credits
110
+
111
+ A huge thank you to [Modal](https://modal.com) for giving us a generous amount of credits to experiment and build Capstan.
112
+
113
+ Built on [verl-omni](https://github.com/verl-project/verl-omni) and [vllm-omni](https://github.com/vllm-project/vllm-omni). Algorithms: [Flow-GRPO](https://arxiv.org/abs/2505.05470) and [MixGRPO](https://arxiv.org/abs/2507.21802). The batch-invariance check and direct rollout-vs-trainer log-prob drift reporting borrow from the vLLM team's train–inference consistency work ([Nov 2025](https://vllm.ai/blog/2025-11-10-bitwise-consistent-train-inference), [Oct 2026](https://vllm.ai/blog/2026-10-14-train-inference-consistency)); their work makes the two bitwise identical, while Capstan measures and gates the gap.
114
+
115
+ ## License
116
+
117
+ [Apache 2.0](LICENSE)