Stonesjtu / pytorch_memlab

Profiling and inspecting memory in pytorch
MIT License
1.01k stars 37 forks source link

turn profile decorator on and off #34

Closed Chen-Cai-OSU closed 3 years ago

Chen-Cai-OSU commented 3 years ago

Hello,

This is probably a naive question. I would like to turn on and off the decorator without commenting things out. What would be an elegant way to achieve this? I have something like the following in mind but I don't know how to achieve this. Would you like to share some thoughts on this? Many thanks!

profile_flag = True # False

@profile(profile_flag)
def func1():
    ...

@profile(profile_flag)
def func2():
    ...
Stonesjtu commented 3 years ago

Well this is absolutely a great feature, and it's quite straightforward to implement that.

Actually the profile API follows the style from line_profiler so I did not made any change to that.

By now we have an enabled option in profiler but the profiler will still prints the profiling results with this option set to False.

However you can monkey patch the profile function to a no-op like:

profile = lambda a:a

Hope it helps.

Chen-Cai-OSU commented 3 years ago

That's great! Thank you so much!