Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- .gitattributes +13 -0
- LICENSE +41 -0
- README.md +191 -0
- chat_template.jinja +115 -0
- config.json +73 -0
- generation_config.json +7 -0
- onnx/.DS_Store +0 -0
- onnx/model_q4f16.onnx +3 -0
- onnx/model_q4f16.onnx_data +3 -0
- onnx/model_q4f16.onnx_data_1 +3 -0
- onnx/model_q4f16.onnx_data_10 +3 -0
- onnx/model_q4f16.onnx_data_11 +3 -0
- onnx/model_q4f16.onnx_data_2 +3 -0
- onnx/model_q4f16.onnx_data_3 +3 -0
- onnx/model_q4f16.onnx_data_4 +3 -0
- onnx/model_q4f16.onnx_data_5 +3 -0
- onnx/model_q4f16.onnx_data_6 +3 -0
- onnx/model_q4f16.onnx_data_7 +3 -0
- onnx/model_q4f16.onnx_data_8 +3 -0
- onnx/model_q4f16.onnx_data_9 +3 -0
- tokenizer.json +3 -0
- tokenizer_config.json +12 -0
.DS_Store
ADDED
|
Binary file (8.2 kB). View file
|
|
|
.gitattributes
CHANGED
|
@@ -33,3 +33,16 @@ 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 |
+
onnx/model_q4f16.onnx_data filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
onnx/model_q4f16.onnx_data_1 filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
onnx/model_q4f16.onnx_data_10 filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
onnx/model_q4f16.onnx_data_11 filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
onnx/model_q4f16.onnx_data_2 filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
onnx/model_q4f16.onnx_data_3 filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
onnx/model_q4f16.onnx_data_4 filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
onnx/model_q4f16.onnx_data_5 filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
onnx/model_q4f16.onnx_data_6 filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
onnx/model_q4f16.onnx_data_7 filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
onnx/model_q4f16.onnx_data_8 filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
onnx/model_q4f16.onnx_data_9 filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
LICENSE TEXT
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
LFM Open License v1.0
|
| 7 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 8 |
+
1. Definitions.
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by this document.
|
| 10 |
+
"Licensor" shall mean Liquid AI, Inc.
|
| 11 |
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
| 12 |
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
| 13 |
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
| 14 |
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
| 15 |
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work.
|
| 16 |
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
| 17 |
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
| 18 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
| 19 |
+
"Commercial Use" shall mean any use of the Work for direct or indirect commercial advantage or monetary compensation.
|
| 20 |
+
"Qualified Non-Profit Organization" shall mean a Legal Entity that is organized and operated exclusively for religious, charitable, scientific, testing for public safety, literary, or educational purposes, and which is exempt from federal income tax under Section 501(c)(3) of the United States Internal Revenue Code of 1986, as amended, or any equivalent non-profit or charitable organization in a foreign jurisdiction.
|
| 21 |
+
"Non-Commercial or Research Purposes" shall mean purposes that do not involve any use of the Work or a Derivative Work for Commercial Use.
|
| 22 |
+
"Threshold" shall mean annual revenue of 10 million United States dollars ($10,000,000) or more.
|
| 23 |
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, including the Commercial Use limitation set forth in Section 5, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
| 24 |
+
3. Grant of Patent License. Subject to the terms and conditions of this License, including the Commercial Use limitation set forth in Section 5, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
| 25 |
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
| 26 |
+
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
| 27 |
+
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
| 28 |
+
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
| 29 |
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
| 30 |
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
| 31 |
+
5. Commercial Use Limitation.
|
| 32 |
+
(a) The rights granted under this License for Commercial Use are conditioned upon You or Your Legal Entity not exceeding the Threshold.
|
| 33 |
+
(b) Any Commercial Use of the Work or a Derivative Work by a Legal Entity that exceeds the Threshold is not licensed under this Agreement.
|
| 34 |
+
(c) The Threshold shall not apply to a Qualified Non-Profit Organization's use of the Work or a Derivative Work for Non-Commercial or Research Purposes.
|
| 35 |
+
6. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
| 36 |
+
7. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except for the reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
| 37 |
+
8. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
| 38 |
+
9. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
| 39 |
+
10. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
| 40 |
+
11. Termination. This License will terminate automatically and immediately if You fail to comply with any of its terms and conditions. Upon termination, You must cease all use of the Work and any Derivative Works and delete all copies in Your possession.
|
| 41 |
+
END OF TERMS AND CONDITIONS
|
README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: lfm1.0
|
| 4 |
+
license_link: LICENSE
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
+
pipeline_tag: text-generation
|
| 8 |
+
tags:
|
| 9 |
+
- liquid
|
| 10 |
+
- edge
|
| 11 |
+
- lfm2
|
| 12 |
+
- lfm2.5
|
| 13 |
+
- moe
|
| 14 |
+
- mixture-of-experts
|
| 15 |
+
- onnx
|
| 16 |
+
- onnxruntime
|
| 17 |
+
base_model:
|
| 18 |
+
- LiquidAI/LFM2.5-8B-A1B
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
<div align="center">
|
| 22 |
+
<img
|
| 23 |
+
src="https://cdn-uploads.huggingface.co/production/uploads/61b8e2ba285851687028d395/2b08LKpev0DNEk6DlnWkY.png"
|
| 24 |
+
alt="Liquid AI"
|
| 25 |
+
style="width: 100%; max-width: 100%; height: auto; display: inline-block; margin-bottom: 0.5em; margin-top: 0.5em;"
|
| 26 |
+
/>
|
| 27 |
+
<div style="display: flex; justify-content: center; gap: 0.5em; margin-bottom: 1em;">
|
| 28 |
+
<a href="https://playground.liquid.ai/"><strong>Try LFM</strong></a> •
|
| 29 |
+
<a href="https://docs.liquid.ai/lfm/getting-started/welcome"><strong>Docs</strong></a> •
|
| 30 |
+
<a href="https://leap.liquid.ai/"><strong>LEAP</strong></a> •
|
| 31 |
+
<a href="https://discord.com/invite/liquid-ai"><strong>Discord</strong></a>
|
| 32 |
+
</div>
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
# LFM2.5-8B-A1B-ONNX
|
| 36 |
+
|
| 37 |
+
ONNX export of [LFM2.5-8B-A1B](https://huggingface.co/LiquidAI/LFM2.5-8B-A1B) for cross-platform inference.
|
| 38 |
+
|
| 39 |
+
LFM2.5-8B-A1B is a Mixture of Experts model with 8B total parameters and about 1B active parameters per token. It uses 32 experts with 4 experts activated per token, combining the efficiency of sparse models with the quality of larger dense models.
|
| 40 |
+
|
| 41 |
+
## Recommended Variants
|
| 42 |
+
|
| 43 |
+
| Precision | Size | Use Case |
|
| 44 |
+
|-----------|------|----------|
|
| 45 |
+
| Q4F16 | ~4.7GB | Recommended (Q4 MoE + FP16 dense) |
|
| 46 |
+
| FP16 | ~15.8GB | Higher quality |
|
| 47 |
+
| Q4 | ~5.2GB | Smallest size |
|
| 48 |
+
| Q8 | ~30.4GB | Highest-fidelity quantized variant |
|
| 49 |
+
|
| 50 |
+
Note: This model is too large for WebGPU browser inference.
|
| 51 |
+
|
| 52 |
+
## Validation
|
| 53 |
+
|
| 54 |
+
This export was validated against the local PyTorch reference for `LiquidAI/LFM2.5-8B-A1B`.
|
| 55 |
+
|
| 56 |
+
- FP32 padded-batch parity passed for both left and right padding, with cosine similarity `1.0000` and top-5 overlap `5/5` at the last valid token for each row.
|
| 57 |
+
- Q4 decoder and coherence checks passed the repository thresholds. Average coherence similarity: `0.7144`.
|
| 58 |
+
- Q4F16 was runtime-validated on `CPUExecutionProvider` and matched the same decoder/coherence thresholds as Q4. Average coherence similarity: `0.7145`.
|
| 59 |
+
- Q8 decoder and coherence checks passed, and stayed very close to the PyTorch reference. Average coherence similarity: `0.9975`.
|
| 60 |
+
|
| 61 |
+
## Model Files
|
| 62 |
+
|
| 63 |
+
```
|
| 64 |
+
onnx/
|
| 65 |
+
├── model.onnx # FP32 model graph
|
| 66 |
+
├── model.onnx_data* # FP32 weights
|
| 67 |
+
├── model_fp16.onnx # FP16 model graph
|
| 68 |
+
├── model_fp16.onnx_data* # FP16 weights
|
| 69 |
+
├── model_q4.onnx # Q4 model graph
|
| 70 |
+
├── model_q4.onnx_data* # Q4 weights
|
| 71 |
+
├── model_q4f16.onnx # Q4 MoE experts + FP16 dense (recommended)
|
| 72 |
+
├── model_q4f16.onnx_data* # Q4F16 weights
|
| 73 |
+
├── model_q8.onnx # Q8 model graph
|
| 74 |
+
└── model_q8.onnx_data* # Q8 weights
|
| 75 |
+
|
| 76 |
+
* Large models split weights across multiple files:
|
| 77 |
+
model.onnx_data, model.onnx_data_1, model.onnx_data_2, etc.
|
| 78 |
+
All data files must be in the same directory as the .onnx file.
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
## Python
|
| 82 |
+
|
| 83 |
+
### Installation
|
| 84 |
+
|
| 85 |
+
```bash
|
| 86 |
+
pip install onnxruntime transformers numpy huggingface_hub
|
| 87 |
+
# or with GPU support:
|
| 88 |
+
pip install onnxruntime-gpu transformers numpy huggingface_hub
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### Inference
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
from huggingface_hub import snapshot_download
|
| 95 |
+
from transformers import AutoConfig, AutoTokenizer
|
| 96 |
+
import numpy as np
|
| 97 |
+
import onnxruntime
|
| 98 |
+
|
| 99 |
+
# 1. Load config, tokenizer, and model
|
| 100 |
+
model_id = "LiquidAI/LFM2.5-8B-A1B-ONNX"
|
| 101 |
+
config = AutoConfig.from_pretrained(model_id)
|
| 102 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 103 |
+
eos_token_id = config.eos_token_id
|
| 104 |
+
|
| 105 |
+
filename = "model_q4f16.onnx" # Options: model.onnx, model_fp16.onnx, model_q4.onnx, model_q4f16.onnx, model_q8.onnx
|
| 106 |
+
model_path = snapshot_download(repo_id=model_id, allow_patterns=f"onnx/{filename}*")
|
| 107 |
+
session = onnxruntime.InferenceSession(f"{model_path}/onnx/{filename}")
|
| 108 |
+
input_names = {inp.name for inp in session.get_inputs()}
|
| 109 |
+
|
| 110 |
+
# 2. Prepare inputs
|
| 111 |
+
prompt = "What is C. elegans?"
|
| 112 |
+
messages = [{"role": "user", "content": prompt}]
|
| 113 |
+
inputs = tokenizer.apply_chat_template(
|
| 114 |
+
messages,
|
| 115 |
+
add_generation_prompt=True,
|
| 116 |
+
tokenize=True,
|
| 117 |
+
return_dict=True,
|
| 118 |
+
return_tensors="np",
|
| 119 |
+
)
|
| 120 |
+
input_ids = inputs["input_ids"]
|
| 121 |
+
attention_mask = inputs["attention_mask"]
|
| 122 |
+
batch_size = input_ids.shape[0]
|
| 123 |
+
|
| 124 |
+
past_cache_values = {}
|
| 125 |
+
for inp in session.get_inputs():
|
| 126 |
+
name = inp.name
|
| 127 |
+
shape = inp.shape
|
| 128 |
+
dtype = np.float32 if inp.type == "tensor(float)" else np.float16
|
| 129 |
+
if name.startswith("past_key_values"):
|
| 130 |
+
past_cache_values[name] = np.zeros([batch_size, shape[1], 0, shape[3]], dtype=dtype)
|
| 131 |
+
elif name.startswith("past_conv"):
|
| 132 |
+
past_cache_values[name] = np.zeros([batch_size, shape[1], shape[2]], dtype=dtype)
|
| 133 |
+
|
| 134 |
+
position_ids = np.arange(input_ids.shape[1], dtype=np.int64).reshape(1, -1)
|
| 135 |
+
|
| 136 |
+
# 3. Generation loop
|
| 137 |
+
max_new_tokens = 256
|
| 138 |
+
generated_tokens = np.array([[]], dtype=np.int64)
|
| 139 |
+
cur_len = input_ids.shape[1]
|
| 140 |
+
for i in range(max_new_tokens):
|
| 141 |
+
if i == 0:
|
| 142 |
+
ids = input_ids
|
| 143 |
+
pos = position_ids
|
| 144 |
+
else:
|
| 145 |
+
ids = generated_tokens[:, -1:]
|
| 146 |
+
pos = np.array([[cur_len - 1]], dtype=np.int64)
|
| 147 |
+
|
| 148 |
+
feed = {
|
| 149 |
+
"input_ids": ids,
|
| 150 |
+
"attention_mask": attention_mask,
|
| 151 |
+
**past_cache_values,
|
| 152 |
+
}
|
| 153 |
+
if "position_ids" in input_names:
|
| 154 |
+
feed["position_ids"] = pos
|
| 155 |
+
|
| 156 |
+
outputs = session.run(None, feed)
|
| 157 |
+
logits = outputs[0]
|
| 158 |
+
next_token = logits[:, -1].argmax(-1, keepdims=True)
|
| 159 |
+
|
| 160 |
+
generated_tokens = (
|
| 161 |
+
next_token if generated_tokens.shape[1] == 0
|
| 162 |
+
else np.concatenate([generated_tokens, next_token], axis=-1)
|
| 163 |
+
)
|
| 164 |
+
attention_mask = np.concatenate(
|
| 165 |
+
[attention_mask, np.ones_like(next_token, dtype=np.int64)],
|
| 166 |
+
axis=-1,
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
output_names = [out.name for out in session.get_outputs()]
|
| 170 |
+
cache_outputs = {
|
| 171 |
+
name: value
|
| 172 |
+
for name, value in zip(output_names[1:], outputs[1:])
|
| 173 |
+
}
|
| 174 |
+
for key in past_cache_values:
|
| 175 |
+
present_key = key.replace("past_key_values", "present").replace("past_conv", "present_conv")
|
| 176 |
+
past_cache_values[key] = cache_outputs[present_key]
|
| 177 |
+
|
| 178 |
+
cur_len += 1
|
| 179 |
+
if np.isin(next_token, eos_token_id).any():
|
| 180 |
+
break
|
| 181 |
+
|
| 182 |
+
print(tokenizer.decode(next_token[0]), end="", flush=True)
|
| 183 |
+
print()
|
| 184 |
+
|
| 185 |
+
# 4. Output result
|
| 186 |
+
print(tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0])
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
## License
|
| 190 |
+
|
| 191 |
+
This model is released under the [LFM 1.0 License](LICENSE).
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{{- bos_token -}}
|
| 2 |
+
{%- set preserve_thinking = preserve_thinking | default(false) -%}
|
| 3 |
+
|
| 4 |
+
{%- macro format_arg_value(arg_value) -%}
|
| 5 |
+
{%- if arg_value is string -%}
|
| 6 |
+
{{- "'" + arg_value + "'" -}}
|
| 7 |
+
{%- elif arg_value is mapping -%}
|
| 8 |
+
{{- arg_value | tojson -}}
|
| 9 |
+
{%- else -%}
|
| 10 |
+
{{- arg_value | string -}}
|
| 11 |
+
{%- endif -%}
|
| 12 |
+
{%- endmacro -%}
|
| 13 |
+
|
| 14 |
+
{%- macro parse_content(content) -%}
|
| 15 |
+
{%- if content is string -%}
|
| 16 |
+
{{- content -}}
|
| 17 |
+
{%- else -%}
|
| 18 |
+
{%- set _ns = namespace(result="") -%}
|
| 19 |
+
{%- for item in content -%}
|
| 20 |
+
{%- if item["type"] == "image" -%}
|
| 21 |
+
{%- set _ns.result = _ns.result + "<image>" -%}
|
| 22 |
+
{%- elif item["type"] == "text" -%}
|
| 23 |
+
{%- set _ns.result = _ns.result + item["text"] -%}
|
| 24 |
+
{%- else -%}
|
| 25 |
+
{%- set _ns.result = _ns.result + item | tojson -%}
|
| 26 |
+
{%- endif -%}
|
| 27 |
+
{%- endfor -%}
|
| 28 |
+
{{- _ns.result -}}
|
| 29 |
+
{%- endif -%}
|
| 30 |
+
{%- endmacro -%}
|
| 31 |
+
|
| 32 |
+
{%- macro render_tool_calls(tool_calls) -%}
|
| 33 |
+
{%- set tool_calls_ns = namespace(tool_calls=[]) -%}
|
| 34 |
+
{%- for tool_call in tool_calls -%}
|
| 35 |
+
{%- set func_name = tool_call["function"]["name"] -%}
|
| 36 |
+
{%- set func_args = tool_call["function"]["arguments"] -%}
|
| 37 |
+
{%- set args_ns = namespace(arg_strings=[]) -%}
|
| 38 |
+
{%- for arg_name, arg_value in func_args.items() -%}
|
| 39 |
+
{%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}
|
| 40 |
+
{%- endfor -%}
|
| 41 |
+
{%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + (args_ns.arg_strings | join(", ")) + ")"] -%}
|
| 42 |
+
{%- endfor -%}
|
| 43 |
+
{{- "<|tool_call_start|>[" + (tool_calls_ns.tool_calls | join(", ")) + "]<|tool_call_end|>" -}}
|
| 44 |
+
{%- endmacro -%}
|
| 45 |
+
|
| 46 |
+
{%- set ns = namespace(system_prompt="", last_user_index=-1) -%}
|
| 47 |
+
{%- if messages[0]["role"] == "system" -%}
|
| 48 |
+
{%- if messages[0].get("content") -%}
|
| 49 |
+
{%- set ns.system_prompt = parse_content(messages[0]["content"]) -%}
|
| 50 |
+
{%- endif -%}
|
| 51 |
+
{%- set messages = messages[1:] -%}
|
| 52 |
+
{%- endif -%}
|
| 53 |
+
{%- if tools -%}
|
| 54 |
+
{%- set ns.system_prompt = ns.system_prompt + ("\n" if ns.system_prompt else "") + "List of tools: [" -%}
|
| 55 |
+
{%- for tool in tools -%}
|
| 56 |
+
{%- if tool is not string -%}
|
| 57 |
+
{%- set tool = tool | tojson -%}
|
| 58 |
+
{%- endif -%}
|
| 59 |
+
{%- set ns.system_prompt = ns.system_prompt + tool -%}
|
| 60 |
+
{%- if not loop.last -%}
|
| 61 |
+
{%- set ns.system_prompt = ns.system_prompt + ", " -%}
|
| 62 |
+
{%- endif -%}
|
| 63 |
+
{%- endfor -%}
|
| 64 |
+
{%- set ns.system_prompt = ns.system_prompt + "]" -%}
|
| 65 |
+
{%- endif -%}
|
| 66 |
+
{%- if ns.system_prompt -%}
|
| 67 |
+
{{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
|
| 68 |
+
{%- endif -%}
|
| 69 |
+
{%- for message in messages -%}
|
| 70 |
+
{%- if message["role"] == "user" -%}
|
| 71 |
+
{%- set ns.last_user_index = loop.index0 -%}
|
| 72 |
+
{%- endif -%}
|
| 73 |
+
{%- endfor -%}
|
| 74 |
+
{%- for message in messages -%}
|
| 75 |
+
{{- "<|im_start|>" + message.role + "\n" -}}
|
| 76 |
+
{%- if message.role == "assistant" -%}
|
| 77 |
+
{%- generation -%}
|
| 78 |
+
{%- if message.thinking is defined and (preserve_thinking or loop.index0 > ns.last_user_index) -%}
|
| 79 |
+
{{- "<think>" + message.thinking + "</think>" -}}
|
| 80 |
+
{%- endif -%}
|
| 81 |
+
{%- set _cfm_tag = "CONTINUE_FINAL_MESSAGE_TAG " -%}
|
| 82 |
+
{%- set _has_cfm = false -%}
|
| 83 |
+
{%- if message.content is defined -%}
|
| 84 |
+
{%- set content = parse_content(message.content) -%}
|
| 85 |
+
{%- if not (preserve_thinking or loop.index0 > ns.last_user_index) -%}
|
| 86 |
+
{%- if "</think>" in content -%}
|
| 87 |
+
{%- set content = content.split("</think>")[-1] | trim -%}
|
| 88 |
+
{%- endif -%}
|
| 89 |
+
{%- endif -%}
|
| 90 |
+
{%- if message.tool_calls is defined and content.endswith(_cfm_tag) -%}
|
| 91 |
+
{%- set _has_cfm = true -%}
|
| 92 |
+
{%- set _trunc_len = (content | length) - (_cfm_tag | length) -%}
|
| 93 |
+
{{- content[:_trunc_len] -}}
|
| 94 |
+
{%- else -%}
|
| 95 |
+
{{- content -}}
|
| 96 |
+
{%- endif -%}
|
| 97 |
+
{%- endif -%}
|
| 98 |
+
{%- if message.tool_calls is defined -%}
|
| 99 |
+
{{- render_tool_calls(message.tool_calls) -}}
|
| 100 |
+
{%- endif -%}
|
| 101 |
+
{%- if _has_cfm -%}
|
| 102 |
+
{{- _cfm_tag -}}
|
| 103 |
+
{%- endif -%}
|
| 104 |
+
{{- "<|im_end|>\n" -}}
|
| 105 |
+
{%- endgeneration -%}
|
| 106 |
+
{%- else %}
|
| 107 |
+
{%- if message.get("content") -%}
|
| 108 |
+
{{- parse_content(message["content"]) -}}
|
| 109 |
+
{%- endif -%}
|
| 110 |
+
{{- "<|im_end|>\n" -}}
|
| 111 |
+
{%- endif %}
|
| 112 |
+
{%- endfor -%}
|
| 113 |
+
{%- if add_generation_prompt -%}
|
| 114 |
+
{{- "<|im_start|>assistant\n" -}}
|
| 115 |
+
{%- endif -%}
|
config.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Lfm2MoeForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"bos_token_id": 124894,
|
| 6 |
+
"conv_L_cache": 3,
|
| 7 |
+
"conv_bias": false,
|
| 8 |
+
"dtype": "bfloat16",
|
| 9 |
+
"eos_token_id": 124900,
|
| 10 |
+
"hidden_size": 2048,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"intermediate_size": 7168,
|
| 13 |
+
"layer_types": [
|
| 14 |
+
"conv",
|
| 15 |
+
"conv",
|
| 16 |
+
"full_attention",
|
| 17 |
+
"conv",
|
| 18 |
+
"conv",
|
| 19 |
+
"conv",
|
| 20 |
+
"full_attention",
|
| 21 |
+
"conv",
|
| 22 |
+
"conv",
|
| 23 |
+
"conv",
|
| 24 |
+
"full_attention",
|
| 25 |
+
"conv",
|
| 26 |
+
"conv",
|
| 27 |
+
"conv",
|
| 28 |
+
"full_attention",
|
| 29 |
+
"conv",
|
| 30 |
+
"conv",
|
| 31 |
+
"conv",
|
| 32 |
+
"full_attention",
|
| 33 |
+
"conv",
|
| 34 |
+
"conv",
|
| 35 |
+
"full_attention",
|
| 36 |
+
"conv",
|
| 37 |
+
"conv"
|
| 38 |
+
],
|
| 39 |
+
"max_position_embeddings": 128000,
|
| 40 |
+
"model_type": "lfm2_moe",
|
| 41 |
+
"moe_intermediate_size": 1792,
|
| 42 |
+
"norm_eps": 1e-05,
|
| 43 |
+
"norm_topk_prob": true,
|
| 44 |
+
"num_attention_heads": 32,
|
| 45 |
+
"num_dense_layers": 2,
|
| 46 |
+
"num_experts": 32,
|
| 47 |
+
"num_experts_per_tok": 4,
|
| 48 |
+
"num_hidden_layers": 24,
|
| 49 |
+
"num_key_value_heads": 8,
|
| 50 |
+
"pad_token_id": 124893,
|
| 51 |
+
"rope_parameters": {
|
| 52 |
+
"rope_theta": 5000000,
|
| 53 |
+
"rope_type": "default"
|
| 54 |
+
},
|
| 55 |
+
"routed_scaling_factor": 1.0,
|
| 56 |
+
"transformers_version": "5.0.0.dev0",
|
| 57 |
+
"use_cache": true,
|
| 58 |
+
"use_expert_bias": true,
|
| 59 |
+
"vocab_size": 128000,
|
| 60 |
+
"transformers.js_config": {
|
| 61 |
+
"use_external_data_format": {
|
| 62 |
+
"model.onnx": 23,
|
| 63 |
+
"model_fp16.onnx": 10,
|
| 64 |
+
"model_q4.onnx": 3,
|
| 65 |
+
"model_q4f16.onnx": 3,
|
| 66 |
+
"model_q8.onnx": 22
|
| 67 |
+
},
|
| 68 |
+
"kv_cache_dtype": {
|
| 69 |
+
"fp16": "float16",
|
| 70 |
+
"q4f16": "float16"
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 124894,
|
| 4 |
+
"eos_token_id": 124900,
|
| 5 |
+
"pad_token_id": 124893,
|
| 6 |
+
"transformers_version": "4.54.0"
|
| 7 |
+
}
|
onnx/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
onnx/model_q4f16.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:191050c4e26d0153fc469e8b8135841bb2a52b11e90bacf788d88a7a7a2983c1
|
| 3 |
+
size 45176245
|
onnx/model_q4f16.onnx_data
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3c9375129d1eed9befe7fb69608909ce3db21761ccb28b1c8b471cc929cc5c15
|
| 3 |
+
size 431128576
|
onnx/model_q4f16.onnx_data_1
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cbc54fe159e179c28d67159c07aac454381592b2d856a2e3788ddd2ef6dc13fb
|
| 3 |
+
size 413138944
|
onnx/model_q4f16.onnx_data_10
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6301d616a54e99400b76d205876e20e7d4a31753981c298fb5108a684cb5db29
|
| 3 |
+
size 408944640
|
onnx/model_q4f16.onnx_data_11
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fbc6ea8117b4860ab69b4669b3e5fb0b478557d5d10e12be5318555746519387
|
| 3 |
+
size 337641472
|
onnx/model_q4f16.onnx_data_2
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:52ae74a629e3b874635139f46b3235efd7a218af9d5742b7221b17d9139b6fe1
|
| 3 |
+
size 408944640
|
onnx/model_q4f16.onnx_data_3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:83a47136736010d94472bd3f4cc717c1438f9a6f33de934f12525eb363eb2a91
|
| 3 |
+
size 413138944
|
onnx/model_q4f16.onnx_data_4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f3215bf47dc88588302bbea38e89ff091b32dbe3286e387786ecc366009e89f3
|
| 3 |
+
size 408944640
|
onnx/model_q4f16.onnx_data_5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5c44493e798ee4754b678aa27c5892cf4b18283e86184ae501f0582a321002d1
|
| 3 |
+
size 413138944
|
onnx/model_q4f16.onnx_data_6
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:37c1659b42ad641ba14f16a66ec7471512190b928f22146e22711b7981e9b338
|
| 3 |
+
size 408944640
|
onnx/model_q4f16.onnx_data_7
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4d1339c1372df1d9c2fd859f97c73aa87aab51c051d8484275dd06ae8d295aa6
|
| 3 |
+
size 413138944
|
onnx/model_q4f16.onnx_data_8
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2ff9fac6c23b627d7b0da49179cfc807a3fdbfb8db96911baa33486d7849cd5d
|
| 3 |
+
size 408944640
|
onnx/model_q4f16.onnx_data_9
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4ba14f44f707417419b3317b9634e7f9b8c7a832b856c80cdb9f4366cfbe196f
|
| 3 |
+
size 413138944
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:695be7802a0e4b8a81048f0ff5ebb7fc811a0ba5a6be63dbb24deb5a81096f41
|
| 3 |
+
size 17905598
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "<|startoftext|>",
|
| 4 |
+
"clean_up_tokenization_spaces": false,
|
| 5 |
+
"eos_token": "<|im_end|>",
|
| 6 |
+
"is_local": true,
|
| 7 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 8 |
+
"pad_token": "<|pad|>",
|
| 9 |
+
"tokenizer_class": "TokenizersBackend",
|
| 10 |
+
"use_default_system_prompt": false,
|
| 11 |
+
"chat_template": "{{- bos_token -}}\n{%- set preserve_thinking = preserve_thinking | default(false) -%}\n\n{%- macro format_arg_value(arg_value) -%}\n {%- if arg_value is string -%}\n {{- \"'\" + arg_value + \"'\" -}}\n {%- elif arg_value is mapping -%}\n {{- arg_value | tojson -}}\n {%- else -%}\n {{- arg_value | string -}}\n {%- endif -%}\n{%- endmacro -%}\n\n{%- macro parse_content(content) -%}\n {%- if content is string -%}\n {{- content -}}\n {%- else -%}\n {%- set _ns = namespace(result=\"\") -%}\n {%- for item in content -%}\n {%- if item[\"type\"] == \"image\" -%}\n {%- set _ns.result = _ns.result + \"<image>\" -%}\n {%- elif item[\"type\"] == \"text\" -%}\n {%- set _ns.result = _ns.result + item[\"text\"] -%}\n {%- else -%}\n {%- set _ns.result = _ns.result + item | tojson -%}\n {%- endif -%}\n {%- endfor -%}\n {{- _ns.result -}}\n {%- endif -%}\n{%- endmacro -%}\n\n{%- macro render_tool_calls(tool_calls) -%}\n {%- set tool_calls_ns = namespace(tool_calls=[]) -%}\n {%- for tool_call in tool_calls -%}\n {%- set func_name = tool_call[\"function\"][\"name\"] -%}\n {%- set func_args = tool_call[\"function\"][\"arguments\"] -%}\n {%- set args_ns = namespace(arg_strings=[]) -%}\n {%- for arg_name, arg_value in func_args.items() -%}\n {%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + \"=\" + format_arg_value(arg_value)] -%}\n {%- endfor -%}\n {%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + \"(\" + (args_ns.arg_strings | join(\", \")) + \")\"] -%}\n {%- endfor -%}\n {{- \"<|tool_call_start|>[\" + (tool_calls_ns.tool_calls | join(\", \")) + \"]<|tool_call_end|>\" -}}\n{%- endmacro -%}\n\n{%- set ns = namespace(system_prompt=\"\", last_user_index=-1) -%}\n{%- if messages[0][\"role\"] == \"system\" -%}\n {%- if messages[0].get(\"content\") -%}\n {%- set ns.system_prompt = parse_content(messages[0][\"content\"]) -%}\n {%- endif -%}\n {%- set messages = messages[1:] -%}\n{%- endif -%}\n{%- if tools -%}\n {%- set ns.system_prompt = ns.system_prompt + (\"\\n\\n\" if ns.system_prompt else \"\") + \"Today's date: \" + strftime_now(\"%Y-%m-%d\") + \"\\n\\nList of tools: \" + (tools | tojson) -%}\n{%- endif -%}\n{%- if ns.system_prompt -%}\n {{- \"<|im_start|>system\\n\" + ns.system_prompt + \"<|im_end|>\\n\" -}}\n{%- endif -%}\n{%- for message in messages -%}\n {%- if message[\"role\"] == \"user\" -%}\n {%- set ns.last_user_index = loop.index0 -%}\n {%- endif -%}\n{%- endfor -%}\n{%- for message in messages -%}\n {{- \"<|im_start|>\" + message.role + \"\\n\" -}}\n {%- if message.role == \"assistant\" -%}\n {%- generation -%}\n {%- if message.thinking is defined and (preserve_thinking or loop.index0 > ns.last_user_index) -%}\n {{- \"<think>\" + message.thinking + \"</think>\" -}}\n {%- endif -%}\n {%- set _cfm_tag = \"CONTINUE_FINAL_MESSAGE_TAG \" -%}\n {%- set _has_cfm = false -%}\n {%- if message.content is defined -%}\n {%- set content = parse_content(message.content) -%}\n {%- if not (preserve_thinking or loop.index0 > ns.last_user_index) -%}\n {%- if \"</think>\" in content -%}\n {%- set content = content.split(\"</think>\")[-1] | trim -%}\n {%- endif -%}\n {%- endif -%}\n {%- if message.tool_calls is defined and content.endswith(_cfm_tag) -%}\n {%- set _has_cfm = true -%}\n {%- set _trunc_len = (content | length) - (_cfm_tag | length) -%}\n {{- content[:_trunc_len] -}}\n {%- else -%}\n {{- content -}}\n {%- endif -%}\n {%- endif -%}\n {%- if message.tool_calls is defined -%}\n {{- render_tool_calls(message.tool_calls) -}}\n {%- endif -%}\n {%- if _has_cfm -%}\n {{- _cfm_tag -}}\n {%- endif -%}\n {{- \"<|im_end|>\\n\" -}}\n {%- endgeneration -%}\n {%- else %}\n {%- if message.get(\"content\") -%}\n {{- parse_content(message[\"content\"]) -}}\n {%- endif -%}\n {{- \"<|im_end|>\\n\" -}}\n {%- endif %}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {{- \"<|im_start|>assistant\\n\" -}}\n{%- endif -%}"
|
| 12 |
+
}
|