atsunobu/llm-jp-3-13b-FineTuning-r36_2024-11-29-1600_lora
Uploaded model
- Developed by: atsunobu
- License: apache-2.0
- Finetuned from model : llm-jp/llm-jp-3-13b
This llama model was trained 2x faster with Unsloth and Huggingface's TRL library.
3.09
以下は、elyza-tasks-100-TV_0.jsonlの回答のためのコードです。
必要なライブラリをインストール
%%capture !pip install unsloth !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git" !pip install -U torch !pip install -U peft
必要なライブラリを読み込み
from unsloth import FastLanguageModel from peft import PeftModel import torch import json from tqdm import tqdm import re HF_TOKEN = "your-token"
ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
modelid = "llm-jp/llm-jp-3-13b" adapterid = "atsunobu/llm-jp-3-13b-FineTuning-r362024-11-29-1600lora"
unslothのFastLanguageModelで元のモデルをロード。
dtype = None # Noneにしておけば自動で設定 loadin4bit = True # 今回は13Bモデルを扱うためTrue
model, tokenizer = FastLanguageModel.frompretrained( modelname=modelid, dtype=dtype, loadin4bit=loadin4bit, trustremote_code=True, )
元のモデルにLoRAのアダプタを統合。
model = PeftModel.frompretrained(model, adapterid, token = HF_TOKEN)
タスクとなるデータの読み込み。
事前にデータをアップロードしてください。
datasets = [] with open("./elyza-tasks-100-TV_0.jsonl", "r") as f: item = "" for line in f: line = line.strip() item += line if item.endswith("}"): datasets.append(json.loads(item)) item = ""
モデルを用いてタスクの推論。
推論するためにモデルのモードを変更
FastLanguageModel.forinference(model) results = [] for dt in tqdm(datasets): input = dt["input"] prompt = f"""### 指示\n{input}\n### 回答\n""" inputs = tokenizer([prompt], returntensors = "pt").to(model.device)
outputs = model.generate(**inputs, maxnewtokens = 512, usecache = True, dosample=False, repetitionpenalty=1.2) prediction = tokenizer.decode(outputs[0], skipspecialtokens=True).split('\n### 回答')[-1] results.append({"taskid": dt["task_id"], "input": input, "output": prediction})
結果をjsonlで保存。
ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
jsonfileid = re.sub(".*/", adapterid) with open(f"/content/{jsonfileid}output.jsonl", 'w', encoding='utf-8') as f: for result in results: json.dump(result, f, ensure_ascii=False) f.write('\n')
