lhallee commited on
Commit
476681a
·
verified ·
1 Parent(s): 87f4ab1

Give this artifact's bridge its own config and model subclasses (FastPLMs#50)

Browse files

Artifacts share one FastPLMs runtime per process. Re-exported shared classes let one artifact's model class answer for another with the same config class, for example ESMFold2-300 built as ESMFold2Model after ESMFold2-Fast loaded. Only modeling_fastplms.py changes; the runtime bundle and weights are untouched. https://github.com/Synthyra/FastPLMs/issues/50

Files changed (1) hide show
  1. modeling_fastplms.py +29 -11
modeling_fastplms.py CHANGED
@@ -180,14 +180,32 @@ def _install_runtime():
180
  return package
181
 
182
  _install_runtime()
183
- _module_182 = _import_without_bytecode("fastplms.models.esmfold2.configuration_esmfold2")
184
- ESMFold2Config = _module_182.ESMFold2Config
185
- ESMFold2Config.__module__ = __name__
186
- _module_185 = _import_without_bytecode("fastplms.models.esmfold2.modeling_esmfold2_classification")
187
- ESMFold2ExperimentalForSequenceClassification = _module_185.ESMFold2ExperimentalForSequenceClassification
188
- ESMFold2ExperimentalForSequenceClassification.__module__ = __name__
189
- ESMFold2ExperimentalForTokenClassification = _module_185.ESMFold2ExperimentalForTokenClassification
190
- ESMFold2ExperimentalForTokenClassification.__module__ = __name__
191
- _module_190 = _import_without_bytecode("fastplms.models.esmfold2.modeling_esmfold2_experimental")
192
- ESMFold2ExperimentalModel = _module_190.ESMFold2ExperimentalModel
193
- ESMFold2ExperimentalModel.__module__ = __name__
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  return package
181
 
182
  _install_runtime()
183
+
184
+ def _artifact_class(base, config_class=None):
185
+ """Subclass a shared runtime class so this artifact registers its own classes.
186
+
187
+ The base constructor is kept explicitly because Transformers turns every config
188
+ subclass into a dataclass, which would otherwise generate a new constructor.
189
+ """
190
+ namespace = {
191
+ "__module__": __name__,
192
+ "__qualname__": base.__name__,
193
+ "__doc__": base.__doc__,
194
+ "__init__": base.__init__,
195
+ }
196
+ base_config_class = getattr(base, "config_class", None)
197
+ if (
198
+ config_class is not None
199
+ and isinstance(base_config_class, type)
200
+ and issubclass(config_class, base_config_class)
201
+ ):
202
+ namespace["config_class"] = config_class
203
+ return type(base.__name__, (base,), namespace)
204
+
205
+ _module_204 = _import_without_bytecode("fastplms.models.esmfold2.configuration_esmfold2")
206
+ _module_205 = _import_without_bytecode("fastplms.models.esmfold2.modeling_esmfold2_classification")
207
+ _module_206 = _import_without_bytecode("fastplms.models.esmfold2.modeling_esmfold2_experimental")
208
+ ESMFold2Config = _artifact_class(_module_204.ESMFold2Config)
209
+ ESMFold2ExperimentalForSequenceClassification = _artifact_class(_module_205.ESMFold2ExperimentalForSequenceClassification, config_class=ESMFold2Config)
210
+ ESMFold2ExperimentalForTokenClassification = _artifact_class(_module_205.ESMFold2ExperimentalForTokenClassification, config_class=ESMFold2Config)
211
+ ESMFold2ExperimentalModel = _artifact_class(_module_206.ESMFold2ExperimentalModel, config_class=ESMFold2Config)