[Feat] Refactoring type assertions
See original GitHub issue🚀 Feature
Refactoring or removing type and shape assertions like in https://github.com/kornia/kornia/blob/65777029ff430f841f19aa5cd08ddb3c4ca36338/kornia/losses/ssim.py#L44-L65
Motivation
These assertions are useful for quick prototyping and debugging. However, after that, they are just slowing down the components, especially if they are used repetitively.
Moreover, one of the goal of the roadmap (#700) is to use PyTorch JIT and a scripted function already has type assertion built-in as it transformed to C++
.
Alternatives
I thought of multiple solutions:
- Removing the type and shape assertions. Probably the easiest to implement, but not the most secure or user friendly.
- Adding a
assert_type: bool = True
argument to every component and the type assertion would only be performedif assert_type == True:
. It would also be best to centralize the type assertions (there is a lot of duplicated code currently). - Similarly to the previous idea, but instead of using an argument, use a global variable
assert_type
, that can be modified with a context manager (inspired bytorch.set_grad_enabled()
).
Additional context
I’m implementing a Image Quality Assessment (IQA) library (piqa). I’m considering merging it into kornia
, but I have put a big effort into making it fast (and supporting JIT) and I don’t want to ruin my efforts with unnecessary type assertions.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
I am, but I will be very busy until the end of July (finishing my thesis). After that, I should come back to you to know how I can help. You can close the issue until then 😉
Hi @edgarriba,
I didn’t knew the
assert
could be disabled, it is a great solution! I’ve used it to implement assertion in PIQA forPSNR
andSSIM
and benchmark (see benchmark.py) the difference.Conclusions:
8x
the PSNR and2x
the SSIM) the implementations.-O
flag (with my implementation) completely removes the bottleneck!__debug__
is a global variable!First, here are the runtimes currently (
kornia
withif ...: raise ...
andpiqa
without any assertions).Then I called the following function in both
PSNR
andSSIM
inpiqa
.which gave, without
-O
flag,and, with the
-O
flag,