Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers library installed.pip install transformers==4.48.3token=True in the pipeline and login to hugginface_hub by running1import huggingface_hub
2huggingface_hub.login(<ACCESS_TOKEN>)token in the pipeline1from transformers import pipeline
2
3generate_text = pipeline(
4 model="emelryan/qwen-14b-1m-remove-endturn",
5 torch_dtype="auto",
6 trust_remote_code=True,
7 device_map={"": "cuda:0"},
8 token=True,
9)
10
11# generate configuration can be modified to your needs
12# generate_text.model.generation_config.min_new_tokens = 2
13# generate_text.model.generation_config.max_new_tokens = 4096
14# generate_text.model.generation_config.do_sample = False
15# generate_text.model.generation_config.num_beams = 1
16# generate_text.model.generation_config.temperature = float(0.0)
17# generate_text.model.generation_config.repetition_penalty = float(1.0)
18
19messages = [
20 {
21 "role": "system",
22 "content": "You are a friendly and polite chatbot.",
23 },
24 {"role": "user", "content": "Hi, how are you?"},
25 {"role": "assistant", "content": "I'm doing great, how about you?"},
26 {"role": "user", "content": "Why is drinking water so healthy?"},
27]
28
29res = generate_text(
30 messages,
31 renormalize_logits=True
32)
33print(res[0]["generated_text"][-1]['content'])1print(generate_text.tokenizer.apply_chat_template(
2 messages,
3 tokenize=False,
4 add_generation_prompt=True,
5))1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "emelryan/qwen-14b-1m-remove-endturn" # either local folder or Hugging Face model name
4# Important: The prompt needs to be in the same format the model was trained with.
5# You can find an example prompt in the experiment logs.
6messages = [
7 {
8 "role": "system",
9 "content": "You are a friendly and polite chatbot.",
10 },
11 {"role": "user", "content": "Hi, how are you?"},
12 {"role": "assistant", "content": "I'm doing great, how about you?"},
13 {"role": "user", "content": "Why is drinking water so healthy?"},
14]
15
16tokenizer = AutoTokenizer.from_pretrained(
17 model_name,
18 trust_remote_code=True,
19)
20model = AutoModelForCausalLM.from_pretrained(
21 model_name,
22 torch_dtype="auto",
23 device_map={"": "cuda:0"},
24 trust_remote_code=True,
25)
26model.cuda().eval()
27
28# generate configuration can be modified to your needs
29# model.generation_config.min_new_tokens = 2
30# model.generation_config.max_new_tokens = 4096
31# model.generation_config.do_sample = False
32# model.generation_config.num_beams = 1
33# model.generation_config.temperature = float(0.0)
34# model.generation_config.repetition_penalty = float(1.0)
35
36inputs = tokenizer.apply_chat_template(
37 messages,
38 tokenize=True,
39 add_generation_prompt=True,
40 return_tensors="pt",
41 return_dict=True,
42).to("cuda")
43
44tokens = model.generate(
45 input_ids=inputs["input_ids"],
46 attention_mask=inputs["attention_mask"],
47 renormalize_logits=True
48)[0]
49
50tokens = tokens[inputs["input_ids"].shape[1]:]
51answer = tokenizer.decode(tokens, skip_special_tokens=True)
52print(answer)load_in_8bit=True or load_in_4bit=True. Also, sharding on multiple GPUs is possible by setting device_map=auto.Qwen2ForCausalLM(
(model): Qwen2Model(
(embed_tokens): Embedding(152064, 5120, padding_idx=151643)
(layers): ModuleList(
(0-47): 48 x Qwen2DecoderLayer(
(self_attn): Qwen2Attention(
(q_proj): Linear(in_features=5120, out_features=5120, bias=True)
(k_proj): Linear(in_features=5120, out_features=1024, bias=True)
(v_proj): Linear(in_features=5120, out_features=1024, bias=True)
(o_proj): Linear(in_features=5120, out_features=5120, bias=False)
)
(mlp): Qwen2MLP(
(gate_proj): Linear(in_features=5120, out_features=13824, bias=False)
(up_proj): Linear(in_features=5120, out_features=13824, bias=False)
(down_proj): Linear(in_features=13824, out_features=5120, bias=False)
(act_fn): SiLU()
)
(input_layernorm): Qwen2RMSNorm((5120,), eps=1e-05)
(post_attention_layernorm): Qwen2RMSNorm((5120,), eps=1e-05)
)
)
(norm): Qwen2RMSNorm((5120,), eps=1e-05)
(rotary_emb): Qwen2RotaryEmbedding()
)
(lm_head): Linear(in_features=5120, out_features=152064, bias=False)
)