Views
No views yet
1%%capture
2!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git
3
4# Install Flash Attention 2 for softcapping support
5import torch
6if torch.cuda.get_device_capability()[0] >= 8:
7 !pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"1from unsloth import FastLanguageModel
2import torch
3import json
4
5max_seq_length = 4096
6dtype = None
7load_in_4bit = True
8
9model, tokenizer = FastLanguageModel.from_pretrained(
10 model_name = "hama-jp/gemma2-27b-sft-241213-lora-06",
11 max_seq_length = max_seq_length,
12 dtype = dtype,
13 load_in_4bit = load_in_4bit,
14)1#@title ELYZA-tasks-100-TVの読み込み
2import json
3
4# testファイルのパスを指定
5file_path = 'elyza-tasks-100-TV_0.jsonl'
6
7# データセットの辞書を初期化
8dataset_test = {}
9
10# JSONLファイルを読み込む
11with open(file_path, 'r', encoding='utf-8') as file:
12 for line in file:
13 # 各行をJSON形式で読み取る
14 task_data = json.loads(line.strip())
15 # task_idとinputを取得
16 task_id = task_data.get("task_id")
17 input_data = task_data.get("input")
18 # task_idをキーにしてdataset_testに格納
19 if task_id is not None:
20 dataset_test[task_id] = {"input": input_data}
21
22EOS_TOKEN = tokenizer.eos_token
23
24# プロンプトテンプレート
25alpaca_prompt = """### 指示
26以下の入力に従って適切に処理してください。
27### 入力:
28{}
29### 出力:
30"""
31
32# dataset_testに"text"キーを追加
33for task_id, content in dataset_test.items():
34 input_text = content["input"]
35 prompt_text = alpaca_prompt.format(input_text) + EOS_TOKEN
36 dataset_test[task_id]["text"] = prompt_text1from unsloth import FastLanguageModel
2
3
4FastLanguageModel.for_inference(model) # Enable native 2x faster inference
5
6def extract_response(full_text):
7 """
8 Extracts the response part after '### 出力:'.
9 Assumes the response starts after ':\n### 出力' and removes any trailing whitespace.
10 """
11 response_marker = "\n### 出力:"
12 if response_marker in full_text:
13 return full_text.split(response_marker, 1)[-1].strip()
14 return full_text.strip()
15
16with open("output.jsonl", "w", encoding="utf-8") as outfile:
17 for i in range(100):
18 # Get the input text
19 input_text = dataset_test[i]["text"]
20
21 # Tokenize and move input to GPU
22 inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
23
24 # Generate output
25 output = model.generate(
26 **inputs,
27 max_new_tokens=1024,
28 temperature=0.15,
29 repetition_penalty=1.05,
30 use_cache=True,
31 do_sample=True
32 )
33
34 # Decode output text
35 decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
36
37 # Extract only the response part
38 response_only = extract_response(decoded_output)
39
40 # Print for debugging
41 print("task_id:",i)
42 print("input:",dataset_test[i]["input"])
43 print("output:",response_only)
44 print("---")
45
46 # Prepare a dictionary for JSONL
47 result = {
48 "task_id": i,
49 "input": dataset_test[i]["input"],
50 "output": response_only
51 }
52
53 # Save to JSONL
54 outfile.write(json.dumps(result, ensure_ascii=False) + "\n")