Module Function Kernel Profiler
ModuleFunctionKernelProfiler shows which CUDA kernels are launched by
torch.nn.functional calls inside a module hierarchy. It attributes every recorded kernel to:
- the nearest profiled module;
- the
torch.nn.functionalcall; - the underlying PyTorch operator;
- the CUDA kernel and its duration.
It can also collect distinct input samples and their call counts for selected functional calls. This is useful when identifying frequently executed operations, preparing representative kernel benchmarks, or deciding which functions are worth optimizing.
See Kernel Providers to prepare, validate, benchmark, and activate alternative implementations for the functional calls identified by the profiler.
This profiler answers a different question from aitune.torch.profile():
Quick start
When the inference callable is an nn.Module, the profiler infers the module hierarchy automatically:
Each item in samples is an (args, kwargs) pair. A warmup iteration executes the complete list once. After warmup,
the profiler executes the complete list once more while recording events.
Profiling a module used by a pipeline
The inference callable does not need to be the module itself. Pass module when a pipeline or another function invokes
the module you want to observe:
When data is omitted, each warmup iteration and the recorded iteration call inference_fn() without arguments.
Profiling results
profiling_df contains one row per attributed CUDA kernel:
describe_results() groups the most expensive functional calls (up to top_k, default 10) and returns one row per
function:
Example dataframe:
Note: time_spent is calculated for that particular function excluding calls for inner functions e.g. for a multi_head_attention_forward which calls linear and scaled_dot_product_attention, time_spent will cover each function own time.
Collected input samples
function_data maps each collected function name to (call_count, sample) pairs. Each sample has the same
(args, kwargs) form accepted by profile():
function_names controls only sample collection; it does not filter rows from profiling_df. Use:
Noneto collect inputs for every observed functional call;- a set such as
{"linear", "conv2d"}to collect inputs only for those calls; - an empty set to collect no inputs while retaining kernel profiling.
Input samples retain references to their tensors until the next profile() call or until the profiler is released.
Restrict function_names when profiling workloads with many distinct inputs to reduce retained memory.
Runtime behavior and requirements
- CUDA is required.
- Warmup and recorded inference run under
torch.no_grad(). - Module forwards and supported
torch.nn.functionalfunctions are instrumented only for the duration ofprofile()and restored afterward, including when inference raises an exception. - Do not concurrently execute inference that uses the same modules or functional namespace while profiling.