highpairformer 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. highpairformer-0.1.0/.github/workflows/publish.yml +61 -0
  2. highpairformer-0.1.0/.gitignore +13 -0
  3. highpairformer-0.1.0/.python-version +1 -0
  4. highpairformer-0.1.0/LICENSE +161 -0
  5. highpairformer-0.1.0/PKG-INFO +175 -0
  6. highpairformer-0.1.0/README.md +145 -0
  7. highpairformer-0.1.0/docs/benchmarks/boltz2-mps.svg +1 -0
  8. highpairformer-0.1.0/docs/benchmarks/boltz2-mps.vg.json +207 -0
  9. highpairformer-0.1.0/pyproject.toml +74 -0
  10. highpairformer-0.1.0/src/highpairformer/__init__.py +76 -0
  11. highpairformer-0.1.0/src/highpairformer/_precision.py +27 -0
  12. highpairformer-0.1.0/src/highpairformer/benchmark.py +1050 -0
  13. highpairformer-0.1.0/src/highpairformer/jax/__init__.py +62 -0
  14. highpairformer-0.1.0/src/highpairformer/jax/_ops.py +375 -0
  15. highpairformer-0.1.0/src/highpairformer/jax/_runtime.py +2223 -0
  16. highpairformer-0.1.0/src/highpairformer/kernels/__init__.py +1 -0
  17. highpairformer-0.1.0/src/highpairformer/kernels/metal/__init__.py +27 -0
  18. highpairformer-0.1.0/src/highpairformer/kernels/metal/_shaders.py +991 -0
  19. highpairformer-0.1.0/src/highpairformer/mlx/__init__.py +41 -0
  20. highpairformer-0.1.0/src/highpairformer/mlx/_metal.py +62 -0
  21. highpairformer-0.1.0/src/highpairformer/mlx/_normalize.py +84 -0
  22. highpairformer-0.1.0/src/highpairformer/mlx/_tensorops.py +263 -0
  23. highpairformer-0.1.0/src/highpairformer/mlx/attention.py +78 -0
  24. highpairformer-0.1.0/src/highpairformer/mlx/swiglu.py +254 -0
  25. highpairformer-0.1.0/src/highpairformer/mlx/triattn.py +236 -0
  26. highpairformer-0.1.0/src/highpairformer/mlx/trimul.py +568 -0
  27. highpairformer-0.1.0/src/highpairformer/py.typed +0 -0
  28. highpairformer-0.1.0/src/highpairformer/torch/__init__.py +19 -0
  29. highpairformer-0.1.0/src/highpairformer/torch/_mps.py +479 -0
  30. highpairformer-0.1.0/src/highpairformer/torch/_ops.py +662 -0
  31. highpairformer-0.1.0/tests/__init__.py +0 -0
  32. highpairformer-0.1.0/tests/conftest.py +27 -0
  33. highpairformer-0.1.0/tests/test_benchmark.py +192 -0
  34. highpairformer-0.1.0/tests/test_jax.py +175 -0
  35. highpairformer-0.1.0/tests/test_mlx.py +285 -0
  36. highpairformer-0.1.0/tests/test_torch.py +242 -0
  37. highpairformer-0.1.0/uv.lock +976 -0
