Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
106 lines (83 loc) · 3.09 KB
/
Copy pathmodel.py
File metadata and controls
106 lines (83 loc) · 3.09 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
100
101
102
103
104
105
106
"""Student-facing NFE-specific model definitions and checkpoint utilities."""
import torch
from torch import nn
from src.utils import count_parameters, parameter_summary
class Model(nn.Module):
"""DO NOT MODIFY: Common parent class for both NFE-specific models."""
def __init__(self, backbone: nn.Module):
super().__init__()
self.backbone = backbone
def count_parameters(self) -> int:
"""Return the total number of parameters, including frozen ones."""
return count_parameters(self, trainable_only=False)
@property
def num_parameters(self) -> int:
return self.count_parameters()
def parameter_summary(self) -> dict[str, int]:
return parameter_summary(self)
def forward(self, x: torch.Tensor, timestep: torch.Tensor, **kwargs):
return self.backbone(x, timestep, **kwargs)
def sample(
self,
shape: tuple[int, ...],
*,
device: torch.device | str = "cuda",
category: torch.Tensor | None = None,
**kwargs,
) -> torch.Tensor:
raise NotImplementedError("Implement sample() in the selected model class.")
@classmethod
def load_checkpoint(
cls,
checkpoint_path: str,
evaluate_mode: str,
device="cpu",
**kwargs,
) -> "Model":
"""Instantiate the selected model class and load its checkpoint."""
model_classes = {
"one_nfe": ModelOneNFE,
"few_nfe": ModelFewNFE,
}
try:
model_class = model_classes[evaluate_mode]
except KeyError as error:
raise ValueError(
f"Unsupported evaluate_mode={evaluate_mode!r}; "
"choose 'one_nfe' or 'few_nfe'."
) from error
model = model_class(device=device, **kwargs)
checkpoint = torch.load(
checkpoint_path,
map_location=device,
weights_only=False,
)
state_dict = checkpoint["state_dict"] if "state_dict" in checkpoint else checkpoint
model.load_state_dict(state_dict)
return model.to(device).eval()
class ModelOneNFE(Model):
"""Student model evaluated with exactly one model function evaluation."""
def __init__(self, device="cpu", **kwargs):
raise NotImplementedError("Implement the one-NFE model constructor.")
def sample(
self,
shape: tuple[int, ...],
*,
device: torch.device | str = "cuda",
category: torch.Tensor | None = None,
**kwargs,
) -> torch.Tensor:
raise NotImplementedError("Implement sample() in ModelOneNFE.")
class ModelFewNFE(Model):
"""Student model evaluated with no more than four model evaluations."""
def __init__(self, device="cpu", **kwargs):
raise NotImplementedError("Implement the few-NFE model constructor.")
def sample(
self,
shape: tuple[int, ...],
*,
device: torch.device | str = "cuda",
category: torch.Tensor | None = None,
**kwargs,
) -> torch.Tensor:
raise NotImplementedError("Implement sample() in ModelFewNFE.")