Views
No views yet
1%%capture
2!pip install unsloth
3!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
4!pip install -U torch
5!pip install -U peft
61from unsloth import FastLanguageModel
2from peft import PeftModel
3import torch
4import json
5from tqdm import tqdm
6import re1model_id = "llm-jp/llm-jp-3-13b"
2
3new_model = "llm-jp-3-13b-N_dpo-6_lr2e-4_lora"
4adapter_id = "Hi-Satoh/" + new_model1from google.colab import userdata
2HF_TOKEN = userdata.get('HF_TOKEN') 1dtype = None # Noneにしておけば自動で設定
2load_in_4bit = True # 今回は13Bモデルを扱うためTrue
3model, tokenizer = FastLanguageModel.from_pretrained(
4 model_name=model_id,
5 dtype=dtype,
6 load_in_4bit=load_in_4bit,
7 trust_remote_code=True,
8)1print(adapter_id)
2model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)1# Google Driveから読む方法
2#from google.colab import drive
3#drive.mount('/content/drive')
4#test_file_name = "/content/drive/MyDrive/Colab Notebooks/LLM_2024_Fall_Student_LLM(公開)/05.最終課題/最新サンプルコード/data/elyza-tasks-100-TV_0.jsonl"
5
6datasets = []
7with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
8#with open(test_file_name, "r") as f:
9 item = ""
10 for line in f:
11 line = line.strip()
12 item += line
13 if item.endswith("}"):
14 datasets.append(json.loads(item))
15 item = ""1# 推論するためにモデルのモードを変更
2FastLanguageModel.for_inference(model)
3
4results = []
5for dt in tqdm(datasets):
6 input = dt["input"]
7
8 prompt = f"""### 指示\n{input}\n### 回答\n"""
9
10 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
11
12 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
13 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
14
15 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
161# ここではadapter_idを元にファイル名を決定しています。
2json_file_id = re.sub(".*/", "", adapter_id)
3with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
4 for result in results:
5 json.dump(result, f, ensure_ascii=False)
6 f.write('\n')
7
8