Instructions to use ggml-org-dev/tiny-lev-for-testing with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use ggml-org-dev/tiny-lev-for-testing with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("ggml-org-dev/tiny-Qwen3.5-4B-for-testing") model = PeftModel.from_pretrained(base_model, "ggml-org-dev/tiny-lev-for-testing") - Notebooks
- Google Colab
- Kaggle
tiny sliced copy for testing
Browse files- .gitattributes +1 -0
- LICENSE +202 -0
- README.md +29 -0
- adapter_config.json +51 -0
- adapter_model.safetensors +3 -0
- calibration.json +23 -0
- chat_template.jinja +154 -0
- lev_release.json +23 -0
- mode_b_head.pt +3 -0
- slice_qwen35_lora.py +254 -0
- tokenizer.json +3 -0
- tokenizer_config.json +32 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Apache License
|
| 3 |
+
Version 2.0, January 2004
|
| 4 |
+
http://www.apache.org/licenses/
|
| 5 |
+
|
| 6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 7 |
+
|
| 8 |
+
1. Definitions.
|
| 9 |
+
|
| 10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 12 |
+
|
| 13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 14 |
+
the copyright owner that is granting the License.
|
| 15 |
+
|
| 16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 17 |
+
other entities that control, are controlled by, or are under common
|
| 18 |
+
control with that entity. For the purposes of this definition,
|
| 19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 20 |
+
direction or management of such entity, whether by contract or
|
| 21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 23 |
+
|
| 24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 25 |
+
exercising permissions granted by this License.
|
| 26 |
+
|
| 27 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
+
including but not limited to software source code, documentation
|
| 29 |
+
source, and configuration files.
|
| 30 |
+
|
| 31 |
+
"Object" form shall mean any form resulting from mechanical
|
| 32 |
+
transformation or translation of a Source form, including but
|
| 33 |
+
not limited to compiled object code, generated documentation,
|
| 34 |
+
and conversions to other media types.
|
| 35 |
+
|
| 36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
+
Object form, made available under the License, as indicated by a
|
| 38 |
+
copyright notice that is included in or attached to the work
|
| 39 |
+
(an example is provided in the Appendix below).
|
| 40 |
+
|
| 41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
+
form, that is based on (or derived from) the Work and for which the
|
| 43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
+
of this License, Derivative Works shall not include works that remain
|
| 46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
+
the Work and Derivative Works thereof.
|
| 48 |
+
|
| 49 |
+
"Contribution" shall mean any work of authorship, including
|
| 50 |
+
the original version of the Work and any modifications or additions
|
| 51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
+
means any form of electronic, verbal, or written communication sent
|
| 56 |
+
to the Licensor or its representatives, including but not limited to
|
| 57 |
+
communication on electronic mailing lists, source code control systems,
|
| 58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
+
excluding communication that is conspicuously marked or otherwise
|
| 61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
+
|
| 63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
+
subsequently incorporated within the Work.
|
| 66 |
+
|
| 67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
+
Work and such Derivative Works in Source or Object form.
|
| 73 |
+
|
| 74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
+
(except as stated in this section) patent license to make, have made,
|
| 78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
+
where such license applies only to those patent claims licensable
|
| 80 |
+
by such Contributor that are necessarily infringed by their
|
| 81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
+
institute patent litigation against any entity (including a
|
| 84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
+
or contributory patent infringement, then any patent licenses
|
| 87 |
+
granted to You under this License for that Work shall terminate
|
| 88 |
+
as of the date such litigation is filed.
|
| 89 |
+
|
| 90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
+
modifications, and in Source or Object form, provided that You
|
| 93 |
+
meet the following conditions:
|
| 94 |
+
|
| 95 |
+
(a) You must give any other recipients of the Work or
|
| 96 |
+
Derivative Works a copy of this License; and
|
| 97 |
+
|
| 98 |
+
(b) You must cause any modified files to carry prominent notices
|
| 99 |
+
stating that You changed the files; and
|
| 100 |
+
|
| 101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
+
that You distribute, all copyright, patent, trademark, and
|
| 103 |
+
attribution notices from the Source form of the Work,
|
| 104 |
+
excluding those notices that do not pertain to any part of
|
| 105 |
+
the Derivative Works; and
|
| 106 |
+
|
| 107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
+
distribution, then any Derivative Works that You distribute must
|
| 109 |
+
include a readable copy of the attribution notices contained
|
| 110 |
+
within such NOTICE file, excluding those notices that do not
|
| 111 |
+
pertain to any part of the Derivative Works, in at least one
|
| 112 |
+
of the following places: within a NOTICE text file distributed
|
| 113 |
+
as part of the Derivative Works; within the Source form or
|
| 114 |
+
documentation, if provided along with the Derivative Works; or,
|
| 115 |
+
within a display generated by the Derivative Works, if and
|
| 116 |
+
wherever such third-party notices normally appear. The contents
|
| 117 |
+
of the NOTICE file are for informational purposes only and
|
| 118 |
+
do not modify the License. You may add Your own attribution
|
| 119 |
+
notices within Derivative Works that You distribute, alongside
|
| 120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
+
that such additional attribution notices cannot be construed
|
| 122 |
+
as modifying the License.
|
| 123 |
+
|
| 124 |
+
You may add Your own copyright statement to Your modifications and
|
| 125 |
+
may provide additional or different license terms and conditions
|
| 126 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
+
the conditions stated in this License.
|
| 130 |
+
|
| 131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
+
this License, without any additional terms or conditions.
|
| 135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
+
the terms of any separate license agreement you may have executed
|
| 137 |
+
with Licensor regarding such Contributions.
|
| 138 |
+
|
| 139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
+
except as required for reasonable and customary use in describing the
|
| 142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
+
|
| 144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
+
implied, including, without limitation, any warranties or conditions
|
| 149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
+
appropriateness of using or redistributing the Work and assume any
|
| 152 |
+
risks associated with Your exercise of permissions under this License.
|
| 153 |
+
|
| 154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
+
unless required by applicable law (such as deliberate and grossly
|
| 157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
+
liable to You for damages, including any direct, indirect, special,
|
| 159 |
+
incidental, or consequential damages of any character arising as a
|
| 160 |
+
result of this License or out of the use or inability to use the
|
| 161 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
+
other commercial damages or losses), even if such Contributor
|
| 164 |
+
has been advised of the possibility of such damages.
|
| 165 |
+
|
| 166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
+
or other liability obligations and/or rights consistent with this
|
| 170 |
+
License. However, in accepting such obligations, You may act only
|
| 171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
+
defend, and hold each Contributor harmless for any liability
|
| 174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
+
of your accepting any such warranty or additional liability.
|
| 176 |
+
|
| 177 |
+
END OF TERMS AND CONDITIONS
|
| 178 |
+
|
| 179 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
+
|
| 181 |
+
To apply the Apache License to your work, attach the following
|
| 182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
+
replaced with your own identifying information. (Don't include
|
| 184 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
+
comment syntax for the file format. We also recommend that a
|
| 186 |
+
file or class name and description of purpose be included on the
|
| 187 |
+
same "printed page" as the copyright notice for easier
|
| 188 |
+
identification within third-party archives.
|
| 189 |
+
|
| 190 |
+
Copyright 2026 Alibaba Cloud
|
| 191 |
+
|
| 192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
+
you may not use this file except in compliance with the License.
|
| 194 |
+
You may obtain a copy of the License at
|
| 195 |
+
|
| 196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
+
|
| 198 |
+
Unless required by applicable law or agreed to in writing, software
|
| 199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
+
See the License for the specific language governing permissions and
|
| 202 |
+
limitations under the License.
|
README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: ggml-org-dev/tiny-Qwen3.5-4B-for-testing
|
| 4 |
+
library_name: peft
|
| 5 |
+
tags:
|
| 6 |
+
- testing
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
# tiny-lev-for-testing
|
| 10 |
+
|
| 11 |
+
> [!WARNING]
|
| 12 |
+
> This is a **modified, damaged copy** of [interfaze-ai/lev](https://huggingface.co/interfaze-ai/lev), made only to test software. Its output is gibberish. Do not use it for anything else.
|
| 13 |
+
|
| 14 |
+
lev is a LoRA adapter for [Qwen/Qwen3.5-4B](https://huggingface.co/Qwen/Qwen3.5-4B). This copy is a LoRA adapter for the tiny base model [ggml-org-dev/tiny-Qwen3.5-4B-for-testing](https://huggingface.co/ggml-org-dev/tiny-Qwen3.5-4B-for-testing), a sliced copy of Qwen3.5-4B. It keeps the calibration, label readout prompt (chat template) and LoRA layout of lev, so it runs the same code paths as the real model in llama.cpp (`lev` decision type: the converter downloads the base model named in `adapter_config.json` and merges the adapter), but it is small enough for CI.
|
| 15 |
+
|
| 16 |
+
GGUF version: [ggml-org-dev/tiny-lev-for-testing-GGUF](https://huggingface.co/ggml-org-dev/tiny-lev-for-testing-GGUF)
|
| 17 |
+
|
| 18 |
+
## Changes from the original adapter
|
| 19 |
+
|
| 20 |
+
Sliced with [slice_qwen35_lora.py](slice_qwen35_lora.py), which follows the `slice-model` skill of llama.cpp:
|
| 21 |
+
|
| 22 |
+
- `base_model_name_or_path` points at ggml-org-dev/tiny-Qwen3.5-4B-for-testing
|
| 23 |
+
- LoRA tensors of 4 of 32 layers (0, 1, 2, 3), narrowed with the same indices as the base model: A keeps the input columns of its base weight, B its output rows (rank 32 and alpha kept)
|
| 24 |
+
- `mode_b_head.pt` (head for large option sets, not used by llama.cpp): the input projections are narrowed to the hidden size 32
|
| 25 |
+
- The README images (`assets/`) are not included
|
| 26 |
+
|
| 27 |
+
## License
|
| 28 |
+
|
| 29 |
+
Derived from lev by Interfaze AI, under the Apache License 2.0 (as is the base model Qwen3.5-4B).
|
adapter_config.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"alora_invocation_tokens": null,
|
| 3 |
+
"alpha_pattern": {},
|
| 4 |
+
"arrow_config": null,
|
| 5 |
+
"auto_mapping": null,
|
| 6 |
+
"base_model_name_or_path": "ggml-org-dev/tiny-Qwen3.5-4B-for-testing",
|
| 7 |
+
"bias": "none",
|
| 8 |
+
"corda_config": null,
|
| 9 |
+
"ensure_weight_tying": false,
|
| 10 |
+
"eva_config": null,
|
| 11 |
+
"exclude_modules": null,
|
| 12 |
+
"fan_in_fan_out": false,
|
| 13 |
+
"inference_mode": true,
|
| 14 |
+
"init_lora_weights": true,
|
| 15 |
+
"kasa_config": null,
|
| 16 |
+
"layer_replication": null,
|
| 17 |
+
"layers_pattern": null,
|
| 18 |
+
"layers_to_transform": null,
|
| 19 |
+
"loftq_config": {},
|
| 20 |
+
"lora_alpha": 64,
|
| 21 |
+
"lora_bias": false,
|
| 22 |
+
"lora_dropout": 0.05,
|
| 23 |
+
"lora_ga_config": null,
|
| 24 |
+
"megatron_config": null,
|
| 25 |
+
"megatron_core": "megatron.core",
|
| 26 |
+
"modules_to_save": null,
|
| 27 |
+
"monteclora_config": null,
|
| 28 |
+
"peft_type": "LORA",
|
| 29 |
+
"peft_version": "0.21.0",
|
| 30 |
+
"qalora_group_size": 16,
|
| 31 |
+
"r": 32,
|
| 32 |
+
"rank_pattern": {},
|
| 33 |
+
"revision": null,
|
| 34 |
+
"target_modules": [
|
| 35 |
+
"gate_proj",
|
| 36 |
+
"down_proj",
|
| 37 |
+
"up_proj",
|
| 38 |
+
"k_proj",
|
| 39 |
+
"v_proj",
|
| 40 |
+
"o_proj",
|
| 41 |
+
"q_proj"
|
| 42 |
+
],
|
| 43 |
+
"target_parameters": null,
|
| 44 |
+
"task_type": "CAUSAL_LM",
|
| 45 |
+
"trainable_token_indices": null,
|
| 46 |
+
"use_bdlora": null,
|
| 47 |
+
"use_dora": false,
|
| 48 |
+
"use_qalora": false,
|
| 49 |
+
"use_rslora": false,
|
| 50 |
+
"velora_config": null
|
| 51 |
+
}
|
adapter_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eabcdbf099cdba48841cc154b4517383cdf63fba5a3d2d0e402463f76b978348
|
| 3 |
+
size 528336
|
calibration.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"temperatures": {
|
| 3 |
+
"score:A": 1.8687166685806083,
|
| 4 |
+
"choice:A:small": 1.466031501359245,
|
| 5 |
+
"choice:A": 1.4606453602789027,
|
| 6 |
+
"noul:A": 1.282162150839547,
|
| 7 |
+
"choice:A:mid": 1.3228486325144098,
|
| 8 |
+
"choice:A:large": 1.4983293579095338,
|
| 9 |
+
"choice:B:large": 0.5794711375492776,
|
| 10 |
+
"choice:B": 0.5794711375492776
|
| 11 |
+
},
|
| 12 |
+
"fitted_on": "calibration (transfer-selected)",
|
| 13 |
+
"n_samples": {
|
| 14 |
+
"score:A": 12258,
|
| 15 |
+
"choice:A:small": 11480,
|
| 16 |
+
"choice:A": 14133,
|
| 17 |
+
"noul:A": 12073,
|
| 18 |
+
"choice:A:mid": 1138,
|
| 19 |
+
"choice:A:large": 1515,
|
| 20 |
+
"choice:B:large": 10227,
|
| 21 |
+
"choice:B": 10227
|
| 22 |
+
}
|
| 23 |
+
}
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set image_count = namespace(value=0) %}
|
| 2 |
+
{%- set video_count = namespace(value=0) %}
|
| 3 |
+
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
| 4 |
+
{%- if content is string %}
|
| 5 |
+
{{- content }}
|
| 6 |
+
{%- elif content is iterable and content is not mapping %}
|
| 7 |
+
{%- for item in content %}
|
| 8 |
+
{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
|
| 9 |
+
{%- if is_system_content %}
|
| 10 |
+
{{- raise_exception('System message cannot contain images.') }}
|
| 11 |
+
{%- endif %}
|
| 12 |
+
{%- if do_vision_count %}
|
| 13 |
+
{%- set image_count.value = image_count.value + 1 %}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{%- if add_vision_id %}
|
| 16 |
+
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 17 |
+
{%- endif %}
|
| 18 |
+
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 19 |
+
{%- elif 'video' in item or item.type == 'video' %}
|
| 20 |
+
{%- if is_system_content %}
|
| 21 |
+
{{- raise_exception('System message cannot contain videos.') }}
|
| 22 |
+
{%- endif %}
|
| 23 |
+
{%- if do_vision_count %}
|
| 24 |
+
{%- set video_count.value = video_count.value + 1 %}
|
| 25 |
+
{%- endif %}
|
| 26 |
+
{%- if add_vision_id %}
|
| 27 |
+
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 28 |
+
{%- endif %}
|
| 29 |
+
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 30 |
+
{%- elif 'text' in item %}
|
| 31 |
+
{{- item.text }}
|
| 32 |
+
{%- else %}
|
| 33 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- endfor %}
|
| 36 |
+
{%- elif content is none or content is undefined %}
|
| 37 |
+
{{- '' }}
|
| 38 |
+
{%- else %}
|
| 39 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 40 |
+
{%- endif %}
|
| 41 |
+
{%- endmacro %}
|
| 42 |
+
{%- if not messages %}
|
| 43 |
+
{{- raise_exception('No messages provided.') }}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{%- if tools and tools is iterable and tools is not mapping %}
|
| 46 |
+
{{- '<|im_start|>system\n' }}
|
| 47 |
+
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
| 48 |
+
{%- for tool in tools %}
|
| 49 |
+
{{- "\n" }}
|
| 50 |
+
{{- tool | tojson }}
|
| 51 |
+
{%- endfor %}
|
| 52 |
+
{{- "\n</tools>" }}
|
| 53 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 54 |
+
{%- if messages[0].role == 'system' %}
|
| 55 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 56 |
+
{%- if content %}
|
| 57 |
+
{{- '\n\n' + content }}
|
| 58 |
+
{%- endif %}
|
| 59 |
+
{%- endif %}
|
| 60 |
+
{{- '<|im_end|>\n' }}
|
| 61 |
+
{%- else %}
|
| 62 |
+
{%- if messages[0].role == 'system' %}
|
| 63 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 64 |
+
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 65 |
+
{%- endif %}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 68 |
+
{%- for message in messages[::-1] %}
|
| 69 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 70 |
+
{%- if ns.multi_step_tool and message.role == "user" %}
|
| 71 |
+
{%- set content = render_content(message.content, false)|trim %}
|
| 72 |
+
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
|
| 73 |
+
{%- set ns.multi_step_tool = false %}
|
| 74 |
+
{%- set ns.last_query_index = index %}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{%- endif %}
|
| 77 |
+
{%- endfor %}
|
| 78 |
+
{%- if ns.multi_step_tool %}
|
| 79 |
+
{{- raise_exception('No user query found in messages.') }}
|
| 80 |
+
{%- endif %}
|
| 81 |
+
{%- for message in messages %}
|
| 82 |
+
{%- set content = render_content(message.content, true)|trim %}
|
| 83 |
+
{%- if message.role == "system" %}
|
| 84 |
+
{%- if not loop.first %}
|
| 85 |
+
{{- raise_exception('System message must be at the beginning.') }}
|
| 86 |
+
{%- endif %}
|
| 87 |
+
{%- elif message.role == "user" %}
|
| 88 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 89 |
+
{%- elif message.role == "assistant" %}
|
| 90 |
+
{%- set reasoning_content = '' %}
|
| 91 |
+
{%- if message.reasoning_content is string %}
|
| 92 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 93 |
+
{%- else %}
|
| 94 |
+
{%- if '</think>' in content %}
|
| 95 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 96 |
+
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- endif %}
|
| 99 |
+
{%- set reasoning_content = reasoning_content|trim %}
|
| 100 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 101 |
+
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 102 |
+
{%- else %}
|
| 103 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 104 |
+
{%- endif %}
|
| 105 |
+
{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 106 |
+
{%- for tool_call in message.tool_calls %}
|
| 107 |
+
{%- if tool_call.function is defined %}
|
| 108 |
+
{%- set tool_call = tool_call.function %}
|
| 109 |
+
{%- endif %}
|
| 110 |
+
{%- if loop.first %}
|
| 111 |
+
{%- if content|trim %}
|
| 112 |
+
{{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 113 |
+
{%- else %}
|
| 114 |
+
{{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 115 |
+
{%- endif %}
|
| 116 |
+
{%- else %}
|
| 117 |
+
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 118 |
+
{%- endif %}
|
| 119 |
+
{%- if tool_call.arguments is defined %}
|
| 120 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 121 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 122 |
+
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
|
| 123 |
+
{{- args_value }}
|
| 124 |
+
{{- '\n</parameter>\n' }}
|
| 125 |
+
{%- endfor %}
|
| 126 |
+
{%- endif %}
|
| 127 |
+
{{- '</function>\n</tool_call>' }}
|
| 128 |
+
{%- endfor %}
|
| 129 |
+
{%- endif %}
|
| 130 |
+
{{- '<|im_end|>\n' }}
|
| 131 |
+
{%- elif message.role == "tool" %}
|
| 132 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 133 |
+
{{- '<|im_start|>user' }}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{{- '\n<tool_response>\n' }}
|
| 136 |
+
{{- content }}
|
| 137 |
+
{{- '\n</tool_response>' }}
|
| 138 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 139 |
+
{{- '<|im_end|>\n' }}
|
| 140 |
+
{%- elif loop.last %}
|
| 141 |
+
{{- '<|im_end|>\n' }}
|
| 142 |
+
{%- endif %}
|
| 143 |
+
{%- else %}
|
| 144 |
+
{{- raise_exception('Unexpected message role.') }}
|
| 145 |
+
{%- endif %}
|
| 146 |
+
{%- endfor %}
|
| 147 |
+
{%- if add_generation_prompt %}
|
| 148 |
+
{{- '<|im_start|>assistant\n' }}
|
| 149 |
+
{%- if enable_thinking is defined and enable_thinking is false %}
|
| 150 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 151 |
+
{%- else %}
|
| 152 |
+
{{- '<think>\n' }}
|
| 153 |
+
{%- endif %}
|
| 154 |
+
{%- endif %}
|
lev_release.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "lev",
|
| 3 |
+
"preset": "4b-instruct",
|
| 4 |
+
"step": 18750,
|
| 5 |
+
"base_model": "ggml-org-dev/tiny-Qwen3.5-4B-for-testing",
|
| 6 |
+
"lora_rank": 32,
|
| 7 |
+
"mode_b_head": true,
|
| 8 |
+
"calibrated": true,
|
| 9 |
+
"noul_readout": "rating",
|
| 10 |
+
"prompt_style": "chat",
|
| 11 |
+
"files": [
|
| 12 |
+
"adapter_config.json",
|
| 13 |
+
"adapter_model.safetensors",
|
| 14 |
+
"chat_template.jinja",
|
| 15 |
+
"mode_b_head.pt",
|
| 16 |
+
"tokenizer.json",
|
| 17 |
+
"tokenizer_config.json",
|
| 18 |
+
"calibration.json"
|
| 19 |
+
],
|
| 20 |
+
"metrics": {},
|
| 21 |
+
"created": "2026-09-24T06:29:31+00:00",
|
| 22 |
+
"adapter_scale": 0.25
|
| 23 |
+
}
|
mode_b_head.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:66674d5e834842c28766dfde4c570502bef306cc95bb5bb25436db6e806439f9
|
| 3 |
+
size 4343749
|
slice_qwen35_lora.py
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Build tiny slices of a Qwen3.5 base model and of a LoRA decision adapter trained on it, for testing. Output is gibberish, only the shapes and code paths are real.
|
| 4 |
+
|
| 5 |
+
Adapted from the slice-model skill of llama.cpp (skills/slice-model): tensors are fetched with HTTP range requests (only the needed rows) and config.json is patched to match, then convert_hf_to_gguf.py does the conversion. The stock script only drops layers, which cannot go below the 248320 x hidden token embedding, so this one also narrows the hidden sizes.
|
| 6 |
+
|
| 7 |
+
The adapter is narrowed with the same indices as the base: LoRA A keeps the input columns of its base weight, LoRA B its output rows (the rank is kept). Only the LoRA tensors of the kept layers are kept.
|
| 8 |
+
|
| 9 |
+
Widths follow the tinyopenjev Qwen3.5 slice: 4 layers (3 linear attention + 1 full attention), hidden 32, FFN 128, 2 heads / 1 KV head (head_dim 256 kept), linear attention 2 key heads / 6 value heads (head dims 128 kept), the MTP layer is kept (narrowed), vision encoder 2 blocks, 2 heads (head_dim kept), out hidden 32 (the text GGUF budget is 10 MB: a 248320 x 32 matrix is 4.5 MB in Q4_0).
|
| 10 |
+
|
| 11 |
+
usage:
|
| 12 |
+
slice_qwen35_lora.py base <base repo> <revision|main> <output dir>
|
| 13 |
+
slice_qwen35_lora.py adapter <adapter repo or local dir> <base repo> <output dir>
|
| 14 |
+
(only writes the narrowed adapter_model.safetensors and the narrowed head .pt files, the json files are patched by hand)
|
| 15 |
+
"""
|
| 16 |
+
import json, struct, sys
|
| 17 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
|
| 20 |
+
import requests
|
| 21 |
+
import torch
|
| 22 |
+
from huggingface_hub import hf_hub_download, hf_hub_url
|
| 23 |
+
from safetensors import safe_open
|
| 24 |
+
from safetensors.torch import save_file
|
| 25 |
+
|
| 26 |
+
E, FF, NH, NKV = 32, 128, 2, 1 # text: hidden, ffn, heads, kv heads. head_dim stays 256
|
| 27 |
+
LNK, LNV = 2, 6 # linear attention key / value heads, head dims stay 128
|
| 28 |
+
KEEP_LAYERS = [0, 1, 2, 3] # linear, linear, linear, full
|
| 29 |
+
VNH = 2 # vision heads, head_dim kept, vision ffn = 2 * hidden
|
| 30 |
+
VKEEP = [0, 1] # vision blocks
|
| 31 |
+
CHUNK = 32 << 20
|
| 32 |
+
|
| 33 |
+
DTYPES = {'BF16': torch.bfloat16, 'F32': torch.float32, 'F16': torch.float16}
|
| 34 |
+
SIZES = {'BF16': 2, 'F32': 4, 'F16': 2}
|
| 35 |
+
sess = requests.Session()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def rng(url, a, b):
|
| 39 |
+
for attempt in range(5):
|
| 40 |
+
try:
|
| 41 |
+
r = sess.get(url, headers={'Range': f'bytes={a}-{b}'}, timeout=600)
|
| 42 |
+
r.raise_for_status()
|
| 43 |
+
assert len(r.content) == b - a + 1
|
| 44 |
+
return r.content
|
| 45 |
+
except Exception:
|
| 46 |
+
if attempt == 4:
|
| 47 |
+
raise
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def read_header(url):
|
| 51 |
+
n = struct.unpack('<Q', rng(url, 0, 7))[0]
|
| 52 |
+
h = json.loads(rng(url, 8, 8 + n - 1))
|
| 53 |
+
h.pop('__metadata__', None)
|
| 54 |
+
return h, 8 + n
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def fetch_rows(pool, url, base, meta, rows):
|
| 58 |
+
"""fetch the given rows (dim 0) of a tensor, None = all rows"""
|
| 59 |
+
shape, dt = meta['shape'], meta['dtype']
|
| 60 |
+
a0 = base + meta['data_offsets'][0]
|
| 61 |
+
row_bytes = SIZES[dt]
|
| 62 |
+
for s in shape[1:]:
|
| 63 |
+
row_bytes *= s
|
| 64 |
+
rows = list(range(shape[0])) if rows is None else [int(r) for r in rows]
|
| 65 |
+
runs = [] # contiguous runs of rows
|
| 66 |
+
for r in rows:
|
| 67 |
+
if runs and runs[-1][1] == r:
|
| 68 |
+
runs[-1][1] = r + 1
|
| 69 |
+
else:
|
| 70 |
+
runs.append([r, r + 1])
|
| 71 |
+
jobs = [] # byte ranges, split in chunks
|
| 72 |
+
for s, e in runs:
|
| 73 |
+
a, b = a0 + s * row_bytes, a0 + e * row_bytes
|
| 74 |
+
while a < b:
|
| 75 |
+
jobs.append((a, min(a + CHUNK, b) - 1))
|
| 76 |
+
a += CHUNK
|
| 77 |
+
data = b''.join(pool.map(lambda j: rng(url, *j), jobs))
|
| 78 |
+
return torch.frombuffer(bytearray(data), dtype=DTYPES[dt]).reshape([len(rows)] + shape[1:])
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def ar(n, start=0):
|
| 82 |
+
return torch.arange(start, start + n)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def text_indices(tc):
|
| 86 |
+
"""(rows, cols) index of each narrowed text weight, by the module name"""
|
| 87 |
+
hd, dk, dv = tc['head_dim'], tc['linear_key_head_dim'], tc['linear_value_head_dim']
|
| 88 |
+
nk = tc['linear_num_key_heads']
|
| 89 |
+
H, I = tc['hidden_size'], tc['intermediate_size']
|
| 90 |
+
hid, ff = ar(E), ar(FF)
|
| 91 |
+
q_gated = ar(NH * hd * 2) if tc.get('attn_output_gate') else ar(NH * hd) # [q, gate] per head
|
| 92 |
+
lin_qkv = torch.cat([ar(LNK * dk), ar(LNK * dk, nk * dk), ar(LNV * dv, 2 * nk * dk)])
|
| 93 |
+
idx2 = {
|
| 94 |
+
'self_attn.q_proj': (q_gated, hid), 'self_attn.k_proj': (ar(NKV * hd), hid), 'self_attn.v_proj': (ar(NKV * hd), hid),
|
| 95 |
+
'self_attn.o_proj': (hid, ar(NH * hd)),
|
| 96 |
+
'mlp.gate_proj': (ff, hid), 'mlp.up_proj': (ff, hid), 'mlp.down_proj': (hid, ff),
|
| 97 |
+
'linear_attn.in_proj_qkv': (lin_qkv, hid), 'linear_attn.in_proj_z': (ar(LNV * dv), hid),
|
| 98 |
+
'linear_attn.in_proj_a': (ar(LNV), hid), 'linear_attn.in_proj_b': (ar(LNV), hid),
|
| 99 |
+
'linear_attn.out_proj': (hid, ar(LNV * dv)),
|
| 100 |
+
}
|
| 101 |
+
idx1 = {
|
| 102 |
+
'input_layernorm.weight': hid, 'post_attention_layernorm.weight': hid,
|
| 103 |
+
'linear_attn.A_log': ar(LNV), 'linear_attn.dt_bias': ar(LNV), 'linear_attn.conv1d.weight': lin_qkv,
|
| 104 |
+
'linear_attn.norm.weight': None, 'self_attn.q_norm.weight': None, 'self_attn.k_norm.weight': None,
|
| 105 |
+
}
|
| 106 |
+
glob = {
|
| 107 |
+
'model.language_model.embed_tokens.weight': (None, hid), 'lm_head.weight': (None, hid),
|
| 108 |
+
'model.language_model.norm.weight': (hid, None),
|
| 109 |
+
'mtp.fc.weight': (hid, torch.cat([hid, ar(E, H)])), # input is [embedding, hidden]
|
| 110 |
+
'mtp.norm.weight': (hid, None), 'mtp.pre_fc_norm_embedding.weight': (hid, None), 'mtp.pre_fc_norm_hidden.weight': (hid, None),
|
| 111 |
+
}
|
| 112 |
+
del I
|
| 113 |
+
return idx2, idx1, glob
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def vision_indices(vc):
|
| 117 |
+
Vo = vc['hidden_size']
|
| 118 |
+
V = VNH * (Vo // vc['num_heads'])
|
| 119 |
+
VFF = 2 * V
|
| 120 |
+
m = vc['spatial_merge_size'] ** 2
|
| 121 |
+
v = ar(V)
|
| 122 |
+
qkv = torch.cat([ar(V, i * Vo) for i in range(3)])
|
| 123 |
+
blk = {
|
| 124 |
+
'attn.qkv.weight': (qkv, v), 'attn.qkv.bias': (qkv, None), 'attn.proj.weight': (v, v), 'attn.proj.bias': (v, None),
|
| 125 |
+
'mlp.linear_fc1.weight': (ar(VFF), v), 'mlp.linear_fc1.bias': (ar(VFF), None),
|
| 126 |
+
'mlp.linear_fc2.weight': (v, ar(VFF)), 'mlp.linear_fc2.bias': (v, None),
|
| 127 |
+
'norm1.weight': (v, None), 'norm1.bias': (v, None), 'norm2.weight': (v, None), 'norm2.bias': (v, None),
|
| 128 |
+
}
|
| 129 |
+
glob = {
|
| 130 |
+
'model.visual.patch_embed.proj.weight': (v, None), 'model.visual.patch_embed.proj.bias': (v, None),
|
| 131 |
+
'model.visual.pos_embed.weight': (None, v),
|
| 132 |
+
'model.visual.merger.norm.weight': (v, None), 'model.visual.merger.norm.bias': (v, None),
|
| 133 |
+
'model.visual.merger.linear_fc1.weight': (ar(m * V), torch.cat([ar(V, i * Vo) for i in range(m)])), # m patches are merged
|
| 134 |
+
'model.visual.merger.linear_fc1.bias': (ar(m * V), None),
|
| 135 |
+
'model.visual.merger.linear_fc2.weight': (ar(E), ar(m * V)), 'model.visual.merger.linear_fc2.bias': (ar(E), None),
|
| 136 |
+
}
|
| 137 |
+
return blk, glob, V, VFF
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def plan(name, cfg):
|
| 141 |
+
"""new name and (rows, cols) for a base tensor, None to drop it"""
|
| 142 |
+
tc, vc = cfg['text_config'], cfg['vision_config']
|
| 143 |
+
idx2, idx1, tglob = text_indices(tc)
|
| 144 |
+
vblk, vglob, _, _ = vision_indices(vc)
|
| 145 |
+
if name in tglob:
|
| 146 |
+
return name, tglob[name]
|
| 147 |
+
if name in vglob:
|
| 148 |
+
return name, vglob[name]
|
| 149 |
+
for prefix, keep in (('model.language_model.layers.', KEEP_LAYERS), ('mtp.layers.', None)):
|
| 150 |
+
if name.startswith(prefix):
|
| 151 |
+
i, key = name[len(prefix):].split('.', 1)
|
| 152 |
+
if keep is not None and int(i) not in keep:
|
| 153 |
+
return None
|
| 154 |
+
new = prefix + str(keep.index(int(i)) if keep is not None else i) + '.' + key
|
| 155 |
+
if key in idx1:
|
| 156 |
+
return new, (idx1[key], None)
|
| 157 |
+
mod = key.removesuffix('.weight')
|
| 158 |
+
if mod in idx2:
|
| 159 |
+
return new, idx2[mod]
|
| 160 |
+
raise SystemExit(f'unhandled tensor {name}')
|
| 161 |
+
if name.startswith('model.visual.blocks.'):
|
| 162 |
+
i, key = name[len('model.visual.blocks.'):].split('.', 1)
|
| 163 |
+
if int(i) not in VKEEP:
|
| 164 |
+
return None
|
| 165 |
+
return f'model.visual.blocks.{VKEEP.index(int(i))}.{key}', vblk[key]
|
| 166 |
+
raise SystemExit(f'unhandled tensor {name}')
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def patch_config(c):
|
| 170 |
+
tc, vc = c['text_config'], c['vision_config']
|
| 171 |
+
_, _, V, VFF = vision_indices(vc)
|
| 172 |
+
tc.update(hidden_size=E, intermediate_size=FF, num_attention_heads=NH, num_key_value_heads=NKV,
|
| 173 |
+
linear_num_key_heads=LNK, linear_num_value_heads=LNV,
|
| 174 |
+
num_hidden_layers=len(KEEP_LAYERS), layer_types=[tc['layer_types'][i] for i in KEEP_LAYERS])
|
| 175 |
+
vc.update(depth=len(VKEEP), hidden_size=V, intermediate_size=VFF, num_heads=VNH, out_hidden_size=E)
|
| 176 |
+
return c
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def slice_base(repo, rev, out):
|
| 180 |
+
rev = None if rev == 'main' else rev
|
| 181 |
+
cfg = json.load(open(hf_hub_download(repo, 'config.json', revision=rev)))
|
| 182 |
+
index = json.load(open(hf_hub_download(repo, 'model.safetensors.index.json', revision=rev)))['weight_map']
|
| 183 |
+
tensors = {}
|
| 184 |
+
with ThreadPoolExecutor(8) as pool:
|
| 185 |
+
for shard in sorted(set(index.values())):
|
| 186 |
+
url = hf_hub_url(repo, shard, revision=rev)
|
| 187 |
+
header, base = read_header(url)
|
| 188 |
+
for name, meta in header.items():
|
| 189 |
+
p = plan(name, cfg)
|
| 190 |
+
if p is None:
|
| 191 |
+
continue
|
| 192 |
+
new, (rows, cols) = p
|
| 193 |
+
t = fetch_rows(pool, url, base, meta, rows)
|
| 194 |
+
if cols is not None:
|
| 195 |
+
t = t[:, cols]
|
| 196 |
+
tensors[new] = t.contiguous().clone()
|
| 197 |
+
print(f'{name} {meta["shape"]} -> {new} {list(t.shape)}', flush=True)
|
| 198 |
+
out.mkdir(parents=True, exist_ok=True)
|
| 199 |
+
save_file(dict(sorted(tensors.items())), out / 'model.safetensors', metadata={'format': 'pt'})
|
| 200 |
+
(out / 'config.json').write_text(json.dumps(patch_config(cfg), indent=4) + '\n')
|
| 201 |
+
print(f'{len(tensors)} tensors, {(out / "model.safetensors").stat().st_size / 1e6:.1f} MB')
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def slice_adapter(src, base_repo, out):
|
| 205 |
+
cfg = json.load(open(hf_hub_download(base_repo, 'config.json'))) # revisions of a base share the config
|
| 206 |
+
idx2, _, _ = text_indices(cfg['text_config'])
|
| 207 |
+
local = Path(src)
|
| 208 |
+
if local.is_dir():
|
| 209 |
+
f = safe_open(local / 'adapter_model.safetensors', 'pt')
|
| 210 |
+
names, get = list(f.keys()), f.get_tensor
|
| 211 |
+
else:
|
| 212 |
+
url = hf_hub_url(src, 'adapter_model.safetensors')
|
| 213 |
+
header, base = read_header(url)
|
| 214 |
+
pool = ThreadPoolExecutor(8)
|
| 215 |
+
names, get = list(header), lambda n: fetch_rows(pool, url, base, header[n], None)
|
| 216 |
+
tensors = {}
|
| 217 |
+
for name in names:
|
| 218 |
+
pre, rest = name.split('layers.', 1)
|
| 219 |
+
i, key = rest.split('.', 1)
|
| 220 |
+
if int(i) not in KEEP_LAYERS:
|
| 221 |
+
continue
|
| 222 |
+
mod, part = key.split('.lora_')
|
| 223 |
+
rows, cols = idx2[mod]
|
| 224 |
+
t = get(name)
|
| 225 |
+
t = t[:, cols] if part == 'A.weight' else t[rows]
|
| 226 |
+
tensors[f'{pre}layers.{KEEP_LAYERS.index(int(i))}.{key}'] = t.contiguous().clone()
|
| 227 |
+
print(f'{name} -> {list(t.shape)}', flush=True)
|
| 228 |
+
out.mkdir(parents=True, exist_ok=True)
|
| 229 |
+
save_file(dict(sorted(tensors.items())), out / 'adapter_model.safetensors', metadata={'format': 'pt'})
|
| 230 |
+
print(f'{len(tensors)} of {len(names)} tensors, {(out / "adapter_model.safetensors").stat().st_size / 1e6:.1f} MB')
|
| 231 |
+
|
| 232 |
+
# extra heads, they read the hidden state: keep the first E input columns
|
| 233 |
+
for fname, keys in (('head.pt', ['q.weight', 'k.weight']), ('mode_b_head.pt', ['question_proj.weight', 'candidate_proj.weight'])):
|
| 234 |
+
try:
|
| 235 |
+
p = local / fname if local.is_dir() else Path(hf_hub_download(src, fname))
|
| 236 |
+
except Exception:
|
| 237 |
+
continue
|
| 238 |
+
if not p.is_file():
|
| 239 |
+
continue
|
| 240 |
+
h = torch.load(p, map_location='cpu', weights_only=True)
|
| 241 |
+
d = h['head'] if 'head' in h else h
|
| 242 |
+
for k in keys:
|
| 243 |
+
d[k] = d[k][:, :E].contiguous().clone()
|
| 244 |
+
torch.save(h, out / fname)
|
| 245 |
+
print(f'{fname}: ' + ', '.join(f'{k} {list(d[k].shape)}' for k in keys))
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
if __name__ == '__main__':
|
| 249 |
+
if sys.argv[1] == 'base':
|
| 250 |
+
slice_base(sys.argv[2], sys.argv[3], Path(sys.argv[4]))
|
| 251 |
+
elif sys.argv[1] == 'adapter':
|
| 252 |
+
slice_adapter(sys.argv[2], sys.argv[3], Path(sys.argv[4]))
|
| 253 |
+
else:
|
| 254 |
+
raise SystemExit(__doc__)
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:06b9509352d2af50381ab2247e083b80d32d5c0aba91c272ca9ff729b6a0e523
|
| 3 |
+
size 19989325
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"audio_bos_token": "<|audio_start|>",
|
| 4 |
+
"audio_eos_token": "<|audio_end|>",
|
| 5 |
+
"audio_token": "<|audio_pad|>",
|
| 6 |
+
"backend": "tokenizers",
|
| 7 |
+
"bos_token": null,
|
| 8 |
+
"clean_up_tokenization_spaces": false,
|
| 9 |
+
"eos_token": "<|im_end|>",
|
| 10 |
+
"errors": "replace",
|
| 11 |
+
"image_token": "<|image_pad|>",
|
| 12 |
+
"is_local": false,
|
| 13 |
+
"local_files_only": false,
|
| 14 |
+
"model_max_length": 262144,
|
| 15 |
+
"model_specific_special_tokens": {
|
| 16 |
+
"audio_bos_token": "<|audio_start|>",
|
| 17 |
+
"audio_eos_token": "<|audio_end|>",
|
| 18 |
+
"audio_token": "<|audio_pad|>",
|
| 19 |
+
"image_token": "<|image_pad|>",
|
| 20 |
+
"video_token": "<|video_pad|>",
|
| 21 |
+
"vision_bos_token": "<|vision_start|>",
|
| 22 |
+
"vision_eos_token": "<|vision_end|>"
|
| 23 |
+
},
|
| 24 |
+
"pad_token": "<|endoftext|>",
|
| 25 |
+
"pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
|
| 26 |
+
"split_special_tokens": false,
|
| 27 |
+
"tokenizer_class": "Qwen2Tokenizer",
|
| 28 |
+
"unk_token": null,
|
| 29 |
+
"video_token": "<|video_pad|>",
|
| 30 |
+
"vision_bos_token": "<|vision_start|>",
|
| 31 |
+
"vision_eos_token": "<|vision_end|>"
|
| 32 |
+
}
|