Skip to content
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
9ac3dbf
add torch export functions
isaaccorley Jul 9, 2025
34a39f0
reorganize to torch directories
isaaccorley Jul 11, 2025
d47dde3
update changelog
isaaccorley Jul 11, 2025
f23c95f
Merge branch 'main' into isaac/torch-export-packaging
isaaccorley Jul 22, 2025
e8e3b53
fix logger.debug callss
isaaccorley Jul 15, 2025
493cd17
change to package and export naming
isaaccorley Jul 15, 2025
6660f34
remove typing in docstring
isaaccorley Jul 22, 2025
8603034
skip torch tests if torch isn't installed
isaaccorley Jul 22, 2025
0e65956
update ruff pre-commit to latest
isaaccorley Jul 22, 2025
fe74306
add torch optional deps
isaaccorley Jul 22, 2025
82749cc
format and lint
isaaccorley Jul 22, 2025
d785e9e
add comment for test yaml
isaaccorley Jul 22, 2025
7c808c8
update dim shapes in ftw metadata yaml
isaaccorley Jul 22, 2025
2eb362b
refactor per suggestions
isaaccorley Jul 31, 2025
b3137f3
fix tmpdir logic
isaaccorley Jul 31, 2025
f370513
revert uv.lock
isaaccorley Jul 31, 2025
60b25a9
Merge branch 'main' into isaac/torch-export-packaging
isaaccorley Jul 31, 2025
ce14de7
remove ipywidgets and ipykernel deps
isaaccorley Jul 31, 2025
1b93af4
make mypy happy
isaaccorley Jul 31, 2025
d951a4e
update lock file
isaaccorley Jul 31, 2025
67f6a62
make mypy happier
isaaccorley Jul 31, 2025
5e289af
skip tests before torch is imported
isaaccorley Jul 31, 2025
0e827f7
update makefile with option for installing with extras
isaaccorley Jul 31, 2025
1297d9f
update lock file
isaaccorley Jul 31, 2025
b190f87
update tests to run appropriately based on python versions
isaaccorley Jul 31, 2025
4b12168
typing.Self not available in python 3.10
isaaccorley Jul 31, 2025
f7ca821
make mypy happy
isaaccorley Jul 31, 2025
4d43ab7
fix tests
isaaccorley Jul 31, 2025
b6730ef
add no transforms tests
isaaccorley Jul 31, 2025
f518601
mark tests as slow, add test all make command, correct make install d…
rbavery Aug 1, 2025
6c7f1a1
fix install-dev-extras, use test index for official release candidate…
rbavery Aug 1, 2025
27ba7e9
use cpu torch extra when linting
rbavery Aug 1, 2025
5613dbc
upgrade mypy, safety, fix mypy errors
rbavery Aug 1, 2025
69fafc8
Merge pull request #3 from wherobots/ryan/torch-export-ci-fixes
rbavery Aug 1, 2025
ea7e7af
remove logger level setting
isaaccorley Aug 4, 2025
cd1993a
move aoti_compile fn to utils
isaaccorley Aug 4, 2025
af805cd
refactor to use typedict and typealias
isaaccorley Aug 4, 2025
05d74e7
validate mlm-metadata from archive in tests
isaaccorley Aug 4, 2025
afc6430
add details for using multiple chained transforms with a nn.Sequential
isaaccorley Aug 4, 2025
210f765
update makefile linting logic
isaaccorley Aug 4, 2025
111ec97
remove precommit comment
isaaccorley Aug 4, 2025
79233a5
fix mypy errors
isaaccorley Aug 4, 2025
a7e86f3
revert formatting
isaaccorley Aug 4, 2025
e065232
str -> Path
isaaccorley Aug 4, 2025
867b92d
update to work on mac osx
isaaccorley Aug 5, 2025
f81e222
don't cache if python=3.10
isaaccorley Aug 5, 2025
f5e8ca2
do cache if python=3.10
isaaccorley Aug 5, 2025
7f93f52
lint needs torch extra because it uses uv run
isaaccorley Aug 5, 2025
9a2e367
add slow decorator back in
isaaccorley Aug 5, 2025
ebf1967
Merge branch 'main' into isaac/torch-export-packaging
isaaccorley Aug 6, 2025
c3cac56
update to torch 2.8
isaaccorley Aug 6, 2025
9560a1a
update lockfile
isaaccorley Aug 6, 2025
c7bfa1c
update makefile to install dev deps with make install-dev-extras
isaaccorley Aug 6, 2025
104d490
format and lint
isaaccorley Aug 6, 2025
c8b2bc1
mark cpu export tests as slow
isaaccorley Aug 6, 2025
05e4976
add torch export/package example to docs
isaaccorley Aug 6, 2025
4df7987
skip slow tests
isaaccorley Aug 6, 2025
b886444
cast paths to os.PathLike to make mypy happy
isaaccorley Aug 6, 2025
5493f29
remove .dev from torch cpu version
isaaccorley Aug 7, 2025
3a7ade8
dont omit torch tests from coverage
isaaccorley Aug 7, 2025
55558f3
pin to torch 2.8.0
isaaccorley Aug 7, 2025
2c7961f
add single layer export/package test
isaaccorley Aug 7, 2025
a4e415b
add coverage_conditional_plugin
isaaccorley Aug 7, 2025
bf4b3dc
add conditional converage plugin
isaaccorley Aug 7, 2025
22fd7d5
try omit if torch not installed
isaaccorley Aug 7, 2025
e619cc1
cond coverage plugin fix
isaaccorley Aug 7, 2025
4347dbe
keep coverage files separate per python version
isaaccorley Aug 7, 2025
747c2ec
Merge branch 'main' into isaac/torch-export-packaging
isaaccorley Aug 8, 2025
e40e97d
fix coverage error
isaaccorley Aug 8, 2025
b858864
refactor tests and override metadata based on dtype/device args
isaaccorley Aug 9, 2025
b91aab8
skip if torchgeo not installed
isaaccorley Aug 9, 2025
90bbe4a
Update README_STAC_MODEL.md
isaaccorley Aug 9, 2025
d2bc5ed
Update README_STAC_MODEL.md
isaaccorley Aug 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/stac-model.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ jobs:
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
env:
COVERAGE_FILE: .coverage.${{ matrix.python-version }}

