Views
No views yet
transformers library on a machine with GPUs, first make sure you have the transformers library installed.pip install transformers==4.38.2token=True in the pipeline and login to hugginface_hub by running
python import huggingface_hub huggingface_hub.login(<ACCESS_TOKEN>)
- Or directly pass your <ACCESS_TOKEN> to token in the pipeline1from transformers import pipeline
2
3generate_text = pipeline(
4 model="borggAI/ginger-limpet",
5 torch_dtype="auto",
6 trust_remote_code=True,
7 use_fast=True,
8 device_map={"": "cuda:0"},
9 token=True,
10)
11
12# generate configuration can be modified to your needs
13# generate_text.model.generation_config.min_new_tokens = 2
14# generate_text.model.generation_config.max_new_tokens = 256
15# generate_text.model.generation_config.do_sample = True
16# generate_text.model.generation_config.num_beams = 1
17# generate_text.model.generation_config.temperature = float(0.5)
18# generate_text.model.generation_config.repetition_penalty = float(1.0)
19
20res = generate_text(
21 "Why is drinking water so healthy?",
22 renormalize_logits=True
23)
24print(res[0]["generated_text"])print(generate_text.preprocess("Why is drinking water so healthy?")["prompt_text"])Why is drinking water so healthy?</s>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 "borggAI/ginger-limpet",
6 use_fast=True,
7 padding_side="left",
8 trust_remote_code=True,
9)
10model = AutoModelForCausalLM.from_pretrained(
11 "borggAI/ginger-limpet",
12 torch_dtype="auto",
13 device_map={"": "cuda:0"},
14 trust_remote_code=True,
15)
16generate_text = H2OTextGenerationPipeline(model=model, tokenizer=tokenizer)
17
18# generate configuration can be modified to your needs
19# generate_text.model.generation_config.min_new_tokens = 2
20# generate_text.model.generation_config.max_new_tokens = 256
21# generate_text.model.generation_config.do_sample = True
22# generate_text.model.generation_config.num_beams = 1
23# generate_text.model.generation_config.temperature = float(0.5)
24# generate_text.model.generation_config.repetition_penalty = float(1.0)
25
26res = generate_text(
27 "Why is drinking water so healthy?",
28 renormalize_logits=True
29)
30print(res[0]["generated_text"])1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "borggAI/ginger-limpet" # 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 = "How are you?</s>"
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
23# model.generation_config.min_new_tokens = 2
24# model.generation_config.max_new_tokens = 256
25# model.generation_config.do_sample = True
26# model.generation_config.num_beams = 1
27# model.generation_config.temperature = float(0.5)
28# model.generation_config.repetition_penalty = float(1.0)
29
30tokens = model.generate(
31 input_ids=inputs["input_ids"],
32 attention_mask=inputs["attention_mask"],
33 renormalize_logits=True
34)[0]
35
36tokens = tokens[inputs["input_ids"].shape[1]:]
37answer = tokenizer.decode(tokens, skip_special_tokens=True)
38print(answer)load_in_8bit=True or load_in_4bit=True. Also, sharding on multiple GPUs is possible by setting device_map=auto.MistralForCausalLM(
(model): MistralModel(
(embed_tokens): Embedding(32000, 4096, padding_idx=0)
(layers): ModuleList(
(0-31): 32 x MistralDecoderLayer(
(self_attn): MistralSdpaAttention(
(q_proj): Linear(in_features=4096, out_features=4096, bias=False)
(k_proj): Linear(in_features=4096, out_features=1024, bias=False)
(v_proj): Linear(in_features=4096, out_features=1024, bias=False)
(o_proj): Linear(in_features=4096, out_features=4096, bias=False)
(rotary_emb): MistralRotaryEmbedding()
)
(mlp): MistralMLP(
(gate_proj): Linear(in_features=4096, out_features=14336, bias=False)
(up_proj): Linear(in_features=4096, out_features=14336, bias=False)
(down_proj): Linear(in_features=14336, out_features=4096, bias=False)
(act_fn): SiLU()
)
(input_layernorm): MistralRMSNorm()
(post_attention_layernorm): MistralRMSNorm()
)
)
(norm): MistralRMSNorm()
)
(lm_head): Linear(in_features=4096, out_features=32000, bias=False)
)