Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2import torch
3from tqdm import tqdm
4import json
5
6# QLoRA config for 4-bit quantization
7bnb_config = BitsAndBytesConfig(
8 load_in_4bit=True,
9 bnb_4bit_quant_type="nf4",
10 bnb_4bit_compute_dtype=torch.bfloat16,
11 bnb_4bit_use_double_quant=False,
12)
13
14# Load model and tokenizer
15model = AutoModelForCausalLM.from_pretrained(
16 model_name,
17 quantization_config=bnb_config,
18 device_map="auto",
19 token=HF_TOKEN
20)
21tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token=HF_TOKEN)elyza-tasks-100-TV_0.jsonl). Each line contains a JSON object with task information: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 = ""1results = []
2for data in tqdm(datasets):
3 input = data["input"]
4 prompt = f"""### Instruction
5 {input}
6 ### Response:
7 """
8
9 tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
10 with torch.no_grad():
11 outputs = model.generate(
12 tokenized_input,
13 max_new_tokens=100,
14 do_sample=False,
15 repetition_penalty=1.2
16 )[0]
17 output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
18
19 results.append({"task_id": data["task_id"], "input": input, "output": output})max_new_tokens=100: Maximum number of tokens to generatedo_sample=False: Deterministic generation (same output every time)repetition_penalty=1.2: Penalize repetition in generated text{"task_id": "task_1", "input": "input text", "output": "generated response"}task_id: Unique identifier for the taskoutput: Response generated by the modelinput: Input text (can be omitted in submission)1{
2 "instruction": "Task instruction text",
3 "input": "Input text (optional)",
4 "output": "Expected output text"
5}instruction: Task instruction that tells the model what to doinput: (Optional) Input text that provides specific context for the instructionoutput: Expected output that represents the ideal response1{"instruction": "以下の文章を要約してください。", "input": "人工知能(AI)は、人間の知能を模倣し、学習、推論、判断などを行うコンピュータシステムです。近年、機械学習や深層学習の発展により、画像認識、自然言語処理、ゲームなど様々な分野で人間に匹敵する、あるいは人間を超える性能を示しています。", "output": "AIは人間の知能を模倣するコンピュータシステムで、機械学習の発展により多くの分野で高い性能を示している。"}
2{"instruction": "次の英文を日本語に翻訳してください。", "input": "Artificial Intelligence is transforming the way we live and work.", "output": "人工知能は私たちの生活と仕事の仕方を変革しています。"}