from dataclasses import dataclass
from typing import List, Sequence
import nemo_run as run
@dataclass
class Model:
"""Dummy model config"""
hidden_size: int
num_layers: int
activation: str
@dataclass
class Optimizer:
"""Dummy optimizer config"""
learning_rate: float
weight_decay: float
betas: List[float]
@run.cli.factory
@run.autoconvert
def my_model(
hidden_size: int = 256,
num_layers: int = 3,
activation: str = 'relu'
) -> Model:
"""Create a model configuration."""
return Model(hidden_size=hidden_size, num_layers=num_layers, activation=activation)
@run.cli.factory
@run.autoconvert
def my_model_2(
hidden_size: int = 512,
num_layers: int = 3,
activation: str = 'relu'
) -> Model:
"""Create a model configuration."""
return Model(hidden_size=hidden_size, num_layers=num_layers, activation=activation)
@run.cli.factory
def my_optimizer(
learning_rate: float = 0.001,
weight_decay: float = 1e-5,
betas: Sequence[float] = (0.9, 0.999,)
) -> run.Config[Optimizer]:
"""Create an optimizer configuration."""
return run.Config(Optimizer, learning_rate=learning_rate, weight_decay=weight_decay, betas=list(betas))
@run.cli.entrypoint
def train_model(
model: Model = my_model(),
optimizer: Optimizer = my_optimizer(),
epochs: int = 10,
batch_size: int = 32
) -> None:
"""
Train a model using the specified configuration.
Args:
model: Configuration for the model.
optimizer: Configuration for the optimizer.
epochs: Number of training epochs. Defaults to 10.
batch_size: Batch size for training. Defaults to 32.
"""
print(f"Training model with the following configuration:")
print(f"Model: {model}")
print(f"Optimizer: {optimizer}")
print(f"Epochs: {epochs}")
print(f"Batch size: {batch_size}")
# Simulating model training
for epoch in range(epochs):
print(f"Epoch {epoch + 1}/{epochs}")
print("Training completed!")
if __name__ == "__main__":
run.cli.main(train_model)
python example.py model=my_model_2 model.num_layers=10
The expected behavior/old behavior is that the cli args are parsed properly. But on main(I don't know exactly when this is broken) getting
Resolved Arguments
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Argument Name ┃ Resolved Value ┃
┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ batch_size │ 32 │
│ epochs │ 10 │
│ model │ Model(num_layers=10) │
│ optimizer │ Optimizer(learning_rate=0.001, weight_decay=1e-05, │
│ │ betas=[0.9, 0.999]) │
└──────────────────────┴──────────────────────────────────────────────────────────────┘
Continue? [y/N]: y
Launching train_model...
Unexpected error: Model.__init__() missing 2 required positional arguments: 'hidden_size' and 'activation'
In this example when I run
The expected behavior/old behavior is that the cli args are parsed properly. But on
main(I don't know exactly when this is broken) getting