Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers and torch libraries installed.1pip install transformers==4.28.1
2pip install torch==2.0.01import torch
2from transformers import pipeline
3
4generate_text = pipeline(
5 model="ali1627/yugioh_training",
6 torch_dtype=torch.float16,
7 trust_remote_code=True,
8 device_map={"": "cuda:0"},
9)
10
11res = generate_text(
12 "Why is drinking water so healthy?",
13 min_new_tokens=2,
14 max_new_tokens=256,
15 do_sample=False,
16 num_beams=2,
17 temperature=float(0.3),
18 repetition_penalty=float(1.2),
19)
20print(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|>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 "ali1627/yugioh_training",
7 padding_side="left"
8)
9model = AutoModelForCausalLM.from_pretrained(
10 "ali1627/yugioh_training",
11 torch_dtype=torch.float16,
12 device_map={"": "cuda:0"}
13)
14generate_text = H2OTextGenerationPipeline(model=model, tokenizer=tokenizer)
15
16res = generate_text(
17 "Why is drinking water so healthy?",
18 min_new_tokens=2,
19 max_new_tokens=256,
20 do_sample=False,
21 num_beams=2,
22 temperature=float(0.3),
23 repetition_penalty=float(1.2),
24)
25print(res[0]["generated_text"])1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3
4model_name = "ali1627/yugioh_training" # 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(model_name)
10model = AutoModelForCausalLM.from_pretrained(model_name)
11model.cuda().eval()
12inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
13
14# generate configuration can be modified to your needs
15tokens = model.generate(
16 **inputs,
17 min_new_tokens=2,
18 max_new_tokens=256,
19 do_sample=False,
20 num_beams=2,
21 temperature=float(0.3),
22 repetition_penalty=float(1.2),
23)[0]
24
25tokens = tokens[inputs["input_ids"].shape[1]:]
26answer = tokenizer.decode(tokens, skip_special_tokens=True)
27print(answer)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)
)CUDA_VISIBLE_DEVICES=0 python main.py --model hf-causal-experimental --model_args pretrained=ali1627/yugioh_training --tasks openbookqa,arc_easy,winogrande,hellaswag,arc_challenge,piqa,boolq --device cuda &> eval.log