Ahead-of-time Inspect Guide
Ahead-of-time Inspect Guide
The inspect function is a powerful tool for analyzing PyTorch models and pipelines. It helps you understand model structure, identify tuneable modules, and gather execution statistics. It can also be a first step to pick modules before ahead-of-time tuning.
Overview
Inspection provides:
- Module Discovery: Automatically finds all PyTorch modules in your model or pipeline
- Execution Tracking: Identifies which modules are executed during inference
- Performance Profiling: Measures execution time for each module
- Input and output data types: records model input and its results
Basic Usage
Inspection Parameters
Complete Signature
Parameter Details
obj (Required)
The object to inspect. Can be:
torch.nn.Module: Any PyTorch module- Callable: Any callable containing PyTorch modules (e.g., HuggingFace pipelines)
dataset (Required)
This is the source of data for your model. It can be:
torch.Tensor- sequence of strings, tensors, or dictionaries. The collate function is used to stack samples into batches.
torch.utils.data.Dataset
For customization you can use ait.DataLoaderFactory class.
inference_function (Optional)
Custom function for running inference. Useful for complex execution logic:
If the custom function uses scalar arguments that should also be tuned, use the same function later in
ait.tune() after wrapping the inspected modules. See
Inspect and tune with the same workload wrapper.
number_of_iterations (Default: 10)
Number of iterations for profiling execution time:
warmup_iterations (Default: 5)
Warmup iterations before profiling to stabilize measurements:
min_depth (Default: 0)
Minimum depth level for module discovery. Increase if root-level modules don’t work:
max_depth (Default: 5)
If a nested (child) module has a larger depth than max_depth it will be skipped from inspection.
Both min_depth and max_depth narrow the inspection search to a reasonable range.
Working with InspectedModulesInfo
The inspect function returns an InspectedModulesInfo object with several useful methods:
describe()
Display comprehensive information about discovered modules:
Output example for stable-diffusion-3-medium-diffusers:
get_modules()
This function allows getting found modules. Its basic usage returns all of them:
You can place additional criteria:
- min_execution_ratio - minimum ratio of total execution time e.g. .9
- limit - maximum number of modules to return, e.g., 5
If those criteria are not sufficient, you can manually filter modules:
Troubleshooting
Issue: No modules found
Summary
Ahead-of-time inspection can be a first step in exploring your model’s structure and performance. It can also be used to select modules for tuning. For details on the tuning workflow, head to the AOT Tuning Guide.