phongtran231 commited on
Commit
38ad2da
·
1 Parent(s): b421769

Create init.py

Browse files
Files changed (1) hide show
  1. init.py +40 -0
init.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
2
+ import torch
3
+
4
+ model_path = "vinai/PhoGPT-7B5-Instruct"
5
+
6
+ config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
7
+ config.init_device = "cuda"
8
+ # config.attn_config['attn_impl'] = 'triton' # Enable if "triton" installed!
9
+
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_path, config=config, torch_dtype=torch.bfloat16, trust_remote_code=True
12
+ )
13
+ # If your GPU does not support bfloat16:
14
+ # model = AutoModelForCausalLM.from_pretrained(model_path, config=config, torch_dtype=torch.float16, trust_remote_code=True)
15
+ model.eval()
16
+
17
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
18
+
19
+ PROMPT = "### Câu hỏi:\n{instruction}\n\n### Trả lời:"
20
+
21
+ input_prompt = PROMPT.format_map(
22
+ {"instruction": "Làm thế nào để cải thiện kỹ năng quản lý thời gian?"}
23
+ )
24
+
25
+ input_ids = tokenizer(input_prompt, return_tensors="pt")
26
+
27
+ outputs = model.generate(
28
+ inputs=input_ids["input_ids"].to("cuda"),
29
+ attention_mask=input_ids["attention_mask"].to("cuda"),
30
+ do_sample=True,
31
+ temperature=1.0,
32
+ top_k=50,
33
+ top_p=0.9,
34
+ max_new_tokens=1024,
35
+ eos_token_id=tokenizer.eos_token_id,
36
+ pad_token_id=tokenizer.pad_token_id
37
+ )
38
+
39
+ response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
40
+ response = response.split("### Trả lời:")[1]