-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtask.py
More file actions
99 lines (78 loc) · 2.72 KB
/
Copy pathtask.py
File metadata and controls
99 lines (78 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import dataclass
from typing import List
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
def my_optimizer(
learning_rate: float = 0.001, weight_decay: float = 1e-5, betas: List[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=betas
)
def train_model(
model: Model,
optimizer: Optimizer,
epochs: int = 10,
batch_size: int = 32,
):
"""
Train a model using the specified configuration.
Args:
model (Model): Configuration for the model.
optimizer (Optimizer): Configuration for the optimizer.
epochs (int, optional): Number of training epochs. Defaults to 10.
batch_size (int, optional): Batch size for training. Defaults to 32.
"""
print("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!")
@run.cli.factory(target=train_model)
def train_recipe() -> run.Partial[train_model]:
return run.Partial(
train_model,
model=my_model(hidden_size=512),
optimizer=my_optimizer(learning_rate=0.0005),
epochs=50,
batch_size=2048,
)
if __name__ == "__main__":
run.cli.main(train_model, cmd_defaults={"skip_confirmation": True})