v2.0.22 - Leave no hooks or buffers behind when profiling fails or shares modules (#140)
🌟 Summary
THOP v2.0.22 makes model profiling safer and more reliable by ensuring temporary hooks and buffers are always cleaned up—even when profiling fails or models contain shared modules. 🧹🛡️
📊 Key Changes
-
Guaranteed cleanup with
finally🧼
Profiling now removes temporary hooks and operation-counting buffers if the model’s forward pass raises an error. -
Prevented duplicate hooks on shared modules 🔗
Modules referenced by multiple parent layers are now hooked only once, avoiding orphaned hook handles. -
Improved operation counting for shared modules 📊
The profiler tracks already-counted modules to prevent duplicate operation and parameter totals when the same module is reached through multiple paths. -
Preserved the model’s original training state ⚙️
Models are still restored to their previous training or evaluation mode after profiling completes or fails. -
Version updated to
2.0.22📦
🎯 Purpose & Impact
-
Prevents permanently corrupted models 🚨
Previously, failed profiling or shared-module architectures could leave hooks pointing to deleted buffers, making the model unusable afterward. This is especially important for models such as RT-DETR-L with heavily shared activation modules. -
Makes profiling safer to run on live models ✅
Users can profile models without worrying that temporary profiling state will remain attached to the original model. -
Improves profiling accuracy 🎯
Shared modules are no longer counted repeatedly, producing more dependable operation and parameter estimates. -
Supports future simplification of FLOPs calculation 🚀
With profiling cleanup now reliable across success and failure paths, Ultralytics can eventually remove the defensivedeepcopy(model)workaround used byget_flops(). -
No user-facing API changes are required 👍
Existingthop.profile()workflows should continue to work while benefiting from improved robustness.
What's Changed
- Leave no hooks or buffers behind when profiling fails or shares modules by @glenn-jocher in #140
Full Changelog: v2.0.21...v2.0.22