赞
踩
- # This code is based on the revised code from fastchat based on tatsu-lab/stanford_alpaca.
-
-
- from dataclasses import dataclass, field
- import json
- import math
- import logging
- import os
- from typing import Dict, Optional, List
- import torch
- from torch.utils.data import Dataset
- from deepspeed import zero
- from deepspeed.runtime.zero.partition_parameters import ZeroParamStatus
- import transformers
- from transformers import Trainer, GPTQConfig, deepspeed
- from transformers.trainer_pt_utils import LabelSmoother
- from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
-
-
- IGNORE_TOKEN_ID = LabelSmoother.ignore_index
-
-
- @dataclass
- class ModelArguments:
- model_name_or_path: Optional[str] = field(default="Qwen/Qwen-7B")
-
-
- @dataclass
- class DataArguments:
- data_path: str = field(
- default=None, metadata={"help": "Path to the training data."}
- )
- eval_data_path: str = field(
- default=None, metadata={"help": "Path to the evaluation data."}
- )
- lazy_preprocess: bool = False
-
-
- @dataclass
- class TrainingArguments(transformers.TrainingArguments):
- cache_dir: Optional[str] = field(default=None)
- optim: str = field(default="adamw_torch")
- model_max_length: int = field(
- default=8192,
- metadata={
- "help": "Maximum sequence length. Sequences will be right padded (and possibly truncated)."
- },
- )
- use_lora: bool = False
-
-
- @dataclass
- class LoraArguments:
- lora_r: int = 64
- lora_alpha: int = 16
- lora_dropout: float = 0.05
- lora_target_modules: List[str] = field(
- default_factory=lambda: ["c_attn", "c_proj", "w1", "w2"]
- )
- lora_weight_path: str = ""
- lora_bias: str = "none"
- q_lora: bool = False
-
-
- def maybe_zero_3(param):
- if hasattr(param, "ds_id"):
- assert param.ds_status == ZeroParamStatus.NOT_AVAILABLE
- with zero.GatheredParameters([param]):
- param = param.data.detach().cpu().clone()
- else:
- param = param.detach().cpu().clone()
- return param
-
-
- # Borrowed from peft.utils.get_peft_model_state_dict
- def get_peft_state_maybe_zero_3(named_params, bias):
- if bias == "none":
- to_return = {k: t for k, t in named_params if "lora_" in k}
- elif bias == "all":
- to_return = {k: t for k, t in named_params if "lora_" in k or "bias" in k}
- elif bias == "lora_only":
- to_return = {}
- maybe_lora_bias = {}
- lora_bias_names = set()
- for k, t in named_params:
- if "lora_" in k:
- to_return[k] = t
- bias_name = k.split("lora_")[0] + "bias"
- lora_bias_names.add(bias_name)
- elif "bias" in k:
- maybe_lora_bias[k] = t
- for k, t in maybe_lora_bias:
- if bias_name in lora_bias_names:
- to_return[bias_name] = t
- else:
- raise NotImplementedError
- to_return = {k: maybe_zero_3(v) for k, v in to_return.items()}
- return to_return
-
- local_rank = None
-
- def rank0_print(*args):
- if local_rank == 0:
- print(*args)
-
-
- def safe_save_model_for_hf_trainer(trainer: transformers.Trainer, output_dir: str, bias="none"):
- """Collects the state dict and dump to disk."""
- # check if zero3 mode enabled
- if deepspeed.is_deepspeed_zero3_enabled():
- state_dict = trainer.model_wrapped._zero3_consolidated_16bit_state_dict()
- else:
- if trainer.args.use_lora:
- state_dict = get_peft_state_maybe_zero_3(
- trainer.model.named_parameters(), bias
- )
- else:
- state_dict = trainer.model.state_dict()
- if trainer.args.should_save and trainer.args.local_rank == 0:
- trainer._save(output_dir, state_dict=state_dict)
-
-
- def preprocess(
- sources,
- tokenizer: transformers.PreTrainedTokenizer,
- max_len: int,
- system_message: str = "You are a helpful assistant."
- ) -> Dict:
- roles = {"user": "<|im_start|>user", "assistant": "<|im_start|>assistant"}
-
- im_start = tokenizer.im_start_id
- im_end = tokenizer.im_end_id
- nl_tokens = tokenizer('\n').input_ids
- _system = tokenizer('system').input_ids + nl_tokens
- _user = tokenizer('user').input_ids + nl_tokens
- _assistant = tokenizer('assistant').input_ids + nl_tokens
-
- # Apply prompt templates
- input_ids, targets = [], []
- for i, source in enumerate(sources):
- if roles[source[0]["from"]] != roles["user"]:
- source = source[1:]
-
- input_id, target = [], []
- system = [im_start] + _system + tokenizer(system_message).input_ids + [im_end] + nl_tokens
- input_id += system
- target += [im_start] + [IGNORE_TOKEN_ID] * (len(system)-3) + [im_end] + nl_tokens
- assert len(input_id) == len(target)
- for j, sentence in enumerate(source):
- role = roles[sentence["from"]]
- _input_id = tokenizer(role).input_ids + nl_tokens + \
- tokenizer(sentence["value"]).input_ids + [im_end] + nl_tokens
- input_id += _input_id
- if role == '<|im_start|>user':
- _target = [im_start] + [IGNORE_TOKEN_ID] * (len(_input_id)-3) + [im_end] + nl_tokens
- elif role == '<|im_start|>assistant':
- _target = [im_start] + [IGNORE_TOKEN_ID] * len(tokenizer(role).input_ids) + \
- _input_id[len(tokenizer(role).input_ids)+1:-2] + [im_end] + nl_tokens
- else:
- raise NotImplementedError
- target += _target
- assert len(input_id) == len(target)
- input_id += [tokenizer.pad_token_id] * (max_len - len(input_id))
- target += [IGNORE_TOKEN_ID] * (max_len - len(target))
- input_ids.append(input_id[:max_len])
- targets.append(target[:max_len])
- input_ids = torch.tensor(input_ids, dtype=torch.int)
- targets = torch.tensor(targets, dtype=torch.int)
-
- return dict(
- input_ids=input_ids,
- labels=targets,
- attention_mask=input_ids.ne(tokenizer.pad_token_id),
- )
-
-
- class SupervisedDataset(Dataset):
- """Dataset for supervised fine-tuning."""
-
- def __init__(self, raw_data, tokenizer: transformers.PreTrainedTokenizer, max_len: int):
- super(SupervisedDataset, self).__init__()
-
- rank0_print("Formatting inputs...")
- sources = [example["conversations"] for example in raw_data]
- data_dict = preprocess(sources, tokenizer, max_len)
-
- self.input_ids = data_dict["input_ids"]
- self.labels = data_dict["labels"]
- self.attention_mask = data_dict["attention_mask"]
-
- def __len__(self):
- return len(self.input_ids)
-
- def __getitem__(self, i) -> Dict[str, torch.Tensor]:
- return dict(
- input_ids=self.input_ids[i],
- labels=self.labels[i],
- attention_mask=self.attention_mask[i],
- )
Python代码,涉及deepspeed、transformers、peft等库,用于定义数据与模型相关的参数和配置。下面我会逐行解释这段代码的含义:
pythonCopy code
from deepspeed import zero
pythonCopy code
from deepspeed.runtime.zero.partition_parameters import ZeroParamStatus
pythonCopy code
import transformers
pythonCopy code
from transformers import Trainer, GPTQConfig, deepspeed
pythonCopy code
from transformers.trainer_pt_utils import LabelSmoother
pythonCopy code
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
pythonCopy code
IGNORE_TOKEN_ID = LabelSmoother.ignore_index
IGNORE_TOKEN_ID
,其值等于LabelSmoother的ignore_index属性。这通常用于指定在计算损失时应该忽略的token ID。pythonCopy code
@dataclass class ModelArguments: model_name_or_path: Optional[str] = field(default="Qwen/Qwen-7B")
pythonCopy code
@dataclass class DataArguments: data_path: str = field( default=None, metadata={"help": "Path to the training data."} ) eval_data_path: str = field( default=None, metadata={"help": "Path to the evaluation data."} ) lazy_preprocess: bool = False
pythonCopy code
@dataclass class TrainingArguments(transformers.TrainingArguments): cache_dir: Optional[str] = field(default=None) optim: str = field(default="adamw_torch") model_max_length: int = field( default=8192, metadata={ "help": "Maximum sequence length. Sequences will be right padded (and possibly truncated)." }, ) use_lora: bool = False
这段代码主要定义了与模型、数据和训练相关的参数和配置类。希望这个解释对您有所帮助!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。