Initialize optimizer research workspace with official baselines and history-free coverage

Vendor pinned MeZO, AGZO, LOZO and LoREN sources with independent environments. Add direct in-place fix/adjust/node training, derived history-free basis gradients, development-best checkpoints and experiment tooling. Keep models, datasets, caches and credentials outside Git.

Validation: 93 project tests passed; 521 vendored author files verified.
This commit is contained in:
gloamxun committed 2026-11-12 23:32:33 +08:00
commit e0feabec8f
596 files changed
+87356

No files matched your search

+939
View File
@@ -0,0 +1,939 @@
import warnings
# Suppress NPU import-time warnings before any imports that may trigger them
# 1) Hide the long ImportWarning from torch_npu.contrib.transfer_to_npu
warnings.filterwarnings("ignore", category=ImportWarning, message=".*torch_npu.*")
# 2) Hide RuntimeWarning regarding torch.jit being disabled by transfer_to_npu
warnings.filterwarnings(
"ignore",
category=RuntimeWarning,
message=r".*torch\.jit\.script.*",
)
import argparse
import os
import random
import wandb
import torch
from torch.distributed.fsdp.fully_sharded_data_parallel import FullyShardedDataParallel as FSDP
from torch.utils.data import Dataset
from tqdm import tqdm
import types
import sys as _sys
from transformers import (
AutoConfig,
AutoTokenizer,
AutoModelForCausalLM,
AutoModel,
HfArgumentParser,
TrainingArguments,
DataCollatorForTokenClassification
)
from metrics import calculate_metric
from modeling_mistral import (
MistralForCausalLM,
MistralConfig
)
from tasks import get_task
from trainer import OurTrainer
from utils import *
# NPU/CUDA compat helpers
import npu_compat as DeviceLoader
os.environ["TRANSFORMERS_CACHE"] = "./cache"
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# (warnings filters are configured at the very top to catch import-time emissions)
def _ensure_transformers_masking_utils():
"""Provide a shim for `transformers.masking_utils.create_causal_mask` when absent.
Some external models (e.g., ModelScope Pangu Embedded) import
`transformers.masking_utils.create_causal_mask`. Newer Transformers versions
may not expose this module, causing ModuleNotFoundError. We inject a minimal
compatible implementation at runtime.
"""
try:
import transformers.masking_utils # type: ignore
return
except Exception:
pass
try:
import torch
mod = types.ModuleType("transformers.masking_utils")
def create_causal_mask(input_ids_shape=None, dtype=torch.float32, device=None, past_key_values_length=0, *args, **kwargs):
# Support both signatures: (input_shape, dtype, past_len) or (bsz, tgt_len,...)
try:
if input_ids_shape is None:
return None
if isinstance(input_ids_shape, (tuple, list)):
if len(input_ids_shape) >= 2:
bsz, tgt_len = int(input_ids_shape[0]), int(input_ids_shape[1])
else:
# fallback assume (tgt_len,)
bsz, tgt_len = 1, int(input_ids_shape[0])
else:
# unknown, fallback to scalar length
bsz, tgt_len = 1, int(input_ids_shape)
except Exception:
bsz, tgt_len = 1, 1
dev = device if device is not None else (torch.npu.current_device() if hasattr(torch, "npu") and hasattr(torch.npu, "current_device") else (torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")))
# Build upper-triangular mask: allow current and past positions, mask future with -inf
attn_shape = (bsz, 1, tgt_len, tgt_len + int(past_key_values_length))
mask = torch.zeros(attn_shape, dtype=dtype, device=dev)
# Positions to mask (future): indices where j > i + past
future = torch.triu(torch.ones((tgt_len, tgt_len + int(past_key_values_length)), device=dev), diagonal=1 + int(past_key_values_length))
future = future.bool()
mask[:, :, :, :] = mask[:, :, :, :].masked_fill(future.expand(bsz, 1, tgt_len, tgt_len + int(past_key_values_length)), float("-inf"))
return mask
mod.create_causal_mask = create_causal_mask
# Register under sys.modules so import works for remote code
_sys.modules["transformers.masking_utils"] = mod
logger.info("Injected shim for transformers.masking_utils.create_causal_mask")
except Exception as e:
logger.warning(f"Failed to inject masking_utils shim: {e}")
try:
AutoConfig.register("mistral", MistralConfig)
except ValueError:
pass
try:
AutoModelForCausalLM.register(MistralConfig, MistralForCausalLM)
except ValueError:
pass
@dataclass
class OurArguments(TrainingArguments):
# dataset and sampling strategy
task_name: str = "SST2" # task name should match the string before Dataset in the Dataset class name. We support the following task_name: SST2, RTE, CB, BoolQ, WSC, WIC, MultiRC, Copa, ReCoRD, SQuAD, DROP
# Number of examples
num_train: int = 0 # ICL mode: number of demonstrations; training mode: number of training samples
num_dev: int = None # (only enabled with training) number of development samples
num_eval: int = None # number of evaluation samples
num_train_sets: int = None # how many sets of training samples/demos to sample; if None and train_set_seed is None, then we will sample one set for each evaluation sample
train_set_seed: int = 0 # designated seed to sample training samples/demos
result_file: str = None # file name for saving performance; if None, then use the task name, model name, and config
# Model loading
model_name: str = "FreedomIntelligence/openPangu-Embedded-1B" # HuggingFace model name
load_float16: bool = False # load model parameters as float16
load_bfloat16: bool = True # load model parameters as bfloat16
load_int8: bool = False # load model parameters as int8
max_length: int = 2048 # max length the model can take
no_auto_device: bool = False # do not load model by auto device; should turn this on when using FSDP
# Calibration
sfc: bool = False # whether to use SFC calibration
icl_sfc: bool = False # whether to use SFC calibration for ICL samples
template_ver: int = 0 # template. For some tasks (SST2, RTE, Copa), we add template ver=1 as the empty template.
# Training
trainer: str = "none"
## options
## - none: no training -- for zero-shot or in-context learning (ICL)
## - regular: regular huggingface trainer -- for fine-tuning
## - zo_sgd: zeroth-order SGD (MeZO) training
## - zo_conserv: zeroth-order SGD conservative training
## - zo_adam: zeroth-order Adam training
## - zo_sign_opt: zeroth-order sign sgd training
## - agzo: activation-guided zeroth-order training # AGZO: 新增trainer选项
## - lozo: low-rank zeroth-order training with adjustable rank r # LOZO: 新增trainer选项
## - forward_grad: forward gradient
optimizer: str = "adamw"
## options
## - sgd
## - adam
## - adamw # this is huggingface default
only_train_option: bool = True # whether to only train the option part of the input
train_as_classification: bool = False # take the log likelihood of all options and train as classification
momentum: float = 0.0 # only work for SGD optimizer
# MeZO
zo_eps: float = 1e-3 # eps in MeZO
perturbation_mode: str = "two_side"
q: int = 1 # number of Gaussian samples for zeroth-order trainers
# Prefix tuning
prefix_tuning: bool = False # whether to use prefix tuning
num_prefix: int = 5 # number of prefixes to use
no_reparam: bool = True # do not use reparameterization trick
prefix_init_by_real_act: bool = True # initialize prefix by real activations of random words
# prompt tuning hyperparameters
prompt_tuning: bool = False # whether to use prompt tuning
num_virtual_tokens: int = 10 # number of prompt tokens to use
prompt_init_by_real_tokens: bool = False # whether to sample random tokens from Embedding layer
# LoRA
lora: bool = False # whether to use LoRA
lora_alpha: int = 16 # alpha in LoRA
lora_r: int = 8 # r in LoRA
# Generation
sampling: bool = False # whether to use sampling
temperature: float = 1.0 # temperature for generation
num_beams: int = 1 # number of beams for generation
top_k: int = None # top-k for generation
top_p: float = 0.95 # top-p for generation
max_new_tokens: int = 50 # max number of new tokens to generate
eos_token: str = "\n" # end of sentence token
# Saving
save_model: bool = False # whether to save the model
no_eval: bool = False # whether to skip evaluation
tag: str = "" # saving tag
# Linear probing
linear_probing: bool = False # whether to do linear probing
lp_early_stopping: bool = False # whether to do early stopping in linear probing
head_tuning: bool = False # head tuning: only tune the LM head
# Untie emb/lm_head weights
untie_emb: bool = False # untie the embeddings and LM head
# Display
verbose: bool = False # verbose output
# Non-diff objective
non_diff: bool = False # use non-differentiable objective (only support F1 for SQuAD for now)
# Auto saving when interrupted
save_on_interrupt: bool = False # save model when interrupted (useful for long training)
clean_model_at_end: bool = True # remove everthing at the end.
# sparse gradient pruning
gradient_sparsity: float = None
sparse_gradient_resample_steps: int = 1
sparse_gradient_group: str = "layer"
"""
Options
## - global: global sparsity will assign different sparsity to each layer, based on the pretrained weight magnitude
## - layer: each layer has the same sparsity
"""
# module-wise perturbation
module_wise_perturbation: bool = False
perturbed_module_level: str = "transformer-block"
coordinate_perturbation: bool = True # If True, will update weight right after the gradient is computed
"""
Options
## - transformer-block: perturb one transformer block at a time
## - mlp-attn: perturb one mlp/attention layer at a time
## - linear: perturb one linear layer at a time
"""
# AGZO: power iteration steps for computing activation principal direction
agzo_power_iter_steps: int = 5
# AGZO: number of guided directions (rank) per layer
agzo_rank: int = 1
# AGZO: empty CUDA cache every N steps (0=disable)
agzo_empty_cache_steps: int = 1
# AGZO: debug flag to log per-layer memory usage during basis collection
agzo_debug_memory: bool = False
# LOZO: rank (r) for low-rank perturbation; r<=min(out_dim,in_dim)
lozo_rank: int = 4
# Device selection
npu_device: Optional[int] = 0 # which npu to use
# Save-best options
save_best: bool = True # whether to save the best model during training
best_metric_name: str = "accuracy" # metric name to compare (default: accuracy)
best_split: str = "val" # prefer validation set (val); fallback to test if no validation set
def parse_args():
parser = argparse.ArgumentParser()
parser = HfArgumentParser(OurArguments)
args = parser.parse_args_into_dataclasses()[0]
print(args)
return args
def set_seed(seed: int):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.npu.manual_seed_all(seed)
class Framework:
def __init__(self, args, task):
self.args = args
self.task = task
self.model, self.tokenizer = self.load_model()
def load_model(self):
"""
Load HuggingFace models
"""
model_name = self.args.model_name
dev_type = None
dev_idx = None
total_gb = None
device_name = None
# 从 npu_compat 统一获取设备信息
dev_type, dev_idx, total_gb, device_name = DeviceLoader.get_active_device_info()
if self.args.load_float16:
dtype = torch.float16
elif self.args.load_bfloat16:
dtype = torch.bfloat16
elif self.args.load_int8:
dtype = torch.int8
else:
dtype = torch.float32
# 显示模型以及加载模型的设备
# Print MODEL and DEVICE on separate, clearly marked lines
if model_name:
logger.info(f"=== MODEL === {model_name}")
if dev_type is not None:
dev_line = f"=== DEVICE === {dev_type}:{dev_idx}"
if total_gb is not None:
dev_line += f" total_mem={total_gb}GB"
if device_name:
dev_line += f" chip={device_name}"
logger.info(dev_line)
with count_time(f"Loading model with {dtype}"):
free_in_GB = int(DeviceLoader.mem_get_info()[0] / 1024 ** 3)
max_memory_gb = max(1, free_in_GB - 5)
logging.info(f"Free memory per device: {free_in_GB} GB, setting max_memory to {max_memory_gb} GB per device.")
model_name_lower = self.args.model_name.lower()
is_qwen = "qwen" in model_name_lower
is_pangu_emb = "openpangu-embedded" in model_name_lower
# # If using ModelScope-only model, download snapshot first
# base_model_path = self.args.model_name
# if is_pangu_emb:
# try:
# from modelscope.hub.snapshot_download import snapshot_download
# base_model_path = snapshot_download(
# model_id=self.args.model_name,
# cache_dir=os.path.join(os.path.dirname(__file__), "..", ".modelscope_cache"),
# )
# logging.info(f"ModelScope snapshot downloaded to: {base_model_path}")
# # Ensure remote code can import masking_utils
# _ensure_transformers_masking_utils()
# except Exception as e:
# print(f"Warning: ModelScope snapshot_download failed: {e}. Fallback to using {self.args.model_name} directly.")
# base_model_path = self.args.model_name
config_kwargs = {}
if is_pangu_emb or is_qwen:
config_kwargs["trust_remote_code"] = True
config = AutoConfig.from_pretrained(self.args.model_name, **config_kwargs)
else:
config = AutoConfig.from_pretrained(self.args.model_name, **config_kwargs)
if self.args.untie_emb:
# Untie embeddings/LM head
logger.warn("Untie embeddings and LM head")
config.tie_word_embeddings = False
if self.args.head_tuning:
if is_qwen or is_pangu_emb:
raise NotImplementedError("Head tuning is not supported for Qwen and Pangumodels yet.")
torch_dtype = torch.float32
if self.args.load_float16:
torch_dtype = torch.float16
elif self.args.load_bfloat16:
torch_dtype = torch.bfloat16
# Head tuning
if "opt" in model_name_lower:
from modeling_opt import OPTForCausalLM
model = OPTForCausalLM.from_pretrained(
self.args.model_name,
config=config,
device_map='auto',
torch_dtype=torch_dtype,
max_memory={i: f'{max_memory_gb}GB' for i in
range(torch.npu.device_count())},
)
elif "llama" in model_name_lower:
from modeling_llama import LlamaForCausalLMWithHeadTuning
model = LlamaForCausalLMWithHeadTuning.from_pretrained(
self.args.model_name,
config=config,
device_map='auto',
torch_dtype=torch_dtype,
max_memory={i: f'{max_memory_gb}GB' for i in
range(torch.npu.device_count())},
)
elif "mistral" in model_name_lower:
from modeling_mistral import MistralForCausalLMWithHeadTuning
model = MistralForCausalLMWithHeadTuning.from_pretrained(
self.args.model_name,
config=config,
device_map='auto',
torch_dtype=torch_dtype,
max_memory={i: f'{max_memory_gb}GB' for i in
range(torch.npu.device_count())},
)
else:
raise NotImplementedError(f"Head tuning is not supported for {self.args.model_name}")
elif self.args.no_auto_device:
# No auto device (use for FSDP)
model = AutoModelForCausalLM.from_pretrained(
self.args.model_name,
config=config,
trust_remote_code=is_qwen or is_pangu_emb,
)
else:
# Auto device loading
torch_dtype = torch.float32
if self.args.load_float16:
torch_dtype = torch.float16
elif self.args.load_bfloat16:
torch_dtype = torch.bfloat16
model = AutoModelForCausalLM.from_pretrained(
self.args.model_name,
config=config,
device_map='auto' if DeviceLoader.is_npu_available() else None,
torch_dtype=torch_dtype,
max_memory={i: f'{max_memory_gb}GB' for i in range(DeviceLoader.device_count())},
load_in_8bit=self.args.load_int8,
trust_remote_code=is_qwen or is_pangu_emb,
)
DeviceLoader.safe_set_device(self.args.npu_device)
model.eval()
# Load tokenizer
# In mezo, use_fast is set to False. But TypeError will occur when running SQuaD. Setting to be True can fix.
tokenizer_kwargs = {"use_fast": True}
if is_qwen or is_pangu_emb:
tokenizer_kwargs["trust_remote_code"] = True
tokenizer = AutoTokenizer.from_pretrained(self.args.model_name, **tokenizer_kwargs)
# HF tokenizer bug fix
if "opt" in self.args.model_name:
tokenizer.bos_token_id = 0
if ("llama" in self.args.model_name) or ("mistral" in self.args.model_name.lower()):
# LLaMA padding token
tokenizer.pad_token_id = 0 # technically <unk>
if is_qwen:
if tokenizer.pad_token is None and tokenizer.eos_token is not None:
tokenizer.pad_token = tokenizer.eos_token
if tokenizer.pad_token_id is None:
tokenizer.pad_token_id = tokenizer.eos_token_id
# Prefix tuning/LoRA
if self.args.prefix_tuning:
from prefix_tuning import PrefixTuning
PrefixTuning(model, num_prefix=self.args.num_prefix, reparam=not self.args.no_reparam,
float16=self.args.load_float16, init_by_real_act=self.args.prefix_init_by_real_act)
if self.args.lora:
from lora import LoRA
LoRA(model, r=self.args.lora_r, alpha=self.args.lora_alpha, float16=self.args.load_float16)
if self.args.prompt_tuning:
from prompt_tuning import PromptTuning
print("Adding Prompt Tuning to model...")
PromptTuning(
model,
num_virtual_tokens=self.args.num_virtual_tokens,
init_by_real_tokens=self.args.prompt_init_by_real_tokens,
hide_virtual_token_logits=True, # a workaround for the other loss/prediction functions
)
print("Total/Trainable number of parameters: {}/{}".format(
sum(p.numel() for p in model.parameters()),
sum(p.numel() for p in model.parameters() if p.requires_grad),
))
if self.args.head_tuning:
if model.config.model_type in ["opt", "llama", "mistral"]:
head_name = "lm_head" if self.args.untie_emb else "embed_tokens"
else:
raise NotImplementedError
for n, p in model.named_parameters():
if head_name not in n:
p.requires_grad = False
else:
logger.info(f"Only tuning {n}")
return model, tokenizer
def forward(self, input_ids, option_len=None, generation=False):
"""
Given input_ids and the length of the option, return the log-likelihood of each token in the option.
For generation tasks, return the generated text.
This function is only for inference
"""
input_ids = torch.tensor([input_ids]).to(self.model.device)
if generation:
args = self.args
# Autoregressive generation
outputs = self.model.generate(input_ids, do_sample=args.sampling, temperature=args.temperature,
num_beams=args.num_beams, top_p=args.top_p, top_k=args.top_k,
max_new_tokens=min(args.max_new_tokens, args.max_length - input_ids.size(1)),
num_return_sequences=1,
eos_token_id=[
self.tokenizer.encode(args.eos_token, add_special_tokens=False)[-1],
self.tokenizer.eos_token_id], )
# For generation, directly return the text output
output_text = self.tokenizer.decode(outputs[0][input_ids.size(1):], skip_special_tokens=True).strip()
return output_text
else:
with torch.inference_mode():
self.model.eval()
logits = self.model(input_ids=input_ids).logits
labels = input_ids[0, 1:]
logits = logits[0, :-1]
log_probs = F.log_softmax(logits, dim=-1)
selected_log_probs = log_probs[torch.arange(len(labels)).to(labels.device), labels]
selected_log_probs = selected_log_probs.cpu().detach()
# Only return the option (candidate) part
return selected_log_probs[-option_len:]
def one_step_pred(self, train_samples, eval_sample, verbose=False):
"""
Return the prediction on the eval sample. In ICL, use train_samples as demonstrations
"""
verbose = verbose or self.args.verbose
# if verbose:
# logger.info("========= Example =========")
# logger.info(f"Candidate: {eval_sample.candidates}")
# logger.info(f"Correct candidate: {eval_sample.correct_candidate}")
# Encode (add prompt and tokenize) the sample; if multiple-choice/classification, encode all candidates (options)
encoded_candidates, option_lens = encode_prompt(self.task,
self.task.get_template(template_version=self.args.template_ver),
train_samples, eval_sample,
self.tokenizer, max_length=self.args.max_length,
generation=self.task.generation,
max_new_tokens=self.args.max_new_tokens)
# Calibration
if self.args.sfc or self.args.icl_sfc:
sfc_encoded_candidates, sfc_option_lens = encode_prompt(self.task, self.task.get_template(
template_version=self.args.template_ver), train_samples,
eval_sample, self.tokenizer,
max_length=self.args.max_length, sfc=self.args.sfc,
icl_sfc=self.args.icl_sfc,
generation=self.task.generation,
max_new_tokens=self.args.max_new_tokens)
outputs = []
if self.task.generation:
# For generation tasks, return the autoregressively-generated text
output_text = self.forward(encoded_candidates[0], generation=True)
# if verbose:
# logger.info("=== Prompt ===")
# logger.info(self.tokenizer.decode(encoded_candidates[0]))
# logger.info(f"Output: {output_text}")
return Prediction(correct_candidate=eval_sample.correct_candidate, predicted_candidate=output_text)
else:
# For classification/multiple-choice, calculate the probabilities of all candidates
for candidate_id, encoded_candidate in enumerate(encoded_candidates):
selected_log_probs = self.forward(encoded_candidate, option_len=option_lens[candidate_id])
if verbose:
# if candidate_id == 0:
# logger.info("=== Candidate %d ===" % candidate_id)
# logger.info(self.tokenizer.decode(encoded_candidate))
# else:
# logger.info("=== Candidate %d (without context)===" % candidate_id)
# logger.info(self.tokenizer.decode(encoded_candidate).split(self.task.train_sep)[-1])
logger.info(f"Log probabilities of the option tokens: {selected_log_probs}")
if self.args.sfc or self.args.icl_sfc:
sfc_selected_log_probs = self.forward(sfc_encoded_candidates[candidate_id],
option_len=sfc_option_lens[
candidate_id]) # if verbose: # logger.info("=== Candidate %d (without context) SFC ===" % candidate_id) # logger.info( # self.tokenizer.decode(sfc_encoded_candidates[candidate_id]).split(self.task.train_sep)[-1]) # logger.info(f"Log probabilities of the option tokens: {sfc_selected_log_probs}")
outputs.append({"log_probs": selected_log_probs,
"sfc_log_probs": sfc_selected_log_probs if self.args.sfc or self.args.icl_sfc else None})
if self.args.sfc or self.args.icl_sfc:
# Calibrated probabilities (surface form competition; https://arxiv.org/pdf/2104.08315.pdf)
# log p(candidate | input) = log p_lm(candidate | input) - log p_lm(candidate | sfc prompt)
scores = [x['log_probs'].sum().item() - x['sfc_log_probs'].sum().item() for x in outputs]
else:
# (Default) length-normalized log probabilities
# log p(candidate | input) = log p_lm(candidate | input) / |candidate #tokens|
scores = [x['log_probs'].mean().item() for x in outputs]
if verbose:
logger.info(f"Prediction scores: {scores}")
if isinstance(eval_sample.correct_candidate, list):
# For some datasets there are multiple correct answers
correct_candidate_id = [eval_sample.candidates.index(c) for c in eval_sample.correct_candidate]
else:
correct_candidate_id = eval_sample.candidates.index(eval_sample.correct_candidate)
return Prediction(correct_candidate=correct_candidate_id, predicted_candidate=int(np.argmax(scores)))
def evaluate(self, train_samples, eval_samples, one_train_set_per_eval_sample=False, description=None):
"""
Evaluate function.
Here, train_samples are used for demonstrations for ICL.
If one_train_set_per_eval_sample is True, then each eval sample has its own training (demonstration) set.
Otherwise, the same training set is used for all eval samples.
"""
if one_train_set_per_eval_sample:
logger.info(f"There are {len(eval_samples)} validation samples and one train set per eval sample")
else:
logger.info(f"There are {len(train_samples)} training samples and {len(eval_samples)} validation samples")
# Prediction loop
predictions = []
for eval_id, eval_sample in enumerate(tqdm(eval_samples, desc=description)):
predictions.append(
self.one_step_pred(train_samples[eval_id] if one_train_set_per_eval_sample else train_samples,
eval_sample, verbose=False))
# Calculate metrics
metric_name = getattr(self.task, "metric_name", "accuracy")
metrics = {metric_name: calculate_metric(predictions, metric_name)}
return metrics
def train(self, train_samples, dev_samples, eval_samples):
"""
Training function
if self.num_dev is not None, eval_samples are dev_samples
"""
logger.info(f"Eval sample length is {len(eval_samples)}")
# Set tokenizer to left padding (so that all the options are right aligned)
self.tokenizer.padding_side = "left"
class HFDataset(Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx]
def _convert(samples):
"""
Convert samples to HF-compatible dataset
"""
data = []
for sample in samples:
encoded_candidates, option_lens = encode_prompt(self.task, self.task.get_template(
template_version=self.args.template_ver), [], sample,
self.tokenizer, max_length=self.args.max_length,
generation=self.task.generation,
generation_with_gold=True,
max_new_tokens=self.args.max_new_tokens)
if self.task.generation:
correct_candidate_id = 0
elif isinstance(sample.correct_candidate, list):
correct_candidate_id = sample.candidates.index(sample.correct_candidate[0])
else:
correct_candidate_id = sample.candidates.index(sample.correct_candidate)
if self.args.non_diff:
# For non-differentiable objective, there is no teacher forcing thus the
# current answer part is removed
encoded_candidates[correct_candidate_id] = encoded_candidates[correct_candidate_id][
:-option_lens[correct_candidate_id]]
if self.args.train_as_classification:
# For classification, we provide the label as the correct candidate id
data.append([{"input_ids": encoded_candidates[_i], "labels": correct_candidate_id,
"option_len": option_lens[_i], "num_options": len(sample.candidates)} for _i in
range(len(encoded_candidates))])
elif self.args.only_train_option:
# Otherwise, it is just LM-style teacher forcing
if self.args.non_diff:
# For non-differentiable objective, we need to provide the gold answer to calculate F1/acc
data.append({"input_ids": encoded_candidates[correct_candidate_id],
"labels": encoded_candidates[correct_candidate_id],
"option_len": option_lens[correct_candidate_id], "gold": sample.correct_candidate})
else:
data.append({"input_ids": encoded_candidates[correct_candidate_id],
"labels": encoded_candidates[correct_candidate_id],
"option_len": option_lens[correct_candidate_id]})
else:
data.append({"input_ids": encoded_candidates[correct_candidate_id],
"labels": encoded_candidates[correct_candidate_id]})
return data
with count_time("Tokenizing training samples"):
train_dataset = HFDataset(_convert(train_samples))
eval_dataset = HFDataset(_convert(eval_samples))
dev_dataset = HFDataset(_convert(dev_samples))
if self.args.only_train_option and not self.args.non_diff:
# If --only_train_option and not with a non-differentiable objective, we wrap the forward function
self.model.original_forward = self.model.forward
self.model.forward = forward_wrap_with_option_len.__get__(self.model, type(self.model))
if self.args.non_diff:
collator = NondiffCollator
else:
collator = DataCollatorForTokenClassification
if self.args.gradient_sparsity is not None:
logger.info(
f"[Sparse gradient] sparsity is {self.args.gradient_sparsity}, resampling per {self.args.sparse_gradient_resample_steps} steps"
)
if self.args.sparse_gradient_group == "global":
logger.info(f"[Sparse gradient] global-ratio random pruning is enabled, "
f"sparsity of each layer is computed based on the pretrained weight magnitude.")
elif self.args.sparse_gradient_group == "layer":
logger.info(f"[Sparse gradient] layer-wise random pruning is enabled, "
f"sparsity of each layer is the same.")
else:
raise NotImplementedError(f"Unknown sparse gradient group: {self.args.sparse_gradient_group}")
perturb_module_regex = None
if self.args.module_wise_perturbation:
if "opt" in self.args.model_name:
assert self.args.perturbed_module_level in OPT_PERTURBATION_LEVEL_TO_REGEX.keys(), f"Unknown perturbed module group {self.args.perturbed_module_level}"
perturb_module_regex = OPT_PERTURBATION_LEVEL_TO_REGEX[self.args.perturbed_module_level]
else:
raise NotImplementedError(f"Unimplemented model {self.args.model_name} for module-wise perturbation")
trainer = OurTrainer(model=self.model,
args=self.args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=self.tokenizer,
data_collator=DataCollatorWithPaddingAndNesting(self.tokenizer,
pad_to_multiple_of=8) if self.args.train_as_classification else collator(
self.tokenizer, pad_to_multiple_of=8),
eval_samples=eval_samples,
dev_samples=dev_samples,
evaluate_func=self.evaluate,
perturb_module_regex=perturb_module_regex,
)
if self.args.save_on_interrupt:
trainer.add_callback(SIGUSR1Callback())
# Resume training from a last checkpoint
last_checkpoint = None
from transformers.trainer_utils import get_last_checkpoint
if os.path.isdir(self.args.output_dir) and not self.args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(self.args.output_dir)
if last_checkpoint is not None and self.args.resume_from_checkpoint is None:
logger.info(f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch.")
if self.args.resume_from_checkpoint is not None:
last_checkpoint = self.args.resume_from_checkpoint
# This calls the trainer._inner_training_loop()
trainer.train(resume_from_checkpoint=last_checkpoint)
# Explicitly save the model
if self.args.save_model:
logger.info("Save model..")
trainer.save_model()
# FSDP compatibility
self.model = trainer.model
# Reset the forward function for evaluation
if self.args.only_train_option and not self.args.non_diff:
if type(self.model) == FSDP:
logger.info("This is an FSDP model now. Be careful when assigning back the original forward function")
self.model._fsdp_wrapped_module.forward = self.model._fsdp_wrapped_module.original_forward
else:
self.model.forward = self.model.original_forward
def delete_checkpoints(self):
import shutil
print(f"\nWARNING: Removing everything at end: {self.args.output_dir}")
deleted_folders = [folder for folder in os.listdir(self.args.output_dir)
if os.path.isdir(os.path.join(self.args.output_dir, folder))
and folder.startswith("checkpoint-")]
for f in deleted_folders:
shutil.rmtree(os.path.join(self.args.output_dir, f))
print(f"deleted folders: ", deleted_folders)
def result_file_tag(args):
"""
Get the result file tag
"""
save_model_name = args.model_name.split("/")[-1]
sfc_tag = "-sfc" if args.sfc else ""
icl_sfc_tag = "-icl_sfc" if args.icl_sfc else ""
sample_eval_tag = "-sampleeval%d" % args.num_eval if args.num_eval is not None else ""
sample_train_tag = "-ntrain%d" % args.num_train if args.num_train > 0 else ""
sample_dev_tag = "-ndev%d" % args.num_dev if args.num_dev is not None else ""
customized_tag = f"-{args.tag}" if len(args.tag) > 0 else ""
return f"{args.task_name}-{save_model_name}" + sfc_tag + icl_sfc_tag + sample_eval_tag + sample_train_tag + sample_dev_tag + customized_tag
def main():
args = parse_args()
if args.prefix_tuning:
args.mode = "prefix"
elif args.lora:
args.mode = "lora"
elif args.prompt_tuning:
args.mode = "prompt"
else:
args.mode = "ft"
args.tag = f"{args.trainer}-{args.task_name}-{args.template_ver}-{args.model_name.split('/')[-1]}-OPTIM_{args.mode}-STEP{args.max_steps}-{args.optimizer}-LR{args.learning_rate}-{args.lr_scheduler_type}-ZOEPS{args.zo_eps}-Q{args.q}"
if args.trainer == "agzo":
args.tag += f"-AGZO_PI{args.agzo_power_iter_steps}" # AGZO: 记录power iteration次数
if getattr(args, "agzo_rank", 1) > 1:
args.tag += f"-AGZO_R{args.agzo_rank}"
if args.agzo_empty_cache_steps > 0:
args.tag += f"-AGZO_EC{args.agzo_empty_cache_steps}" # AGZO: 记录缓存清理频率
if args.agzo_debug_memory:
args.tag += "-AGZO_DBG"
args.tag = "momen" + args.tag if args.momentum > 0 else args.tag
args.tag = f"sparse_grad-{args.gradient_sparsity}-{args.sparse_gradient_group}-{args.sparse_gradient_resample_steps}-" + args.tag if args.gradient_sparsity is not None else args.tag
args.tag = f"module_perturb-{args.perturbed_module_level}-" + args.tag if args.module_wise_perturbation else args.tag
args.run_name = args.tag
args.output_dir = f"result/{args.tag}"
args.result_file = f"result/{args.tag}/results.json"
os.makedirs(args.output_dir, exist_ok=True)
args.logging_dir = os.path.join(args.output_dir, "logs")
os.makedirs(args.logging_dir, exist_ok=True)
# If user only needs metrics, disable checkpoint saving
args.save_strategy = "no" # user requests no checkpoints to save disk usage
args.save_model = False
args.save_total_limit = 0
args.load_best_model_at_end = False
try:
wandb.init(project='zo-bench', name=args.tag, config=args)
except Exception as e:
# If running in a non-interactive environment without WANDB API key,
# wandb raises a UsageError prompting for an API key. Fall back to
# offline mode so the script can continue.
logger.warning("W&B init failed (%s). Falling back to offline mode.", e)
os.environ.setdefault("WANDB_MODE", "offline")
try:
wandb.init(project='zo-bench', name=args.tag, config=args, mode="offline")
except Exception:
# If still failing, continue without wandb (user can disable reporting via args)
logger.warning("W&B offline init also failed; continuing without wandb.")
set_seed(args.seed)
task = get_task(args.task_name)
# This function samples both training and validation samples. The validation (dev) samples are also stored in "train_sets"
# Later the train_samples and dev_samples are separated
train_sets = task.sample_train_sets(num_train=args.num_train, num_dev=args.num_dev, num_eval=args.num_eval,
num_train_sets=args.num_train_sets, seed=args.train_set_seed)
# Initialize trainer and load model
framework = Framework(args, task)
# ZO-Bench Added
# We add these parameters to evaluate the model during the training.
# These two parameters will be used in the training loop
# args.task = task
# args.framework = framework
metrics = {}
if args.train_set_seed is not None or args.num_train_sets is not None:
# Training goes to this way
# Eval samples share one (or multiple) training set(s)
for train_set_id, train_samples in enumerate(train_sets):
train_set_seed = train_set_id if args.train_set_seed is None else args.train_set_seed
# Sample eval samples
if args.num_eval is not None:
eval_samples = task.sample_subset(data_split="valid", seed=train_set_seed, num=args.num_eval)
else:
eval_samples = task.valid_samples
metrics = {}
if args.trainer != "none":
# Here the training samples are seperated
if args.num_dev is not None:
# Dev samples
# assert args.num_dev + args.num_train <= len(train_samples), f"num_dev({args.num_dev})+num_train({args.num_train}) is more than actual num of training samples ({len(train_samples)})."
dev_samples = train_samples[-args.num_dev:]
train_samples = train_samples[:-args.num_dev]
logger.info("Dev samples: %d" % len(dev_samples))
logger.info("Train samples: %d" % len(train_samples))
else:
dev_samples = None
logger.info("Train samples: %d" % len(train_samples))
logger.info("No dev samples")
args.dev_samples = dev_samples
args.eval_samples = eval_samples
# Training
framework.train(train_samples, dev_samples if dev_samples is not None else eval_samples, eval_samples)
if not args.no_eval: # This is True
metrics = framework.evaluate([], eval_samples, description="Evaluating on the Test Set")
_keys = list(metrics.keys())
for m in _keys:
metrics["test_" + m] = metrics[m]
if dev_samples is not None:
dev_metrics = framework.evaluate(
[], dev_samples, description="Evaluating on the Validation Set"
)
_keys = list(dev_metrics.keys())
for m in _keys:
metrics["val_" + m] = dev_metrics[m]
else:
assert args.num_dev is None
# Zero-shot / in-context learning
metrics = framework.evaluate(train_samples, eval_samples)
logger.info(metrics)
wandb.log(
metrics,
step=framework.trainer.state.global_step if hasattr(framework, "trainer") else None,
)
if not args.no_eval:
logger.info("===== Train set %d =====" % train_set_seed)
logger.info(metrics)
wandb.log(metrics, step=framework.trainer.state.global_step if hasattr(framework, "trainer") else None)
if args.local_rank <= 0:
write_metrics_to_file(metrics, "result/" + result_file_tag(
args) + f"-trainset{train_set_id}.json" if args.result_file is None else args.result_file)
if args.trainer != "none" and args.clean_model_at_end:
framework.delete_checkpoints()
else:
# For each eval sample, there is a training set. no training is allowed
# This is for in-context learning (ICL)
assert args.trainer == "none"
if args.num_eval is not None:
eval_samples = task.sample_subset(data_split="valid", seed=0, num=args.num_eval)
else:
eval_samples = task.valid_samples
metrics = framework.evaluate(train_sets, eval_samples, one_train_set_per_eval_sample=True)
logger.info(metrics)
wandb.log(metrics, step=framework.trainer.state.global_step if hasattr(framework, "trainer") else None)
if args.local_rank <= 0:
write_metrics_to_file(metrics, "result/" + result_file_tag(
args) + "-onetrainpereval.json" if args.result_file is None else args.result_file)
if __name__ == "__main__":
main()