-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_powerformer.py
58 lines (44 loc) · 1.68 KB
/
train_powerformer.py
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
from typing import Literal
import hydra
import optuna
from hydra_trainer import BaseTrainer
from omegaconf import DictConfig
from powerformer.dataset import PowerformerDataset, PowerformerDatasetConfig
from powerformer.model import Powerformer, PowerformerConfig
from powerformer.util import get_device_name
# logging.getLogger("transformers").setLevel(logging.ERROR)
class PowerformerTrainer(BaseTrainer[PowerformerDataset, PowerformerDatasetConfig]):
def __init__(self, cfg: DictConfig):
super().__init__(cfg)
def dataset_factory(
self,
dataset_cfg: PowerformerDatasetConfig,
dataset_key: Literal["train", "eval"],
) -> PowerformerDataset:
return PowerformerDataset(
dataset_cfg,
dataset_key,
self.cfg.model.context_len,
self.cfg.model.prediction_len,
)
def model_init_factory(self):
def model_init(trial: optuna.Trial | None = None):
model_cfg = self.get_trial_model_cfg(trial, self.cfg)
cfg = PowerformerConfig(**model_cfg)
model = (
Powerformer.from_pretrained(self.cfg.checkpoint_path)
if self.cfg.checkpoint_path is not None
else Powerformer(cfg)
)
model.to(get_device_name()) # type: ignore
if trial is None:
print(model)
print(f"Model size: {model.num_parameters()} parameters.")
return model
return model_init
@hydra.main(config_path="conf", config_name="config", version_base=None)
def main(cfg: DictConfig):
predictor = PowerformerTrainer(cfg)
predictor.train()
if __name__ == "__main__":
main()