Views
No views yet
1!pip uninstall unsloth -y
2!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
3!pip install --upgrade torch
4!pip install --upgrade xformers
5
6# import necessary libraries
7from unsloth import FastLanguageModel
8from peft import PeftModel
9import torch
10import json
11from tqdm import tqdm
12
13# mount your Google Drive
14from google.colab import drive
15drive.mount('/content/drive')
16%cd /content/drive/MyDrive/directory_name
17
18# install Flash Attention 2 for softcapping support
19import torch
20if torch.cuda.get_device_capability()[0] >= 8:
21 !pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
22
23# get Hugging Face token
24# register your Hugging Face token to the secrets in advance
25from google.colab import userdata
26HF_TOKEN=userdata.get('HF_TOKEN')
27
28# merge adapter with base model
29model_id = "llm-jp/llm-jp-3-13b"
30adapter_id = "koi777/llm-jp-3-13b_20241216_3"
31dtype = torch.bfloat16
32load_in_4bit = True # set True in order to hundle 13B model
33
34model, tokenizer = FastLanguageModel.from_pretrained(
35 model_name=model_id,
36 dtype=dtype,
37 load_in_4bit=load_in_4bit,
38 trust_remote_code=True,
39)
40model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
41
42# load test dataset
43dataset_TV = []
44with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
45 item = ""
46 for line in f:
47 line = line.strip()
48 item += line
49 if item.endswith("}"):
50 dataset_TV.append(json.loads(item))
51 item = ""
52
53# start inference
54from tqdm import tqdm
55
56# change to inference mode
57FastLanguageModel.for_inference(model)
58
59results = []
60
61for dt in tqdm(dataset_TV):
62 input = dt["input"]
63 prompt = f"""### 問題\n{input}\n### 回答\n"""
64 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
65 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
66 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
67 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
68
69# save model output as jsonl file
70with open("./output_jsonl/output.jsonl", 'w', encoding='utf-8') as f:
71 for result in results:
72 json.dump(result, f, ensure_ascii=False)
73 f.write('\n')| Language | Dataset | description |
|---|---|---|
| Japanese | Synthetic dataset based on elyza-tasks-100 | Dataset synthesized by ChatGPT o1 pro mode and hand based on elyza-tasks-100 (CC-BY-SA-4.0) |