Tag PyTorch submodules and export them as Torch, TorchScript, or Core ML models.
Project description
torch_layer_segregator
Tag PyTorch submodules and export them as standalone Torch, TorchScript, or Core ML models.
Useful for profiling individual layers, benchmarking conversions, and building per-layer deployment pipelines without manually tracing each submodule.
Install
pip install torch_layer_segregator
Requirements: Python 3.10+, PyTorch 2.7+. Core ML export also requires coremltools 9.0+ on macOS.
Quick start
import torch.nn as nn
from torch_layer_segregator import TorchLayerSaver, make_saveable_layer
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.block = make_saveable_layer(nn.Conv2d(3, 16, 3, padding=1), key="conv_block")
def forward(self, x):
return self.block(x)
model = MyModel()
input_shape = (1, 3, 224, 224)
saver = TorchLayerSaver(model, "exports/my_model", input_shape, device="cpu")
saver.save_torch_model_layers()
saver.save_script_model_layers()
saver.save_coreml_model_layers()
How it works
- Tag layers — wrap submodules with
make_saveable_layerto mark them for export. - Infer shapes —
TorchLayerSaverruns one forward pass and records each tagged layer's input and output shapes. - Export — save tagged layers to disk in the format you need.
Tagged layers are written under subdirectories of your output path, each with a layer_details.json manifest.
| Method | Directory | Output |
|---|---|---|
save_torch_model_layers() |
torch_layers/ |
.pt (metadata + state_dict) |
save_script_model_layers() |
script_layers/ |
TorchScript .ts |
save_coreml_model_layers() |
coreml_layers/ |
Core ML .mlpackage |
If no key is passed to make_saveable_layer, the filename is derived from the module's dotted name (e.g. encoder.block0 → encoder_block0).
Core ML conversion traces each layer and exports an ML Program with float16 precision for all compute units (iOS 26+ deployment target).
API
make_saveable_layer(module, key=None, input_shape=None)
Mark a nn.Module for export. Returns the same module for inline use in __init__.
TorchLayerSaver(model, out_dir, input_shape, device="cpu")
Run shape inference and coordinate exports. input_shape is the full model input tuple (batch included). device is used for inference and scripted/Core ML export.
All save_* methods accept an optional out_dir to override the default subdirectory.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_layer_segregator-0.1.0.tar.gz.
File metadata
- Download URL: torch_layer_segregator-0.1.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
569eda9f661ee72ae80eeac44e0e73dea8754a6a6ed7e8762e1672a214a7b454
|
|
| MD5 |
978e447a220cfb8219124c16a9b771ad
|
|
| BLAKE2b-256 |
252a4a278f6fdcd6e775cb9f5e520e520b7e025f5d1eaecd08f7cdda051f524e
|
File details
Details for the file torch_layer_segregator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: torch_layer_segregator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af44ee4f466a1df9ace0c6840d1b3b73c94eafc023a7b7c66d8ec2e0b367189c
|
|
| MD5 |
e9cacf1cd4e61fe1a6db0296ce8f8326
|
|
| BLAKE2b-256 |
76c52c7892776466cdeeab30aaa5cba61455cce801aa562e6b049005733585c0
|