rwkv-quant 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.
- rwkv_quant-0.1.0/LICENSE +201 -0
- rwkv_quant-0.1.0/PKG-INFO +303 -0
- rwkv_quant-0.1.0/README.md +280 -0
- rwkv_quant-0.1.0/pyproject.toml +34 -0
- rwkv_quant-0.1.0/rwkv_quant/__init__.py +4 -0
- rwkv_quant-0.1.0/rwkv_quant/api.py +111 -0
- rwkv_quant-0.1.0/rwkv_quant/backends/__init__.py +0 -0
- rwkv_quant-0.1.0/rwkv_quant/backends/cuda/__init__.py +0 -0
- rwkv_quant-0.1.0/rwkv_quant/backends/metal/__init__.py +0 -0
- rwkv_quant-0.1.0/rwkv_quant/backends/metal/quant_linear.py +106 -0
- rwkv_quant-0.1.0/rwkv_quant/backends/metal/quant_linear_gw.py +1020 -0
- rwkv_quant-0.1.0/rwkv_quant/backends/metal/quant_linear_v2.py +351 -0
- rwkv_quant-0.1.0/rwkv_quant/backends/metal/quant_model.py +637 -0
- rwkv_quant-0.1.0/rwkv_quant/calibration/__init__.py +4 -0
- rwkv_quant-0.1.0/rwkv_quant/calibration/ablation.py +143 -0
- rwkv_quant-0.1.0/rwkv_quant/calibration/fake_quant.py +65 -0
- rwkv_quant-0.1.0/rwkv_quant/calibration/group_config.py +55 -0
- rwkv_quant-0.1.0/rwkv_quant/calibration/outlier_scan.py +68 -0
- rwkv_quant-0.1.0/rwkv_quant/formats/__init__.py +6 -0
- rwkv_quant-0.1.0/rwkv_quant/formats/export_mlx.py +55 -0
- rwkv_quant-0.1.0/rwkv_quant/formats/reader.py +69 -0
- rwkv_quant-0.1.0/rwkv_quant/formats/schema.py +163 -0
- rwkv_quant-0.1.0/rwkv_quant/formats/writer.py +426 -0
- rwkv_quant-0.1.0/rwkv_quant/models/__init__.py +4 -0
- rwkv_quant-0.1.0/rwkv_quant/models/naming.py +19 -0
- rwkv_quant-0.1.0/rwkv_quant/models/rwkv7_ref.py +273 -0
- rwkv_quant-0.1.0/rwkv_quant/presets.py +73 -0
- rwkv_quant-0.1.0/rwkv_quant.egg-info/PKG-INFO +303 -0
- rwkv_quant-0.1.0/rwkv_quant.egg-info/SOURCES.txt +48 -0
- rwkv_quant-0.1.0/rwkv_quant.egg-info/dependency_links.txt +1 -0
- rwkv_quant-0.1.0/rwkv_quant.egg-info/requires.txt +8 -0
- rwkv_quant-0.1.0/rwkv_quant.egg-info/top_level.txt +1 -0
- rwkv_quant-0.1.0/setup.cfg +4 -0
- rwkv_quant-0.1.0/tests/test_combined_sanity_check.py +38 -0
- rwkv_quant-0.1.0/tests/test_fake_quant.py +0 -0
- rwkv_quant-0.1.0/tests/test_fuse_parity.py +82 -0
- rwkv_quant-0.1.0/tests/test_gemm_prefill.py +96 -0
- rwkv_quant-0.1.0/tests/test_gw_kernel.py +30 -0
- rwkv_quant-0.1.0/tests/test_gw_kernel_int6.py +44 -0
- rwkv_quant-0.1.0/tests/test_gw_nb_parity.py +38 -0
- rwkv_quant-0.1.0/tests/test_int4_packing.py +82 -0
- rwkv_quant-0.1.0/tests/test_naming_detection.py +0 -0
- rwkv_quant-0.1.0/tests/test_quant_linear_metal.py +92 -0
- rwkv_quant-0.1.0/tests/test_quant_linear_v2.py +56 -0
- rwkv_quant-0.1.0/tests/test_quant_model_smoke.py +112 -0
- rwkv_quant-0.1.0/tests/test_quant_parity.py +145 -0
- rwkv_quant-0.1.0/tests/test_streaming_inference.py +89 -0
- rwkv_quant-0.1.0/tests/test_v2_format.py +69 -0
- rwkv_quant-0.1.0/tests/test_v2_negative.py +33 -0
- rwkv_quant-0.1.0/tests/test_wkv_var_model.py +81 -0
rwkv_quant-0.1.0/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.
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rwkv-quant
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Quantization toolkit and Metal inference backend for RWKV-7 on Apple Silicon
|
|
5
|
+
Author: Alexei Goncharov
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://impulseleap.com
|
|
8
|
+
Keywords: rwkv,quantization,mlx,metal,apple-silicon,llm,inference
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Operating System :: MacOS
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: mlx>=0.31
|
|
16
|
+
Requires-Dist: numpy
|
|
17
|
+
Requires-Dist: torch
|
|
18
|
+
Requires-Dist: safetensors
|
|
19
|
+
Requires-Dist: rwkv-metal>=0.1.3
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest; extra == "dev"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# rwkv-quant
|
|
25
|
+
|
|
26
|
+
Quantization toolkit **and Metal inference backend** for RWKV-7 on Apple
|
|
27
|
+
Silicon. Portable `.rwkvq` checkpoint format, outlier-aware calibration, and
|
|
28
|
+
custom Metal GEMV kernels that decode the quantized format directly — no
|
|
29
|
+
dequantized weight copy in memory.
|
|
30
|
+
|
|
31
|
+
Reference model: `rwkv7-g1h-1.5b` (BlinkDL G1H 1.5B, bf16 2953 MB).
|
|
32
|
+
Reference machine: M4 MacBook Air 16 GB (base chip, fanless).
|
|
33
|
+
|
|
34
|
+
## Results
|
|
35
|
+
|
|
36
|
+
| model | size | ppl | vs bf16 | decode | prefill (T=1024) |
|
|
37
|
+
|---|---|---|---|---|---|
|
|
38
|
+
| bf16 (no quant) | 2953 MB | 11.430 | — | — | — |
|
|
39
|
+
| **`reduction`** (ours — groupwise int6 + AW) | 1255.9 MB (2.35x) | 11.4438 | **+0.12%** | 17.7 ms/tok | 437 tok/s |
|
|
40
|
+
| **`compression`** (ours — groupwise int4/5 + AW) | 970.7 MB (3.04x) | 11.7125 | +2.47% | **14.8 ms/tok** (14.15 fused, 14.02 w/ n-gram speculation) | 545 tok/s |
|
|
41
|
+
| MollySophia MLX int6† (community, native MLX affine) | 1272.0 MB | 11.5507 | +1.06% | 15.10 ms/tok | 591 tok/s |
|
|
42
|
+
| Canonical int6 RTN‡ (naive per-row, no groups/AW) | 1530.5 MB | 13.2726 | +16.12% | 19.44 ms/tok | 630 tok/s |
|
|
43
|
+
| Canonical int4 RTN‡ (naive per-row, no groups/AW) | 767.1 MB | 3798.62 | ~332x (broken) | 13.65 ms/tok | 543 tok/s |
|
|
44
|
+
|
|
45
|
+
`reduction` beats the community int6 build on quality (9x less degradation)
|
|
46
|
+
at a comparable size. `compression` beats it on every axis: smaller, faster,
|
|
47
|
+
and less than half the quality loss. Against the canonical (naive,
|
|
48
|
+
off-the-shelf-style) baselines: `reduction` is both **smaller and 130x less
|
|
49
|
+
degraded** than canonical int6 despite the same nominal bit depth — canonical
|
|
50
|
+
int6 has no real sub-byte packing in this codebase, so it lands at roughly
|
|
51
|
+
the same disk size as int8 would; naive bit-width choice alone does not buy
|
|
52
|
+
real compression without a dedicated packer. Canonical int4 goes further and
|
|
53
|
+
simply breaks the model (ppl 3798, not a typo). See [Method](#method) for why.
|
|
54
|
+
|
|
55
|
+
† MollySophia hasn't published a G1H build, so her row is measured on the G1
|
|
56
|
+
checkpoint, not G1H — size/decode/prefill are apples-to-apples (identical
|
|
57
|
+
architecture, same process, same machine), but part of the ppl gap may
|
|
58
|
+
reflect the different base checkpoint, not only the quantization scheme.
|
|
59
|
+
|
|
60
|
+
‡ Canonical rows were measured on the same reference machine and corpus, but
|
|
61
|
+
as separate single-model runs (`tests/eval_canonical_int4.py` /
|
|
62
|
+
`eval_canonical_int6.py`), not in the same interleaved four-way A/B process
|
|
63
|
+
used for the bf16/MollySophia/`compression`/`reduction` row — see
|
|
64
|
+
[What "ppl" means](#what-ppl-means-and-how-its-measured) for why that
|
|
65
|
+
distinction matters for the timing columns (not for ppl, which doesn't
|
|
66
|
+
depend on thermal state).
|
|
67
|
+
|
|
68
|
+
## Method
|
|
69
|
+
|
|
70
|
+
Four things distinguish `reduction`/`compression` from a generic INT4/INT6
|
|
71
|
+
quantizer (i.e. from the canonical rows above):
|
|
72
|
+
|
|
73
|
+
1. **Groupwise asymmetric scale, not per-row/per-tensor.** Weights are split
|
|
74
|
+
into blocks of 32 (each with its own 6-bit scale/min pair), grouped into
|
|
75
|
+
superblocks of 256 (each with an fp16 scale pair the 6-bit pairs multiply
|
|
76
|
+
against) — see [Format](#format). A per-row or per-tensor scale is set by
|
|
77
|
+
that row's single largest value; real RWKV-7 weights have per-channel
|
|
78
|
+
outliers **40-96x** the typical magnitude (measured on the 1.5B
|
|
79
|
+
reference — see [Root cause](#root-cause)), so a per-row scale forces
|
|
80
|
+
everything else in that row into 1-2 quantization codes. A block of 32 is
|
|
81
|
+
small enough that an outlier only damages its own block, not the whole
|
|
82
|
+
row.
|
|
83
|
+
2. **Real sub-byte packing.** Blocks are stored as nibbles (with extra
|
|
84
|
+
bit-planes for 5/6-bit codes) — an actual `IN/2`-byte footprint for 4-bit
|
|
85
|
+
codes, not codes-stored-as-int8-anyway. This is *why* canonical int6 in
|
|
86
|
+
the table above is the biggest file in the comparison despite being lower
|
|
87
|
+
bit-depth than bf16's implicit 16: without a dedicated packer, "6-bit"
|
|
88
|
+
quantization has nowhere to shrink to below int8.
|
|
89
|
+
3. **Activation-weighted (AW) scale search, applied per group, not
|
|
90
|
+
blanket.** Instead of minimizing raw weight reconstruction error, AW
|
|
91
|
+
search minimizes error weighted by activation statistics from a
|
|
92
|
+
calibration pass — it measurably helps `cmix`/`emb_head` at these bit
|
|
93
|
+
depths, but *hurts* `proj` at 6-bit (a result from direct measurement,
|
|
94
|
+
not something predicted in advance — see `presets.py`). Each preset turns
|
|
95
|
+
AW on or off per group based on what the measurement showed, not a single
|
|
96
|
+
global switch.
|
|
97
|
+
4. **Per-group bit-width calibration instead of a uniform bit depth.**
|
|
98
|
+
Sensitivity to quantization varies enormously by parameter group *and*
|
|
99
|
+
doesn't transfer across model scale — `small`/`g_lora` survive INT2 on a
|
|
100
|
+
61M model and go catastrophic on the 1.5B reference at the same bit
|
|
101
|
+
depth (see [Why quantization sensitivity doesn't transfer across
|
|
102
|
+
scale](#why-quantization-sensitivity-doesnt-transfer-across-scale)).
|
|
103
|
+
`reduction`/`compression` are presets calibrated on the 1.5B reference;
|
|
104
|
+
`calibrate()` runs the same per-group search on a checkpoint of your
|
|
105
|
+
choosing instead of assuming a preset transfers.
|
|
106
|
+
|
|
107
|
+
A sparse-outlier scheme (SpQR-style: store a few exact values out-of-band
|
|
108
|
+
instead of quantizing them) was tried earlier and is retained in
|
|
109
|
+
`calibration/` for reference, but groupwise scale alone reached about half
|
|
110
|
+
the quality loss of per-row+SpQR at the same size — see
|
|
111
|
+
[Format](#format) for that finding.
|
|
112
|
+
|
|
113
|
+
## What "ppl" means and how it's measured
|
|
114
|
+
|
|
115
|
+
**Perplexity (ppl)** = `exp(mean token negative log-likelihood)` under
|
|
116
|
+
teacher forcing — informally, how "surprised" the model is by the correct
|
|
117
|
+
next token on average, exponentiated back into a per-token scale. Lower is
|
|
118
|
+
better; a bf16 model is the reference point, and every other row is reported
|
|
119
|
+
as `+X%` — the relative *increase* in ppl caused by quantization (so `+0.12%`
|
|
120
|
+
means near-lossless, `~332x` means broken).
|
|
121
|
+
|
|
122
|
+
All numbers in this README come from the same fixed corpus: an 8-sequence x
|
|
123
|
+
128-token slice (1024 tokens, 1016 scored after the next-token shift) from a
|
|
124
|
+
~91k-character, multi-language, multi-topic text mix (literature, technical
|
|
125
|
+
writing, encyclopedic text, several languages), tokenized once with the
|
|
126
|
+
standard RWKV World tokenizer (byte-level trie, greedy longest match) and
|
|
127
|
+
reused unchanged across every row. **This is not a published benchmark** —
|
|
128
|
+
not WikiText, not LAMBADA — so the absolute ppl numbers are only meaningful
|
|
129
|
+
*relative to each other*, on this exact slice, with this exact tokenizer;
|
|
130
|
+
don't compare them to numbers from other papers or other corpora.
|
|
131
|
+
|
|
132
|
+
Every row is scored through its own real quantized kernel end-to-end (never
|
|
133
|
+
a dequant-to-dense shortcut) — `reduction`/`compression` via
|
|
134
|
+
`backends/metal/quant_linear_gw.py`, MollySophia's via `mx.quantized_matmul`
|
|
135
|
+
(her native MLX format), canonical via the plain per-row kernel — so the ppl
|
|
136
|
+
numbers reflect what you'd actually get running that kernel, not a
|
|
137
|
+
theoretical best case. The WKV-7 recurrence itself is never quantized in any
|
|
138
|
+
row; only the linear projections differ by scheme.
|
|
139
|
+
|
|
140
|
+
## Quick start
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from rwkv_quant import quantize
|
|
144
|
+
|
|
145
|
+
# near-lossless, 2.35x smaller than bf16 (+0.12% ppl on the 1.5B reference)
|
|
146
|
+
quantize("model.pth", "model.rwkvq", preset="reduction")
|
|
147
|
+
|
|
148
|
+
# 3.04x smaller, moderate cost (+2.47% ppl), fastest decode
|
|
149
|
+
quantize("model.pth", "model.rwkvq", preset="compression")
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Both presets use activation-weighted (AW) scale search and expect activation
|
|
153
|
+
statistics at the path set in `QuantConfig.act_stats_path`
|
|
154
|
+
(`tests/collect_act_stats.py` produces them in ~30 s).
|
|
155
|
+
|
|
156
|
+
Inference (Metal / MLX):
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from rwkv_quant.formats.reader import load_raw
|
|
160
|
+
from rwkv_quant.backends.metal.quant_model import QuantRWKV7
|
|
161
|
+
|
|
162
|
+
model = QuantRWKV7(load_raw("model.rwkvq"))
|
|
163
|
+
state = model.init_state(1)
|
|
164
|
+
logits, state = model.forward_stateful(token_ids, state) # prefill / decode
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Presets are calibrated on `rwkv7-g1h-1.5b` — see
|
|
168
|
+
[Why presets aren't universal](#why-quantization-sensitivity-doesnt-transfer-across-scale).
|
|
169
|
+
For a checkpoint-specific config run `calibrate()` or build a `QuantConfig`
|
|
170
|
+
by hand (per-group bits, group scale sizes, scale modes, clipping).
|
|
171
|
+
|
|
172
|
+
## Format
|
|
173
|
+
|
|
174
|
+
`.rwkvq` stores group-wise asymmetric quantization ("sb6"): blocks of 32
|
|
175
|
+
weights share a 6-bit scale/min pair (`qs`/`qm`), superblocks of 256 share an
|
|
176
|
+
fp16 pair (`d`/`dm`) that the 6-bit pairs multiply. Codes are packed as
|
|
177
|
+
nibbles; INT5/INT6 add one/two bit-planes on top. Scale search is
|
|
178
|
+
activation-weighted where it helps (per-group setting). The format is
|
|
179
|
+
backend-independent; per-tensor bits and modes live in the file, not in code.
|
|
180
|
+
|
|
181
|
+
A finding that shaped the presets: **granularity beats bits.** Group-wise
|
|
182
|
+
sb6 at INT4/5 replaced an earlier per-row + SpQR-outlier scheme of the same
|
|
183
|
+
size with roughly half the quality loss. Sub-nibble packing (sub-887 MB at
|
|
184
|
+
sane ppl) does not fit the nibble container — that's a future format, not a
|
|
185
|
+
tuning exercise.
|
|
186
|
+
|
|
187
|
+
## Kernels
|
|
188
|
+
|
|
189
|
+
`backends/metal/` decodes sb6 on the fly inside GEMV — weights never exist
|
|
190
|
+
dequantized in memory. Highlights (all validated bit-exact against the
|
|
191
|
+
reference implementation, so quality numbers carry over without re-eval):
|
|
192
|
+
|
|
193
|
+
- **Layout borrowed from MLX `qmv`** (PR #1503): N simdgroups x R rows per
|
|
194
|
+
threadgroup, dispatch table per matrix shape.
|
|
195
|
+
- **Interleaved load-time repack**: codes + bit-planes contiguous per block,
|
|
196
|
+
quality scalars as `uchar2`/`half2` — 4-5 memory transactions per
|
|
197
|
+
(row, block) instead of 7. On-disk format untouched; memory stays 1x.
|
|
198
|
+
- **Bit-plane decode via multiply trick** (`(nib * 0x00204081) & 0x01010101`)
|
|
199
|
+
— ~3x less ALU per plane; this is what unlocked INT6 decode speed
|
|
200
|
+
(head INT6: 88 → 103 GB/s, ~85 % of the M4's DRAM bandwidth).
|
|
201
|
+
- **Batched verify kernels** (weights decoded once per N columns) for
|
|
202
|
+
speculative decoding; n-gram prompt-lookup speculation ships in the demo
|
|
203
|
+
scripts (1.08-1.25x on repetitive text, never slower).
|
|
204
|
+
- Fused r/k/v projection launch and fused lerp/LoRA batching in the decode
|
|
205
|
+
path.
|
|
206
|
+
|
|
207
|
+
## Why quantization sensitivity doesn't transfer across scale
|
|
208
|
+
|
|
209
|
+
Ran weight-only fake-quantization ablations on two RWKV-7 checkpoints — a
|
|
210
|
+
custom 61M Russian model (18L/D448) and BlinkDL's official 1.5B G1H — across
|
|
211
|
+
8 parameter groups: `proj` (R/K/V/O), the four LoRA-style projections
|
|
212
|
+
(`w_lora`/`a_lora`/`v_lora`/`g_lora` — decay, in-context learning rate, value
|
|
213
|
+
residual, output gate), `small` (k_k/k_a/r_k), `cmix` (FFN), `emb_head`.
|
|
214
|
+
|
|
215
|
+
**On 61M**, every LoRA-ish component survived INT2 with <2% ppl loss. Only the
|
|
216
|
+
full-rank `proj`/`cmix` matrices were fragile at INT2.
|
|
217
|
+
|
|
218
|
+
**On 1.5B**, that pattern inverts. At INT4 alone:
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
group 61M Δppl 1.5B Δppl
|
|
222
|
+
proj +0.55% +19.97%
|
|
223
|
+
cmix +1.41% +48.09%
|
|
224
|
+
emb_head +4.03% +97.17%
|
|
225
|
+
g_lora -0.02% +7.55%
|
|
226
|
+
small +0.05% +21,784,766% <- yes, really
|
|
227
|
+
w/a/v_lora ~0.02% <0.5%
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
`small` and `g_lora` go from "quantize freely" to "catastrophic" as the model
|
|
231
|
+
scales up. **Assuming a group is safe because it was safe on a smaller
|
|
232
|
+
checkpoint is a real trap** — this repo's `calibrate()` exists specifically so
|
|
233
|
+
you don't have to guess.
|
|
234
|
+
|
|
235
|
+
### Root cause
|
|
236
|
+
|
|
237
|
+
Per-row max/mean ratios of 40–96x show up in `r_k`, `k_k`, `k_a`, and even in
|
|
238
|
+
`proj`/`cmix`. With symmetric per-channel quantization, scale = max/qmax — a
|
|
239
|
+
single 96x outlier in an otherwise tight channel forces the other ~63 "normal"
|
|
240
|
+
values into 1–2 quantization codes, destroying the channel. Same mechanism as
|
|
241
|
+
`LLM.int8()`'s outlier features in transformers, showing up in RWKV-7's
|
|
242
|
+
LoRA-style decay/gate projections instead of attention.
|
|
243
|
+
|
|
244
|
+
Mitigations differ per group and are not interchangeable: percentile clipping
|
|
245
|
+
rescues `small` (INT6 +11.55 % → +1.60 %) but *hurts* the dense matrices,
|
|
246
|
+
whose outlier tail is trained signal. For dense groups the current presets
|
|
247
|
+
use group-wise asymmetric scales (see [Format](#format)); the earlier
|
|
248
|
+
SpQR-style sparse-outlier path is retained in `calibration/` for study.
|
|
249
|
+
|
|
250
|
+
## Caveats
|
|
251
|
+
|
|
252
|
+
- Presets are calibrated on one 1.5B checkpoint; recalibrate for other sizes
|
|
253
|
+
(`small`/`g_lora` stay INT8 in both presets for a reason).
|
|
254
|
+
- Kernel dispatch tables are tuned on an M4 base chip; other Apple Silicon
|
|
255
|
+
will work but may prefer different (simdgroups x rows) configs.
|
|
256
|
+
- ppl deltas are measured on a small held-out corpus; treat them as relative
|
|
257
|
+
quality signals, not benchmarks.
|
|
258
|
+
- `scripts/` and `examples/` are placeholders for now — the maintained entry
|
|
259
|
+
points are `rwkv_quant.api` and the benches/gates under `tests/`.
|
|
260
|
+
- CUDA backend is an empty stub; Metal is the only real inference path today.
|
|
261
|
+
|
|
262
|
+
## Repo layout
|
|
263
|
+
|
|
264
|
+
See [STRUCTURE.md](./STRUCTURE.md). Session-to-session engineering log with
|
|
265
|
+
measurement methodology (fanless-Mac A/B discipline, bit-exactness gates)
|
|
266
|
+
lives in [NEXT_SESSION.md](./NEXT_SESSION.md) and git history.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Open problems / contributions welcome
|
|
271
|
+
|
|
272
|
+
A few things are known gaps, not yet done — good entry points if you want to
|
|
273
|
+
contribute:
|
|
274
|
+
|
|
275
|
+
- **CUDA backend is an empty stub.** Metal is the only real inference path
|
|
276
|
+
today; the `.rwkvq` format itself is backend-independent, so a CUDA kernel
|
|
277
|
+
implementation is "just" a kernel, not a format change.
|
|
278
|
+
- **LoRA-style gate branch is un-fused.** The small per-layer decay/gate
|
|
279
|
+
matmuls (`w/a/v/g_lora`) currently cost ~6-8 separate kernel launches per
|
|
280
|
+
layer; fusing them into one or two custom kernels is estimated at another
|
|
281
|
+
~0.5-1 ms/token on decode, not yet built.
|
|
282
|
+
- **The fused kernel path (`FUSE=True`) isn't the default yet**, despite
|
|
283
|
+
being a stable ~0.8 ms/token win with matching correctness gates — flipping
|
|
284
|
+
the default (and updating the benchmarks that assume `FUSE=False`) is
|
|
285
|
+
pending.
|
|
286
|
+
- **Sub-nibble packing.** The current nibble container has a hard floor
|
|
287
|
+
around 887 MB for this model at acceptable quality — going smaller needs a
|
|
288
|
+
new on-disk format (sparsity- or sub-nibble-based), not just a bit-width
|
|
289
|
+
tuning pass.
|
|
290
|
+
- **`calibrate()` is single-group ablation.** It picks per-group bit width
|
|
291
|
+
independently and doesn't fully model interaction effects when several
|
|
292
|
+
groups are quantized simultaneously; a joint search would be more accurate
|
|
293
|
+
but much more expensive to run.
|
|
294
|
+
- **Presets are calibrated on one 1.5B checkpoint.** Validating (or
|
|
295
|
+
recalibrating) `reduction`/`compression` on other model sizes — especially
|
|
296
|
+
much smaller or much larger ones, where sensitivity per group is known to
|
|
297
|
+
shift (see [Why quantization sensitivity doesn't transfer across
|
|
298
|
+
scale](#why-quantization-sensitivity-doesnt-transfer-across-scale)) — is
|
|
299
|
+
open.
|
|
300
|
+
|
|
301
|
+
## Author
|
|
302
|
+
|
|
303
|
+
Alexei Goncharov
|