Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers, accelerate and torch libraries installed.1pip install transformers==4.29.2
2pip install einops==0.6.1
3pip install accelerate==0.19.0
4pip install torch==2.0.01import torch
2from transformers import pipeline
3
4generate_text = pipeline(
5 model="Shishir1807/CT_M2",
6 torch_dtype="auto",
7 trust_remote_code=True,
8 use_fast=True,
9 device_map={"": "cuda:0"},
10)
11
12res = generate_text(
13 "Why is drinking water so healthy?",
14 min_new_tokens=2,
15 max_new_tokens=256,
16 do_sample=False,
17 num_beams=1,
18 temperature=float(0.0),
19 repetition_penalty=float(1.2),
20 renormalize_logits=True
21)
22print(res[0]["generated_text"])print(generate_text.preprocess("Why is drinking water so healthy?")["prompt_text"])<|prompt|>Why is drinking water so healthy?<|endoftext|><|answer|>transformers package, this will allow you to set trust_remote_code=False.1import torch
2from h2oai_pipeline import H2OTextGenerationPipeline
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5tokenizer = AutoTokenizer.from_pretrained(
6 "Shishir1807/CT_M2",
7 use_fast=True,
8 padding_side="left",
9 trust_remote_code=True,
10)
11model = AutoModelForCausalLM.from_pretrained(
12 "Shishir1807/CT_M2",
13 torch_dtype="auto",
14 device_map={"": "cuda:0"},
15 trust_remote_code=True,
16)
17generate_text = H2OTextGenerationPipeline(model=model, tokenizer=tokenizer)
18
19res = generate_text(
20 "Why is drinking water so healthy?",
21 min_new_tokens=2,
22 max_new_tokens=256,
23 do_sample=False,
24 num_beams=1,
25 temperature=float(0.0),
26 repetition_penalty=float(1.2),
27 renormalize_logits=True
28)
29print(res[0]["generated_text"])1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "Shishir1807/CT_M2" # 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.
6prompt = "<|prompt|>How are you?<|endoftext|><|answer|>"
7
8tokenizer = AutoTokenizer.from_pretrained(
9 model_name,
10 use_fast=True,
11 trust_remote_code=True,
12)
13model = AutoModelForCausalLM.from_pretrained(
14 model_name,
15 torch_dtype="auto",
16 device_map={"": "cuda:0"},
17 trust_remote_code=True,
18)
19model.cuda().eval()
20inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
21
22# generate configuration can be modified to your needs
23tokens = model.generate(
24 input_ids=inputs["input_ids"],
25 attention_mask=inputs["attention_mask"],
26 min_new_tokens=2,
27 max_new_tokens=256,
28 do_sample=False,
29 num_beams=1,
30 temperature=float(0.0),
31 repetition_penalty=float(1.2),
32 renormalize_logits=True
33)[0]
34
35tokens = tokens[inputs["input_ids"].shape[1]:]
36answer = tokenizer.decode(tokens, skip_special_tokens=True)
37print(answer)load_in_8bit=True or load_in_4bit=True. Also, sharding on multiple GPUs is possible by setting device_map=auto.GPTNeoXForCausalLM(
(gpt_neox): GPTNeoXModel(
(embed_in): Embedding(50304, 2560)
(layers): ModuleList(
(0-31): 32 x GPTNeoXLayer(
(input_layernorm): LayerNorm((2560,), eps=1e-05, elementwise_affine=True)
(post_attention_layernorm): LayerNorm((2560,), eps=1e-05, elementwise_affine=True)
(attention): GPTNeoXAttention(
(rotary_emb): RotaryEmbedding()
(query_key_value): Linear(in_features=2560, out_features=7680, bias=True)
(dense): Linear(in_features=2560, out_features=2560, bias=True)
)
(mlp): GPTNeoXMLP(
(dense_h_to_4h): Linear(in_features=2560, out_features=10240, bias=True)
(dense_4h_to_h): Linear(in_features=10240, out_features=2560, bias=True)
(act): GELUActivation()
)
)
)
(final_layer_norm): LayerNorm((2560,), eps=1e-05, elementwise_affine=True)
)
(embed_out): Linear(in_features=2560, out_features=50304, bias=False)
)