Instructions to use diwank/dial-t0-silicone with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use diwank/dial-t0-silicone with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("diwank/dial-t0-silicone") model = AutoModelForSeq2SeqLM.from_pretrained("diwank/dial-t0-silicone", device_map="auto") - Notebooks
- Google Colab
- Kaggle
feat: Add trained model
Browse filesSigned-off-by: Julep AI <developers@julep.ai>
Files changed (18)
- config.jsonAdded+32
- global_step1600/bf16_zero_pp_rank_0_mp_rank_00_optim_states.ptAdded+3
- global_step1600/bf16_zero_pp_rank_1_mp_rank_00_optim_states.ptAdded+3
- global_step1600/bf16_zero_pp_rank_2_mp_rank_00_optim_states.ptAdded+3
- global_step1600/bf16_zero_pp_rank_3_mp_rank_00_optim_states.ptAdded+3
- global_step1600/mp_rank_00_model_states.ptAdded+3
- latestAdded+1
- pytorch_model.binAdded+3
- rng_state_0.pthAdded+3
- rng_state_1.pthAdded+3
- rng_state_2.pthAdded+3
- rng_state_3.pthAdded+3
- special_tokens_map.jsonAdded+107
- tokenizer.jsonAdded
- tokenizer_config.jsonAdded+113
- trainer_state.jsonAdded+444
- training_args.binAdded+3
- zero_to_fp32.pyAdded+482
1234567891011121314151617181920212223242526272829303132{ "_name_or_path": "prakharz/DIAL-T0", "architectures": [ "T5ForConditionalGeneration" ], "d_ff": 5120, "d_kv": 64, "d_model": 2048, "decoder_start_token_id": 0, "dense_act_fn": "gelu_new", "dropout_rate": 0.1, "eos_token_id": 1, "feed_forward_proj": "gated-gelu", "gradient_checkpointing": false, "initializer_factor": 1.0, "is_encoder_decoder": true, "is_gated_act": true, "layer_norm_epsilon": 1e-06, "model_type": "t5", "num_decoder_layers": 24, "num_heads": 32, "num_layers": 24, "output_past": true, "pad_token_id": 0, "relative_attention_max_distance": 128, "relative_attention_num_buckets": 32, "tie_word_embeddings": false, "torch_dtype": "bfloat16", "transformers_version": "4.25.1", "use_cache": false, "vocab_size": 32100}123version https://git-lfs.github.com/spec/v1oid sha256:cb2bf10ab79a734abf48b4526e85617b23cb1c5b7111ba3ad379dbac908bad78size 8548942587123version https://git-lfs.github.com/spec/v1oid sha256:a5a8b042a08e277e9607ae79d179bc99de7f521b92e835ee4da73b2c46aeb054size 8548943675123version https://git-lfs.github.com/spec/v1oid sha256:b94e350dee7dc475cd000bdcb40cb1defaa8bb627d8fd31733e726003f4393a5size 8548944827123version https://git-lfs.github.com/spec/v1oid sha256:ce62491926e9ac6b1c1cc2b7dad2e51995e342c008162e81bff5549bc55054f7size 8548943867123version https://git-lfs.github.com/spec/v1oid sha256:4a5eefa82e897593644630ca4a35533b7d933cb8a29d64ca442390b9cb02df76size 56994818191global_step1600123version https://git-lfs.github.com/spec/v1oid sha256:19fb7c9a42c8115a0b2518631b5bbb17dea18f382fc7c1533c4c534df2229d15size 5699366451123version https://git-lfs.github.com/spec/v1oid sha256:c41b81c8227fa28b27fd97d2ad7721930a51b65a4edde92cf2f4fab8c936ef99size 14583123version https://git-lfs.github.com/spec/v1oid sha256:191c7a58927c9f14ad9603e587b64634de332f567bbc5010a7f0d63bda38db1bsize 14583123version https://git-lfs.github.com/spec/v1oid sha256:030bb0a6f5114351f55cfc1c6138a702b0c7aabf96b28f202d0656ade264bc2dsize 14583123version https://git-lfs.github.com/spec/v1oid sha256:26bd79bd6efb5789c1f2bbb43bb094cf34184e47e809cc4029a946738f0927d9size 14583123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107{ "additional_special_tokens": [ "<extra_id_0>", "<extra_id_1>", "<extra_id_2>", "<extra_id_3>", "<extra_id_4>", "<extra_id_5>", "<extra_id_6>", "<extra_id_7>", "<extra_id_8>", "<extra_id_9>", "<extra_id_10>", "<extra_id_11>", "<extra_id_12>", "<extra_id_13>", "<extra_id_14>", "<extra_id_15>", "<extra_id_16>", "<extra_id_17>", "<extra_id_18>", "<extra_id_19>", "<extra_id_20>", "<extra_id_21>", "<extra_id_22>", "<extra_id_23>", "<extra_id_24>", "<extra_id_25>", "<extra_id_26>", "<extra_id_27>", "<extra_id_28>", "<extra_id_29>", "<extra_id_30>", "<extra_id_31>", "<extra_id_32>", "<extra_id_33>", "<extra_id_34>", "<extra_id_35>", "<extra_id_36>", "<extra_id_37>", "<extra_id_38>", "<extra_id_39>", "<extra_id_40>", "<extra_id_41>", "<extra_id_42>", "<extra_id_43>", "<extra_id_44>", "<extra_id_45>", "<extra_id_46>", "<extra_id_47>", "<extra_id_48>", "<extra_id_49>", "<extra_id_50>", "<extra_id_51>", "<extra_id_52>", "<extra_id_53>", "<extra_id_54>", "<extra_id_55>", "<extra_id_56>", "<extra_id_57>", "<extra_id_58>", "<extra_id_59>", "<extra_id_60>", "<extra_id_61>", "<extra_id_62>", "<extra_id_63>", "<extra_id_64>", "<extra_id_65>", "<extra_id_66>", "<extra_id_67>", "<extra_id_68>", "<extra_id_69>", "<extra_id_70>", "<extra_id_71>", "<extra_id_72>", "<extra_id_73>", "<extra_id_74>", "<extra_id_75>", "<extra_id_76>", "<extra_id_77>", "<extra_id_78>", "<extra_id_79>", "<extra_id_80>", "<extra_id_81>", "<extra_id_82>", "<extra_id_83>", "<extra_id_84>", "<extra_id_85>", "<extra_id_86>", "<extra_id_87>", "<extra_id_88>", "<extra_id_89>", "<extra_id_90>", "<extra_id_91>", "<extra_id_92>", "<extra_id_93>", "<extra_id_94>", "<extra_id_95>", "<extra_id_96>", "<extra_id_97>", "<extra_id_98>", "<extra_id_99>" ], "eos_token": "</s>", "pad_token": "<pad>", "unk_token": "<unk>"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113{ "additional_special_tokens": [ "<extra_id_0>", "<extra_id_1>", "<extra_id_2>", "<extra_id_3>", "<extra_id_4>", "<extra_id_5>", "<extra_id_6>", "<extra_id_7>", "<extra_id_8>", "<extra_id_9>", "<extra_id_10>", "<extra_id_11>", "<extra_id_12>", "<extra_id_13>", "<extra_id_14>", "<extra_id_15>", "<extra_id_16>", "<extra_id_17>", "<extra_id_18>", "<extra_id_19>", "<extra_id_20>", "<extra_id_21>", "<extra_id_22>", "<extra_id_23>", "<extra_id_24>", "<extra_id_25>", "<extra_id_26>", "<extra_id_27>", "<extra_id_28>", "<extra_id_29>", "<extra_id_30>", "<extra_id_31>", "<extra_id_32>", "<extra_id_33>", "<extra_id_34>", "<extra_id_35>", "<extra_id_36>", "<extra_id_37>", "<extra_id_38>", "<extra_id_39>", "<extra_id_40>", "<extra_id_41>", "<extra_id_42>", "<extra_id_43>", "<extra_id_44>", "<extra_id_45>", "<extra_id_46>", "<extra_id_47>", "<extra_id_48>", "<extra_id_49>", "<extra_id_50>", "<extra_id_51>", "<extra_id_52>", "<extra_id_53>", "<extra_id_54>", "<extra_id_55>", "<extra_id_56>", "<extra_id_57>", "<extra_id_58>", "<extra_id_59>", "<extra_id_60>", "<extra_id_61>", "<extra_id_62>", "<extra_id_63>", "<extra_id_64>", "<extra_id_65>", "<extra_id_66>", "<extra_id_67>", "<extra_id_68>", "<extra_id_69>", "<extra_id_70>", "<extra_id_71>", "<extra_id_72>", "<extra_id_73>", "<extra_id_74>", "<extra_id_75>", "<extra_id_76>", "<extra_id_77>", "<extra_id_78>", "<extra_id_79>", "<extra_id_80>", "<extra_id_81>", "<extra_id_82>", "<extra_id_83>", "<extra_id_84>", "<extra_id_85>", "<extra_id_86>", "<extra_id_87>", "<extra_id_88>", "<extra_id_89>", "<extra_id_90>", "<extra_id_91>", "<extra_id_92>", "<extra_id_93>", "<extra_id_94>", "<extra_id_95>", "<extra_id_96>", "<extra_id_97>", "<extra_id_98>", "<extra_id_99>" ], "eos_token": "</s>", "extra_ids": 100, "model_max_length": 512, "name_or_path": "prakharz/DIAL-T0", "pad_token": "<pad>", "sp_model_kwargs": {}, "special_tokens_map_file": "/home/ubuntu/.cache/huggingface/transformers/303fbee39a17e96552ac07e02b70ba62ff0ad760609687e3a1b92b4ad2dff58c.c94798918c92ded6aeef2d2f0e666d2cc4145eca1aa6e1336fde07f2e13e2f46", "tokenizer_class": "T5Tokenizer", "unk_token": "<unk>"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444{ "best_metric": 85.9274, "best_model_checkpoint": "./output/checkpoint-1600", "epoch": 1.463719512195122, "global_step": 1600, "is_hyper_param_search": false, "is_local_process_zero": true, "is_world_process_zero": true, "log_history": [ { "epoch": 0.02, "learning_rate": 4.9496336996337e-05, "loss": 0.7035, "step": 25 }, { "epoch": 0.05, "learning_rate": 4.8923992673992674e-05, "loss": 0.3973, "step": 50 }, { "epoch": 0.07, "learning_rate": 4.8351648351648355e-05, "loss": 0.3092, "step": 75 }, { "epoch": 0.09, "learning_rate": 4.7779304029304036e-05, "loss": 0.2813, "step": 100 }, { "epoch": 0.11, "learning_rate": 4.720695970695971e-05, "loss": 0.2603, "step": 125 }, { "epoch": 0.14, "learning_rate": 4.6634615384615384e-05, "loss": 0.2371, "step": 150 }, { "epoch": 0.16, "learning_rate": 4.606227106227106e-05, "loss": 0.2334, "step": 175 }, { "epoch": 0.18, "learning_rate": 4.548992673992674e-05, "loss": 0.2296, "step": 200 }, { "epoch": 0.21, "learning_rate": 4.491758241758242e-05, "loss": 0.2219, "step": 225 }, { "epoch": 0.23, "learning_rate": 4.4345238095238095e-05, "loss": 0.2078, "step": 250 }, { "epoch": 0.25, "learning_rate": 4.3772893772893776e-05, "loss": 0.2123, "step": 275 }, { "epoch": 0.27, "learning_rate": 4.320054945054946e-05, "loss": 0.2063, "step": 300 }, { "epoch": 0.3, "learning_rate": 4.262820512820513e-05, "loss": 0.2002, "step": 325 }, { "epoch": 0.32, "learning_rate": 4.2055860805860806e-05, "loss": 0.2102, "step": 350 }, { "epoch": 0.34, "learning_rate": 4.148351648351649e-05, "loss": 0.2003, "step": 375 }, { "epoch": 0.37, "learning_rate": 4.091117216117216e-05, "loss": 0.2019, "step": 400 }, { "epoch": 0.39, "learning_rate": 4.033882783882784e-05, "loss": 0.1944, "step": 425 }, { "epoch": 0.41, "learning_rate": 3.9766483516483516e-05, "loss": 0.1935, "step": 450 }, { "epoch": 0.43, "learning_rate": 3.91941391941392e-05, "loss": 0.1974, "step": 475 }, { "epoch": 0.46, "learning_rate": 3.862179487179488e-05, "loss": 0.1922, "step": 500 }, { "epoch": 0.48, "learning_rate": 3.804945054945055e-05, "loss": 0.188, "step": 525 }, { "epoch": 0.5, "learning_rate": 3.747710622710623e-05, "loss": 0.1945, "step": 550 }, { "epoch": 0.53, "learning_rate": 3.690476190476191e-05, "loss": 0.1897, "step": 575 }, { "epoch": 0.55, "learning_rate": 3.633241758241758e-05, "loss": 0.1797, "step": 600 }, { "epoch": 0.57, "learning_rate": 3.576007326007326e-05, "loss": 0.1752, "step": 625 }, { "epoch": 0.59, "learning_rate": 3.5187728937728944e-05, "loss": 0.1797, "step": 650 }, { "epoch": 0.62, "learning_rate": 3.461538461538462e-05, "loss": 0.1785, "step": 675 }, { "epoch": 0.64, "learning_rate": 3.404304029304029e-05, "loss": 0.185, "step": 700 }, { "epoch": 0.66, "learning_rate": 3.347069597069597e-05, "loss": 0.1751, "step": 725 }, { "epoch": 0.69, "learning_rate": 3.289835164835165e-05, "loss": 0.1799, "step": 750 }, { "epoch": 0.71, "learning_rate": 3.232600732600733e-05, "loss": 0.1801, "step": 775 }, { "epoch": 0.73, "learning_rate": 3.1753663003663003e-05, "loss": 0.1748, "step": 800 }, { "epoch": 0.73, "eval_Bleu_1": 0.8552, "eval_Bleu_2": 0.0009, "eval_Bleu_3": 0.0001, "eval_Bleu_4": 0.0, "eval_ROUGE_L": 0.8552, "eval_accuracy": 85.52, "eval_f1": 85.52, "eval_gen_len": 2.2084, "eval_loss": 0.17816422879695892, "eval_precision": 85.52, "eval_recall": 85.52, "eval_rouge1": 85.6587, "eval_rouge2": 3.7183, "eval_rougeL": 85.6528, "eval_rougeLsum": 85.6348, "eval_runtime": 1004.3102, "eval_samples_per_second": 34.705, "eval_steps_per_second": 4.338, "step": 800 }, { "epoch": 0.75, "learning_rate": 3.1181318681318685e-05, "loss": 0.1698, "step": 825 }, { "epoch": 0.78, "learning_rate": 3.0608974358974366e-05, "loss": 0.1763, "step": 850 }, { "epoch": 0.8, "learning_rate": 3.0036630036630036e-05, "loss": 0.1655, "step": 875 }, { "epoch": 0.82, "learning_rate": 2.9464285714285718e-05, "loss": 0.1703, "step": 900 }, { "epoch": 0.85, "learning_rate": 2.8891941391941392e-05, "loss": 0.174, "step": 925 }, { "epoch": 0.87, "learning_rate": 2.831959706959707e-05, "loss": 0.1723, "step": 950 }, { "epoch": 0.89, "learning_rate": 2.774725274725275e-05, "loss": 0.1739, "step": 975 }, { "epoch": 0.91, "learning_rate": 2.7174908424908425e-05, "loss": 0.1673, "step": 1000 }, { "epoch": 0.94, "learning_rate": 2.6602564102564102e-05, "loss": 0.1647, "step": 1025 }, { "epoch": 0.96, "learning_rate": 2.6030219780219783e-05, "loss": 0.1739, "step": 1050 }, { "epoch": 0.98, "learning_rate": 2.5457875457875458e-05, "loss": 0.1703, "step": 1075 }, { "epoch": 1.01, "learning_rate": 2.488553113553114e-05, "loss": 0.1727, "step": 1100 }, { "epoch": 1.03, "learning_rate": 2.4313186813186813e-05, "loss": 0.1603, "step": 1125 }, { "epoch": 1.05, "learning_rate": 2.374084249084249e-05, "loss": 0.1511, "step": 1150 }, { "epoch": 1.07, "learning_rate": 2.3168498168498172e-05, "loss": 0.1544, "step": 1175 }, { "epoch": 1.1, "learning_rate": 2.2596153846153846e-05, "loss": 0.1517, "step": 1200 }, { "epoch": 1.12, "learning_rate": 2.2023809523809524e-05, "loss": 0.1556, "step": 1225 }, { "epoch": 1.14, "learning_rate": 2.14514652014652e-05, "loss": 0.1688, "step": 1250 }, { "epoch": 1.17, "learning_rate": 2.0879120879120882e-05, "loss": 0.1658, "step": 1275 }, { "epoch": 1.19, "learning_rate": 2.0306776556776557e-05, "loss": 0.1587, "step": 1300 }, { "epoch": 1.21, "learning_rate": 1.9734432234432234e-05, "loss": 0.1664, "step": 1325 }, { "epoch": 1.24, "learning_rate": 1.9162087912087915e-05, "loss": 0.1558, "step": 1350 }, { "epoch": 1.26, "learning_rate": 1.858974358974359e-05, "loss": 0.1591, "step": 1375 }, { "epoch": 1.28, "learning_rate": 1.8017399267399267e-05, "loss": 0.1558, "step": 1400 }, { "epoch": 1.3, "learning_rate": 1.7445054945054945e-05, "loss": 0.1583, "step": 1425 }, { "epoch": 1.33, "learning_rate": 1.6872710622710626e-05, "loss": 0.155, "step": 1450 }, { "epoch": 1.35, "learning_rate": 1.63003663003663e-05, "loss": 0.1644, "step": 1475 }, { "epoch": 1.37, "learning_rate": 1.5728021978021978e-05, "loss": 0.1574, "step": 1500 }, { "epoch": 1.4, "learning_rate": 1.5155677655677656e-05, "loss": 0.1555, "step": 1525 }, { "epoch": 1.42, "learning_rate": 1.4583333333333335e-05, "loss": 0.1542, "step": 1550 }, { "epoch": 1.44, "learning_rate": 1.4010989010989013e-05, "loss": 0.1537, "step": 1575 }, { "epoch": 1.46, "learning_rate": 1.3438644688644688e-05, "loss": 0.1493, "step": 1600 }, { "epoch": 1.46, "eval_Bleu_1": 0.8593, "eval_Bleu_2": 0.0009, "eval_Bleu_3": 0.0001, "eval_Bleu_4": 0.0, "eval_ROUGE_L": 0.8593, "eval_accuracy": 85.9274, "eval_f1": 85.9274, "eval_gen_len": 2.2038, "eval_loss": 0.1681133359670639, "eval_precision": 85.9274, "eval_recall": 85.9274, "eval_rouge1": 86.0601, "eval_rouge2": 3.5662, "eval_rougeL": 86.0539, "eval_rougeLsum": 86.0496, "eval_runtime": 1001.4875, "eval_samples_per_second": 34.803, "eval_steps_per_second": 4.351, "step": 1600 } ], "max_steps": 2186, "num_train_epochs": 2, "total_flos": 2.7769320388994007e+18, "trial_name": null, "trial_params": null}123version https://git-lfs.github.com/spec/v1oid sha256:2850b9e779255d3efce03dff445e52ea503bbb829fd1b72d24779361b420c4a9size 4603123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482#!/usr/bin/env python
# This script extracts fp32 consolidated weights from a zero 2 and 3 DeepSpeed checkpoints. It gets# copied into the top level checkpoint dir, so the user can easily do the conversion at any point in# the future. Once extracted, the weights don't require DeepSpeed and can be used in any# application.## example: python zero_to_fp32.py . pytorch_model.bin
import argparseimport torchimport globimport mathimport osimport refrom collections import OrderedDict
# while this script doesn't use deepspeed to recover data, since the checkpoints are pickled with# DeepSpeed data structures it has to be available in the current python environment.from deepspeed.utils import loggerfrom deepspeed.checkpoint.constants import (DS_VERSION, OPTIMIZER_STATE_DICT, SINGLE_PARTITION_OF_FP32_GROUPS, FP32_FLAT_GROUPS, ZERO_STAGE, PARTITION_COUNT, PARAM_SHAPES, BUFFER_NAMES)
debug = 0
# load to cpudevice = torch.device('cpu')
def atoi(text): return int(text) if text.isdigit() else text
def natural_keys(text): ''' alist.sort(key=natural_keys) sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html (See Toothy's implementation in the comments) ''' return [atoi(c) for c in re.split(r'(\d+)', text)]
def get_model_state_file(checkpoint_dir, zero_stage): if not os.path.isdir(checkpoint_dir): raise FileNotFoundError(f"Directory '{checkpoint_dir}' doesn't exist")
# there should be only one file if zero_stage == 2: file = os.path.join(checkpoint_dir, "mp_rank_00_model_states.pt") elif zero_stage == 3: file = os.path.join(checkpoint_dir, "zero_pp_rank_0_mp_rank_00_model_states.pt")
if not os.path.exists(file): raise FileNotFoundError(f"can't find model states file at '{file}'")
return file
def get_optim_files(checkpoint_dir): # XXX: need to test that this simple glob rule works for multi-node setup too optim_files = sorted(glob.glob(os.path.join(checkpoint_dir, "*_optim_states.pt")), key=natural_keys)
if len(optim_files) == 0: raise FileNotFoundError( f"can't find '*_optim_states.pt' files in directory '{checkpoint_dir}'")
return optim_files
def parse_model_state(file): state_dict = torch.load(file, map_location=device)
if BUFFER_NAMES not in state_dict: raise ValueError(f"{file} is not a model state checkpoint") buffer_names = state_dict[BUFFER_NAMES] if debug: print("Found buffers:", buffer_names)
# recover just the buffers while restoring them to fp32 if they were saved in fp16 buffers = { k: v.float() for k, v in state_dict["module"].items() if k in buffer_names } param_shapes = state_dict[PARAM_SHAPES]
ds_version = state_dict.get(DS_VERSION, None)
return buffers, param_shapes, ds_version
def parse_optim_states(files, ds_checkpoint_dir):
total_files = len(files) state_dicts = [] for f in files: state_dicts.append(torch.load(f, map_location=device))
if not ZERO_STAGE in state_dicts[0][OPTIMIZER_STATE_DICT]: raise ValueError(f"{files[0]} is not a zero checkpoint") zero_stage = state_dicts[0][OPTIMIZER_STATE_DICT][ZERO_STAGE] world_size = state_dicts[0][OPTIMIZER_STATE_DICT][PARTITION_COUNT]
# For ZeRO-2 each param group can have different partition_count as data parallelism for expert # parameters can be different from data parallelism for non-expert parameters. So we can just # use the max of the partition_count to get the dp world_size.
if type(world_size) is list: world_size = max(world_size)
if world_size != total_files: raise ValueError( f"Expected {world_size} of '*_optim_states.pt' under '{ds_checkpoint_dir}' but found {total_files} files. " "Possibly due to an overwrite of an old checkpoint, or a checkpoint didn't get saved by one or more processes." )
# the groups are named differently in each stage if zero_stage == 2: fp32_groups_key = SINGLE_PARTITION_OF_FP32_GROUPS elif zero_stage == 3: fp32_groups_key = FP32_FLAT_GROUPS else: raise ValueError(f"unknown zero stage {zero_stage}")
if zero_stage == 2: fp32_flat_groups = [ state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key] for i in range(len(state_dicts)) ] elif zero_stage == 3: # if there is more than one param group, there will be multiple flattened tensors - one # flattened tensor per group - for simplicity merge them into a single tensor # # XXX: could make the script more memory efficient for when there are multiple groups - it # will require matching the sub-lists of param_shapes for each param group flattened tensor
fp32_flat_groups = [ torch.cat(state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key], 0) for i in range(len(state_dicts)) ]
return zero_stage, world_size, fp32_flat_groups
def _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir): """ Returns fp32 state_dict reconstructed from ds checkpoint
Args: - ``ds_checkpoint_dir``: path to the deepspeed checkpoint folder (where the optimizer files are)
""" print(f"Processing zero checkpoint '{ds_checkpoint_dir}'")
optim_files = get_optim_files(ds_checkpoint_dir) zero_stage, world_size, fp32_flat_groups = parse_optim_states(optim_files, ds_checkpoint_dir) print( f"Detected checkpoint of type zero stage {zero_stage}, world_size: {world_size}")
model_file = get_model_state_file(ds_checkpoint_dir, zero_stage) buffers, param_shapes, ds_version = parse_model_state(model_file) print(f'Parsing checkpoint created by deepspeed=={ds_version}')
if zero_stage == 2: return _get_fp32_state_dict_from_zero2_checkpoint(world_size, param_shapes, fp32_flat_groups, buffers) elif zero_stage == 3: return _get_fp32_state_dict_from_zero3_checkpoint(world_size, param_shapes, fp32_flat_groups, buffers)
def _get_fp32_state_dict_from_zero2_checkpoint(world_size, param_shapes, fp32_flat_groups, buffers):
# Reconstruction protocol: # # XXX: document this
if debug: for i in range(world_size): for j in range(len(fp32_flat_groups[0])): print( f"{FP32_FLAT_GROUPS}[{i}][{j}].shape={fp32_flat_groups[i][j].shape}")
# XXX: memory usage doubles here (zero2) num_param_groups = len(fp32_flat_groups[0]) merged_single_partition_of_fp32_groups = [] for i in range(num_param_groups): merged_partitions = [sd[i] for sd in fp32_flat_groups] full_single_fp32_vector = torch.cat(merged_partitions, 0) merged_single_partition_of_fp32_groups.append(full_single_fp32_vector) avail_numel = sum([ full_single_fp32_vector.numel() for full_single_fp32_vector in merged_single_partition_of_fp32_groups ])
if debug: wanted_params = sum([len(shapes) for shapes in param_shapes]) wanted_numel = sum( [sum(shape.numel() for shape in shapes.values()) for shapes in param_shapes]) # not asserting if there is a mismatch due to possible padding print(f"Have {avail_numel} numels to process.") print(f"Need {wanted_numel} numels in {wanted_params} params.")
state_dict = OrderedDict()
# buffers state_dict.update(buffers) if debug: print(f"added {len(buffers)} buffers")
# params # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support # out-of-core computing solution total_numel = 0 total_params = 0 for shapes, full_single_fp32_vector in zip(param_shapes, merged_single_partition_of_fp32_groups): offset = 0 avail_numel = full_single_fp32_vector.numel() for name, shape in shapes.items():
unpartitioned_numel = shape.numel() total_numel += unpartitioned_numel total_params += 1
if debug: print( f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} " ) state_dict[name] = full_single_fp32_vector.narrow( 0, offset, unpartitioned_numel).view(shape) offset += unpartitioned_numel
# Z2 started to align to 2*world_size to improve nccl performance. Therefore both offset and # avail_numel can differ by anywhere between 0..2*world_size. Due to two unrelated complex # paddings performed in the code it's almost impossible to predict the exact numbers w/o the # live optimizer object, so we are checking that the numbers are within the right range align_to = 2 * world_size
def zero2_align(x): return align_to * math.ceil(x / align_to)
if debug: print(f"original offset={offset}, avail_numel={avail_numel}")
offset = zero2_align(offset) avail_numel = zero2_align(avail_numel)
if debug: print(f"aligned offset={offset}, avail_numel={avail_numel}")
# Sanity check if offset != avail_numel: raise ValueError( f"consumed {offset} numels out of {avail_numel} - something is wrong")
print( f"Reconstructed fp32 state dict with {total_params} params {total_numel} elements" )
return state_dict
def zero3_partitioned_param_info(unpartitioned_numel, world_size): remainder = unpartitioned_numel % world_size padding_numel = (world_size - remainder) if remainder else 0 partitioned_numel = math.ceil(unpartitioned_numel / world_size) return partitioned_numel, padding_numel
def _get_fp32_state_dict_from_zero3_checkpoint(world_size, param_shapes, fp32_flat_groups, buffers):
# Reconstruction protocol: For zero3 we need to zip the partitions together at boundary of each # param, re-consolidating each param, while dealing with padding if any
avail_numel = fp32_flat_groups[0].numel() * world_size # merge list of dicts, preserving order param_shapes = {k: v for d in param_shapes for k, v in d.items()}
if debug: for i in range(world_size): print(f"{FP32_FLAT_GROUPS}[{i}].shape={fp32_flat_groups[i].shape}")
wanted_params = len(param_shapes) wanted_numel = sum(shape.numel() for shape in param_shapes.values()) # not asserting if there is a mismatch due to possible padding print(f"Have {avail_numel} numels to process.") print(f"Need {wanted_numel} numels in {wanted_params} params.")
state_dict = OrderedDict()
# buffers state_dict.update(buffers) if debug: print(f"added {len(buffers)} buffers")
# params # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support # out-of-core computing solution offset = 0 total_numel = 0 total_params = 0 for name, shape in param_shapes.items():
unpartitioned_numel = shape.numel() total_numel += unpartitioned_numel total_params += 1
partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
if debug: print( f"{total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}" )
# XXX: memory usage doubles here state_dict[name] = torch.cat( tuple(fp32_flat_groups[i].narrow(0, offset, partitioned_numel) for i in range(world_size)), 0).narrow(0, 0, unpartitioned_numel).view(shape) offset += partitioned_numel
offset *= world_size
# Sanity check if offset != avail_numel: raise ValueError( f"consumed {offset} numels out of {avail_numel} - something is wrong")
print( f"Reconstructed fp32 state dict with {total_params} params {total_numel} elements" )
return state_dict
def get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag=None): """ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated state_dict that can be loaded with ``load_state_dict()`` and used for training without DeepSpeed or shared with others, for example via a model hub.
Args: - ``checkpoint_dir``: path to the desired checkpoint folder - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in 'latest' file. e.g., ``global_step14``
Returns: - pytorch ``state_dict``
Note: this approach may not work if your application doesn't have sufficient free CPU memory and you may need to use the offline approach using the ``zero_to_fp32.py`` script that is saved with the checkpoint.
A typical usage might be ::
from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint # do the training and checkpoint saving state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu model = model.cpu() # move to cpu model.load_state_dict(state_dict) # submit to model hub or save the model to share with others
In this example the ``model`` will no longer be usable in the deepspeed context of the same application. i.e. you will need to re-initialize the deepspeed engine, since ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
If you want it all done for you, use ``load_state_dict_from_zero_checkpoint`` instead.
""" if tag is None: latest_path = os.path.join(checkpoint_dir, 'latest') if os.path.isfile(latest_path): with open(latest_path, 'r') as fd: tag = fd.read().strip() else: raise ValueError(f"Unable to find 'latest' file at {latest_path}")
ds_checkpoint_dir = os.path.join(checkpoint_dir, tag)
if not os.path.isdir(ds_checkpoint_dir): raise FileNotFoundError(f"Directory '{ds_checkpoint_dir}' doesn't exist")
return _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir)
def convert_zero_checkpoint_to_fp32_state_dict(checkpoint_dir, output_file, tag=None): """ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict`` file that can be loaded with ``torch.load(file)`` + ``load_state_dict()`` and used for training without DeepSpeed.
Args: - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``) - ``output_file``: path to the pytorch fp32 state_dict output file (e.g. path/pytorch_model.bin) - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14`` """
state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag) print(f"Saving fp32 state dict to {output_file}") torch.save(state_dict, output_file)
def load_state_dict_from_zero_checkpoint(model, checkpoint_dir, tag=None): """ 1. Put the provided model to cpu 2. Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict`` 3. Load it into the provided model
Args: - ``model``: the model object to update - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``) - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14``
Returns: - ``model`: modified model
Make sure you have plenty of CPU memory available before you call this function. If you don't have enough use the ``zero_to_fp32.py`` utility to do the conversion. You will find it conveniently placed for you in the checkpoint folder.
A typical usage might be ::
from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir) # submit to model hub or save the model to share with others
Note, that once this was run, the ``model`` will no longer be usable in the deepspeed context of the same application. i.e. you will need to re-initialize the deepspeed engine, since ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
""" logger.info(f"Extracting fp32 weights") state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
logger.info(f"Overwriting model with fp32 weights") model = model.cpu() model.load_state_dict(state_dict, strict=False)
return model
if __name__ == "__main__":
parser = argparse.ArgumentParser() parser.add_argument( "checkpoint_dir", type=str, help="path to the desired checkpoint folder, e.g., path/checkpoint-12") parser.add_argument( "output_file", type=str, help= "path to the pytorch fp32 state_dict output file (e.g. path/checkpoint-12/pytorch_model.bin)" ) parser.add_argument("-d", "--debug", action='store_true', help="enable debug") args = parser.parse_args()
debug = args.debug
convert_zero_checkpoint_to_fp32_state_dict(args.checkpoint_dir, args.output_file)config.jsonAdded+32
global_step1600/bf16_zero_pp_rank_0_mp_rank_00_optim_states.ptAdded+3
global_step1600/bf16_zero_pp_rank_1_mp_rank_00_optim_states.ptAdded+3
global_step1600/bf16_zero_pp_rank_2_mp_rank_00_optim_states.ptAdded+3
global_step1600/bf16_zero_pp_rank_3_mp_rank_00_optim_states.ptAdded+3
global_step1600/mp_rank_00_model_states.ptAdded+3
latestAdded+1
pytorch_model.binAdded+3
rng_state_0.pthAdded+3
rng_state_1.pthAdded+3
rng_state_2.pthAdded+3
rng_state_3.pthAdded+3
special_tokens_map.jsonAdded+107
tokenizer.jsonAdded
tokenizer_config.jsonAdded+113
trainer_state.jsonAdded+444
training_args.binAdded+3
zero_to_fp32.pyAdded+482