Text Generation
MLX
Safetensors
English
Chinese
longcat_flash_sparse
longcat
lsa
Mixture of Experts
ngram-embedding
conversational
4-bit precision
Instructions to use mlx-community/LongCat-Flash-Lite-Sparse-4bit with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use mlx-community/LongCat-Flash-Lite-Sparse-4bit with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("mlx-community/LongCat-Flash-Lite-Sparse-4bit") prompt = "Write a story about Einstein" messages = [{"role": "user", "content": prompt}] prompt = tokenizer.apply_chat_template( messages, add_generation_prompt=True ) text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Pi
How to use mlx-community/LongCat-Flash-Lite-Sparse-4bit with Pi:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "mlx-community/LongCat-Flash-Lite-Sparse-4bit"
Configure the model in Pi
# Install Pi: npm install -g @earendil-works/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "mlx-lm": { "baseUrl": "http://localhost:8080/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "mlx-community/LongCat-Flash-Lite-Sparse-4bit" } ] } } }Run Pi
# Start Pi in your project directory: pi
- MLX LM
How to use mlx-community/LongCat-Flash-Lite-Sparse-4bit with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Interactive chat REPL mlx_lm.chat --model "mlx-community/LongCat-Flash-Lite-Sparse-4bit"
Run an OpenAI-compatible server
# Install MLX LM uv tool install mlx-lm # Start the server mlx_lm.server --model "mlx-community/LongCat-Flash-Lite-Sparse-4bit" # Calling the OpenAI-compatible server with curl curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mlx-community/LongCat-Flash-Lite-Sparse-4bit", "messages": [ {"role": "user", "content": "Hello"} ] }' - Hermes Agent
How to use mlx-community/LongCat-Flash-Lite-Sparse-4bit with Hermes Agent:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "mlx-community/LongCat-Flash-Lite-Sparse-4bit"
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:8080/v1 hermes config set model.default mlx-community/LongCat-Flash-Lite-Sparse-4bit
Run Hermes
hermes
- Atomic Chat
- OpenClaw
How to use mlx-community/LongCat-Flash-Lite-Sparse-4bit with OpenClaw:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "mlx-community/LongCat-Flash-Lite-Sparse-4bit"
Configure OpenClaw
# Install OpenClaw: npm install -g openclaw@latest # Register the local server and set it as the default model: openclaw onboard --non-interactive --mode local \ --auth-choice custom-api-key \ --custom-base-url http://127.0.0.1:8080/v1 \ --custom-model-id "mlx-community/LongCat-Flash-Lite-Sparse-4bit" \ --custom-provider-id mlx-lm \ --custom-compatibility openai \ --custom-text-input \ --accept-risk \ --skip-health
Run OpenClaw
openclaw agent --local --agent main --message "Hello from Hugging Face"
Upload folder using huggingface_hub
Browse files- README.md +67 -0
- chat_template.jinja +190 -0
- config.json +197 -0
- generation_config.json +11 -0
- model-00001-of-00008.safetensors +3 -0
- model-00002-of-00008.safetensors +3 -0
- model-00003-of-00008.safetensors +3 -0
- model-00004-of-00008.safetensors +3 -0
- model-00005-of-00008.safetensors +3 -0
- model-00006-of-00008.safetensors +3 -0
- model-00007-of-00008.safetensors +3 -0
- model-00008-of-00008.safetensors +3 -0
- model.safetensors.index.json +0 -0
- special_tokens_map.json +30 -0
- tokenization_llama.py +231 -0
- tokenizer.json +0 -0
- tokenizer_config.json +14 -0
README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
- zh
|
| 5 |
+
license: mit
|
| 6 |
+
library_name: mlx
|
| 7 |
+
tags:
|
| 8 |
+
- mlx
|
| 9 |
+
- longcat
|
| 10 |
+
- lsa
|
| 11 |
+
- moe
|
| 12 |
+
- ngram-embedding
|
| 13 |
+
base_model: meituan-longcat/LongCat-Flash-Lite-Sparse
|
| 14 |
+
pipeline_tag: text-generation
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# LongCat-Flash-Lite-Sparse-4bit (MLX)
|
| 18 |
+
|
| 19 |
+
4-bit MLX quantization of [meituan-longcat/LongCat-Flash-Lite-Sparse](https://huggingface.co/meituan-longcat/LongCat-Flash-Lite-Sparse) (69B-A3B, `LongcatCausalLM`).
|
| 20 |
+
|
| 21 |
+
To our knowledge this is the **first working implementation of LongCat-Flash-Lite-Sparse in any framework** — no upstream serving stack (mlx-lm, vLLM, SGLang, llama.cpp) supports the `oe_embed_*` variant yet.
|
| 22 |
+
|
| 23 |
+
## What's in this checkpoint
|
| 24 |
+
LongCat-Flash-Lite-Sparse adds three things vanilla LongCat-Flash lacks:
|
| 25 |
+
- **LongCat Sparse Attention (LSA)** — a DeepSeek-style lightning indexer over MLA, with streaming-aware indexing (fixed sink + local window) and cross-layer index reuse (`cli_factor`). Native long context.
|
| 26 |
+
- **Zero-computation (identity) experts** in the ScMoE decoder (256 routed + 128 identity, top-12).
|
| 27 |
+
- **N-gram ("oe") input embedding** — ~46% of the parameters, fused into the token embedding.
|
| 28 |
+
|
| 29 |
+
## The n-gram fix
|
| 30 |
+
The `oe` embedding hash and tables are identical to the published n-gram references (the *Scaling Embeddings* paper, mlx-lm, SGLang, llama.cpp, Meituan's dense modeling). The one difference in `LongcatCausalLM` is the **fusion**: it keeps the word embedding at **full scale** —
|
| 31 |
+
`word + Σ projections / (1 + num_embedders)` — rather than the dense form `(word + Σ projections) / (1 + num_embedders)`. Dividing the word by 13 garbles generation; this build applies the correct fusion.
|
| 32 |
+
|
| 33 |
+
## Usage
|
| 34 |
+
Requires mlx-vlm with `longcat_flash_sparse` support ([PR #2063](https://github.com/Blaizzy/mlx-vlm/pull/2063)):
|
| 35 |
+
|
| 36 |
+
```bash
|
| 37 |
+
pip install git+https://github.com/Lazarus-931/mlx-vlm@add-longcat-flash
|
| 38 |
+
```
|
| 39 |
+
```python
|
| 40 |
+
from mlx_vlm import load, generate
|
| 41 |
+
model, processor = load("AlazarM/LongCat-Flash-Lite-Sparse-4bit", trust_remote_code=True)
|
| 42 |
+
tok = processor.tokenizer
|
| 43 |
+
text = tok.apply_chat_template(
|
| 44 |
+
[{"role": "user", "content": "What is the capital of France?"}],
|
| 45 |
+
tokenize=False, add_generation_prompt=True,
|
| 46 |
+
)
|
| 47 |
+
print(generate(model, processor, text, max_tokens=64, temperature=0.0))
|
| 48 |
+
# -> The capital of France is Paris.
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
## Baseline (M5 Max, batch 1, greedy)
|
| 52 |
+
Prefill (chunked at 512) and decode throughput vs. context length:
|
| 53 |
+
|
| 54 |
+
| context | prefill tok/s | decode tok/s | peak mem |
|
| 55 |
+
|--:|--:|--:|--:|
|
| 56 |
+
| 512 | 1907 | 110.3 | 39.3 GB |
|
| 57 |
+
| 1024 | 2724 | 99.0 | 39.4 GB |
|
| 58 |
+
| 2048 | 1969 | 80.4 | 39.5 GB |
|
| 59 |
+
| 4096 | 1781 | 82.5 | 39.8 GB |
|
| 60 |
+
| 8192 | 1446 | 80.2 | 40.5 GB |
|
| 61 |
+
| 16384 | 1067 | 76.7 | 41.8 GB |
|
| 62 |
+
| 32768 | 571 | 72.6 | 44.2 GB |
|
| 63 |
+
|
| 64 |
+
LSA's dynamic sparse selection activates once the KV length exceeds `index_topk` (2048), which keeps decode throughput nearly flat (110→73 tok/s) as context grows to 32k. Peak memory is the 4-bit weights (~36 GB) plus the MLA + indexer caches.
|
| 65 |
+
|
| 66 |
+
## License
|
| 67 |
+
MIT, inherited from the base model.
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set tool_choice = tool_choice | default('auto') -%}
|
| 2 |
+
{%- set enable_thinking = enable_thinking | default(none) -%}
|
| 3 |
+
{%- set save_history_reasoning_content = save_history_reasoning_content | default(false) -%}
|
| 4 |
+
{%- set ns = namespace(final_messages = [], tool_types = [], last_query_index = -1) -%}
|
| 5 |
+
{%- for message in messages -%}
|
| 6 |
+
{%- set role = message.get('role') -%}
|
| 7 |
+
{%- set c = message.get('content') -%}
|
| 8 |
+
{%- if c is string -%}
|
| 9 |
+
{%- set ns.final_messages = ns.final_messages + [message] -%}
|
| 10 |
+
{%- elif c is iterable and c is not mapping -%}
|
| 11 |
+
{%- set parts = namespace(text=[], thinking=[], tool_calls=[], tool_results=[]) -%}
|
| 12 |
+
{%- for item in c -%}
|
| 13 |
+
{%- if item['type'] == 'text' -%}
|
| 14 |
+
{%- set parts.text = parts.text + [item['text']] -%}
|
| 15 |
+
{%- elif item['type'] == 'thinking' -%}
|
| 16 |
+
{%- if item['thinking'] -%}
|
| 17 |
+
{%- set parts.thinking = parts.thinking + [item['thinking']] -%}
|
| 18 |
+
{%- endif -%}
|
| 19 |
+
{%- elif item['type'] == 'tool_use' -%}
|
| 20 |
+
{%- set parts.tool_calls = parts.tool_calls + [{
|
| 21 |
+
'id': item['id'],
|
| 22 |
+
'type': 'function',
|
| 23 |
+
'function': {
|
| 24 |
+
'name': item['name'],
|
| 25 |
+
'arguments': item['input']
|
| 26 |
+
}
|
| 27 |
+
}] -%}
|
| 28 |
+
{%- elif item['type'] == 'tool_result' -%}
|
| 29 |
+
{%- set parts.tool_results = parts.tool_results + [{
|
| 30 |
+
'role': 'tool',
|
| 31 |
+
'name': item.get('name', ''),
|
| 32 |
+
'tool_call_id': item['tool_use_id'],
|
| 33 |
+
'content': item['content']
|
| 34 |
+
}] -%}
|
| 35 |
+
{%- endif -%}
|
| 36 |
+
{%- endfor -%}
|
| 37 |
+
{%- set ns.final_messages = ns.final_messages + parts.tool_results -%}
|
| 38 |
+
{%- if not (parts.tool_results | length > 0 and parts.text | length == 0 and role == 'user') -%}
|
| 39 |
+
{%- set ns.final_messages = ns.final_messages + [{
|
| 40 |
+
'role': role,
|
| 41 |
+
'content': parts.text | join(''),
|
| 42 |
+
'reasoning_content': parts.thinking | join(''),
|
| 43 |
+
'tool_calls': parts.tool_calls
|
| 44 |
+
}] -%}
|
| 45 |
+
{%- endif -%}
|
| 46 |
+
{%- else -%}
|
| 47 |
+
{%- set ns.final_messages = ns.final_messages + [message] -%}
|
| 48 |
+
{%- endif -%}
|
| 49 |
+
{%- endfor -%}
|
| 50 |
+
{%- set messages = ns.final_messages -%}
|
| 51 |
+
|
| 52 |
+
{%- for idx in range(messages|length) -%}
|
| 53 |
+
{%- set msg = messages[idx] -%}
|
| 54 |
+
{%- if msg.role == 'user' -%}
|
| 55 |
+
{%- set ns.last_query_index = idx -%}
|
| 56 |
+
{%- endif -%}
|
| 57 |
+
{%- endfor -%}
|
| 58 |
+
|
| 59 |
+
{%- if tools and tool_choice != 'none' -%}
|
| 60 |
+
{{- "<longcat_tool_declare>\n" -}}
|
| 61 |
+
{{- "# Tools\n" -}}
|
| 62 |
+
{{- "You have access to the following tools:\n\n" -}}
|
| 63 |
+
{%- for tool in tools -%}
|
| 64 |
+
{%- if tool.type not in ns.tool_types -%}
|
| 65 |
+
{%- set ns.tool_types = ns.tool_types + [tool.type] -%}
|
| 66 |
+
{{- "## Tool namespace: " ~ tool.type ~ "\n\n" -}}
|
| 67 |
+
{%- endif -%}
|
| 68 |
+
{%- if tool.type == 'code_interpreter' -%}
|
| 69 |
+
{%- set tool = {"type":"code_interpreter","function":{"name":"code_interpreter_preview","description":"The code will be executed in a stateful Jupyter notebook sandbox environment, only supports local computation, data processing, and file operations.\nCode sandbox environment (network isolated) Any external network requests or online API calls are prohibited.\nIf online functionality is needed, please use other permitted tools.\nCode will respond with the output of the execution or time out after 60.0 seconds. ","parameters":{"type":"object","properties":{"language":{"type":"string","description":"The programming language of the code to be executed. Available values: python (Default), java, go, js, ts, c, c++."},"code":{"type":"string","description":"Python code to be executed must not include the following:\n- Importing network libraries such as requests, httplib, etc.\n- Any form of HTTP requests.\n- External API calls.\n- Network port operations. Example: ```python\nimport pandas as pd\npd.DataFrame({'A':[1,2]})\n```"},"timeout":{"type":"number","description":"The maximum execution time of the code, in seconds. Default is 60.0."}}},"required":["code"]}} -%}
|
| 70 |
+
{%- endif -%}
|
| 71 |
+
{{- "### Tool name: " + tool.function.name + "\n" -}}
|
| 72 |
+
{{- "Description: " + tool.function.description + "\n\n" -}}
|
| 73 |
+
{{- "InputSchema: " + tool.function.parameters | tojson(ensure_ascii=False) + "\n\n" -}}
|
| 74 |
+
{%- endfor -%}
|
| 75 |
+
{{- '**Note**: For each function call, output the function name and arguments within the following XML format:\n<longcat_tool_call>{function-name}\n<longcat_arg_key>{arg-key-1}</longcat_arg_key>\n<longcat_arg_value>{arg-value-1}</longcat_arg_value>\n<longcat_arg_key>{arg-key-2}</longcat_arg_key>\n<longcat_arg_value>{arg-value-2}</longcat_arg_value>\n...\n</longcat_tool_call>\n' -}}
|
| 76 |
+
{{- "</longcat_tool_declare>"-}}
|
| 77 |
+
{%- endif -%}
|
| 78 |
+
|
| 79 |
+
{%- for msg in messages -%}
|
| 80 |
+
{%- if msg.role == "system" -%}
|
| 81 |
+
{{- "<longcat_system>" + msg.content -}}
|
| 82 |
+
{%- elif msg.role == "user" -%}
|
| 83 |
+
{{- "<longcat_user>" -}}
|
| 84 |
+
{%- if msg["files"] -%}
|
| 85 |
+
{{- '<longcat_files>\n' ~ msg.files | tojson(indent=2) ~ '\n</longcat_files>' -}}
|
| 86 |
+
{%- endif -%}
|
| 87 |
+
{{- msg.content -}}
|
| 88 |
+
{%- if save_history_reasoning_content and enable_thinking is not none -%}
|
| 89 |
+
{%- if loop.last or messages[loop.index0 + 1].role != 'assistant' -%}
|
| 90 |
+
{# pass #}
|
| 91 |
+
{%- else -%}
|
| 92 |
+
{%- if enable_thinking == true -%}
|
| 93 |
+
{{- " /think_on" -}}
|
| 94 |
+
{%- if thinking_budget -%}
|
| 95 |
+
{%- if thinking_budget < 1024 -%}
|
| 96 |
+
{%- set thinking_budget = 1024 -%}
|
| 97 |
+
{%- endif -%}
|
| 98 |
+
{{- "\nthinking_budget: < " ~ thinking_budget ~ "." -}}
|
| 99 |
+
{%- endif -%}
|
| 100 |
+
{{- " " -}}
|
| 101 |
+
{%- elif enable_thinking == false -%}
|
| 102 |
+
{{- " /think_off " -}}
|
| 103 |
+
{%- endif -%}
|
| 104 |
+
{%- endif -%}
|
| 105 |
+
{%- elif loop.index0 >= ns.last_query_index and enable_thinking is not none -%}
|
| 106 |
+
{%- if loop.last or messages[loop.index0 + 1].role != 'assistant' -%}
|
| 107 |
+
{# pass #}
|
| 108 |
+
{%- else -%}
|
| 109 |
+
{%- if enable_thinking == true -%}
|
| 110 |
+
{{- " /think_on" -}}
|
| 111 |
+
{%- if thinking_budget -%}
|
| 112 |
+
{%- if thinking_budget < 1024 -%}
|
| 113 |
+
{%- set thinking_budget = 1024 -%}
|
| 114 |
+
{%- endif -%}
|
| 115 |
+
{{- "\nthinking_budget: < " ~ thinking_budget ~ "." -}}
|
| 116 |
+
{%- endif -%}
|
| 117 |
+
{{- " " -}}
|
| 118 |
+
{%- elif enable_thinking == false -%}
|
| 119 |
+
{{- " /think_off " -}}
|
| 120 |
+
{%- endif -%}
|
| 121 |
+
{%- endif -%}
|
| 122 |
+
{%- endif -%}
|
| 123 |
+
{%- elif msg.role == "assistant" -%}
|
| 124 |
+
{{- "<longcat_assistant>" -}}
|
| 125 |
+
{%- if save_history_reasoning_content == true or loop.index0 > ns.last_query_index -%}
|
| 126 |
+
{%- if enable_thinking == true -%}
|
| 127 |
+
{%- if msg.reasoning_content -%}
|
| 128 |
+
{%- set reasoning_content = msg.reasoning_content | trim -%}
|
| 129 |
+
{{- "<longcat_think>\n" ~ reasoning_content ~ "\n</longcat_think>\n" -}}
|
| 130 |
+
{%- else -%}
|
| 131 |
+
{{- "<longcat_think>\n\n</longcat_think>\n" -}}
|
| 132 |
+
{%- endif -%}
|
| 133 |
+
{%- elif enable_thinking == false -%}
|
| 134 |
+
{{- "<longcat_think>\n\n</longcat_think>\n" -}}
|
| 135 |
+
{%- endif -%}
|
| 136 |
+
{%- endif -%}
|
| 137 |
+
{%- if msg.content -%}
|
| 138 |
+
{{- msg.content -}}
|
| 139 |
+
{%- endif -%}
|
| 140 |
+
{%- if msg.tool_calls -%}
|
| 141 |
+
{%- for tool_call in msg.tool_calls -%}
|
| 142 |
+
{{- "<longcat_tool_call>" ~ tool_call.function.name ~ "\n" -}}
|
| 143 |
+
{%- set _args = tool_call.function.arguments -%}
|
| 144 |
+
{%- for k, v in _args.items() -%}
|
| 145 |
+
{{- "<longcat_arg_key>" ~ k ~ "</longcat_arg_key>\n" -}}
|
| 146 |
+
{{- "<longcat_arg_value>" ~ (v if v is string else v | tojson(ensure_ascii=False)) ~ "</longcat_arg_value>\n" -}}
|
| 147 |
+
{% endfor -%}
|
| 148 |
+
{{- "</longcat_tool_call>\n" -}}
|
| 149 |
+
{%- endfor -%}
|
| 150 |
+
{%- endif -%}
|
| 151 |
+
{{- "</longcat_s>" -}}
|
| 152 |
+
{%- elif msg.role == "tool" -%}
|
| 153 |
+
{%- if messages[loop.index0 - 1].role != "tool" -%}
|
| 154 |
+
{{- "<longcat_observation>" -}}
|
| 155 |
+
{%- endif -%}
|
| 156 |
+
{{- "<longcat_tool_response>" ~ msg.name ~ "\n" ~ msg.content ~ "</longcat_tool_response>" -}}
|
| 157 |
+
{%- if loop.index0 == messages|length - 1 -%}
|
| 158 |
+
{# pass #}
|
| 159 |
+
{%- elif (save_history_reasoning_content or loop.index0 > ns.last_query_index) and messages[loop.index0 + 1].role != "tool" -%}
|
| 160 |
+
{%- if enable_thinking == true -%}
|
| 161 |
+
{{- " /think_on" -}}
|
| 162 |
+
{%- if thinking_budget -%}
|
| 163 |
+
{%- if thinking_budget < 1024 -%}
|
| 164 |
+
{%- set thinking_budget = 1024 -%}
|
| 165 |
+
{%- endif -%}
|
| 166 |
+
{{- "\nthinking_budget: < " ~ thinking_budget ~ "." -}}
|
| 167 |
+
{%- endif -%}
|
| 168 |
+
{{- " " -}}
|
| 169 |
+
{%- elif enable_thinking == false -%}
|
| 170 |
+
{{- " /think_off " -}}
|
| 171 |
+
{%- endif -%}
|
| 172 |
+
{%- endif -%}
|
| 173 |
+
{%- endif -%}
|
| 174 |
+
{%- endfor -%}
|
| 175 |
+
{%- if add_generation_prompt -%}
|
| 176 |
+
{%- if enable_thinking == true -%}
|
| 177 |
+
{{- " /think_on" -}}
|
| 178 |
+
{%- if thinking_budget -%}
|
| 179 |
+
{%- if thinking_budget < 1024 -%}
|
| 180 |
+
{%- set thinking_budget = 1024 -%}
|
| 181 |
+
{%- endif -%}
|
| 182 |
+
{{- "\nthinking_budget: < " ~ thinking_budget ~ "." -}}
|
| 183 |
+
{%- endif -%}
|
| 184 |
+
{{- " <longcat_assistant><longcat_think>\n" -}}
|
| 185 |
+
{%- elif enable_thinking == false -%}
|
| 186 |
+
{{- " /think_off <longcat_assistant><longcat_think>\n\n</longcat_think>\n" -}}
|
| 187 |
+
{%- else -%}
|
| 188 |
+
{{- "<longcat_assistant>" -}}
|
| 189 |
+
{%- endif -%}
|
| 190 |
+
{%- endif -%}
|
config.json
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LongcatCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"attention_method": "LSA",
|
| 8 |
+
"bos_token_id": 1,
|
| 9 |
+
"cli_factor": 2,
|
| 10 |
+
"dsa_mtp_cli": true,
|
| 11 |
+
"eos_token_id": 2,
|
| 12 |
+
"expert_ffn_hidden_size": 1024,
|
| 13 |
+
"ffn_hidden_size": 6144,
|
| 14 |
+
"generation_config": {
|
| 15 |
+
"_from_model_config": true,
|
| 16 |
+
"bos_token_id": 1,
|
| 17 |
+
"eos_token_id": 2,
|
| 18 |
+
"pad_token_id": 3,
|
| 19 |
+
"transformers_version": "4.57.6",
|
| 20 |
+
"repetition_penalty": 1.06,
|
| 21 |
+
"temperature": 0.7,
|
| 22 |
+
"top_p": 0.95,
|
| 23 |
+
"top_k": 4
|
| 24 |
+
},
|
| 25 |
+
"hidden_size": 3072,
|
| 26 |
+
"index_head_dim": 128,
|
| 27 |
+
"index_init_tokens": 16,
|
| 28 |
+
"index_k_norm_type": "rms",
|
| 29 |
+
"index_local_tokens": 1024,
|
| 30 |
+
"index_n_heads": 16,
|
| 31 |
+
"index_topk": 2048,
|
| 32 |
+
"indexer_rope_interleave": true,
|
| 33 |
+
"kv_lora_rank": 512,
|
| 34 |
+
"max_position_embeddings": 983040,
|
| 35 |
+
"mla_scale_kv_lora": true,
|
| 36 |
+
"mla_scale_q_lora": true,
|
| 37 |
+
"model_type": "longcat_flash_sparse",
|
| 38 |
+
"moe_impl": "mix",
|
| 39 |
+
"moe_switch_token_num": 1024,
|
| 40 |
+
"moe_topk": 12,
|
| 41 |
+
"mtp_disable_over_tokenizer": false,
|
| 42 |
+
"mtp_num_layers": 3,
|
| 43 |
+
"mtp_replicate_modules": true,
|
| 44 |
+
"n_routed_experts": 256,
|
| 45 |
+
"num_attention_heads": 32,
|
| 46 |
+
"num_layers": 14,
|
| 47 |
+
"oe_neighbor_num": 4,
|
| 48 |
+
"oe_split_num": 4,
|
| 49 |
+
"oe_vocab_size_ratio": 78,
|
| 50 |
+
"q_lora_rank": 1536,
|
| 51 |
+
"qk_nope_head_dim": 128,
|
| 52 |
+
"qk_rope_head_dim": 64,
|
| 53 |
+
"quantization": {
|
| 54 |
+
"group_size": 64,
|
| 55 |
+
"bits": 4,
|
| 56 |
+
"mode": "affine",
|
| 57 |
+
"language_model.model.layers.0.mlp.router.classifier": {
|
| 58 |
+
"group_size": 64,
|
| 59 |
+
"bits": 8
|
| 60 |
+
},
|
| 61 |
+
"language_model.model.layers.1.mlp.router.classifier": {
|
| 62 |
+
"group_size": 64,
|
| 63 |
+
"bits": 8
|
| 64 |
+
},
|
| 65 |
+
"language_model.model.layers.2.mlp.router.classifier": {
|
| 66 |
+
"group_size": 64,
|
| 67 |
+
"bits": 8
|
| 68 |
+
},
|
| 69 |
+
"language_model.model.layers.3.mlp.router.classifier": {
|
| 70 |
+
"group_size": 64,
|
| 71 |
+
"bits": 8
|
| 72 |
+
},
|
| 73 |
+
"language_model.model.layers.4.mlp.router.classifier": {
|
| 74 |
+
"group_size": 64,
|
| 75 |
+
"bits": 8
|
| 76 |
+
},
|
| 77 |
+
"language_model.model.layers.5.mlp.router.classifier": {
|
| 78 |
+
"group_size": 64,
|
| 79 |
+
"bits": 8
|
| 80 |
+
},
|
| 81 |
+
"language_model.model.layers.6.mlp.router.classifier": {
|
| 82 |
+
"group_size": 64,
|
| 83 |
+
"bits": 8
|
| 84 |
+
},
|
| 85 |
+
"language_model.model.layers.7.mlp.router.classifier": {
|
| 86 |
+
"group_size": 64,
|
| 87 |
+
"bits": 8
|
| 88 |
+
},
|
| 89 |
+
"language_model.model.layers.8.mlp.router.classifier": {
|
| 90 |
+
"group_size": 64,
|
| 91 |
+
"bits": 8
|
| 92 |
+
},
|
| 93 |
+
"language_model.model.layers.9.mlp.router.classifier": {
|
| 94 |
+
"group_size": 64,
|
| 95 |
+
"bits": 8
|
| 96 |
+
},
|
| 97 |
+
"language_model.model.layers.10.mlp.router.classifier": {
|
| 98 |
+
"group_size": 64,
|
| 99 |
+
"bits": 8
|
| 100 |
+
},
|
| 101 |
+
"language_model.model.layers.11.mlp.router.classifier": {
|
| 102 |
+
"group_size": 64,
|
| 103 |
+
"bits": 8
|
| 104 |
+
},
|
| 105 |
+
"language_model.model.layers.12.mlp.router.classifier": {
|
| 106 |
+
"group_size": 64,
|
| 107 |
+
"bits": 8
|
| 108 |
+
},
|
| 109 |
+
"language_model.model.layers.13.mlp.router.classifier": {
|
| 110 |
+
"group_size": 64,
|
| 111 |
+
"bits": 8
|
| 112 |
+
}
|
| 113 |
+
},
|
| 114 |
+
"quantization_config": {
|
| 115 |
+
"group_size": 64,
|
| 116 |
+
"bits": 4,
|
| 117 |
+
"mode": "affine",
|
| 118 |
+
"language_model.model.layers.0.mlp.router.classifier": {
|
| 119 |
+
"group_size": 64,
|
| 120 |
+
"bits": 8
|
| 121 |
+
},
|
| 122 |
+
"language_model.model.layers.1.mlp.router.classifier": {
|
| 123 |
+
"group_size": 64,
|
| 124 |
+
"bits": 8
|
| 125 |
+
},
|
| 126 |
+
"language_model.model.layers.2.mlp.router.classifier": {
|
| 127 |
+
"group_size": 64,
|
| 128 |
+
"bits": 8
|
| 129 |
+
},
|
| 130 |
+
"language_model.model.layers.3.mlp.router.classifier": {
|
| 131 |
+
"group_size": 64,
|
| 132 |
+
"bits": 8
|
| 133 |
+
},
|
| 134 |
+
"language_model.model.layers.4.mlp.router.classifier": {
|
| 135 |
+
"group_size": 64,
|
| 136 |
+
"bits": 8
|
| 137 |
+
},
|
| 138 |
+
"language_model.model.layers.5.mlp.router.classifier": {
|
| 139 |
+
"group_size": 64,
|
| 140 |
+
"bits": 8
|
| 141 |
+
},
|
| 142 |
+
"language_model.model.layers.6.mlp.router.classifier": {
|
| 143 |
+
"group_size": 64,
|
| 144 |
+
"bits": 8
|
| 145 |
+
},
|
| 146 |
+
"language_model.model.layers.7.mlp.router.classifier": {
|
| 147 |
+
"group_size": 64,
|
| 148 |
+
"bits": 8
|
| 149 |
+
},
|
| 150 |
+
"language_model.model.layers.8.mlp.router.classifier": {
|
| 151 |
+
"group_size": 64,
|
| 152 |
+
"bits": 8
|
| 153 |
+
},
|
| 154 |
+
"language_model.model.layers.9.mlp.router.classifier": {
|
| 155 |
+
"group_size": 64,
|
| 156 |
+
"bits": 8
|
| 157 |
+
},
|
| 158 |
+
"language_model.model.layers.10.mlp.router.classifier": {
|
| 159 |
+
"group_size": 64,
|
| 160 |
+
"bits": 8
|
| 161 |
+
},
|
| 162 |
+
"language_model.model.layers.11.mlp.router.classifier": {
|
| 163 |
+
"group_size": 64,
|
| 164 |
+
"bits": 8
|
| 165 |
+
},
|
| 166 |
+
"language_model.model.layers.12.mlp.router.classifier": {
|
| 167 |
+
"group_size": 64,
|
| 168 |
+
"bits": 8
|
| 169 |
+
},
|
| 170 |
+
"language_model.model.layers.13.mlp.router.classifier": {
|
| 171 |
+
"group_size": 64,
|
| 172 |
+
"bits": 8
|
| 173 |
+
}
|
| 174 |
+
},
|
| 175 |
+
"rms_norm_eps": 1e-05,
|
| 176 |
+
"rope_scaling": {
|
| 177 |
+
"original_max_position_embeddings": 8192,
|
| 178 |
+
"rope_type": "deepseek_yarn",
|
| 179 |
+
"factor": 120,
|
| 180 |
+
"beta_fast": 32,
|
| 181 |
+
"beta_slow": 1,
|
| 182 |
+
"mscale": 1,
|
| 183 |
+
"mscale_all_dim": 1
|
| 184 |
+
},
|
| 185 |
+
"rope_theta": 1000000.0,
|
| 186 |
+
"routed_scaling_factor": 6.0,
|
| 187 |
+
"temperature": 0.7,
|
| 188 |
+
"top_k": 4,
|
| 189 |
+
"top_p": 0.95,
|
| 190 |
+
"use_cache": true,
|
| 191 |
+
"use_mla": 1,
|
| 192 |
+
"v_head_dim": 128,
|
| 193 |
+
"vision_config": {},
|
| 194 |
+
"vocab_size": 131072,
|
| 195 |
+
"zero_expert_num": 128,
|
| 196 |
+
"zero_expert_type": "identity"
|
| 197 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": 2,
|
| 5 |
+
"pad_token_id": 3,
|
| 6 |
+
"transformers_version": "4.57.6",
|
| 7 |
+
"repetition_penalty": 1.06,
|
| 8 |
+
"temperature": 0.7,
|
| 9 |
+
"top_p": 0.95,
|
| 10 |
+
"top_k": 4
|
| 11 |
+
}
|
model-00001-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:73e479dc535f3965dd7a2e3c76d715195eb9e9eaa39a8bfafaba9fe7abcda2b1
|
| 3 |
+
size 4643097497
|
model-00002-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:58724c05ce00d7182403e9173a30bdc66a84e55cef47750fd98e8bd81dcff4b5
|
| 3 |
+
size 4416607252
|
model-00003-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9f38306da4822b19f3a23a350d7033b4e3fb4389918cb5bf3be4227c89dc620a
|
| 3 |
+
size 4416609836
|
model-00004-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a210eb77f23687b1b7f9e5f808311715184921f5547f04fd3d0ae69ca96c2c27
|
| 3 |
+
size 5327891749
|
model-00005-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4d2c0a575508f6ef2a563706e186edb13e78d0cf2422bd1c5d59881cb3598418
|
| 3 |
+
size 5348043880
|
model-00006-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7df21cc89ef5d28ca6133dce43f45275e88d0830fb6bc915b239dbc682dc2e49
|
| 3 |
+
size 5366577377
|
model-00007-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6e0f8583a44cac37cecf09e8c922ca589f4398002b2a008ffc56b00269dbb30b
|
| 3 |
+
size 5351455810
|
model-00008-of-00008.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:269bc29de2a60cde804677664cce54b77bd8f389d63961b0e81cb73fa32eac38
|
| 3 |
+
size 3734215463
|
model.safetensors.index.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<longcat_s>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"eos_token": {
|
| 10 |
+
"content": "</longcat_s>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "<longcat_pad>",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"unk_token": {
|
| 24 |
+
"content": "<longcat_unk>",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
}
|
| 30 |
+
}
|
tokenization_llama.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
| 5 |
+
# and OPT implementations in this library. It has been modified from its
|
| 6 |
+
# original forms to accommodate minor architectural differences compared
|
| 7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
| 8 |
+
#
|
| 9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 10 |
+
# you may not use this file except in compliance with the License.
|
| 11 |
+
# You may obtain a copy of the License at
|
| 12 |
+
#
|
| 13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 14 |
+
#
|
| 15 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 18 |
+
# See the License for the specific language governing permissions and
|
| 19 |
+
# limitations under the License.
|
| 20 |
+
|
| 21 |
+
"""Tokenization classes for LLaMA."""
|
| 22 |
+
import os
|
| 23 |
+
from shutil import copyfile
|
| 24 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 25 |
+
|
| 26 |
+
import sentencepiece as spm
|
| 27 |
+
|
| 28 |
+
from transformers.tokenization_utils import PreTrainedTokenizer
|
| 29 |
+
from transformers.utils import logging
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
logger = logging.get_logger(__name__)
|
| 33 |
+
|
| 34 |
+
VOCAB_FILES_NAMES = {"vocab_file": "tokenizer.model"}
|
| 35 |
+
|
| 36 |
+
PRETRAINED_VOCAB_FILES_MAP = {}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class LlamaTokenizer(PreTrainedTokenizer):
|
| 40 |
+
"""
|
| 41 |
+
Construct a Llama tokenizer. Based on byte-level Byte-Pair-Encoding.
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
vocab_file (`str`):
|
| 45 |
+
Path to the vocabulary file.
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
| 49 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
| 50 |
+
model_input_names = ["input_ids", "attention_mask"]
|
| 51 |
+
|
| 52 |
+
def __init__(
|
| 53 |
+
self,
|
| 54 |
+
vocab_file,
|
| 55 |
+
unk_token="<unk>",
|
| 56 |
+
bos_token="<s>",
|
| 57 |
+
eos_token="</s>",
|
| 58 |
+
sp_model_kwargs: Optional[Dict[str, Any]] = None,
|
| 59 |
+
add_bos_token=True,
|
| 60 |
+
add_eos_token=False,
|
| 61 |
+
decode_with_prefix_space=False,
|
| 62 |
+
**kwargs,
|
| 63 |
+
):
|
| 64 |
+
self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
|
| 65 |
+
super().__init__(bos_token=bos_token, eos_token=eos_token, unk_token=unk_token, **kwargs)
|
| 66 |
+
self.vocab_file = vocab_file
|
| 67 |
+
self.add_bos_token = add_bos_token
|
| 68 |
+
self.add_eos_token = add_eos_token
|
| 69 |
+
self.decode_with_prefix_space = decode_with_prefix_space
|
| 70 |
+
self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
|
| 71 |
+
self.sp_model.Load(vocab_file)
|
| 72 |
+
self._no_prefix_space_tokens = None
|
| 73 |
+
|
| 74 |
+
""" Initialisation"""
|
| 75 |
+
|
| 76 |
+
@property
|
| 77 |
+
def no_prefix_space_tokens(self):
|
| 78 |
+
if self._no_prefix_space_tokens is None:
|
| 79 |
+
vocab = self.convert_ids_to_tokens(list(range(self.vocab_size)))
|
| 80 |
+
self._no_prefix_space_tokens = {i for i, tok in enumerate(vocab) if not tok.startswith("▁")}
|
| 81 |
+
return self._no_prefix_space_tokens
|
| 82 |
+
|
| 83 |
+
@property
|
| 84 |
+
def vocab_size(self):
|
| 85 |
+
"""Returns vocab size"""
|
| 86 |
+
return self.sp_model.get_piece_size()
|
| 87 |
+
|
| 88 |
+
@property
|
| 89 |
+
def bos_token_id(self) -> Optional[int]:
|
| 90 |
+
return self.sp_model.bos_id()
|
| 91 |
+
|
| 92 |
+
@property
|
| 93 |
+
def eos_token_id(self) -> Optional[int]:
|
| 94 |
+
return self.sp_model.eos_id()
|
| 95 |
+
|
| 96 |
+
def get_vocab(self):
|
| 97 |
+
"""Returns vocab as a dict"""
|
| 98 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
| 99 |
+
vocab.update(self.added_tokens_encoder)
|
| 100 |
+
return vocab
|
| 101 |
+
|
| 102 |
+
def _tokenize(self, text):
|
| 103 |
+
"""Returns a tokenized string."""
|
| 104 |
+
return self.sp_model.encode(text, out_type=str)
|
| 105 |
+
|
| 106 |
+
def _convert_token_to_id(self, token):
|
| 107 |
+
"""Converts a token (str) in an id using the vocab."""
|
| 108 |
+
return self.sp_model.piece_to_id(token)
|
| 109 |
+
|
| 110 |
+
def _convert_id_to_token(self, index):
|
| 111 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
| 112 |
+
token = self.sp_model.IdToPiece(index)
|
| 113 |
+
return token
|
| 114 |
+
|
| 115 |
+
def _maybe_add_prefix_space(self, tokens, decoded):
|
| 116 |
+
if tokens and tokens[0] not in self.no_prefix_space_tokens:
|
| 117 |
+
return " " + decoded
|
| 118 |
+
else:
|
| 119 |
+
return decoded
|
| 120 |
+
|
| 121 |
+
def convert_tokens_to_string(self, tokens):
|
| 122 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
| 123 |
+
current_sub_tokens = []
|
| 124 |
+
out_string = ""
|
| 125 |
+
prev_is_special = False
|
| 126 |
+
for token in tokens:
|
| 127 |
+
# make sure that special tokens are not decoded using sentencepiece model
|
| 128 |
+
if token in self.all_special_tokens:
|
| 129 |
+
if not prev_is_special:
|
| 130 |
+
out_string += " "
|
| 131 |
+
out_string += self.sp_model.decode(current_sub_tokens) + token
|
| 132 |
+
prev_is_special = True
|
| 133 |
+
current_sub_tokens = []
|
| 134 |
+
else:
|
| 135 |
+
current_sub_tokens.append(token)
|
| 136 |
+
prev_is_special = False
|
| 137 |
+
out_string += self.sp_model.decode(current_sub_tokens)
|
| 138 |
+
out_string = self._maybe_add_prefix_space(tokens=tokens, decoded=out_string)
|
| 139 |
+
return out_string
|
| 140 |
+
|
| 141 |
+
def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
| 142 |
+
"""
|
| 143 |
+
Save the vocabulary and special tokens file to a directory.
|
| 144 |
+
|
| 145 |
+
Args:
|
| 146 |
+
save_directory (`str`):
|
| 147 |
+
The directory in which to save the vocabulary.
|
| 148 |
+
|
| 149 |
+
Returns:
|
| 150 |
+
`Tuple(str)`: Paths to the files saved.
|
| 151 |
+
"""
|
| 152 |
+
if not os.path.isdir(save_directory):
|
| 153 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 154 |
+
return
|
| 155 |
+
out_vocab_file = os.path.join(
|
| 156 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file):
|
| 160 |
+
copyfile(self.vocab_file, out_vocab_file)
|
| 161 |
+
elif not os.path.isfile(self.vocab_file):
|
| 162 |
+
with open(out_vocab_file, "wb") as fi:
|
| 163 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
| 164 |
+
fi.write(content_spiece_model)
|
| 165 |
+
|
| 166 |
+
return (out_vocab_file,)
|
| 167 |
+
|
| 168 |
+
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
| 169 |
+
if self.add_bos_token:
|
| 170 |
+
bos_token_ids = [self.bos_token_id]
|
| 171 |
+
else:
|
| 172 |
+
bos_token_ids = []
|
| 173 |
+
|
| 174 |
+
output = bos_token_ids + token_ids_0
|
| 175 |
+
|
| 176 |
+
if token_ids_1 is not None:
|
| 177 |
+
output = output + token_ids_1
|
| 178 |
+
|
| 179 |
+
if self.add_eos_token:
|
| 180 |
+
output = output + [self.eos_token_id]
|
| 181 |
+
|
| 182 |
+
return output
|
| 183 |
+
|
| 184 |
+
def get_special_tokens_mask(
|
| 185 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
| 186 |
+
) -> List[int]:
|
| 187 |
+
"""
|
| 188 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
| 189 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
| 190 |
+
|
| 191 |
+
Args:
|
| 192 |
+
token_ids_0 (`List[int]`):
|
| 193 |
+
List of IDs.
|
| 194 |
+
token_ids_1 (`List[int]`, *optional*):
|
| 195 |
+
Optional second list of IDs for sequence pairs.
|
| 196 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
| 197 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
| 198 |
+
|
| 199 |
+
Returns:
|
| 200 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
| 201 |
+
"""
|
| 202 |
+
if already_has_special_tokens:
|
| 203 |
+
return super().get_special_tokens_mask(
|
| 204 |
+
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
if token_ids_1 is None:
|
| 208 |
+
return [1] + ([0] * len(token_ids_0)) + [1]
|
| 209 |
+
return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1]
|
| 210 |
+
|
| 211 |
+
def create_token_type_ids_from_sequences(
|
| 212 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
| 213 |
+
) -> List[int]:
|
| 214 |
+
"""
|
| 215 |
+
Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make
|
| 216 |
+
use of token type ids, therefore a list of zeros is returned.
|
| 217 |
+
|
| 218 |
+
Args:
|
| 219 |
+
token_ids_0 (`List[int]`):
|
| 220 |
+
List of IDs.
|
| 221 |
+
token_ids_1 (`List[int]`, *optional*):
|
| 222 |
+
Optional second list of IDs for sequence pairs.
|
| 223 |
+
|
| 224 |
+
Returns:
|
| 225 |
+
`List[int]`: List of zeros.
|
| 226 |
+
"""
|
| 227 |
+
eos = [self.eos_token_id]
|
| 228 |
+
|
| 229 |
+
if token_ids_1 is None:
|
| 230 |
+
return len(token_ids_0 + eos) * [0]
|
| 231 |
+
return len(token_ids_0 + eos + token_ids_1 + eos) * [0]
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": "<longcat_s>",
|
| 5 |
+
"clean_up_tokenization_spaces": false,
|
| 6 |
+
"eos_token": "</longcat_s>",
|
| 7 |
+
"is_local": true,
|
| 8 |
+
"local_files_only": false,
|
| 9 |
+
"model_max_length": 131072,
|
| 10 |
+
"pad_token": "<longcat_pad>",
|
| 11 |
+
"sp_model_kwargs": {},
|
| 12 |
+
"tokenizer_class": "TokenizersBackend",
|
| 13 |
+
"unk_token": "<longcat_unk>"
|
| 14 |
+
}
|