Skip to content
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

Legacy pickled models support #162

Merged
merged 5 commits into from
Jan 31, 2023
Merged

Legacy pickled models support #162

merged 5 commits into from
Jan 31, 2023

Conversation

KSGulin
Copy link

@KSGulin KSGulin commented Jan 30, 2023

This PR adds backwards compatibility for models that were pickled in a legacy version of our YOLOv5 integration. Three causes of errors are patched here:

  • In the previous integration, we pickled any model that wasn't quantized. load_sparsified_model was updated to work with a pickled mode
  • Our custom _Add has been moved, which causes unpickling issues. '_Add' has been programmatically added to common, where it is expected
  • Our previous integration was always installed as a package, which means that all pickled classes are saved with the path yolov5.... With the new integration our tools can also be used in the ultralytics upstream repo. The yolov5 and yolov5.models namespaces have been added to accomodate loading these legacy models on upstream as well.

Test plan
Local testing with legacy model on both installed package through SparseML and directly on fork

@KSGulin KSGulin requested a review from a team January 30, 2023 17:00
@KSGulin KSGulin self-assigned this Jan 30, 2023
@KSGulin KSGulin requested review from bfineran, rahul-tuli and dfneuralmagic and removed request for a team January 30, 2023 17:00
@KSGulin KSGulin changed the title Namespace patch Legacy pickled models support Jan 30, 2023
@KSGulin KSGulin changed the base branch from master to nm_integration January 30, 2023 17:23
@KSGulin KSGulin changed the base branch from nm_integration to master January 30, 2023 17:23
utils/neuralmagic/utils.py Show resolved Hide resolved
utils/neuralmagic/utils.py Show resolved Hide resolved
utils/neuralmagic/utils.py Show resolved Hide resolved
# pickled, pruned models.
import models
from models import common
setattr(common, "_Add", _Add) # Definition of the _Add module has moved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah I love this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was hoping to import it directly in common.py, but quickly found myself in circular import hell. This way is probably more intentional anyway

Comment on lines +41 to +44
if "yolov5" not in sys.modules:
sys.modules["yolov5"] = ""
sys.modules["yolov5.models"] = models
sys.modules["yolov5.models.common"] = common

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this does work after all? You were saying it wasn't working at first right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue I was running into with this was actually for the _Add module. During unpickling it was trying to grab _Add from models.common which is not the same as grabbing models.common._Add. Basically trying to append to an existing namespace like that doesn't work. But creating a new namespace was pretty much painless, just gotta make sure to address every module level (yolov5, yolov5.models, etc..)

Copy link

@corey-nm corey-nm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@KSGulin KSGulin merged commit 5044189 into master Jan 31, 2023
KSGulin added a commit that referenced this pull request Jan 31, 2023
KSGulin added a commit that referenced this pull request Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants