Load dynamic module (remote code) only once if code isn't change (#33162)
* Load remote code only once * Use hash as load indicator * Add a new option `force_reload` for old behavior (i.e. always reload) * Add test for dynamic module is cached * Add more type annotations to improve code readability * Address comments from code review
This commit is contained in:
@@ -167,12 +167,29 @@ class AutoImageProcessorTest(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(image_processor.__class__.__name__, "NewImageProcessor")
|
||||
|
||||
# Test the dynamic module is loaded only once.
|
||||
reloaded_image_processor = AutoImageProcessor.from_pretrained(
|
||||
"hf-internal-testing/test_dynamic_image_processor", trust_remote_code=True
|
||||
)
|
||||
self.assertIs(image_processor.__class__, reloaded_image_processor.__class__)
|
||||
|
||||
# Test image processor can be reloaded.
|
||||
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.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
|
||||
)
|
||||
self.assertIsNot(image_processor.__class__, reloaded_image_processor.__class__)
|
||||
|
||||
def test_new_image_processor_registration(self):
|
||||
try:
|
||||
AutoConfig.register("custom", CustomConfig)
|
||||
|
||||
Reference in New Issue
Block a user