Skip to content

Commit f208e89

Browse files
committed
build: prepare package for PyPI publishing
- complete project metadata: author (Adrian Szczepański), keywords, classifiers, Homepage/Issues URLs - add MIT LICENSE and declare license-files - adopt ruff (defaults + import sorting), wire lint into Tier 0 CI, apply initial formatting - add Trusted Publishing (OIDC) workflow, triggered on GitHub Release Verified: uv build + twine check pass; LICENSE bundled in wheel.
1 parent 0cc9382 commit f208e89

19 files changed

Lines changed: 269 additions & 58 deletions

.github/workflows/publish-pypi.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Publish to PyPI
2+
3+
# Trusted Publishing (OIDC) — no API token stored. Configure a PyPI publisher
4+
# for this repo/workflow first (see https://docs.pypi.org/trusted-publishers/).
5+
# Fires when a GitHub Release is published; the release tag must match the
6+
# version in pyproject.toml (e.g. tag v0.1.0 -> version 0.1.0).
7+
on:
8+
release:
9+
types: [published]
10+
workflow_dispatch:
11+
12+
jobs:
13+
publish:
14+
runs-on: ubuntu-latest
15+
environment: pypi
16+
permissions:
17+
id-token: write # required for Trusted Publishing
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- uses: astral-sh/setup-uv@v7
22+
with:
23+
enable-cache: true
24+
25+
- name: Build
26+
run: uv build
27+
28+
- name: Publish
29+
run: uv publish

.github/workflows/tier0.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ jobs:
2222
- name: uv sync
2323
run: uv sync
2424

25+
- name: Lint (ruff)
26+
run: |
27+
uv run ruff check .
28+
uv run ruff format --check .
29+
2530
- name: Run Tier 0
2631
run: uv run pytest -m unit tests/ -v

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-2026 Adrian Szczepański
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

coreml_diffusion/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
2020
because a saved workflow JSON references these strings verbatim.
2121
"""
22+
2223
from enum import Enum
2324

24-
from coreml_diffusion.model_version import ModelVersion
2525
from coreml_diffusion.attention import ATTENTION_IMPLEMENTATIONS
26+
from coreml_diffusion.model_version import ModelVersion
2627
from coreml_diffusion.naming import (
2728
QUANT_NBITS_VALUES,
2829
compose_out_name,
@@ -43,7 +44,7 @@
4344

4445

4546
class Status(Enum):
46-
VERIFIED = "verified" # has a golden anchor + passing [M2-ANE] check
47+
VERIFIED = "verified" # has a golden anchor + passing [M2-ANE] check
4748
EXPERIMENTAL = "experimental" # convertible, not yet anchored/verified
4849

4950

@@ -59,7 +60,7 @@ class Status(Enum):
5960
ModelVersion.SD15: Status.VERIFIED,
6061
ModelVersion.SDXL: Status.VERIFIED,
6162
ModelVersion.SDXL_REFINER: Status.EXPERIMENTAL, # -> VERIFIED after a refiner golden anchor
62-
ModelVersion.LCM: Status.EXPERIMENTAL, # -> VERIFIED after E-LCM golden anchor
63+
ModelVersion.LCM: Status.EXPERIMENTAL, # -> VERIFIED after E-LCM golden anchor
6364
}
6465

6566

coreml_diffusion/cli.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
coreml-diffusion convert --ckpt model.safetensors --model-version SD15 \\
1111
--out unet.mlpackage --height 512 --width 512 --attn-impl SPLIT_EINSUM
1212
"""
13+
1314
import argparse
1415

1516
import coreml_diffusion
@@ -52,7 +53,9 @@ def build_parser():
5253
sub = parser.add_subparsers(dest="command", required=True)
5354

