Views
No views yet
推論対象となるファイルの配置とライブラリのインストールです。elyza-tasks-100-TV_0.jsonlファイルをコードを実行するディレクトリに配置してください。1# 依存ライブラリは次のとおりです。
2# - unsloth
3# - tqdm
4# - torch
5
6# Google Colab で実行する場合は `unsloth` のみインストールしてください。
7!pip install unsloth
8!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"elyza-tasks-100-TV_0.jsonl)から提出用ファイル(llm-jp-3-13b-finetune-LoRA-241124_output.jsonl)ファイルを出力します。
(なお、Colabでコードを実行する際はランタイムのタイプをL4に変更して行ってください。1# ライブラリのインポート
2from unsloth import FastLanguageModel
3from tqdm import tqdm # 進行状況を表示するためのライブラリ
4import torch
5import json
6
7# モデル名の指定
8model_name = "codemafia0000/llm-jp-3-13b-finetune-LoRA-241124"
9# 提出用ファイル名
10output_file_name = model_name.split('/')[-1] # モデル名のみ取得
11output_file_name = f"{output_file_name}_output.jsonl"
12
13# Hugging Faceのアクセストークンを設定します。自身のトークンに置き換えてください。
14HF_TOKEN = "YOUR_TOKEN"
15
16# モデルの設定
17max_seq_length = 2048 # 入力シーケンスの最大長
18dtype = None # データ型(デフォルト)
19load_in_4bit = True # 4ビット精度でモデルをロードし、メモリ使用量を削減
20
21# モデルとトークナイザーをロードします
22model, tokenizer = FastLanguageModel.from_pretrained(
23 model_name = model_name,
24 max_seq_length = max_seq_length,
25 dtype = dtype,
26 load_in_4bit = load_in_4bit,
27 token = HF_TOKEN,
28)
29
30# モデルを推論モードに設定
31FastLanguageModel.for_inference(model)
32
33# タスクデータの読み込み
34datasets = []
35with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
36 item = ""
37 for line in f:
38 line = line.strip()
39 item += line
40 if item.endswith("}"):
41 datasets.append(json.loads(item))
42 item = ""
43
44# 結果を保存するリストを初期化
45results = []
46
47# 各タスクに対してモデルを実行
48for dt in tqdm(datasets):
49 input = dt["input"]
50
51 # モデルへの入力プロンプトを作成します
52 prompt = f"""### question\n{input}\n### answer\n"""
53
54 # トークナイザーを使用してプロンプトをトークン化
55 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
56
57 # モデルを使用して応答を生成
58 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
59
60 # 生成されたトークンをデコードしてテキストに変換、不要な部分を削除
61 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### answer')[-1]
62
63 # 結果をリストに追記
64 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
65
66# 結果をJSONL形式で保存
67with open(output_file_name, 'w', encoding='utf-8') as f:
68 for result in results:
69 json.dump(result, f, ensure_ascii=False)
70 f.write('\n')
71
72# EOF