Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers library installed.pip install transformers==4.40.2token=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="jofaichow/z7b-beta-data-v3",
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 = 1024
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 = "jofaichow/z7b-beta-data-v3" # either local folder or huggingface 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 = 1024
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.MistralForCausalLM(
(model): MistralModel(
(embed_tokens): Embedding(32000, 4096, padding_idx=2)
(layers): ModuleList(
(0-31): 32 x MistralDecoderLayer(
(self_attn): MistralSdpaAttention(
(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)
(rotary_emb): MistralRotaryEmbedding()
)
(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()
(post_attention_layernorm): MistralRMSNorm()
)
)
(norm): MistralRMSNorm()
)
(lm_head): Linear(in_features=4096, out_features=32000, bias=False)
)