|
| 1 | +from pydantic import Field |
| 2 | +from pydantic_settings import ( |
| 3 | + BaseSettings, |
| 4 | + PydanticBaseSettingsSource, |
| 5 | + SettingsConfigDict, |
| 6 | + YamlConfigSettingsSource, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class ScratchGPTArchitecture(BaseSettings): |
| 11 | + """ |
| 12 | + All settings for training the model. |
| 13 | + """ |
| 14 | + |
| 15 | + block_size: int = 256 |
| 16 | + embedding_size: int = 384 |
| 17 | + """ Size of the individual embeddings vector """ |
| 18 | + num_heads: int = 6 |
| 19 | + num_blocks: int = 6 |
| 20 | + vocab_size: int | None = None |
| 21 | + |
| 22 | + model_config = SettingsConfigDict( |
| 23 | + env_prefix="ARCHITECTURE_", |
| 24 | + extra="allow", |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +class ScratchGPTTraining(BaseSettings): |
| 29 | + """ |
| 30 | + All training related parameters |
| 31 | + """ |
| 32 | + |
| 33 | + max_epochs: int = 50 |
| 34 | + learning_rate: float = 3e-4 |
| 35 | + batch_size: int = 32 |
| 36 | + dropout_rate: float = 0.2 |
| 37 | + random_seed: int = 1337 |
| 38 | + |
| 39 | + model_config = SettingsConfigDict( |
| 40 | + env_prefix="TRAINING_", |
| 41 | + extra="allow", |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class ScratchGPTConfig(BaseSettings): |
| 46 | + """ |
| 47 | + Full model config |
| 48 | + """ |
| 49 | + |
| 50 | + architecture: ScratchGPTArchitecture = Field(default_factory=ScratchGPTArchitecture) |
| 51 | + training: ScratchGPTTraining = Field(default_factory=ScratchGPTTraining) |
| 52 | + |
| 53 | + model_config = SettingsConfigDict( |
| 54 | + env_prefix="SCRATCH_GPT_", |
| 55 | + extra="allow", |
| 56 | + ) |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def settings_customise_sources( |
| 60 | + cls, |
| 61 | + settings_cls: type[BaseSettings], |
| 62 | + init_settings: PydanticBaseSettingsSource, |
| 63 | + env_settings: PydanticBaseSettingsSource, |
| 64 | + dotenv_settings: PydanticBaseSettingsSource, |
| 65 | + file_secret_settings: PydanticBaseSettingsSource, |
| 66 | + ) -> tuple[PydanticBaseSettingsSource, ...]: |
| 67 | + return ( |
| 68 | + env_settings, |
| 69 | + init_settings, |
| 70 | + file_secret_settings, |
| 71 | + YamlConfigSettingsSource(settings_cls, yaml_file="scratch_gpt.yaml"), |
| 72 | + ) |
0 commit comments