2023-03-14 06:29:18 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Callable
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
__all__ = ["ModelZooRegistry", "ModelAttribute", "model_zoo"]
|
2023-03-14 06:29:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ModelAttribute:
|
|
|
|
"""
|
|
|
|
Attributes of a model.
|
2023-03-15 02:42:07 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
has_control_flow (bool): Whether the model contains branching in its forward method.
|
|
|
|
has_stochastic_depth_prob (bool): Whether the model contains stochastic depth probability. Often seen in the torchvision models.
|
2023-03-14 06:29:18 +00:00
|
|
|
"""
|
2023-09-19 06:20:26 +00:00
|
|
|
|
2023-03-14 06:29:18 +00:00
|
|
|
has_control_flow: bool = False
|
2023-03-15 02:42:07 +00:00
|
|
|
has_stochastic_depth_prob: bool = False
|
2023-03-14 06:29:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModelZooRegistry(dict):
|
|
|
|
"""
|
|
|
|
A registry to map model names to model and data generation functions.
|
|
|
|
"""
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
def register(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
model_fn: Callable,
|
|
|
|
data_gen_fn: Callable,
|
|
|
|
output_transform_fn: Callable,
|
|
|
|
loss_fn: Callable = None,
|
|
|
|
model_attribute: ModelAttribute = None,
|
|
|
|
):
|
2023-03-14 06:29:18 +00:00
|
|
|
"""
|
|
|
|
Register a model and data generation function.
|
|
|
|
|
|
|
|
Examples:
|
2023-06-21 01:32:46 +00:00
|
|
|
|
|
|
|
```python
|
|
|
|
# normal forward workflow
|
|
|
|
model = resnet18()
|
|
|
|
data = resnet18_data_gen()
|
|
|
|
output = model(**data)
|
|
|
|
transformed_output = output_transform_fn(output)
|
|
|
|
loss = loss_fn(transformed_output)
|
|
|
|
|
|
|
|
# Register
|
|
|
|
model_zoo = ModelZooRegistry()
|
|
|
|
model_zoo.register('resnet18', resnet18, resnet18_data_gen, output_transform_fn, loss_fn)
|
|
|
|
```
|
2023-03-14 06:29:18 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): Name of the model.
|
2023-06-21 01:32:46 +00:00
|
|
|
model_fn (Callable): A function that returns a model. **It must not contain any arguments.**
|
|
|
|
data_gen_fn (Callable): A function that returns a data sample in the form of Dict. **It must not contain any arguments.**
|
|
|
|
output_transform_fn (Callable): A function that transforms the output of the model into Dict.
|
|
|
|
loss_fn (Callable): a function to compute the loss from the given output. Defaults to None
|
2023-03-14 06:29:18 +00:00
|
|
|
model_attribute (ModelAttribute): Attributes of the model. Defaults to None.
|
|
|
|
"""
|
2023-06-21 01:32:46 +00:00
|
|
|
self[name] = (model_fn, data_gen_fn, output_transform_fn, loss_fn, model_attribute)
|
2023-03-14 06:29:18 +00:00
|
|
|
|
|
|
|
def get_sub_registry(self, keyword: str):
|
|
|
|
"""
|
|
|
|
Get a sub registry with models that contain the keyword.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keyword (str): Keyword to filter models.
|
|
|
|
"""
|
|
|
|
new_dict = dict()
|
|
|
|
|
|
|
|
for k, v in self.items():
|
|
|
|
if keyword in k:
|
|
|
|
new_dict[k] = v
|
2023-06-30 08:16:44 +00:00
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
assert len(new_dict) > 0, f"No model found with keyword {keyword}"
|
2023-03-14 06:29:18 +00:00
|
|
|
return new_dict
|
|
|
|
|
|
|
|
|
|
|
|
model_zoo = ModelZooRegistry()
|