Fixed QwQ tokenizer

This commit is contained in:
2025-04-04 08:54:06 +00:00
parent 156d3aa7d3
commit 0e2cca2e4f
3 changed files with 263 additions and 5 deletions

241
qwq_tokenizer_config.json Normal file
View File

@@ -0,0 +1,241 @@
{
"add_bos_token": false,
"add_prefix_space": false,
"added_tokens_decoder": {
"151643": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151644": {
"content": "<|im_start|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151645": {
"content": "<|im_end|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151646": {
"content": "<|object_ref_start|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151647": {
"content": "<|object_ref_end|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151648": {
"content": "<|box_start|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151649": {
"content": "<|box_end|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151650": {
"content": "<|quad_start|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151651": {
"content": "<|quad_end|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151652": {
"content": "<|vision_start|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151653": {
"content": "<|vision_end|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151654": {
"content": "<|vision_pad|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151655": {
"content": "<|image_pad|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151656": {
"content": "<|video_pad|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"151657": {
"content": "<tool_call>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151658": {
"content": "</tool_call>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151659": {
"content": "<|fim_prefix|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151660": {
"content": "<|fim_middle|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151661": {
"content": "<|fim_suffix|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151662": {
"content": "<|fim_pad|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151663": {
"content": "<|repo_name|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151664": {
"content": "<|file_sep|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151665": {
"content": "<tool_response>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151666": {
"content": "</tool_response>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151667": {
"content": "<think>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
},
"151668": {
"content": "</think>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": false
}
},
"additional_special_tokens": [
"<|im_start|>",
"<|im_end|>",
"<|object_ref_start|>",
"<|object_ref_end|>",
"<|box_start|>",
"<|box_end|>",
"<|quad_start|>",
"<|quad_end|>",
"<|vision_start|>",
"<|vision_end|>",
"<|vision_pad|>",
"<|image_pad|>",
"<|video_pad|>"
],
"bos_token": null,
"chat_template": "{%- for message in messages %}\n {{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>\\n' }}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n{%- endif %}",
"clean_up_tokenization_spaces": false,
"eos_token": "<|im_end|>",
"errors": "replace",
"extra_special_tokens": {},
"model_max_length": 131072,
"pad_token": "<|vision_pad|>",
"padding_side": "left",
"split_special_tokens": false,
"tokenizer_class": "Qwen2Tokenizer",
"unk_token": null
}

View File

@@ -2,6 +2,7 @@ from pathlib import Path
from threading import Thread from threading import Thread
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, TextIteratorStreamer, BitsAndBytesConfig from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, TextIteratorStreamer, BitsAndBytesConfig
from typing import Callable, Iterator from typing import Callable, Iterator
import json
import torch import torch
from . import LlmEngine from . import LlmEngine
@@ -26,6 +27,9 @@ class QwQLlmEngine(LlmEngine):
self._temperature = temperature self._temperature = temperature
self._token_limit = token_limit self._token_limit = token_limit
with open('/root/sia/qwq_tokenizer_config.json', 'r') as f:
tokenizer_config = json.load(f)
quantization_config = BitsAndBytesConfig( quantization_config = BitsAndBytesConfig(
load_in_4bit=True, load_in_4bit=True,
bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_compute_dtype=torch.bfloat16,
@@ -41,7 +45,10 @@ class QwQLlmEngine(LlmEngine):
quantization_config=quantization_config, quantization_config=quantization_config,
) )
self._tokenizer = AutoTokenizer.from_pretrained(model_path) self._tokenizer = AutoTokenizer.from_pretrained(
model_path,
tokenizer_config=tokenizer_config,
)
self._pipline = pipeline( self._pipline = pipeline(
"text-generation", "text-generation",
@@ -92,7 +99,7 @@ class QwQLlmEngine(LlmEngine):
generation_thread.start() generation_thread.start()
for text in util.stop_before_value(streamer, '<|eot_id|>'): for text in util.stop_before_value(streamer, self._tokenizer.eos_token):
yield text yield text
if should_stop(): if should_stop():
break break

View File

@@ -8,10 +8,11 @@ from unsloth import FastLanguageModel, is_bfloat16_supported
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from transformers import TrainingArguments from transformers import AutoTokenizer, TrainingArguments
from trl import SFTTrainer, DataCollatorForCompletionOnlyLM from trl import SFTTrainer
from typing import Optional, List from typing import Optional, List
import argparse import argparse
import json
import os import os
from .dataset import Dataset from .dataset import Dataset
@@ -73,13 +74,22 @@ def main():
max_seq_length = 2048 # Can increase for longer reasoning traces max_seq_length = 2048 # Can increase for longer reasoning traces
lora_rank = 64 # Larger rank = smarter, but slower lora_rank = 64 # Larger rank = smarter, but slower
model, tokenizer = FastLanguageModel.from_pretrained( with open('/root/sia/qwq_tokenizer_config.json', 'r') as f:
tokenizer_config = json.load(f)
tokenizer = AutoTokenizer.from_pretrained(
args.base_model,
tokenizer_config=tokenizer_config,
)
model, _returned_tokenizer = FastLanguageModel.from_pretrained(
model_name = args.base_model, model_name = args.base_model,
max_seq_length = max_seq_length, max_seq_length = max_seq_length,
load_in_4bit = True, # False for LoRA 16bit load_in_4bit = True, # False for LoRA 16bit
fast_inference = True, # Enable vLLM fast inference fast_inference = True, # Enable vLLM fast inference
max_lora_rank = lora_rank, max_lora_rank = lora_rank,
gpu_memory_utilization = 0.5, # Reduce if out of memory gpu_memory_utilization = 0.5, # Reduce if out of memory
tokenizer = tokenizer,
) )
model = FastLanguageModel.get_peft_model( model = FastLanguageModel.get_peft_model(