Misc

Python function profiler utilities

nnabla.utils.function_profile.profile(fn=None, condition=None, profile_class=<class 'cProfile.Profile'>, print_freq=0, sort_keys=None, print_restrictions=None)[source]

Decorating a function that is profiled with a Python profiler such as cProfile.Profile.

Note: function doesn’t refer to Function. A Python function.

Parameters
  • fn (function) – A function that is profiled. If None is specified (default), it returns a new decorator function. It is used when you want to specify optional arguments of this decorator function.

  • condition (function) – A function object which takes the same inputs with the decorated function, and returns a boolean value. The decorated function is profiled only when the condition function returns True. By default, it returns always True, hence profiling is performed everytime the decorated function is called.

  • profile_class (class) – A profiler class such as cProfile.Profile and Profile.Profile. The default value is cProfile.Profile.

  • print_freq (int) – The profiling result is printed at function calls with an interval specified by print_freq. If 0 is specified (default), the profiling result is only printed at the end of the Python process unless decorated_func.profiler.print_stats() is called manually.

  • sort_keys (iterable) – A list or tuple of string, which is passed to pstats.Stats.sort_stats() as arguments. The default is ('cumulative', 'time', 'calls').

  • print_restriction (iterable) – A list or tuple which is passed to pstats.Stats.print_stats() as arguments. The default value is (40,), which results in only 40 functions inside the decorated function are printed in the profiling result.

Returns: function

A decorated function. If fn is None, a new decorator function with optional arguments specified.

Example

By decorating a function as following, the profling result is printed at the end of the Python process.

from nnabla.utils import function_profile

@function_profile.profile
def foo(a, b, c=None, d=None):
    ...

If you want to manually print the profiling result so far, use FunctionProfile.print_stats() of the FunctionProfile object attached to the decorated function as profiler attribute.

foo.profiler.print_stats()

If you want to profile the function only when a specific argument is passed to, use the condition argument as following.

def profile_only_if_c_is_not_none(a, b, c=None, d=None):
    return c is not None

@function_profile.profile(condition=profile_only_if_c_is_not_none)
def foo(a, b, c=None, d=None):
    ...
class nnabla.utils.function_profile.FunctionProfile(fn, condition=None, profile_class=<class 'cProfile.Profile'>, print_freq=0, sort_keys=None, print_restrictions=None)[source]

Function profiler object.

This is usually not directly used by users. It’s created via profile(), and attached to a decorated function object as an attribute profiler. See profile function for details.

print_stats(reset=True)[source]

Manually print profiling result.

Parameters

reset (bool) – If False is specified, the profiling statistics so far is maintained. If True (default), reset_stats is called to reset the profiling statistics.

reset_stats()[source]

Manually reset the profiling statistics collected so far.