|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +from simple_parsing import ArgumentParser |
| 6 | +from simple_parsing.helpers import subgroups |
| 7 | +from simple_parsing.helpers.partial import Partial, config_for |
| 8 | + |
| 9 | + |
| 10 | +# Suppose we want to choose between the Adam and SGD optimizers from PyTorch: |
| 11 | +# (NOTE: We don't import pytorch here, so we just create the types to illustrate) |
| 12 | +class Optimizer: |
| 13 | + def __init__(self, params): |
| 14 | + ... |
| 15 | + |
| 16 | + |
| 17 | +class Adam(Optimizer): |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + params, |
| 21 | + lr: float = 3e-4, |
| 22 | + beta1: float = 0.9, |
| 23 | + beta2: float = 0.999, |
| 24 | + eps: float = 1e-08, |
| 25 | + ): |
| 26 | + self.params = params |
| 27 | + self.lr = lr |
| 28 | + self.beta1 = beta1 |
| 29 | + self.beta2 = beta2 |
| 30 | + self.eps = eps |
| 31 | + |
| 32 | + |
| 33 | +class SGD(Optimizer): |
| 34 | + def __init__( |
| 35 | + self, |
| 36 | + params, |
| 37 | + lr: float = 3e-4, |
| 38 | + weight_decay: float | None = None, |
| 39 | + momentum: float = 0.9, |
| 40 | + eps: float = 1e-08, |
| 41 | + ): |
| 42 | + self.params = params |
| 43 | + self.lr = lr |
| 44 | + self.weight_decay = weight_decay |
| 45 | + self.momentum = momentum |
| 46 | + self.eps = eps |
| 47 | + |
| 48 | + |
| 49 | +# Dynamically create a dataclass that will be used for the above type: |
| 50 | +# NOTE: We could use Partial[Adam] or Partial[Optimizer], however this would treat `params` as a |
| 51 | +# required argument. |
| 52 | +# AdamConfig = Partial[Adam] # would treat 'params' as a required argument. |
| 53 | +# SGDConfig = Partial[SGD] # same here |
| 54 | +AdamConfig: type[Partial[Adam]] = config_for(Adam, ignore_args="params") |
| 55 | +SGDConfig: type[Partial[SGD]] = config_for(SGD, ignore_args="params") |
| 56 | + |
| 57 | + |
| 58 | +@dataclass |
| 59 | +class Config: |
| 60 | + |
| 61 | + # Which optimizer to use. |
| 62 | + optimizer: Partial[Optimizer] = subgroups( |
| 63 | + { |
| 64 | + "sgd": SGDConfig, |
| 65 | + "adam": AdamConfig, |
| 66 | + }, |
| 67 | + default_factory=AdamConfig, |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +parser = ArgumentParser() |
| 72 | +parser.add_arguments(Config, "config") |
| 73 | +args = parser.parse_args() |
| 74 | + |
| 75 | + |
| 76 | +config: Config = args.config |
| 77 | +print(config) |
| 78 | +expected = "Config(optimizer=AdamConfig(lr=0.0003, beta1=0.9, beta2=0.999, eps=1e-08))" |
| 79 | + |
| 80 | +my_model_parameters = [123] # nn.Sequential(...).parameters() |
| 81 | + |
| 82 | +optimizer = config.optimizer(params=my_model_parameters) |
| 83 | +print(vars(optimizer)) |
| 84 | +expected += """ |
| 85 | +{'params': [123], 'lr': 0.0003, 'beta1': 0.9, 'beta2': 0.999, 'eps': 1e-08} |
| 86 | +""" |
0 commit comments