Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers, accelerate, torch and einops libraries installed.1pip install transformers==4.29.2
2pip install accelerate==0.19.0
3pip install torch==2.0.0
4pip install einops==0.6.11import torch
2from transformers import AutoTokenizer, pipeline
3
4
5tokenizer = AutoTokenizer.from_pretrained(
6 "h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3",
7 use_fast=False,
8 padding_side="left",
9 trust_remote_code=True,
10)
11
12generate_text = pipeline(
13 model="h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3",
14 tokenizer=tokenizer,
15 torch_dtype=torch.float16,
16 trust_remote_code=True,
17 use_fast=False,
18 device_map={"": "cuda:0"},
19)
20
21res = generate_text(
22 "Why is drinking water so healthy?",
23 min_new_tokens=2,
24 max_new_tokens=1024,
25 do_sample=False,
26 num_beams=1,
27 temperature=float(0.3),
28 repetition_penalty=float(1.2),
29 renormalize_logits=True
30)
31print(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|>1import torch
2from h2oai_pipeline import H2OTextGenerationPipeline
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5tokenizer = AutoTokenizer.from_pretrained(
6 "h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3",
7 use_fast=False,
8 padding_side="left",
9 trust_remote_code=True,
10)
11model = AutoModelForCausalLM.from_pretrained(
12 "h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3",
13 torch_dtype=torch.float16,
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=1024,
23 do_sample=False,
24 num_beams=1,
25 temperature=float(0.3),
26 repetition_penalty=float(1.2),
27 renormalize_logits=True
28)
29print(res[0]["generated_text"])1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_name = "h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3" # either local folder or huggingface model name
5# Important: The prompt needs to be in the same format the model was trained with.
6# You can find an example prompt in the experiment logs.
7prompt = "<|prompt|>How are you?<|endoftext|><|answer|>"
8
9tokenizer = AutoTokenizer.from_pretrained(
10 model_name,
11 use_fast=False,
12 trust_remote_code=True,
13)
14model = AutoModelForCausalLM.from_pretrained(
15 model_name,
16 torch_dtype=torch.float16,
17 device_map={"": "cuda:0"},
18 trust_remote_code=True,
19)
20model.cuda().eval()
21inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
22
23# generate configuration can be modified to your needs
24tokens = model.generate(
25 **inputs,
26 min_new_tokens=2,
27 max_new_tokens=1024,
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)RWForCausalLM(
(transformer): RWModel(
(word_embeddings): Embedding(65024, 4544)
(h): ModuleList(
(0-31): 32 x DecoderLayer(
(input_layernorm): LayerNorm((4544,), eps=1e-05, elementwise_affine=True)
(self_attention): Attention(
(maybe_rotary): RotaryEmbedding()
(query_key_value): Linear(in_features=4544, out_features=4672, bias=False)
(dense): Linear(in_features=4544, out_features=4544, bias=False)
(attention_dropout): Dropout(p=0.0, inplace=False)
)
(mlp): MLP(
(dense_h_to_4h): Linear(in_features=4544, out_features=18176, bias=False)
(act): GELU(approximate='none')
(dense_4h_to_h): Linear(in_features=18176, out_features=4544, bias=False)
)
)
)
(ln_f): LayerNorm((4544,), eps=1e-05, elementwise_affine=True)
)
(lm_head): Linear(in_features=4544, out_features=65024, bias=False)
)