Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers library installed.pip install transformers==4.56.1token=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="wigyo/AFP-ROUTE-3-5",
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 = 256
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 {"role": "user", "content": "Hi, how are you?"},
21 {"role": "assistant", "content": "I'm doing great, how about you?"},
22 {"role": "user", "content": "Why is drinking water so healthy?"},
23]
24
25res = generate_text(
26 messages,
27 renormalize_logits=True
28)
29print(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 = "wigyo/AFP-ROUTE-3-5" # 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 {"role": "user", "content": "Hi, how are you?"},
8 {"role": "assistant", "content": "I'm doing great, how about you?"},
9 {"role": "user", "content": "Why is drinking water so healthy?"},
10]
11
12tokenizer = AutoTokenizer.from_pretrained(
13 model_name,
14 trust_remote_code=True,
15)
16model = AutoModelForCausalLM.from_pretrained(
17 model_name,
18 torch_dtype="auto",
19 device_map={"": "cuda:0"},
20 trust_remote_code=True,
21)
22model.cuda().eval()
23
24# generate configuration can be modified to your needs
25# model.generation_config.min_new_tokens = 2
26# model.generation_config.max_new_tokens = 256
27# model.generation_config.do_sample = False
28# model.generation_config.num_beams = 1
29# model.generation_config.temperature = float(0.0)
30# model.generation_config.repetition_penalty = float(1.0)
31
32inputs = tokenizer.apply_chat_template(
33 messages,
34 tokenize=True,
35 add_generation_prompt=True,
36 return_tensors="pt",
37 return_dict=True,
38).to("cuda")
39
40tokens = model.generate(
41 input_ids=inputs["input_ids"],
42 attention_mask=inputs["attention_mask"],
43 renormalize_logits=True
44)[0]
45
46tokens = tokens[inputs["input_ids"].shape[1]:]
47answer = tokenizer.decode(tokens, skip_special_tokens=True)
48print(answer)load_in_8bit=True or load_in_4bit=True. Also, sharding on multiple GPUs is possible by setting device_map=auto.MistralForCausalLM(
(model): MistralModel(
(embed_tokens): Embedding(32000, 4096, padding_idx=0)
(layers): ModuleList(
(0-31): 32 x MistralDecoderLayer(
(self_attn): MistralAttention(
(q_proj): Linear(in_features=4096, out_features=4096, bias=False)
(k_proj): Linear(in_features=4096, out_features=1024, bias=False)
(v_proj): Linear(in_features=4096, out_features=1024, bias=False)
(o_proj): Linear(in_features=4096, out_features=4096, bias=False)
)
(mlp): MistralMLP(
(gate_proj): Linear(in_features=4096, out_features=14336, bias=False)
(up_proj): Linear(in_features=4096, out_features=14336, bias=False)
(down_proj): Linear(in_features=14336, out_features=4096, bias=False)
(act_fn): SiLU()
)
(input_layernorm): MistralRMSNorm((4096,), eps=1e-05)
(post_attention_layernorm): MistralRMSNorm((4096,), eps=1e-05)
)
)
(norm): MistralRMSNorm((4096,), eps=1e-05)
(rotary_emb): MistralRotaryEmbedding()
)
(lm_head): Linear(in_features=4096, out_features=32000, bias=False)
)