File size: 17,430 Bytes
625a17f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
import os
import torch
import shutil
from transformers import Trainer
from transformers.modeling_utils import unwrap_model
from transformers.models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
import torch.distributed as dist
from typing import Optional
from torch import nn
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
from transformers.utils import is_sagemaker_mp_enabled, is_apex_available, is_torch_tpu_available,is_accelerate_available
if is_apex_available():
from apex import amp
if is_sagemaker_mp_enabled():
from transformers.trainer_pt_utils import smp_forward_backward
import contextlib
import copy
import functools
import glob
import importlib.metadata
import inspect
import math
import os
import random
import re
import shutil
import sys
import tempfile
import time
import warnings
from collections.abc import Mapping
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
import torch
from packaging import version
from torch.utils.data import DataLoader, Dataset, RandomSampler, SequentialSampler
from transformers.integrations.deepspeed import deepspeed_init, deepspeed_load_checkpoint, is_deepspeed_available
from transformers.modelcard import TrainingSummary
from transformers.modeling_utils import PreTrainedModel, load_sharded_checkpoint, unwrap_model
from transformers.models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.trainer_callback import (
CallbackHandler,
DefaultFlowCallback,
PrinterCallback,
ProgressCallback,
TrainerCallback,
TrainerControl,
TrainerState,
)
from transformers.utils import (
ADAPTER_CONFIG_NAME,
ADAPTER_SAFE_WEIGHTS_NAME,
ADAPTER_WEIGHTS_NAME,
CONFIG_NAME,
SAFE_WEIGHTS_INDEX_NAME,
SAFE_WEIGHTS_NAME,
WEIGHTS_INDEX_NAME,
WEIGHTS_NAME,
PushInProgress,
can_return_loss,
find_labels,
is_accelerate_available,
is_apex_available,
is_bitsandbytes_available,
is_datasets_available,
is_in_notebook,
is_ipex_available,
is_peft_available,
is_safetensors_available,
is_sagemaker_dp_enabled,
is_sagemaker_mp_enabled,
is_torch_compile_available,
is_torch_neuroncore_available,
is_torch_npu_available,
is_torch_tpu_available,
logging,
strtobool,
)
DEFAULT_CALLBACKS = [DefaultFlowCallback]
DEFAULT_PROGRESS_CALLBACK = ProgressCallback
if is_in_notebook():
from transformers.utils.notebook import NotebookProgressCallback
DEFAULT_PROGRESS_CALLBACK = NotebookProgressCallback
if is_apex_available():
from apex import amp
if is_datasets_available():
import datasets
if is_torch_tpu_available(check_device=False):
import torch_xla.core.xla_model as xm
import torch_xla.debug.metrics as met
if is_sagemaker_mp_enabled():
import smdistributed.modelparallel.torch as smp
from smdistributed.modelparallel import __version__ as SMP_VERSION
IS_SAGEMAKER_MP_POST_1_10 = version.parse(SMP_VERSION) >= version.parse("1.10")
from transformers.trainer_pt_utils import smp_forward_backward, smp_forward_only, smp_gather, smp_nested_concat
else:
IS_SAGEMAKER_MP_POST_1_10 = False
if is_safetensors_available():
import safetensors.torch
if is_peft_available():
from peft import PeftModel
if is_accelerate_available():
from accelerate import Accelerator, skip_first_batches
from accelerate import __version__ as accelerate_version
from accelerate.utils import (
DistributedDataParallelKwargs,
GradientAccumulationPlugin,
load_fsdp_model,
load_fsdp_optimizer,
save_fsdp_model,
save_fsdp_optimizer,
)
DATA_SAMPLERS = [RandomSampler]
if version.parse(accelerate_version) > version.parse("0.23.0"):
from accelerate.data_loader import SeedableRandomSampler
DATA_SAMPLERS += [SeedableRandomSampler]
if is_deepspeed_available():
from accelerate.utils import DeepSpeedSchedulerWrapper
if TYPE_CHECKING:
import optuna
logger = logging.get_logger(__name__)
# Name of the files used for checkpointing
TRAINING_ARGS_NAME = "training_args.bin"
TRAINER_STATE_NAME = "trainer_state.json"
OPTIMIZER_NAME = "optimizer.pt"
OPTIMIZER_NAME_BIN = "optimizer.bin"
SCHEDULER_NAME = "scheduler.pt"
SCALER_NAME = "scaler.pt"
FSDP_MODEL_NAME = "pytorch_model_fsdp"
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
class LLaVATrainerSSL(Trainer):
def _save_checkpoint(self, model, trial, metrics=None):
if getattr(self.args, 'tune_mm_mlp_adapter', False):
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']
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(LLaVATrainerSSL, self)._save_checkpoint(model, trial, metrics)
def _save(self, output_dir: Optional[str] = None, state_dict=None):
if getattr(self.args, 'tune_mm_mlp_adapter', False):
pass
else:
super(LLaVATrainerSSL, self)._save(output_dir, state_dict)
def update_history_loss_dict(self,outputs):
if not hasattr(self,'history_loss_dict'):
self.history_loss_dict = {}
for name, value in outputs.items():
if 'loss' in name and name != 'loss':
if name not in self.history_loss_dict:
self.history_loss_dict[name] = value.item()
else:
if value != 0:
self.history_loss_dict[name] = value.item()
def compute_loss(self, model, inputs, return_outputs=False):
"""
How the loss is computed by Trainer. By default, all models return the loss in the first element.
Subclass and override for custom behavior.
"""
if self.label_smoother is not None and "labels" in inputs:
labels = inputs.pop("labels")
else:
labels = None
# print(inputs.keys())
outputs = model(**inputs)
#print('Llava_trainer_SSL: compute loss here')
# Save past state if it exists
# TODO: this needs to be fixed and made cleaner later.
if self.args.past_index >= 0:
self._past = outputs[self.args.past_index]
if labels is not None:
if unwrap_model(model)._get_name() in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES.values():
loss = self.label_smoother(outputs, labels, shift_labels=True)
else:
loss = self.label_smoother(outputs, labels)
#print('Llava_trainer_SSL: loss1:', loss) #NO
else:
if isinstance(outputs, dict) and "loss" not in outputs:
raise ValueError(
"The model did not return a loss from the inputs, only the following keys: "
f"{','.join(outputs.keys())}. For reference, the inputs it received are {','.join(inputs.keys())}."
)
# We don't use .loss here since the model may return tuples instead of ModelOutput # Yes
loss = outputs["loss"] if isinstance(outputs, dict) else outputs[0]
#print('Llava_trainer_SSL: loss2:', loss)
if isinstance(outputs, dict) and 'loss_dice' in outputs:
loss_dict = {}
for name,value in outputs.items():
if 'loss' in name and name != 'loss':
loss_value = value.item()
if loss_value == 0 and hasattr(self,'history_loss_dict'):
loss_value = self.history_loss_dict[name]
loss_dict[name] = loss_value
self.update_history_loss_dict(outputs)
self.log(loss_dict)
return (loss, outputs) if return_outputs else loss
class LLaVATrainer(Trainer):
# def training_step(self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]) -> torch.Tensor:
# """
# Perform a training step on a batch of inputs.
#
# Subclass and override to inject custom behavior.
#
# Args:
# model (`nn.Module`):
# The model to train.
# inputs (`Dict[str, Union[torch.Tensor, Any]]`):
# The inputs and targets of the model.
#
# The dictionary will be unpacked before being fed to the model. Most models expect the targets under the
# argument `labels`. Check your model's documentation for all accepted arguments.
#
# Return:
# `torch.Tensor`: The tensor with training loss on this batch.
# """
# model.train()
# inputs = self._prepare_inputs(inputs)
# if dist.is_available():
# dist.barrier()
# import ipdb;ipdb.set_trace()
# if hasattr(self.train_dataset,'cur_dataset_index'):
# self.train_dataset.update_dataset_index()
# print(self.train_dataset.cur_dataset_index)
#
#
# if is_sagemaker_mp_enabled():
# loss_mb = smp_forward_backward(model, inputs, self.args.gradient_accumulation_steps)
# return loss_mb.reduce_mean().detach().to(self.args.device)
#
# with self.compute_loss_context_manager():
# loss = self.compute_loss(model, inputs)
#
# if self.args.n_gpu > 1:
# loss = loss.mean() # mean() to average on multi-gpu parallel training
#
# if self.use_apex:
# with amp.scale_loss(loss, self.optimizer) as scaled_loss:
# scaled_loss.backward()
# else:
# self.accelerator.backward(loss)
#
# return loss.detach() / self.args.gradient_accumulation_steps
def _save_checkpoint(self, model, trial, metrics=None):
if getattr(self.args, 'tune_mm_mlp_adapter', False):
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']
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 getattr(self.args, 'tune_mm_mlp_adapter', False):
pass
else:
super(LLaVATrainer, self)._save(output_dir, state_dict)
def update_history_loss_dict(self,outputs):
if not hasattr(self,'history_loss_dict'):
self.history_loss_dict = {}
for name, value in outputs.items():
if 'loss' in name and name != 'loss':
if name not in self.history_loss_dict:
self.history_loss_dict[name] = value.item()
else:
if value != 0:
self.history_loss_dict[name] = value.item()
def compute_loss(self, model, inputs, return_outputs=False):
"""
How the loss is computed by Trainer. By default, all models return the loss in the first element.
Subclass and override for custom behavior.
"""
if self.label_smoother is not None and "labels" in inputs:
labels = inputs.pop("labels")
else:
labels = None
# print(inputs.keys())
outputs = model(**inputs)
# Save past state if it exists
# TODO: this needs to be fixed and made cleaner later.
if self.args.past_index >= 0:
self._past = outputs[self.args.past_index]
if labels is not None:
if unwrap_model(model)._get_name() in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES.values():
loss = self.label_smoother(outputs, labels, shift_labels=True)
else:
loss = self.label_smoother(outputs, labels)
else:
if isinstance(outputs, dict) and "loss" not in outputs:
raise ValueError(
"The model did not return a loss from the inputs, only the following keys: "
f"{','.join(outputs.keys())}. For reference, the inputs it received are {','.join(inputs.keys())}."
)
# We don't use .loss here since the model may return tuples instead of ModelOutput.
loss = outputs["loss"] if isinstance(outputs, dict) else outputs[0]
if isinstance(outputs, dict) and 'loss_dice' in outputs:
loss_dict = {}
for name,value in outputs.items():
if 'loss' in name and name != 'loss':
loss_value = value.item()
if loss_value == 0 and hasattr(self,'history_loss_dict'):
loss_value = self.history_loss_dict[name]
loss_dict[name] = loss_value
self.update_history_loss_dict(outputs)
# loss_mask = outputs["loss_mask"].item() if isinstance(outputs, dict) else 0
# loss_dice = outputs["loss_dice"].item() if isinstance(outputs, dict) else 0
# loss_SEG_class = outputs["loss_SEG_class"].item() if isinstance(outputs, dict) else 0
# loss_class_name_class = outputs["loss_class_name_class"].item() if isinstance(outputs, dict) else 0
# loss_dict = {
# 'loss_mask':loss_mask,
# 'loss_dice': loss_dice,
# 'loss_SEG_class':loss_SEG_class,
# 'loss_class_name_class': loss_class_name_class
# }
self.log(loss_dict)
return (loss, outputs) if return_outputs else loss
# def training_step(self, model, inputs) -> torch.Tensor:
# """
# Perform a training step on a batch of inputs.
#
# Subclass and override to inject custom behavior.
#
# Args:
# model (`nn.Module`):
# The model to train.
# inputs (`Dict[str, Union[torch.Tensor, Any]]`):
# The inputs and targets of the model.
#
# The dictionary will be unpacked before being fed to the model. Most models expect the targets under the
# argument `labels`. Check your model's documentation for all accepted arguments.
#
# Return:
# `torch.Tensor`: The tensor with training loss on this batch.
# """
# model.train()
# inputs = self._prepare_inputs(inputs)
#
# if is_sagemaker_mp_enabled():
# loss_mb = smp_forward_backward(model, inputs, self.args.gradient_accumulation_steps)
# return loss_mb.reduce_mean().detach().to(self.args.device)
#
# with self.compute_loss_context_manager():
# loss = self.compute_loss(model, inputs)
#
# if self.args.n_gpu > 1:
# loss = loss.mean() # mean() to average on multi-gpu parallel training
#
# if self.do_grad_scaling:
# self.scaler.scale(loss).backward()
# elif self.use_apex:
# with amp.scale_loss(loss, self.optimizer) as scaled_loss:
# scaled_loss.backward()
# else:
# self.accelerator.backward(loss)
#
# return loss.detach() / self.args.gradient_accumulation_steps |