🚨 🚨 Fix custom code saving (#37716)
* Firstly: Better detection of when we're a custom class * Trigger tests * Let's break everything * make fixup * fix mistaken line doubling * Let's try to get rid of it from config classes at least * Let's try to get rid of it from config classes at least * Fixup image processor * no more circular import * Let's go back to setting `_auto_class` again * Let's go back to setting `_auto_class` again * stash commit * Revert the irrelevant changes until we figure out AutoConfig * Change tests since we're breaking expectations * make fixup * do the same for all custom classes * Cleanup for feature extractor tests * Cleanup tokenization tests too * typo * Fix tokenizer tests * make fixup * fix image processor test * make fixup * Remove warning from register_for_auto_class * Stop adding model info to auto map entirely * Remove todo * Remove the other todo * Let's start slapping _auto_class on models why not * Let's start slapping _auto_class on models why not * Make sure the tests know what's up * Make sure the tests know what's up * Completely remove add_model_info_to_* * Start adding _auto_class to models * Start adding _auto_class to models * Add a flaky decorator * Add a flaky decorator and import * stash commit * More message cleanup * make fixup * fix indent * Fix trust_remote_code prompts * make fixup * correct indentation * Reincorporate changes into dynamic_module_utils * Update call to trust_remote_code * make fixup * Fix video processors too * Fix video processors too * Remove is_flaky additions * make fixup
This commit is contained in:
@@ -122,19 +122,11 @@ class AutoConfigTest(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
config.save_pretrained(tmp_dir)
|
||||
reloaded_config = AutoConfig.from_pretrained(tmp_dir, trust_remote_code=True)
|
||||
self.assertTrue(os.path.exists(os.path.join(tmp_dir, "configuration.py"))) # Assert we saved config code
|
||||
# Assert we're pointing at local code and not another remote repo
|
||||
self.assertEqual(reloaded_config.auto_map["AutoConfig"], "configuration.NewModelConfig")
|
||||
self.assertEqual(reloaded_config.__class__.__name__, "NewModelConfig")
|
||||
|
||||
# The configuration file is cached in the snapshot directory. So the module file is not changed after dumping
|
||||
# to a temp dir. Because the revision of the configuration file is not changed.
|
||||
# Test the dynamic module is loaded only once if the configuration file is not changed.
|
||||
self.assertIs(config.__class__, reloaded_config.__class__)
|
||||
|
||||
# Test the dynamic module is reloaded if we force it.
|
||||
reloaded_config = AutoConfig.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_model", trust_remote_code=True, force_download=True
|
||||
)
|
||||
self.assertIsNot(config.__class__, reloaded_config.__class__)
|
||||
|
||||
def test_from_pretrained_dynamic_config_conflict(self):
|
||||
class NewModelConfigLocal(BertConfig):
|
||||
model_type = "new-model"
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -125,19 +126,12 @@ class AutoFeatureExtractorTest(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
feature_extractor.save_pretrained(tmp_dir)
|
||||
reloaded_feature_extractor = AutoFeatureExtractor.from_pretrained(tmp_dir, trust_remote_code=True)
|
||||
self.assertTrue(os.path.exists(os.path.join(tmp_dir, "feature_extractor.py"))) # Assert we saved code
|
||||
self.assertEqual(
|
||||
reloaded_feature_extractor.auto_map["AutoFeatureExtractor"], "feature_extractor.NewFeatureExtractor"
|
||||
)
|
||||
self.assertEqual(reloaded_feature_extractor.__class__.__name__, "NewFeatureExtractor")
|
||||
|
||||
# The feature extractor file is cached in the snapshot directory. So the module file is not changed after dumping
|
||||
# to a temp dir. Because the revision of the module file is not changed.
|
||||
# Test the dynamic module is loaded only once if the module file is not changed.
|
||||
self.assertIs(feature_extractor.__class__, reloaded_feature_extractor.__class__)
|
||||
|
||||
# Test the dynamic module is reloaded if we force it.
|
||||
reloaded_feature_extractor = AutoFeatureExtractor.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_feature_extractor", trust_remote_code=True, force_download=True
|
||||
)
|
||||
self.assertIsNot(feature_extractor.__class__, reloaded_feature_extractor.__class__)
|
||||
|
||||
def test_new_feature_extractor_registration(self):
|
||||
try:
|
||||
AutoConfig.register("custom", CustomConfig)
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -190,13 +191,12 @@ class AutoImageProcessorTest(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
image_processor.save_pretrained(tmp_dir)
|
||||
reloaded_image_processor = AutoImageProcessor.from_pretrained(tmp_dir, trust_remote_code=True)
|
||||
self.assertTrue(os.path.exists(os.path.join(tmp_dir, "image_processor.py"))) # Assert we saved custom code
|
||||
self.assertEqual(
|
||||
reloaded_image_processor.auto_map["AutoImageProcessor"], "image_processor.NewImageProcessor"
|
||||
)
|
||||
self.assertEqual(reloaded_image_processor.__class__.__name__, "NewImageProcessor")
|
||||
|
||||
# The image processor file is cached in the snapshot directory. So the module file is not changed after dumping
|
||||
# to a temp dir. Because the revision of the module file is not changed.
|
||||
# Test the dynamic module is loaded only once if the module file is not changed.
|
||||
self.assertIs(image_processor.__class__, reloaded_image_processor.__class__)
|
||||
|
||||
# Test the dynamic module is reloaded if we force it.
|
||||
reloaded_image_processor = AutoImageProcessor.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_image_processor", trust_remote_code=True, force_download=True
|
||||
|
||||
@@ -332,11 +332,6 @@ class AutoModelTest(unittest.TestCase):
|
||||
for p1, p2 in zip(model.parameters(), reloaded_model.parameters()):
|
||||
self.assertTrue(torch.equal(p1, p2))
|
||||
|
||||
# The model file is cached in the snapshot directory. So the module file is not changed after dumping
|
||||
# to a temp dir. Because the revision of the module file is not changed.
|
||||
# Test the dynamic module is loaded only once if the module file is not changed.
|
||||
self.assertIs(model.__class__, reloaded_model.__class__)
|
||||
|
||||
# Test the dynamic module is reloaded if we force it.
|
||||
reloaded_model = AutoModel.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_model", trust_remote_code=True, force_download=True
|
||||
@@ -362,11 +357,6 @@ class AutoModelTest(unittest.TestCase):
|
||||
for p1, p2 in zip(model.parameters(), reloaded_model.parameters()):
|
||||
self.assertTrue(torch.equal(p1, p2))
|
||||
|
||||
# The model file is cached in the snapshot directory. So the module file is not changed after dumping
|
||||
# to a temp dir. Because the revision of the module file is not changed.
|
||||
# Test the dynamic module is loaded only once if the module file is not changed.
|
||||
self.assertIs(model.__class__, reloaded_model.__class__)
|
||||
|
||||
# Test the dynamic module is reloaded if we force it.
|
||||
reloaded_model = AutoModel.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_model_with_util", trust_remote_code=True, force_download=True
|
||||
|
||||
@@ -342,17 +342,20 @@ class AutoTokenizerTest(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
tokenizer.save_pretrained(tmp_dir)
|
||||
reloaded_tokenizer = AutoTokenizer.from_pretrained(tmp_dir, trust_remote_code=True, use_fast=False)
|
||||
self.assertTrue(
|
||||
os.path.exists(os.path.join(tmp_dir, "tokenization.py"))
|
||||
) # Assert we saved tokenizer code
|
||||
self.assertEqual(reloaded_tokenizer._auto_class, "AutoTokenizer")
|
||||
with open(os.path.join(tmp_dir, "tokenizer_config.json"), "r") as f:
|
||||
tokenizer_config = json.load(f)
|
||||
# Assert we're pointing at local code and not another remote repo
|
||||
self.assertEqual(tokenizer_config["auto_map"]["AutoTokenizer"], ["tokenization.NewTokenizer", None])
|
||||
self.assertEqual(reloaded_tokenizer.__class__.__name__, "NewTokenizer")
|
||||
self.assertTrue(reloaded_tokenizer.special_attribute_present)
|
||||
else:
|
||||
self.assertEqual(tokenizer.__class__.__name__, "NewTokenizer")
|
||||
self.assertEqual(reloaded_tokenizer.__class__.__name__, "NewTokenizer")
|
||||
|
||||
# The tokenizer file is cached in the snapshot directory. So the module file is not changed after dumping
|
||||
# to a temp dir. Because the revision of the module file is not changed.
|
||||
# Test the dynamic module is loaded only once if the module file is not changed.
|
||||
self.assertIs(tokenizer.__class__, reloaded_tokenizer.__class__)
|
||||
|
||||
# Test the dynamic module is reloaded if we force it.
|
||||
reloaded_tokenizer = AutoTokenizer.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_tokenizer", trust_remote_code=True, force_download=True
|
||||
|
||||
@@ -174,17 +174,6 @@ class AutoVideoProcessorTest(unittest.TestCase):
|
||||
reloaded_video_processor = AutoVideoProcessor.from_pretrained(tmp_dir, trust_remote_code=True)
|
||||
self.assertEqual(reloaded_video_processor.__class__.__name__, "NewVideoProcessor")
|
||||
|
||||
# The image processor file is cached in the snapshot directory. So the module file is not changed after dumping
|
||||
# to a temp dir. Because the revision of the module file is not changed.
|
||||
# Test the dynamic module is loaded only once if the module file is not changed.
|
||||
self.assertIs(video_processor.__class__, reloaded_video_processor.__class__)
|
||||
|
||||
# Test the dynamic module is reloaded if we force it.
|
||||
reloaded_video_processor = AutoVideoProcessor.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_video_processor", trust_remote_code=True, force_download=True
|
||||
)
|
||||
self.assertIsNot(video_processor.__class__, reloaded_video_processor.__class__)
|
||||
|
||||
def test_new_video_processor_registration(self):
|
||||
try:
|
||||
AutoConfig.register("custom", CustomConfig)
|
||||
|
||||
Reference in New Issue
Block a user