-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into t5_lm_adaptation
- Loading branch information
Showing
26 changed files
with
784 additions
and
73 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
64 changes: 64 additions & 0 deletions
64
examples/nlp/language_modeling/conf/megatron_t5_config_finetune_glue_mnli.yaml
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,64 @@ | ||
name: megatron_t5_glue | ||
|
||
trainer: | ||
gpus: 2 | ||
num_nodes: 1 | ||
accelerator: ddp | ||
precision: 16 | ||
logger: False # logger provided by exp_manager | ||
checkpoint_callback: False | ||
replace_sampler_ddp: False | ||
max_epochs: 3 | ||
max_steps: null # consumed_samples = global_step * micro_batch_size * data_parallel_size * accumulate_grad_batches | ||
log_every_n_steps: 10 | ||
val_check_interval: 300 | ||
accumulate_grad_batches: 2 | ||
gradient_clip_val: 1.0 | ||
|
||
exp_manager: | ||
explicit_log_dir: null | ||
exp_dir: null | ||
name: megatron_t5_glue | ||
create_wandb_logger: False | ||
wandb_logger_kwargs: | ||
project: null | ||
name: null | ||
resume_if_exists: True | ||
resume_ignore_no_checkpoint: True | ||
create_checkpoint_callback: True | ||
checkpoint_callback_params: | ||
monitor: val_acc | ||
save_top_k: 10 | ||
mode: max | ||
always_save_nemo: False # TODO: add support | ||
filename: 'megatron_t5--{val_acc:.3f}-{step}' | ||
model_parallel_size: ${model.tensor_model_parallel_size} | ||
save_best_model: True | ||
|
||
model: | ||
restore_from_path: ??? # Path to a trained T5 .nemo file | ||
tensor_model_parallel_size: 1 | ||
|
||
data: | ||
train_ds: | ||
task_name: 'mnli' | ||
file_path: ??? # Path to the TSV file for MNLI train ex: '/raid/Data/GLUE/MNLI/train.tsv' | ||
batch_size: 32 | ||
shuffle: True | ||
num_workers: 8 | ||
pin_memory: True | ||
max_seq_length: 512 | ||
|
||
validation_ds: | ||
task_name: 'mnli' | ||
file_path: ??? # Path to the TSV file for MNLI dev ex: '/raid/Data/GLUE/MNLI/dev_matched.tsv' | ||
batch_size: 32 | ||
shuffle: False | ||
num_workers: 8 | ||
pin_memory: True | ||
max_seq_length: 512 | ||
|
||
optim: | ||
name: fused_adam | ||
lr: 5e-6 | ||
weight_decay: 0.0 |
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,80 @@ | ||
# Copyright (c) 2022, 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. | ||
|
||
from pathlib import Path | ||
|
||
from omegaconf.omegaconf import OmegaConf, open_dict | ||
from pytorch_lightning import Trainer | ||
from pytorch_lightning.callbacks.timer import Timer | ||
from pytorch_lightning.plugins.environments.torchelastic_environment import TorchElasticEnvironment | ||
from pytorch_lightning.plugins.precision.native_amp import NativeMixedPrecisionPlugin | ||
from pytorch_lightning.trainer.connectors.checkpoint_connector import CheckpointConnector | ||
|
||
from nemo.collections.nlp.models.language_modeling.megatron_glue_model import MegatronT5GLUEModel | ||
from nemo.collections.nlp.modules.common.megatron.megatron_utils import compute_model_parallel_rank | ||
from nemo.collections.nlp.parts.nlp_overrides import GradScaler, NLPDDPPlugin | ||
from nemo.core.config import hydra_runner | ||
from nemo.utils import logging | ||
from nemo.utils.exp_manager import StatelessTimer, exp_manager | ||
|
||
|
||
@hydra_runner(config_path="conf", config_name="megatron_t5_config_finetune") | ||
def main(cfg) -> None: | ||
logging.info("\n\n************** Experiment configuration ***********") | ||
logging.info(f'\n{OmegaConf.to_yaml(cfg)}') | ||
|
||
plugins = [NLPDDPPlugin(num_nodes=cfg.trainer.num_nodes)] | ||
if cfg.trainer.precision == 16: | ||
scaler = GradScaler( | ||
init_scale=cfg.model.get('native_amp_init_scale', 2 ** 32), | ||
growth_interval=cfg.model.get('native_amp_growth_interval', 1000), | ||
) | ||
plugins.append(NativeMixedPrecisionPlugin(precision=16, device='cuda', scaler=scaler)) | ||
|
||
if cfg.get('cluster_type', None) == 'BCP': | ||
plugins.append(TorchElasticEnvironment()) | ||
|
||
trainer = Trainer(plugins=plugins, **cfg.trainer) | ||
|
||
exp_manager(trainer, cfg.exp_manager) | ||
|
||
# update resume from checkpoint found by exp_manager | ||
resume_from_checkpoint = trainer.checkpoint_connector.resume_from_checkpoint_fit_path | ||
if resume_from_checkpoint is not None: | ||
# inject mp_rank into resume_from_checkpoint | ||
if cfg.model.tensor_model_parallel_size is not None and cfg.model.tensor_model_parallel_size > 1: | ||
mp_rank = compute_model_parallel_rank(trainer.local_rank, cfg.model.tensor_model_parallel_size) | ||
resume_from_checkpoint = Path(resume_from_checkpoint) | ||
resume_from_checkpoint = resume_from_checkpoint.parent.parent.joinpath(f'mp_rank_{mp_rank:02d}').joinpath( | ||
resume_from_checkpoint.name | ||
) | ||
resume_from_checkpoint = str(resume_from_checkpoint) | ||
logging.info(f'Resuming training from checkpoint: {resume_from_checkpoint}') | ||
|
||
trainer.checkpoint_connector = CheckpointConnector(trainer, resume_from_checkpoint=resume_from_checkpoint) | ||
# Override timer callback to a stateless one | ||
for idx, callback in enumerate(trainer.callbacks): | ||
if isinstance(callback, Timer): | ||
trainer.callbacks[idx] = StatelessTimer(cfg.trainer.max_time,) | ||
|
||
# hydra interpolation does not work here as the interpolation key is lost when PTL saves hparams | ||
with open_dict(cfg): | ||
cfg.model.precision = cfg.trainer.precision | ||
model = MegatronT5GLUEModel(cfg.model, trainer) | ||
trainer.fit(model) | ||
trainer.validate(model) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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
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
Oops, something went wrong.