|
| 1 | +# Copyright 2024 The Flax Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Default Hyperparameter configuration.""" |
| 16 | + |
| 17 | +import dataclasses |
| 18 | + |
| 19 | +from train import MeshRules, TrainConfig |
| 20 | + |
| 21 | + |
| 22 | +@dataclasses.dataclass(unsafe_hash=True) |
| 23 | +class Config: |
| 24 | + # Path to load or store sentencepiece vocab file. |
| 25 | + vocab_path: str | None = None |
| 26 | + # Vocabulary size if `vocab_path` is not given. |
| 27 | + vocab_size: int = 35_000 # lm1b dataset vocab size: 35913 (Gemma expected vocab size: 262_144) |
| 28 | + # Maximum number of characters to use for training. |
| 29 | + max_corpus_chars: int = 10**7 |
| 30 | + # Name of TFDS translation dataset to use. |
| 31 | + dataset_name: str = 'lm1b' |
| 32 | + # Optional name of TFDS translation dataset to use for evaluation. |
| 33 | + eval_dataset_name: str = 'lm1b' |
| 34 | + # Optional name of TFDS split to use for evaluation. |
| 35 | + eval_split: str = 'test' |
| 36 | + # Per device batch size for training. |
| 37 | + per_device_batch_size: int = 32 |
| 38 | + # Per device batch size for training. |
| 39 | + eval_per_device_batch_size: int = 32 |
| 40 | + |
| 41 | + # Prompt for language model sampling |
| 42 | + prompts: tuple[str, ...] = ( |
| 43 | + 'Paris is a the capital', |
| 44 | + 'Flax is a', |
| 45 | + # From train set: |
| 46 | + 'The shutdown was aimed at creating efficiencies as', |
| 47 | + # -> the plant was already operating at its maximum capacity of 3,000 tonnes of cellulose paste per day |
| 48 | + 'A big theme of this hire is that there are parts of', |
| 49 | + # -> our operations that to use a pretty trite phrase , need to be taken to the next level ... |
| 50 | + |
| 51 | + # From test set: |
| 52 | + 'Because of Bear Stearns , many analysts are', |
| 53 | + # -> raising the odds that a 2008 recession could be worse than expected |
| 54 | + 'Next month , the Brazilian bourse', |
| 55 | + # -> opens a London office', |
| 56 | + ) |
| 57 | + # Temperature for top_p sampling. |
| 58 | + sampling_temperature: float = 0.0 |
| 59 | + # Top-p sampling threshold. |
| 60 | + sampling_top_p: float = 0.95 |
| 61 | + |
| 62 | + # Number of steps to take during training. |
| 63 | + num_train_steps: int = 500_000 |
| 64 | + # Number of steps to take during evaluation. |
| 65 | + # Large enough to evaluate all samples: 306_688 / (32 * 8) = 1198 |
| 66 | + num_eval_steps: int = 2_000 |
| 67 | + # Number of steps to generate predictions. |
| 68 | + # -1 will use the whole eval dataset. |
| 69 | + num_predict_steps: int = 50 |
| 70 | + # Base learning rate. |
| 71 | + learning_rate: float = 0.0016 |
| 72 | + # Linear learning rate warmup. |
| 73 | + warmup_steps: int = 1000 |
| 74 | + # Cross entropy loss label smoothing. |
| 75 | + label_smoothing: float = 0.0 |
| 76 | + # Decay factor for AdamW style weight decay. |
| 77 | + weight_decay: float = 0.1 |
| 78 | + # Maximum length cutoff for training examples. |
| 79 | + max_target_length: int = 128 |
| 80 | + # Maximum length cutoff for eval examples. |
| 81 | + max_eval_target_length: int = 512 |
| 82 | + |
| 83 | + # Gemma transformer name. |
| 84 | + # Possible values defined in transformer.TransformerConfig: |
| 85 | + # (gemma_2b, gemma_7b, gemma2_2b, gemma2_9b, gemma2_27b, gemma3_1b, gemma3_4b, ...) |
| 86 | + transformer_name: str | None = "gemma3_1b" |
| 87 | + # or alternatively define the model using the dict of parameters |
| 88 | + transformer_params: dict | None = None |
| 89 | + |
| 90 | + # Whether to save model checkpoints. |
| 91 | + save_checkpoints: bool = True |
| 92 | + # Whether to restore from existing model checkpoints. |
| 93 | + restore_checkpoints: bool = True |
| 94 | + # Save a checkpoint every these number of steps. |
| 95 | + checkpoint_every_steps: int = 10_000 |
| 96 | + # Frequency of eval during training, e.g. every 1_000 steps. |
| 97 | + eval_every_steps: int = 5_000 |
| 98 | + # Use bfloat16 mixed precision training instead of float32. |
| 99 | + use_bfloat16: bool = True |
| 100 | + # Integer for PRNG random seed. |
| 101 | + seed: int = 0 |
| 102 | + |
| 103 | + # Parallelism |
| 104 | + mesh_axes: tuple[str, ...] = ('data', 'fsdp', 'tensor') |
| 105 | + axis_rules: MeshRules = MeshRules( |
| 106 | + embed='fsdp', |
| 107 | + mlp='tensor', |
| 108 | + kv='tensor', |
| 109 | + vocab='tensor', |
| 110 | + ) |
| 111 | + data_sharding: tuple[str, ...] = ('data', 'fsdp') |
| 112 | + |
| 113 | + # One axis for each parallelism type may hold a placeholder (-1) |
| 114 | + # value to auto-shard based on available slices and devices. |
| 115 | + # By default, product of the DCN axes should equal number of slices |
| 116 | + # and product of the ICI axes should equal number of devices per slice. |
| 117 | + # ICI (Inter-Chip Interconnection): A high-speed connection between |
| 118 | + # sets of TPU chips, which form the TPU network. |
| 119 | + # DCN (Data Center Network): A connection between the TPU networks; |
| 120 | + # not as fast as ICI. |
| 121 | + # ICI has around 100x the bandwidth of DCN, but it is not a general |
| 122 | + # purpose connection, which is why DCN is necessary for scaling to |
| 123 | + # extremely large ML models. |
| 124 | + dcn_data_parallelism: int = -1 |
| 125 | + dcn_fsdp_parallelism: int = 1 |
| 126 | + dcn_tensor_parallelism: int = 1 |
| 127 | + ici_data_parallelism: int = 1 |
| 128 | + ici_fsdp_parallelism: int = -1 |
| 129 | + ici_tensor_parallelism: int = 1 |
| 130 | + |
| 131 | + |
| 132 | +def get_config() -> TrainConfig: |
| 133 | + """Get the default hyperparameter configuration.""" |
| 134 | + config = Config() |
| 135 | + return TrainConfig(**dataclasses.asdict(config)) |
0 commit comments