-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore original module type on first use #22
base: main
Are you sure you want to change the base?
Conversation
👋 Hello @Y-T-G, thank you for submitting an
For more detailed guidance on our contribution process, refer to the Contributing Guide. If your changes are addressing a bug, a minimum reproducible example (MRE) demonstrating the issue before and after your updates would help us evaluate and review your PR more effectively. 🎉 Thank you again for your contribution! This is an automated response, but rest assured an Ultralytics engineer will review your submission and provide additional feedback or approval soon. If you have any questions in the meantime, feel free to comment below. 🚀✨ |
Signed-off-by: Mohammed Yasin <[email protected]>
@Y-T-G oh this is a really innovative solution! |
All the tests seem to be passing correctly. |
Signed-off-by: Mohammed Yasin <[email protected]>
@glenn-jocher It shouldn't affect the local import functionality. Just that it can't restore or swap the module to the original one for local import. |
Global Test In [2]: def test_global():
...: from autoimport import lazy
...: import sys
...: global torch
...: with lazy():
...: import torch
...: print("Type before use:", type(torch))
...:
...: torch.nn
...: print("Still LazyLoader?:", hasattr(torch, "_module"))
...: print("torch pointing to the actual module?", torch is sys.modules["torch"])
...: print("Type after use:", type(torch))
...:
In [3]: test_global()
Type before use: <class 'autoimport.main.LazyLoader'>
Still LazyLoader?: False
torch pointing to the actual module? True
Type after use: <class 'module'> The actual reference gets swapped so Local Test In [2]: def test_local():
...: from autoimport import lazy
...: import sys
...: with lazy():
...: import torch
...: print("Type before use:", type(torch))
...:
...: torch.nn
...: print("Still LazyLoader?:", hasattr(torch, "_module"))
...: print("torch pointing to the actual module?", torch is sys.modules["torch"])
...: print("Type after use:", type(torch))
...:
In [3]: test_local()
Type before use: <class 'autoimport.main.LazyLoader'>
Still LazyLoader?: True
torch pointing to the actual module? False
Type after use: <class 'module'>
|
@Y-T-G oh I understand, so we need to predefine the packages as |
@glenn-jocher Assigns the original module to the variable on the first use, so that there's no LazyLoader being middle-man anymore. It uses the original module directly on subsequent use.
This will work for global import. It won't work for local import, because in local import the module is assigned to
locals()
, butlocals()
is not passed intolazy_import()
. Onlyglobals()
is passed. Even though there's an argument forlocals
, it's not passed. It's alwaysNone
. So it means it won't work inside test functions.We could modify the context manager to pass
locals()
manually:with lazy(locals()):
🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Improves the
LazyLoader
for better handling of lazy imports, enabling more robust and reliable module loading. 🚀📊 Key Changes
LazyLoader
class to track the originating module's name (from_name
) and optionally receive theglobals
dictionary to manage lazy imports better._load_module
to replace the global reference with the actual loaded module if the global matches the lazy instance.lazy_import
function to passglobals
andfrom_name
when initializingLazyLoader
.🎯 Purpose & Impact
lazy_import
utility more functional and robust, especially when used in complex projects or dynamic import scenarios. 💡