Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2device = "cuda" # the device to load the model onto
3
4model = AutoModelForCausalLM.from_pretrained(
5 "Hack337/WavGPT-2.5",
6 torch_dtype="auto",
7 device_map="auto"
8)
9tokenizer = AutoTokenizer.from_pretrained("Hack337/WavGPT-2.5")
10
11prompt = "Give me a short introduction to large language model."
12messages = [
13 {"role": "system", "content": "Вы очень полезный помощник."},
14 {"role": "user", "content": prompt}
15]
16text = tokenizer.apply_chat_template(
17 messages,
18 tokenize=False,
19 add_generation_prompt=True
20)
21model_inputs = tokenizer([text], return_tensors="pt").to(device)
22
23generated_ids = model.generate(
24 model_inputs.input_ids,
25 max_new_tokens=512
26)
27generated_ids = [
28 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
29]
30
31response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
321from transformers import AutoTokenizer, TextStreamer
2from intel_npu_acceleration_library import NPUModelForCausalLM
3import torch
4
5# Load the NPU-optimized model without LoRA
6model = NPUModelForCausalLM.from_pretrained(
7 "Hack337/WavGPT-2.5",
8 use_cache=True,
9 dtype=torch.float16 # Use float16 for the NPU
10).eval()
11
12# Load the tokenizer
13tokenizer = AutoTokenizer.from_pretrained("Hack337/WavGPT-2.5")
14tokenizer.pad_token_id = tokenizer.eos_token_id
15streamer = TextStreamer(tokenizer, skip_special_tokens=True)
16
17# Prompt handling
18prompt = "Give me a short introduction to large language model."
19messages = [
20 {"role": "system", "content": "Вы очень полезный помощник."},
21 {"role": "user", "content": prompt}
22]
23
24# Convert to a text format compatible with the model
25text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
26prefix = tokenizer([text], return_tensors="pt")["input_ids"].to("npu")
27
28# Generation configuration
29generation_kwargs = dict(
30 input_ids=prefix,
31 streamer=streamer,
32 do_sample=True,
33 top_k=50,
34 top_p=0.9,
35 max_new_tokens=512,
36)
37
38# Run inference on the NPU
39print("Run inference")
40_ = model.generate(**generation_kwargs)
41