kmamaroziqov commited on
Commit
c6e2548
·
verified ·
1 Parent(s): 569f809

Document verified concise four-sentence inference config

Browse files
Files changed (1) hide show
  1. README.md +66 -5
README.md CHANGED
@@ -52,11 +52,35 @@ included in this repository.
52
  ## Usage
53
 
54
  ```python
 
55
  import torch
56
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  model_id = "NeuronUz/qwen3.5-2b-fine-tuned"
59
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
 
60
 
61
  tokenizer = AutoTokenizer.from_pretrained(model_id)
62
  model = AutoModelForCausalLM.from_pretrained(
@@ -67,7 +91,14 @@ model = AutoModelForCausalLM.from_pretrained(
67
  )
68
 
69
  messages = [
70
- {"role": "system", "content": "Siz foydali AI yordamchisiz."},
 
 
 
 
 
 
 
71
  {"role": "user", "content": "O'zbekiston haqida qisqacha ma'lumot bering."},
72
  ]
73
 
@@ -78,15 +109,43 @@ inputs = tokenizer.apply_chat_template(
78
  return_dict=True,
79
  ).to(model.device)
80
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  with torch.inference_mode():
82
  output = model.generate(
83
  **inputs,
84
  max_new_tokens=256,
85
  do_sample=False,
 
 
 
 
 
86
  )
87
 
88
  prompt_length = inputs["input_ids"].shape[-1]
89
- print(tokenizer.decode(output[0][prompt_length:], skip_special_tokens=True))
 
 
 
 
 
 
 
 
 
 
 
90
  ```
91
 
92
  Use a recent Transformers release with Qwen3.5 support.
@@ -94,8 +153,10 @@ Use a recent Transformers release with Qwen3.5 support.
94
  When multiple GPUs are visible, avoid `device_map="auto"` with this checkpoint.
95
  Current Accelerate/Transformers releases may split the Qwen3.5 hybrid layers
96
  across GPUs and produce invalid text. Pin the complete model to one GPU as shown
97
- above. If sampling is desired, a tested starting point is `temperature=0.7`,
98
- `top_p=0.8`, and `top_k=20`.
 
 
99
 
100
  ## Limitations
101
 
 
52
  ## Usage
53
 
54
  ```python
55
+ import re
56
  import torch
57
+ from transformers import (
58
+ AutoModelForCausalLM,
59
+ AutoTokenizer,
60
+ StoppingCriteria,
61
+ StoppingCriteriaList,
62
+ )
63
+
64
+
65
+ class SentenceLimitCriteria(StoppingCriteria):
66
+ """Stop after a fixed number of complete generated sentences."""
67
+
68
+ def __init__(self, tokenizer, prompt_length, max_sentences=4):
69
+ self.tokenizer = tokenizer
70
+ self.prompt_length = prompt_length
71
+ self.max_sentences = max_sentences
72
+
73
+ def __call__(self, input_ids, scores, **kwargs):
74
+ generated = self.tokenizer.decode(
75
+ input_ids[0, self.prompt_length:], skip_special_tokens=True
76
+ )
77
+ endings = re.findall(r'[.!?](?:["\'’”)]*)?\s+', generated)
78
+ return len(endings) >= self.max_sentences
79
+
80
 
81
  model_id = "NeuronUz/qwen3.5-2b-fine-tuned"
82
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
83
+ max_sentences = 4
84
 
85
  tokenizer = AutoTokenizer.from_pretrained(model_id)
86
  model = AutoModelForCausalLM.from_pretrained(
 
91
  )
92
 
93
  messages = [
94
+ {
95
+ "role": "system",
96
+ "content": (
97
+ "Siz foydali AI yordamchisiz. Javoblarni qisqa va aniq yozing. "
98
+ "Agar foydalanuvchi batafsil javob so'ramasa, odatda 2-4 ta "
99
+ "to'liq gap bilan javob bering."
100
+ ),
101
+ },
102
  {"role": "user", "content": "O'zbekiston haqida qisqacha ma'lumot bering."},
103
  ]
104
 
 
109
  return_dict=True,
110
  ).to(model.device)
111
 
112
+ im_end_id = tokenizer.convert_tokens_to_ids("<|im_end|>")
113
+ eos_ids = [tokenizer.eos_token_id, im_end_id]
114
+ stopping_criteria = StoppingCriteriaList(
115
+ [
116
+ SentenceLimitCriteria(
117
+ tokenizer,
118
+ prompt_length=inputs["input_ids"].shape[-1],
119
+ max_sentences=max_sentences,
120
+ )
121
+ ]
122
+ )
123
+
124
  with torch.inference_mode():
125
  output = model.generate(
126
  **inputs,
127
  max_new_tokens=256,
128
  do_sample=False,
129
+ repetition_penalty=1.15,
130
+ no_repeat_ngram_size=3,
131
+ eos_token_id=eos_ids,
132
+ pad_token_id=tokenizer.eos_token_id,
133
+ stopping_criteria=stopping_criteria,
134
  )
135
 
136
  prompt_length = inputs["input_ids"].shape[-1]
137
+ reply = tokenizer.decode(
138
+ output[0][prompt_length:], skip_special_tokens=True
139
+ ).strip()
140
+
141
+ # A token can contain the final period and the start of the next word, so trim
142
+ # the displayed output back to the fourth complete sentence.
143
+ sentence_end_re = re.compile(r'[.!?](?:["\'’”)]*)?(?=\s|$)')
144
+ sentence_endings = list(sentence_end_re.finditer(reply))
145
+ if len(sentence_endings) >= max_sentences:
146
+ reply = reply[:sentence_endings[max_sentences - 1].end()].strip()
147
+
148
+ print(reply)
149
  ```
150
 
151
  Use a recent Transformers release with Qwen3.5 support.
 
153
  When multiple GPUs are visible, avoid `device_map="auto"` with this checkpoint.
154
  Current Accelerate/Transformers releases may split the Qwen3.5 hybrid layers
155
  across GPUs and produce invalid text. Pin the complete model to one GPU as shown
156
+ above. The example uses greedy decoding (`do_sample=False`, equivalent to
157
+ temperature 0 in the local chat script) and limits normal answers to four
158
+ complete sentences. If sampling is desired, a tested starting point is
159
+ `temperature=0.7`, `top_p=0.8`, and `top_k=20`.
160
 
161
  ## Limitations
162