Guidance for implementing CLI tools that perform inference using PyTorch models in native languages (C/C++/Rust). This skill should be used when tasks involve extracting weights from PyTorch .pth files, implementing neural network forward passes in C/C++, or creating standalone inference tools without Python dependencies.
This skill provides procedural guidance for creating command-line tools that perform inference using PyTorch models without Python dependencies. These tasks typically involve extracting weights from .pth files, implementing forward passes in C/C++, and handling image preprocessing correctly.
Examine the PyTorch model architecture
.pth file in Python to understand the model structureInspect input data format
Plan weight extraction format
[out_features, in_features]Create a weight extraction script
torch.load() with appropriate map_locationVerify extracted weights
Document weight ordering
Handle image loading and preprocessing
Implement forward pass
Add comprehensive error handling
Compare outputs with PyTorch reference
Test edge cases
Read back written files
PyTorch linear layers store weights as [out_features, in_features]. When implementing matrix multiplication in C/C++:
x of shape [in_features]W @ x where W is [out_features, in_features][out_features]gray = 0.299*R + 0.587*G + 0.114*BBefore considering the task complete:
weight_extraction_patterns.md - Common patterns for extracting weights from different PyTorch model typesverification_strategies.md - Detailed strategies for verifying native implementations against PyTorchScripts directory available for reusable extraction or verification utilities if needed.