Spaces:
Runtime error
Runtime error
rai-sant commited on
Commit ·
e35fa73
1
Parent(s): b3663f5
Add requirements.txt with dependencies
Browse files- .DS_Store +0 -0
- Dockerfile +17 -0
- LICENSE +201 -0
- Makefile +20 -0
- README.md +255 -12
- assets/plan-of-attack.png +0 -0
- configs/ddp.yaml +16 -0
- configs/zero2.yaml +21 -0
- configs/zero3.yaml +22 -0
- requirements.txt +7 -0
- setup.cfg +41 -0
- setup.py +135 -0
- slurm/evaluate.slurm +50 -0
- slurm/generate.slurm +201 -0
- slurm/sft.slurm +88 -0
- src/open_r1/__init__.py +36 -0
- src/open_r1/evaluate.py +103 -0
- src/open_r1/generate.py +156 -0
- src/open_r1/grpo.py +141 -0
- src/open_r1/sft.py +101 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
# Copy and install requirements
|
| 10 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy application files
|
| 14 |
+
COPY --chown=user . /app
|
| 15 |
+
|
| 16 |
+
# Set the application to run on port 7860
|
| 17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License
|
| 2 |
+
Version 2.0, January 2004
|
| 3 |
+
http://www.apache.org/licenses/
|
| 4 |
+
|
| 5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 6 |
+
|
| 7 |
+
1. Definitions.
|
| 8 |
+
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 36 |
+
Object form, made available under the License, as indicated by a
|
| 37 |
+
copyright notice that is included in or attached to the work
|
| 38 |
+
(an example is provided in the Appendix below).
|
| 39 |
+
|
| 40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 41 |
+
form, that is based on (or derived from) the Work and for which the
|
| 42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 44 |
+
of this License, Derivative Works shall not include works that remain
|
| 45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 46 |
+
the Work and Derivative Works thereof.
|
| 47 |
+
|
| 48 |
+
"Contribution" shall mean any work of authorship, including
|
| 49 |
+
the original version of the Work and any modifications or additions
|
| 50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 54 |
+
means any form of electronic, verbal, or written communication sent
|
| 55 |
+
to the Licensor or its representatives, including but not limited to
|
| 56 |
+
communication on electronic mailing lists, source code control systems,
|
| 57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 59 |
+
excluding communication that is conspicuously marked or otherwise
|
| 60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 61 |
+
|
| 62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 64 |
+
subsequently incorporated within the Work.
|
| 65 |
+
|
| 66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 71 |
+
Work and such Derivative Works in Source or Object form.
|
| 72 |
+
|
| 73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 76 |
+
(except as stated in this section) patent license to make, have made,
|
| 77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 78 |
+
where such license applies only to those patent claims licensable
|
| 79 |
+
by such Contributor that are necessarily infringed by their
|
| 80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 82 |
+
institute patent litigation against any entity (including a
|
| 83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 84 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 85 |
+
or contributory patent infringement, then any patent licenses
|
| 86 |
+
granted to You under this License for that Work shall terminate
|
| 87 |
+
as of the date such litigation is filed.
|
| 88 |
+
|
| 89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 90 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 91 |
+
modifications, and in Source or Object form, provided that You
|
| 92 |
+
meet the following conditions:
|
| 93 |
+
|
| 94 |
+
(a) You must give any other recipients of the Work or
|
| 95 |
+
Derivative Works a copy of this License; and
|
| 96 |
+
|
| 97 |
+
(b) You must cause any modified files to carry prominent notices
|
| 98 |
+
stating that You changed the files; and
|
| 99 |
+
|
| 100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 101 |
+
that You distribute, all copyright, patent, trademark, and
|
| 102 |
+
attribution notices from the Source form of the Work,
|
| 103 |
+
excluding those notices that do not pertain to any part of
|
| 104 |
+
the Derivative Works; and
|
| 105 |
+
|
| 106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 107 |
+
distribution, then any Derivative Works that You distribute must
|
| 108 |
+
include a readable copy of the attribution notices contained
|
| 109 |
+
within such NOTICE file, excluding those notices that do not
|
| 110 |
+
pertain to any part of the Derivative Works, in at least one
|
| 111 |
+
of the following places: within a NOTICE text file distributed
|
| 112 |
+
as part of the Derivative Works; within the Source form or
|
| 113 |
+
documentation, if provided along with the Derivative Works; or,
|
| 114 |
+
within a display generated by the Derivative Works, if and
|
| 115 |
+
wherever such third-party notices normally appear. The contents
|
| 116 |
+
of the NOTICE file are for informational purposes only and
|
| 117 |
+
do not modify the License. You may add Your own attribution
|
| 118 |
+
notices within Derivative Works that You distribute, alongside
|
| 119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 120 |
+
that such additional attribution notices cannot be construed
|
| 121 |
+
as modifying the License.
|
| 122 |
+
|
| 123 |
+
You may add Your own copyright statement to Your modifications and
|
| 124 |
+
may provide additional or different license terms and conditions
|
| 125 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 126 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 127 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 128 |
+
the conditions stated in this License.
|
| 129 |
+
|
| 130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 132 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 133 |
+
this License, without any additional terms or conditions.
|
| 134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 135 |
+
the terms of any separate license agreement you may have executed
|
| 136 |
+
with Licensor regarding such Contributions.
|
| 137 |
+
|
| 138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 140 |
+
except as required for reasonable and customary use in describing the
|
| 141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 142 |
+
|
| 143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 144 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 147 |
+
implied, including, without limitation, any warranties or conditions
|
| 148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 150 |
+
appropriateness of using or redistributing the Work and assume any
|
| 151 |
+
risks associated with Your exercise of permissions under this License.
|
| 152 |
+
|
| 153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 154 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 155 |
+
unless required by applicable law (such as deliberate and grossly
|
| 156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 157 |
+
liable to You for damages, including any direct, indirect, special,
|
| 158 |
+
incidental, or consequential damages of any character arising as a
|
| 159 |
+
result of this License or out of the use or inability to use the
|
| 160 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 161 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 162 |
+
other commercial damages or losses), even if such Contributor
|
| 163 |
+
has been advised of the possibility of such damages.
|
| 164 |
+
|
| 165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 168 |
+
or other liability obligations and/or rights consistent with this
|
| 169 |
+
License. However, in accepting such obligations, You may act only
|
| 170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 171 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 172 |
+
defend, and hold each Contributor harmless for any liability
|
| 173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 174 |
+
of your accepting any such warranty or additional liability.
|
| 175 |
+
|
| 176 |
+
END OF TERMS AND CONDITIONS
|
| 177 |
+
|
| 178 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 179 |
+
|
| 180 |
+
To apply the Apache License to your work, attach the following
|
| 181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 182 |
+
replaced with your own identifying information. (Don't include
|
| 183 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 184 |
+
comment syntax for the file format. We also recommend that a
|
| 185 |
+
file or class name and description of purpose be included on the
|
| 186 |
+
same "printed page" as the copyright notice for easier
|
| 187 |
+
identification within third-party archives.
|
| 188 |
+
|
| 189 |
+
Copyright [yyyy] [name of copyright owner]
|
| 190 |
+
|
| 191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 192 |
+
you may not use this file except in compliance with the License.
|
| 193 |
+
You may obtain a copy of the License at
|
| 194 |
+
|
| 195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 196 |
+
|
| 197 |
+
Unless required by applicable law or agreed to in writing, software
|
| 198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 200 |
+
See the License for the specific language governing permissions and
|
| 201 |
+
limitations under the License.
|
Makefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.PHONY: style quality
|
| 2 |
+
|
| 3 |
+
# make sure to test the local checkout in scripts and not the pre-installed one (don't use quotes!)
|
| 4 |
+
export PYTHONPATH = src
|
| 5 |
+
|
| 6 |
+
check_dirs := src
|
| 7 |
+
|
| 8 |
+
style:
|
| 9 |
+
black --line-length 119 --target-version py310 $(check_dirs) setup.py
|
| 10 |
+
isort $(check_dirs) setup.py
|
| 11 |
+
|
| 12 |
+
quality:
|
| 13 |
+
black --check --line-length 119 --target-version py310 $(check_dirs) setup.py
|
| 14 |
+
isort --check-only $(check_dirs) setup.py
|
| 15 |
+
flake8 --max-line-length 119 $(check_dirs) setup.py
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Evaluation
|
| 19 |
+
|
| 20 |
+
evaluate:
|
README.md
CHANGED
|
@@ -1,12 +1,255 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Open R1
|
| 2 |
+
|
| 3 |
+
*A fully open reproduction of DeepSeek-R1. This repo is work in progress, let's build it together!*
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
The goal of this repo is to build the missing pieces of the R1 pipeline such that everybody can reproduce and build on top of it. The project is simple by design and mostly consists of:
|
| 8 |
+
|
| 9 |
+
- `src/open_r1` contains the scripts to train and evaluate models as well generate synthetic data:
|
| 10 |
+
- `grpo.py`: trains a model with GRPO on a given dataset.
|
| 11 |
+
- `sft.py`: simple SFT of a model on a dataset.
|
| 12 |
+
- `evaluate.py`: evaluates a model on the R1 benchmarks.
|
| 13 |
+
- `generate.py`: generate synthetic data from a model using [Distilabel](https://github.com/argilla-io/distilabel).
|
| 14 |
+
- `Makefile` contains an easy to run command for each step in the R1 pipeline leveraging the scripts above.
|
| 15 |
+
|
| 16 |
+
### Plan of attack
|
| 17 |
+
|
| 18 |
+
We will use the DeepSeek-R1 [tech report](https://github.com/deepseek-ai/DeepSeek-R1) as a guide, which can roughly be broken down into three main steps:
|
| 19 |
+
|
| 20 |
+
* Step 1: replicate the R1-Distill models by distilling a high-quality corpus from DeepSeek-R1.
|
| 21 |
+
* Step 2: replicate the pure RL pipeline that DeepSeek used to create R1-Zero. This will likely involve curating new, large-scale datasets for math, reasoning, and code.
|
| 22 |
+
* Step 3: show we can go from base model to RL-tuned via multi-stage training.
|
| 23 |
+
|
| 24 |
+
<center>
|
| 25 |
+
<img src="assets/plan-of-attack.png" width="500">
|
| 26 |
+
</center>
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
## Installation
|
| 30 |
+
|
| 31 |
+
To run the code in this project, first, create a Python virtual environment using e.g. Conda:
|
| 32 |
+
|
| 33 |
+
```shell
|
| 34 |
+
conda create -n openr1 python=3.11 && conda activate openr1
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
Next, install vLLM:
|
| 38 |
+
|
| 39 |
+
```shell
|
| 40 |
+
pip install vllm==0.6.6.post1
|
| 41 |
+
|
| 42 |
+
# For HF (cluster only has CUDA 12.1)
|
| 43 |
+
pip install vllm==0.6.6.post1 --extra-index-url https://download.pytorch.org/whl/cu121
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
This will also install PyTorch `v2.5.1` and it is **very important** to use this version since the vLLM binaries are compiled for it. You can then install the remaining dependencies for your specific use case via `pip install -e .[LIST OF MODES]`. For most contributors, we recommend:
|
| 47 |
+
|
| 48 |
+
```shell
|
| 49 |
+
pip install -e ".[dev]"
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
Next, log into your Hugging Face and Weights and Biases accounts as follows:
|
| 53 |
+
|
| 54 |
+
```shell
|
| 55 |
+
huggingface-cli login
|
| 56 |
+
wandb login
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
Finally, check your system has Git LFS installed so that you can load and push models/datasets to the Hugging Face Hub:
|
| 60 |
+
|
| 61 |
+
```shell
|
| 62 |
+
git-lfs --version
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
If it isn't installed, run:
|
| 66 |
+
|
| 67 |
+
```shell
|
| 68 |
+
sudo apt-get install git-lfs
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Training models
|
| 72 |
+
|
| 73 |
+
We support training models with either DDP or DeepSpeed ZeRO-2 and ZeRO-3. To switch between methods, simply change the path to the `accelerate` YAML config in `configs`.
|
| 74 |
+
|
| 75 |
+
> [!NOTE]
|
| 76 |
+
> The training commands below are configured for a node of 8 x H100s (80GB). For different hardware and topologies, you may need to tune the batch size and number of gradient accumulation steps.
|
| 77 |
+
|
| 78 |
+
### SFT
|
| 79 |
+
|
| 80 |
+
To run SFT on a dataset distilled from DeepSeek-R1 with reasoning traces such as [Bespoke-Stratos-17k](https://huggingface.co/datasets/bespokelabs/Bespoke-Stratos-17k), run:
|
| 81 |
+
|
| 82 |
+
```
|
| 83 |
+
accelerate launch --config_file=configs/zero3.yaml src/open_r1/sft.py \
|
| 84 |
+
--model_name_or_path Qwen/Qwen2.5-Math-1.5B-Instruct \
|
| 85 |
+
--dataset_name HuggingFaceH4/Bespoke-Stratos-17k \
|
| 86 |
+
--learning_rate 2.0e-5 \
|
| 87 |
+
--num_train_epochs 1 \
|
| 88 |
+
--packing \
|
| 89 |
+
--max_seq_length 4096 \
|
| 90 |
+
--per_device_train_batch_size 4 \
|
| 91 |
+
--per_device_eval_batch_size 4 \
|
| 92 |
+
--gradient_accumulation_steps 4 \
|
| 93 |
+
--gradient_checkpointing \
|
| 94 |
+
--bf16 \
|
| 95 |
+
--logging_steps 5 \
|
| 96 |
+
--eval_strategy steps \
|
| 97 |
+
--eval_steps 100 \
|
| 98 |
+
--output_dir data/Qwen2.5-1.5B-Open-R1-Distill
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
To launch a Slurm job, run:
|
| 102 |
+
|
| 103 |
+
```shell
|
| 104 |
+
sbatch --output=/path/to/logs/%x-%j.out --err=/path/to/logs/%x-%j.err slurm/sft.slurm {model} {dataset} {accelerator}
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
Here `{model}` and `{dataset}` refer to the model and dataset IDs on the Hugging Face Hub, while `{accelerator}` refers to the choice of 🤗 Accelerate config in `configs`.
|
| 108 |
+
|
| 109 |
+
### GRPO
|
| 110 |
+
|
| 111 |
+
```
|
| 112 |
+
accelerate launch --config_file configs/zero3.yaml src/open_r1/grpo.py \
|
| 113 |
+
--output_dir DeepSeek-R1-Distill-Qwen-7B-GRPO \
|
| 114 |
+
--model_name_or_path deepseek-ai/DeepSeek-R1-Distill-Qwen-7B \
|
| 115 |
+
--dataset_name AI-MO/NuminaMath-TIR \
|
| 116 |
+
--max_prompt_length 256 \
|
| 117 |
+
--per_device_train_batch_size 1 \
|
| 118 |
+
--gradient_accumulation_steps 16 \
|
| 119 |
+
--logging_steps 10 \
|
| 120 |
+
--bf16
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
## Evaluating models
|
| 124 |
+
|
| 125 |
+
We use `lighteval` to evaluate models, with custom tasks defined in `src/open_r1/evaluate.py`. For models which fit on a single GPU, run:
|
| 126 |
+
|
| 127 |
+
```shell
|
| 128 |
+
MODEL=deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
|
| 129 |
+
MODEL_ARGS="pretrained=$MODEL,dtype=float16,max_model_length=32768,gpu_memory_utilisation=0.8"
|
| 130 |
+
TASK=aime24
|
| 131 |
+
OUTPUT_DIR=data/evals/$MODEL
|
| 132 |
+
|
| 133 |
+
lighteval vllm $MODEL_ARGS "custom|$TASK|0|0" \
|
| 134 |
+
--custom-tasks src/open_r1/evaluate.py \
|
| 135 |
+
--use-chat-template \
|
| 136 |
+
--system-prompt="Please reason step by step, and put your final answer within \boxed{}." \
|
| 137 |
+
--output-dir $OUTPUT_DIR
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
To increase throughput across multiple GPUs, use _data parallel_ as follows:
|
| 141 |
+
|
| 142 |
+
```shell
|
| 143 |
+
NUM_GPUS=8
|
| 144 |
+
MODEL=deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
|
| 145 |
+
MODEL_ARGS="pretrained=$MODEL,dtype=float16,data_parallel_size=$NUM_GPUS,max_model_length=32768,gpu_memory_utilisation=0.8"
|
| 146 |
+
TASK=aime24
|
| 147 |
+
OUTPUT_DIR=data/evals/$MODEL
|
| 148 |
+
|
| 149 |
+
lighteval vllm $MODEL_ARGS "custom|$TASK|0|0" \
|
| 150 |
+
--custom-tasks src/open_r1/evaluate.py \
|
| 151 |
+
--use-chat-template \
|
| 152 |
+
--system-prompt="Please reason step by step, and put your final answer within \boxed{}." \
|
| 153 |
+
--output-dir $OUTPUT_DIR
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
For large models which require sharding across GPUs, use _tensor parallel_ and run:
|
| 157 |
+
|
| 158 |
+
```shell
|
| 159 |
+
NUM_GPUS=8
|
| 160 |
+
MODEL=deepseek-ai/DeepSeek-R1-Distill-Qwen-32B
|
| 161 |
+
MODEL_ARGS="pretrained=$MODEL,dtype=float16,tensor_parallel_size=$NUM_GPUS,max_model_length=32768,gpu_memory_utilisation=0.8"
|
| 162 |
+
TASK=aime24
|
| 163 |
+
OUTPUT_DIR=data/evals/$MODEL
|
| 164 |
+
|
| 165 |
+
export VLLM_WORKER_MULTIPROC_METHOD=spawn
|
| 166 |
+
lighteval vllm $MODEL_ARGS "custom|$TASK|0|0" \
|
| 167 |
+
--custom-tasks src/open_r1/evaluate.py \
|
| 168 |
+
--use-chat-template \
|
| 169 |
+
--system-prompt="Please reason step by step, and put your final answer within \boxed{}." \
|
| 170 |
+
--output-dir $OUTPUT_DIR
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
## Data generation
|
| 174 |
+
|
| 175 |
+
### Generate data from a smol distilled R1 model
|
| 176 |
+
|
| 177 |
+
The following example can be run in 1xH100.
|
| 178 |
+
First install the following dependencies:
|
| 179 |
+
|
| 180 |
+
```shell
|
| 181 |
+
pip install "distilabel[vllm]>=1.5.2"
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
Now save the following snippet into a file named `pipeline.py` and run with `python pipeline.py`. It will generate for each of the 10 examples 4 generations (change the username for the repository to your org/user name):
|
| 185 |
+
|
| 186 |
+
```python
|
| 187 |
+
from datasets import load_dataset
|
| 188 |
+
from distilabel.models import vLLM
|
| 189 |
+
from distilabel.pipeline import Pipeline
|
| 190 |
+
from distilabel.steps.tasks import TextGeneration
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
prompt_template = """\
|
| 194 |
+
You will be given a problem. Please reason step by step, and put your final answer within \boxed{}:
|
| 195 |
+
{{ instruction }}"""
|
| 196 |
+
|
| 197 |
+
dataset = load_dataset("AI-MO/NuminaMath-TIR", split="train").select(range(10))
|
| 198 |
+
|
| 199 |
+
model_id = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B" # Exchange with another smol distilled r1
|
| 200 |
+
|
| 201 |
+
with Pipeline(
|
| 202 |
+
name="distill-qwen-7b-r1",
|
| 203 |
+
description="A pipeline to generate data from a distilled r1 model",
|
| 204 |
+
) as pipeline:
|
| 205 |
+
|
| 206 |
+
llm = vLLM(
|
| 207 |
+
model=model_id,
|
| 208 |
+
tokenizer=model_id,
|
| 209 |
+
extra_kwargs={
|
| 210 |
+
"tensor_parallel_size": 1,
|
| 211 |
+
"max_model_len": 8192,
|
| 212 |
+
},
|
| 213 |
+
generation_kwargs={
|
| 214 |
+
"temperature": 0.6,
|
| 215 |
+
"max_new_tokens": 8192,
|
| 216 |
+
},
|
| 217 |
+
)
|
| 218 |
+
prompt_column = "problem"
|
| 219 |
+
text_generation = TextGeneration(
|
| 220 |
+
llm=llm,
|
| 221 |
+
template=prompt_template,
|
| 222 |
+
num_generations=4,
|
| 223 |
+
input_mappings={"instruction": prompt_column} if prompt_column is not None else {}
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
distiset = pipeline.run(dataset=dataset)
|
| 229 |
+
distiset.push_to_hub(repo_id="username/numina-deepseek-r1-qwen-7b")
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
Take a look at the sample dataset at [HuggingFaceH4/numina-deepseek-r1-qwen-7b](https://huggingface.co/datasets/HuggingFaceH4/numina-deepseek-r1-qwen-7b).
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
### Generate data from DeepSeek-R1
|
| 236 |
+
|
| 237 |
+
To run the bigger DeepSeek-R1, we used 2 nodes of 8xH100 each one, using the slurm file present in this repo at `slurm/generate.slurm`. First, install the dependencies:
|
| 238 |
+
|
| 239 |
+
(for now we need to install the vllm dev wheel that [fixes the R1 cuda graph capture](https://github.com/vllm-project/vllm/commits/221d388cc5a836fa189305785ed7e887cea8b510/csrc/moe/moe_align_sum_kernels.cu))
|
| 240 |
+
```shell
|
| 241 |
+
pip install https://wheels.vllm.ai/221d388cc5a836fa189305785ed7e887cea8b510/vllm-1.0.0.dev-cp38-abi3-manylinux1_x86_64.whl --extra-index-url https://download.pytorch.org/whl/cu121
|
| 242 |
+
|
| 243 |
+
pip install "distilabel[vllm,ray,openai]>=1.5.2"
|
| 244 |
+
```
|
| 245 |
+
|
| 246 |
+
And then, place the `generate.slurm` file at the same level as `src/open_r1/generate.py` (it will try to run the file in the relative path), and run the following command:
|
| 247 |
+
|
| 248 |
+
```shell
|
| 249 |
+
sbatch generate.slurm \
|
| 250 |
+
--hf-dataset AI-MO/NuminaMath-TIR \
|
| 251 |
+
--temperature 0.6 \
|
| 252 |
+
--prompt-column problem \
|
| 253 |
+
--model deepseek-ai/DeepSeek-R1 \
|
| 254 |
+
--hf-output-dataset username/r1-dataset
|
| 255 |
+
```
|
assets/plan-of-attack.png
ADDED
|
configs/ddp.yaml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
compute_environment: LOCAL_MACHINE
|
| 2 |
+
debug: false
|
| 3 |
+
distributed_type: MULTI_GPU
|
| 4 |
+
downcast_bf16: 'no'
|
| 5 |
+
gpu_ids: all
|
| 6 |
+
machine_rank: 0
|
| 7 |
+
main_training_function: main
|
| 8 |
+
mixed_precision: bf16
|
| 9 |
+
num_machines: 1
|
| 10 |
+
num_processes: 8
|
| 11 |
+
rdzv_backend: static
|
| 12 |
+
same_network: true
|
| 13 |
+
tpu_env: []
|
| 14 |
+
tpu_use_cluster: false
|
| 15 |
+
tpu_use_sudo: false
|
| 16 |
+
use_cpu: false
|
configs/zero2.yaml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
compute_environment: LOCAL_MACHINE
|
| 2 |
+
debug: false
|
| 3 |
+
deepspeed_config:
|
| 4 |
+
deepspeed_multinode_launcher: standard
|
| 5 |
+
offload_optimizer_device: none
|
| 6 |
+
offload_param_device: none
|
| 7 |
+
zero3_init_flag: false
|
| 8 |
+
zero_stage: 2
|
| 9 |
+
distributed_type: DEEPSPEED
|
| 10 |
+
downcast_bf16: 'no'
|
| 11 |
+
machine_rank: 0
|
| 12 |
+
main_training_function: main
|
| 13 |
+
mixed_precision: bf16
|
| 14 |
+
num_machines: 1
|
| 15 |
+
num_processes: 8
|
| 16 |
+
rdzv_backend: static
|
| 17 |
+
same_network: true
|
| 18 |
+
tpu_env: []
|
| 19 |
+
tpu_use_cluster: false
|
| 20 |
+
tpu_use_sudo: false
|
| 21 |
+
use_cpu: false
|
configs/zero3.yaml
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
compute_environment: LOCAL_MACHINE
|
| 2 |
+
debug: false
|
| 3 |
+
deepspeed_config:
|
| 4 |
+
deepspeed_multinode_launcher: standard
|
| 5 |
+
offload_optimizer_device: none
|
| 6 |
+
offload_param_device: none
|
| 7 |
+
zero3_init_flag: true
|
| 8 |
+
zero3_save_16bit_model: true
|
| 9 |
+
zero_stage: 3
|
| 10 |
+
distributed_type: DEEPSPEED
|
| 11 |
+
downcast_bf16: 'no'
|
| 12 |
+
machine_rank: 0
|
| 13 |
+
main_training_function: main
|
| 14 |
+
mixed_precision: bf16
|
| 15 |
+
num_machines: 1
|
| 16 |
+
num_processes: 8
|
| 17 |
+
rdzv_backend: static
|
| 18 |
+
same_network: true
|
| 19 |
+
tpu_env: []
|
| 20 |
+
tpu_use_cluster: false
|
| 21 |
+
tpu_use_sudo: false
|
| 22 |
+
use_cpu: false
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
torch>=2.0
|
| 4 |
+
transformers
|
| 5 |
+
vllm==0.6.6.post1
|
| 6 |
+
distilabel[vllm]>=1.5.2
|
| 7 |
+
datasets
|
setup.cfg
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[isort]
|
| 2 |
+
default_section = FIRSTPARTY
|
| 3 |
+
ensure_newline_before_comments = True
|
| 4 |
+
force_grid_wrap = 0
|
| 5 |
+
include_trailing_comma = True
|
| 6 |
+
known_first_party = open_r1
|
| 7 |
+
known_third_party =
|
| 8 |
+
transformers
|
| 9 |
+
datasets
|
| 10 |
+
fugashi
|
| 11 |
+
git
|
| 12 |
+
h5py
|
| 13 |
+
matplotlib
|
| 14 |
+
nltk
|
| 15 |
+
numpy
|
| 16 |
+
packaging
|
| 17 |
+
pandas
|
| 18 |
+
psutil
|
| 19 |
+
pytest
|
| 20 |
+
rouge_score
|
| 21 |
+
sacrebleu
|
| 22 |
+
seqeval
|
| 23 |
+
sklearn
|
| 24 |
+
streamlit
|
| 25 |
+
torch
|
| 26 |
+
tqdm
|
| 27 |
+
|
| 28 |
+
line_length = 119
|
| 29 |
+
lines_after_imports = 2
|
| 30 |
+
multi_line_output = 3
|
| 31 |
+
use_parentheses = True
|
| 32 |
+
|
| 33 |
+
[flake8]
|
| 34 |
+
ignore = E203, E501, E741, W503, W605
|
| 35 |
+
max-line-length = 119
|
| 36 |
+
per-file-ignores =
|
| 37 |
+
# imported but unused
|
| 38 |
+
__init__.py: F401
|
| 39 |
+
|
| 40 |
+
[tool:pytest]
|
| 41 |
+
doctest_optionflags=NUMBER NORMALIZE_WHITESPACE ELLIPSIS
|
setup.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
#
|
| 15 |
+
# Adapted from huggingface/transformers: https://github.com/huggingface/transformers/blob/21a2d900eceeded7be9edc445b56877b95eda4ca/setup.py
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import re
|
| 19 |
+
import shutil
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
|
| 22 |
+
from setuptools import find_packages, setup
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Remove stale open_r1.egg-info directory to avoid https://github.com/pypa/pip/issues/5466
|
| 26 |
+
stale_egg_info = Path(__file__).parent / "open_r1.egg-info"
|
| 27 |
+
if stale_egg_info.exists():
|
| 28 |
+
print(
|
| 29 |
+
(
|
| 30 |
+
"Warning: {} exists.\n\n"
|
| 31 |
+
"If you recently updated open_r1, this is expected,\n"
|
| 32 |
+
"but it may prevent open_r1 from installing in editable mode.\n\n"
|
| 33 |
+
"This directory is automatically generated by Python's packaging tools.\n"
|
| 34 |
+
"I will remove it now.\n\n"
|
| 35 |
+
"See https://github.com/pypa/pip/issues/5466 for details.\n"
|
| 36 |
+
).format(stale_egg_info)
|
| 37 |
+
)
|
| 38 |
+
shutil.rmtree(stale_egg_info)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# IMPORTANT: all dependencies should be listed here with their version requirements, if any.
|
| 42 |
+
# * If a dependency is fast-moving (e.g. transformers), pin to the exact version
|
| 43 |
+
_deps = [
|
| 44 |
+
"accelerate>=1.2.1",
|
| 45 |
+
"bitsandbytes>=0.43.0",
|
| 46 |
+
"black>=24.4.2",
|
| 47 |
+
"datasets>=3.2.0",
|
| 48 |
+
"deepspeed==0.15.4",
|
| 49 |
+
"distilabel[vllm,ray,openai]>=1.5.2",
|
| 50 |
+
"einops>=0.8.0",
|
| 51 |
+
"flake8>=6.0.0",
|
| 52 |
+
"hf_transfer>=0.1.4",
|
| 53 |
+
"huggingface-hub[cli]>=0.19.2,<1.0",
|
| 54 |
+
"isort>=5.12.0",
|
| 55 |
+
"liger_kernel==0.5.2",
|
| 56 |
+
"lighteval @ git+https://github.com/huggingface/lighteval.git@4f381b352c0e467b5870a97d41cb66b487a2c503#egg=lighteval[math]",
|
| 57 |
+
"math-verify>=0.3.2", # Used for math verification in grpo
|
| 58 |
+
"packaging>=23.0",
|
| 59 |
+
"parameterized>=0.9.0",
|
| 60 |
+
"pytest",
|
| 61 |
+
"safetensors>=0.3.3",
|
| 62 |
+
"sentencepiece>=0.1.99",
|
| 63 |
+
"torch>=2.5.1",
|
| 64 |
+
"transformers @ git+https://github.com/huggingface/transformers.git@main",
|
| 65 |
+
"trl @ git+https://github.com/huggingface/trl.git@main",
|
| 66 |
+
"vllm==0.6.6.post1",
|
| 67 |
+
"wandb>=0.19.1",
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
# this is a lookup table with items like:
|
| 71 |
+
#
|
| 72 |
+
# tokenizers: "tokenizers==0.9.4"
|
| 73 |
+
# packaging: "packaging"
|
| 74 |
+
#
|
| 75 |
+
# some of the values are versioned whereas others aren't.
|
| 76 |
+
deps = {b: a for a, b in (re.findall(r"^(([^!=<>~ \[\]]+)(?:\[[^\]]+\])?(?:[!=<>~ ].*)?$)", x)[0] for x in _deps)}
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def deps_list(*pkgs):
|
| 80 |
+
return [deps[pkg] for pkg in pkgs]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
extras = {}
|
| 84 |
+
extras["tests"] = deps_list("pytest", "parameterized")
|
| 85 |
+
extras["torch"] = deps_list("torch")
|
| 86 |
+
extras["quality"] = deps_list("black", "isort", "flake8")
|
| 87 |
+
extras["eval"] = deps_list("lighteval", "math-verify")
|
| 88 |
+
extras["dev"] = extras["quality"] + extras["tests"] + extras["eval"]
|
| 89 |
+
|
| 90 |
+
# core dependencies shared across the whole project - keep this to a bare minimum :)
|
| 91 |
+
install_requires = [
|
| 92 |
+
deps["accelerate"],
|
| 93 |
+
deps["bitsandbytes"],
|
| 94 |
+
deps["einops"],
|
| 95 |
+
deps["datasets"],
|
| 96 |
+
deps["deepspeed"],
|
| 97 |
+
deps["hf_transfer"],
|
| 98 |
+
deps["huggingface-hub"],
|
| 99 |
+
deps["liger_kernel"],
|
| 100 |
+
deps["packaging"], # utilities from PyPA to e.g., compare versions
|
| 101 |
+
deps["safetensors"],
|
| 102 |
+
deps["sentencepiece"],
|
| 103 |
+
deps["transformers"],
|
| 104 |
+
deps["trl"],
|
| 105 |
+
]
|
| 106 |
+
|
| 107 |
+
setup(
|
| 108 |
+
name="open-r1",
|
| 109 |
+
version="0.1.0.dev0", # expected format is one of x.y.z.dev0, or x.y.z.rc1 or x.y.z (no to dashes, yes to dots)
|
| 110 |
+
author="The Hugging Face team (past and future)",
|
| 111 |
+
author_email="lewis@huggingface.co",
|
| 112 |
+
description="Open R1",
|
| 113 |
+
long_description=open("README.md", "r", encoding="utf-8").read(),
|
| 114 |
+
long_description_content_type="text/markdown",
|
| 115 |
+
keywords="llm inference-time compute reasoning",
|
| 116 |
+
license="Apache",
|
| 117 |
+
url="https://github.com/huggingface/open-r1",
|
| 118 |
+
package_dir={"": "src"},
|
| 119 |
+
packages=find_packages("src"),
|
| 120 |
+
zip_safe=False,
|
| 121 |
+
extras_require=extras,
|
| 122 |
+
python_requires=">=3.10.9",
|
| 123 |
+
install_requires=install_requires,
|
| 124 |
+
classifiers=[
|
| 125 |
+
"Development Status :: 3 - Alpha",
|
| 126 |
+
"Intended Audience :: Developers",
|
| 127 |
+
"Intended Audience :: Education",
|
| 128 |
+
"Intended Audience :: Science/Research",
|
| 129 |
+
"License :: OSI Approved :: Apache Software License",
|
| 130 |
+
"Operating System :: OS Independent",
|
| 131 |
+
"Programming Language :: Python :: 3",
|
| 132 |
+
"Programming Language :: Python :: 3.10",
|
| 133 |
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
| 134 |
+
],
|
| 135 |
+
)
|
slurm/evaluate.slurm
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
#SBATCH --job-name=open-r1-evaluate
|
| 3 |
+
#SBATCH --nodes=1
|
| 4 |
+
#SBATCH --ntasks-per-node=1
|
| 5 |
+
#SBATCH --exclusive
|
| 6 |
+
#SBATCH --gres=gpu:8
|
| 7 |
+
#SBATCH --partition=hopper-prod
|
| 8 |
+
#SBATCH --time=01:59:00
|
| 9 |
+
#SBATCH --output=./logs/evaluate/%x-%j.out
|
| 10 |
+
#SBATCH --err=./logs/evaluate/%x-%j.err
|
| 11 |
+
|
| 12 |
+
# Usage: sbatch slurm/evaluate.slurm deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B aime24
|
| 13 |
+
|
| 14 |
+
set -x -e
|
| 15 |
+
|
| 16 |
+
source ~/.bashrc
|
| 17 |
+
conda activate openr1
|
| 18 |
+
module load cuda/12.1
|
| 19 |
+
echo "START TIME: $(date)"
|
| 20 |
+
echo "PYTHON ENV: $(which python)"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
NUM_GPUS=8
|
| 24 |
+
MODEL=$1
|
| 25 |
+
TASK=$2
|
| 26 |
+
MODEL_ARGS="pretrained=$MODEL,dtype=float16,data_parallel_size=$NUM_GPUS,max_model_length=32768,gpu_memory_utilisation=0.8"
|
| 27 |
+
OUTPUT_DIR=data/evals/$MODEL
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# force crashing on nccl issues like hanging broadcast
|
| 31 |
+
export NCCL_ASYNC_ERROR_HANDLING=1
|
| 32 |
+
# export NCCL_DEBUG=INFO
|
| 33 |
+
# export NCCL_DEBUG_SUBSYS=COLL
|
| 34 |
+
# export NCCL_SOCKET_NTHREADS=1
|
| 35 |
+
# export NCCL_NSOCKS_PERTHREAD=1
|
| 36 |
+
# export CUDA_LAUNCH_BLOCKING=1
|
| 37 |
+
|
| 38 |
+
# Specific configuration optimized for the Hugging Face Compute Cluster
|
| 39 |
+
# Be ye warned this may not work on other clusters!
|
| 40 |
+
module load cuda/12.1
|
| 41 |
+
|
| 42 |
+
lighteval vllm $MODEL_ARGS "custom|$TASK|0|0" \
|
| 43 |
+
--custom-tasks src/open_r1/evaluate.py \
|
| 44 |
+
--use-chat-template \
|
| 45 |
+
--system-prompt="Please reason step by step, and put your final answer within \boxed{}." \
|
| 46 |
+
--save-details
|
| 47 |
+
--output-dir $OUTPUT_DIR
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
echo "END TIME: $(date)"
|
slurm/generate.slurm
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
#SBATCH --job-name=deepseek-r1-generation
|
| 3 |
+
#SBATCH --partition=hopper-prod
|
| 4 |
+
#SBATCH --qos=normal
|
| 5 |
+
#SBATCH --nodes=4
|
| 6 |
+
#SBATCH --exclusive
|
| 7 |
+
#SBATCH --gpus-per-node=8
|
| 8 |
+
#SBATCH --output=./logs/%x-%j.out
|
| 9 |
+
#SBATCH --err=./logs/%x-%j.err
|
| 10 |
+
#SBATCH --time=08:00:00
|
| 11 |
+
|
| 12 |
+
# Parse command line arguments
|
| 13 |
+
while [[ $# -gt 0 ]]; do
|
| 14 |
+
case $1 in
|
| 15 |
+
--hf-dataset)
|
| 16 |
+
HF_DATASET="$2"
|
| 17 |
+
shift 2
|
| 18 |
+
;;
|
| 19 |
+
--hf-dataset-config)
|
| 20 |
+
HF_DATASET_CONFIG="$2"
|
| 21 |
+
shift 2
|
| 22 |
+
;;
|
| 23 |
+
--hf-dataset-split)
|
| 24 |
+
HF_DATASET_SPLIT="$2"
|
| 25 |
+
shift 2
|
| 26 |
+
;;
|
| 27 |
+
--prompt-column)
|
| 28 |
+
PROMPT_COLUMN="$2"
|
| 29 |
+
shift 2
|
| 30 |
+
;;
|
| 31 |
+
--model)
|
| 32 |
+
MODEL="$2"
|
| 33 |
+
shift 2
|
| 34 |
+
;;
|
| 35 |
+
--temperature)
|
| 36 |
+
TEMPERATURE="$2"
|
| 37 |
+
shift 2
|
| 38 |
+
;;
|
| 39 |
+
--top-p)
|
| 40 |
+
TOP_P="$2"
|
| 41 |
+
shift 2
|
| 42 |
+
;;
|
| 43 |
+
--max-new-tokens)
|
| 44 |
+
MAX_NEW_TOKENS="$2"
|
| 45 |
+
shift 2
|
| 46 |
+
;;
|
| 47 |
+
--num-generations)
|
| 48 |
+
NUM_GENERATIONS="$2"
|
| 49 |
+
shift 2
|
| 50 |
+
;;
|
| 51 |
+
--hf-output-dataset)
|
| 52 |
+
HF_OUTPUT_DATASET="$2"
|
| 53 |
+
shift 2
|
| 54 |
+
;;
|
| 55 |
+
--private)
|
| 56 |
+
PRIVATE="true"
|
| 57 |
+
shift
|
| 58 |
+
;;
|
| 59 |
+
*)
|
| 60 |
+
echo "Unknown parameter: $1"
|
| 61 |
+
exit 1
|
| 62 |
+
;;
|
| 63 |
+
esac
|
| 64 |
+
done
|
| 65 |
+
|
| 66 |
+
if [ -z "$MODEL" ] || [ -z "$HF_DATASET" ]; then
|
| 67 |
+
echo "Error: --model and --hf-dataset are required parameters"
|
| 68 |
+
exit 1
|
| 69 |
+
fi
|
| 70 |
+
|
| 71 |
+
# Set default values for optional parameters
|
| 72 |
+
HF_DATASET_SPLIT=${HF_DATASET_SPLIT:-"train"}
|
| 73 |
+
PROMPT_COLUMN=${PROMPT_COLUMN:-"prompt"}
|
| 74 |
+
MAX_NEW_TOKENS=${MAX_NEW_TOKENS:-8192}
|
| 75 |
+
NUM_GENERATIONS=${NUM_GENERATIONS:-1}
|
| 76 |
+
PRIVATE=${PRIVATE:-"false"}
|
| 77 |
+
|
| 78 |
+
# Print all input arguments
|
| 79 |
+
echo "Input arguments:"
|
| 80 |
+
echo "MODEL: $MODEL"
|
| 81 |
+
echo "HF_DATASET: $HF_DATASET"
|
| 82 |
+
echo "HF_DATASET_CONFIG: $HF_DATASET_CONFIG"
|
| 83 |
+
echo "HF_DATASET_SPLIT: $HF_DATASET_SPLIT"
|
| 84 |
+
echo "PROMPT_COLUMN: $PROMPT_COLUMN"
|
| 85 |
+
echo "TEMPERATURE: $TEMPERATURE"
|
| 86 |
+
echo "TOP_P: $TOP_P"
|
| 87 |
+
echo "MAX_NEW_TOKENS: $MAX_NEW_TOKENS"
|
| 88 |
+
echo "NUM_GENERATIONS: $NUM_GENERATIONS"
|
| 89 |
+
echo "HF_OUTPUT_DATASET: $HF_OUTPUT_DATASET"
|
| 90 |
+
echo "PRIVATE: $PRIVATE"
|
| 91 |
+
echo "-------------------"
|
| 92 |
+
|
| 93 |
+
set -ex
|
| 94 |
+
|
| 95 |
+
module load cuda/12.1
|
| 96 |
+
|
| 97 |
+
export LD_LIBRARY_PATH=.venv/lib/python3.11/site-packages/nvidia/nvjitlink/lib
|
| 98 |
+
|
| 99 |
+
echo "SLURM_JOB_ID: $SLURM_JOB_ID"
|
| 100 |
+
echo "SLURM_JOB_NODELIST: $SLURM_JOB_NODELIST"
|
| 101 |
+
|
| 102 |
+
source .venv/bin/activate
|
| 103 |
+
|
| 104 |
+
# Getting the node names
|
| 105 |
+
nodes=$(scontrol show hostnames "$SLURM_JOB_NODELIST")
|
| 106 |
+
nodes_array=($nodes)
|
| 107 |
+
|
| 108 |
+
# Get the IP address of the head node
|
| 109 |
+
head_node=${nodes_array[0]}
|
| 110 |
+
head_node_ip=$(srun --nodes=1 --ntasks=1 -w "$head_node" hostname --ip-address)
|
| 111 |
+
|
| 112 |
+
# Start Ray head node
|
| 113 |
+
port=6379
|
| 114 |
+
ip_head=$head_node_ip:$port
|
| 115 |
+
export ip_head
|
| 116 |
+
echo "IP Head: $ip_head"
|
| 117 |
+
|
| 118 |
+
echo "Starting HEAD at $head_node"
|
| 119 |
+
srun --nodes=1 --ntasks=1 -w "$head_node" \
|
| 120 |
+
ray start --head --node-ip-address="$head_node_ip" --port=$port \
|
| 121 |
+
--dashboard-host=0.0.0.0 \
|
| 122 |
+
--dashboard-port=8265 \
|
| 123 |
+
--block &
|
| 124 |
+
|
| 125 |
+
# Give some time to head node to start...
|
| 126 |
+
sleep 10
|
| 127 |
+
|
| 128 |
+
# Start Ray worker nodes
|
| 129 |
+
worker_num=$((SLURM_JOB_NUM_NODES - 1))
|
| 130 |
+
|
| 131 |
+
# Start from 1 (0 is head node)
|
| 132 |
+
for ((i = 1; i <= worker_num; i++)); do
|
| 133 |
+
node_i=${nodes_array[$i]}
|
| 134 |
+
echo "Starting WORKER $i at $node_i"
|
| 135 |
+
srun --nodes=1 --ntasks=1 -w "$node_i" \
|
| 136 |
+
ray start --address "$ip_head" \
|
| 137 |
+
--block &
|
| 138 |
+
sleep 5
|
| 139 |
+
done
|
| 140 |
+
|
| 141 |
+
# Give some time to the Ray cluster to gather info
|
| 142 |
+
echo "Waiting a bit for Ray cluster to gather node info..."
|
| 143 |
+
sleep 60
|
| 144 |
+
|
| 145 |
+
# Run vllm
|
| 146 |
+
RAY_ADDRESS="http://$head_node_ip:8265" ray job submit \
|
| 147 |
+
--working-dir src/open_r1 \
|
| 148 |
+
--no-wait \
|
| 149 |
+
-- vllm serve $MODEL \
|
| 150 |
+
--tensor-parallel-size 8 \
|
| 151 |
+
--pipeline-parallel-size 4 \
|
| 152 |
+
--gpu-memory-utilization=0.85 \
|
| 153 |
+
--max-model-len 16384 \
|
| 154 |
+
--enable-chunked-prefill \
|
| 155 |
+
--trust-remote-code \
|
| 156 |
+
--distributed-executor-backend ray
|
| 157 |
+
|
| 158 |
+
# wait for vllm to load the model
|
| 159 |
+
echo "Waiting for vLLM (http://$head_node_ip:8000) server to be up..."
|
| 160 |
+
|
| 161 |
+
# wait for vllm to load and serve the model
|
| 162 |
+
while true; do
|
| 163 |
+
if curl -s -o /dev/null -w "%{http_code}" http://$head_node_ip:8000 >/dev/null 2>&1; then
|
| 164 |
+
echo "Received response from http://$head_node_ip:8000"
|
| 165 |
+
break
|
| 166 |
+
else
|
| 167 |
+
echo "Still waiting... (Press Ctrl+C to cancel)"
|
| 168 |
+
sleep 60
|
| 169 |
+
fi
|
| 170 |
+
done
|
| 171 |
+
|
| 172 |
+
echo "Checking available models..."
|
| 173 |
+
curl http://$head_node_ip:8000/v1/models
|
| 174 |
+
|
| 175 |
+
echo "Executing sanity check..."
|
| 176 |
+
curl http://$head_node_ip:8000/v1/completions \
|
| 177 |
+
-H "Content-Type: application/json" \
|
| 178 |
+
-d "{
|
| 179 |
+
\"model\": \"$MODEL\",
|
| 180 |
+
\"prompt\": \"<|begin▁of▁sentence|><|User|>hi, how are you?<|Assistant|>\",
|
| 181 |
+
\"max_tokens\": 2048,
|
| 182 |
+
\"temperature\": 0.6
|
| 183 |
+
}"
|
| 184 |
+
|
| 185 |
+
# Finally submit the job to the cluster
|
| 186 |
+
echo "Submitting job to ray cluster..."
|
| 187 |
+
RAY_ADDRESS="http://$head_node_ip:8265" ray job submit \
|
| 188 |
+
--working-dir src/open_r1 \
|
| 189 |
+
-- python -u generate.py \
|
| 190 |
+
--model "$MODEL" \
|
| 191 |
+
--hf-dataset "$HF_DATASET" \
|
| 192 |
+
${HF_DATASET_CONFIG:+--hf-dataset-config "$HF_DATASET_CONFIG"} \
|
| 193 |
+
--hf-dataset-split "$HF_DATASET_SPLIT" \
|
| 194 |
+
--prompt-column "$PROMPT_COLUMN" \
|
| 195 |
+
${TEMPERATURE:+--temperature "$TEMPERATURE"} \
|
| 196 |
+
${TOP_P:+--top-p "$TOP_P"} \
|
| 197 |
+
--max-new-tokens "$MAX_NEW_TOKENS" \
|
| 198 |
+
--num-generations "$NUM_GENERATIONS" \
|
| 199 |
+
${HF_OUTPUT_DATASET:+--hf-output-dataset "$HF_OUTPUT_DATASET"} \
|
| 200 |
+
${PRIVATE:+--private} \
|
| 201 |
+
--vllm-server-url "http://$head_node_ip:8000/v1"
|
slurm/sft.slurm
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
#SBATCH --job-name=open-r1-sft
|
| 3 |
+
#SBATCH --nodes=1
|
| 4 |
+
#SBATCH --ntasks-per-node=1
|
| 5 |
+
#SBATCH --exclusive
|
| 6 |
+
#SBATCH --gres=gpu:8
|
| 7 |
+
#SBATCH --partition=hopper-prod
|
| 8 |
+
#SBATCH --output=./logs/%x-%j.out
|
| 9 |
+
#SBATCH --err=./logs/%x-%j.err
|
| 10 |
+
|
| 11 |
+
set -x -e
|
| 12 |
+
|
| 13 |
+
source ~/.bashrc
|
| 14 |
+
conda activate openr1
|
| 15 |
+
module load cuda/12.1
|
| 16 |
+
echo "START TIME: $(date)"
|
| 17 |
+
echo "PYTHON ENV: $(which python)"
|
| 18 |
+
|
| 19 |
+
MODEL_PATH=$1
|
| 20 |
+
DATASET_PATH=$2
|
| 21 |
+
ACCELERATOR=$3
|
| 22 |
+
|
| 23 |
+
# Training setup
|
| 24 |
+
NUM_NODES=$SLURM_NNODES
|
| 25 |
+
GPUS_PER_NODE=8
|
| 26 |
+
WORLD_SIZE=$(($NUM_NODES*$GPUS_PER_NODE))
|
| 27 |
+
|
| 28 |
+
# so processes know who to talk to
|
| 29 |
+
MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1)
|
| 30 |
+
MASTER_PORT=6000
|
| 31 |
+
|
| 32 |
+
export CMD=" \
|
| 33 |
+
src/open_r1/sft.py \
|
| 34 |
+
--model_name_or_path $MODEL_PATH \
|
| 35 |
+
--dataset_name $DATASET_PATH \
|
| 36 |
+
--use_liger_kernel true \
|
| 37 |
+
--learning_rate 2.0e-5 \
|
| 38 |
+
--num_train_epochs 1 \
|
| 39 |
+
--packing \
|
| 40 |
+
--max_seq_length 4096 \
|
| 41 |
+
--per_device_train_batch_size 4 \
|
| 42 |
+
--per_device_eval_batch_size 4 \
|
| 43 |
+
--gradient_accumulation_steps 4 \
|
| 44 |
+
--gradient_checkpointing \
|
| 45 |
+
--bf16 \
|
| 46 |
+
--logging_steps 5 \
|
| 47 |
+
--eval_strategy steps \
|
| 48 |
+
--eval_steps 100 \
|
| 49 |
+
--output_dir data/Qwen2.5-1.5B-Open-R1-Distill
|
| 50 |
+
"
|
| 51 |
+
|
| 52 |
+
export LAUNCHER="HF_HUB_ENABLE_HF_TRANSFER=1 ACCELERATE_LOG_LEVEL=info TRANSFORMERS_VERBOSITY=info accelerate launch \
|
| 53 |
+
--config_file configs/$ACCELERATOR.yaml \
|
| 54 |
+
--gradient_accumulation_steps 4 \
|
| 55 |
+
--num_machines $NUM_NODES \
|
| 56 |
+
--num_processes $WORLD_SIZE \
|
| 57 |
+
--main_process_ip $MASTER_ADDR \
|
| 58 |
+
--main_process_port $MASTER_PORT \
|
| 59 |
+
--machine_rank \$SLURM_PROCID \
|
| 60 |
+
--rdzv_conf "rdzv_backend=c10d,rdzv_endpoint=$MASTER_ADDR:$MASTER_PORT" \
|
| 61 |
+
--max_restarts 1 \
|
| 62 |
+
--role \$(hostname -s): \
|
| 63 |
+
--tee 3 \
|
| 64 |
+
"
|
| 65 |
+
|
| 66 |
+
# force crashing on nccl issues like hanging broadcast
|
| 67 |
+
export NCCL_ASYNC_ERROR_HANDLING=1
|
| 68 |
+
# export NCCL_DEBUG=INFO
|
| 69 |
+
# export NCCL_DEBUG_SUBSYS=COLL
|
| 70 |
+
# export NCCL_SOCKET_NTHREADS=1
|
| 71 |
+
# export NCCL_NSOCKS_PERTHREAD=1
|
| 72 |
+
# export CUDA_LAUNCH_BLOCKING=1
|
| 73 |
+
|
| 74 |
+
# Specific configuration optimized for the Hugging Face Compute Cluster
|
| 75 |
+
# Be ye warned this may not work on other clusters!
|
| 76 |
+
module load cuda/12.1
|
| 77 |
+
|
| 78 |
+
# srun error handling:
|
| 79 |
+
# --wait=60: wait 60 sec after the first task terminates before terminating all remaining tasks
|
| 80 |
+
# --kill-on-bad-exit=1: terminate a step if any task exits with a non-zero exit code
|
| 81 |
+
SRUN_ARGS=" \
|
| 82 |
+
--wait=60 \
|
| 83 |
+
--kill-on-bad-exit=1 \
|
| 84 |
+
"
|
| 85 |
+
|
| 86 |
+
clear; srun $SRUN_ARGS --jobid $SLURM_JOB_ID bash -c "$LAUNCHER --role \$SLURMD_NODENAME: $CMD" 2>&1
|
| 87 |
+
|
| 88 |
+
echo "END TIME: $(date)"
|
src/open_r1/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
__version__ = "0.3.0.dev0"
|
| 16 |
+
|
| 17 |
+
from .configs import DataArguments, DPOConfig, H4ArgumentParser, ModelArguments, SFTConfig
|
| 18 |
+
from .data import apply_chat_template, get_datasets
|
| 19 |
+
from .decontaminate import decontaminate_humaneval
|
| 20 |
+
from .model_utils import get_checkpoint, get_kbit_device_map, get_quantization_config, get_tokenizer
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
__all__ = [
|
| 24 |
+
"DataArguments",
|
| 25 |
+
"DPOConfig",
|
| 26 |
+
"H4ArgumentParser",
|
| 27 |
+
"ModelArguments",
|
| 28 |
+
"SFTConfig",
|
| 29 |
+
"apply_chat_template",
|
| 30 |
+
"get_datasets",
|
| 31 |
+
"decontaminate_humaneval",
|
| 32 |
+
"get_checkpoint",
|
| 33 |
+
"get_kbit_device_map",
|
| 34 |
+
"get_quantization_config",
|
| 35 |
+
"get_tokenizer",
|
| 36 |
+
]
|
src/open_r1/evaluate.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
"""Custom evaluation tasks for LightEval."""
|
| 16 |
+
|
| 17 |
+
from lighteval.metrics.dynamic_metrics import (
|
| 18 |
+
ExprExtractionConfig,
|
| 19 |
+
LatexExtractionConfig,
|
| 20 |
+
multilingual_extractive_match_metric,
|
| 21 |
+
)
|
| 22 |
+
from lighteval.tasks.lighteval_task import LightevalTaskConfig
|
| 23 |
+
from lighteval.tasks.requests import Doc
|
| 24 |
+
from lighteval.utils.language import Language
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
latex_gold_metric = multilingual_extractive_match_metric(
|
| 28 |
+
language=Language.ENGLISH,
|
| 29 |
+
fallback_mode="first_match",
|
| 30 |
+
precision=5,
|
| 31 |
+
gold_extraction_target=(LatexExtractionConfig(),),
|
| 32 |
+
pred_extraction_target=(ExprExtractionConfig(), LatexExtractionConfig()),
|
| 33 |
+
aggregation_function=max,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
expr_gold_metric = multilingual_extractive_match_metric(
|
| 37 |
+
language=Language.ENGLISH,
|
| 38 |
+
fallback_mode="first_match",
|
| 39 |
+
precision=5,
|
| 40 |
+
gold_extraction_target=(ExprExtractionConfig(),),
|
| 41 |
+
pred_extraction_target=(ExprExtractionConfig(), LatexExtractionConfig()),
|
| 42 |
+
aggregation_function=max,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def prompt_fn(line, task_name: str = None):
|
| 47 |
+
"""Assumes the model is either prompted to emit \\boxed{answer} or does so automatically"""
|
| 48 |
+
return Doc(
|
| 49 |
+
task_name=task_name,
|
| 50 |
+
query=line["problem"],
|
| 51 |
+
choices=[line["solution"]],
|
| 52 |
+
gold_index=0,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def aime_prompt_fn(line, task_name: str = None):
|
| 57 |
+
return Doc(
|
| 58 |
+
task_name=task_name,
|
| 59 |
+
query=line["problem"],
|
| 60 |
+
choices=[line["answer"]],
|
| 61 |
+
gold_index=0,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Define tasks
|
| 66 |
+
aime24 = LightevalTaskConfig(
|
| 67 |
+
name="aime24",
|
| 68 |
+
suite=["custom"],
|
| 69 |
+
prompt_function=aime_prompt_fn,
|
| 70 |
+
hf_repo="HuggingFaceH4/aime_2024",
|
| 71 |
+
hf_subset="default",
|
| 72 |
+
hf_avail_splits=["train"],
|
| 73 |
+
evaluation_splits=["train"],
|
| 74 |
+
few_shots_split=None,
|
| 75 |
+
few_shots_select=None,
|
| 76 |
+
generation_size=32768,
|
| 77 |
+
metric=[expr_gold_metric],
|
| 78 |
+
version=1,
|
| 79 |
+
)
|
| 80 |
+
math_500 = LightevalTaskConfig(
|
| 81 |
+
name="math_500",
|
| 82 |
+
suite=["custom"],
|
| 83 |
+
prompt_function=prompt_fn,
|
| 84 |
+
hf_repo="HuggingFaceH4/MATH-500",
|
| 85 |
+
hf_subset="default",
|
| 86 |
+
hf_avail_splits=["test"],
|
| 87 |
+
evaluation_splits=["test"],
|
| 88 |
+
few_shots_split=None,
|
| 89 |
+
few_shots_select=None,
|
| 90 |
+
generation_size=32768,
|
| 91 |
+
metric=[latex_gold_metric],
|
| 92 |
+
version=1,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Add tasks to the table
|
| 96 |
+
TASKS_TABLE = []
|
| 97 |
+
TASKS_TABLE.append(aime24)
|
| 98 |
+
TASKS_TABLE.append(math_500)
|
| 99 |
+
|
| 100 |
+
# MODULE LOGIC
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
print([t["name"] for t in TASKS_TABLE])
|
| 103 |
+
print(len(TASKS_TABLE))
|
src/open_r1/generate.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
from typing import Optional
|
| 16 |
+
|
| 17 |
+
from distilabel.llms import OpenAILLM
|
| 18 |
+
from distilabel.pipeline import Pipeline
|
| 19 |
+
from distilabel.steps.tasks import TextGeneration
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def build_distilabel_pipeline(
|
| 23 |
+
model: str,
|
| 24 |
+
base_url: str = "http://localhost:8000/v1",
|
| 25 |
+
prompt_column: Optional[str] = None,
|
| 26 |
+
temperature: Optional[float] = None,
|
| 27 |
+
top_p: Optional[float] = None,
|
| 28 |
+
max_new_tokens: int = 8192,
|
| 29 |
+
num_generations: int = 1,
|
| 30 |
+
) -> Pipeline:
|
| 31 |
+
generation_kwargs = {"max_new_tokens": max_new_tokens}
|
| 32 |
+
|
| 33 |
+
if temperature is not None:
|
| 34 |
+
generation_kwargs["temperature"] = temperature
|
| 35 |
+
|
| 36 |
+
if top_p is not None:
|
| 37 |
+
generation_kwargs["top_p"] = top_p
|
| 38 |
+
|
| 39 |
+
with Pipeline().ray() as pipeline:
|
| 40 |
+
TextGeneration(
|
| 41 |
+
llm=OpenAILLM(
|
| 42 |
+
base_url=base_url,
|
| 43 |
+
api_key="something",
|
| 44 |
+
model=model,
|
| 45 |
+
# thinking can take some time...
|
| 46 |
+
timeout=10 * 60,
|
| 47 |
+
generation_kwargs=generation_kwargs,
|
| 48 |
+
),
|
| 49 |
+
input_mappings={"instruction": prompt_column} if prompt_column is not None else {},
|
| 50 |
+
input_batch_size=64, # on 4 nodes bs ~60+ leads to preemption due to KV cache exhaustion
|
| 51 |
+
num_generations=num_generations,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return pipeline
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
import argparse
|
| 59 |
+
|
| 60 |
+
from datasets import load_dataset
|
| 61 |
+
|
| 62 |
+
parser = argparse.ArgumentParser(description="Run distilabel pipeline for generating responses with DeepSeek R1")
|
| 63 |
+
parser.add_argument(
|
| 64 |
+
"--hf-dataset",
|
| 65 |
+
type=str,
|
| 66 |
+
required=True,
|
| 67 |
+
help="HuggingFace dataset to load",
|
| 68 |
+
)
|
| 69 |
+
parser.add_argument(
|
| 70 |
+
"--hf-dataset-config",
|
| 71 |
+
type=str,
|
| 72 |
+
required=False,
|
| 73 |
+
help="Dataset config to use",
|
| 74 |
+
)
|
| 75 |
+
parser.add_argument(
|
| 76 |
+
"--hf-dataset-split",
|
| 77 |
+
type=str,
|
| 78 |
+
default="train",
|
| 79 |
+
help="Dataset split to use",
|
| 80 |
+
)
|
| 81 |
+
parser.add_argument("--prompt-column", type=str, default="prompt")
|
| 82 |
+
parser.add_argument(
|
| 83 |
+
"--model",
|
| 84 |
+
type=str,
|
| 85 |
+
required=True,
|
| 86 |
+
help="Model name to use for generation",
|
| 87 |
+
)
|
| 88 |
+
parser.add_argument(
|
| 89 |
+
"--vllm-server-url",
|
| 90 |
+
type=str,
|
| 91 |
+
default="http://localhost:8000/v1",
|
| 92 |
+
help="URL of the vLLM server",
|
| 93 |
+
)
|
| 94 |
+
parser.add_argument(
|
| 95 |
+
"--temperature",
|
| 96 |
+
type=float,
|
| 97 |
+
help="Temperature for generation",
|
| 98 |
+
)
|
| 99 |
+
parser.add_argument(
|
| 100 |
+
"--top-p",
|
| 101 |
+
type=float,
|
| 102 |
+
help="Top-p value for generation",
|
| 103 |
+
)
|
| 104 |
+
parser.add_argument(
|
| 105 |
+
"--max-new-tokens",
|
| 106 |
+
type=int,
|
| 107 |
+
default=8192,
|
| 108 |
+
help="Maximum number of new tokens to generate",
|
| 109 |
+
)
|
| 110 |
+
parser.add_argument(
|
| 111 |
+
"--num-generations",
|
| 112 |
+
type=int,
|
| 113 |
+
default=1,
|
| 114 |
+
help="Number of generations per problem",
|
| 115 |
+
)
|
| 116 |
+
parser.add_argument(
|
| 117 |
+
"--hf-output-dataset",
|
| 118 |
+
type=str,
|
| 119 |
+
required=False,
|
| 120 |
+
help="HuggingFace repo to push results to",
|
| 121 |
+
)
|
| 122 |
+
parser.add_argument(
|
| 123 |
+
"--private",
|
| 124 |
+
action="store_true",
|
| 125 |
+
help="Whether to make the output dataset private when pushing to HF Hub",
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
args = parser.parse_args()
|
| 129 |
+
|
| 130 |
+
print("\nRunning with arguments:")
|
| 131 |
+
for arg, value in vars(args).items():
|
| 132 |
+
print(f" {arg}: {value}")
|
| 133 |
+
print()
|
| 134 |
+
|
| 135 |
+
print(f"Loading '{args.hf_dataset}' (config: {args.hf_dataset_config}, split: {args.hf_dataset_split}) dataset...")
|
| 136 |
+
dataset = load_dataset(args.hf_dataset, split=args.hf_dataset_split)
|
| 137 |
+
print("Dataset loaded!")
|
| 138 |
+
|
| 139 |
+
pipeline = build_distilabel_pipeline(
|
| 140 |
+
model=args.model,
|
| 141 |
+
base_url=args.vllm_server_url,
|
| 142 |
+
prompt_column=args.prompt_column,
|
| 143 |
+
temperature=args.temperature,
|
| 144 |
+
top_p=args.top_p,
|
| 145 |
+
max_new_tokens=args.max_new_tokens,
|
| 146 |
+
num_generations=args.num_generations,
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
print("Running generation pipeline...")
|
| 150 |
+
distiset = pipeline.run(dataset=dataset, use_cache=False)
|
| 151 |
+
print("Generation pipeline finished!")
|
| 152 |
+
|
| 153 |
+
if args.hf_output_dataset:
|
| 154 |
+
print(f"Pushing resulting dataset to '{args.hf_output_dataset}'...")
|
| 155 |
+
distiset.push_to_hub(args.hf_output_dataset, private=args.private)
|
| 156 |
+
print("Dataset pushed!")
|
src/open_r1/grpo.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import re
|
| 16 |
+
from dataclasses import dataclass, field
|
| 17 |
+
|
| 18 |
+
from datasets import load_dataset
|
| 19 |
+
|
| 20 |
+
from latex2sympy2_extended import NormalizationConfig
|
| 21 |
+
from math_verify import LatexExtractionConfig, parse, verify
|
| 22 |
+
from trl import GRPOConfig, GRPOTrainer, ModelConfig, ScriptArguments, TrlParser, get_peft_config
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@dataclass
|
| 26 |
+
class GRPOScriptArguments(ScriptArguments):
|
| 27 |
+
"""
|
| 28 |
+
Script arguments for the GRPO training script.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
reward_funcs (`list[str]`):
|
| 32 |
+
List of reward functions. Possible values: 'accuracy', 'format'.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
reward_funcs: list[str] = field(
|
| 36 |
+
default_factory=lambda: ["accuracy", "format"],
|
| 37 |
+
metadata={"help": "List of reward functions. Possible values: 'accuracy', 'format'"},
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def accuracy_reward(completions, solution, **kwargs):
|
| 42 |
+
"""Reward function that checks if the completion is the same as the ground truth."""
|
| 43 |
+
contents = [completion[0]["content"] for completion in completions]
|
| 44 |
+
rewards = []
|
| 45 |
+
for content, sol in zip(contents, solution):
|
| 46 |
+
gold_parsed = parse(sol, extraction_mode="first_match", extraction_config=[LatexExtractionConfig()])
|
| 47 |
+
if len(gold_parsed) != 0:
|
| 48 |
+
# We require the answer to be provided in correct latex (no malformed operators)
|
| 49 |
+
answer_parsed = parse(
|
| 50 |
+
content,
|
| 51 |
+
extraction_config=[
|
| 52 |
+
LatexExtractionConfig(
|
| 53 |
+
normalization_config=NormalizationConfig(
|
| 54 |
+
nits=False,
|
| 55 |
+
malformed_operators=False,
|
| 56 |
+
basic_latex=True,
|
| 57 |
+
equations=True,
|
| 58 |
+
boxed=True,
|
| 59 |
+
units=True,
|
| 60 |
+
),
|
| 61 |
+
# Ensures that boxed is tried first
|
| 62 |
+
boxed_match_priority=0,
|
| 63 |
+
try_extract_without_anchor=False,
|
| 64 |
+
)
|
| 65 |
+
],
|
| 66 |
+
extraction_mode="first_match",
|
| 67 |
+
)
|
| 68 |
+
# Reward 1 if the content is the same as the ground truth, 0 otherwise
|
| 69 |
+
reward = float(verify(answer_parsed, gold_parsed))
|
| 70 |
+
else:
|
| 71 |
+
# If the gold solution is not parseable, we reward 1 to skip this example
|
| 72 |
+
reward = 1.0
|
| 73 |
+
print("Failed to parse gold solution: ", sol)
|
| 74 |
+
rewards.append(reward)
|
| 75 |
+
|
| 76 |
+
return rewards
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def format_reward(completions, **kwargs):
|
| 80 |
+
"""Reward function that checks if the completion has a specific format."""
|
| 81 |
+
pattern = r"^<think>.*?</think><answer>.*?</answer>$"
|
| 82 |
+
completion_contents = [completion[0]["content"] for completion in completions]
|
| 83 |
+
matches = [re.match(pattern, content) for content in completion_contents]
|
| 84 |
+
return [1.0 if match else 0.0 for match in matches]
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
reward_funcs_registry = {
|
| 88 |
+
"accuracy": accuracy_reward,
|
| 89 |
+
"format": format_reward,
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
SYSTEM_PROMPT = (
|
| 93 |
+
"A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant "
|
| 94 |
+
"first thinks about the reasoning process in the mind and then provides the user with the answer. The reasoning "
|
| 95 |
+
"process and answer are enclosed within <think> </think> and <answer> </answer> tags, respectively, i.e., "
|
| 96 |
+
"<think> reasoning process here </think><answer> answer here </answer>"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def main(script_args, training_args, model_args):
|
| 101 |
+
# Get reward functions
|
| 102 |
+
reward_funcs = [reward_funcs_registry[func] for func in script_args.reward_funcs]
|
| 103 |
+
|
| 104 |
+
# Load the dataset
|
| 105 |
+
dataset = load_dataset(script_args.dataset_name, name=script_args.dataset_config)
|
| 106 |
+
|
| 107 |
+
# Format into conversation
|
| 108 |
+
def make_conversation(example):
|
| 109 |
+
return {
|
| 110 |
+
"prompt": [
|
| 111 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 112 |
+
{"role": "user", "content": example["problem"]},
|
| 113 |
+
],
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
dataset = dataset.map(make_conversation)
|
| 117 |
+
dataset = dataset.remove_columns("messages")
|
| 118 |
+
|
| 119 |
+
# Initialize the GRPO trainer
|
| 120 |
+
trainer = GRPOTrainer(
|
| 121 |
+
model=model_args.model_name_or_path,
|
| 122 |
+
reward_funcs=reward_funcs,
|
| 123 |
+
args=training_args,
|
| 124 |
+
train_dataset=dataset[script_args.dataset_train_split],
|
| 125 |
+
eval_dataset=dataset[script_args.dataset_test_split] if training_args.eval_strategy != "no" else None,
|
| 126 |
+
peft_config=get_peft_config(model_args),
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Train and push the model to the Hub
|
| 130 |
+
trainer.train()
|
| 131 |
+
|
| 132 |
+
# Save and push to hub
|
| 133 |
+
trainer.save_model(training_args.output_dir)
|
| 134 |
+
if training_args.push_to_hub:
|
| 135 |
+
trainer.push_to_hub(dataset_name=script_args.dataset_name)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
parser = TrlParser((GRPOScriptArguments, GRPOConfig, ModelConfig))
|
| 140 |
+
script_args, training_args, model_args = parser.parse_args_and_config()
|
| 141 |
+
main(script_args, training_args, model_args)
|
src/open_r1/sft.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
"""
|
| 16 |
+
Supervised fine-tuning script for decoder language models.
|
| 17 |
+
|
| 18 |
+
Usage:
|
| 19 |
+
|
| 20 |
+
# One 1 node of 8 x H100s
|
| 21 |
+
accelerate launch --config_file=configs/zero3.yaml src/open_r1/sft.py \
|
| 22 |
+
--model_name_or_path Qwen/Qwen2.5-1.5B-Instruct \
|
| 23 |
+
--dataset_name HuggingFaceH4/Bespoke-Stratos-17k \
|
| 24 |
+
--learning_rate 2.0e-5 \
|
| 25 |
+
--num_train_epochs 1 \
|
| 26 |
+
--packing \
|
| 27 |
+
--max_seq_length 4096 \
|
| 28 |
+
--per_device_train_batch_size 4 \
|
| 29 |
+
--gradient_accumulation_steps 4 \
|
| 30 |
+
--gradient_checkpointing \
|
| 31 |
+
--bf16 \
|
| 32 |
+
--logging_steps 5 \
|
| 33 |
+
--eval_strategy steps \
|
| 34 |
+
--eval_steps 100 \
|
| 35 |
+
--output_dir data/Qwen2.5-1.5B-Open-R1-Distill
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
from datasets import load_dataset
|
| 39 |
+
from transformers import AutoTokenizer
|
| 40 |
+
|
| 41 |
+
from trl import (
|
| 42 |
+
ModelConfig,
|
| 43 |
+
ScriptArguments,
|
| 44 |
+
SFTConfig,
|
| 45 |
+
SFTTrainer,
|
| 46 |
+
TrlParser,
|
| 47 |
+
get_kbit_device_map,
|
| 48 |
+
get_peft_config,
|
| 49 |
+
get_quantization_config,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def main(script_args, training_args, model_args):
|
| 54 |
+
################
|
| 55 |
+
# Model init kwargs & Tokenizer
|
| 56 |
+
################
|
| 57 |
+
quantization_config = get_quantization_config(model_args)
|
| 58 |
+
model_kwargs = dict(
|
| 59 |
+
revision=model_args.model_revision,
|
| 60 |
+
trust_remote_code=model_args.trust_remote_code,
|
| 61 |
+
attn_implementation=model_args.attn_implementation,
|
| 62 |
+
torch_dtype=model_args.torch_dtype,
|
| 63 |
+
use_cache=False if training_args.gradient_checkpointing else True,
|
| 64 |
+
device_map=get_kbit_device_map() if quantization_config is not None else None,
|
| 65 |
+
quantization_config=quantization_config,
|
| 66 |
+
)
|
| 67 |
+
training_args.model_init_kwargs = model_kwargs
|
| 68 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 69 |
+
model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code, use_fast=True
|
| 70 |
+
)
|
| 71 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 72 |
+
|
| 73 |
+
################
|
| 74 |
+
# Dataset
|
| 75 |
+
################
|
| 76 |
+
dataset = load_dataset(script_args.dataset_name, name=script_args.dataset_config)
|
| 77 |
+
|
| 78 |
+
################
|
| 79 |
+
# Training
|
| 80 |
+
################
|
| 81 |
+
trainer = SFTTrainer(
|
| 82 |
+
model=model_args.model_name_or_path,
|
| 83 |
+
args=training_args,
|
| 84 |
+
train_dataset=dataset[script_args.dataset_train_split],
|
| 85 |
+
eval_dataset=dataset[script_args.dataset_test_split] if training_args.eval_strategy != "no" else None,
|
| 86 |
+
processing_class=tokenizer,
|
| 87 |
+
peft_config=get_peft_config(model_args),
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
trainer.train()
|
| 91 |
+
|
| 92 |
+
# Save and push to hub
|
| 93 |
+
trainer.save_model(training_args.output_dir)
|
| 94 |
+
if training_args.push_to_hub:
|
| 95 |
+
trainer.push_to_hub(dataset_name=script_args.dataset_name)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
parser = TrlParser((ScriptArguments, SFTConfig, ModelConfig))
|
| 100 |
+
script_args, training_args, model_args = parser.parse_args_and_config()
|
| 101 |
+
main(script_args, training_args, model_args)
|