Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
```python
|
2 |
+
!pip install -q -U trl transformers accelerate git+https://github.com/huggingface/peft.git
|
3 |
+
!pip install -q datasets bitsandbytes einops wandb sentencepiece transformers_stream_generator tiktoken
|
4 |
+
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
+
import torch
|
7 |
+
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("TinyPixel/qwen-1.8B-guanaco", trust_remote_code=True)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained("TinyPixel/qwen-1.8B-guanaco", torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)
|
10 |
+
|
11 |
+
device = "cuda:0"
|
12 |
+
|
13 |
+
from transformers import StoppingCriteria, StoppingCriteriaList
|
14 |
+
|
15 |
+
stop_token_ids = [[14374, 11097, 25], [14374, 21388, 25]]
|
16 |
+
stop_token_ids = [torch.LongTensor(x).to(device) for x in stop_token_ids]
|
17 |
+
|
18 |
+
from transformers import StoppingCriteria, StoppingCriteriaList
|
19 |
+
|
20 |
+
class StopOnTokens(StoppingCriteria):
|
21 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
22 |
+
for stop_ids in stop_token_ids:
|
23 |
+
if torch.eq(input_ids[0][-len(stop_ids):], stop_ids).all():
|
24 |
+
return True
|
25 |
+
return False
|
26 |
+
|
27 |
+
stopping_criteria = StoppingCriteriaList([StopOnTokens()])
|
28 |
+
|
29 |
+
text = '''### Human: what is the difference between a dog and a cat on a biological level?
|
30 |
+
### Assistant:'''
|
31 |
+
|
32 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
33 |
+
outputs = model.generate(**inputs,
|
34 |
+
max_new_tokens=512,
|
35 |
+
stopping_criteria=stopping_criteria,
|
36 |
+
do_sample=True,
|
37 |
+
top_p=0.95,
|
38 |
+
temperature=0.7,
|
39 |
+
top_k=50)
|
40 |
+
|
41 |
+
print(tokenizer.decode(outputs[0], skip_special_tokens=False)
|
42 |
+
```
|