|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +import torch.distributed.checkpoint as DCP |
| 7 | + |
| 8 | +from torchtrain.config_manager import JobConfig |
| 9 | +from torchtrain.datasets import create_tokenizer |
| 10 | +from torchtrain.float8_linear import build_fp8_linear |
| 11 | +from torchtrain.logging_utils import init_logger, logger |
| 12 | +from torchtrain.models import model_name_to_cls, model_name_to_tokenizer, models_config |
| 13 | + |
| 14 | +_is_local_logging = True |
| 15 | +if "SLURM_JOB_ID" in os.environ: |
| 16 | + _is_local_logging = False |
| 17 | + |
| 18 | + |
| 19 | +def main(job_config: JobConfig): |
| 20 | + init_logger() |
| 21 | + |
| 22 | + model_name = job_config.model.name |
| 23 | + |
| 24 | + # build tokenizer |
| 25 | + tokenizer_type = model_name_to_tokenizer[model_name] |
| 26 | + tokenizer = create_tokenizer(tokenizer_type, job_config.model.tokenizer_path) |
| 27 | + |
| 28 | + # build model (using meta init) |
| 29 | + model_cls = model_name_to_cls[model_name] |
| 30 | + model_config = models_config[model_name][job_config.model.flavor] |
| 31 | + model_config.vocab_size = tokenizer.n_words |
| 32 | + logger.info(f"Building {model_name} {job_config.model.flavor} with {model_config}") |
| 33 | + model = model_cls.from_model_args(model_config) |
| 34 | + |
| 35 | + # apply fp8 linear module swap |
| 36 | + if job_config.training.fp8_linear: |
| 37 | + build_fp8_linear(model, job_config) |
| 38 | + |
| 39 | + model.reset_parameters() |
| 40 | + |
| 41 | + checkpoint_id = os.path.join(job_config.training.checkpoint_folder, "step-0") |
| 42 | + logger.info(f"Creating seed (step-0) checkpoint in {checkpoint_id}") |
| 43 | + DCP.save( |
| 44 | + state_dict={ |
| 45 | + "model": model.state_dict(), |
| 46 | + }, |
| 47 | + checkpoint_id=checkpoint_id, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +""" |
| 52 | +1. how do i serialize enough info about the model config to ensure i don't try to load an incompatible checkpoint later? |
| 53 | + - maybe skip this. users responsible to manage their checkpoints, and we can partially help by managing their 'dump folder'? |
| 54 | +
|
| 55 | +2. would i apply fp8 before creating the seed or not? I think probably before |
| 56 | +3. can i skip optimizer in seed file? i think so. optimizer can later create its states from the model post-sharding |
| 57 | +""" |
| 58 | +if __name__ == "__main__": |
| 59 | + config = JobConfig() |
| 60 | + config.parse_args() |
| 61 | + main(config) |
0 commit comments