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.28.1
2pip install accelerate==0.18.0
3pip install torch==2.0.01import torch
2from transformers import pipeline
3
4generate_text = pipeline(
5 model="h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-7b-preview-700bt",
6 torch_dtype=torch.float16,
7 trust_remote_code=True,
8 use_fast=False,
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=1024,
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?</s><|answer|>trust_remote_code=True you can download h2oai_pipeline.py, store it alongside your notebook, and construct the pipeline yourself from the loaded model and tokenizer:1import torch
2from h2oai_pipeline import H2OTextGenerationPipeline
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5tokenizer = AutoTokenizer.from_pretrained(
6 "h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-7b-preview-700bt",
7 use_fast=False,
8 padding_side="left"
9)
10model = AutoModelForCausalLM.from_pretrained(
11 "h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-7b-preview-700bt",
12 torch_dtype=torch.float16,
13 device_map={"": "cuda:0"}
14)
15generate_text = H2OTextGenerationPipeline(model=model, tokenizer=tokenizer)
16
17res = generate_text(
18 "Why is drinking water so healthy?",
19 min_new_tokens=2,
20 max_new_tokens=1024,
21 do_sample=False,
22 num_beams=1,
23 temperature=float(0.3),
24 repetition_penalty=float(1.2),
25 renormalize_logits=True
26)
27print(res[0]["generated_text"])1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-7b-preview-700bt" # 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?</s><|answer|>"
7
8tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
9model = AutoModelForCausalLM.from_pretrained(model_name)
10model.cuda().eval()
11inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
12
13# generate configuration can be modified to your needs
14tokens = model.generate(
15 **inputs,
16 min_new_tokens=2,
17 max_new_tokens=1024,
18 do_sample=False,
19 num_beams=1,
20 temperature=float(0.3),
21 repetition_penalty=float(1.2),
22 renormalize_logits=True
23)[0]
24
25tokens = tokens[inputs["input_ids"].shape[1]:]
26answer = tokenizer.decode(tokens, skip_special_tokens=True)
27print(answer)LlamaForCausalLM(
(model): LlamaModel(
(embed_tokens): Embedding(32000, 4096, padding_idx=0)
(layers): ModuleList(
(0-31): 32 x LlamaDecoderLayer(
(self_attn): LlamaAttention(
(q_proj): Linear(in_features=4096, out_features=4096, bias=False)
(k_proj): Linear(in_features=4096, out_features=4096, bias=False)
(v_proj): Linear(in_features=4096, out_features=4096, bias=False)
(o_proj): Linear(in_features=4096, out_features=4096, bias=False)
(rotary_emb): LlamaRotaryEmbedding()
)
(mlp): LlamaMLP(
(gate_proj): Linear(in_features=4096, out_features=11008, bias=False)
(down_proj): Linear(in_features=11008, out_features=4096, bias=False)
(up_proj): Linear(in_features=4096, out_features=11008, bias=False)
(act_fn): SiLUActivation()
)
(input_layernorm): LlamaRMSNorm()
(post_attention_layernorm): LlamaRMSNorm()
)
)
(norm): LlamaRMSNorm()
)
(lm_head): Linear(in_features=4096, out_features=32000, bias=False)
)CUDA_VISIBLE_DEVICES=0 python main.py --model hf-causal-experimental --model_args pretrained=h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-7b-preview-700bt --tasks openbookqa,arc_easy,winogrande,hellaswag,arc_challenge,piqa,boolq --device cuda &> eval.log