5455
conv = sub.add_parser("convert", help="Convert a checkpoint's UNet to a .mlpackage")
55-
conv.add_argument("--ckpt", required=True, help="Path to the source .safetensors checkpoint")
56+
conv.add_argument(
57+
"--ckpt", required=True, help="Path to the source .safetensors checkpoint"
58+
)
5659
conv.add_argument(
5760
"--model-version",
5861
required=True,
@@ -62,9 +65,15 @@ def build_parser():
6265
help="Model architecture (verified: SD15, SDXL; experimental otherwise)",
6366
)
6467
conv.add_argument("--out", required=True, help="Output .mlpackage path to write")
65-
conv.add_argument("--height", type=int, default=512, help="Target image height (default 512)")
66-
conv.add_argument("--width", type=int, default=512, help="Target image width (default 512)")
67-
conv.add_argument("--batch-size", type=int, default=1, help="Batch size (default 1)")
68+
conv.add_argument(
69+
"--height", type=int, default=512, help="Target image height (default 512)"
70+
)
71+
conv.add_argument(
72+
"--width", type=int, default=512, help="Target image width (default 512)"
73+
)
74+
conv.add_argument(
75+
"--batch-size", type=int, default=1, help="Batch size (default 1)"
76+
)
6877
conv.add_argument(
6978
"--attn-impl",
7079
choices=coreml_diffusion.list_attention_impls(),
@@ -82,7 +91,9 @@ def build_parser():
8291
metavar="PATH[:STRENGTH]",
8392
help="LoRA to fuse before conversion; repeatable. STRENGTH defaults to 1.0",
8493
)
85-
conv.add_argument("--config", default=None, help="Optional original-config YAML path")
94+
conv.add_argument(
95+
"--config", default=None, help="Optional original-config YAML path"
96+
)
8697
conv.add_argument(
8798
"--quantize",
8899
choices=coreml_diffusion.list_quant_modes(),

coreml_diffusion/conversion/attention.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def apply_attention_implementation(unet, attention_implementation):
1919
unet.set_attn_processor(SplitEinsumV2AttnProcessor())
2020
return unet
2121

22-
raise ValueError(f"Unsupported attention implementation: {attention_implementation}")
22+
raise ValueError(
23+
f"Unsupported attention implementation: {attention_implementation}"
24+
)
2325

2426

2527
class SplitEinsumAttnProcessor:
@@ -80,14 +82,18 @@ def _attention_forward(
8082
input_ndim = hidden_states.ndim
8183
if input_ndim == 4:
8284
batch_size, channel, height, width = hidden_states.shape
83-
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
85+
hidden_states = hidden_states.view(
86+
batch_size, channel, height * width
87+
).transpose(1, 2)
8488
else:
8589
batch_size, _, channel = hidden_states.shape
8690
height = None
8791
width = None
8892

8993
batch_size, key_sequence_length, _ = (
90-
hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
94+
hidden_states.shape
95+
if encoder_hidden_states is None
96+
else encoder_hidden_states.shape
9197
)
9298

9399
if attention_mask is not None:

coreml_diffusion/convert.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
derives the trace timestep from an LCM scheduler) shares this single
99
implementation instead of keeping a near-duplicate copy.
1010
"""
11+
1112
import gc
1213
import os
1314
import time
@@ -18,12 +19,12 @@
1819
from diffusers import UNet2DConditionModel
1920

2021
from coreml_diffusion.attention import ATTENTION_IMPLEMENTATIONS
21-
from coreml_diffusion.model_version import ModelVersion
2222
from coreml_diffusion.conversion.attention import apply_attention_implementation
2323
from coreml_diffusion.conversion.shapes import conv2d_output_shape
2424
from coreml_diffusion.conversion.trace import prepare_unet_for_coreml_trace
2525
from coreml_diffusion.conversion.unet import CoreMLUNetWrapper
2626
from coreml_diffusion.logger import logger
27+
from coreml_diffusion.model_version import ModelVersion
2728

2829
DEFAULT_TRACE_TIMESTEP = 999.0
2930
TEXT_TOKEN_SEQUENCE_LENGTH = 77
@@ -97,7 +98,9 @@ def convert_to_coreml(
9798
return coreml_model
9899

99100

100-
def get_sample_input(batch_size, encoder_hidden_states_shape, sample_shape, scheduler=None):
101+
def get_sample_input(
102+
batch_size, encoder_hidden_states_shape, sample_shape, scheduler=None
103+
):
101104
"""Build the example inputs used to JIT-trace the UNet.
102105
103106
When ``scheduler`` is provided (the LCM path) the trace timestep is taken
@@ -106,7 +109,9 @@ def get_sample_input(batch_size, encoder_hidden_states_shape, sample_shape, sche
106109
graph — the random values are placeholders.
107110
"""
108111
timestep_value = (
109-
scheduler.timesteps[0].item() if scheduler is not None else DEFAULT_TRACE_TIMESTEP
112+
scheduler.timesteps[0].item()
113+
if scheduler is not None
114+
else DEFAULT_TRACE_TIMESTEP
110115
)
111116
sample_unet_inputs = dict(
112117
[
@@ -144,7 +149,10 @@ def sdxl_inputs(sample_unet_inputs, ref_unet, model_version):
144149
time_ids_list = list(original_size + crops_coords_top_left + target_size)
145150

146151
time_ids = torch.tensor(time_ids_list).repeat(batch_size, 1).to(torch.int64)
147-
text_embeds_shape = (batch_size, get_sdxl_text_embeds_dim(ref_unet, len(time_ids_list)))
152+
text_embeds_shape = (
153+
batch_size,
154+
get_sdxl_text_embeds_dim(ref_unet, len(time_ids_list)),
155+
)
148156

149157
return {
150158
"time_ids": time_ids,

coreml_diffusion/naming.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
cache key: every workflow that references a converted .mlpackage depends
66
on it staying byte-for-byte identical.
77
"""
8+
89
from typing import Iterable, Tuple
910

1011
ATTN_SUFFIX = {
@@ -52,7 +53,11 @@ def compose_out_name(
5253
)
5354
stem = ckpt_name.split(".")[0]
5455
sorted_names = sorted(lora_names)
55-
lora_str = "_" + "_".join(name.split(".")[0] for name in sorted_names) if sorted_names else ""
56+
lora_str = (
57+
"_" + "_".join(name.split(".")[0] for name in sorted_names)
58+
if sorted_names
59+
else ""
60+
)
5661
cn_suffix = "_cn" if controlnet_support else ""
5762
attn_suffix = "_" + ATTN_SUFFIX[attention_implementation]
5863
quant_suffix = f"_q{quantize_nbits}" if quantize_nbits != "none" else ""

pyproject.toml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@ name = "coreml-diffusion"
33
description = "Convert diffusion-model checkpoints (SD1.5/SDXL) to Core ML for Apple Neural Engine — framework-free, ComfyUI-independent."
44
version = "0.1.0"
55
license = "MIT"
6+
license-files = ["LICENSE"]
67
requires-python = ">=3.12,<3.13"
78
readme = "README.md"
8-
authors = [{ name = "aszc-dev" }]
9+
authors = [{ name = "Adrian Szczepański", email = "hi@aszc.dev" }]
10+
keywords = [
11+
"coreml",
12+
"core-ml",
13+
"diffusion",
14+
"stable-diffusion",
15+
"sdxl",
16+
"apple-neural-engine",
17+
"ane",
18+
"diffusers",
19+
"comfyui",
20+
]
21+
classifiers = [
22+
"Development Status :: 4 - Beta",
23+
"Intended Audience :: Developers",
24+
"Operating System :: MacOS",
25+
"Programming Language :: Python :: 3",
26+
"Programming Language :: Python :: 3.12",
27+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
28+
"Topic :: Multimedia :: Graphics :: Graphics Conversion",
29+
"Typing :: Typed",
30+
]
931
dependencies = [
1032
"torch>=2.7,<2.8",
1133
"coremltools>=9,<10",
@@ -20,7 +42,9 @@ dependencies = [
2042
coreml-diffusion = "coreml_diffusion.cli:main"
2143

2244
[project.urls]
45+
Homepage = "https://github.com/aszc-dev/coreml-diffusion"
2346
Repository = "https://github.com/aszc-dev/coreml-diffusion"
47+
Issues = "https://github.com/aszc-dev/coreml-diffusion/issues"
2448

2549
[build-system]
2650
requires = ["hatchling"]
@@ -30,7 +54,17 @@ build-backend = "hatchling.build"
3054
packages = ["coreml_diffusion"]
3155

3256
[dependency-groups]
33-
dev = ["pytest>=9.0.3"]
57+
dev = [
58+
"pytest>=9.0.3",
59+
"ruff>=0.14",
60+
]
61+
62+
[tool.ruff]
63+
target-version = "py312"
64+
65+
[tool.ruff.lint]
66+
# Ruff defaults (E, F) plus import sorting; ruff format does not sort imports.
67+
extend-select = ["I"]
3468

3569
[tool.pytest.ini_options]
3670
markers = [

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
tier is requested via ``-m``) skips the other tiers at collection time so Tier 0
55
on Linux never imports the Mac-only stack (coremltools) that Tier 1/2 pull in.
66
"""
7+
78
import pytest
89

910
_TIER_BY_DIR = {

0 commit comments

Comments
 (0)