OpenBMB / OpenBMB/CPM-Bee

如果要使用transformer和accelerator进行微调,应该怎么处理数据

Open
#114 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.4k
Forks
176
PR merge metrics
No merged PRs in 30d

Description

使用huggging face官方给出的代码修改后

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import torch_npu
from torch.utils.data import Dataset, DataLoader
import time
from data_prepare import CPMDataset

torch_npu.npu.set_compile_mode(jit_compile=False)
torch.npu.empty_cache()

trainset = CPMDataset("basic_task_finetune/bee_data/eval.jsonl")
trainset = trainset[:100]
train_loader = DataLoader(trainset, batch_size=2)

model_path = "models/cpm-bee-2b"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True).to('npu')

optimizer = torch.optim.Adam(model.parameters())

for iter, data in enumerate(train_loader):
    model.train()
    
    step_start = time.perf_counter()
    
    optimizer.zero_grad()
    input_encoded = tokenizer.prepare_for_finetune(data, max_length=1024).to(model.device)
    outputs = model(**input_encoded)
    loss = outputs.loss
    loss.backward()
    optimizer.step()
    
    step_time = time.perf_counter() - step_start
    
    print(f"Step {iter}, Loss: {loss.item():.4f}, Time per step: {step_time:.4f} s")

输出的loss为NaN

数据处理为

import json
from torch.utils.data import Dataset

class CPMDataset(Dataset):
    def __init__(self, jsonl_file):
        self.data = []
        with open(jsonl_file, 'r', encoding='utf-8') as file:
            for line in file:
                # 解析每一行 JSON 数据
                item = json.loads(line)
                # 提取需要的字段
                # input = item['input']
                # options = item['options']
                # question = item['question']
                # answer = item['<ans>']
                # input_text = f"{input}<sep>{question}<sep>{options}"
                # 将数据添加到列表中
                self.data.append(item)

    def __len__(self):
        # 返回数据集的大小
        return len(self.data)

    def __getitem__(self, idx):
        # 返回格式化的数据
        return self.data[idx]

if __name__=="__main__":

    dataset = CPMDataset('eval.jsonl')

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CPMDataset implementation and the training entry point that calls tokenizer.prepare_for_finetune; inspect the eval.jsonl records and the resulting batch values before the model call. Reproduce the loop with the NPU setup and determine whether the inputs or training step first produces NaN; done means the cause is identified and the fine-tuning loss remains finite.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.