-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contant batch zero lr for loss spike debug
- Loading branch information
1 parent
6757a5e
commit b1efbea
Showing
4 changed files
with
250 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
|
||
import torch | ||
from megatron.core.optimizer import OptimizerConfig | ||
from transformers import AutoProcessor | ||
|
||
from nemo import lightning as nl | ||
from nemo.collections import llm, vlm | ||
from nemo.collections.common.tokenizers.huggingface.auto_tokenizer import AutoTokenizer | ||
from nemo.collections.multimodal.data.energon import SimpleMultiModalDataModule | ||
from nemo.collections.multimodal.data.energon.config import MultiModalSampleConfig | ||
from nemo.collections.vlm import ImageDataConfig, LlavaNextTaskEncoder | ||
from nemo.lightning.pytorch.optim import CosineAnnealingScheduler | ||
from nemo.lightning.pytorch.optim.megatron import MegatronOptimizerModule | ||
from nemo.utils.exp_manager import TimingCallback | ||
|
||
|
||
def main(args): | ||
# Global and micro batch sizes | ||
gbs = 2 | ||
mbs = 2 | ||
seq_length = 4096 | ||
|
||
processor = AutoProcessor.from_pretrained("llava-hf/llava-v1.6-vicuna-7b-hf") | ||
data_path = args.data_path | ||
image_processor = processor.image_processor | ||
# tokenizer = processor.tokenizer | ||
tokenizer = AutoTokenizer("llava-hf/llava-v1.6-vicuna-7b-hf") | ||
|
||
multimodal_sample_config = MultiModalSampleConfig() | ||
|
||
task_encoder = LlavaNextTaskEncoder( | ||
tokenizer=tokenizer.tokenizer, | ||
image_processor=image_processor, | ||
multimodal_sample_config=multimodal_sample_config, | ||
seq_length=seq_length, | ||
) | ||
data = SimpleMultiModalDataModule( | ||
path=data_path, | ||
tokenizer=tokenizer, | ||
image_processor=image_processor, | ||
num_workers=0, | ||
micro_batch_size=mbs, | ||
global_batch_size=gbs, | ||
multimodal_sample_config=multimodal_sample_config, | ||
task_encoder=task_encoder, | ||
) | ||
|
||
# Transformer configurations | ||
language_transformer_config = llm.Llama2Config7B() | ||
vision_transformer_config = vlm.HFCLIPVisionConfig( | ||
pretrained_model_name_or_path="openai/clip-vit-large-patch14-336" | ||
) | ||
vision_projection_config = vlm.MultimodalProjectorConfig( | ||
projector_type=args.projector_type, | ||
input_size=1024, | ||
hidden_size=4096, | ||
ffn_hidden_size=4096, | ||
) | ||
|
||
# NEVA model configuration | ||
neva_config = vlm.NevaConfig( | ||
language_transformer_config=language_transformer_config, | ||
vision_transformer_config=vision_transformer_config, | ||
vision_projection_config=vision_projection_config, | ||
language_model_from_pretrained=args.language_model_path, | ||
freeze_language_model=False, | ||
is_llava_next=True, | ||
) | ||
|
||
model = vlm.NevaModel(neva_config, tokenizer=data.tokenizer) | ||
|
||
strategy = nl.MegatronStrategy( | ||
tensor_model_parallel_size=args.tp_size, | ||
pipeline_model_parallel_size=args.pp_size, | ||
pipeline_dtype=torch.bfloat16, | ||
ckpt_load_optimizer=True, | ||
) | ||
|
||
# Checkpoint callback setup | ||
checkpoint_callback = nl.ModelCheckpoint( | ||
save_last=True, | ||
monitor="reduced_train_loss", | ||
save_top_k=2, | ||
every_n_train_steps=111, | ||
dirpath=args.log_dir, | ||
) | ||
|
||
# Trainer setup | ||
trainer = nl.Trainer( | ||
devices=args.devices, | ||
max_steps=5190, | ||
accelerator="gpu", | ||
strategy=strategy, | ||
plugins=nl.MegatronMixedPrecision(precision="bf16-mixed"), | ||
callbacks=[checkpoint_callback, TimingCallback()], | ||
val_check_interval=211, # 1000, | ||
limit_val_batches=gbs, # gbs, | ||
log_every_n_steps=1, | ||
num_sanity_val_steps=0, | ||
) | ||
|
||
# Logger setup | ||
from pytorch_lightning.loggers import WandbLogger | ||
|
||
nemo_logger = nl.NeMoLogger( | ||
log_dir=args.log_dir, | ||
name=args.name, | ||
wandb=WandbLogger(project=args.wandb_project, name=args.name) if args.wandb_project is not None else None, | ||
) | ||
nemo_logger.setup( | ||
trainer, | ||
resume_if_exists=True, | ||
) | ||
|
||
# Auto resume setup | ||
from nemo.lightning.pytorch.strategies.utils import RestoreConfig | ||
|
||
resume = nl.AutoResume( | ||
resume_if_exists=True, | ||
resume_ignore_no_checkpoint=True, | ||
# resume_from_directory=args.log_dir, | ||
restore_config=( | ||
RestoreConfig( | ||
path=args.restore_path, | ||
load_optim_state=False, | ||
) | ||
if args.restore_path is not None | ||
else None | ||
), | ||
) | ||
resume.setup(trainer, model) | ||
|
||
# Optimizer and scheduler setup | ||
opt_config = OptimizerConfig( | ||
optimizer='adam', | ||
lr=0, # ,2.0e-5 | ||
adam_beta1=0.9, | ||
adam_beta2=0.95, | ||
use_distributed_optimizer=False, | ||
bf16=True, | ||
) | ||
sched = CosineAnnealingScheduler( | ||
max_steps=trainer.max_steps, warmup_steps=150, constant_steps=0, min_lr=0 # 2.0e-07, | ||
) | ||
opt = MegatronOptimizerModule(opt_config, sched) | ||
opt.connect(model) | ||
|
||
# Start training | ||
|
||
trainer.fit(model, data) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="NEVA Model Training Script") | ||
|
||
# Argument parsing | ||
parser.add_argument("--data_path", type=str, required=True, help="Path to the dataset JSON file") | ||
parser.add_argument("--image_folder", type=str, required=True, help="Path to the image folder") | ||
parser.add_argument("--log_dir", type=str, required=True, help="Directory for logging and checkpoints") | ||
parser.add_argument( | ||
"--language_model_path", type=str, required=False, default=None, help="Path to the pretrained language model" | ||
) | ||
parser.add_argument( | ||
"--restore_path", type=str, required=False, default=None, help="Path to restore model from checkpoint" | ||
) | ||
parser.add_argument("--devices", type=int, required=False, default=8) | ||
# parser.add_argument("--tp_size", type=int, required=False, default=4) | ||
parser.add_argument("--tp_size", type=int, required=False, default=4) | ||
parser.add_argument("--pp_size", type=int, required=False, default=1) | ||
parser.add_argument("--projector_type", type=str, required=False, default="mlp2x_gelu") | ||
parser.add_argument("--name", type=str, required=False, default="neva_finetune") | ||
parser.add_argument("--wandb_project", type=str, required=False, default=None) | ||
|
||
args = parser.parse_args() | ||
main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters