Views
No views yet
1# conda環境の構築
2wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
3
4# このコマンドではいくつか質問があるので答えて下さい。おそらくインストール先のデフォルトは/root/miniforge3かと思います
5bash Miniforge3-$(uname)-$(uname -m).sh
6
7# 以下、インストール先が/root/miniforge3であることを前提とします
8export PATH=/root/miniforge3/bin:$PATH
9conda init
10
11# ここで一度、terminalを立ち上げ直す必要があります。
12# 以下のリンク先に従い環境を作ります。
13# https://docs.unsloth.ai/get-started/installation/conda-install
14conda create --name unsloth_env python=3.10 pytorch-cuda=12.1 pytorch cudatoolkit xformers -c pytorch -c nvidia -c xformers -y
15conda activate unsloth_env
16pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
17pip install --no-deps "trl<0.9.0" peft accelerate bitsandbytes
18
19# jupyter notebook用のセットアップ。
20conda install -c conda-forge ipykernel
21python -m ipykernel install --user --name=unsloth_env --display-name "Python (unsloth_env)" 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"1from unsloth import FastLanguageModel
2import torch
3import json
4
5model_name = "tomofusa/llm-jp-3-13b-finetune-2"
6
7max_seq_length = 2048
8dtype = None
9load_in_4bit = True
10
11model, tokenizer = FastLanguageModel.from_pretrained(
12 model_name = model_name,
13 max_seq_length = max_seq_length,
14 dtype = dtype,
15 load_in_4bit = load_in_4bit,
16 # token = "hf-token", # In the Google Colab case, it call from ENV. If you want to write the token directly, please comment it out.
17)
18FastLanguageModel.for_inference(model)1datasets = []
2with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
3 item = ""
4 for line in f:
5 line = line.strip()
6 item += line
7 if item.endswith("}"):
8 datasets.append(json.loads(item))
9 item = ""1from tqdm import tqdm
2
3# inference
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})1file_name = model_name.replace("/", "_") + "_output.jsonl"
2with open(f"./{file_name}", 'w', encoding='utf-8') as f:
3 for result in results:
4 json.dump(result, f, ensure_ascii=False)
5 f.write('\n')