MonteXiaofeng commited on
Commit
2f4f4e8
·
verified ·
1 Parent(s): 316fcce

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +78 -0
README.md CHANGED
@@ -29,3 +29,81 @@ The evaluation method is: use GPT4 on the validation set of each dataset to comp
29
 
30
 
31
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642f6c64f945a8a5c9ee5b5d/hvLm7eK0YImjWYbQE4ZCd.png)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
 
31
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642f6c64f945a8a5c9ee5b5d/hvLm7eK0YImjWYbQE4ZCd.png)
32
+
33
+ ## How to use
34
+
35
+ ```python
36
+ # !/usr/bin/env python
37
+ # -*- coding:utf-8 -*-
38
+ # ==================================================================
39
+ # [Author] : xiaofeng
40
+ # [Descriptions] :
41
+ # ==================================================================
42
+
43
+ from transformers import AutoTokenizer, AutoModelForCausalLM
44
+ import transformers
45
+ import torch
46
+
47
+
48
+ llama3_jinja = """{% if messages[0]['role'] == 'system' %}
49
+ {% set offset = 1 %}
50
+ {% else %}
51
+ {% set offset = 0 %}
52
+ {% endif %}
53
+
54
+ {{ bos_token }}
55
+ {% for message in messages %}
56
+ {% if (message['role'] == 'user') != (loop.index0 % 2 == offset) %}
57
+ {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
58
+ {% endif %}
59
+
60
+ {{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] | trim + '<|eot_id|>' }}
61
+ {% endfor %}
62
+
63
+ {% if add_generation_prompt %}
64
+ {{ '<|start_header_id|>' + 'assistant' + '<|end_header_id|>\n\n' }}
65
+ {% endif %}"""
66
+
67
+
68
+ dtype = torch.bfloat16
69
+
70
+ model_dir = "MonteXiaofeng/Tranport-llama3_1_8B_instruct"
71
+ model = AutoModelForCausalLM.from_pretrained(
72
+ model_dir,
73
+ device_map="cuda",
74
+ torch_dtype=dtype,
75
+ )
76
+
77
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
78
+ tokenizer.chat_template = llama3_jinja # update template
79
+
80
+ message = [
81
+ {"role": "system", "content": "You are a helpful assistant"},
82
+ {"role": "user", "content": "天气如何"},
83
+ ]
84
+ prompt = tokenizer.apply_chat_template(
85
+ message, tokenize=False, add_generation_prompt=True
86
+ )
87
+ print(prompt)
88
+ inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
89
+ prompt_length = len(inputs[0])
90
+ print(f"prompt_length:{prompt_length}")
91
+
92
+ generating_args = {
93
+ "do_sample": True,
94
+ "temperature": 1.0,
95
+ "top_p": 0.5,
96
+ "top_k": 15,
97
+ "max_new_tokens": 150,
98
+ }
99
+
100
+
101
+ generate_output = model.generate(input_ids=inputs.to(model.device), **generating_args)
102
+
103
+ response_ids = generate_output[:, prompt_length:]
104
+ response = tokenizer.batch_decode(
105
+ response_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
106
+ )
107
+ print(response)
108
+
109
+ ```