Views
No views yet
meta-llama/Meta-Llama-3-8B-Instruct model that finetuned on Japanese conversation dataset.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 running1import huggingface_hub
2huggingface_hub.login(<ACCESS_TOKEN>)token in the pipeline1from transformers import pipeline
2
3generate_text = pipeline(
4 model="haqishen/h2o-Llama-3-8B-Japanese-Instruct",
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 = False
16# generate_text.model.generation_config.num_beams = 1
17# generate_text.model.generation_config.temperature = float(0.0)
18# generate_text.model.generation_config.repetition_penalty = float(1.0)
19
20messages = [
21 {"role": "system", "content": "あなたは、常に海賊の言葉で返事する海賊チャットボットです!"},
22 {"role": "user", "content": "自己紹介してください"},
23]
24
25res = generate_text(
26 messages,
27 renormalize_logits=True
28)
29print(res[0]["generated_text"][-1]['content'])1print(generate_text.tokenizer.apply_chat_template(
2 messages,
3 tokenize=False,
4 add_generation_prompt=True,
5))1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "haqishen/h2o-Llama-3-8B-Japanese-Instruct" # 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.
6messages = [
7 {"role": "system", "content": "あなたは、常に海賊の言葉で返事する海賊チャットボットです!"},
8 {"role": "user", "content": "自己紹介してください"},
9]
10
11tokenizer = AutoTokenizer.from_pretrained(
12 model_name,
13 use_fast=True,
14 trust_remote_code=True,
15)
16model = AutoModelForCausalLM.from_pretrained(
17 model_name,
18 torch_dtype="auto",
19 device_map={"": "cuda:0"},
20 trust_remote_code=True,
21)
22model.cuda().eval()
23
24# generate configuration can be modified to your needs
25# model.generation_config.min_new_tokens = 2
26# model.generation_config.max_new_tokens = 256
27# model.generation_config.do_sample = False
28# model.generation_config.num_beams = 1
29# model.generation_config.temperature = float(0.0)
30# model.generation_config.repetition_penalty = float(1.0)
31
32inputs = tokenizer.apply_chat_template(
33 messages,
34 tokenize=True,
35 add_generation_prompt=True,
36 return_tensors="pt",
37 return_dict=True,
38).to("cuda")
39
40tokens = model.generate(
41 input_ids=inputs["input_ids"],
42 attention_mask=inputs["attention_mask"],
43 renormalize_logits=True
44)[0]
45
46tokens = tokens[inputs["input_ids"].shape[1]:]
47answer = tokenizer.decode(tokens, skip_special_tokens=True)
48print(answer)1from vllm import LLM, SamplingParams
2model_id = "haqishen/h2o-Llama-3-8B-Japanese-Instruct"
3llm = LLM(
4 model=model_id,
5 trust_remote_code=True,
6 tensor_parallel_size=2,
7)
8tokenizer = llm.get_tokenizer()
9messages = [
10 {"role": "system", "content": "あなたは、常に海賊の言葉で返事する海賊チャットボットです!"},
11 {"role": "user", "content": "自己紹介してください"},
12]
13conversations = tokenizer.apply_chat_template(
14 messages,
15 tokenize=False,
16 add_generation_prompt=True
17)
18outputs = llm.generate(
19 [conversations],
20 SamplingParams(
21 temperature=0.6,
22 top_p=0.9,
23 max_tokens=1024,
24 stop_token_ids=[tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>")],
25 )
26)
27print(outputs[0].outputs[0].text.strip())load_in_8bit=True or load_in_4bit=True. Also, sharding on multiple GPUs is possible by setting device_map=auto.LlamaForCausalLM(
(model): LlamaModel(
(embed_tokens): Embedding(128256, 4096, padding_idx=128001)
(layers): ModuleList(
(0-31): 32 x LlamaDecoderLayer(
(self_attn): LlamaSdpaAttention(
(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): LlamaRotaryEmbedding()
)
(mlp): LlamaMLP(
(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): LlamaRMSNorm()
(post_attention_layernorm): LlamaRMSNorm()
)
)
(norm): LlamaRMSNorm()
)
(lm_head): Linear(in_features=4096, out_features=128256, bias=False)
)