Agent quickstart¶
Use TorchScan to inspect model cost, measure the owner's workload, check one optimization, and save the evidence. Install the development version for timing, process RSS, profiler diagnostics, and workload reports. The owner defines acceptable output quality and resource budgets.
Default workflow¶
- Import the existing model and construct representative inputs without downloading weights unless authorized.
- Use
crawl_module(..., args=..., kwargs=...)for a real call orinput_shapefor a simple tensor input. - Define a zero-argument workload that owns evaluation/training state, gradient mode, precision, transfers, and inputs. Configure device placement and threads before measurement. Timing repeats the callable; training must manage its state.
- Use
measure_workloadto assemble selected FLOPs, clean timing, tensor/allocator memory, and optional profiler passes. Measure RSS with an explicit fresh-process command that reconstructs the same configuration. - Compare baseline and candidate with
compare_benchmarks(..., check=...); preserve a failed check and withheld deltas. - Serialize reports as JSON and use
render_reportfor offline HTML. Check status and diagnostics before numbers; never scrapesummaryor workload terminal text. - Preserve model/input revisions, seeds, hardware/software, execution mode, precision, threads, and measurement boundaries. Use owner-supplied thresholds for budgets or pass/fail decisions.
Use strict=True when incomplete model metrics must stop the task; it raises IncompleteAnalysisError.
Pick one API¶
| Task | Use |
|---|---|
| Inspect module structure and formula metrics | crawl_module |
| Show a table to a person and retain the report | summary |
| Inspect shapes and parameters with less overhead | crawl_module(..., mode="structure") or summary(..., mode="structure") |
| Assemble selected workload resources and print a readable summary | measure_workload |
| Count operator FLOPs for arbitrary code | measure_flops |
| Measure one workload's PyTorch peak memory | measure_peak_memory |
| Measure warmed latency/IQR and declared work-unit throughput | measure_latency |
| Measure imports, loading, and execution in a fresh child | measure_peak_rss(command) |
| Investigate operators in a separate instrumented pass | profile_workload |
| Compare model cost estimates | compare_reports |
| Check workload performance with an output callback | compare_benchmarks |
| Consume a saved model or workload report offline | render_report |
summary returns an AnalysisReport from an evaluation forward with gradients disabled, restoring original training
flags. measure_workload and measure_latency return BenchmarkReport evidence and preserve callable side effects.
Model storage and formula counts do not predict latency or process RSS. Keep the two reports together;
do not add module and operator FLOPs or use compare_reports for timing.
Trust rules¶
complete: usevaluewith the report's method and context.partial:known_valueis only a lower bound. Preserve diagnostics and do not extrapolate.unavailable: report that no measurement was produced.- Numeric zero is meaningful only when status is
complete. - Module FLOPs and operator FLOPs are separate methods; never add or average them.
- Peak PyTorch memory is not process RSS or total accelerator use.
- A skipped or mocked CUDA/MPS check is not device validation.
- First-call latency includes all callable work; warmed block-average latency is not individual-request p95.
- Throughput uses the work completed by one call, such as 32 samples. It is not queued-service throughput.
- RSS, CPU tracked-tensor peaks, and accelerator allocated/reserved peaks have distinct scopes. Do not sum them.
- Profiler self times include instrumentation and may overlap. They are not clean latency.
- An output check establishes only its declared tolerance, not task accuracy. IQR labels are descriptive.
Reuse the checked experiment and its repository scripts. Locally initialized CNN/Transformer weights can validate runtime behavior without downloads; they cannot validate task accuracy or another device's speed.
Owner-controlled budgets¶
The owner supplies memory budgets and quality thresholds. Compare them with complete metrics of the matching scope; record hardware and workload state with accelerator results. Do not invent a default budget.
When an operator is uncounted¶
- Preserve the partial result and diagnostic.
- Confirm the operator and its exact overload in the installed PyTorch version.
- Rerun with
custom_mappingoncrawl_module,summary, ormeasure_flopsonly when the counting method is known and reviewable. Operator overrides stay scoped to that analysis. - Keep the formula with the experiment or project that owns the assumption.
For custom module/model estimates, pass custom_modules={ModuleType: ModuleHandler(callback)}. The callback receives
the actual complete call context and may supply each metric independently. Declare inclusive subtree ownership for
any estimate that includes child work; preserve diagnostics when fields are incomplete or callbacks fail.
See the copyable extension tutorial, Model and input support, and Methodology.