Add files using upload-large-folder tool
Browse filesFiles changed (20)
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--N0065--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--N0099--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--N0150--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0038--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0056--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0114--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0263--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0312--2d_none.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BraTS2020--BraTS20_Training_255--z_0146--0003_002.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BraTS2021--BraTS2021_00787--x_0086--0007_000.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BrainTumour--BRATS_034--x_0086--0003_000.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BrainTumour--BRATS_143--z_0139--0003_000.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1--BraTS2020--BraTS20_Training_224--x_0060--0003_001.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1ce--BraTS2019--HGG__BraTS19_TCIA02_118_1--z_0110--0002_001.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1ce--BraTS2021--BraTS2021_00320--y_0126--0007_000.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1w--BrainTumour--BRATS_283--x_0135--0003_000.pngAdded
- benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t2--BraTS2015--brats_2013_pat0007_1_54551--y_0122--0004_001.pngAdded
- 之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava/model/multimodal_encoder/eva_clip/model_configs/EVA-CLIP-18B.jsonAdded+27
- 之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava/train/llava_trainer.pyAdded+1305
- 之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava/utils.pyAdded+198
123456789101112131415161718192021222324252627{ "embed_dim": 1536, "vision_cfg": { "image_size": 224, "layers": 48, "width": 5120, "head_width": 128, "mlp_ratio": 5, "patch_size": 14, "eva_model_name": "eva-clip-18b-14-x", "drop_path_rate": 0, "qkv_bias": false, "xattn": true, "postnorm": true, "fusedLN": false, "use_rms_norm": true }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 1280, "heads": 20, "layers": 32, "xattn": false, "fusedLN": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305import osimport torchimport torch.nn as nnimport datetime
from accelerate import Acceleratorfrom accelerate.utils import InitProcessGroupKwargs, GradientAccumulationPluginfrom torch.utils.data import Dataset, Sampler, DataLoader
from trl.trainer import DPOTrainerfrom trl.trainer.utils import DPODataCollatorWithPadding
from transformers import Trainerfrom transformers.trainer import is_sagemaker_mp_enabled, get_parameter_names, has_length, ALL_LAYERNORM_LAYERS, logger, is_accelerate_available, is_datasets_available, GradientAccumulationPluginfrom transformers.trainer_utils import seed_workerfrom transformers.trainer_pt_utils import get_length_grouped_indices as get_length_grouped_indices_hffrom transformers.trainer_pt_utils import AcceleratorConfigfrom typing import List, Optionalfrom datetime import timedelta
if is_accelerate_available(): from accelerate import Accelerator, skip_first_batches, InitProcessGroupKwargs
if is_datasets_available(): import datasets
from llava.utils import rank0_print
def maybe_zero_3(param, ignore_status=False, name=None): from deepspeed import zero from deepspeed.runtime.zero.partition_parameters import ZeroParamStatus
if hasattr(param, "ds_id"): if param.ds_status == ZeroParamStatus.NOT_AVAILABLE: if not ignore_status: print(name, "no ignore status") with zero.GatheredParameters([param]): param = param.data.detach().cpu().clone() else: param = param.detach().cpu().clone() return param
def get_mm_adapter_state_maybe_zero_3(named_params, keys_to_match): to_return = {k: t for k, t in named_params if any(key_match in k for key_match in keys_to_match)} to_return = {k: maybe_zero_3(v, ignore_status=True, name=k).cpu() for k, v in to_return.items()} return to_return
def should_only_save_mm_adapter(args) -> bool: # LoRA runs need full PEFT checkpoint handling, even when adapters are also trainable. if getattr(args, "lora_enable", False): return False
if getattr(args, "tune_mm_mlp_adapter", False): return True
mm_tunable_parts = getattr(args, "mm_tunable_parts", None) if not mm_tunable_parts: return False
tunable_parts = [part.strip() for part in mm_tunable_parts.split(",") if part.strip()] return len(tunable_parts) == 1 and tunable_parts[0] in {"mm_mlp_adapter", "mm_vision_resampler"}
def split_to_even_chunks(indices, lengths, num_chunks): """ Split a list of indices into `chunks` chunks of roughly equal lengths. """
if len(indices) % num_chunks != 0: return [indices[i::num_chunks] for i in range(num_chunks)]
num_indices_per_chunk = len(indices) // num_chunks
chunks = [[] for _ in range(num_chunks)] chunks_lengths = [0 for _ in range(num_chunks)] for index in indices: shortest_chunk = chunks_lengths.index(min(chunks_lengths)) chunks[shortest_chunk].append(index) chunks_lengths[shortest_chunk] += lengths[index] if len(chunks[shortest_chunk]) == num_indices_per_chunk: chunks_lengths[shortest_chunk] = float("inf")
return chunks
def get_variable_length_grouped_indices(lengths, batch_size, world_size, megabatch_mult=8, generator=None): # We need to use torch for the random part as a distributed sampler will set the random seed for torch. indices = torch.randperm(len(lengths), generator=generator) sorted_indices = sorted(range(len(lengths)), key=lambda i: lengths[i], reverse=True) megabatch_size = world_size * batch_size * megabatch_mult megabatches = [sorted_indices[i : i + megabatch_size] for i in range(0, len(lengths), megabatch_size)] megabatches = [sorted(megabatch, key=lambda i: indices[i], reverse=True) for megabatch in megabatches] shuffled_indices = [i for megabatch in megabatches for i in megabatch] world_batch_size = world_size * batch_size batches = [shuffled_indices[i : i + world_batch_size] for i in range(0, len(lengths), world_batch_size)] batch_indices = torch.randperm(len(batches), generator=generator) batches = [batches[i] for i in batch_indices]
return [i for batch in batches for i in batch]
def get_modality_length_grouped_indices(lengths, batch_size, world_size, generator=None): """ Return a list of indices so that each slice of `batch_size` consecutive indices correspond to elements of similar lengths. To do this, the indices are:
- randomly permuted - grouped in mega-batches of size `mega_batch_mult * batch_size` - reorder by length in each mega-batch
The result is the concatenation of all mega-batches, with the batch of `batch_size` containing the element of maximum length placed first, so that an OOM happens sooner rather than later. """
# We need to use torch for the random part as a distributed sampler will set the random seed for torch. assert all(l != 0 for l in lengths), "Should not have zero length." if all(l > 0 for l in lengths) or all(l < 0 for l in lengths): # all samples are in the same modality return get_length_grouped_indices(lengths, batch_size, world_size, generator=generator) mm_indices, mm_lengths = zip(*[(i, l) for i, l in enumerate(lengths) if l > 0]) lang_indices, lang_lengths = zip(*[(i, -l) for i, l in enumerate(lengths) if l < 0])
mm_shuffle = [mm_indices[i] for i in get_length_grouped_indices(mm_lengths, batch_size, world_size, generator=None)] lang_shuffle = [lang_indices[i] for i in get_length_grouped_indices(lang_lengths, batch_size, world_size, generator=None)] megabatch_size = world_size * batch_size mm_megabatches = [mm_shuffle[i : i + megabatch_size] for i in range(0, len(mm_shuffle), megabatch_size)] lang_megabatches = [lang_shuffle[i : i + megabatch_size] for i in range(0, len(lang_shuffle), megabatch_size)]
last_mm = mm_megabatches[-1] last_lang = lang_megabatches[-1] additional_batch = last_mm + last_lang megabatches = mm_megabatches[:-1] + lang_megabatches[:-1] megabatch_indices = torch.randperm(len(megabatches), generator=generator) megabatches = [megabatches[i] for i in megabatch_indices]
if len(additional_batch) > 0: megabatches.append(sorted(additional_batch))
return [i for megabatch in megabatches for i in megabatch]
def get_length_grouped_indices(lengths, batch_size, world_size, generator=None, merge=True): """ Return a list of indices so that each slice of `batch_size` consecutive indices correspond to elements of similar lengths. To do this, the indices are:
- randomly permuted - grouped in mega-batches of size `mega_batch_mult * batch_size` - reorder by length in each mega-batch
The result is the concatenation of all mega-batches, with the batch of `batch_size` containing the element of maximum length placed first, so that an OOM happens sooner rather than later. """
# We need to use torch for the random part as a distributed sampler will set the random seed for torch. indices = torch.randperm(len(lengths), generator=generator) megabatch_size = world_size * batch_size megabatches = [indices[i : i + megabatch_size].tolist() for i in range(0, len(lengths), megabatch_size)] megabatches = [sorted(megabatch, key=lambda i: lengths[i], reverse=True) for megabatch in megabatches] megabatches = [split_to_even_chunks(megabatch, lengths, world_size) for megabatch in megabatches]
return [i for megabatch in megabatches for batch in megabatch for i in batch]
def get_length_grouped_indices_auto_single(lengths, batch_size, world_size, generator=None): indices = get_length_grouped_indices_hf(lengths, batch_size * world_size, generator=generator)
megabatch_size = world_size * batch_size megabatches = [indices[i : i + megabatch_size] for i in range(0, len(lengths), megabatch_size)] megabatches = [sorted(megabatch, key=lambda i: lengths[i], reverse=True) for megabatch in megabatches] megabatches = [split_to_even_chunks(megabatch, lengths, world_size) for megabatch in megabatches]
# We need to use torch for the random part as a distributed sampler will set the random seed for torch. batch_indices = torch.randperm(len(megabatches), generator=generator) megabatches = [megabatches[i] for i in batch_indices]
return [i for megabatch in megabatches for batch in megabatch for i in batch]
def get_modality_length_grouped_indices_auto(lengths, batch_size, world_size, generator=None): # We need to use torch for the random part as a distributed sampler will set the random seed for torch. assert all(l != 0 for l in lengths), "Should not have zero length." if all(l > 0 for l in lengths) or all(l < 0 for l in lengths): # all samples are in the same modality return get_length_grouped_indices_auto_single(lengths, batch_size, world_size, generator=generator) mm_indices, mm_lengths = zip(*[(i, l) for i, l in enumerate(lengths) if l > 0]) lang_indices, lang_lengths = zip(*[(i, -l) for i, l in enumerate(lengths) if l < 0])
mm_shuffle = [mm_indices[i] for i in get_length_grouped_indices_auto_single(mm_lengths, batch_size, world_size, generator=None)] lang_shuffle = [lang_indices[i] for i in get_length_grouped_indices_auto_single(lang_lengths, batch_size, world_size, generator=None)] megabatch_size = world_size * batch_size mm_megabatches = [mm_shuffle[i : i + megabatch_size] for i in range(0, len(mm_shuffle), megabatch_size)] lang_megabatches = [lang_shuffle[i : i + megabatch_size] for i in range(0, len(lang_shuffle), megabatch_size)]
last_mm = mm_megabatches[-1] last_lang = lang_megabatches[-1] additional_batch = last_mm + last_lang megabatches = mm_megabatches[:-1] + lang_megabatches[:-1] megabatch_indices = torch.randperm(len(megabatches), generator=generator) megabatches = [megabatches[i] for i in megabatch_indices]
# FIXME: Hard code to avoid last batch mixed with different modalities # if len(additional_batch) > 0: # megabatches.append(sorted(additional_batch))
return [i for megabatch in megabatches for i in megabatch]
class LengthGroupedSampler(Sampler): r""" Sampler that samples indices in a way that groups together features of the dataset of roughly the same length while keeping a bit of randomness. """
def __init__( self, batch_size: int, world_size: int, lengths: Optional[List[int]] = None, generator=None, variable_length: bool = False, group_by_modality: bool = False, group_by_modality_auto: bool = False, ): if lengths is None: raise ValueError("Lengths must be provided.")
self.batch_size = batch_size self.world_size = world_size self.lengths = lengths self.generator = generator self.variable_length = variable_length self.group_by_modality = group_by_modality self.group_by_modality_auto = group_by_modality_auto
def __len__(self): return len(self.lengths)
def __iter__(self): if self.variable_length: assert not self.group_by_modality, "Variable length grouping is not supported with modality grouping." indices = get_variable_length_grouped_indices(self.lengths, self.batch_size, self.world_size, generator=self.generator) else: if self.group_by_modality: indices = get_modality_length_grouped_indices(self.lengths, self.batch_size, self.world_size, generator=self.generator) elif self.group_by_modality_auto: indices = get_modality_length_grouped_indices_auto(self.lengths, self.batch_size, self.world_size, generator=self.generator) else: indices = get_length_grouped_indices_auto_single(self.lengths, self.batch_size, self.world_size, generator=self.generator) return iter(indices)
class LLaVATrainer(Trainer): def __init__(self, **kwargs): """ Initialize the LLaVATrainer with MeZO support.
Args: trainer_mode (str): Mode of training (`regular`, `zo`). zo_eps (float): MeZO hyperparameter epsilon. zo_num_directions (int): Number of directions for zero-order optimization. """ super().__init__(**kwargs)
args = self.args
self.trainer_mode = getattr(args, "trainer_mode", "regular") self.zo_eps = getattr(args, "zo_eps", 1e-3) self.zo_num_directions = getattr(args, "zo_num_directions", 1)
print(f"MeZOLLaVATrainer initialized with mode: {self.trainer_mode}") self.projected_grad = None self.zo_random_seed = None
self.zo_direction_accumulator = [] self.zo_accumulation_count = 0
self.batch_zo_seeds = None
self.trainable_params = [(p[0], p[1]) for p in self.model.named_parameters() if p[1].requires_grad]
print(f"Trainable parameters amount: {len(self.trainable_params)}")
if not self.trainable_params: raise ValueError("No trainable parameters found.")
if self.trainer_mode == "zo": for param in self.model.parameters(): param.requires_grad = False # Ensure no accidental gradient computation
self.mezo_update_history = []
######################## # MeZO-specific Methods ########################
def zo_perturb_parameters(self, scaling_factor=1.0): """ Perturb the model parameters with a random vector z.
Args: scaling_factor (float): Scaling factor for the perturbation. """ torch.manual_seed(self.zo_random_seed) for name, param in self.trainable_params: z = torch.normal( mean=0, std=1, size=param.data.size(), device=param.device, dtype=param.dtype ) param.data += scaling_factor * z * self.zo_eps
def zo_forward(self, model, inputs): """ Compute the loss.
Args: model (nn.Module): The model. inputs (Dict): Batch of inputs.
Returns: torch.Tensor: The computed loss. """ model.eval() with torch.inference_mode(): inputs = self._prepare_inputs(inputs) with self.compute_loss_context_manager(): loss = self.compute_loss(model, inputs) if self.args.n_gpu > 1: loss = loss.mean() return loss.detach()
def zo_step(self, model, inputs): """ Estimate the gradient using MeZO by performing multiple directional forward passes.
Args: model (nn.Module): The model. inputs (Dict): Batch of inputs.
Returns: torch.Tensor: The loss from the first forward pass. """ directions = []
if self.batch_zo_seeds is None: self.batch_zo_seeds = [np.random.randint(1000000000) for _ in range(self.zo_num_directions)] print(f"Sampled batch seeds: {self.batch_zo_seeds}")
for seed in self.batch_zo_seeds: self.zo_random_seed = seed
self.zo_perturb_parameters(scaling_factor=1.0) loss_plus = self.zo_forward(model, inputs)
self.zo_perturb_parameters(scaling_factor=-2.0) loss_minus = self.zo_forward(model, inputs) print(f"Seed {seed} loss_plus: {loss_plus} loss_minus: {loss_minus}")
self.zo_perturb_parameters(scaling_factor=1.0)
grad_estimate = ((loss_plus - loss_minus) / (2 * self.zo_eps)).item()
directions.append((seed, grad_estimate))
update_entry = { "type": "zo_step", "global_step": self.state.global_step, "zo_eps": self.zo_eps, "seed": seed, }
self.mezo_update_history.append(update_entry)
self.zo_direction_accumulator.extend(directions) self.zo_accumulation_count += 1
return loss_plus / (self.args.gradient_accumulation_steps * self.zo_num_directions)
def zo_update(self, learning_rate): """ Update model parameters based on the estimated gradient.
Args: learning_rate (float): The learning rate used for the update. """ if len(self.zo_direction_accumulator) == 0: print("No accumulated directions to update.") return
seed_group = {} for seed, grad_estimate in self.zo_direction_accumulator: if seed in seed_group: seed_group[seed] += grad_estimate / (self.args.gradient_accumulation_steps * self.zo_num_directions) else: seed_group[seed] = grad_estimate / (self.args.gradient_accumulation_steps * self.zo_num_directions)
for seed, grad_sum in seed_group.items(): torch.manual_seed(seed)
for name, param in self.trainable_params: z = torch.normal( mean=0, std=1, size=param.data.size(), device=param.device, dtype=param.dtype ) if "bias" not in name and "layer_norm" not in name and "layernorm" not in name: param.data -= learning_rate * (grad_sum * z + self.args.weight_decay * param.data) else: param.data -= learning_rate * grad_sum * z
print( f"Applied MeZO update with aggregated grad estimates " f"from {len(self.zo_direction_accumulator)} directions " f"(aggregated to {len(seed_group)} unique seeds)." f"Seed group: {seed_group}")
self.zo_direction_accumulator = [] self.zo_accumulation_count = 0 self.batch_zo_seeds = None
update_entry = { "type": "mezo_update", "global_step": self.state.global_step, "learning_rate": learning_rate, "seed_group": seed_group }
self.mezo_update_history.append(update_entry)
def _save_mezo_state(self, output_dir: str): """ Save the MeZO state checkpoint that includes the noise seeds and the accumulated grad estimates. """ mezo_state = { "weight_decay": self.args.weight_decay, "zo_eps": self.zo_eps, "zo_num_directions": self.zo_num_directions, "trainable_params_names": [name for name, _ in self.trainable_params], "trainable_params_sizes": {name: param.size() for name, param in self.trainable_params}, "update_history": self.mezo_update_history } mezo_checkpoint_path = os.path.join(output_dir, "mezo_state.pt") torch.save(mezo_state, mezo_checkpoint_path) print(f"MeZO checkpoint saved at {mezo_checkpoint_path}")
def save_model(self, output_dir: Optional[str] = None, _internal_call: bool = False): """ Override the save_model method to also store the lightweight MeZO checkpoint. """ ret = super().save_model(output_dir, _internal_call)
if self.trainer_mode == "zo": output_dir = output_dir if output_dir is not None else self.args.output_dir self._save_mezo_state(output_dir) return ret
######################## # MeZO-specific Methods ########################
def create_accelerator_and_postprocess(self): grad_acc_kwargs = {"num_steps": self.args.gradient_accumulation_steps} grad_acc_kwargs["sync_with_dataloader"] = False gradient_accumulation_plugin = GradientAccumulationPlugin(**grad_acc_kwargs)
accelerator_kwargs = InitProcessGroupKwargs(timeout=timedelta(weeks=52)) rank0_print("Setting NCCL timeout to INF to avoid running errors.")
# create accelerator object self.accelerator = Accelerator( dispatch_batches=self.args.dispatch_batches, split_batches=self.args.split_batches, deepspeed_plugin=self.args.deepspeed_plugin, gradient_accumulation_plugin=gradient_accumulation_plugin, kwargs_handlers=[accelerator_kwargs] ) # some Trainer classes need to use `gather` instead of `gather_for_metrics`, thus we store a flag self.gather_function = self.accelerator.gather_for_metrics
# deepspeed and accelerate flags covering both trainer args and accelerate launcher self.is_deepspeed_enabled = getattr(self.accelerator.state, "deepspeed_plugin", None) is not None self.is_fsdp_enabled = getattr(self.accelerator.state, "fsdp_plugin", None) is not None
# post accelerator creation setup if self.is_fsdp_enabled: fsdp_plugin = self.accelerator.state.fsdp_plugin fsdp_plugin.limit_all_gathers = self.args.fsdp_config.get("limit_all_gathers", fsdp_plugin.limit_all_gathers) if is_accelerate_available("0.23.0"): fsdp_plugin.activation_checkpointing = self.args.fsdp_config.get("activation_checkpointing", fsdp_plugin.activation_checkpointing) if fsdp_plugin.activation_checkpointing and self.args.gradient_checkpointing: raise ValueError("The activation_checkpointing in FSDP config and the gradient_checkpointing in training arg " "can't be set to True simultaneously. Please use FSDP's activation_checkpointing logic " "when using FSDP.")
if self.is_deepspeed_enabled and getattr(self.args, "hf_deepspeed_config", None) is None: self.propagate_args_to_deepspeed()
def _get_train_sampler(self) -> Optional[torch.utils.data.Sampler]: if self.train_dataset is None or not has_length(self.train_dataset): return None
if self.args.group_by_length: lengths = self.train_dataset.lengths return LengthGroupedSampler( # self.args.train_batch_size * self.args.gradient_accumulation_steps, # TODO: seems that we should not have gradient_accumulation_steps self.args.train_batch_size, # world_size=self.args.world_size, world_size=self.args.world_size * self.args.gradient_accumulation_steps, # TODO: seems that this may work? lengths=lengths, ) elif self.args.group_by_modality_length: lengths = self.train_dataset.modality_lengths return LengthGroupedSampler( # self.args.train_batch_size * self.args.gradient_accumulation_steps, # TODO: seems that we should not have gradient_accumulation_steps self.args.train_batch_size, # world_size=self.args.world_size, world_size=self.args.world_size * self.args.gradient_accumulation_steps, # TODO: seems that this may work? lengths=lengths, group_by_modality=True, ) elif self.args.group_by_modality_length_auto: lengths = self.train_dataset.modality_lengths return LengthGroupedSampler( # self.args.train_batch_size * self.args.gradient_accumulation_steps, # TODO: seems that we should not have gradient_accumulation_steps self.args.train_batch_size, # world_size=self.args.world_size, world_size=self.args.world_size * self.args.gradient_accumulation_steps, # TODO: seems that this may work? lengths=lengths, group_by_modality_auto=True, ) elif self.args.group_by_varlen: lengths = self.train_dataset.lengths return LengthGroupedSampler( self.args.train_batch_size * self.args.gradient_accumulation_steps, # self.args.train_batch_size, # TODO: seems that we should have gradient_accumulation_steps # world_size=self.args.world_size, world_size=self.args.world_size * self.args.gradient_accumulation_steps, # TODO: seems that this may work? lengths=lengths, variable_length=True, ) else: return super()._get_train_sampler()
def get_train_dataloader(self) -> DataLoader: """ Returns the training [`~torch.utils.data.DataLoader`].
Will use no sampler if `train_dataset` does not implement `__len__`, a random sampler (adapted to distributed training if necessary) otherwise.
Subclass and override this method if you want to inject some custom behavior. """ if self.train_dataset is None: raise ValueError("Trainer: training requires a train_dataset.")
train_dataset = self.train_dataset data_collator = self.data_collator if is_datasets_available() and isinstance(train_dataset, datasets.Dataset): train_dataset = self._remove_unused_columns(train_dataset, description="training") else: data_collator = self._get_collator_with_removed_columns(data_collator, description="training")
dataloader_params = { "batch_size": self._train_batch_size, "collate_fn": data_collator, "num_workers": self.args.dataloader_num_workers, "pin_memory": self.args.dataloader_pin_memory, "persistent_workers": self.args.dataloader_persistent_workers, }
if not isinstance(train_dataset, torch.utils.data.IterableDataset): dataloader_params["sampler"] = self._get_train_sampler() dataloader_params["drop_last"] = self.args.dataloader_drop_last dataloader_params["worker_init_fn"] = seed_worker dataloader_params["prefetch_factor"] = self.args.dataloader_num_workers * 2 if self.args.dataloader_num_workers != 0 else None
dataloader = self.accelerator.prepare(DataLoader(train_dataset, **dataloader_params))
return dataloader
def create_optimizer(self): """ Setup the optimizer.
We provide a reasonable default that works well. If you want to use something else, you can pass a tuple in the Trainer's init through `optimizers`, or subclass and override this method in a subclass. """ if self.trainer_mode == "zo": # Create a dummy optimizer with this dummy parameter. # This optimizer will never really be stepped using backprop gradients. # We use no optimizer for MeZO. All updates are done in the `zo` methods. dummy_param = torch.nn.Parameter(torch.zeros(1, device=self.args.device)) self.optimizer = torch.optim.AdamW([dummy_param], lr=self.args.learning_rate) return self.optimizer
if is_sagemaker_mp_enabled(): return super().create_optimizer()
opt_model = self.model
if self.optimizer is None: decay_parameters = get_parameter_names(opt_model, ALL_LAYERNORM_LAYERS) decay_parameters = [name for name in decay_parameters if "bias" not in name] lr_mapper = {} if self.args.mm_projector_lr is not None: lr_mapper["mm_projector"] = self.args.mm_projector_lr if self.args.mm_vision_tower_lr is not None: lr_mapper["vision_tower"] = self.args.mm_vision_tower_lr if len(lr_mapper) > 0: special_lr_parameters = [name for name, _ in opt_model.named_parameters() if any(module_keyword in name for module_keyword in lr_mapper)] optimizer_grouped_parameters = [ { "params": [p for n, p in opt_model.named_parameters() if (n in decay_parameters and n not in special_lr_parameters and p.requires_grad)], "weight_decay": self.args.weight_decay, }, { "params": [p for n, p in opt_model.named_parameters() if (n not in decay_parameters and n not in special_lr_parameters and p.requires_grad)], "weight_decay": 0.0, }, ] for module_keyword, lr in lr_mapper.items(): module_parameters = [name for name, _ in opt_model.named_parameters() if module_keyword in name] optimizer_grouped_parameters.extend( [ { "params": [p for n, p in opt_model.named_parameters() if (n in decay_parameters and n in module_parameters and p.requires_grad)], "weight_decay": self.args.weight_decay, "lr": lr, }, { "params": [p for n, p in opt_model.named_parameters() if (n not in decay_parameters and n in module_parameters and p.requires_grad)], "weight_decay": 0.0, "lr": lr, }, ] ) else: optimizer_grouped_parameters = [ { "params": [p for n, p in opt_model.named_parameters() if (n in decay_parameters and p.requires_grad)], "weight_decay": self.args.weight_decay, }, { "params": [p for n, p in opt_model.named_parameters() if (n not in decay_parameters and p.requires_grad)], "weight_decay": 0.0, }, ]
optimizer_cls, optimizer_kwargs = Trainer.get_optimizer_cls_and_kwargs(self.args)
self.optimizer = optimizer_cls(optimizer_grouped_parameters, **optimizer_kwargs) if optimizer_cls.__name__ == "Adam8bit": import bitsandbytes
manager = bitsandbytes.optim.GlobalOptimManager.get_instance()
skipped = 0 for module in opt_model.modules(): if isinstance(module, nn.Embedding): skipped += sum({p.data_ptr(): p.numel() for p in module.parameters()}.values()) logger.info(f"skipped {module}: {skipped/2**20}M params") manager.register_module_override(module, "weight", {"optim_bits": 32}) logger.debug(f"bitsandbytes: will optimize {module} in fp32") logger.info(f"skipped: {skipped/2**20}M params")
return self.optimizer
def _save_checkpoint(self, model, trial, metrics=None): if should_only_save_mm_adapter(self.args): from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR
checkpoint_folder = f"{PREFIX_CHECKPOINT_DIR}-{self.state.global_step}"
run_dir = self._get_output_dir(trial=trial) output_dir = os.path.join(run_dir, checkpoint_folder)
# Only save Adapter keys_to_match = ["mm_projector", "vision_resampler"] if getattr(self.args, "use_im_start_end", False): keys_to_match.extend(["embed_tokens", "embed_in"])
weight_to_save = get_mm_adapter_state_maybe_zero_3(self.model.named_parameters(), keys_to_match)
if self.args.local_rank == 0 or self.args.local_rank == -1: self.model.config.save_pretrained(output_dir) torch.save(weight_to_save, os.path.join(output_dir, f"mm_projector.bin")) else: super(LLaVATrainer, self)._save_checkpoint(model, trial, metrics)
def _save(self, output_dir: Optional[str] = None, state_dict=None): if should_only_save_mm_adapter(self.args): pass else: super(LLaVATrainer, self)._save(output_dir, state_dict)
def _inner_training_loop( self, batch_size=None, args=None, resume_from_checkpoint=None, trial=None, ignore_keys_for_eval=None ): if args is None: args = self.args
# Start of existing training loop setup self.accelerator.free_memory() self._train_batch_size = batch_size if self.args.auto_find_batch_size: if self.state.train_batch_size != self._train_batch_size: from accelerate.utils import release_memory
(self.model_wrapped,) = release_memory(self.model_wrapped) self.model_wrapped = self.model
# Check for DeepSpeed *after* the initial pass and modify the config if self.is_deepspeed_enabled: # Temporarily unset `self.args.train_batch_size` original_bs = self.args.per_device_train_batch_size self.args.per_device_train_batch_size = self._train_batch_size // max(1, self.args.n_gpu) self.propagate_args_to_deepspeed(True) self.args.per_device_train_batch_size = original_bs self.state.train_batch_size = self._train_batch_size logger.debug(f"Currently training with a batch size of: {self._train_batch_size}") # Data loader and number of training steps train_dataloader = self.get_train_dataloader() if self.is_fsdp_xla_v2_enabled: train_dataloader = tpu_spmd_dataloader(train_dataloader)
# Setting up training control variables: # number of training epochs: num_train_epochs # number of training steps per epoch: num_update_steps_per_epoch # total number of training steps to execute: max_steps total_train_batch_size = self._train_batch_size * args.gradient_accumulation_steps * args.world_size
len_dataloader = None num_train_tokens = None if has_length(train_dataloader): len_dataloader = len(train_dataloader) num_update_steps_per_epoch = len_dataloader // args.gradient_accumulation_steps num_update_steps_per_epoch = max(num_update_steps_per_epoch, 1) num_examples = self.num_examples(train_dataloader) if args.max_steps > 0: max_steps = args.max_steps num_train_epochs = args.max_steps // num_update_steps_per_epoch + int( args.max_steps % num_update_steps_per_epoch > 0 ) # May be slightly incorrect if the last batch in the training dataloader has a smaller size but it's # the best we can do. num_train_samples = args.max_steps * total_train_batch_size if args.include_tokens_per_second: num_train_tokens = ( self.num_tokens(train_dataloader, args.max_steps) * args.gradient_accumulation_steps ) else: max_steps = math.ceil(args.num_train_epochs * num_update_steps_per_epoch) num_train_epochs = math.ceil(args.num_train_epochs) num_train_samples = self.num_examples(train_dataloader) * args.num_train_epochs if args.include_tokens_per_second: num_train_tokens = self.num_tokens(train_dataloader) * args.num_train_epochs elif args.max_steps > 0: # Rely on max_steps when dataloader does not have a working size max_steps = args.max_steps # Setting a very large number of epochs so we go as many times as necessary over the iterator. num_train_epochs = sys.maxsize num_update_steps_per_epoch = max_steps num_examples = total_train_batch_size * args.max_steps num_train_samples = args.max_steps * total_train_batch_size if args.include_tokens_per_second: num_train_tokens = self.num_tokens(train_dataloader, args.max_steps) * args.gradient_accumulation_steps else: raise ValueError( "args.max_steps must be set to a positive value if dataloader does not have a length, was" f" {args.max_steps}" )
if DebugOption.UNDERFLOW_OVERFLOW in self.args.debug: if self.args.n_gpu > 1: # nn.DataParallel(model) replicates the model, creating new variables and module # references registered here no longer work on other gpus, breaking the module raise ValueError( "Currently --debug underflow_overflow is not supported under DP. Please use DDP" " (torchrun or torch.distributed.launch (deprecated))." ) else: debug_overflow = DebugUnderflowOverflow(self.model) # noqa
delay_optimizer_creation = is_sagemaker_mp_enabled() or self.is_fsdp_xla_enabled or self.is_fsdp_enabled
# We need to reset the scheduler, as its parameters may be different on subsequent calls if self._created_lr_scheduler: self.lr_scheduler = None self._created_lr_scheduler = False
if self.is_deepspeed_enabled: ######################## # Changed Code ######################## if self.trainer_mode == "zo": # We need to set the optimizer to dummy optimizer for MeZO self.optimizer = self.create_optimizer() else: self.optimizer, self.lr_scheduler = deepspeed_init(self, num_training_steps=max_steps) ######################## # Changed Code ########################
if not delay_optimizer_creation: self.create_optimizer_and_scheduler(num_training_steps=max_steps)
self.state = TrainerState() self.state.is_hyper_param_search = trial is not None self.state.train_batch_size = self._train_batch_size
# Compute absolute values for logging, eval, and save if given as ratio if args.logging_steps is not None: if args.logging_steps < 1: self.state.logging_steps = math.ceil(max_steps * args.logging_steps) else: self.state.logging_steps = args.logging_steps if args.eval_steps is not None: if args.eval_steps < 1: self.state.eval_steps = math.ceil(max_steps * args.eval_steps) else: self.state.eval_steps = args.eval_steps if args.save_steps is not None: if args.save_steps < 1: self.state.save_steps = math.ceil(max_steps * args.save_steps) else: self.state.save_steps = args.save_steps
# Activate gradient checkpointing if needed if args.gradient_checkpointing: if args.gradient_checkpointing_kwargs is None: gradient_checkpointing_kwargs = {} else: gradient_checkpointing_kwargs = args.gradient_checkpointing_kwargs
self.model.gradient_checkpointing_enable(gradient_checkpointing_kwargs=gradient_checkpointing_kwargs)
model = self._wrap_model(self.model_wrapped)
# as the model is wrapped, don't use `accelerator.prepare` # this is for unhandled cases such as # FSDP-XLA, SageMaker MP/DP, DataParallel, IPEX use_accelerator_prepare = True if model is self.model else False
if delay_optimizer_creation: if use_accelerator_prepare: self._fsdp_qlora_plugin_updates() self.model = self.accelerator.prepare(self.model) self.create_optimizer_and_scheduler(num_training_steps=max_steps)
# prepare using `accelerator` prepare if use_accelerator_prepare: self.model.train() if hasattr(self.lr_scheduler, "step"): if self.use_apex: model = self.accelerator.prepare(self.model) else: model, self.optimizer = self.accelerator.prepare(self.model, self.optimizer) else: # to handle cases wherein we pass "DummyScheduler" such as when it is specified in DeepSpeed config. model, self.optimizer, self.lr_scheduler = self.accelerator.prepare( self.model, self.optimizer, self.lr_scheduler )
if self.is_fsdp_enabled: self.model = self.model_wrapped = model
# for the rest of this function `model` is the outside model, whether it was wrapped or not if model is not self.model: self.model_wrapped = model
# backward compatibility if self.is_deepspeed_enabled: self.deepspeed = self.model_wrapped
# ckpt loading if resume_from_checkpoint is not None: if self.is_deepspeed_enabled: deepspeed_load_checkpoint( self.model_wrapped, resume_from_checkpoint, load_module_strict=not _is_peft_model(self.model) ) elif is_sagemaker_mp_enabled() or self.is_fsdp_enabled: self._load_from_checkpoint(resume_from_checkpoint, self.model_wrapped)
# Check if saved optimizer or scheduler states exist self._load_optimizer_and_scheduler(resume_from_checkpoint)
# important: at this point: # self.model is the Transformers Model # self.model_wrapped is DDP(Transformers Model), Deepspeed(Transformers Model), # FSDP(Transformers Model), Dynamo Optimized Module(Transformers Model) etc.
# Train! logger.info("***** Running training *****") logger.info(f" Num examples = {num_examples:,}") logger.info(f" Num Epochs = {num_train_epochs:,}") logger.info(f" Instantaneous batch size per device = {self.args.per_device_train_batch_size:,}") if self.args.per_device_train_batch_size != self._train_batch_size: logger.info(f" Training with DataParallel so batch size has been adjusted to: {self._train_batch_size:,}") logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_train_batch_size:,}") logger.info(f" Gradient Accumulation steps = {args.gradient_accumulation_steps}") logger.info(f" Total optimization steps = {max_steps:,}") logger.info(f" Number of trainable parameters = {get_model_param_count(model, trainable_only=True):,}")
self.state.epoch = 0 start_time = time.time() epochs_trained = 0 steps_trained_in_current_epoch = 0 steps_trained_progress_bar = None
# Check if continuing training from a checkpoint if resume_from_checkpoint is not None and os.path.isfile( os.path.join(resume_from_checkpoint, TRAINER_STATE_NAME) ): self.state = TrainerState.load_from_json(os.path.join(resume_from_checkpoint, TRAINER_STATE_NAME)) self.compare_trainer_and_checkpoint_args(self.args, self.state) epochs_trained = self.state.global_step // num_update_steps_per_epoch if not args.ignore_data_skip: steps_trained_in_current_epoch = self.state.global_step % (num_update_steps_per_epoch) steps_trained_in_current_epoch *= args.gradient_accumulation_steps else: steps_trained_in_current_epoch = 0
logger.info(" Continuing training from checkpoint, will skip to saved global_step") logger.info(f" Continuing training from epoch {epochs_trained}") logger.info(f" Continuing training from global step {self.state.global_step}") if not args.ignore_data_skip: logger.info( f" Will skip the first {epochs_trained} epochs then the first" f" {steps_trained_in_current_epoch} batches in the first epoch." )
# Update the references self.callback_handler.model = self.model self.callback_handler.optimizer = self.optimizer self.callback_handler.lr_scheduler = self.lr_scheduler self.callback_handler.train_dataloader = train_dataloader if self.hp_name is not None and self._trial is not None: # use self._trial because the SigOpt/Optuna hpo only call `_hp_search_setup(trial)` instead of passing trial # parameter to Train when using DDP. self.state.trial_name = self.hp_name(self._trial) if trial is not None: assignments = trial.assignments if self.hp_search_backend == HPSearchBackend.SIGOPT else trial self.state.trial_params = hp_params(assignments) else: self.state.trial_params = None # This should be the same if the state has been saved but in case the training arguments changed, it's safer # to set this after the load. self.state.max_steps = max_steps self.state.num_train_epochs = num_train_epochs self.state.is_local_process_zero = self.is_local_process_zero() self.state.is_world_process_zero = self.is_world_process_zero()
# tr_loss is a tensor to avoid synchronization of TPUs through .item() tr_loss = torch.tensor(0.0).to(args.device) # _total_loss_scalar is updated everytime .item() has to be called on tr_loss and stores the sum of all losses self._total_loss_scalar = 0.0 self._globalstep_last_logged = self.state.global_step model.zero_grad() grad_norm: Optional[float] = None
self.control = self.callback_handler.on_train_begin(args, self.state, self.control)
# Skip the first epochs_trained epochs to get the random state of the dataloader at the right point. if not args.ignore_data_skip: for epoch in range(epochs_trained): sampler = get_dataloader_sampler(train_dataloader) sampler_kinds = [RandomSampler] if version.parse(accelerate_version) > version.parse("0.23.0"): sampler_kinds.append(SeedableRandomSampler) is_random_sampler = isinstance(sampler, tuple(sampler_kinds)) if not is_random_sampler: # We just need to begin an iteration to create the randomization of the sampler. for _ in train_dataloader: break else: # Otherwise we need to call the whooooole sampler cause there is some random operation added # AT THE VERY END! sampler = sampler if sampler is not None else [] _ = list(sampler)
total_batched_samples = 0 for epoch in range(epochs_trained, num_train_epochs): epoch_iterator = train_dataloader if hasattr(epoch_iterator, "set_epoch"): epoch_iterator.set_epoch(epoch)
# Reset the past mems state at the beginning of each epoch if necessary. if args.past_index >= 0: self._past = None
steps_in_epoch = ( len(epoch_iterator) if len_dataloader is not None else args.max_steps * args.gradient_accumulation_steps ) self.control = self.callback_handler.on_epoch_begin(args, self.state, self.control)
if epoch == epochs_trained and resume_from_checkpoint is not None and steps_trained_in_current_epoch == 0: self._load_rng_state(resume_from_checkpoint)
rng_to_sync = False steps_skipped = 0 if steps_trained_in_current_epoch > 0: epoch_iterator = skip_first_batches(epoch_iterator, steps_trained_in_current_epoch) steps_skipped = steps_trained_in_current_epoch steps_trained_in_current_epoch = 0 rng_to_sync = True
step = -1 for step, inputs in enumerate(epoch_iterator): total_batched_samples += 1
if self.args.include_num_input_tokens_seen: main_input_name = getattr(self.model, "main_input_name", "input_ids") if main_input_name not in inputs: logger.warning( "Tried to track the number of tokens seen, however the current model is " "not configured properly to know what item is the input. To fix this, add " "a `main_input_name` attribute to the model class you are using." ) else: input_device = inputs[main_input_name].device self.state.num_input_tokens_seen += torch.sum( self.accelerator.gather( torch.tensor(inputs[main_input_name].numel(), device=input_device, dtype=torch.int64) ) ).item() if rng_to_sync: self._load_rng_state(resume_from_checkpoint) rng_to_sync = False
# Skip past any already trained steps if resuming training if steps_trained_in_current_epoch > 0: steps_trained_in_current_epoch -= 1 if steps_trained_progress_bar is not None: steps_trained_progress_bar.update(1) if steps_trained_in_current_epoch == 0: self._load_rng_state(resume_from_checkpoint) continue elif steps_trained_progress_bar is not None: steps_trained_progress_bar.close() steps_trained_progress_bar = None
if step % args.gradient_accumulation_steps == 0: self.control = self.callback_handler.on_step_begin(args, self.state, self.control)
######################## # MeZO Integration ######################## if self.trainer_mode == "zo": # Perform MeZO step tr_loss_step = self.zo_step(model, inputs) else: # Regular training step with gradient accumulation with self.accelerator.accumulate(model): tr_loss_step = self.training_step(model, inputs) print(f"Step {step} loss: {tr_loss_step}")
torch.cuda.empty_cache() # Releasing reserved memory seems not to work correctly, so do it manually ######################## # End of MeZO Integration ########################
# Accumulate loss if ( args.logging_nan_inf_filter and not is_torch_xla_available() and (torch.isnan(tr_loss_step) or torch.isinf(tr_loss_step)) ): # if loss is nan or inf simply add the average of previous logged losses tr_loss += tr_loss / (1 + self.state.global_step - self._globalstep_last_logged) else: if tr_loss.device != tr_loss_step.device: raise ValueError( f"Calculated loss must be on the original device: {tr_loss.device} but device in use is {tr_loss_step.device}" ) tr_loss += tr_loss_step
self.current_flos += float(self.floating_point_ops(inputs))
is_last_step_and_steps_less_than_grad_acc = ( steps_in_epoch <= args.gradient_accumulation_steps and (step + 1) == steps_in_epoch )
if ( total_batched_samples % args.gradient_accumulation_steps == 0 or # last step in epoch but step is always smaller than gradient_accumulation_steps is_last_step_and_steps_less_than_grad_acc ): ######################## # MeZO Integration ######################## if self.trainer_mode == "zo": self.zo_update(learning_rate=self._get_learning_rate()) self.lr_scheduler.step() ######################## # End of MeZO Integration ######################## else: # the `or` condition of `is_last_step_and_steps_less_than_grad_acc` is not covered # in accelerate. So, explicitly enable sync gradients to True in that case. if is_last_step_and_steps_less_than_grad_acc: self.accelerator.gradient_state._set_sync_gradients(True)
# Gradient clipping if args.max_grad_norm is not None and args.max_grad_norm > 0: # deepspeed does its own clipping
if is_sagemaker_mp_enabled() and args.fp16: _grad_norm = self.optimizer.clip_master_grads(args.max_grad_norm) elif self.use_apex: # Revert to normal clipping otherwise, handling Apex or full precision _grad_norm = nn.utils.clip_grad_norm_( amp.master_params(self.optimizer), args.max_grad_norm, ) else: _grad_norm = self.accelerator.clip_grad_norm_( model.parameters(), args.max_grad_norm, )
if ( is_accelerate_available() and self.accelerator.distributed_type == DistributedType.DEEPSPEED ): grad_norm = model.get_global_grad_norm() # In some cases the grad norm may not return a float if hasattr(grad_norm, "item"): grad_norm = grad_norm.item() else: grad_norm = _grad_norm
# Optimizer step self.optimizer.step() optimizer_was_run = not self.accelerator.optimizer_step_was_skipped if optimizer_was_run: # Delay optimizer scheduling until metrics are generated if not isinstance(self.lr_scheduler, torch.optim.lr_scheduler.ReduceLROnPlateau): self.lr_scheduler.step()
model.zero_grad()
self.state.global_step += 1 self.state.epoch = epoch + (step + 1 + steps_skipped) / steps_in_epoch self.control = self.callback_handler.on_step_end(args, self.state, self.control)
self._maybe_log_save_evaluate(tr_loss, grad_norm, model, trial, epoch, ignore_keys_for_eval) else: self.control = self.callback_handler.on_substep_end(args, self.state, self.control)
if self.control.should_epoch_stop or self.control.should_training_stop: # PyTorch/XLA relies on the data loader to insert the mark_step for # each step. Since we are breaking the loop early, we need to manually # insert the mark_step here. if is_torch_xla_available(): xm.mark_step() break
if step < 0: logger.warning( "There seems to be not a single sample in your epoch_iterator, stopping training at step" f" {self.state.global_step}! This is expected if you're using an IterableDataset and set" f" num_steps ({max_steps}) higher than the number of available samples." ) self.control.should_training_stop = True
self.control = self.callback_handler.on_epoch_end(args, self.state, self.control) self._maybe_log_save_evaluate(tr_loss, grad_norm, model, trial, epoch, ignore_keys_for_eval)
if DebugOption.TPU_METRICS_DEBUG in self.args.debug: if is_torch_xla_available(): # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report()) else: logger.warning( "You enabled PyTorch/XLA debug metrics but you don't have a TPU " "configured. Check your training configuration if this is unexpected." ) if self.control.should_training_stop: break
if args.past_index and hasattr(self, "_past"): # Clean the state at the end of training delattr(self, "_past")
logger.info("\n\nTraining completed. Do not forget to share your model on huggingface.co/models =)\n\n") if args.load_best_model_at_end and self.state.best_model_checkpoint is not None: # Wait for everyone to get here so we are sure the model has been saved by process 0. if is_torch_xla_available(): xm.rendezvous("load_best_model_at_end") elif args.parallel_mode == ParallelMode.DISTRIBUTED: dist.barrier() elif is_sagemaker_mp_enabled(): smp.barrier()
self._load_best_model()
# add remaining tr_loss self._total_loss_scalar += tr_loss.item() effective_global_step = max(self.state.global_step, 0.001) # Avoid ZeroDivisionError train_loss = self._total_loss_scalar / effective_global_step
metrics = speed_metrics( "train", start_time, num_samples=num_train_samples, num_steps=self.state.max_steps, num_tokens=num_train_tokens, ) self.store_flos() metrics["total_flos"] = self.state.total_flos metrics["train_loss"] = train_loss
self.is_in_train = False
self._memory_tracker.stop_and_update_metrics(metrics)
self.log(metrics)
run_dir = self._get_output_dir(trial) checkpoints_sorted = self._sorted_checkpoints(use_mtime=False, output_dir=run_dir)
# Delete the last checkpoint when save_total_limit=1 if it's different from the best checkpoint and process allowed to save. if self.args.should_save and self.state.best_model_checkpoint is not None and self.args.save_total_limit == 1: for checkpoint in checkpoints_sorted: if not os.path.samefile(checkpoint, self.state.best_model_checkpoint): logger.info(f"Deleting older checkpoint [{checkpoint}] due to args.save_total_limit") shutil.rmtree(checkpoint)
self.control = self.callback_handler.on_train_end(args, self.state, self.control)
# Wait for the checkpoint to be uploaded. self._finish_current_push()
# After training we make sure to retrieve back the original forward pass method # for the embedding layer by removing the forward post hook. if self.neftune_noise_alpha is not None: self._deactivate_neftune(self.model)
plot_graphs_based_on_log_history( log_history=self.state.log_history, output_dir=run_dir, metrics=["train_loss"], )
return TrainOutput(self.state.global_step, train_loss, metrics)
class LLaVADPOTrainer(DPOTrainer): def _get_train_sampler(self) -> Optional[torch.utils.data.Sampler]: if self.train_dataset is None or not has_length(self.train_dataset): return None
if self.args.group_by_modality_length: lengths = self.train_dataset.modality_lengths return LengthGroupedSampler( # self.args.train_batch_size * self.args.gradient_accumulation_steps, # TODO: seems that we should not have gradient_accumulation_steps self.args.train_batch_size, world_size=self.args.world_size, lengths=lengths, group_by_modality=True, ) else: return super()._get_train_sampler()
def _save_checkpoint(self, model, trial, metrics=None): if should_only_save_mm_adapter(self.args): from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR
checkpoint_folder = f"{PREFIX_CHECKPOINT_DIR}-{self.state.global_step}"
run_dir = self._get_output_dir(trial=trial) output_dir = os.path.join(run_dir, checkpoint_folder)
# Only save Adapter keys_to_match = ["mm_projector", "vision_resampler"] if getattr(self.args, "use_im_start_end", False): keys_to_match.extend(["embed_tokens", "embed_in"])
weight_to_save = get_mm_adapter_state_maybe_zero_3(self.model.named_parameters(), keys_to_match)
if self.args.local_rank == 0 or self.args.local_rank == -1: self.model.config.save_pretrained(output_dir) torch.save(weight_to_save, os.path.join(output_dir, f"mm_projector.bin")) else: # super(LLaVADPOTrainer, self)._save_checkpoint(model, trial, metrics) # print(type(model)) # from transformers.modeling_utils import unwrap_model # print(type(unwrap_model(model))) # print(unwrap_model(model).config) if self.args.lora_enable: from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR
checkpoint_folder = f"{PREFIX_CHECKPOINT_DIR}-{self.state.global_step}" run_dir = self._get_output_dir(trial=trial) output_dir = os.path.join(run_dir, checkpoint_folder) from transformers.modeling_utils import unwrap_model
unwrapped_model = unwrap_model(model) self.save_my_lora_ckpt(output_dir, self.args, unwrapped_model) else: super(LLaVADPOTrainer, self)._save_checkpoint(model, trial, metrics)
def _save(self, output_dir: Optional[str] = None, state_dict=None): if should_only_save_mm_adapter(self.args): pass else: super(LLaVADPOTrainer, self)._save(output_dir, state_dict)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198import datetimeimport loggingimport logging.handlersimport osimport sysimport numpy as np
import requests
from llava.constants import LOGDIR
server_error_msg = "**NETWORK ERROR DUE TO HIGH TRAFFIC. PLEASE REGENERATE OR REFRESH THIS PAGE.**"moderation_msg = "I am sorry. Your input may violate our content moderation guidelines. Please avoid using harmful or offensive content."
handler = None
import torch.distributed as dist
try: import av from decord import VideoReader, cpuexcept ImportError: print("Please install pyav to use video processing functions.")
def process_video_with_decord(video_file, data_args): vr = VideoReader(video_file, ctx=cpu(0), num_threads=1) total_frame_num = len(vr) video_time = total_frame_num / vr.get_avg_fps() avg_fps = round(vr.get_avg_fps() / data_args.video_fps) frame_idx = [i for i in range(0, total_frame_num, avg_fps)] frame_time = [i/avg_fps for i in frame_idx]
if data_args.frames_upbound > 0: if len(frame_idx) > data_args.frames_upbound or data_args.force_sample: uniform_sampled_frames = np.linspace(0, total_frame_num - 1, data_args.frames_upbound, dtype=int) frame_idx = uniform_sampled_frames.tolist() frame_time = [i/vr.get_avg_fps() for i in frame_idx] video = vr.get_batch(frame_idx).asnumpy() frame_time = ",".join([f"{i:.2f}s" for i in frame_time])
num_frames_to_sample = num_frames = len(frame_idx) # https://github.com/dmlc/decord/issues/208 vr.seek(0) return video, video_time, frame_time, num_frames_to_sample
def process_video_with_pyav(video_file, data_args): container = av.open(video_file) # !!! This is the only difference. Using auto threading container.streams.video[0].thread_type = "AUTO"
video_frames = [] for packet in container.demux(): if packet.stream.type == 'video': for frame in packet.decode(): video_frames.append(frame) total_frame_num = len(video_frames) video_time = video_frames[-1].time avg_fps = round(total_frame_num / video_time / data_args.video_fps) frame_idx = [i for i in range(0, total_frame_num, avg_fps)]
if data_args.frames_upbound > 0: if len(frame_idx) > data_args.frames_upbound: uniform_sampled_frames = np.linspace(0, total_frame_num - 1, data_args.frames_upbound, dtype=int) frame_idx = uniform_sampled_frames.tolist()
frames = [video_frames[i] for i in frame_idx] return np.stack([x.to_ndarray(format="rgb24") for x in frames])
def rank0_print(*args): if dist.is_initialized(): if dist.get_rank() == 0: print(f"Rank {dist.get_rank()}: ", *args) else: print(*args)
def rank_print(*args): if dist.is_initialized(): print(f"Rank {dist.get_rank()}: ", *args) else: print(*args)
def build_logger(logger_name, logger_filename): global handler
formatter = logging.Formatter( fmt="%(asctime)s | %(levelname)s | %(name)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S", )
# Set the format of root handlers if not logging.getLogger().handlers: logging.basicConfig(level=logging.INFO) logging.getLogger().handlers[0].setFormatter(formatter)
# Redirect stdout and stderr to loggers stdout_logger = logging.getLogger("stdout") stdout_logger.setLevel(logging.INFO) sl = StreamToLogger(stdout_logger, logging.INFO) sys.stdout = sl
stderr_logger = logging.getLogger("stderr") stderr_logger.setLevel(logging.ERROR) sl = StreamToLogger(stderr_logger, logging.ERROR) sys.stderr = sl
# Get logger logger = logging.getLogger(logger_name) logger.setLevel(logging.INFO)
# Add a file handler for all loggers if handler is None: os.makedirs(LOGDIR, exist_ok=True) filename = os.path.join(LOGDIR, logger_filename) handler = logging.handlers.TimedRotatingFileHandler(filename, when="D", utc=True) handler.setFormatter(formatter)
for name, item in logging.root.manager.loggerDict.items(): if isinstance(item, logging.Logger): item.addHandler(handler)
return logger
class StreamToLogger(object): """ Fake file-like stream object that redirects writes to a logger instance. """
def __init__(self, logger, log_level=logging.INFO): self.terminal = sys.stdout self.logger = logger self.log_level = log_level self.linebuf = ""
def __getattr__(self, attr): return getattr(self.terminal, attr)
def write(self, buf): temp_linebuf = self.linebuf + buf self.linebuf = "" for line in temp_linebuf.splitlines(True): # From the io.TextIOWrapper docs: # On output, if newline is None, any '\n' characters written # are translated to the system default line separator. # By default sys.stdout.write() expects '\n' newlines and then # translates them so this is still cross platform. if line[-1] == "\n": self.logger.log(self.log_level, line.rstrip()) else: self.linebuf += line
def flush(self): if self.linebuf != "": self.logger.log(self.log_level, self.linebuf.rstrip()) self.linebuf = ""
def disable_torch_init(): """ Disable the redundant torch default initialization to accelerate model creation. """ import torch
setattr(torch.nn.Linear, "reset_parameters", lambda self: None) setattr(torch.nn.LayerNorm, "reset_parameters", lambda self: None)
def violates_moderation(text): """ Check whether the text violates OpenAI moderation API. """ url = "https://api.openai.com/v1/moderations" headers = {"Content-Type": "application/json", "Authorization": "Bearer " + os.environ["OPENAI_API_KEY"]} text = text.replace("\n", "") data = "{" + '"input": ' + f'"{text}"' + "}" data = data.encode("utf-8") try: ret = requests.post(url, headers=headers, data=data, timeout=5) flagged = ret.json()["results"][0]["flagged"] except requests.exceptions.RequestException as e: print(f"######################### Moderation Error: {e} #########################") flagged = False except KeyError as e: print(f"######################### Moderation Error: {e} #########################") flagged = False
return flagged
def pretty_print_semaphore(semaphore): if semaphore is None: return "None" return f"Semaphore(value={semaphore._value}, locked={semaphore.locked()})"benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--N0065--2d_none.pngAdded

Large File Pointer Details
- SHA256: 6a58c5f0ff1fe94e7630b4368225e035652714da186a0ffef8fc31e04c720a43
- Pointer size: 132 Bytes
- Size of remote file: 3.55 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--N0099--2d_none.pngAdded

Large File Pointer Details
- SHA256: 2dc868b1610ca4812b5a14539bbc614d5293ab268d78a7d9b45911924858148b
- Pointer size: 132 Bytes
- Size of remote file: 3.35 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--N0150--2d_none.pngAdded

Large File Pointer Details
- SHA256: 5b14dd22081b31fd0a1ccfee7abfc778d0999a070c9d32518a4e2019f9d0fe41
- Pointer size: 132 Bytes
- Size of remote file: 3.78 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0038--2d_none.pngAdded

Large File Pointer Details
- SHA256: 70b5914a172d8eab216ee2504169c4b87be3229d65914e1b2d6a7c2894f7b18f
- Pointer size: 132 Bytes
- Size of remote file: 3.45 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0056--2d_none.pngAdded

Large File Pointer Details
- SHA256: df1be2265c48a6db69a094123c6e45dc4a51f8303e83fbe291cdb060f01a7e40
- Pointer size: 132 Bytes
- Size of remote file: 3.42 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0114--2d_none.pngAdded

Large File Pointer Details
- SHA256: f869840f1c53a1e56ec0dd9ecc2240497f74bdb175ea63c0f03920ed74d854f9
- Pointer size: 132 Bytes
- Size of remote file: 1.84 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0263--2d_none.pngAdded

Large File Pointer Details
- SHA256: aff706e9388376342641179c2a30021a83fca50526dfb21675dbeb3e65ee1a99
- Pointer size: 132 Bytes
- Size of remote file: 3.3 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/fundus_photography/images/fundus_photography--PALM19--V0312--2d_none.pngAdded

Large File Pointer Details
- SHA256: be01655349c5b0b1a394ee6ae0b8f637e92214327d68597c8b1f6badb80d5a7b
- Pointer size: 132 Bytes
- Size of remote file: 3.57 MB
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BraTS2020--BraTS20_Training_255--z_0146--0003_002.pngAdded

Large File Pointer Details
- SHA256: 3975473feb6bea3322c78f56073c901d2dda30d935b75ec9673bb5f2915ae238
- Pointer size: 128 Bytes
- Size of remote file: 431 Bytes
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BraTS2021--BraTS2021_00787--x_0086--0007_000.pngAdded

Large File Pointer Details
- SHA256: 47606a7544c028e48e4acab5d04840fbd9d411efae0ff74ef85eb81a712f97f0
- Pointer size: 128 Bytes
- Size of remote file: 893 Bytes
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BrainTumour--BRATS_034--x_0086--0003_000.pngAdded

Large File Pointer Details
- SHA256: e3ca3bdab351e3e9c72271454c600d6470d73eb185082aa4fc9f2a41f2d9863e
- Pointer size: 129 Bytes
- Size of remote file: 1.11 kB
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_flair--BrainTumour--BRATS_143--z_0139--0003_000.pngAdded

Large File Pointer Details
- SHA256: 0b9af65fa04aefe06d6bc4f002abe9eb9d0e2929130e5801f4c7f7771b3420f1
- Pointer size: 128 Bytes
- Size of remote file: 552 Bytes
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1--BraTS2020--BraTS20_Training_224--x_0060--0003_001.pngAdded

Large File Pointer Details
- SHA256: dd15fb8c42492fd6e737d5e2571589628030d87315390070ccf5720f72d658fa
- Pointer size: 128 Bytes
- Size of remote file: 706 Bytes
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1ce--BraTS2019--HGG__BraTS19_TCIA02_118_1--z_0110--0002_001.pngAdded

Large File Pointer Details
- SHA256: 23d9aabe7eba7aecfd50089b2eed7f240b5c78d347ed9e1c03415fcbefe93067
- Pointer size: 128 Bytes
- Size of remote file: 454 Bytes
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1ce--BraTS2021--BraTS2021_00320--y_0126--0007_000.pngAdded

Large File Pointer Details
- SHA256: dfe3b196d6ad7cca85c29b8d1b8c07297ad5fd6d3a9ca441047a48e6fdc56b1d
- Pointer size: 128 Bytes
- Size of remote file: 440 Bytes
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t1w--BrainTumour--BRATS_283--x_0135--0003_000.pngAdded

Large File Pointer Details
- SHA256: 36b07f4703cbcd08305a49b3c3aad424bcdb0f266d109c770cde638c53a73ee8
- Pointer size: 128 Bytes
- Size of remote file: 574 Bytes
benchmarks/VGMED/image_folder/SAMed2D_v1/mr/masks/mr_t2--BraTS2015--brats_2013_pat0007_1_54551--y_0122--0004_001.pngAdded

Large File Pointer Details
- SHA256: bbbacbcafe9cd9177f6691a873bfeb435f54d57644641669d7159e052f5c261d
- Pointer size: 128 Bytes
- Size of remote file: 479 Bytes
之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava/model/multimodal_encoder/eva_clip/model_configs/EVA-CLIP-18B.jsonAdded+27
之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava/train/llava_trainer.pyAdded+1305
之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava/utils.pyAdded+198