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
4!pip install --upgrade torch
5!pip install --upgrade xformers1import torch
2if torch.cuda.get_device_capability()[0] >= 8:
3 !pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
4
5from unsloth import FastLanguageModel
6
7max_seq_length = 512 # unslothではRoPEをサポートしているのでコンテキスト長は自由に設定可能
8dtype = None # Noneにしておけば自動で設定
9load_in_4bit = True # 今回は13Bモデルを扱うためTrue
10
11model, tokenizer = FastLanguageModel.from_pretrained(
12 model_name="Hide101111001111000/llm-jp-3-13b-it_lora-DPO-ja",
13 dtype=dtype,
14 load_in_4bit=load_in_4bit,
15 trust_remote_code=True,
16)
17
18import json
19datasets = []
20with open("/content/elyza-tasks-100-TV_0 .jsonl", "r") as f:
21 item = ""
22 for line in f:
23 line = line.strip()
24 item += line
25 if item.endswith("}"):
26 datasets.append(json.loads(item))
27 item = ""
28
29
30from tqdm import tqdm
31
32
33FastLanguageModel.for_inference(model)
34
35results = []
36for dt in tqdm(datasets):
37 input = dt["input"]
38
39 prompt = f"""### 指示\n{input}\n### 回答\n"""
40
41 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
42
43 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
44 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
45
46 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
47
48with open(f"model_output.jsonl", 'w', encoding='utf-8') as f:
49 for result in results:
50 json.dump(result, f, ensure_ascii=False)
51 f.write('\n')