aamixsh commited on
Commit
c548c11
·
1 Parent(s): ecb1c70

First training attempt

Browse files
README.md ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: /src/tanner/chess-roberta-code/model_configs/chess_roberta.json
3
+ tags:
4
+ - generated_from_trainer
5
+ datasets:
6
+ - TannerGladson/chess-roberta-pretraining
7
+ metrics:
8
+ - accuracy
9
+ model-index:
10
+ - name: 2024.09.24-01.27
11
+ results:
12
+ - task:
13
+ name: Masked Language Modeling
14
+ type: fill-mask
15
+ dataset:
16
+ name: TannerGladson/chess-roberta-pretraining
17
+ type: TannerGladson/chess-roberta-pretraining
18
+ metrics:
19
+ - name: Accuracy
20
+ type: accuracy
21
+ value: 0.5445640378503339
22
+ ---
23
+
24
+ <!-- This model card has been generated automatically according to the information the Trainer had access to. You
25
+ should probably proofread and complete it, then remove this comment. -->
26
+
27
+ [<img src="https://raw.githubusercontent.com/wandb/assets/main/wandb-github-badge-28.svg" alt="Visualize in Weights & Biases" width="200" height="32"/>](https://wandb.ai/tanner-gladson/huggingface/runs/cs121ioj)
28
+ # 2024.09.24-01.27
29
+
30
+ This model is a fine-tuned version of [/src/tanner/chess-roberta-code/model_configs/chess_roberta.json](https://huggingface.co//src/tanner/chess-roberta-code/model_configs/chess_roberta.json) on the TannerGladson/chess-roberta-pretraining dataset.
31
+ It achieves the following results on the evaluation set:
32
+ - Loss: 2.1202
33
+ - Accuracy: 0.5446
34
+
35
+ ## Model description
36
+
37
+ More information needed
38
+
39
+ ## Intended uses & limitations
40
+
41
+ More information needed
42
+
43
+ ## Training and evaluation data
44
+
45
+ More information needed
46
+
47
+ ## Training procedure
48
+
49
+ ### Training hyperparameters
50
+
51
+ The following hyperparameters were used during training:
52
+ - learning_rate: 0.0005
53
+ - train_batch_size: 8
54
+ - eval_batch_size: 8
55
+ - seed: 42
56
+ - distributed_type: multi-GPU
57
+ - gradient_accumulation_steps: 16
58
+ - total_train_batch_size: 128
59
+ - optimizer: Adam with betas=(0.9,0.98) and epsilon=1e-06
60
+ - lr_scheduler_type: linear
61
+ - lr_scheduler_warmup_steps: 200
62
+ - training_steps: 9000
63
+
64
+ ### Training results
65
+
66
+ | Training Loss | Epoch | Step | Validation Loss | Accuracy |
67
+ |:-------------:|:------:|:----:|:---------------:|:--------:|
68
+ | 2.1201 | 0.0120 | 1000 | 2.1195 | 0.5445 |
69
+ | 2.1222 | 0.0239 | 2000 | 2.1199 | 0.5446 |
70
+ | 2.1179 | 0.0359 | 3000 | 2.1199 | 0.5444 |
71
+ | 2.1215 | 0.0479 | 4000 | 2.1186 | 0.5446 |
72
+ | 2.1176 | 0.0598 | 5000 | 2.1194 | 0.5445 |
73
+ | 2.1189 | 0.0718 | 6000 | 2.1193 | 0.5445 |
74
+ | 2.1184 | 0.0838 | 7000 | 2.1194 | 0.5446 |
75
+ | 2.119 | 0.0957 | 8000 | 2.1190 | 0.5446 |
76
+ | 2.1237 | 0.1077 | 9000 | 2.1201 | 0.5446 |
77
+
78
+
79
+ ### Framework versions
80
+
81
+ - Transformers 4.42.4
82
+ - Pytorch 2.0.1+cu117
83
+ - Datasets 2.17.1
84
+ - Tokenizers 0.19.1
all_results.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "epoch": 0.10768528974073271,
3
+ "eval_accuracy": 0.5445640378503339,
4
+ "eval_loss": 2.1201696395874023,
5
+ "eval_runtime": 8310.7479,
6
+ "eval_samples": 562811,
7
+ "eval_samples_per_second": 67.721,
8
+ "eval_steps_per_second": 8.465,
9
+ "perplexity": 8.332550898295501,
10
+ "total_flos": 1158274171797504.0,
11
+ "train_loss": 2.120608181423611,
12
+ "train_runtime": 152416.4466,
13
+ "train_samples": 10697834,
14
+ "train_samples_per_second": 7.558,
15
+ "train_steps_per_second": 0.059
16
+ }
checkpoint-9000/config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/src/tanner/chess-roberta-code/model_configs/chess_roberta.json",
3
+ "architectures": [
4
+ "RobertaForMaskedLM"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "classifier_dropout": null,
9
+ "eos_token_id": 1,
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 768,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 3072,
15
+ "layer_norm_eps": 1e-05,
16
+ "max_position_embeddings": 1030,
17
+ "model_type": "roberta",
18
+ "num_attention_heads": 12,
19
+ "num_hidden_layers": 16,
20
+ "pad_token_id": 2,
21
+ "position_embedding_type": "absolute",
22
+ "torch_dtype": "bfloat16",
23
+ "transformers_version": "4.42.4",
24
+ "type_vocab_size": 1,
25
+ "use_cache": true,
26
+ "vocab_size": 64
27
+ }
checkpoint-9000/global_step9000/bf16_zero_pp_rank_0_mp_rank_00_optim_states.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c279dc883c0d65945b83146c6a1901c3265ee01b398b33b0e234c652ee2d446
3
+ size 1378093111
checkpoint-9000/global_step9000/zero_pp_rank_0_mp_rank_00_model_states.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:78aa760df334a408b1ff1cad6785692d84af6c1c49652716d7cfd0b815914075
3
+ size 140897
checkpoint-9000/latest ADDED
@@ -0,0 +1 @@
 
 
1
+ global_step9000
checkpoint-9000/rng_state.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:76bca5d4fb610fbdcd81ca77a69064a3272c24f56e6e5af9d5f760e817a866fe
3
+ size 14575
checkpoint-9000/special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "mask_token": {
10
+ "content": "[MASK]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "[PAD]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "sep_token": {
24
+ "content": "[SEP]",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "[UNK]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
checkpoint-9000/tokenizer.json ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "version": "1.0",
3
+ "truncation": null,
4
+ "padding": null,
5
+ "added_tokens": [
6
+ {
7
+ "id": 0,
8
+ "content": "[CLS]",
9
+ "single_word": false,
10
+ "lstrip": false,
11
+ "rstrip": false,
12
+ "normalized": false,
13
+ "special": true
14
+ },
15
+ {
16
+ "id": 1,
17
+ "content": "[SEP]",
18
+ "single_word": false,
19
+ "lstrip": false,
20
+ "rstrip": false,
21
+ "normalized": false,
22
+ "special": true
23
+ },
24
+ {
25
+ "id": 2,
26
+ "content": "[PAD]",
27
+ "single_word": false,
28
+ "lstrip": false,
29
+ "rstrip": false,
30
+ "normalized": false,
31
+ "special": true
32
+ },
33
+ {
34
+ "id": 3,
35
+ "content": "[UNK]",
36
+ "single_word": false,
37
+ "lstrip": false,
38
+ "rstrip": false,
39
+ "normalized": false,
40
+ "special": true
41
+ },
42
+ {
43
+ "id": 4,
44
+ "content": "[MASK]",
45
+ "single_word": false,
46
+ "lstrip": false,
47
+ "rstrip": false,
48
+ "normalized": false,
49
+ "special": true
50
+ },
51
+ {
52
+ "id": 5,
53
+ "content": "[ILLEGAL]",
54
+ "single_word": false,
55
+ "lstrip": false,
56
+ "rstrip": false,
57
+ "normalized": false,
58
+ "special": true
59
+ },
60
+ {
61
+ "id": 6,
62
+ "content": "~",
63
+ "single_word": false,
64
+ "lstrip": false,
65
+ "rstrip": false,
66
+ "normalized": false,
67
+ "special": true
68
+ }
69
+ ],
70
+ "normalizer": {
71
+ "type": "Sequence",
72
+ "normalizers": [
73
+ {
74
+ "type": "NFD"
75
+ },
76
+ {
77
+ "type": "StripAccents"
78
+ }
79
+ ]
80
+ },
81
+ "pre_tokenizer": {
82
+ "type": "Split",
83
+ "pattern": {
84
+ "String": ""
85
+ },
86
+ "behavior": "Isolated",
87
+ "invert": false
88
+ },
89
+ "post_processor": {
90
+ "type": "RobertaProcessing",
91
+ "sep": [
92
+ "[SEP]",
93
+ 1
94
+ ],
95
+ "cls": [
96
+ "[CLS]",
97
+ 0
98
+ ],
99
+ "trim_offsets": false,
100
+ "add_prefix_space": false
101
+ },
102
+ "decoder": {
103
+ "type": "WordPiece",
104
+ "prefix": "##",
105
+ "cleanup": true
106
+ },
107
+ "model": {
108
+ "type": "WordPiece",
109
+ "unk_token": "[UNK]",
110
+ "continuing_subword_prefix": "##",
111
+ "max_input_chars_per_word": 100,
112
+ "vocab": {
113
+ "[CLS]": 0,
114
+ "[SEP]": 1,
115
+ "[PAD]": 2,
116
+ "[UNK]": 3,
117
+ "[MASK]": 4,
118
+ "[ILLEGAL]": 5,
119
+ "~": 6,
120
+ ">": 7,
121
+ " ": 8,
122
+ "#": 9,
123
+ "+": 10,
124
+ "-": 11,
125
+ "/": 12,
126
+ "0": 13,
127
+ "1": 14,
128
+ "2": 15,
129
+ "3": 16,
130
+ "4": 17,
131
+ "5": 18,
132
+ "6": 19,
133
+ "7": 20,
134
+ "8": 21,
135
+ "9": 22,
136
+ "=": 23,
137
+ "B": 24,
138
+ "K": 25,
139
+ "N": 26,
140
+ "O": 27,
141
+ "P": 28,
142
+ "Q": 29,
143
+ "R": 30,
144
+ "a": 31,
145
+ "b": 32,
146
+ "c": 33,
147
+ "d": 34,
148
+ "e": 35,
149
+ "f": 36,
150
+ "g": 37,
151
+ "h": 38,
152
+ "k": 39,
153
+ "n": 40,
154
+ "p": 41,
155
+ "q": 42,
156
+ "r": 43,
157
+ "w": 44,
158
+ "x": 45,
159
+ "_": 46
160
+ }
161
+ }
162
+ }
checkpoint-9000/tokenizer_config.json ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[CLS]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "[SEP]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "[PAD]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "[UNK]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "4": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ },
43
+ "5": {
44
+ "content": "[ILLEGAL]",
45
+ "lstrip": false,
46
+ "normalized": false,
47
+ "rstrip": false,
48
+ "single_word": false,
49
+ "special": true
50
+ },
51
+ "6": {
52
+ "content": "~",
53
+ "lstrip": false,
54
+ "normalized": false,
55
+ "rstrip": false,
56
+ "single_word": false,
57
+ "special": true
58
+ }
59
+ },
60
+ "clean_up_tokenization_spaces": true,
61
+ "cls_token": "[CLS]",
62
+ "mask_token": "[MASK]",
63
+ "model_max_length": 1000000000000000019884624838656,
64
+ "pad_token": "[PAD]",
65
+ "sep_token": "[SEP]",
66
+ "tokenizer_class": "PreTrainedTokenizerFast",
67
+ "unk_token": "[UNK]"
68
+ }
checkpoint-9000/trainer_state.json ADDED
@@ -0,0 +1,744 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "best_metric": null,
3
+ "best_model_checkpoint": null,
4
+ "epoch": 0.10768528974073271,
5
+ "eval_steps": 1000,
6
+ "global_step": 9000,
7
+ "is_hyper_param_search": false,
8
+ "is_local_process_zero": true,
9
+ "is_world_process_zero": true,
10
+ "log_history": [
11
+ {
12
+ "epoch": 0.0011965032193414745,
13
+ "grad_norm": 0.4959045114979248,
14
+ "learning_rate": 0.0002475,
15
+ "loss": 2.2052,
16
+ "step": 100
17
+ },
18
+ {
19
+ "epoch": 0.002393006438682949,
20
+ "grad_norm": 0.5957897298098443,
21
+ "learning_rate": 0.0004975,
22
+ "loss": 2.1236,
23
+ "step": 200
24
+ },
25
+ {
26
+ "epoch": 0.0035895096580244234,
27
+ "grad_norm": 0.5462649355396835,
28
+ "learning_rate": 0.000494375,
29
+ "loss": 2.123,
30
+ "step": 300
31
+ },
32
+ {
33
+ "epoch": 0.004786012877365898,
34
+ "grad_norm": 0.479307539013867,
35
+ "learning_rate": 0.0004886931818181818,
36
+ "loss": 2.1201,
37
+ "step": 400
38
+ },
39
+ {
40
+ "epoch": 0.005982516096707372,
41
+ "grad_norm": 0.4702932661295846,
42
+ "learning_rate": 0.0004830113636363637,
43
+ "loss": 2.1218,
44
+ "step": 500
45
+ },
46
+ {
47
+ "epoch": 0.007179019316048847,
48
+ "grad_norm": 0.4536330377549505,
49
+ "learning_rate": 0.0004773295454545455,
50
+ "loss": 2.1199,
51
+ "step": 600
52
+ },
53
+ {
54
+ "epoch": 0.008375522535390322,
55
+ "grad_norm": 0.30397230580627244,
56
+ "learning_rate": 0.00047164772727272724,
57
+ "loss": 2.122,
58
+ "step": 700
59
+ },
60
+ {
61
+ "epoch": 0.009572025754731796,
62
+ "grad_norm": 0.5497084490461644,
63
+ "learning_rate": 0.0004659659090909091,
64
+ "loss": 2.1195,
65
+ "step": 800
66
+ },
67
+ {
68
+ "epoch": 0.010768528974073271,
69
+ "grad_norm": 0.31265244740901976,
70
+ "learning_rate": 0.0004602840909090909,
71
+ "loss": 2.1214,
72
+ "step": 900
73
+ },
74
+ {
75
+ "epoch": 0.011965032193414745,
76
+ "grad_norm": 0.38764126785839226,
77
+ "learning_rate": 0.0004546022727272727,
78
+ "loss": 2.1201,
79
+ "step": 1000
80
+ },
81
+ {
82
+ "epoch": 0.011965032193414745,
83
+ "eval_accuracy": 0.544493419943561,
84
+ "eval_loss": 2.119503974914551,
85
+ "eval_runtime": 14498.4824,
86
+ "eval_samples_per_second": 38.819,
87
+ "eval_steps_per_second": 4.852,
88
+ "step": 1000
89
+ },
90
+ {
91
+ "epoch": 0.01316153541275622,
92
+ "grad_norm": 0.37861216335098746,
93
+ "learning_rate": 0.00044892045454545456,
94
+ "loss": 2.1179,
95
+ "step": 1100
96
+ },
97
+ {
98
+ "epoch": 0.014358038632097694,
99
+ "grad_norm": 0.3244816871915935,
100
+ "learning_rate": 0.00044323863636363636,
101
+ "loss": 2.123,
102
+ "step": 1200
103
+ },
104
+ {
105
+ "epoch": 0.015554541851439169,
106
+ "grad_norm": 0.39134094432184025,
107
+ "learning_rate": 0.0004375568181818182,
108
+ "loss": 2.1192,
109
+ "step": 1300
110
+ },
111
+ {
112
+ "epoch": 0.016751045070780644,
113
+ "grad_norm": 0.31850239786818274,
114
+ "learning_rate": 0.000431875,
115
+ "loss": 2.1195,
116
+ "step": 1400
117
+ },
118
+ {
119
+ "epoch": 0.017947548290122118,
120
+ "grad_norm": 0.3353874978413676,
121
+ "learning_rate": 0.0004261931818181818,
122
+ "loss": 2.1187,
123
+ "step": 1500
124
+ },
125
+ {
126
+ "epoch": 0.01914405150946359,
127
+ "grad_norm": 0.3666941040198008,
128
+ "learning_rate": 0.0004205113636363637,
129
+ "loss": 2.1208,
130
+ "step": 1600
131
+ },
132
+ {
133
+ "epoch": 0.02034055472880507,
134
+ "grad_norm": 0.2874688345676318,
135
+ "learning_rate": 0.0004148295454545455,
136
+ "loss": 2.1182,
137
+ "step": 1700
138
+ },
139
+ {
140
+ "epoch": 0.021537057948146542,
141
+ "grad_norm": 0.28829546768632713,
142
+ "learning_rate": 0.00040914772727272723,
143
+ "loss": 2.1202,
144
+ "step": 1800
145
+ },
146
+ {
147
+ "epoch": 0.022733561167488016,
148
+ "grad_norm": 0.3462480959698083,
149
+ "learning_rate": 0.0004034659090909091,
150
+ "loss": 2.1185,
151
+ "step": 1900
152
+ },
153
+ {
154
+ "epoch": 0.02393006438682949,
155
+ "grad_norm": 0.32146933893861385,
156
+ "learning_rate": 0.0003977840909090909,
157
+ "loss": 2.1222,
158
+ "step": 2000
159
+ },
160
+ {
161
+ "epoch": 0.02393006438682949,
162
+ "eval_accuracy": 0.5445787464391751,
163
+ "eval_loss": 2.1198692321777344,
164
+ "eval_runtime": 8324.5002,
165
+ "eval_samples_per_second": 67.609,
166
+ "eval_steps_per_second": 8.451,
167
+ "step": 2000
168
+ },
169
+ {
170
+ "epoch": 0.025126567606170967,
171
+ "grad_norm": 0.3383598719071586,
172
+ "learning_rate": 0.0003921022727272727,
173
+ "loss": 2.1181,
174
+ "step": 2100
175
+ },
176
+ {
177
+ "epoch": 0.02632307082551244,
178
+ "grad_norm": 0.23870777428521156,
179
+ "learning_rate": 0.00038642045454545456,
180
+ "loss": 2.1201,
181
+ "step": 2200
182
+ },
183
+ {
184
+ "epoch": 0.027519574044853914,
185
+ "grad_norm": 0.26512869204988454,
186
+ "learning_rate": 0.00038073863636363636,
187
+ "loss": 2.1181,
188
+ "step": 2300
189
+ },
190
+ {
191
+ "epoch": 0.028716077264195387,
192
+ "grad_norm": 0.38291085278916004,
193
+ "learning_rate": 0.0003750568181818182,
194
+ "loss": 2.1214,
195
+ "step": 2400
196
+ },
197
+ {
198
+ "epoch": 0.029912580483536864,
199
+ "grad_norm": 0.2922237601337328,
200
+ "learning_rate": 0.000369375,
201
+ "loss": 2.1178,
202
+ "step": 2500
203
+ },
204
+ {
205
+ "epoch": 0.031109083702878338,
206
+ "grad_norm": 0.28007930758064187,
207
+ "learning_rate": 0.0003636931818181818,
208
+ "loss": 2.1174,
209
+ "step": 2600
210
+ },
211
+ {
212
+ "epoch": 0.03230558692221981,
213
+ "grad_norm": 0.37650924862735796,
214
+ "learning_rate": 0.0003580113636363637,
215
+ "loss": 2.1202,
216
+ "step": 2700
217
+ },
218
+ {
219
+ "epoch": 0.03350209014156129,
220
+ "grad_norm": 0.32087380641998864,
221
+ "learning_rate": 0.0003523295454545455,
222
+ "loss": 2.1205,
223
+ "step": 2800
224
+ },
225
+ {
226
+ "epoch": 0.03469859336090276,
227
+ "grad_norm": 0.3065591070785156,
228
+ "learning_rate": 0.00034664772727272723,
229
+ "loss": 2.1219,
230
+ "step": 2900
231
+ },
232
+ {
233
+ "epoch": 0.035895096580244236,
234
+ "grad_norm": 0.4973396205464692,
235
+ "learning_rate": 0.0003409659090909091,
236
+ "loss": 2.1179,
237
+ "step": 3000
238
+ },
239
+ {
240
+ "epoch": 0.035895096580244236,
241
+ "eval_accuracy": 0.5444474125165799,
242
+ "eval_loss": 2.119871139526367,
243
+ "eval_runtime": 14003.3046,
244
+ "eval_samples_per_second": 40.191,
245
+ "eval_steps_per_second": 5.024,
246
+ "step": 3000
247
+ },
248
+ {
249
+ "epoch": 0.03709159979958571,
250
+ "grad_norm": 0.36054926026192535,
251
+ "learning_rate": 0.0003352840909090909,
252
+ "loss": 2.1207,
253
+ "step": 3100
254
+ },
255
+ {
256
+ "epoch": 0.03828810301892718,
257
+ "grad_norm": 0.2609329016792503,
258
+ "learning_rate": 0.0003296022727272727,
259
+ "loss": 2.1187,
260
+ "step": 3200
261
+ },
262
+ {
263
+ "epoch": 0.03948460623826866,
264
+ "grad_norm": 0.4377934640382202,
265
+ "learning_rate": 0.00032392045454545455,
266
+ "loss": 2.1183,
267
+ "step": 3300
268
+ },
269
+ {
270
+ "epoch": 0.04068110945761014,
271
+ "grad_norm": 0.30040663333811507,
272
+ "learning_rate": 0.00031823863636363636,
273
+ "loss": 2.1207,
274
+ "step": 3400
275
+ },
276
+ {
277
+ "epoch": 0.04187761267695161,
278
+ "grad_norm": 0.4058031327319446,
279
+ "learning_rate": 0.0003125568181818182,
280
+ "loss": 2.1175,
281
+ "step": 3500
282
+ },
283
+ {
284
+ "epoch": 0.043074115896293085,
285
+ "grad_norm": 0.2221276357895751,
286
+ "learning_rate": 0.000306875,
287
+ "loss": 2.1191,
288
+ "step": 3600
289
+ },
290
+ {
291
+ "epoch": 0.044270619115634555,
292
+ "grad_norm": 0.2171065800667589,
293
+ "learning_rate": 0.0003011931818181818,
294
+ "loss": 2.1222,
295
+ "step": 3700
296
+ },
297
+ {
298
+ "epoch": 0.04546712233497603,
299
+ "grad_norm": 0.24009215814108797,
300
+ "learning_rate": 0.0002955113636363637,
301
+ "loss": 2.1206,
302
+ "step": 3800
303
+ },
304
+ {
305
+ "epoch": 0.04666362555431751,
306
+ "grad_norm": 0.23325080103491874,
307
+ "learning_rate": 0.0002898295454545455,
308
+ "loss": 2.1219,
309
+ "step": 3900
310
+ },
311
+ {
312
+ "epoch": 0.04786012877365898,
313
+ "grad_norm": 0.38496953527087835,
314
+ "learning_rate": 0.00028414772727272723,
315
+ "loss": 2.1215,
316
+ "step": 4000
317
+ },
318
+ {
319
+ "epoch": 0.04786012877365898,
320
+ "eval_accuracy": 0.5445841718354034,
321
+ "eval_loss": 2.1186254024505615,
322
+ "eval_runtime": 14020.6018,
323
+ "eval_samples_per_second": 40.142,
324
+ "eval_steps_per_second": 5.018,
325
+ "step": 4000
326
+ },
327
+ {
328
+ "epoch": 0.049056631993000456,
329
+ "grad_norm": 0.2049052054281515,
330
+ "learning_rate": 0.0002784659090909091,
331
+ "loss": 2.1184,
332
+ "step": 4100
333
+ },
334
+ {
335
+ "epoch": 0.05025313521234193,
336
+ "grad_norm": 0.2458322734082573,
337
+ "learning_rate": 0.0002727840909090909,
338
+ "loss": 2.1196,
339
+ "step": 4200
340
+ },
341
+ {
342
+ "epoch": 0.0514496384316834,
343
+ "grad_norm": 0.1995524240389471,
344
+ "learning_rate": 0.0002671022727272727,
345
+ "loss": 2.1188,
346
+ "step": 4300
347
+ },
348
+ {
349
+ "epoch": 0.05264614165102488,
350
+ "grad_norm": 0.25790141869897587,
351
+ "learning_rate": 0.00026142045454545455,
352
+ "loss": 2.1184,
353
+ "step": 4400
354
+ },
355
+ {
356
+ "epoch": 0.05384264487036636,
357
+ "grad_norm": 0.28730537897175445,
358
+ "learning_rate": 0.00025573863636363636,
359
+ "loss": 2.1202,
360
+ "step": 4500
361
+ },
362
+ {
363
+ "epoch": 0.05503914808970783,
364
+ "grad_norm": 0.32493426349642957,
365
+ "learning_rate": 0.0002500568181818182,
366
+ "loss": 2.118,
367
+ "step": 4600
368
+ },
369
+ {
370
+ "epoch": 0.056235651309049305,
371
+ "grad_norm": 0.29535898171610375,
372
+ "learning_rate": 0.000244375,
373
+ "loss": 2.1206,
374
+ "step": 4700
375
+ },
376
+ {
377
+ "epoch": 0.057432154528390775,
378
+ "grad_norm": 0.22683681078434978,
379
+ "learning_rate": 0.00023869318181818182,
380
+ "loss": 2.117,
381
+ "step": 4800
382
+ },
383
+ {
384
+ "epoch": 0.05862865774773225,
385
+ "grad_norm": 0.3053349551242291,
386
+ "learning_rate": 0.00023301136363636362,
387
+ "loss": 2.1179,
388
+ "step": 4900
389
+ },
390
+ {
391
+ "epoch": 0.05982516096707373,
392
+ "grad_norm": 0.1948263427424577,
393
+ "learning_rate": 0.00022732954545454545,
394
+ "loss": 2.1176,
395
+ "step": 5000
396
+ },
397
+ {
398
+ "epoch": 0.05982516096707373,
399
+ "eval_accuracy": 0.5445371572840895,
400
+ "eval_loss": 2.119377613067627,
401
+ "eval_runtime": 12119.2615,
402
+ "eval_samples_per_second": 46.439,
403
+ "eval_steps_per_second": 5.805,
404
+ "step": 5000
405
+ },
406
+ {
407
+ "epoch": 0.0610216641864152,
408
+ "grad_norm": 0.29200116272624654,
409
+ "learning_rate": 0.00022164772727272728,
410
+ "loss": 2.1192,
411
+ "step": 5100
412
+ },
413
+ {
414
+ "epoch": 0.062218167405756676,
415
+ "grad_norm": 0.25843746999947237,
416
+ "learning_rate": 0.00021596590909090912,
417
+ "loss": 2.1193,
418
+ "step": 5200
419
+ },
420
+ {
421
+ "epoch": 0.06341467062509815,
422
+ "grad_norm": 0.23460770073478823,
423
+ "learning_rate": 0.00021028409090909092,
424
+ "loss": 2.1196,
425
+ "step": 5300
426
+ },
427
+ {
428
+ "epoch": 0.06461117384443962,
429
+ "grad_norm": 0.2143373312023261,
430
+ "learning_rate": 0.00020460227272727272,
431
+ "loss": 2.1194,
432
+ "step": 5400
433
+ },
434
+ {
435
+ "epoch": 0.0658076770637811,
436
+ "grad_norm": 0.2738612197646279,
437
+ "learning_rate": 0.00019892045454545455,
438
+ "loss": 2.1215,
439
+ "step": 5500
440
+ },
441
+ {
442
+ "epoch": 0.06700418028312258,
443
+ "grad_norm": 0.22066659063762534,
444
+ "learning_rate": 0.00019323863636363636,
445
+ "loss": 2.1185,
446
+ "step": 5600
447
+ },
448
+ {
449
+ "epoch": 0.06820068350246405,
450
+ "grad_norm": 0.2649683554628019,
451
+ "learning_rate": 0.00018755681818181819,
452
+ "loss": 2.117,
453
+ "step": 5700
454
+ },
455
+ {
456
+ "epoch": 0.06939718672180552,
457
+ "grad_norm": 0.36660584937802804,
458
+ "learning_rate": 0.00018187500000000002,
459
+ "loss": 2.1179,
460
+ "step": 5800
461
+ },
462
+ {
463
+ "epoch": 0.070593689941147,
464
+ "grad_norm": 0.2683451066571477,
465
+ "learning_rate": 0.00017619318181818182,
466
+ "loss": 2.1201,
467
+ "step": 5900
468
+ },
469
+ {
470
+ "epoch": 0.07179019316048847,
471
+ "grad_norm": 0.1943497599849036,
472
+ "learning_rate": 0.00017051136363636362,
473
+ "loss": 2.1189,
474
+ "step": 6000
475
+ },
476
+ {
477
+ "epoch": 0.07179019316048847,
478
+ "eval_accuracy": 0.5445187284838173,
479
+ "eval_loss": 2.1193103790283203,
480
+ "eval_runtime": 8292.7883,
481
+ "eval_samples_per_second": 67.868,
482
+ "eval_steps_per_second": 8.484,
483
+ "step": 6000
484
+ },
485
+ {
486
+ "epoch": 0.07298669637982995,
487
+ "grad_norm": 0.2698036045598882,
488
+ "learning_rate": 0.00016482954545454545,
489
+ "loss": 2.1196,
490
+ "step": 6100
491
+ },
492
+ {
493
+ "epoch": 0.07418319959917143,
494
+ "grad_norm": 0.22553506284296373,
495
+ "learning_rate": 0.00015914772727272728,
496
+ "loss": 2.1195,
497
+ "step": 6200
498
+ },
499
+ {
500
+ "epoch": 0.07537970281851289,
501
+ "grad_norm": 0.21994704816511382,
502
+ "learning_rate": 0.00015346590909090911,
503
+ "loss": 2.119,
504
+ "step": 6300
505
+ },
506
+ {
507
+ "epoch": 0.07657620603785437,
508
+ "grad_norm": 0.20519656139775935,
509
+ "learning_rate": 0.00014778409090909092,
510
+ "loss": 2.1184,
511
+ "step": 6400
512
+ },
513
+ {
514
+ "epoch": 0.07777270925719584,
515
+ "grad_norm": 0.3246545352385696,
516
+ "learning_rate": 0.00014210227272727272,
517
+ "loss": 2.1181,
518
+ "step": 6500
519
+ },
520
+ {
521
+ "epoch": 0.07896921247653732,
522
+ "grad_norm": 0.25066878379158264,
523
+ "learning_rate": 0.00013642045454545455,
524
+ "loss": 2.1196,
525
+ "step": 6600
526
+ },
527
+ {
528
+ "epoch": 0.0801657156958788,
529
+ "grad_norm": 0.24038943677884456,
530
+ "learning_rate": 0.00013073863636363635,
531
+ "loss": 2.118,
532
+ "step": 6700
533
+ },
534
+ {
535
+ "epoch": 0.08136221891522027,
536
+ "grad_norm": 0.21920956719889126,
537
+ "learning_rate": 0.00012505681818181818,
538
+ "loss": 2.1188,
539
+ "step": 6800
540
+ },
541
+ {
542
+ "epoch": 0.08255872213456174,
543
+ "grad_norm": 0.2013303206012115,
544
+ "learning_rate": 0.000119375,
545
+ "loss": 2.1201,
546
+ "step": 6900
547
+ },
548
+ {
549
+ "epoch": 0.08375522535390321,
550
+ "grad_norm": 0.17865191614703033,
551
+ "learning_rate": 0.00011369318181818182,
552
+ "loss": 2.1184,
553
+ "step": 7000
554
+ },
555
+ {
556
+ "epoch": 0.08375522535390321,
557
+ "eval_accuracy": 0.5445597073721878,
558
+ "eval_loss": 2.119361400604248,
559
+ "eval_runtime": 8286.5111,
560
+ "eval_samples_per_second": 67.919,
561
+ "eval_steps_per_second": 8.49,
562
+ "step": 7000
563
+ },
564
+ {
565
+ "epoch": 0.08495172857324469,
566
+ "grad_norm": 0.2242220420058521,
567
+ "learning_rate": 0.00010801136363636365,
568
+ "loss": 2.1206,
569
+ "step": 7100
570
+ },
571
+ {
572
+ "epoch": 0.08614823179258617,
573
+ "grad_norm": 0.2361418777976826,
574
+ "learning_rate": 0.00010232954545454545,
575
+ "loss": 2.1223,
576
+ "step": 7200
577
+ },
578
+ {
579
+ "epoch": 0.08734473501192765,
580
+ "grad_norm": 0.2251269866404104,
581
+ "learning_rate": 9.664772727272728e-05,
582
+ "loss": 2.1188,
583
+ "step": 7300
584
+ },
585
+ {
586
+ "epoch": 0.08854123823126911,
587
+ "grad_norm": 0.2650506829716411,
588
+ "learning_rate": 9.09659090909091e-05,
589
+ "loss": 2.1204,
590
+ "step": 7400
591
+ },
592
+ {
593
+ "epoch": 0.08973774145061059,
594
+ "grad_norm": 0.2960065490838976,
595
+ "learning_rate": 8.52840909090909e-05,
596
+ "loss": 2.1166,
597
+ "step": 7500
598
+ },
599
+ {
600
+ "epoch": 0.09093424466995206,
601
+ "grad_norm": 0.2513130989479583,
602
+ "learning_rate": 7.960227272727273e-05,
603
+ "loss": 2.1191,
604
+ "step": 7600
605
+ },
606
+ {
607
+ "epoch": 0.09213074788929354,
608
+ "grad_norm": 0.23550646079468862,
609
+ "learning_rate": 7.392045454545454e-05,
610
+ "loss": 2.1207,
611
+ "step": 7700
612
+ },
613
+ {
614
+ "epoch": 0.09332725110863502,
615
+ "grad_norm": 0.20425354689020786,
616
+ "learning_rate": 6.823863636363637e-05,
617
+ "loss": 2.1196,
618
+ "step": 7800
619
+ },
620
+ {
621
+ "epoch": 0.0945237543279765,
622
+ "grad_norm": 0.27000259463730636,
623
+ "learning_rate": 6.255681818181818e-05,
624
+ "loss": 2.1208,
625
+ "step": 7900
626
+ },
627
+ {
628
+ "epoch": 0.09572025754731796,
629
+ "grad_norm": 0.23680396479288449,
630
+ "learning_rate": 5.6875e-05,
631
+ "loss": 2.119,
632
+ "step": 8000
633
+ },
634
+ {
635
+ "epoch": 0.09572025754731796,
636
+ "eval_accuracy": 0.54459064011865,
637
+ "eval_loss": 2.1189937591552734,
638
+ "eval_runtime": 8311.026,
639
+ "eval_samples_per_second": 67.719,
640
+ "eval_steps_per_second": 8.465,
641
+ "step": 8000
642
+ },
643
+ {
644
+ "epoch": 0.09691676076665943,
645
+ "grad_norm": 0.2356498019575465,
646
+ "learning_rate": 5.119318181818182e-05,
647
+ "loss": 2.117,
648
+ "step": 8100
649
+ },
650
+ {
651
+ "epoch": 0.09811326398600091,
652
+ "grad_norm": 0.2452139474957504,
653
+ "learning_rate": 4.5511363636363634e-05,
654
+ "loss": 2.1191,
655
+ "step": 8200
656
+ },
657
+ {
658
+ "epoch": 0.09930976720534239,
659
+ "grad_norm": 0.2570095681654715,
660
+ "learning_rate": 3.982954545454546e-05,
661
+ "loss": 2.1182,
662
+ "step": 8300
663
+ },
664
+ {
665
+ "epoch": 0.10050627042468387,
666
+ "grad_norm": 0.2928491115350356,
667
+ "learning_rate": 3.4147727272727274e-05,
668
+ "loss": 2.119,
669
+ "step": 8400
670
+ },
671
+ {
672
+ "epoch": 0.10170277364402533,
673
+ "grad_norm": 0.2534123898084123,
674
+ "learning_rate": 2.846590909090909e-05,
675
+ "loss": 2.1227,
676
+ "step": 8500
677
+ },
678
+ {
679
+ "epoch": 0.1028992768633668,
680
+ "grad_norm": 0.29376855634271687,
681
+ "learning_rate": 2.278409090909091e-05,
682
+ "loss": 2.1198,
683
+ "step": 8600
684
+ },
685
+ {
686
+ "epoch": 0.10409578008270828,
687
+ "grad_norm": 0.3837678078363553,
688
+ "learning_rate": 1.7102272727272728e-05,
689
+ "loss": 2.1206,
690
+ "step": 8700
691
+ },
692
+ {
693
+ "epoch": 0.10529228330204976,
694
+ "grad_norm": 0.24342629903402227,
695
+ "learning_rate": 1.1420454545454547e-05,
696
+ "loss": 2.12,
697
+ "step": 8800
698
+ },
699
+ {
700
+ "epoch": 0.10648878652139124,
701
+ "grad_norm": 0.18367698561178294,
702
+ "learning_rate": 5.7386363636363634e-06,
703
+ "loss": 2.1197,
704
+ "step": 8900
705
+ },
706
+ {
707
+ "epoch": 0.10768528974073271,
708
+ "grad_norm": 0.27943165556655064,
709
+ "learning_rate": 5.681818181818182e-08,
710
+ "loss": 2.1237,
711
+ "step": 9000
712
+ },
713
+ {
714
+ "epoch": 0.10768528974073271,
715
+ "eval_accuracy": 0.544624927857023,
716
+ "eval_loss": 2.12009596824646,
717
+ "eval_runtime": 8298.0507,
718
+ "eval_samples_per_second": 67.824,
719
+ "eval_steps_per_second": 8.478,
720
+ "step": 9000
721
+ }
722
+ ],
723
+ "logging_steps": 100,
724
+ "max_steps": 9000,
725
+ "num_input_tokens_seen": 0,
726
+ "num_train_epochs": 1,
727
+ "save_steps": 450,
728
+ "stateful_callbacks": {
729
+ "TrainerControl": {
730
+ "args": {
731
+ "should_epoch_stop": false,
732
+ "should_evaluate": false,
733
+ "should_log": false,
734
+ "should_save": true,
735
+ "should_training_stop": true
736
+ },
737
+ "attributes": {}
738
+ }
739
+ },
740
+ "total_flos": 1158274171797504.0,
741
+ "train_batch_size": 8,
742
+ "trial_name": null,
743
+ "trial_params": null
744
+ }
checkpoint-9000/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cef530fb4e1d2e9334e368a9556d9eb4c37e254304636d9458c4b52855ca4d03
3
+ size 6459
checkpoint-9000/zero_to_fp32.py ADDED
@@ -0,0 +1,592 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright (c) Microsoft Corporation.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ # DeepSpeed Team
7
+
8
+ # This script extracts fp32 consolidated weights from a zero 1, 2 and 3 DeepSpeed checkpoints. It gets
9
+ # copied into the top level checkpoint dir, so the user can easily do the conversion at any point in
10
+ # the future. Once extracted, the weights don't require DeepSpeed and can be used in any
11
+ # application.
12
+ #
13
+ # example: python zero_to_fp32.py . pytorch_model.bin
14
+
15
+ import argparse
16
+ import torch
17
+ import glob
18
+ import math
19
+ import os
20
+ import re
21
+ from collections import OrderedDict
22
+ from dataclasses import dataclass
23
+
24
+ # while this script doesn't use deepspeed to recover data, since the checkpoints are pickled with
25
+ # DeepSpeed data structures it has to be available in the current python environment.
26
+ from deepspeed.utils import logger
27
+ from deepspeed.checkpoint.constants import (DS_VERSION, OPTIMIZER_STATE_DICT, SINGLE_PARTITION_OF_FP32_GROUPS,
28
+ FP32_FLAT_GROUPS, ZERO_STAGE, PARTITION_COUNT, PARAM_SHAPES, BUFFER_NAMES,
29
+ FROZEN_PARAM_SHAPES, FROZEN_PARAM_FRAGMENTS)
30
+
31
+
32
+ @dataclass
33
+ class zero_model_state:
34
+ buffers: dict()
35
+ param_shapes: dict()
36
+ shared_params: list
37
+ ds_version: int
38
+ frozen_param_shapes: dict()
39
+ frozen_param_fragments: dict()
40
+
41
+
42
+ debug = 0
43
+
44
+ # load to cpu
45
+ device = torch.device('cpu')
46
+
47
+
48
+ def atoi(text):
49
+ return int(text) if text.isdigit() else text
50
+
51
+
52
+ def natural_keys(text):
53
+ '''
54
+ alist.sort(key=natural_keys) sorts in human order
55
+ http://nedbatchelder.com/blog/200712/human_sorting.html
56
+ (See Toothy's implementation in the comments)
57
+ '''
58
+ return [atoi(c) for c in re.split(r'(\d+)', text)]
59
+
60
+
61
+ def get_model_state_file(checkpoint_dir, zero_stage):
62
+ if not os.path.isdir(checkpoint_dir):
63
+ raise FileNotFoundError(f"Directory '{checkpoint_dir}' doesn't exist")
64
+
65
+ # there should be only one file
66
+ if zero_stage <= 2:
67
+ file = os.path.join(checkpoint_dir, "mp_rank_00_model_states.pt")
68
+ elif zero_stage == 3:
69
+ file = os.path.join(checkpoint_dir, "zero_pp_rank_0_mp_rank_00_model_states.pt")
70
+
71
+ if not os.path.exists(file):
72
+ raise FileNotFoundError(f"can't find model states file at '{file}'")
73
+
74
+ return file
75
+
76
+
77
+ def get_checkpoint_files(checkpoint_dir, glob_pattern):
78
+ # XXX: need to test that this simple glob rule works for multi-node setup too
79
+ ckpt_files = sorted(glob.glob(os.path.join(checkpoint_dir, glob_pattern)), key=natural_keys)
80
+
81
+ if len(ckpt_files) == 0:
82
+ raise FileNotFoundError(f"can't find {glob_pattern} files in directory '{checkpoint_dir}'")
83
+
84
+ return ckpt_files
85
+
86
+
87
+ def get_optim_files(checkpoint_dir):
88
+ return get_checkpoint_files(checkpoint_dir, "*_optim_states.pt")
89
+
90
+
91
+ def get_model_state_files(checkpoint_dir):
92
+ return get_checkpoint_files(checkpoint_dir, "*_model_states.pt")
93
+
94
+
95
+ def parse_model_states(files):
96
+ zero_model_states = []
97
+ for file in files:
98
+ state_dict = torch.load(file, map_location=device)
99
+
100
+ if BUFFER_NAMES not in state_dict:
101
+ raise ValueError(f"{file} is not a model state checkpoint")
102
+ buffer_names = state_dict[BUFFER_NAMES]
103
+ if debug:
104
+ print("Found buffers:", buffer_names)
105
+
106
+ # recover just the buffers while restoring them to fp32 if they were saved in fp16
107
+ buffers = {k: v.float() for k, v in state_dict["module"].items() if k in buffer_names}
108
+ param_shapes = state_dict[PARAM_SHAPES]
109
+
110
+ # collect parameters that are included in param_shapes
111
+ param_names = []
112
+ for s in param_shapes:
113
+ for name in s.keys():
114
+ param_names.append(name)
115
+
116
+ # update with frozen parameters
117
+ frozen_param_shapes = state_dict.get(FROZEN_PARAM_SHAPES, None)
118
+ if frozen_param_shapes is not None:
119
+ if debug:
120
+ print(f"Found frozen_param_shapes: {frozen_param_shapes}")
121
+ param_names += list(frozen_param_shapes.keys())
122
+
123
+ # handle shared params
124
+ shared_params = [[k, v] for k, v in state_dict["shared_params"].items()]
125
+
126
+ ds_version = state_dict.get(DS_VERSION, None)
127
+
128
+ frozen_param_fragments = state_dict.get(FROZEN_PARAM_FRAGMENTS, None)
129
+
130
+ z_model_state = zero_model_state(buffers=buffers,
131
+ param_shapes=param_shapes,
132
+ shared_params=shared_params,
133
+ ds_version=ds_version,
134
+ frozen_param_shapes=frozen_param_shapes,
135
+ frozen_param_fragments=frozen_param_fragments)
136
+ zero_model_states.append(z_model_state)
137
+
138
+ return zero_model_states
139
+
140
+
141
+ def parse_optim_states(files, ds_checkpoint_dir):
142
+
143
+ total_files = len(files)
144
+ state_dicts = []
145
+ for f in files:
146
+ state_dict = torch.load(f, map_location=device)
147
+ # immediately discard the potentially huge 2 optimizer states as we only care for fp32 master weights
148
+ # and also handle the case where it was already removed by another helper script
149
+ state_dict["optimizer_state_dict"].pop("optimizer_state_dict", None)
150
+ state_dicts.append(state_dict)
151
+
152
+ if not ZERO_STAGE in state_dicts[0][OPTIMIZER_STATE_DICT]:
153
+ raise ValueError(f"{files[0]} is not a zero checkpoint")
154
+ zero_stage = state_dicts[0][OPTIMIZER_STATE_DICT][ZERO_STAGE]
155
+ world_size = state_dicts[0][OPTIMIZER_STATE_DICT][PARTITION_COUNT]
156
+
157
+ # For ZeRO-2 each param group can have different partition_count as data parallelism for expert
158
+ # parameters can be different from data parallelism for non-expert parameters. So we can just
159
+ # use the max of the partition_count to get the dp world_size.
160
+
161
+ if type(world_size) is list:
162
+ world_size = max(world_size)
163
+
164
+ if world_size != total_files:
165
+ raise ValueError(
166
+ f"Expected {world_size} of '*_optim_states.pt' under '{ds_checkpoint_dir}' but found {total_files} files. "
167
+ "Possibly due to an overwrite of an old checkpoint, or a checkpoint didn't get saved by one or more processes."
168
+ )
169
+
170
+ # the groups are named differently in each stage
171
+ if zero_stage <= 2:
172
+ fp32_groups_key = SINGLE_PARTITION_OF_FP32_GROUPS
173
+ elif zero_stage == 3:
174
+ fp32_groups_key = FP32_FLAT_GROUPS
175
+ else:
176
+ raise ValueError(f"unknown zero stage {zero_stage}")
177
+
178
+ if zero_stage <= 2:
179
+ fp32_flat_groups = [state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key] for i in range(len(state_dicts))]
180
+ elif zero_stage == 3:
181
+ # if there is more than one param group, there will be multiple flattened tensors - one
182
+ # flattened tensor per group - for simplicity merge them into a single tensor
183
+ #
184
+ # XXX: could make the script more memory efficient for when there are multiple groups - it
185
+ # will require matching the sub-lists of param_shapes for each param group flattened tensor
186
+
187
+ fp32_flat_groups = [
188
+ torch.cat(state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key], 0) for i in range(len(state_dicts))
189
+ ]
190
+
191
+ return zero_stage, world_size, fp32_flat_groups
192
+
193
+
194
+ def _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir):
195
+ """
196
+ Returns fp32 state_dict reconstructed from ds checkpoint
197
+
198
+ Args:
199
+ - ``ds_checkpoint_dir``: path to the deepspeed checkpoint folder (where the optimizer files are)
200
+
201
+ """
202
+ print(f"Processing zero checkpoint '{ds_checkpoint_dir}'")
203
+
204
+ optim_files = get_optim_files(ds_checkpoint_dir)
205
+ zero_stage, world_size, fp32_flat_groups = parse_optim_states(optim_files, ds_checkpoint_dir)
206
+ print(f"Detected checkpoint of type zero stage {zero_stage}, world_size: {world_size}")
207
+
208
+ model_files = get_model_state_files(ds_checkpoint_dir)
209
+
210
+ zero_model_states = parse_model_states(model_files)
211
+ print(f'Parsing checkpoint created by deepspeed=={zero_model_states[0].ds_version}')
212
+
213
+ if zero_stage <= 2:
214
+ return _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states)
215
+ elif zero_stage == 3:
216
+ return _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states)
217
+
218
+
219
+ def _zero2_merge_frozen_params(state_dict, zero_model_states):
220
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
221
+ return
222
+
223
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
224
+ frozen_param_fragments = zero_model_states[0].frozen_param_fragments
225
+
226
+ if debug:
227
+ num_elem = sum(s.numel() for s in frozen_param_shapes.values())
228
+ print(f'rank 0: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
229
+
230
+ wanted_params = len(frozen_param_shapes)
231
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
232
+ avail_numel = sum([p.numel() for p in frozen_param_fragments.values()])
233
+ print(f'Frozen params: Have {avail_numel} numels to process.')
234
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
235
+
236
+ total_params = 0
237
+ total_numel = 0
238
+ for name, shape in frozen_param_shapes.items():
239
+ total_params += 1
240
+ unpartitioned_numel = shape.numel()
241
+ total_numel += unpartitioned_numel
242
+
243
+ state_dict[name] = frozen_param_fragments[name]
244
+
245
+ if debug:
246
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
247
+
248
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
249
+
250
+
251
+ def _has_callable(obj, fn):
252
+ attr = getattr(obj, fn, None)
253
+ return callable(attr)
254
+
255
+
256
+ def _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
257
+ param_shapes = zero_model_states[0].param_shapes
258
+
259
+ # Reconstruction protocol:
260
+ #
261
+ # XXX: document this
262
+
263
+ if debug:
264
+ for i in range(world_size):
265
+ for j in range(len(fp32_flat_groups[0])):
266
+ print(f"{FP32_FLAT_GROUPS}[{i}][{j}].shape={fp32_flat_groups[i][j].shape}")
267
+
268
+ # XXX: memory usage doubles here (zero2)
269
+ num_param_groups = len(fp32_flat_groups[0])
270
+ merged_single_partition_of_fp32_groups = []
271
+ for i in range(num_param_groups):
272
+ merged_partitions = [sd[i] for sd in fp32_flat_groups]
273
+ full_single_fp32_vector = torch.cat(merged_partitions, 0)
274
+ merged_single_partition_of_fp32_groups.append(full_single_fp32_vector)
275
+ avail_numel = sum(
276
+ [full_single_fp32_vector.numel() for full_single_fp32_vector in merged_single_partition_of_fp32_groups])
277
+
278
+ if debug:
279
+ wanted_params = sum([len(shapes) for shapes in param_shapes])
280
+ wanted_numel = sum([sum(shape.numel() for shape in shapes.values()) for shapes in param_shapes])
281
+ # not asserting if there is a mismatch due to possible padding
282
+ print(f"Have {avail_numel} numels to process.")
283
+ print(f"Need {wanted_numel} numels in {wanted_params} params.")
284
+
285
+ # params
286
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
287
+ # out-of-core computing solution
288
+ total_numel = 0
289
+ total_params = 0
290
+ for shapes, full_single_fp32_vector in zip(param_shapes, merged_single_partition_of_fp32_groups):
291
+ offset = 0
292
+ avail_numel = full_single_fp32_vector.numel()
293
+ for name, shape in shapes.items():
294
+
295
+ unpartitioned_numel = shape.numel() if _has_callable(shape, 'numel') else math.prod(shape)
296
+ total_numel += unpartitioned_numel
297
+ total_params += 1
298
+
299
+ if debug:
300
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
301
+ state_dict[name] = full_single_fp32_vector.narrow(0, offset, unpartitioned_numel).view(shape)
302
+ offset += unpartitioned_numel
303
+
304
+ # Z2 started to align to 2*world_size to improve nccl performance. Therefore both offset and
305
+ # avail_numel can differ by anywhere between 0..2*world_size. Due to two unrelated complex
306
+ # paddings performed in the code it's almost impossible to predict the exact numbers w/o the
307
+ # live optimizer object, so we are checking that the numbers are within the right range
308
+ align_to = 2 * world_size
309
+
310
+ def zero2_align(x):
311
+ return align_to * math.ceil(x / align_to)
312
+
313
+ if debug:
314
+ print(f"original offset={offset}, avail_numel={avail_numel}")
315
+
316
+ offset = zero2_align(offset)
317
+ avail_numel = zero2_align(avail_numel)
318
+
319
+ if debug:
320
+ print(f"aligned offset={offset}, avail_numel={avail_numel}")
321
+
322
+ # Sanity check
323
+ if offset != avail_numel:
324
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
325
+
326
+ print(f"Reconstructed fp32 state dict with {total_params} params {total_numel} elements")
327
+
328
+
329
+ def _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states):
330
+ state_dict = OrderedDict()
331
+
332
+ # buffers
333
+ buffers = zero_model_states[0].buffers
334
+ state_dict.update(buffers)
335
+ if debug:
336
+ print(f"added {len(buffers)} buffers")
337
+
338
+ _zero2_merge_frozen_params(state_dict, zero_model_states)
339
+
340
+ _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
341
+
342
+ # recover shared parameters
343
+ for pair in zero_model_states[0].shared_params:
344
+ if pair[1] in state_dict:
345
+ state_dict[pair[0]] = state_dict[pair[1]]
346
+
347
+ return state_dict
348
+
349
+
350
+ def zero3_partitioned_param_info(unpartitioned_numel, world_size):
351
+ remainder = unpartitioned_numel % world_size
352
+ padding_numel = (world_size - remainder) if remainder else 0
353
+ partitioned_numel = math.ceil(unpartitioned_numel / world_size)
354
+ return partitioned_numel, padding_numel
355
+
356
+
357
+ def _zero3_merge_frozen_params(state_dict, world_size, zero_model_states):
358
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
359
+ return
360
+
361
+ if debug:
362
+ for i in range(world_size):
363
+ num_elem = sum(s.numel() for s in zero_model_states[i].frozen_param_fragments.values())
364
+ print(f'rank {i}: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
365
+
366
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
367
+ wanted_params = len(frozen_param_shapes)
368
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
369
+ avail_numel = sum([p.numel() for p in zero_model_states[0].frozen_param_fragments.values()]) * world_size
370
+ print(f'Frozen params: Have {avail_numel} numels to process.')
371
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
372
+
373
+ total_params = 0
374
+ total_numel = 0
375
+ for name, shape in zero_model_states[0].frozen_param_shapes.items():
376
+ total_params += 1
377
+ unpartitioned_numel = shape.numel()
378
+ total_numel += unpartitioned_numel
379
+
380
+ param_frags = tuple(model_state.frozen_param_fragments[name] for model_state in zero_model_states)
381
+ state_dict[name] = torch.cat(param_frags, 0).narrow(0, 0, unpartitioned_numel).view(shape)
382
+
383
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
384
+
385
+ if debug:
386
+ print(
387
+ f"Frozen params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
388
+ )
389
+
390
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
391
+
392
+
393
+ def _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
394
+ param_shapes = zero_model_states[0].param_shapes
395
+ avail_numel = fp32_flat_groups[0].numel() * world_size
396
+ # Reconstruction protocol: For zero3 we need to zip the partitions together at boundary of each
397
+ # param, re-consolidating each param, while dealing with padding if any
398
+
399
+ # merge list of dicts, preserving order
400
+ param_shapes = {k: v for d in param_shapes for k, v in d.items()}
401
+
402
+ if debug:
403
+ for i in range(world_size):
404
+ print(f"{FP32_FLAT_GROUPS}[{i}].shape={fp32_flat_groups[i].shape}")
405
+
406
+ wanted_params = len(param_shapes)
407
+ wanted_numel = sum(shape.numel() for shape in param_shapes.values())
408
+ # not asserting if there is a mismatch due to possible padding
409
+ avail_numel = fp32_flat_groups[0].numel() * world_size
410
+ print(f"Trainable params: Have {avail_numel} numels to process.")
411
+ print(f"Trainable params: Need {wanted_numel} numels in {wanted_params} params.")
412
+
413
+ # params
414
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
415
+ # out-of-core computing solution
416
+ offset = 0
417
+ total_numel = 0
418
+ total_params = 0
419
+ for name, shape in param_shapes.items():
420
+
421
+ unpartitioned_numel = shape.numel()
422
+ total_numel += unpartitioned_numel
423
+ total_params += 1
424
+
425
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
426
+
427
+ if debug:
428
+ print(
429
+ f"Trainable params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
430
+ )
431
+
432
+ # XXX: memory usage doubles here
433
+ state_dict[name] = torch.cat(
434
+ tuple(fp32_flat_groups[i].narrow(0, offset, partitioned_numel) for i in range(world_size)),
435
+ 0).narrow(0, 0, unpartitioned_numel).view(shape)
436
+ offset += partitioned_numel
437
+
438
+ offset *= world_size
439
+
440
+ # Sanity check
441
+ if offset != avail_numel:
442
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
443
+
444
+ print(f"Reconstructed Trainable fp32 state dict with {total_params} params {total_numel} elements")
445
+
446
+
447
+ def _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states):
448
+ state_dict = OrderedDict()
449
+
450
+ # buffers
451
+ buffers = zero_model_states[0].buffers
452
+ state_dict.update(buffers)
453
+ if debug:
454
+ print(f"added {len(buffers)} buffers")
455
+
456
+ _zero3_merge_frozen_params(state_dict, world_size, zero_model_states)
457
+
458
+ _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
459
+
460
+ # recover shared parameters
461
+ for pair in zero_model_states[0].shared_params:
462
+ if pair[1] in state_dict:
463
+ state_dict[pair[0]] = state_dict[pair[1]]
464
+
465
+ return state_dict
466
+
467
+
468
+ def get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag=None):
469
+ """
470
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated state_dict that can be loaded with
471
+ ``load_state_dict()`` and used for training without DeepSpeed or shared with others, for example
472
+ via a model hub.
473
+
474
+ Args:
475
+ - ``checkpoint_dir``: path to the desired checkpoint folder
476
+ - ``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``
477
+
478
+ Returns:
479
+ - pytorch ``state_dict``
480
+
481
+ Note: this approach may not work if your application doesn't have sufficient free CPU memory and
482
+ you may need to use the offline approach using the ``zero_to_fp32.py`` script that is saved with
483
+ the checkpoint.
484
+
485
+ A typical usage might be ::
486
+
487
+ from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
488
+ # do the training and checkpoint saving
489
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu
490
+ model = model.cpu() # move to cpu
491
+ model.load_state_dict(state_dict)
492
+ # submit to model hub or save the model to share with others
493
+
494
+ In this example the ``model`` will no longer be usable in the deepspeed context of the same
495
+ application. i.e. you will need to re-initialize the deepspeed engine, since
496
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
497
+
498
+ If you want it all done for you, use ``load_state_dict_from_zero_checkpoint`` instead.
499
+
500
+ """
501
+ if tag is None:
502
+ latest_path = os.path.join(checkpoint_dir, 'latest')
503
+ if os.path.isfile(latest_path):
504
+ with open(latest_path, 'r') as fd:
505
+ tag = fd.read().strip()
506
+ else:
507
+ raise ValueError(f"Unable to find 'latest' file at {latest_path}")
508
+
509
+ ds_checkpoint_dir = os.path.join(checkpoint_dir, tag)
510
+
511
+ if not os.path.isdir(ds_checkpoint_dir):
512
+ raise FileNotFoundError(f"Directory '{ds_checkpoint_dir}' doesn't exist")
513
+
514
+ return _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir)
515
+
516
+
517
+ def convert_zero_checkpoint_to_fp32_state_dict(checkpoint_dir, output_file, tag=None):
518
+ """
519
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict`` file that can be
520
+ loaded with ``torch.load(file)`` + ``load_state_dict()`` and used for training without DeepSpeed.
521
+
522
+ Args:
523
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
524
+ - ``output_file``: path to the pytorch fp32 state_dict output file (e.g. path/pytorch_model.bin)
525
+ - ``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``
526
+ """
527
+
528
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
529
+ print(f"Saving fp32 state dict to {output_file}")
530
+ torch.save(state_dict, output_file)
531
+
532
+
533
+ def load_state_dict_from_zero_checkpoint(model, checkpoint_dir, tag=None):
534
+ """
535
+ 1. Put the provided model to cpu
536
+ 2. Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict``
537
+ 3. Load it into the provided model
538
+
539
+ Args:
540
+ - ``model``: the model object to update
541
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
542
+ - ``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``
543
+
544
+ Returns:
545
+ - ``model`: modified model
546
+
547
+ Make sure you have plenty of CPU memory available before you call this function. If you don't
548
+ have enough use the ``zero_to_fp32.py`` utility to do the conversion. You will find it
549
+ conveniently placed for you in the checkpoint folder.
550
+
551
+ A typical usage might be ::
552
+
553
+ from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint
554
+ model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir)
555
+ # submit to model hub or save the model to share with others
556
+
557
+ Note, that once this was run, the ``model`` will no longer be usable in the deepspeed context
558
+ of the same application. i.e. you will need to re-initialize the deepspeed engine, since
559
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
560
+
561
+ """
562
+ logger.info(f"Extracting fp32 weights")
563
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
564
+
565
+ logger.info(f"Overwriting model with fp32 weights")
566
+ model = model.cpu()
567
+ model.load_state_dict(state_dict, strict=False)
568
+
569
+ return model
570
+
571
+
572
+ if __name__ == "__main__":
573
+
574
+ parser = argparse.ArgumentParser()
575
+ parser.add_argument("checkpoint_dir",
576
+ type=str,
577
+ help="path to the desired checkpoint folder, e.g., path/checkpoint-12")
578
+ parser.add_argument(
579
+ "output_file",
580
+ type=str,
581
+ help="path to the pytorch fp32 state_dict output file (e.g. path/checkpoint-12/pytorch_model.bin)")
582
+ parser.add_argument("-t",
583
+ "--tag",
584
+ type=str,
585
+ default=None,
586
+ help="checkpoint tag used as a unique identifier for checkpoint. e.g., global_step1")
587
+ parser.add_argument("-d", "--debug", action='store_true', help="enable debug")
588
+ args = parser.parse_args()
589
+
590
+ debug = args.debug
591
+
592
+ convert_zero_checkpoint_to_fp32_state_dict(args.checkpoint_dir, args.output_file, tag=args.tag)
config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/src/tanner/chess-roberta-code/model_configs/chess_roberta.json",
3
+ "architectures": [
4
+ "RobertaForMaskedLM"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "classifier_dropout": null,
9
+ "eos_token_id": 1,
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 768,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 3072,
15
+ "layer_norm_eps": 1e-05,
16
+ "max_position_embeddings": 1030,
17
+ "model_type": "roberta",
18
+ "num_attention_heads": 12,
19
+ "num_hidden_layers": 16,
20
+ "pad_token_id": 2,
21
+ "position_embedding_type": "absolute",
22
+ "torch_dtype": "bfloat16",
23
+ "transformers_version": "4.42.4",
24
+ "type_vocab_size": 1,
25
+ "use_cache": true,
26
+ "vocab_size": 64
27
+ }
eval_results.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "epoch": 0.10768528974073271,
3
+ "eval_accuracy": 0.5445640378503339,
4
+ "eval_loss": 2.1201696395874023,
5
+ "eval_runtime": 8310.7479,
6
+ "eval_samples": 562811,
7
+ "eval_samples_per_second": 67.721,
8
+ "eval_steps_per_second": 8.465,
9
+ "perplexity": 8.332550898295501
10
+ }
global_step9000/bf16_zero_pp_rank_0_mp_rank_00_optim_states.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c279dc883c0d65945b83146c6a1901c3265ee01b398b33b0e234c652ee2d446
3
+ size 1378093111
global_step9000/zero_pp_rank_0_mp_rank_00_model_states.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:78aa760df334a408b1ff1cad6785692d84af6c1c49652716d7cfd0b815914075
3
+ size 140897
latest ADDED
@@ -0,0 +1 @@
 
 
1
+ global_step9000
special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "mask_token": {
10
+ "content": "[MASK]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "[PAD]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "sep_token": {
24
+ "content": "[SEP]",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "[UNK]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
tokenizer.json ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "version": "1.0",
3
+ "truncation": null,
4
+ "padding": null,
5
+ "added_tokens": [
6
+ {
7
+ "id": 0,
8
+ "content": "[CLS]",
9
+ "single_word": false,
10
+ "lstrip": false,
11
+ "rstrip": false,
12
+ "normalized": false,
13
+ "special": true
14
+ },
15
+ {
16
+ "id": 1,
17
+ "content": "[SEP]",
18
+ "single_word": false,
19
+ "lstrip": false,
20
+ "rstrip": false,
21
+ "normalized": false,
22
+ "special": true
23
+ },
24
+ {
25
+ "id": 2,
26
+ "content": "[PAD]",
27
+ "single_word": false,
28
+ "lstrip": false,
29
+ "rstrip": false,
30
+ "normalized": false,
31
+ "special": true
32
+ },
33
+ {
34
+ "id": 3,
35
+ "content": "[UNK]",
36
+ "single_word": false,
37
+ "lstrip": false,
38
+ "rstrip": false,
39
+ "normalized": false,
40
+ "special": true
41
+ },
42
+ {
43
+ "id": 4,
44
+ "content": "[MASK]",
45
+ "single_word": false,
46
+ "lstrip": false,
47
+ "rstrip": false,
48
+ "normalized": false,
49
+ "special": true
50
+ },
51
+ {
52
+ "id": 5,
53
+ "content": "[ILLEGAL]",
54
+ "single_word": false,
55
+ "lstrip": false,
56
+ "rstrip": false,
57
+ "normalized": false,
58
+ "special": true
59
+ },
60
+ {
61
+ "id": 6,
62
+ "content": "~",
63
+ "single_word": false,
64
+ "lstrip": false,
65
+ "rstrip": false,
66
+ "normalized": false,
67
+ "special": true
68
+ }
69
+ ],
70
+ "normalizer": {
71
+ "type": "Sequence",
72
+ "normalizers": [
73
+ {
74
+ "type": "NFD"
75
+ },
76
+ {
77
+ "type": "StripAccents"
78
+ }
79
+ ]
80
+ },
81
+ "pre_tokenizer": {
82
+ "type": "Split",
83
+ "pattern": {
84
+ "String": ""
85
+ },
86
+ "behavior": "Isolated",
87
+ "invert": false
88
+ },
89
+ "post_processor": {
90
+ "type": "RobertaProcessing",
91
+ "sep": [
92
+ "[SEP]",
93
+ 1
94
+ ],
95
+ "cls": [
96
+ "[CLS]",
97
+ 0
98
+ ],
99
+ "trim_offsets": false,
100
+ "add_prefix_space": false
101
+ },
102
+ "decoder": {
103
+ "type": "WordPiece",
104
+ "prefix": "##",
105
+ "cleanup": true
106
+ },
107
+ "model": {
108
+ "type": "WordPiece",
109
+ "unk_token": "[UNK]",
110
+ "continuing_subword_prefix": "##",
111
+ "max_input_chars_per_word": 100,
112
+ "vocab": {
113
+ "[CLS]": 0,
114
+ "[SEP]": 1,
115
+ "[PAD]": 2,
116
+ "[UNK]": 3,
117
+ "[MASK]": 4,
118
+ "[ILLEGAL]": 5,
119
+ "~": 6,
120
+ ">": 7,
121
+ " ": 8,
122
+ "#": 9,
123
+ "+": 10,
124
+ "-": 11,
125
+ "/": 12,
126
+ "0": 13,
127
+ "1": 14,
128
+ "2": 15,
129
+ "3": 16,
130
+ "4": 17,
131
+ "5": 18,
132
+ "6": 19,
133
+ "7": 20,
134
+ "8": 21,
135
+ "9": 22,
136
+ "=": 23,
137
+ "B": 24,
138
+ "K": 25,
139
+ "N": 26,
140
+ "O": 27,
141
+ "P": 28,
142
+ "Q": 29,
143
+ "R": 30,
144
+ "a": 31,
145
+ "b": 32,
146
+ "c": 33,
147
+ "d": 34,
148
+ "e": 35,
149
+ "f": 36,
150
+ "g": 37,
151
+ "h": 38,
152
+ "k": 39,
153
+ "n": 40,
154
+ "p": 41,
155
+ "q": 42,
156
+ "r": 43,
157
+ "w": 44,
158
+ "x": 45,
159
+ "_": 46
160
+ }
161
+ }
162
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[CLS]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "[SEP]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "[PAD]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "[UNK]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "4": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ },
43
+ "5": {
44
+ "content": "[ILLEGAL]",
45
+ "lstrip": false,
46
+ "normalized": false,
47
+ "rstrip": false,
48
+ "single_word": false,
49
+ "special": true
50
+ },
51
+ "6": {
52
+ "content": "~",
53
+ "lstrip": false,
54
+ "normalized": false,
55
+ "rstrip": false,
56
+ "single_word": false,
57
+ "special": true
58
+ }
59
+ },
60
+ "clean_up_tokenization_spaces": true,
61
+ "cls_token": "[CLS]",
62
+ "mask_token": "[MASK]",
63
+ "model_max_length": 1000000000000000019884624838656,
64
+ "pad_token": "[PAD]",
65
+ "sep_token": "[SEP]",
66
+ "tokenizer_class": "PreTrainedTokenizerFast",
67
+ "unk_token": "[UNK]"
68
+ }
train_results.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "epoch": 0.10768528974073271,
3
+ "total_flos": 1158274171797504.0,
4
+ "train_loss": 2.120608181423611,
5
+ "train_runtime": 152416.4466,
6
+ "train_samples": 10697834,
7
+ "train_samples_per_second": 7.558,
8
+ "train_steps_per_second": 0.059
9
+ }
trainer_state.json ADDED
@@ -0,0 +1,753 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "best_metric": null,
3
+ "best_model_checkpoint": null,
4
+ "epoch": 0.10768528974073271,
5
+ "eval_steps": 1000,
6
+ "global_step": 9000,
7
+ "is_hyper_param_search": false,
8
+ "is_local_process_zero": true,
9
+ "is_world_process_zero": true,
10
+ "log_history": [
11
+ {
12
+ "epoch": 0.0011965032193414745,
13
+ "grad_norm": 0.4959045114979248,
14
+ "learning_rate": 0.0002475,
15
+ "loss": 2.2052,
16
+ "step": 100
17
+ },
18
+ {
19
+ "epoch": 0.002393006438682949,
20
+ "grad_norm": 0.5957897298098443,
21
+ "learning_rate": 0.0004975,
22
+ "loss": 2.1236,
23
+ "step": 200
24
+ },
25
+ {
26
+ "epoch": 0.0035895096580244234,
27
+ "grad_norm": 0.5462649355396835,
28
+ "learning_rate": 0.000494375,
29
+ "loss": 2.123,
30
+ "step": 300
31
+ },
32
+ {
33
+ "epoch": 0.004786012877365898,
34
+ "grad_norm": 0.479307539013867,
35
+ "learning_rate": 0.0004886931818181818,
36
+ "loss": 2.1201,
37
+ "step": 400
38
+ },
39
+ {
40
+ "epoch": 0.005982516096707372,
41
+ "grad_norm": 0.4702932661295846,
42
+ "learning_rate": 0.0004830113636363637,
43
+ "loss": 2.1218,
44
+ "step": 500
45
+ },
46
+ {
47
+ "epoch": 0.007179019316048847,
48
+ "grad_norm": 0.4536330377549505,
49
+ "learning_rate": 0.0004773295454545455,
50
+ "loss": 2.1199,
51
+ "step": 600
52
+ },
53
+ {
54
+ "epoch": 0.008375522535390322,
55
+ "grad_norm": 0.30397230580627244,
56
+ "learning_rate": 0.00047164772727272724,
57
+ "loss": 2.122,
58
+ "step": 700
59
+ },
60
+ {
61
+ "epoch": 0.009572025754731796,
62
+ "grad_norm": 0.5497084490461644,
63
+ "learning_rate": 0.0004659659090909091,
64
+ "loss": 2.1195,
65
+ "step": 800
66
+ },
67
+ {
68
+ "epoch": 0.010768528974073271,
69
+ "grad_norm": 0.31265244740901976,
70
+ "learning_rate": 0.0004602840909090909,
71
+ "loss": 2.1214,
72
+ "step": 900
73
+ },
74
+ {
75
+ "epoch": 0.011965032193414745,
76
+ "grad_norm": 0.38764126785839226,
77
+ "learning_rate": 0.0004546022727272727,
78
+ "loss": 2.1201,
79
+ "step": 1000
80
+ },
81
+ {
82
+ "epoch": 0.011965032193414745,
83
+ "eval_accuracy": 0.544493419943561,
84
+ "eval_loss": 2.119503974914551,
85
+ "eval_runtime": 14498.4824,
86
+ "eval_samples_per_second": 38.819,
87
+ "eval_steps_per_second": 4.852,
88
+ "step": 1000
89
+ },
90
+ {
91
+ "epoch": 0.01316153541275622,
92
+ "grad_norm": 0.37861216335098746,
93
+ "learning_rate": 0.00044892045454545456,
94
+ "loss": 2.1179,
95
+ "step": 1100
96
+ },
97
+ {
98
+ "epoch": 0.014358038632097694,
99
+ "grad_norm": 0.3244816871915935,
100
+ "learning_rate": 0.00044323863636363636,
101
+ "loss": 2.123,
102
+ "step": 1200
103
+ },
104
+ {
105
+ "epoch": 0.015554541851439169,
106
+ "grad_norm": 0.39134094432184025,
107
+ "learning_rate": 0.0004375568181818182,
108
+ "loss": 2.1192,
109
+ "step": 1300
110
+ },
111
+ {
112
+ "epoch": 0.016751045070780644,
113
+ "grad_norm": 0.31850239786818274,
114
+ "learning_rate": 0.000431875,
115
+ "loss": 2.1195,
116
+ "step": 1400
117
+ },
118
+ {
119
+ "epoch": 0.017947548290122118,
120
+ "grad_norm": 0.3353874978413676,
121
+ "learning_rate": 0.0004261931818181818,
122
+ "loss": 2.1187,
123
+ "step": 1500
124
+ },
125
+ {
126
+ "epoch": 0.01914405150946359,
127
+ "grad_norm": 0.3666941040198008,
128
+ "learning_rate": 0.0004205113636363637,
129
+ "loss": 2.1208,
130
+ "step": 1600
131
+ },
132
+ {
133
+ "epoch": 0.02034055472880507,
134
+ "grad_norm": 0.2874688345676318,
135
+ "learning_rate": 0.0004148295454545455,
136
+ "loss": 2.1182,
137
+ "step": 1700
138
+ },
139
+ {
140
+ "epoch": 0.021537057948146542,
141
+ "grad_norm": 0.28829546768632713,
142
+ "learning_rate": 0.00040914772727272723,
143
+ "loss": 2.1202,
144
+ "step": 1800
145
+ },
146
+ {
147
+ "epoch": 0.022733561167488016,
148
+ "grad_norm": 0.3462480959698083,
149
+ "learning_rate": 0.0004034659090909091,
150
+ "loss": 2.1185,
151
+ "step": 1900
152
+ },
153
+ {
154
+ "epoch": 0.02393006438682949,
155
+ "grad_norm": 0.32146933893861385,
156
+ "learning_rate": 0.0003977840909090909,
157
+ "loss": 2.1222,
158
+ "step": 2000
159
+ },
160
+ {
161
+ "epoch": 0.02393006438682949,
162
+ "eval_accuracy": 0.5445787464391751,
163
+ "eval_loss": 2.1198692321777344,
164
+ "eval_runtime": 8324.5002,
165
+ "eval_samples_per_second": 67.609,
166
+ "eval_steps_per_second": 8.451,
167
+ "step": 2000
168
+ },
169
+ {
170
+ "epoch": 0.025126567606170967,
171
+ "grad_norm": 0.3383598719071586,
172
+ "learning_rate": 0.0003921022727272727,
173
+ "loss": 2.1181,
174
+ "step": 2100
175
+ },
176
+ {
177
+ "epoch": 0.02632307082551244,
178
+ "grad_norm": 0.23870777428521156,
179
+ "learning_rate": 0.00038642045454545456,
180
+ "loss": 2.1201,
181
+ "step": 2200
182
+ },
183
+ {
184
+ "epoch": 0.027519574044853914,
185
+ "grad_norm": 0.26512869204988454,
186
+ "learning_rate": 0.00038073863636363636,
187
+ "loss": 2.1181,
188
+ "step": 2300
189
+ },
190
+ {
191
+ "epoch": 0.028716077264195387,
192
+ "grad_norm": 0.38291085278916004,
193
+ "learning_rate": 0.0003750568181818182,
194
+ "loss": 2.1214,
195
+ "step": 2400
196
+ },
197
+ {
198
+ "epoch": 0.029912580483536864,
199
+ "grad_norm": 0.2922237601337328,
200
+ "learning_rate": 0.000369375,
201
+ "loss": 2.1178,
202
+ "step": 2500
203
+ },
204
+ {
205
+ "epoch": 0.031109083702878338,
206
+ "grad_norm": 0.28007930758064187,
207
+ "learning_rate": 0.0003636931818181818,
208
+ "loss": 2.1174,
209
+ "step": 2600
210
+ },
211
+ {
212
+ "epoch": 0.03230558692221981,
213
+ "grad_norm": 0.37650924862735796,
214
+ "learning_rate": 0.0003580113636363637,
215
+ "loss": 2.1202,
216
+ "step": 2700
217
+ },
218
+ {
219
+ "epoch": 0.03350209014156129,
220
+ "grad_norm": 0.32087380641998864,
221
+ "learning_rate": 0.0003523295454545455,
222
+ "loss": 2.1205,
223
+ "step": 2800
224
+ },
225
+ {
226
+ "epoch": 0.03469859336090276,
227
+ "grad_norm": 0.3065591070785156,
228
+ "learning_rate": 0.00034664772727272723,
229
+ "loss": 2.1219,
230
+ "step": 2900
231
+ },
232
+ {
233
+ "epoch": 0.035895096580244236,
234
+ "grad_norm": 0.4973396205464692,
235
+ "learning_rate": 0.0003409659090909091,
236
+ "loss": 2.1179,
237
+ "step": 3000
238
+ },
239
+ {
240
+ "epoch": 0.035895096580244236,
241
+ "eval_accuracy": 0.5444474125165799,
242
+ "eval_loss": 2.119871139526367,
243
+ "eval_runtime": 14003.3046,
244
+ "eval_samples_per_second": 40.191,
245
+ "eval_steps_per_second": 5.024,
246
+ "step": 3000
247
+ },
248
+ {
249
+ "epoch": 0.03709159979958571,
250
+ "grad_norm": 0.36054926026192535,
251
+ "learning_rate": 0.0003352840909090909,
252
+ "loss": 2.1207,
253
+ "step": 3100
254
+ },
255
+ {
256
+ "epoch": 0.03828810301892718,
257
+ "grad_norm": 0.2609329016792503,
258
+ "learning_rate": 0.0003296022727272727,
259
+ "loss": 2.1187,
260
+ "step": 3200
261
+ },
262
+ {
263
+ "epoch": 0.03948460623826866,
264
+ "grad_norm": 0.4377934640382202,
265
+ "learning_rate": 0.00032392045454545455,
266
+ "loss": 2.1183,
267
+ "step": 3300
268
+ },
269
+ {
270
+ "epoch": 0.04068110945761014,
271
+ "grad_norm": 0.30040663333811507,
272
+ "learning_rate": 0.00031823863636363636,
273
+ "loss": 2.1207,
274
+ "step": 3400
275
+ },
276
+ {
277
+ "epoch": 0.04187761267695161,
278
+ "grad_norm": 0.4058031327319446,
279
+ "learning_rate": 0.0003125568181818182,
280
+ "loss": 2.1175,
281
+ "step": 3500
282
+ },
283
+ {
284
+ "epoch": 0.043074115896293085,
285
+ "grad_norm": 0.2221276357895751,
286
+ "learning_rate": 0.000306875,
287
+ "loss": 2.1191,
288
+ "step": 3600
289
+ },
290
+ {
291
+ "epoch": 0.044270619115634555,
292
+ "grad_norm": 0.2171065800667589,
293
+ "learning_rate": 0.0003011931818181818,
294
+ "loss": 2.1222,
295
+ "step": 3700
296
+ },
297
+ {
298
+ "epoch": 0.04546712233497603,
299
+ "grad_norm": 0.24009215814108797,
300
+ "learning_rate": 0.0002955113636363637,
301
+ "loss": 2.1206,
302
+ "step": 3800
303
+ },
304
+ {
305
+ "epoch": 0.04666362555431751,
306
+ "grad_norm": 0.23325080103491874,
307
+ "learning_rate": 0.0002898295454545455,
308
+ "loss": 2.1219,
309
+ "step": 3900
310
+ },
311
+ {
312
+ "epoch": 0.04786012877365898,
313
+ "grad_norm": 0.38496953527087835,
314
+ "learning_rate": 0.00028414772727272723,
315
+ "loss": 2.1215,
316
+ "step": 4000
317
+ },
318
+ {
319
+ "epoch": 0.04786012877365898,
320
+ "eval_accuracy": 0.5445841718354034,
321
+ "eval_loss": 2.1186254024505615,
322
+ "eval_runtime": 14020.6018,
323
+ "eval_samples_per_second": 40.142,
324
+ "eval_steps_per_second": 5.018,
325
+ "step": 4000
326
+ },
327
+ {
328
+ "epoch": 0.049056631993000456,
329
+ "grad_norm": 0.2049052054281515,
330
+ "learning_rate": 0.0002784659090909091,
331
+ "loss": 2.1184,
332
+ "step": 4100
333
+ },
334
+ {
335
+ "epoch": 0.05025313521234193,
336
+ "grad_norm": 0.2458322734082573,
337
+ "learning_rate": 0.0002727840909090909,
338
+ "loss": 2.1196,
339
+ "step": 4200
340
+ },
341
+ {
342
+ "epoch": 0.0514496384316834,
343
+ "grad_norm": 0.1995524240389471,
344
+ "learning_rate": 0.0002671022727272727,
345
+ "loss": 2.1188,
346
+ "step": 4300
347
+ },
348
+ {
349
+ "epoch": 0.05264614165102488,
350
+ "grad_norm": 0.25790141869897587,
351
+ "learning_rate": 0.00026142045454545455,
352
+ "loss": 2.1184,
353
+ "step": 4400
354
+ },
355
+ {
356
+ "epoch": 0.05384264487036636,
357
+ "grad_norm": 0.28730537897175445,
358
+ "learning_rate": 0.00025573863636363636,
359
+ "loss": 2.1202,
360
+ "step": 4500
361
+ },
362
+ {
363
+ "epoch": 0.05503914808970783,
364
+ "grad_norm": 0.32493426349642957,
365
+ "learning_rate": 0.0002500568181818182,
366
+ "loss": 2.118,
367
+ "step": 4600
368
+ },
369
+ {
370
+ "epoch": 0.056235651309049305,
371
+ "grad_norm": 0.29535898171610375,
372
+ "learning_rate": 0.000244375,
373
+ "loss": 2.1206,
374
+ "step": 4700
375
+ },
376
+ {
377
+ "epoch": 0.057432154528390775,
378
+ "grad_norm": 0.22683681078434978,
379
+ "learning_rate": 0.00023869318181818182,
380
+ "loss": 2.117,
381
+ "step": 4800
382
+ },
383
+ {
384
+ "epoch": 0.05862865774773225,
385
+ "grad_norm": 0.3053349551242291,
386
+ "learning_rate": 0.00023301136363636362,
387
+ "loss": 2.1179,
388
+ "step": 4900
389
+ },
390
+ {
391
+ "epoch": 0.05982516096707373,
392
+ "grad_norm": 0.1948263427424577,
393
+ "learning_rate": 0.00022732954545454545,
394
+ "loss": 2.1176,
395
+ "step": 5000
396
+ },
397
+ {
398
+ "epoch": 0.05982516096707373,
399
+ "eval_accuracy": 0.5445371572840895,
400
+ "eval_loss": 2.119377613067627,
401
+ "eval_runtime": 12119.2615,
402
+ "eval_samples_per_second": 46.439,
403
+ "eval_steps_per_second": 5.805,
404
+ "step": 5000
405
+ },
406
+ {
407
+ "epoch": 0.0610216641864152,
408
+ "grad_norm": 0.29200116272624654,
409
+ "learning_rate": 0.00022164772727272728,
410
+ "loss": 2.1192,
411
+ "step": 5100
412
+ },
413
+ {
414
+ "epoch": 0.062218167405756676,
415
+ "grad_norm": 0.25843746999947237,
416
+ "learning_rate": 0.00021596590909090912,
417
+ "loss": 2.1193,
418
+ "step": 5200
419
+ },
420
+ {
421
+ "epoch": 0.06341467062509815,
422
+ "grad_norm": 0.23460770073478823,
423
+ "learning_rate": 0.00021028409090909092,
424
+ "loss": 2.1196,
425
+ "step": 5300
426
+ },
427
+ {
428
+ "epoch": 0.06461117384443962,
429
+ "grad_norm": 0.2143373312023261,
430
+ "learning_rate": 0.00020460227272727272,
431
+ "loss": 2.1194,
432
+ "step": 5400
433
+ },
434
+ {
435
+ "epoch": 0.0658076770637811,
436
+ "grad_norm": 0.2738612197646279,
437
+ "learning_rate": 0.00019892045454545455,
438
+ "loss": 2.1215,
439
+ "step": 5500
440
+ },
441
+ {
442
+ "epoch": 0.06700418028312258,
443
+ "grad_norm": 0.22066659063762534,
444
+ "learning_rate": 0.00019323863636363636,
445
+ "loss": 2.1185,
446
+ "step": 5600
447
+ },
448
+ {
449
+ "epoch": 0.06820068350246405,
450
+ "grad_norm": 0.2649683554628019,
451
+ "learning_rate": 0.00018755681818181819,
452
+ "loss": 2.117,
453
+ "step": 5700
454
+ },
455
+ {
456
+ "epoch": 0.06939718672180552,
457
+ "grad_norm": 0.36660584937802804,
458
+ "learning_rate": 0.00018187500000000002,
459
+ "loss": 2.1179,
460
+ "step": 5800
461
+ },
462
+ {
463
+ "epoch": 0.070593689941147,
464
+ "grad_norm": 0.2683451066571477,
465
+ "learning_rate": 0.00017619318181818182,
466
+ "loss": 2.1201,
467
+ "step": 5900
468
+ },
469
+ {
470
+ "epoch": 0.07179019316048847,
471
+ "grad_norm": 0.1943497599849036,
472
+ "learning_rate": 0.00017051136363636362,
473
+ "loss": 2.1189,
474
+ "step": 6000
475
+ },
476
+ {
477
+ "epoch": 0.07179019316048847,
478
+ "eval_accuracy": 0.5445187284838173,
479
+ "eval_loss": 2.1193103790283203,
480
+ "eval_runtime": 8292.7883,
481
+ "eval_samples_per_second": 67.868,
482
+ "eval_steps_per_second": 8.484,
483
+ "step": 6000
484
+ },
485
+ {
486
+ "epoch": 0.07298669637982995,
487
+ "grad_norm": 0.2698036045598882,
488
+ "learning_rate": 0.00016482954545454545,
489
+ "loss": 2.1196,
490
+ "step": 6100
491
+ },
492
+ {
493
+ "epoch": 0.07418319959917143,
494
+ "grad_norm": 0.22553506284296373,
495
+ "learning_rate": 0.00015914772727272728,
496
+ "loss": 2.1195,
497
+ "step": 6200
498
+ },
499
+ {
500
+ "epoch": 0.07537970281851289,
501
+ "grad_norm": 0.21994704816511382,
502
+ "learning_rate": 0.00015346590909090911,
503
+ "loss": 2.119,
504
+ "step": 6300
505
+ },
506
+ {
507
+ "epoch": 0.07657620603785437,
508
+ "grad_norm": 0.20519656139775935,
509
+ "learning_rate": 0.00014778409090909092,
510
+ "loss": 2.1184,
511
+ "step": 6400
512
+ },
513
+ {
514
+ "epoch": 0.07777270925719584,
515
+ "grad_norm": 0.3246545352385696,
516
+ "learning_rate": 0.00014210227272727272,
517
+ "loss": 2.1181,
518
+ "step": 6500
519
+ },
520
+ {
521
+ "epoch": 0.07896921247653732,
522
+ "grad_norm": 0.25066878379158264,
523
+ "learning_rate": 0.00013642045454545455,
524
+ "loss": 2.1196,
525
+ "step": 6600
526
+ },
527
+ {
528
+ "epoch": 0.0801657156958788,
529
+ "grad_norm": 0.24038943677884456,
530
+ "learning_rate": 0.00013073863636363635,
531
+ "loss": 2.118,
532
+ "step": 6700
533
+ },
534
+ {
535
+ "epoch": 0.08136221891522027,
536
+ "grad_norm": 0.21920956719889126,
537
+ "learning_rate": 0.00012505681818181818,
538
+ "loss": 2.1188,
539
+ "step": 6800
540
+ },
541
+ {
542
+ "epoch": 0.08255872213456174,
543
+ "grad_norm": 0.2013303206012115,
544
+ "learning_rate": 0.000119375,
545
+ "loss": 2.1201,
546
+ "step": 6900
547
+ },
548
+ {
549
+ "epoch": 0.08375522535390321,
550
+ "grad_norm": 0.17865191614703033,
551
+ "learning_rate": 0.00011369318181818182,
552
+ "loss": 2.1184,
553
+ "step": 7000
554
+ },
555
+ {
556
+ "epoch": 0.08375522535390321,
557
+ "eval_accuracy": 0.5445597073721878,
558
+ "eval_loss": 2.119361400604248,
559
+ "eval_runtime": 8286.5111,
560
+ "eval_samples_per_second": 67.919,
561
+ "eval_steps_per_second": 8.49,
562
+ "step": 7000
563
+ },
564
+ {
565
+ "epoch": 0.08495172857324469,
566
+ "grad_norm": 0.2242220420058521,
567
+ "learning_rate": 0.00010801136363636365,
568
+ "loss": 2.1206,
569
+ "step": 7100
570
+ },
571
+ {
572
+ "epoch": 0.08614823179258617,
573
+ "grad_norm": 0.2361418777976826,
574
+ "learning_rate": 0.00010232954545454545,
575
+ "loss": 2.1223,
576
+ "step": 7200
577
+ },
578
+ {
579
+ "epoch": 0.08734473501192765,
580
+ "grad_norm": 0.2251269866404104,
581
+ "learning_rate": 9.664772727272728e-05,
582
+ "loss": 2.1188,
583
+ "step": 7300
584
+ },
585
+ {
586
+ "epoch": 0.08854123823126911,
587
+ "grad_norm": 0.2650506829716411,
588
+ "learning_rate": 9.09659090909091e-05,
589
+ "loss": 2.1204,
590
+ "step": 7400
591
+ },
592
+ {
593
+ "epoch": 0.08973774145061059,
594
+ "grad_norm": 0.2960065490838976,
595
+ "learning_rate": 8.52840909090909e-05,
596
+ "loss": 2.1166,
597
+ "step": 7500
598
+ },
599
+ {
600
+ "epoch": 0.09093424466995206,
601
+ "grad_norm": 0.2513130989479583,
602
+ "learning_rate": 7.960227272727273e-05,
603
+ "loss": 2.1191,
604
+ "step": 7600
605
+ },
606
+ {
607
+ "epoch": 0.09213074788929354,
608
+ "grad_norm": 0.23550646079468862,
609
+ "learning_rate": 7.392045454545454e-05,
610
+ "loss": 2.1207,
611
+ "step": 7700
612
+ },
613
+ {
614
+ "epoch": 0.09332725110863502,
615
+ "grad_norm": 0.20425354689020786,
616
+ "learning_rate": 6.823863636363637e-05,
617
+ "loss": 2.1196,
618
+ "step": 7800
619
+ },
620
+ {
621
+ "epoch": 0.0945237543279765,
622
+ "grad_norm": 0.27000259463730636,
623
+ "learning_rate": 6.255681818181818e-05,
624
+ "loss": 2.1208,
625
+ "step": 7900
626
+ },
627
+ {
628
+ "epoch": 0.09572025754731796,
629
+ "grad_norm": 0.23680396479288449,
630
+ "learning_rate": 5.6875e-05,
631
+ "loss": 2.119,
632
+ "step": 8000
633
+ },
634
+ {
635
+ "epoch": 0.09572025754731796,
636
+ "eval_accuracy": 0.54459064011865,
637
+ "eval_loss": 2.1189937591552734,
638
+ "eval_runtime": 8311.026,
639
+ "eval_samples_per_second": 67.719,
640
+ "eval_steps_per_second": 8.465,
641
+ "step": 8000
642
+ },
643
+ {
644
+ "epoch": 0.09691676076665943,
645
+ "grad_norm": 0.2356498019575465,
646
+ "learning_rate": 5.119318181818182e-05,
647
+ "loss": 2.117,
648
+ "step": 8100
649
+ },
650
+ {
651
+ "epoch": 0.09811326398600091,
652
+ "grad_norm": 0.2452139474957504,
653
+ "learning_rate": 4.5511363636363634e-05,
654
+ "loss": 2.1191,
655
+ "step": 8200
656
+ },
657
+ {
658
+ "epoch": 0.09930976720534239,
659
+ "grad_norm": 0.2570095681654715,
660
+ "learning_rate": 3.982954545454546e-05,
661
+ "loss": 2.1182,
662
+ "step": 8300
663
+ },
664
+ {
665
+ "epoch": 0.10050627042468387,
666
+ "grad_norm": 0.2928491115350356,
667
+ "learning_rate": 3.4147727272727274e-05,
668
+ "loss": 2.119,
669
+ "step": 8400
670
+ },
671
+ {
672
+ "epoch": 0.10170277364402533,
673
+ "grad_norm": 0.2534123898084123,
674
+ "learning_rate": 2.846590909090909e-05,
675
+ "loss": 2.1227,
676
+ "step": 8500
677
+ },
678
+ {
679
+ "epoch": 0.1028992768633668,
680
+ "grad_norm": 0.29376855634271687,
681
+ "learning_rate": 2.278409090909091e-05,
682
+ "loss": 2.1198,
683
+ "step": 8600
684
+ },
685
+ {
686
+ "epoch": 0.10409578008270828,
687
+ "grad_norm": 0.3837678078363553,
688
+ "learning_rate": 1.7102272727272728e-05,
689
+ "loss": 2.1206,
690
+ "step": 8700
691
+ },
692
+ {
693
+ "epoch": 0.10529228330204976,
694
+ "grad_norm": 0.24342629903402227,
695
+ "learning_rate": 1.1420454545454547e-05,
696
+ "loss": 2.12,
697
+ "step": 8800
698
+ },
699
+ {
700
+ "epoch": 0.10648878652139124,
701
+ "grad_norm": 0.18367698561178294,
702
+ "learning_rate": 5.7386363636363634e-06,
703
+ "loss": 2.1197,
704
+ "step": 8900
705
+ },
706
+ {
707
+ "epoch": 0.10768528974073271,
708
+ "grad_norm": 0.27943165556655064,
709
+ "learning_rate": 5.681818181818182e-08,
710
+ "loss": 2.1237,
711
+ "step": 9000
712
+ },
713
+ {
714
+ "epoch": 0.10768528974073271,
715
+ "eval_accuracy": 0.544624927857023,
716
+ "eval_loss": 2.12009596824646,
717
+ "eval_runtime": 8298.0507,
718
+ "eval_samples_per_second": 67.824,
719
+ "eval_steps_per_second": 8.478,
720
+ "step": 9000
721
+ },
722
+ {
723
+ "epoch": 0.10768528974073271,
724
+ "step": 9000,
725
+ "total_flos": 1158274171797504.0,
726
+ "train_loss": 2.120608181423611,
727
+ "train_runtime": 152416.4466,
728
+ "train_samples_per_second": 7.558,
729
+ "train_steps_per_second": 0.059
730
+ }
731
+ ],
732
+ "logging_steps": 100,
733
+ "max_steps": 9000,
734
+ "num_input_tokens_seen": 0,
735
+ "num_train_epochs": 1,
736
+ "save_steps": 450,
737
+ "stateful_callbacks": {
738
+ "TrainerControl": {
739
+ "args": {
740
+ "should_epoch_stop": false,
741
+ "should_evaluate": false,
742
+ "should_log": false,
743
+ "should_save": true,
744
+ "should_training_stop": true
745
+ },
746
+ "attributes": {}
747
+ }
748
+ },
749
+ "total_flos": 1158274171797504.0,
750
+ "train_batch_size": 8,
751
+ "trial_name": null,
752
+ "trial_params": null
753
+ }
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cef530fb4e1d2e9334e368a9556d9eb4c37e254304636d9458c4b52855ca4d03
3
+ size 6459
zero_to_fp32.py ADDED
@@ -0,0 +1,592 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright (c) Microsoft Corporation.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ # DeepSpeed Team
7
+
8
+ # This script extracts fp32 consolidated weights from a zero 1, 2 and 3 DeepSpeed checkpoints. It gets
9
+ # copied into the top level checkpoint dir, so the user can easily do the conversion at any point in
10
+ # the future. Once extracted, the weights don't require DeepSpeed and can be used in any
11
+ # application.
12
+ #
13
+ # example: python zero_to_fp32.py . pytorch_model.bin
14
+
15
+ import argparse
16
+ import torch
17
+ import glob
18
+ import math
19
+ import os
20
+ import re
21
+ from collections import OrderedDict
22
+ from dataclasses import dataclass
23
+
24
+ # while this script doesn't use deepspeed to recover data, since the checkpoints are pickled with
25
+ # DeepSpeed data structures it has to be available in the current python environment.
26
+ from deepspeed.utils import logger
27
+ from deepspeed.checkpoint.constants import (DS_VERSION, OPTIMIZER_STATE_DICT, SINGLE_PARTITION_OF_FP32_GROUPS,
28
+ FP32_FLAT_GROUPS, ZERO_STAGE, PARTITION_COUNT, PARAM_SHAPES, BUFFER_NAMES,
29
+ FROZEN_PARAM_SHAPES, FROZEN_PARAM_FRAGMENTS)
30
+
31
+
32
+ @dataclass
33
+ class zero_model_state:
34
+ buffers: dict()
35
+ param_shapes: dict()
36
+ shared_params: list
37
+ ds_version: int
38
+ frozen_param_shapes: dict()
39
+ frozen_param_fragments: dict()
40
+
41
+
42
+ debug = 0
43
+
44
+ # load to cpu
45
+ device = torch.device('cpu')
46
+
47
+
48
+ def atoi(text):
49
+ return int(text) if text.isdigit() else text
50
+
51
+
52
+ def natural_keys(text):
53
+ '''
54
+ alist.sort(key=natural_keys) sorts in human order
55
+ http://nedbatchelder.com/blog/200712/human_sorting.html
56
+ (See Toothy's implementation in the comments)
57
+ '''
58
+ return [atoi(c) for c in re.split(r'(\d+)', text)]
59
+
60
+
61
+ def get_model_state_file(checkpoint_dir, zero_stage):
62
+ if not os.path.isdir(checkpoint_dir):
63
+ raise FileNotFoundError(f"Directory '{checkpoint_dir}' doesn't exist")
64
+
65
+ # there should be only one file
66
+ if zero_stage <= 2:
67
+ file = os.path.join(checkpoint_dir, "mp_rank_00_model_states.pt")
68
+ elif zero_stage == 3:
69
+ file = os.path.join(checkpoint_dir, "zero_pp_rank_0_mp_rank_00_model_states.pt")
70
+
71
+ if not os.path.exists(file):
72
+ raise FileNotFoundError(f"can't find model states file at '{file}'")
73
+
74
+ return file
75
+
76
+
77
+ def get_checkpoint_files(checkpoint_dir, glob_pattern):
78
+ # XXX: need to test that this simple glob rule works for multi-node setup too
79
+ ckpt_files = sorted(glob.glob(os.path.join(checkpoint_dir, glob_pattern)), key=natural_keys)
80
+
81
+ if len(ckpt_files) == 0:
82
+ raise FileNotFoundError(f"can't find {glob_pattern} files in directory '{checkpoint_dir}'")
83
+
84
+ return ckpt_files
85
+
86
+
87
+ def get_optim_files(checkpoint_dir):
88
+ return get_checkpoint_files(checkpoint_dir, "*_optim_states.pt")
89
+
90
+
91
+ def get_model_state_files(checkpoint_dir):
92
+ return get_checkpoint_files(checkpoint_dir, "*_model_states.pt")
93
+
94
+
95
+ def parse_model_states(files):
96
+ zero_model_states = []
97
+ for file in files:
98
+ state_dict = torch.load(file, map_location=device)
99
+
100
+ if BUFFER_NAMES not in state_dict:
101
+ raise ValueError(f"{file} is not a model state checkpoint")
102
+ buffer_names = state_dict[BUFFER_NAMES]
103
+ if debug:
104
+ print("Found buffers:", buffer_names)
105
+
106
+ # recover just the buffers while restoring them to fp32 if they were saved in fp16
107
+ buffers = {k: v.float() for k, v in state_dict["module"].items() if k in buffer_names}
108
+ param_shapes = state_dict[PARAM_SHAPES]
109
+
110
+ # collect parameters that are included in param_shapes
111
+ param_names = []
112
+ for s in param_shapes:
113
+ for name in s.keys():
114
+ param_names.append(name)
115
+
116
+ # update with frozen parameters
117
+ frozen_param_shapes = state_dict.get(FROZEN_PARAM_SHAPES, None)
118
+ if frozen_param_shapes is not None:
119
+ if debug:
120
+ print(f"Found frozen_param_shapes: {frozen_param_shapes}")
121
+ param_names += list(frozen_param_shapes.keys())
122
+
123
+ # handle shared params
124
+ shared_params = [[k, v] for k, v in state_dict["shared_params"].items()]
125
+
126
+ ds_version = state_dict.get(DS_VERSION, None)
127
+
128
+ frozen_param_fragments = state_dict.get(FROZEN_PARAM_FRAGMENTS, None)
129
+
130
+ z_model_state = zero_model_state(buffers=buffers,
131
+ param_shapes=param_shapes,
132
+ shared_params=shared_params,
133
+ ds_version=ds_version,
134
+ frozen_param_shapes=frozen_param_shapes,
135
+ frozen_param_fragments=frozen_param_fragments)
136
+ zero_model_states.append(z_model_state)
137
+
138
+ return zero_model_states
139
+
140
+
141
+ def parse_optim_states(files, ds_checkpoint_dir):
142
+
143
+ total_files = len(files)
144
+ state_dicts = []
145
+ for f in files:
146
+ state_dict = torch.load(f, map_location=device)
147
+ # immediately discard the potentially huge 2 optimizer states as we only care for fp32 master weights
148
+ # and also handle the case where it was already removed by another helper script
149
+ state_dict["optimizer_state_dict"].pop("optimizer_state_dict", None)
150
+ state_dicts.append(state_dict)
151
+
152
+ if not ZERO_STAGE in state_dicts[0][OPTIMIZER_STATE_DICT]:
153
+ raise ValueError(f"{files[0]} is not a zero checkpoint")
154
+ zero_stage = state_dicts[0][OPTIMIZER_STATE_DICT][ZERO_STAGE]
155
+ world_size = state_dicts[0][OPTIMIZER_STATE_DICT][PARTITION_COUNT]
156
+
157
+ # For ZeRO-2 each param group can have different partition_count as data parallelism for expert
158
+ # parameters can be different from data parallelism for non-expert parameters. So we can just
159
+ # use the max of the partition_count to get the dp world_size.
160
+
161
+ if type(world_size) is list:
162
+ world_size = max(world_size)
163
+
164
+ if world_size != total_files:
165
+ raise ValueError(
166
+ f"Expected {world_size} of '*_optim_states.pt' under '{ds_checkpoint_dir}' but found {total_files} files. "
167
+ "Possibly due to an overwrite of an old checkpoint, or a checkpoint didn't get saved by one or more processes."
168
+ )
169
+
170
+ # the groups are named differently in each stage
171
+ if zero_stage <= 2:
172
+ fp32_groups_key = SINGLE_PARTITION_OF_FP32_GROUPS
173
+ elif zero_stage == 3:
174
+ fp32_groups_key = FP32_FLAT_GROUPS
175
+ else:
176
+ raise ValueError(f"unknown zero stage {zero_stage}")
177
+
178
+ if zero_stage <= 2:
179
+ fp32_flat_groups = [state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key] for i in range(len(state_dicts))]
180
+ elif zero_stage == 3:
181
+ # if there is more than one param group, there will be multiple flattened tensors - one
182
+ # flattened tensor per group - for simplicity merge them into a single tensor
183
+ #
184
+ # XXX: could make the script more memory efficient for when there are multiple groups - it
185
+ # will require matching the sub-lists of param_shapes for each param group flattened tensor
186
+
187
+ fp32_flat_groups = [
188
+ torch.cat(state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key], 0) for i in range(len(state_dicts))
189
+ ]
190
+
191
+ return zero_stage, world_size, fp32_flat_groups
192
+
193
+
194
+ def _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir):
195
+ """
196
+ Returns fp32 state_dict reconstructed from ds checkpoint
197
+
198
+ Args:
199
+ - ``ds_checkpoint_dir``: path to the deepspeed checkpoint folder (where the optimizer files are)
200
+
201
+ """
202
+ print(f"Processing zero checkpoint '{ds_checkpoint_dir}'")
203
+
204
+ optim_files = get_optim_files(ds_checkpoint_dir)
205
+ zero_stage, world_size, fp32_flat_groups = parse_optim_states(optim_files, ds_checkpoint_dir)
206
+ print(f"Detected checkpoint of type zero stage {zero_stage}, world_size: {world_size}")
207
+
208
+ model_files = get_model_state_files(ds_checkpoint_dir)
209
+
210
+ zero_model_states = parse_model_states(model_files)
211
+ print(f'Parsing checkpoint created by deepspeed=={zero_model_states[0].ds_version}')
212
+
213
+ if zero_stage <= 2:
214
+ return _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states)
215
+ elif zero_stage == 3:
216
+ return _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states)
217
+
218
+
219
+ def _zero2_merge_frozen_params(state_dict, zero_model_states):
220
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
221
+ return
222
+
223
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
224
+ frozen_param_fragments = zero_model_states[0].frozen_param_fragments
225
+
226
+ if debug:
227
+ num_elem = sum(s.numel() for s in frozen_param_shapes.values())
228
+ print(f'rank 0: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
229
+
230
+ wanted_params = len(frozen_param_shapes)
231
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
232
+ avail_numel = sum([p.numel() for p in frozen_param_fragments.values()])
233
+ print(f'Frozen params: Have {avail_numel} numels to process.')
234
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
235
+
236
+ total_params = 0
237
+ total_numel = 0
238
+ for name, shape in frozen_param_shapes.items():
239
+ total_params += 1
240
+ unpartitioned_numel = shape.numel()
241
+ total_numel += unpartitioned_numel
242
+
243
+ state_dict[name] = frozen_param_fragments[name]
244
+
245
+ if debug:
246
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
247
+
248
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
249
+
250
+
251
+ def _has_callable(obj, fn):
252
+ attr = getattr(obj, fn, None)
253
+ return callable(attr)
254
+
255
+
256
+ def _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
257
+ param_shapes = zero_model_states[0].param_shapes
258
+
259
+ # Reconstruction protocol:
260
+ #
261
+ # XXX: document this
262
+
263
+ if debug:
264
+ for i in range(world_size):
265
+ for j in range(len(fp32_flat_groups[0])):
266
+ print(f"{FP32_FLAT_GROUPS}[{i}][{j}].shape={fp32_flat_groups[i][j].shape}")
267
+
268
+ # XXX: memory usage doubles here (zero2)
269
+ num_param_groups = len(fp32_flat_groups[0])
270
+ merged_single_partition_of_fp32_groups = []
271
+ for i in range(num_param_groups):
272
+ merged_partitions = [sd[i] for sd in fp32_flat_groups]
273
+ full_single_fp32_vector = torch.cat(merged_partitions, 0)
274
+ merged_single_partition_of_fp32_groups.append(full_single_fp32_vector)
275
+ avail_numel = sum(
276
+ [full_single_fp32_vector.numel() for full_single_fp32_vector in merged_single_partition_of_fp32_groups])
277
+
278
+ if debug:
279
+ wanted_params = sum([len(shapes) for shapes in param_shapes])
280
+ wanted_numel = sum([sum(shape.numel() for shape in shapes.values()) for shapes in param_shapes])
281
+ # not asserting if there is a mismatch due to possible padding
282
+ print(f"Have {avail_numel} numels to process.")
283
+ print(f"Need {wanted_numel} numels in {wanted_params} params.")
284
+
285
+ # params
286
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
287
+ # out-of-core computing solution
288
+ total_numel = 0
289
+ total_params = 0
290
+ for shapes, full_single_fp32_vector in zip(param_shapes, merged_single_partition_of_fp32_groups):
291
+ offset = 0
292
+ avail_numel = full_single_fp32_vector.numel()
293
+ for name, shape in shapes.items():
294
+
295
+ unpartitioned_numel = shape.numel() if _has_callable(shape, 'numel') else math.prod(shape)
296
+ total_numel += unpartitioned_numel
297
+ total_params += 1
298
+
299
+ if debug:
300
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
301
+ state_dict[name] = full_single_fp32_vector.narrow(0, offset, unpartitioned_numel).view(shape)
302
+ offset += unpartitioned_numel
303
+
304
+ # Z2 started to align to 2*world_size to improve nccl performance. Therefore both offset and
305
+ # avail_numel can differ by anywhere between 0..2*world_size. Due to two unrelated complex
306
+ # paddings performed in the code it's almost impossible to predict the exact numbers w/o the
307
+ # live optimizer object, so we are checking that the numbers are within the right range
308
+ align_to = 2 * world_size
309
+
310
+ def zero2_align(x):
311
+ return align_to * math.ceil(x / align_to)
312
+
313
+ if debug:
314
+ print(f"original offset={offset}, avail_numel={avail_numel}")
315
+
316
+ offset = zero2_align(offset)
317
+ avail_numel = zero2_align(avail_numel)
318
+
319
+ if debug:
320
+ print(f"aligned offset={offset}, avail_numel={avail_numel}")
321
+
322
+ # Sanity check
323
+ if offset != avail_numel:
324
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
325
+
326
+ print(f"Reconstructed fp32 state dict with {total_params} params {total_numel} elements")
327
+
328
+
329
+ def _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states):
330
+ state_dict = OrderedDict()
331
+
332
+ # buffers
333
+ buffers = zero_model_states[0].buffers
334
+ state_dict.update(buffers)
335
+ if debug:
336
+ print(f"added {len(buffers)} buffers")
337
+
338
+ _zero2_merge_frozen_params(state_dict, zero_model_states)
339
+
340
+ _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
341
+
342
+ # recover shared parameters
343
+ for pair in zero_model_states[0].shared_params:
344
+ if pair[1] in state_dict:
345
+ state_dict[pair[0]] = state_dict[pair[1]]
346
+
347
+ return state_dict
348
+
349
+
350
+ def zero3_partitioned_param_info(unpartitioned_numel, world_size):
351
+ remainder = unpartitioned_numel % world_size
352
+ padding_numel = (world_size - remainder) if remainder else 0
353
+ partitioned_numel = math.ceil(unpartitioned_numel / world_size)
354
+ return partitioned_numel, padding_numel
355
+
356
+
357
+ def _zero3_merge_frozen_params(state_dict, world_size, zero_model_states):
358
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
359
+ return
360
+
361
+ if debug:
362
+ for i in range(world_size):
363
+ num_elem = sum(s.numel() for s in zero_model_states[i].frozen_param_fragments.values())
364
+ print(f'rank {i}: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
365
+
366
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
367
+ wanted_params = len(frozen_param_shapes)
368
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
369
+ avail_numel = sum([p.numel() for p in zero_model_states[0].frozen_param_fragments.values()]) * world_size
370
+ print(f'Frozen params: Have {avail_numel} numels to process.')
371
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
372
+
373
+ total_params = 0
374
+ total_numel = 0
375
+ for name, shape in zero_model_states[0].frozen_param_shapes.items():
376
+ total_params += 1
377
+ unpartitioned_numel = shape.numel()
378
+ total_numel += unpartitioned_numel
379
+
380
+ param_frags = tuple(model_state.frozen_param_fragments[name] for model_state in zero_model_states)
381
+ state_dict[name] = torch.cat(param_frags, 0).narrow(0, 0, unpartitioned_numel).view(shape)
382
+
383
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
384
+
385
+ if debug:
386
+ print(
387
+ f"Frozen params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
388
+ )
389
+
390
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
391
+
392
+
393
+ def _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
394
+ param_shapes = zero_model_states[0].param_shapes
395
+ avail_numel = fp32_flat_groups[0].numel() * world_size
396
+ # Reconstruction protocol: For zero3 we need to zip the partitions together at boundary of each
397
+ # param, re-consolidating each param, while dealing with padding if any
398
+
399
+ # merge list of dicts, preserving order
400
+ param_shapes = {k: v for d in param_shapes for k, v in d.items()}
401
+
402
+ if debug:
403
+ for i in range(world_size):
404
+ print(f"{FP32_FLAT_GROUPS}[{i}].shape={fp32_flat_groups[i].shape}")
405
+
406
+ wanted_params = len(param_shapes)
407
+ wanted_numel = sum(shape.numel() for shape in param_shapes.values())
408
+ # not asserting if there is a mismatch due to possible padding
409
+ avail_numel = fp32_flat_groups[0].numel() * world_size
410
+ print(f"Trainable params: Have {avail_numel} numels to process.")
411
+ print(f"Trainable params: Need {wanted_numel} numels in {wanted_params} params.")
412
+
413
+ # params
414
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
415
+ # out-of-core computing solution
416
+ offset = 0
417
+ total_numel = 0
418
+ total_params = 0
419
+ for name, shape in param_shapes.items():
420
+
421
+ unpartitioned_numel = shape.numel()
422
+ total_numel += unpartitioned_numel
423
+ total_params += 1
424
+
425
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
426
+
427
+ if debug:
428
+ print(
429
+ f"Trainable params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
430
+ )
431
+
432
+ # XXX: memory usage doubles here
433
+ state_dict[name] = torch.cat(
434
+ tuple(fp32_flat_groups[i].narrow(0, offset, partitioned_numel) for i in range(world_size)),
435
+ 0).narrow(0, 0, unpartitioned_numel).view(shape)
436
+ offset += partitioned_numel
437
+
438
+ offset *= world_size
439
+
440
+ # Sanity check
441
+ if offset != avail_numel:
442
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
443
+
444
+ print(f"Reconstructed Trainable fp32 state dict with {total_params} params {total_numel} elements")
445
+
446
+
447
+ def _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states):
448
+ state_dict = OrderedDict()
449
+
450
+ # buffers
451
+ buffers = zero_model_states[0].buffers
452
+ state_dict.update(buffers)
453
+ if debug:
454
+ print(f"added {len(buffers)} buffers")
455
+
456
+ _zero3_merge_frozen_params(state_dict, world_size, zero_model_states)
457
+
458
+ _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
459
+
460
+ # recover shared parameters
461
+ for pair in zero_model_states[0].shared_params:
462
+ if pair[1] in state_dict:
463
+ state_dict[pair[0]] = state_dict[pair[1]]
464
+
465
+ return state_dict
466
+
467
+
468
+ def get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag=None):
469
+ """
470
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated state_dict that can be loaded with
471
+ ``load_state_dict()`` and used for training without DeepSpeed or shared with others, for example
472
+ via a model hub.
473
+
474
+ Args:
475
+ - ``checkpoint_dir``: path to the desired checkpoint folder
476
+ - ``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``
477
+
478
+ Returns:
479
+ - pytorch ``state_dict``
480
+
481
+ Note: this approach may not work if your application doesn't have sufficient free CPU memory and
482
+ you may need to use the offline approach using the ``zero_to_fp32.py`` script that is saved with
483
+ the checkpoint.
484
+
485
+ A typical usage might be ::
486
+
487
+ from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
488
+ # do the training and checkpoint saving
489
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu
490
+ model = model.cpu() # move to cpu
491
+ model.load_state_dict(state_dict)
492
+ # submit to model hub or save the model to share with others
493
+
494
+ In this example the ``model`` will no longer be usable in the deepspeed context of the same
495
+ application. i.e. you will need to re-initialize the deepspeed engine, since
496
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
497
+
498
+ If you want it all done for you, use ``load_state_dict_from_zero_checkpoint`` instead.
499
+
500
+ """
501
+ if tag is None:
502
+ latest_path = os.path.join(checkpoint_dir, 'latest')
503
+ if os.path.isfile(latest_path):
504
+ with open(latest_path, 'r') as fd:
505
+ tag = fd.read().strip()
506
+ else:
507
+ raise ValueError(f"Unable to find 'latest' file at {latest_path}")
508
+
509
+ ds_checkpoint_dir = os.path.join(checkpoint_dir, tag)
510
+
511
+ if not os.path.isdir(ds_checkpoint_dir):
512
+ raise FileNotFoundError(f"Directory '{ds_checkpoint_dir}' doesn't exist")
513
+
514
+ return _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir)
515
+
516
+
517
+ def convert_zero_checkpoint_to_fp32_state_dict(checkpoint_dir, output_file, tag=None):
518
+ """
519
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict`` file that can be
520
+ loaded with ``torch.load(file)`` + ``load_state_dict()`` and used for training without DeepSpeed.
521
+
522
+ Args:
523
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
524
+ - ``output_file``: path to the pytorch fp32 state_dict output file (e.g. path/pytorch_model.bin)
525
+ - ``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``
526
+ """
527
+
528
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
529
+ print(f"Saving fp32 state dict to {output_file}")
530
+ torch.save(state_dict, output_file)
531
+
532
+
533
+ def load_state_dict_from_zero_checkpoint(model, checkpoint_dir, tag=None):
534
+ """
535
+ 1. Put the provided model to cpu
536
+ 2. Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict``
537
+ 3. Load it into the provided model
538
+
539
+ Args:
540
+ - ``model``: the model object to update
541
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
542
+ - ``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``
543
+
544
+ Returns:
545
+ - ``model`: modified model
546
+
547
+ Make sure you have plenty of CPU memory available before you call this function. If you don't
548
+ have enough use the ``zero_to_fp32.py`` utility to do the conversion. You will find it
549
+ conveniently placed for you in the checkpoint folder.
550
+
551
+ A typical usage might be ::
552
+
553
+ from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint
554
+ model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir)
555
+ # submit to model hub or save the model to share with others
556
+
557
+ Note, that once this was run, the ``model`` will no longer be usable in the deepspeed context
558
+ of the same application. i.e. you will need to re-initialize the deepspeed engine, since
559
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
560
+
561
+ """
562
+ logger.info(f"Extracting fp32 weights")
563
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
564
+
565
+ logger.info(f"Overwriting model with fp32 weights")
566
+ model = model.cpu()
567
+ model.load_state_dict(state_dict, strict=False)
568
+
569
+ return model
570
+
571
+
572
+ if __name__ == "__main__":
573
+
574
+ parser = argparse.ArgumentParser()
575
+ parser.add_argument("checkpoint_dir",
576
+ type=str,
577
+ help="path to the desired checkpoint folder, e.g., path/checkpoint-12")
578
+ parser.add_argument(
579
+ "output_file",
580
+ type=str,
581
+ help="path to the pytorch fp32 state_dict output file (e.g. path/checkpoint-12/pytorch_model.bin)")
582
+ parser.add_argument("-t",
583
+ "--tag",
584
+ type=str,
585
+ default=None,
586
+ help="checkpoint tag used as a unique identifier for checkpoint. e.g., global_step1")
587
+ parser.add_argument("-d", "--debug", action='store_true', help="enable debug")
588
+ args = parser.parse_args()
589
+
590
+ debug = args.debug
591
+
592
+ convert_zero_checkpoint_to_fp32_state_dict(args.checkpoint_dir, args.output_file, tag=args.tag)