steps:
- uses: actions/checkout@v4
Expand All @@ -24,12 +26,18 @@ jobs:
with:
path: .venv
key: venv-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}-${{ hashFiles('uv.lock') }}

- name: Install dependencies
run: make install-dev
if: ${{ matrix.python-version == '3.10' }}

- name: Install dependencies with extras
run: make install-dev-extras
if: ${{ matrix.python-version != '3.10' }}

- name: Run checks
run: |
make lint-all
if: ${{ matrix.python-version != '3.10' }}
run: make lint-all
Comment thread
isaaccorley marked this conversation as resolved.

- name: Run tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
- id: check-yaml
- id: end-of-file-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.12' # Use the latest version of ruff-pre-commit
rev: 'v0.12.4'
hooks:
- id: ruff
pass_filenames: false
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add torch export and packaging utilities for combining a model, transforms, and MLM schema
Comment thread
isaaccorley marked this conversation as resolved.
compliant metadata into a single `.pt2` archive.
- Add `cpu` device type to `mlm:accelerator` and corresponding `stac_model.runtime.AcceleratorEnum`.
- Add `pre_processing_function` and `post_processing_function` support as JSON array
of [Processing Expression](README.md#processing-expression) definitions respectively
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ install-dev: setup
$(UV_COMMAND) export --format requirements-txt -o requirements-dev.txt
$(UV_COMMAND) pip install --python "$(UV_PYTHON_ROOT)" -r requirements-dev.txt

.PHONY: install-dev-extras
install-dev-extras: setup
$(UV_COMMAND) export --format requirements-txt -o requirements-dev.txt
$(UV_COMMAND) pip install --python "$(UV_PYTHON_ROOT)" -e .[torch] -r requirements-dev.txt

.PHONY: pre-commit-install
pre-commit-install: setup
$(UV_COMMAND) run --no-sync --python "$(UV_PYTHON_ROOT)" pre-commit install
Expand All @@ -59,11 +64,12 @@ codestyle: setup
.PHONY: format
format: codestyle

#* Linting
#* Testing
.PHONY: test
test: setup
$(UV_COMMAND) run --no-sync --python "$(UV_PYTHON_ROOT)" pytest -c pyproject.toml -v --cov-report=html --cov=stac_model tests/
$(UV_COMMAND) run --no-sync --python "$(UV_PYTHON_ROOT)" pytest -c pyproject.toml -v --cov-report=html --cov=stac_model --cov-config pyproject.toml tests/

#* Linting
.PHONY: check
check: check-examples check-markdown check-lint check-mypy check-safety check-citation

Expand Down
56 changes: 56 additions & 0 deletions README_STAC_MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,62 @@ with open("examples/mlm-metadata.yaml", "r", encoding="utf-8") as f:
MLModelProperties.model_validate(metadata["properties"])
```

## Exporting and Packaging PyTorch Models, Transforms, and Model Metadata

As of PyTorch 2.8, and stac_model 1.5.0, you can now export and package PyTorch models, transforms,
Comment thread
isaaccorley marked this conversation as resolved.
Outdated
and model metadata using functions in `stac_model.torch.export`. Below is an example of exporting a
U-Net model pretrained on the [Fields of The World (FTW) dataset](https://fieldsofthe.world/) for
field boundary segmentation in Sentinel-2 satellite imagery using the [TorchGeo](https://github.com/microsoft/torchgeo) library.

> 📝 **Note:** To customize the metadata for your model you can use this [example](./tests/torch/metadata.yaml) as a template.

```python
import torch
import torchvision.transforms.v2 as T
from torchgeo.models import Unet_Weights, unet
from stac_model.torch.export import save

weights = Unet_Weights.SENTINEL2_3CLASS_FTW
transforms = torch.nn.Sequential(
T.Resize((256, 256)),
T.Normalize(mean=[0.0], std=[3000.0])
)
model = unet(weights=weights)

save(
output_file="ftw.pt2",
model=model, # Must be an nn.Module
transforms=transforms, # Must be an nn.Module
metadata_path="metadata.yaml", # Can be a metadata yaml or MLModelProperties object
Comment thread
isaaccorley marked this conversation as resolved.
Outdated
input_shape=[-1, 8, -1, -1], # -1 indicates a dynamic shaped dimension
device="cpu",
dtype=torch.float32,
aoti_compile_and_package=False, # True for AOTInductor compile otherwise use torch.export
)
```

The model, transforms, and metadata can then be loaded into an environment with only torch and stac_model as required dependencies like below:

```python
import yaml
from torch.export.pt2_archive._package import load_pt2

pt2 = load_pt2(archive_path)
metadata = yaml.safe_load(pt2.extra_files["mlm-metadata"])

# If exported with aoti_compile_and_package=True
model = pt2.aoti_runners["model"]
transforms = pt2.aoti_runners["transforms"]

# If exported with aoti_compile_and_package=False
model = pt2.exported_programs["model"].module()
transforms = pt2.exported_programs["transforms"].module()

# Inference
batch = ... # An input batch tensor
outputs = model(transforms(batch))
```

## 📈 Releases

You can see the list of available releases on the [GitHub Releases][github-releases] page.
Expand Down
Loading