Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers library installed.pip install transformers==4.31.0token=True in the pipeline and login to hugginface_hub by running
python import huggingface_hub huggingface_hub.login(<ACCES_TOKEN>)
- Or directly pass your <ACCES_TOKEN> to token in the pipeline1from transformers import pipeline
2
3generate_text = pipeline(
4 model="overenginar/gpt2-oasst",
5 torch_dtype="auto",
6 trust_remote_code=True,
7 use_fast=True,
8 device_map={"": "cuda:0"},
9 token=True,
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.3),
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.1from h2oai_pipeline import H2OTextGenerationPipeline
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4tokenizer = AutoTokenizer.from_pretrained(
5 "overenginar/gpt2-oasst",
6 use_fast=True,
7 padding_side="left",
8 trust_remote_code=True,
9)
10model = AutoModelForCausalLM.from_pretrained(
11 "overenginar/gpt2-oasst",
12 torch_dtype="auto",
13 device_map={"": "cuda:0"},
14 trust_remote_code=True,
15)
16generate_text = H2OTextGenerationPipeline(model=model, tokenizer=tokenizer)
17
18res = generate_text(
19 "Why is drinking water so healthy?",
20 min_new_tokens=2,
21 max_new_tokens=256,
22 do_sample=False,
23 num_beams=1,
24 temperature=float(0.3),
25 repetition_penalty=float(1.2),
26 renormalize_logits=True
27)
28print(res[0]["generated_text"])1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "overenginar/gpt2-oasst" # 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.3),
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.GPT2LMHeadModel(
(transformer): GPT2Model(
(wte): Embedding(50257, 768)
(wpe): Embedding(1024, 768)
(drop): Dropout(p=0.1, inplace=False)
(h): ModuleList(
(0-11): 12 x GPT2Block(
(ln_1): LayerNorm((768,), eps=1e-05, elementwise_affine=True)
(attn): GPT2Attention(
(c_attn): Conv1D()
(c_proj): Conv1D()
(attn_dropout): Dropout(p=0.1, inplace=False)
(resid_dropout): Dropout(p=0.1, inplace=False)
)
(ln_2): LayerNorm((768,), eps=1e-05, elementwise_affine=True)
(mlp): GPT2MLP(
(c_fc): Conv1D()
(c_proj): Conv1D()
(act): NewGELUActivation()
(dropout): Dropout(p=0.1, inplace=False)
)
)
)
(ln_f): LayerNorm((768,), eps=1e-05, elementwise_affine=True)
)
(lm_head): Linear(in_features=768, out_features=50257, bias=False)
)