@@ -0,0 +1,61 @@
1
+ on:
2
+ pull_request:
3
+ release:
4
+ types: [published]
5
+ workflow_dispatch:
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ jobs:
11
+ build:
12
+ name: Build distribution
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v7.0.1
16
+
17
+ - name: Install uv
18
+ uses: astral-sh/setup-uv@v10.4.1
19
+
20
+ - name: Set up Python
21
+ run: uv python install 3.12
22
+
23
+ - name: Install dependencies (JAX, Equinox, PyTorch CPU, dev)
24
+ run: uv sync --locked --extra jax --extra equinox --extra torch
25
+
26
+ - name: Lint, typecheck, and test
27
+ run: |
28
+ uv run --no-sync ruff check src/ tests/
29
+ uv run --no-sync ty check src/
30
+ uv run --no-sync pytest -v --ignore=tests/test_mlx.py
31
+
32
+ - name: Build wheel and sdist
33
+ run: uv build
34
+
35
+ - name: Upload distribution artifacts
36
+ uses: actions/upload-artifact@v7.0.2
37
+ with:
38
+ name: release-dists
39
+ path: dist/
40
+
41
+ publish:
42
+ name: Publish to PyPI
43
+ if: github.event_name == 'release' || github.event_name == 'workflow_dispatch'
44
+ needs: build
45
+ runs-on: ubuntu-latest
46
+ environment:
47
+ name: pypi
48
+ url: https://pypi.org/p/highpairformer
49
+ permissions:
50
+ id-token: write
51
+ steps:
52
+ - name: Download distribution artifacts
53
+ uses: actions/download-artifact@v8.0.2
54
+ with:
55
+ name: release-dists
56
+ path: dist/
57
+
58
+ - name: Publish package distributions to PyPI
59
+ uses: pypa/gh-action-pypi-publish@v1.14.3
60
+ with:
61
+ packages-dir: dist/
@@ -0,0 +1,13 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ .pytest_cache/
5
+ .ruff_cache/
6
+ .ty_cache/
7
+ .env
8
+ bench.jsonl
9
+ /.benchmarks/
10
+ dist/
11
+ build/
12
+ *.egg-info/
13
+ .DS_Store
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,161 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to the Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by the Licensor or its
58
+ representatives for the purpose of discussing and improving the Work,
59
+ but excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the Licensor as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the Work; and
99
+
100
+ (c) You must retain, in the Source form of the Work that You
101
+ distribute, all copyright notices, patent notices, and
102
+ trademark notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Work; and
105
+
106
+ (d) If the Work includes a "Notice" file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a copy of the Notice notice in the Notice file.
109
+
110
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
111
+ any Contribution intentionally submitted for inclusion in the Work
112
+ by You to the Licensor shall be under the terms and conditions of
113
+ this License, without any additional terms or conditions.
114
+ Notwithstanding the above, nothing herein shall supersede or modify
115
+ the terms of any separate license agreement you may have with
116
+ Licensor.
117
+
118
+ 6. Trademarks. This License does not grant permission to use the trade
119
+ names, trademarks, or service names of the Work, except as required
120
+ for reasonable and customary use in describing the origin of the Work
121
+ and reproducing the content of the Work.
122
+
123
+ 7. Disclaimer of Warranty. Unless required by applicable law or
124
+ applicable law or agreed to in writing, Licensor provides the Work
125
+ (and each Contributor provides its Contributions) on an "AS IS" BASIS,
126
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
127
+ implied, including, without limitation, any warranties or conditions
128
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A
129
+ PARTICULAR PURPOSE. You are solely responsible for the Work.
130
+
131
+ 8. Limitation of Liability. In no event and under no legal theory,
132
+ whether in tort (including negligence), contract, or otherwise,
133
+ unless required by applicable law (such as deliberate and gross
134
+ negligence) or agreed to in writing, shall any Contributor be liable
135
+ to You for damages, including any direct, indirect, special,
136
+ incidental, or consequential damages of any character arising as a
137
+ result of this License or out of the Work.
138
+
139
+ 9. Accepting Warranty or Additional Liability. While redistributing
140
+ the Work or Derivative Works thereof, You may choose to offer,
141
+ and offer to sell, support, and distribute support, indemnity,
142
+ or other liability obligations and/or rights consistent with this
143
+ License. However, in accepting such obligations, You may act only
144
+ on Your own behalf and on Your own responsibility, and not on behalf
145
+ of any other Contributor.
146
+
147
+ END OF TERMS AND CONDITIONS
148
+
149
+ Copyright 2026 Revanta, Inc.
150
+
151
+ Licensed under the Apache License, Version 2.0 (the "License");
152
+ you may not use this file except in compliance with the License.
153
+ You may obtain a copy of the License at
154
+
155
+ http://www.apache.org/licenses/LICENSE-2.0
156
+
157
+ Unless required by applicable law or agreed to in writing, software
158
+ distributed under the License is distributed on an "AS IS" BASIS,
159
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
160
+ See the License for the specific language governing permissions and
161
+ limitations under the License.
@@ -0,0 +1,175 @@
1
+ Metadata-Version: 2.5
2
+ Name: highpairformer
3
+ Version: 0.1.0
4
+ Summary: Drop-in Apple Silicon Metal/MLX replacement for cuEquivariance triangle operations across JAX, PyTorch, and MLX.
5
+ Project-URL: Homepage, https://github.com/Revanta-Inc/highpairformer
6
+ Project-URL: Repository, https://github.com/Revanta-Inc/highpairformer
7
+ Project-URL: Issues, https://github.com/Revanta-Inc/highpairformer/issues
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Keywords: alphafold,apple-silicon,cuequivariance,jax,metal,mlx,mps,pairformer,pytorch
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Operating System :: MacOS :: MacOS X
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
19
+ Requires-Python: >=3.12
20
+ Requires-Dist: numpy>=2.0
21
+ Provides-Extra: equinox
22
+ Requires-Dist: equinox>=0.13; extra == 'equinox'
23
+ Provides-Extra: jax
24
+ Requires-Dist: jax>=0.9; extra == 'jax'
25
+ Provides-Extra: mlx
26
+ Requires-Dist: mlx>=0.32.5; (platform_system == 'Darwin' and platform_machine == 'arm64') and extra == 'mlx'
27
+ Provides-Extra: torch
28
+ Requires-Dist: torch>=2.6; extra == 'torch'
29
+ Description-Content-Type: text/markdown
30
+
31
+ # highpairformer
32
+
33
+ Apple Silicon Metal kernels for biomolecular pairformer models — a drop-in replacement for NVIDIA `cuequivariance-jax` and `cuequivariance-torch` on macOS, with native **JAX**, **PyTorch (MPS)**, and **MLX** frontends.
34
+
35
+ ## Features
36
+
37
+ - **Drop-in `cuequivariance` ops**:
38
+ - `triangle_attention` — FlashAttention-style triangle attention with pair bias and mask
39
+ - `triangle_multiplicative_update` — outgoing and incoming triangle multiplicative updates with fused FP16 packing, FP32-accumulated BMM (overflow-safe past `65504`), and fused output gating
40
+ - `biased_attention` — pair-biased multi-head self-attention
41
+ - `swiglu_transition` — fused LayerNorm + SwiGLU up/gate projection + down GEMM with optional residual addition
42
+ - **Three first-class frontends** sharing one MSL kernel library (`highpairformer.kernels.metal`):
43
+ - **JAX (`highpairformer` / `highpairformer.jax`)**: drop-in `cuequivariance_jax` API plus a pure-Python Jaxpr-to-MLX runtime (`jit`, `filter_jit`, `grad`, `value_and_grad`) running forward passes on custom Metal kernels and standard JAX reverse-mode autodiff (`jax.grad`, `jax.checkpoint`, `lax.scan`) on Apple GPU without `jax-mps`.
44
+ - **PyTorch (`highpairformer.torch`)**: drop-in `cuequivariance_torch` API backed by `torch.mps.compile_shader` on MPS with `torch.autograd` support (zero MLX dependency) and automatic CPU/non-MPS fallback.
45
+ - **MLX (`highpairformer.mlx`)**: zero-overhead native MLX kernels with VJP, JVP, and `vmap` support.
46
+
47
+ ## Requirements
48
+
49
+ - **Hardware**: Apple Silicon (`arm64` — M1 or later)
50
+ - **Python**: 3.12 or newer
51
+
52
+ ## Installation
53
+
54
+ Install only the framework extra you need:
55
+
56
+ ```bash
57
+ # JAX on Apple Silicon (uses MLX Metal backend)
58
+ uv add "highpairformer[jax,mlx]"
59
+ # Optional: Equinox filter_jit support
60
+ uv add "highpairformer[jax,mlx,equinox]"
61
+
62
+ # PyTorch on Apple Silicon (MPS Metal shaders)
63
+ uv add "highpairformer[torch]"
64
+
65
+ # Native MLX only
66
+ uv add "highpairformer[mlx]"
67
+ ```
68
+
69
+ ## Quick Start
70
+
71
+ ### JAX (`cuequivariance_jax` drop-in)
72
+
73
+ ```python
74
+ import highpairformer as cueq # or: import highpairformer.jax as cueq
75
+
76
+ @cueq.jit
77
+ def pair_block(q, k, v, bias, mask):
78
+ out, lse, amax = cueq.triangle_attention(q, k, v, bias, mask=mask)
79
+ return out
80
+ ```
81
+
82
+ Reverse-mode autodiff works out of the box with `cueq.grad` / `cueq.value_and_grad` (or standard `jax.grad` inside `cueq.jit`):
83
+
84
+ ```python
85
+ loss, grads = cueq.value_and_grad(loss_fn)(params, x)
86
+ ```
87
+
88
+ ### PyTorch (`cuequivariance_torch` drop-in)
89
+
90
+ ```python
91
+ import torch
92
+ import highpairformer.torch as cueq
93
+
94
+ device = torch.device("mps")
95
+ out = cueq.triangle_attention(q, k, v, bias, mask=mask)
96
+ out.sum().backward()
97
+ # Pass return_aux=True for (out, lse, amax)
98
+ ```
99
+
100
+ ### Native MLX
101
+
102
+ ```python
103
+ import highpairformer.mlx as hp
104
+
105
+ out, lse, amax = hp.triangle_attention(q, k, v, bias, mask=mask)
106
+ ```
107
+
108
+ `hp.adaptive_layer_norm(x, scale, shift)` normalizes the last axis and applies a conditioning scale and shift broadcastable to `x`. A denoiser can supply `mx.sigmoid(scale_logits)` for a sigmoid gate or `1 + delta` for residual scaling. The caller can pack the conditioning projections across layers and compose this operation with `hp.biased_attention`.
109
+
110
+ ## Performance Tips: Native Layouts & Fusion
111
+
112
+ While `highpairformer` defaults to `cuequivariance`-compatible shapes so existing models run as a drop-in replacement, using its native layouts and fused residual options avoids redundant memory traffic on Apple Silicon's unified memory:
113
+
114
+ - **Prefer `layout="BNSHD"` over `layout="BNHSD"` in `triangle_attention`**:
115
+ `cuequivariance` expects `BNHSD` (`[B, N, H, S, D]`), which forces callers to transpose `Q`, `K`, and `V` out of linear projection order (`[B, N, S, H, D]`) before attention and transpose the output back afterward — four full `O(N^2 H D)` pair-tensor copies per layer. `highpairformer`'s Metal attention kernel reads and writes `BNSHD` (`[B, N, S, H, D]`) directly with zero transpose:
116
+
117
+ ```python
118
+ # Zero-transpose path (no BNHSD permutes before or after attention):
119
+ out, lse, amax = cueq.triangle_attention(q, k, v, bias, mask=mask, layout="BNSHD")
120
+ ```
121
+
122
+ - **Skip unused softmax statistics**:
123
+ During inference, use `biased_attention` (always output-only `BNSHD`) or keep `return_aux=False` in `highpairformer.torch.triangle_attention`. Both compile the Metal kernel with `HPF_OUTPUT_ONLY`, avoiding allocating and writing `lse` and `amax` and unlocking wider query tiles (`BQ=64`).
124
+
125
+ - **Fuse residual additions**:
126
+ Pass `residual=True` to `triangle_multiplicative_update` (and `update=...`, `residual=True` to `swiglu_transition`) to fold residual additions into the LayerNorm load and output GEMM epilogue instead of launching separate elementwise passes over the pair tensor.
127
+
128
+ ## Benchmarks
129
+
130
+ ![Median resident FP32 Boltz-2 trunk and diffusion time on Apple M1 Max. For GFP, PyTorch MPS took 36.37 seconds and the MLX adapter took 21.67 seconds. For pancreatic α-amylase, they took 173.75 and 74.19 seconds, respectively.](docs/benchmarks/boltz2-mps.svg)
131
+
132
+ Both backends predicted single chains of GFP (238 residues) and mature human pancreatic α-amylase (496 residues, positions 16–511 of AMY2A). Each prediction used FP32, `--accelerator mps`, three recycles, 200 denoising steps, one diffusion sample, and an empty MSA. [GFP sequence](https://www.uniprot.org/uniprotkb/P42212/entry) · [α-amylase sequence](https://www.uniprot.org/uniprotkb/P04746/entry)
133
+
134
+ Each bar shows median trunk plus diffusion time with the model and input already resident. Stage lengths average the two observations that determine the median. Each backend contributed four repeats for GFP and two for α-amylase. [Boltz-2 benchmark revision](https://github.com/Novel-Therapeutics/boltz-community/commit/d91963a31900df736accc2eaf6e722b8ebf9f586) · [Vega source and all 12 timings](docs/benchmarks/boltz2-mps.vg.json)
135
+
136
+ The yellow bars measure the complete Boltz adapter. The trunk calls highpairformer's triangle operations. The diffusion token stack runs in MLX with packed projections and cached attention bias. On GFP, switching from native MLX primitives to highpairformer reduced the median token-stack call from 31.80 to 30.26 ms over 20 repeats. Across four matched seeds the full sampler took 15.20 s with native MLX and 15.28 s with highpairformer. Each won two pairs. [Boltz adapter](https://github.com/monken/boltz-community/tree/feat/highpairformer-mlx)
137
+
138
+ ### Kernel benchmark suite
139
+
140
+ Run the standardized Apple Silicon Metal benchmark suite directly via `uvx` / `uv tool run`:
141
+
142
+ ```bash
143
+ uv tool run --from "highpairformer[mlx]" highpairformer-benchmark
144
+ # or:
145
+ uvx --from "highpairformer[mlx]" highpairformer-benchmark
146
+ ```
147
+
148
+ The benchmark sweeps real biomolecular pairformer shapes across `N ∈ {256, 384, 512}`, `c_z ∈ {64, 128, 256}`, and `float16`/`bfloat16` — covering Boltz-2 / AlphaFold3 (`c_z=128`, `H=4×32`, `c_s=384`) and ESMFold2 / Protenix-v2 (`c_z=256`, `H=8×32`, `c_token=768`) layouts (`BNSHD` and `BNHSD`), `biased_attention`, `triangle_multiplicative_update`, `swiglu_transition`, and full `pairformer_block` presets. It collects sanitized hardware metadata via `system_profiler SPHardwareDataType` (never sending serial numbers or raw UUIDs, and deriving a deterministic scrypt-hashed `hardware_uuid_hash` for deduplication) and warns if macOS Low Power Mode is enabled.
149
+
150
+ Results stay local by default. Useful flags:
151
+ - `--publish`: opt in to upload the sanitized benchmark report to `https://highpairformer.revanta.xyz/v1/benchmarks`.
152
+ - `--compare reference` (default): choose comma-separated baselines (`reference`, `cpu`, `reference,cpu`, or `none`).
153
+ - `--quick`: smaller grid (`N=128, 256`, `float16`, `c_z=128`).
154
+ - `--jsonl`: stream JSONL records (`meta`, `result`, `summary`) directly to stdout instead of a table.
155
+ - `--output results.jsonl` / `--dry-run`: stream JSONL to a file or print the full JSONL report.
156
+
157
+ ## Architecture
158
+
159
+ ```text
160
+ src/highpairformer/
161
+ ├── _precision.py # Shared Precision enum (cuequivariance-compatible)
162
+ ├── kernels/metal/ # Shared MSL kernel source (_shaders.py)
163
+ ├── mlx/ # Native MLX frontend (mx.fast.metal_kernel)
164
+ ├── jax/ # JAX cueq ops + Jaxpr-to-MLX runtime & autodiff
165
+ └── torch/ # PyTorch cueq ops + torch.mps.compile_shader backend
166
+ ```
167
+
168
+ ## Environment Variables
169
+
170
+ - `HIGHPAIRFORMER_MLX=0`: disable the JAX-to-MLX runtime and fall back to standard JAX execution.
171
+ - `HIGHPAIRFORMER_MLX_FP16=0`: disable fused FP16 Metal kernels in the JAX runtime (keep native BF16/FP32).
172
+
173
+ ## License
174
+
175
+ Apache License 2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,145 @@
1
+ # highpairformer
2
+
3
+ Apple Silicon Metal kernels for biomolecular pairformer models — a drop-in replacement for NVIDIA `cuequivariance-jax` and `cuequivariance-torch` on macOS, with native **JAX**, **PyTorch (MPS)**, and **MLX** frontends.
4
+
5
+ ## Features
6
+
7
+ - **Drop-in `cuequivariance` ops**:
8
+ - `triangle_attention` — FlashAttention-style triangle attention with pair bias and mask
9
+ - `triangle_multiplicative_update` — outgoing and incoming triangle multiplicative updates with fused FP16 packing, FP32-accumulated BMM (overflow-safe past `65504`), and fused output gating
10
+ - `biased_attention` — pair-biased multi-head self-attention
11
+ - `swiglu_transition` — fused LayerNorm + SwiGLU up/gate projection + down GEMM with optional residual addition
12
+ - **Three first-class frontends** sharing one MSL kernel library (`highpairformer.kernels.metal`):
13
+ - **JAX (`highpairformer` / `highpairformer.jax`)**: drop-in `cuequivariance_jax` API plus a pure-Python Jaxpr-to-MLX runtime (`jit`, `filter_jit`, `grad`, `value_and_grad`) running forward passes on custom Metal kernels and standard JAX reverse-mode autodiff (`jax.grad`, `jax.checkpoint`, `lax.scan`) on Apple GPU without `jax-mps`.
14
+ - **PyTorch (`highpairformer.torch`)**: drop-in `cuequivariance_torch` API backed by `torch.mps.compile_shader` on MPS with `torch.autograd` support (zero MLX dependency) and automatic CPU/non-MPS fallback.
15
+ - **MLX (`highpairformer.mlx`)**: zero-overhead native MLX kernels with VJP, JVP, and `vmap` support.
16
+
17
+ ## Requirements
18
+
19
+ - **Hardware**: Apple Silicon (`arm64` — M1 or later)
20
+ - **Python**: 3.12 or newer
21
+
22
+ ## Installation
23
+
24
+ Install only the framework extra you need:
25
+
26
+ ```bash
27
+ # JAX on Apple Silicon (uses MLX Metal backend)
28
+ uv add "highpairformer[jax,mlx]"
29
+ # Optional: Equinox filter_jit support
30
+ uv add "highpairformer[jax,mlx,equinox]"
31
+
32
+ # PyTorch on Apple Silicon (MPS Metal shaders)
33
+ uv add "highpairformer[torch]"
34
+
35
+ # Native MLX only
36
+ uv add "highpairformer[mlx]"
37
+ ```
38
+
39
+ ## Quick Start
40
+
41
+ ### JAX (`cuequivariance_jax` drop-in)
42
+
43
+ ```python
44
+ import highpairformer as cueq # or: import highpairformer.jax as cueq
45
+
46
+ @cueq.jit
47
+ def pair_block(q, k, v, bias, mask):
48
+ out, lse, amax = cueq.triangle_attention(q, k, v, bias, mask=mask)
49
+ return out
50
+ ```
51
+
52
+ Reverse-mode autodiff works out of the box with `cueq.grad` / `cueq.value_and_grad` (or standard `jax.grad` inside `cueq.jit`):
53
+
54
+ ```python
55
+ loss, grads = cueq.value_and_grad(loss_fn)(params, x)
56
+ ```
57
+
58
+ ### PyTorch (`cuequivariance_torch` drop-in)
59
+
60
+ ```python
61
+ import torch
62
+ import highpairformer.torch as cueq
63
+
64
+ device = torch.device("mps")
65
+ out = cueq.triangle_attention(q, k, v, bias, mask=mask)
66
+ out.sum().backward()
67
+ # Pass return_aux=True for (out, lse, amax)
68
+ ```
69
+
70
+ ### Native MLX
71
+
72
+ ```python
73
+ import highpairformer.mlx as hp
74
+
75
+ out, lse, amax = hp.triangle_attention(q, k, v, bias, mask=mask)
76
+ ```
77
+
78
+ `hp.adaptive_layer_norm(x, scale, shift)` normalizes the last axis and applies a conditioning scale and shift broadcastable to `x`. A denoiser can supply `mx.sigmoid(scale_logits)` for a sigmoid gate or `1 + delta` for residual scaling. The caller can pack the conditioning projections across layers and compose this operation with `hp.biased_attention`.
79
+
80
+ ## Performance Tips: Native Layouts & Fusion
81
+
82
+ While `highpairformer` defaults to `cuequivariance`-compatible shapes so existing models run as a drop-in replacement, using its native layouts and fused residual options avoids redundant memory traffic on Apple Silicon's unified memory:
83
+
84
+ - **Prefer `layout="BNSHD"` over `layout="BNHSD"` in `triangle_attention`**:
85
+ `cuequivariance` expects `BNHSD` (`[B, N, H, S, D]`), which forces callers to transpose `Q`, `K`, and `V` out of linear projection order (`[B, N, S, H, D]`) before attention and transpose the output back afterward — four full `O(N^2 H D)` pair-tensor copies per layer. `highpairformer`'s Metal attention kernel reads and writes `BNSHD` (`[B, N, S, H, D]`) directly with zero transpose:
86
+
87
+ ```python
88
+ # Zero-transpose path (no BNHSD permutes before or after attention):
89
+ out, lse, amax = cueq.triangle_attention(q, k, v, bias, mask=mask, layout="BNSHD")
90
+ ```
91
+
92
+ - **Skip unused softmax statistics**:
93
+ During inference, use `biased_attention` (always output-only `BNSHD`) or keep `return_aux=False` in `highpairformer.torch.triangle_attention`. Both compile the Metal kernel with `HPF_OUTPUT_ONLY`, avoiding allocating and writing `lse` and `amax` and unlocking wider query tiles (`BQ=64`).
94
+
95
+ - **Fuse residual additions**:
96
+ Pass `residual=True` to `triangle_multiplicative_update` (and `update=...`, `residual=True` to `swiglu_transition`) to fold residual additions into the LayerNorm load and output GEMM epilogue instead of launching separate elementwise passes over the pair tensor.
97
+
98
+ ## Benchmarks
99
+
100
+ ![Median resident FP32 Boltz-2 trunk and diffusion time on Apple M1 Max. For GFP, PyTorch MPS took 36.37 seconds and the MLX adapter took 21.67 seconds. For pancreatic α-amylase, they took 173.75 and 74.19 seconds, respectively.](docs/benchmarks/boltz2-mps.svg)
101
+
102
+ Both backends predicted single chains of GFP (238 residues) and mature human pancreatic α-amylase (496 residues, positions 16–511 of AMY2A). Each prediction used FP32, `--accelerator mps`, three recycles, 200 denoising steps, one diffusion sample, and an empty MSA. [GFP sequence](https://www.uniprot.org/uniprotkb/P42212/entry) · [α-amylase sequence](https://www.uniprot.org/uniprotkb/P04746/entry)
103
+
104
+ Each bar shows median trunk plus diffusion time with the model and input already resident. Stage lengths average the two observations that determine the median. Each backend contributed four repeats for GFP and two for α-amylase. [Boltz-2 benchmark revision](https://github.com/Novel-Therapeutics/boltz-community/commit/d91963a31900df736accc2eaf6e722b8ebf9f586) · [Vega source and all 12 timings](docs/benchmarks/boltz2-mps.vg.json)
105
+
106
+ The yellow bars measure the complete Boltz adapter. The trunk calls highpairformer's triangle operations. The diffusion token stack runs in MLX with packed projections and cached attention bias. On GFP, switching from native MLX primitives to highpairformer reduced the median token-stack call from 31.80 to 30.26 ms over 20 repeats. Across four matched seeds the full sampler took 15.20 s with native MLX and 15.28 s with highpairformer. Each won two pairs. [Boltz adapter](https://github.com/monken/boltz-community/tree/feat/highpairformer-mlx)
107
+
108
+ ### Kernel benchmark suite
109
+
110
+ Run the standardized Apple Silicon Metal benchmark suite directly via `uvx` / `uv tool run`:
111
+
112
+ ```bash
113
+ uv tool run --from "highpairformer[mlx]" highpairformer-benchmark
114
+ # or:
115
+ uvx --from "highpairformer[mlx]" highpairformer-benchmark
116
+ ```
117
+
118
+ The benchmark sweeps real biomolecular pairformer shapes across `N ∈ {256, 384, 512}`, `c_z ∈ {64, 128, 256}`, and `float16`/`bfloat16` — covering Boltz-2 / AlphaFold3 (`c_z=128`, `H=4×32`, `c_s=384`) and ESMFold2 / Protenix-v2 (`c_z=256`, `H=8×32`, `c_token=768`) layouts (`BNSHD` and `BNHSD`), `biased_attention`, `triangle_multiplicative_update`, `swiglu_transition`, and full `pairformer_block` presets. It collects sanitized hardware metadata via `system_profiler SPHardwareDataType` (never sending serial numbers or raw UUIDs, and deriving a deterministic scrypt-hashed `hardware_uuid_hash` for deduplication) and warns if macOS Low Power Mode is enabled.
119
+
120
+ Results stay local by default. Useful flags:
121
+ - `--publish`: opt in to upload the sanitized benchmark report to `https://highpairformer.revanta.xyz/v1/benchmarks`.
122
+ - `--compare reference` (default): choose comma-separated baselines (`reference`, `cpu`, `reference,cpu`, or `none`).
123
+ - `--quick`: smaller grid (`N=128, 256`, `float16`, `c_z=128`).
124
+ - `--jsonl`: stream JSONL records (`meta`, `result`, `summary`) directly to stdout instead of a table.
125
+ - `--output results.jsonl` / `--dry-run`: stream JSONL to a file or print the full JSONL report.
126
+
127
+ ## Architecture
128
+
129
+ ```text
130
+ src/highpairformer/
131
+ ├── _precision.py # Shared Precision enum (cuequivariance-compatible)
132
+ ├── kernels/metal/ # Shared MSL kernel source (_shaders.py)
133
+ ├── mlx/ # Native MLX frontend (mx.fast.metal_kernel)
134
+ ├── jax/ # JAX cueq ops + Jaxpr-to-MLX runtime & autodiff
135
+ └── torch/ # PyTorch cueq ops + torch.mps.compile_shader backend
136
+ ```
137
+
138
+ ## Environment Variables
139
+
140
+ - `HIGHPAIRFORMER_MLX=0`: disable the JAX-to-MLX runtime and fall back to standard JAX execution.
141
+ - `HIGHPAIRFORMER_MLX_FP16=0`: disable fused FP16 Metal kernels in the JAX runtime (keep native BF16/FP32).
142
+
143
+ ## License
144
+
145
+ Apache License 2.0. See [LICENSE](LICENSE).
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" class="marks" width="910" height="294" viewBox="0 0 910 294"><rect width="910" height="294" fill="#ffffff"/><g fill="none" stroke-miterlimit="4" transform="translate(0,0)"><g class="mark-group role-frame root" role="graphics-object" aria-roledescription="group mark container"><g transform="translate(0,0)"><path class="background" aria-hidden="true" d="M0,0h910v294h-910Z"/><g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="start" transform="translate(26,38)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="28px" font-weight="700" fill="#17212f">Boltz-2 trunk and diffusion time</text></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="start" transform="translate(26,64)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="14px" fill="#4c596c">Resident FP32 model · Apple M1 Max · 200 denoising steps · One diffusion sample</text></g><g class="mark-rect role-mark" aria-hidden="true"><path d="M28,81L37,81C38.103830048988,81,39,81.896169951012,39,83L39,92C39,93.103830048988,38.103830048988,94,37,94L28,94C26.896169951012,94,26,93.103830048988,26,92L26,83C26,81.896169951012,26.896169951012,81,28,81Z" fill="#a0a8b4" stroke="#768291" stroke-width="0.75"/><path d="M162,81L171,81C172.103830048988,81,173,81.896169951012,173,83L173,92C173,93.103830048988,172.103830048988,94,171,94L162,94C160.896169951012,94,160,93.103830048988,160,92L160,83C160,81.896169951012,160.896169951012,81,162,81Z" fill="#f9c83e" stroke="#a87912" stroke-width="0.75"/></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="start" transform="translate(46,92)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="13px" font-weight="600" fill="#283547">PyTorch MPS</text><text text-anchor="start" transform="translate(180,92)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="13px" font-weight="600" fill="#283547">MLX adapter with highpairformer</text></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="start" transform="translate(26,119)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" font-weight="700" fill="#4c596c">PROTEIN</text><text text-anchor="start" transform="translate(207,119)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" font-weight="700" fill="#4c596c">TRUNK + DIFFUSION (SECONDS)</text><text text-anchor="middle" transform="translate(855,119)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" font-weight="700" fill="#4c596c">SPEEDUP</text></g><g class="mark-rule role-mark" aria-hidden="true"><line transform="translate(207,130)" x2="0" y2="130" stroke="#a0a8b4"/><line transform="translate(319.20000000000005,130)" x2="0" y2="130" stroke="#e2e6eb"/><line transform="translate(431.40000000000003,130)" x2="0" y2="130" stroke="#e2e6eb"/><line transform="translate(543.5999999999999,130)" x2="0" y2="130" stroke="#e2e6eb"/><line transform="translate(655.8000000000001,130)" x2="0" y2="130" stroke="#e2e6eb"/><line transform="translate(768,130)" x2="0" y2="130" stroke="#e2e6eb"/></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="middle" transform="translate(207,278)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" fill="#4c596c">0</text><text text-anchor="middle" transform="translate(319.20000000000005,278)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" fill="#4c596c">40</text><text text-anchor="middle" transform="translate(431.40000000000003,278)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" fill="#4c596c">80</text><text text-anchor="middle" transform="translate(543.5999999999999,278)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" fill="#4c596c">120</text><text text-anchor="middle" transform="translate(655.8000000000001,278)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" fill="#4c596c">160</text><text text-anchor="middle" transform="translate(768,278)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11px" fill="#4c596c">200</text></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="start" transform="translate(26,159)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="15px" font-weight="700" fill="#17212f">GFP</text><text text-anchor="start" transform="translate(26,225)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="15px" font-weight="700" fill="#17212f">Pancreatic α-amylase</text></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="start" transform="translate(26,177)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="12px" fill="#4c596c">238 residues</text><text text-anchor="start" transform="translate(26,243)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="12px" fill="#4c596c">496 residues</text></g><g class="mark-rect role-mark stageBars" role="graphics-object" aria-roledescription="rect mark container"><path aria-label="GFP, PyTorch MPS, Trunk, 21.77 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M207,142h61.066668984997534v18h-61.066668984997534Z" fill="#a0a8b4" stroke="#768291" stroke-width="0.7"/><path aria-label="GFP, PyTorch MPS, Diffusion, 14.60 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M268.06666898499753,142h40.955090952187504v18h-40.955090952187504Z" fill="#a0a8b4" stroke="#768291" stroke-width="0.7"/><path aria-label="GFP, MLX adapter with highpairformer, Trunk, 11.19 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M207,164h31.387626956564986v18h-31.387626956564986Z" fill="#f9c83e" stroke="#a87912" stroke-width="0.7"/><path aria-label="GFP, MLX adapter with highpairformer, Diffusion, 10.48 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M238.387626956565,164h29.399797264530037v18h-29.399797264530037Z" fill="#f9c83e" stroke="#a87912" stroke-width="0.7"/><path aria-label="Pancreatic α-amylase, PyTorch MPS, Trunk, 145.88 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M207,208h409.1818143556276v18h-409.1818143556276Z" fill="#a0a8b4" stroke="#768291" stroke-width="0.7"/><path aria-label="Pancreatic α-amylase, PyTorch MPS, Diffusion, 27.87 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M616.1818143556276,208h78.17776545702748v18h-78.17776545702748Z" fill="#a0a8b4" stroke="#768291" stroke-width="0.7"/><path aria-label="Pancreatic α-amylase, MLX adapter with highpairformer, Trunk, 53.55 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M207,230h150.21001176406503v18h-150.21001176406503Z" fill="#f9c83e" stroke="#a87912" stroke-width="0.7"/><path aria-label="Pancreatic α-amylase, MLX adapter with highpairformer, Diffusion, 20.64 seconds" role="graphics-symbol" aria-roledescription="rect mark" d="M357.21001176406503,230h57.89515056327747v18h-57.89515056327747Z" fill="#f9c83e" stroke="#a87912" stroke-width="0.7"/></g><g class="mark-text role-mark" aria-hidden="true"><text text-anchor="middle" transform="translate(237.53333449249877,154)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11.5px" font-weight="600" fill="#202c3a">21.8</text><text text-anchor="middle" transform="translate(288.54421446109126,154)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11.5px" font-weight="600" fill="#202c3a">14.6</text><text text-anchor="middle" transform="translate(222.6938134782825,176)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="10.5px" font-weight="600" fill="#202c3a">11.2</text><text text-anchor="middle" transform="translate(253.08752558883003,176)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="10.5px" font-weight="600" fill="#202c3a">10.5</text><text text-anchor="middle" transform="translate(411.5909071778138,220)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11.5px" font-weight="600" fill="#202c3a">145.9</text><text text-anchor="middle" transform="translate(655.2706970841414,220)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11.5px" font-weight="600" fill="#202c3a">27.9</text><text text-anchor="middle" transform="translate(282.1050058820325,242)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11.5px" font-weight="600" fill="#202c3a">53.6</text><text text-anchor="middle" transform="translate(386.15758704570374,242)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="11.5px" font-weight="600" fill="#202c3a">20.6</text></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="start" transform="translate(317.02175993718504,155)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="13px" font-weight="650" fill="#283547">36.37 s</text><text text-anchor="start" transform="translate(275.787424221095,177)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="13px" font-weight="650" fill="#805308">21.67 s</text><text text-anchor="start" transform="translate(702.3595798126548,221)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="13px" font-weight="650" fill="#283547">173.75 s</text><text text-anchor="start" transform="translate(423.1051623273425,243)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="13px" font-weight="650" fill="#805308">74.19 s</text></g><g class="mark-text role-mark" role="graphics-object" aria-roledescription="text mark container"><text text-anchor="middle" transform="translate(855,169)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="22px" font-weight="700" fill="#805308">1.68×</text><text text-anchor="middle" transform="translate(855,235)" font-family="system-ui, -apple-system, BlinkMacSystemFont, sans-serif" font-size="22px" font-weight="700" fill="#805308">2.34×</text></g></g><path class="foreground" aria-hidden="true" d="" display="none"/></g></g></g></svg>