Files
xautodl/lib/xlayers/super_module.py

43 lines
1.1 KiB
Python
Raw Normal View History

2021-03-18 16:02:55 +08:00
#####################################################
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.01 #
#####################################################
import abc
2021-03-17 10:06:29 +00:00
import torch.nn as nn
2021-03-18 18:32:26 +08:00
from enum import Enum
class SuperRunMode(Enum):
"""This class defines the enumerations for Super Model Running Mode."""
FullModel = "fullmodel"
Default = "fullmodel"
2021-03-17 10:06:29 +00:00
2021-03-18 20:15:50 +08:00
class SuperModule(abc.ABC, nn.Module):
2021-03-18 16:02:55 +08:00
"""This class equips the nn.Module class with the ability to apply AutoDL."""
def __init__(self):
super(SuperModule, self).__init__()
2021-03-18 20:15:50 +08:00
self._super_run_type = SuperRunMode.Default
2021-03-18 16:02:55 +08:00
@abc.abstractmethod
def abstract_search_space(self):
raise NotImplementedError
2021-03-18 18:32:26 +08:00
@property
def super_run_type(self):
return self._super_run_type
@abc.abstractmethod
def forward_raw(self, *inputs):
raise NotImplementedError
def forward(self, *inputs):
if self.super_run_type == SuperRunMode.FullModel:
return self.forward_raw(*inputs)
else:
raise ModeError(
"Unknown Super Model Run Mode: {:}".format(self.super_run_type)
)