profiler
shrinkai.profiler
Profiling: measuring and comparing a model's deployment footprint.
Profiler.compare (returning a BenchmarkReport) is the main entry point,
gathering parameter count, disk size, latency/throughput, optional accuracy,
and optional FLOPs (count_flops) for a teacher/student pair in one call,
also reachable as Distiller.benchmark, Pruner.benchmark,
ChannelPruner.benchmark, and Quantizer.benchmark. The individual measuring
functions (measure_latency, count_parameters, estimate_model_size_mb,
get_process_ram_mb, get_device_memory_mb, count_flops) are also usable
standalone.
Modules:
| Name | Description |
|---|---|
accuracy |
|
benchmark |
|
flops |
FLOPs (floating point operations) counting for edge-deployment profiling. |
latency |
|
memory |
|
Classes:
| Name | Description |
|---|---|
BenchmarkReport |
Holds comparison results between Teacher and Student models. |
ModelProfile |
Container holding profiled metrics for an individual model. |
Profiler |
Benchmark runner comparing Teacher and Student architectures. |
Functions:
| Name | Description |
|---|---|
count_flops |
Counts the total FLOPs of one forward pass of |
count_parameters |
Counts total, trainable, and non-trainable parameters. |
estimate_model_size_mb |
Estimates serialized state dictionary size on disk in Megabytes (MB). |
get_device_memory_mb |
Returns the currently allocated memory on the specified hardware accelerator. |
get_process_ram_mb |
Returns current host RAM consumption of the running Python process. |
measure_latency |
Measures average inference latency and throughput (FPS / samples per second). |
Classes
BenchmarkReport
Holds comparison results between Teacher and Student models.
Methods:
| Name | Description |
|---|---|
show |
Renders an interactive formatted comparison table in the console. |
Source code in src/shrinkai/profiler/benchmark.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
Methods:
show
Renders an interactive formatted comparison table in the console.
Source code in src/shrinkai/profiler/benchmark.py
ModelProfile
dataclass
Container holding profiled metrics for an individual model.
Source code in src/shrinkai/profiler/benchmark.py
Profiler
Benchmark runner comparing Teacher and Student architectures.
Methods:
| Name | Description |
|---|---|
compare |
Executes complete profiling suite on both models and generates comparison. |
Source code in src/shrinkai/profiler/benchmark.py
Methods:
compare
staticmethod
compare(
teacher: Module,
student: Module,
sample_input: Tensor,
device: device | str = "auto",
teacher_name: str = "Teacher",
student_name: str = "Student",
teacher_acc: float | None = None,
student_acc: float | None = None,
compute_flops: bool = False,
) -> BenchmarkReport
Executes complete profiling suite on both models and generates comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
teacher
|
Module
|
Teacher PyTorch model. |
required |
student
|
Module
|
Student PyTorch model. |
required |
sample_input
|
Tensor
|
Representative tensor input batch. |
required |
device
|
device | str
|
Device target ('auto', 'mps', 'cuda', 'cpu'). |
'auto'
|
teacher_name
|
str
|
Display label for teacher. |
'Teacher'
|
student_name
|
str
|
Display label for student. |
'Student'
|
teacher_acc
|
float | None
|
Optional pre-computed teacher accuracy. |
None
|
student_acc
|
float | None
|
Optional pre-computed student accuracy. |
None
|
compute_flops
|
bool
|
If True, also counts and reports FLOPs per sample for
both models (see |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
BenchmarkReport |
BenchmarkReport
|
Structured report ready for |
Source code in src/shrinkai/profiler/benchmark.py
Functions:
count_flops
count_flops(
model: Module,
sample_input: Tensor | tuple[Tensor, ...],
device: device | str = "auto",
) -> int
Counts the total FLOPs of one forward pass of model on sample_input.
Uses torch.utils.flop_counter.FlopCounterMode, which instruments the actual
tensor operations dispatched during the forward pass, covering standard
layers (Conv, Linear, matmul, attention, ...) precisely, rather than a
hand-maintained per-layer-type formula. 1 multiply-add (MAC) is counted as 2
FLOPs, matching the usual convention.
Note
Custom/opaque kernels (e.g. the quantized ops produced by
shrinkai.compression.quantization.Quantizer) have no FLOPs formula
registered and are silently counted as 0. A warning is logged if the
total comes back as 0 despite the model having parameters, since that
usually signals an undercount rather than a genuinely free model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Model to profile. |
required |
sample_input
|
Tensor | tuple[Tensor, ...]
|
Representative input tensor (or tuple of tensors, for multi-input models) matching the model's forward signature. |
required |
device
|
device | str
|
Device to run the single, untimed forward pass on. FLOPs are a static property of the computation graph and do not depend on the device; this only needs to be a device the model can actually run on. |
'auto'
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total FLOPs for one forward pass on |
Source code in src/shrinkai/profiler/flops.py
count_parameters
Counts total, trainable, and non-trainable parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch model. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
dict[str, int]: Parameter count breakdown. |
Source code in src/shrinkai/profiler/memory.py
estimate_model_size_mb
Estimates serialized state dictionary size on disk in Megabytes (MB).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Estimated file size in MB. |
Source code in src/shrinkai/profiler/memory.py
get_device_memory_mb
Returns the currently allocated memory on the specified hardware accelerator.
This is crucial for edge AI profiling, as VRAM is often the primary bottleneck.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device | str
|
Computing device ('auto', 'cuda', 'mps', 'cpu', or torch.device). |
'auto'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Allocated accelerator memory in MB. Returns 0.0 for CPU
(use |
Source code in src/shrinkai/profiler/memory.py
get_process_ram_mb
Returns current host RAM consumption of the running Python process.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Resident memory in MB. |
Source code in src/shrinkai/profiler/memory.py
measure_latency
measure_latency(
model: Module,
sample_input: Tensor,
device: device,
num_runs: int = 100,
warmup_runs: int = 15,
) -> dict[str, float]
Measures average inference latency and throughput (FPS / samples per second).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Model to profile. |
required |
sample_input
|
Tensor
|
Input batch tensor matching inference shape. |
required |
device
|
device
|
Computing device. |
required |
num_runs
|
int
|
Number of timed inference iterations. |
100
|
warmup_runs
|
int
|
Initial iterations discarded to warm up hardware caches. |
15
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
dict[str, float]: Latency per sample (ms), per batch (ms), and throughput (FPS). |