Skip to content

Commit e99a231

Browse files
Rocketknight1LysandreJik
authored andcommitted
Experimenting with adding proper get_config() and from_config() methods (huggingface#14361)
* Experimenting with adding proper get_config() and from_config() methods * Adding a test for get/from config * Fix test for get/from config
1 parent 341a059 commit e99a231

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/transformers/modeling_tf_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,13 @@ def __init__(self, config, *inputs, **kwargs):
692692
self.config = config
693693
self.name_or_path = config.name_or_path
694694

695+
def get_config(self):
696+
return self.config
697+
698+
@classmethod
699+
def from_config(cls, config, **kwargs):
700+
return cls._from_config(config, **kwargs)
701+
695702
@classmethod
696703
def _from_config(cls, config, **kwargs):
697704
"""

tests/test_modeling_tf_common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ def test_save_load(self):
160160

161161
self.assert_outputs_same(after_outputs, outputs)
162162

163+
def test_save_load_config(self):
164+
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
165+
166+
for model_class in self.all_model_classes:
167+
model = model_class(config)
168+
outputs = model(self._prepare_for_class(inputs_dict, model_class))
169+
170+
new_model = model_class.from_config(model.get_config())
171+
_ = new_model(self._prepare_for_class(inputs_dict, model_class)) # Build model
172+
new_model.set_weights(model.get_weights())
173+
after_outputs = new_model(self._prepare_for_class(inputs_dict, model_class))
174+
175+
self.assert_outputs_same(after_outputs, outputs)
176+
163177
@tooslow
164178
def test_graph_mode(self):
165179
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

0 commit comments

Comments
 (0)