(cls: Type[si_module_TpV])
| 11 | # we use this in our research codebase to make modules from callable configs |
| 12 | si_module_TpV = TypeVar('si_module_TpV') |
| 13 | def si_module(cls: Type[si_module_TpV]) -> Type[si_module_TpV]: |
| 14 | if not hasattr(cls, 'Config') or not isinstance(cls.Config, type): |
| 15 | class Config: |
| 16 | pass |
| 17 | cls.Config = Config |
| 18 | |
| 19 | cls.Config = dataclass(cls.Config) |
| 20 | |
| 21 | class ConfigWrapper(cls.Config, Generic[si_module_TpV]): |
| 22 | def __call__(self, *args, **kwargs) -> si_module_TpV: |
| 23 | if len(kwargs) > 0: |
| 24 | config_dict = {field.name: getattr(self, field.name) for field in self.__dataclass_fields__.values()} |
| 25 | config_dict.update(kwargs) |
| 26 | new_config = type(self)(**config_dict) |
| 27 | return cls(new_config) |
| 28 | else: |
| 29 | return cls(self, *args) |
| 30 | |
| 31 | ConfigWrapper.__module__ = cls.__module__ |
| 32 | ConfigWrapper.__name__ = f"{cls.__name__}Config" |
| 33 | ConfigWrapper.__qualname__ = f"{cls.__qualname__}.Config" |
| 34 | |
| 35 | cls.Config = ConfigWrapper |
| 36 | |
| 37 | original_init = cls.__init__ |
| 38 | def new_init(self, *args, **kwargs): |
| 39 | self.c = next((arg for arg in args if isinstance(arg, cls.Config)), None) or next((arg for arg in kwargs.values() if isinstance(arg, cls.Config)), None) |
| 40 | original_init(self, *args, **kwargs) |
| 41 | self.register_buffer('_device_tracker', T.Tensor(), persistent=False) |
| 42 | |
| 43 | cls.__init__ = new_init |
| 44 | |
| 45 | @property |
| 46 | def device(self): |
| 47 | return self._device_tracker.device |
| 48 | |
| 49 | @property |
| 50 | def dtype(self): |
| 51 | return self._device_tracker.dtype |
| 52 | |
| 53 | cls.device = device |
| 54 | cls.dtype = dtype |
| 55 | |
| 56 | return cls |
| 57 | |
| 58 | |
| 59 | def get_activation(nonlinear_activation, nonlinear_activation_params={}): |
nothing calls this directly
no outgoing calls
no test coverage detected