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.30.2
2pip install einops==0.6.1
3pip install accelerate==0.20.3
4pip install torch==2.0.01import torch
2from transformers import pipeline
3
4generate_text = pipeline(
5 model="shashank-mugiwara/thor",
6 torch_dtype="auto",
7 trust_remote_code=True,
8 use_fast=True,
9 device_map={"": "cuda:0"},
10)
11
12res = generate_text(
13 "What is thor service?",
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("What is thor service?")["prompt_text"])1import torch
2from h2oai_pipeline import H2OTextGenerationPipeline
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5tokenizer = AutoTokenizer.from_pretrained(
6 "shashank-mugiwara/thor",
7 use_fast=True,
8 padding_side="left",
9 trust_remote_code=True,
10)
11model = AutoModelForCausalLM.from_pretrained(
12 "shashank-mugiwara/thor",
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.3),
26 repetition_penalty=float(1.2),
27 renormalize_logits=True
28)
29print(res[0]["generated_text"])1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "shashank-mugiwara/thor" # either local folder or huggingface model name
4prompt = "<|prompt|>What is thor service?</s><|answer|>"
5
6tokenizer = AutoTokenizer.from_pretrained(
7 model_name,
8 use_fast=True,
9 trust_remote_code=True,
10)
11model = AutoModelForCausalLM.from_pretrained(
12 model_name,
13 torch_dtype="auto",
14 device_map={"": "cuda:0"},
15 trust_remote_code=True,
16)
17model.cuda().eval()
18inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
19
20# generate configuration can be modified to your needs
21tokens = model.generate(
22 input_ids=inputs["input_ids"],
23 attention_mask=inputs["attention_mask"],
24 min_new_tokens=2,
25 max_new_tokens=256,
26 do_sample=False,
27 num_beams=1,
28 temperature=float(0.3),
29 repetition_penalty=float(1.2),
30 renormalize_logits=True
31)[0]
32
33tokens = tokens[inputs["input_ids"].shape[1]:]
34answer = tokenizer.decode(tokens, skip_special_tokens=True)
35print(answer)load_in_8bit=True or load_in_4bit=True. Also, sharding on multiple GPUs is possible by setting device_map=auto.