medmlx-core 0.1.2__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 (65) hide show
  1. medmlx_core-0.1.2/.gitignore +26 -0
  2. medmlx_core-0.1.2/.hypothesis/.gitignore +9 -0
  3. medmlx_core-0.1.2/.hypothesis/constants/08abc73d38e2a158 +4 -0
  4. medmlx_core-0.1.2/.hypothesis/constants/0d8ec412b22beadb +4 -0
  5. medmlx_core-0.1.2/.hypothesis/constants/34990f1212c247ce +4 -0
  6. medmlx_core-0.1.2/.hypothesis/constants/396a64bc0176d3a9 +4 -0
  7. medmlx_core-0.1.2/.hypothesis/constants/3bbd74727a5a8204 +4 -0
  8. medmlx_core-0.1.2/.hypothesis/constants/3e9c700c66f9d9c5 +4 -0
  9. medmlx_core-0.1.2/.hypothesis/constants/6116a31f7220fa84 +4 -0
  10. medmlx_core-0.1.2/.hypothesis/constants/6f29e0369cfb318e +4 -0
  11. medmlx_core-0.1.2/.hypothesis/constants/86bf87a3fa3f2620 +4 -0
  12. medmlx_core-0.1.2/.hypothesis/constants/8bf2c9a81c8a39f4 +4 -0
  13. medmlx_core-0.1.2/.hypothesis/constants/cbb5757b65496821 +4 -0
  14. medmlx_core-0.1.2/.hypothesis/unicode_data/15.0.0/charmap.json.gz +0 -0
  15. medmlx_core-0.1.2/.hypothesis/unicode_data/15.0.0/codec-v2-utf-8.json.gz +0 -0
  16. medmlx_core-0.1.2/LICENSE +202 -0
  17. medmlx_core-0.1.2/Makefile +5 -0
  18. medmlx_core-0.1.2/PKG-INFO +176 -0
  19. medmlx_core-0.1.2/README.md +152 -0
  20. medmlx_core-0.1.2/THIRD_PARTY_NOTICES.md +16 -0
  21. medmlx_core-0.1.2/licenses/MONAI_LICENSE +201 -0
  22. medmlx_core-0.1.2/pyproject.toml +65 -0
  23. medmlx_core-0.1.2/scripts/make_channel_last_fixtures.py +103 -0
  24. medmlx_core-0.1.2/scripts/make_reference_fixtures.py +146 -0
  25. medmlx_core-0.1.2/scripts/make_sliding_window_fixtures.py +62 -0
  26. medmlx_core-0.1.2/scripts/reference_support.py +69 -0
  27. medmlx_core-0.1.2/src/medmlx_core/__init__.py +118 -0
  28. medmlx_core-0.1.2/src/medmlx_core/channel_last.py +82 -0
  29. medmlx_core-0.1.2/src/medmlx_core/checkpoints.py +68 -0
  30. medmlx_core-0.1.2/src/medmlx_core/conversion.py +31 -0
  31. medmlx_core-0.1.2/src/medmlx_core/errors.py +87 -0
  32. medmlx_core-0.1.2/src/medmlx_core/layout.py +70 -0
  33. medmlx_core-0.1.2/src/medmlx_core/ops.py +281 -0
  34. medmlx_core-0.1.2/src/medmlx_core/precision.py +327 -0
  35. medmlx_core-0.1.2/src/medmlx_core/py.typed +0 -0
  36. medmlx_core-0.1.2/src/medmlx_core/runtime.py +306 -0
  37. medmlx_core-0.1.2/src/medmlx_core/sliding_window.py +258 -0
  38. medmlx_core-0.1.2/src/medmlx_core/upsample.py +143 -0
  39. medmlx_core-0.1.2/tests/NUMERICAL_PARITY.md +110 -0
  40. medmlx_core-0.1.2/tests/conftest.py +1 -0
  41. medmlx_core-0.1.2/tests/fixture_cases.py +500 -0
  42. medmlx_core-0.1.2/tests/fixtures/darwin-arm64/channel_last.npz +0 -0
  43. medmlx_core-0.1.2/tests/fixtures/darwin-arm64/layout.npz +0 -0
  44. medmlx_core-0.1.2/tests/fixtures/darwin-arm64/ops.npz +0 -0
  45. medmlx_core-0.1.2/tests/fixtures/darwin-arm64/precision.npz +0 -0
  46. medmlx_core-0.1.2/tests/fixtures/darwin-arm64/sliding_window_constant.npz +0 -0
  47. medmlx_core-0.1.2/tests/fixtures/darwin-arm64/sliding_window_gaussian.npz +0 -0
  48. medmlx_core-0.1.2/tests/fixtures/darwin-arm64/upsample.npz +0 -0
  49. medmlx_core-0.1.2/tests/numerical.py +124 -0
  50. medmlx_core-0.1.2/tests/numerical_contract.json +44 -0
  51. medmlx_core-0.1.2/tests/reference_spec.json +7 -0
  52. medmlx_core-0.1.2/tests/test_channel_last.py +109 -0
  53. medmlx_core-0.1.2/tests/test_checkpoint_loading.py +56 -0
  54. medmlx_core-0.1.2/tests/test_checkpoints.py +82 -0
  55. medmlx_core-0.1.2/tests/test_dependencies.py +45 -0
  56. medmlx_core-0.1.2/tests/test_equivalence.py +42 -0
  57. medmlx_core-0.1.2/tests/test_errors.py +44 -0
  58. medmlx_core-0.1.2/tests/test_import.py +19 -0
  59. medmlx_core-0.1.2/tests/test_input_contracts.py +45 -0
  60. medmlx_core-0.1.2/tests/test_numerical.py +92 -0
  61. medmlx_core-0.1.2/tests/test_reference_fixtures.py +156 -0
  62. medmlx_core-0.1.2/tests/test_runtime.py +102 -0
  63. medmlx_core-0.1.2/tests/test_sliding_window.py +364 -0
  64. medmlx_core-0.1.2/tests/test_upsample_parity.py +57 -0
  65. medmlx_core-0.1.2/uv.lock +698 -0
@@ -0,0 +1,26 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ .pytest_cache/
8
+ .ruff_cache/
9
+
10
+ # Checkpoints and converted weights are never committed.
11
+ checkpoints/
12
+ weights/
13
+ *.safetensors
14
+ *.npz
15
+ !tests/fixtures/*.npz
16
+ !tests/fixtures/*/*.npz
17
+ *.pt
18
+ *.pth
19
+ *.ckpt
20
+
21
+ # Imaging data is never committed.
22
+ data/
23
+ *.nii
24
+ *.nii.gz
25
+ *.dcm
26
+ .DS_Store
@@ -0,0 +1,9 @@
1
+ # This .gitignore file was automatically created by Hypothesis. Hypothesis gitignores
2
+ # .hypothesis by default, because we generally recommend that .hypothesis not be checked
3
+ # into version control.
4
+ #
5
+ # If you *would* like to check .hypothesis into version control, you should delete this
6
+ # file. Hypothesis will not re-create this .gitignore unless .hypothesis is deleted (and
7
+ # if it does, that's a bug - please report it!)
8
+
9
+ *
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/checkpoints.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ ['HostArray', '_maybe_array', 'cpu', 'detach', 'mapping_from_pairs', 'numpy']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/__init__.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ ['0.1.2', 'AssetNotReadyError', 'Float32Operators', 'InvalidInputError', 'MLX_EXTRA', 'MedmlxError', 'MlxHostReport', 'ModelExecutionError', 'as_fp32', 'avg_pool3d_ncdhw', 'conv3d_ncdhw', 'conv3d_weight_to_mlx', 'deconv2x_ncdhw', 'dense_patch_slices', 'group_norm_ncdhw', 'import_mlx', 'linear', 'mapping_from_pairs', 'probe_mlx_runtime', 'require_mlx_device', 'require_mlx_runtime', 'require_ncdhw', 'silu', 'split_conv3d_ncdhw', 'to_ncdhw', 'to_ndhwc', 'tokens_from_ncdhw', 'tokens_to_ncdhw', 'upsample_add_ncdhw']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/runtime.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ ['-n', '.', '/usr/sbin/sysctl', 'Darwin', 'MLX_EXTRA', 'MlxHostReport', '__version__', 'apple_chip', 'arm64', 'available', 'get_peak_memory', 'gpu', 'hint', 'hw.memsize', 'import_mlx', 'macos_version', 'memory_bytes', 'metal', 'mlx', 'mlx is not installed', 'mlx_version', 'platform_machine', 'platform_system', 'probe_mlx_runtime', 'python_version', 'reason', 'require_mlx_device', 'require_mlx_runtime', 'reset_peak_memory']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/layout.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ ['attention input', 'maisi.layout.embed_w', 'maisi.layout.ncdhw', 'maisi.layout.vector']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/upsample.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ [255, 256, 'decoder input', 'decoder skip', 'math_mode', 'out', 'safe', 'skip', 'x']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/channel_last.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ [1e-05, 0.01]
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/errors.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ ['AssetNotReadyError', 'InvalidInputError', 'MedmlxError', 'ModelExecutionError']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/ops.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ ['avg pool input', 'conv3d input', 'group_norm input', 'linear', 'mlx.nn', 'skip concat left', 'skip concat right', 'spatial pad input', 'split_conv3d']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/precision.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ [127, 128, 256, 512, 'B', 'BIAS', 'EPS', 'M', 'MA', 'MEAN', 'MO', 'S', 'V', 'VA', 'VAR', 'VO', 'W', 'X', 'Y', 'affine', 'coeff', 'conv', 'f', 'finish', 'join', 'merge', 'tile']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/sliding_window.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ [0.001, 0.125, 0.25, 'circular', 'constant', 'cpu', 'dense_patch_slices', 'edge', 'gaussian', 'gpu', 'reflect', 'replicate', 'wrap']
@@ -0,0 +1,4 @@
1
+ # file: /private/var/folders/_b/0j6pnq_16w7bgz89p1tx637m0000gn/T/medmlx-public-release-ht52soje/core/src/medmlx_core/conversion.py
2
+ # hypothesis_version: 6.168.5
3
+
4
+ ['conversion', 'cpu']
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,5 @@
1
+ .PHONY: typecheck
2
+
3
+ typecheck:
4
+ uv run --locked --extra dev pyright --project . --warnings
5
+ bash -o pipefail -c 'rg --files --hidden --no-ignore --glob "*.py" --glob "*.pyi" --null scripts src tests | xargs -0 uv run --locked --extra dev ruff check --config pyproject.toml --select TID251,ANN401 --ignore-noqa --no-force-exclude --'
@@ -0,0 +1,176 @@
1
+ Metadata-Version: 2.5
2
+ Name: medmlx-core
3
+ Version: 0.1.2
4
+ Summary: Shared MLX runtime for MedMLX model packages
5
+ Author: Joseph Sandoval, Alfredo Sandoval Jr.
6
+ License-Expression: Apache-2.0
7
+ License-File: LICENSE
8
+ License-File: THIRD_PARTY_NOTICES.md
9
+ License-File: licenses/MONAI_LICENSE
10
+ Requires-Python: <3.14,>=3.12
11
+ Requires-Dist: mlx<0.33,>=0.32.3; platform_system == 'Darwin' and platform_machine == 'arm64'
12
+ Requires-Dist: numpy<3,>=2.0
13
+ Provides-Extra: conversion
14
+ Requires-Dist: torch<3,>=2.6; extra == 'conversion'
15
+ Provides-Extra: dev
16
+ Requires-Dist: hypothesis<7,>=6.140; extra == 'dev'
17
+ Requires-Dist: monai==1.6.0; extra == 'dev'
18
+ Requires-Dist: numpy<3,>=2.2; extra == 'dev'
19
+ Requires-Dist: pyright[nodejs]==1.1.414; extra == 'dev'
20
+ Requires-Dist: pytest<10,>=8.4; extra == 'dev'
21
+ Requires-Dist: ruff<1,>=0.13; extra == 'dev'
22
+ Requires-Dist: torch==2.14.1; extra == 'dev'
23
+ Description-Content-Type: text/markdown
24
+
25
+ # medmlx-core
26
+
27
+ Shared runtime and MLX primitives for standalone MedMLX medical imaging models.
28
+ Runtime dependencies are NumPy and MLX. Importing `medmlx_core` does not import
29
+ MLX; Torch is loaded only when the optional checkpoint converter is called.
30
+ No model architectures, pretrained weights, image I/O, or model runners are included.
31
+
32
+ ## Install and platform policy
33
+
34
+ ```sh
35
+ uv pip install "medmlx-core @ git+https://github.com/MedMLX/medmlx-core.git@v0.1.2"
36
+ ```
37
+
38
+ Only macOS arm64 with MLX on Metal is supported. `import_mlx()` admits the host
39
+ and selects the Metal GPU. Unsupported hosts are rejected before importing MLX;
40
+ unavailable Metal is an error. There are no backend extras or backend fallback.
41
+ The existing `require_mlx_device("mlx")` API checks explicit MLX readiness.
42
+ Python is `>=3.12,<3.14`; MLX is `>=0.32.3,<0.33`. Runtime admission rejects
43
+ older or unsupported MLX versions before inference.
44
+
45
+ The `conversion` extra supplies Torch for `load_torch_checkpoint()`. Safe
46
+ `weights_only=True` loading is the default. Explicit `weights_only=False` remains
47
+ available for trusted legacy payloads. Missing Torch reports `extra="conversion"`
48
+ and the corresponding install hint.
49
+
50
+ ## Public API and references
51
+
52
+ Main helpers are re-exported from `medmlx_core`. Errors derive from the real
53
+ `MedmlxError` base and retain their built-in catch types: `InvalidInputError`
54
+ (`ValueError`), `MissingDependencyError` (`ImportError`), `ModelExecutionError`
55
+ (`RuntimeError`), and `AssetNotReadyError` (`FileNotFoundError`).
56
+
57
+ The canonical reference pins are in `tests/reference_spec.json`. MONAI is
58
+ 1.6.0, revision [`eccefc57550b111ed781d82249dfe77872a0e918`](https://github.com/Project-MONAI/MONAI/tree/eccefc57550b111ed781d82249dfe77872a0e918).
59
+ PyTorch is 2.14.1, revision [`5c4886908584029761b579af026dcfb627c84070`](https://github.com/pytorch/pytorch/tree/5c4886908584029761b579af026dcfb627c84070).
60
+ Development dependencies pin these package versions; generators reject a different
61
+ version or source revision. Reference frameworks are absent from runtime imports.
62
+
63
+ | Module | API / independent reference |
64
+ | --- | --- |
65
+ | `runtime.py` | Host readiness, Metal admission and peak-memory counters; direct platform/API contract tests |
66
+ | `errors.py` | Exception identity, messages and metadata; direct public contract tests |
67
+ | `checkpoints.py`, `conversion.py` | Duplicate-safe host mappings and optional Torch deserialization; original seeded arrays and Torch save/load round trips |
68
+ | `layout.py` | NCDHW/NDHWC, convolution-weight and token permutations; plain NumPy float64 reference |
69
+ | `ops.py` | SiLU, linear, conv3d, conv_transpose3d, GroupNorm, pooling, interpolation, padding and concatenation; PyTorch functional operations |
70
+ | `ops.py` split convolutions | Actual MONAI `MaisiConvolution`, including its overlap, integer size-ratio checks and crop semantics |
71
+ | `upsample.py` | Kernel=stride=2 transpose convolution and fused trilinear interpolation + skip addition; PyTorch functional operations |
72
+ | `precision.py` | Metal FP32 convolution and GroupNorm versus PyTorch; custom epsilon decomposition and Welford moments versus NumPy float64 |
73
+ | `sliding_window.py` | MONAI `inferers/utils.py` and `data/utils.py` |
74
+ | `channel_last.py` | PyTorch layers, MONAI Swish and `UnetResBlock`; individual layers and complete seeded 2D/3D graphs |
75
+
76
+ Inference helpers run on Metal. Model topology, checkpoint keys, label meanings and
77
+ pre/postprocessing belong to each model package. Channel-last references use the
78
+ core's MONAI 1.6.0 pin; they do not qualify consumer models pinned to another release.
79
+
80
+ ## Sliding-window inference
81
+
82
+ ```python
83
+ from medmlx_core import sliding_window_inference
84
+
85
+ logits = sliding_window_inference(
86
+ image, # NumPy or MLX (N, C, *spatial), float32/float16
87
+ roi_size=(128, 128, 128),
88
+ sw_batch_size=1,
89
+ predictor=network, # MLX array -> MLX array, same spatial resolution
90
+ overlap=0.25,
91
+ mode="constant",
92
+ padding_mode="replicate",
93
+ )
94
+ ```
95
+
96
+ The result is a host NumPy array with the input dtype and original spatial size;
97
+ output channels follow the predictor. Public helpers include `dense_patch_slices`
98
+ and `compute_importance_map`. Scalar/per-axis overlap and sigma, fallback ROI
99
+ components, all four Torch padding modes, supplied ROI maps, window batching
100
+ across images, predictor arguments, and `with_coord` are supported. Batch size
101
+ is used as supplied. The existing `sw_device` parameter accepts None, `"gpu"`
102
+ or an MLX GPU device; CPU prediction is rejected. `device` is None or `"cpu"`
103
+ because NumPy accumulation stays on the host. `import_mlx()` admits Metal before
104
+ the predictor is invoked.
105
+
106
+ Window stride is `max(int(roi * (1-overlap)), 1)`, with the final window shifted
107
+ onto the edge. Small inputs receive symmetric padding, with an extra voxel on
108
+ the right for odd deficits. Gaussian maps use FP32 separable factors, center
109
+ `(size-1)/2`, sigma `size*sigma_scale`, no peak normalization, and minimum 0.001.
110
+ Predictions are weighted in predictor dtype before ordered input-dtype accumulation.
111
+
112
+ Multiple/scaled outputs, positive `buffer_steps`, `process_fn`, progress bars,
113
+ and MetaTensor metadata are unsupported and fail explicitly where applicable.
114
+ No background threshold or class-ID mapping is applied by the shared stitcher.
115
+ Recorded postprocessing references use MONAI `AsDiscrete(argmax=True)` only.
116
+
117
+ ## Reference fixtures and regeneration
118
+
119
+ Seven small upstream-reference archives are included under
120
+ `tests/fixtures/darwin-arm64/` (each below 80 KB). Inputs retain the original
121
+ seeds: 7081 for helpers and 20261007 for channel-last/window graphs. Coverage
122
+ includes input/layout preparation, every helper, padding/cropping, window logits,
123
+ coordinates, importance maps, argmax labels, and complete synthetic layer graphs.
124
+ Each archive has `schema_version=2`, upstream versions/revisions, seed, reference
125
+ device, NumPy version, Torch build and recording platform. Generators use Torch
126
+ CPU and NumPy, import neither MedMLX nor MLX, and are never imported by the
127
+ numerical tests.
128
+ The reference frameworks execute on the CPU of this Mac; production inference
129
+ uses MLX on Metal. Tests load the one recording directory directly, without
130
+ backend selection. To regenerate on another Apple Silicon Mac:
131
+
132
+ ```sh
133
+ export UV_CACHE_DIR="$TMPDIR/uv-cache"
134
+ uv venv "$TMPDIR/venv-$(basename "$PWD")" --python 3.12
135
+ uv pip install --python "$TMPDIR/venv-$(basename "$PWD")/bin/python" -e ".[dev,conversion]"
136
+ export PYTHONDONTWRITEBYTECODE=1
137
+ PYTHON="$TMPDIR/venv-$(basename "$PWD")/bin/python"
138
+
139
+ "$PYTHON" scripts/make_reference_fixtures.py
140
+ "$PYTHON" scripts/make_channel_last_fixtures.py
141
+ "$PYTHON" scripts/make_sliding_window_fixtures.py
142
+
143
+ RUFF_CACHE_DIR="$TMPDIR/ruff-cache-medmlx-core" "$PYTHON" -m ruff check .
144
+ "$PYTHON" -m pytest -q -o cache_dir="$TMPDIR/pytest-cache-medmlx-core"
145
+ ```
146
+
147
+ Generators require macOS arm64. `--output-dir` permits regeneration in a temporary
148
+ directory for review. Missing references fail with a regeneration hint.
149
+
150
+ Numerical policy is versioned in `tests/numerical_contract.json`. Every comparison
151
+ checks shape, FP32 dtype and finite values. Copies, permutations, nearest-neighbor
152
+ replication and discrete outputs compare exactly. Arithmetic compares with
153
+ operation-specific absolute and relative bounds, accounting for reduction order,
154
+ FMA and exponential rounding. Each channel-last layer is checked on its upstream
155
+ stage input; complete graphs have a separate cumulative budget. The triage and
156
+ bound rationale are in `tests/NUMERICAL_PARITY.md`. The repeated 48-cubed decoder
157
+ test uses seeded weights and requires no external checkpoint.
158
+
159
+ Independent sliding-window tests retain exact comparison for representable
160
+ constant-blending inputs, maximum absolute error 1e-6 for Gaussian logits and
161
+ 1e-7 for Gaussian maps because NumPy/Torch FP32 exponential kernels round differently.
162
+
163
+ **Qualification:** The reviewer ran the previous suite on Metal and reported
164
+ 159 passes, 35 failures from cross-framework bitwise arithmetic assertions and
165
+ two skips. Those assertions and environment-dependent tests have been replaced;
166
+ the revised numerical gates still require reviewer Metal verification. Host
167
+ checks and reference generation do not qualify MLX execution. Reference arrays
168
+ retain the independently recorded upstream outputs and provenance.
169
+
170
+ Fixtures are synthetic helper references. Clinical accuracy, released model
171
+ weights and full-volume memory/performance are not qualified by these fixtures.
172
+
173
+ ## License
174
+
175
+ Apache-2.0; see [LICENSE](LICENSE). The built wheel includes the upstream MONAI
176
+ license and existing third-party notices alongside the runtime modules.
@@ -0,0 +1,152 @@
1
+ # medmlx-core
2
+
3
+ Shared runtime and MLX primitives for standalone MedMLX medical imaging models.
4
+ Runtime dependencies are NumPy and MLX. Importing `medmlx_core` does not import
5
+ MLX; Torch is loaded only when the optional checkpoint converter is called.
6
+ No model architectures, pretrained weights, image I/O, or model runners are included.
7
+
8
+ ## Install and platform policy
9
+
10
+ ```sh
11
+ uv pip install "medmlx-core @ git+https://github.com/MedMLX/medmlx-core.git@v0.1.2"
12
+ ```
13
+
14
+ Only macOS arm64 with MLX on Metal is supported. `import_mlx()` admits the host
15
+ and selects the Metal GPU. Unsupported hosts are rejected before importing MLX;
16
+ unavailable Metal is an error. There are no backend extras or backend fallback.
17
+ The existing `require_mlx_device("mlx")` API checks explicit MLX readiness.
18
+ Python is `>=3.12,<3.14`; MLX is `>=0.32.3,<0.33`. Runtime admission rejects
19
+ older or unsupported MLX versions before inference.
20
+
21
+ The `conversion` extra supplies Torch for `load_torch_checkpoint()`. Safe
22
+ `weights_only=True` loading is the default. Explicit `weights_only=False` remains
23
+ available for trusted legacy payloads. Missing Torch reports `extra="conversion"`
24
+ and the corresponding install hint.
25
+
26
+ ## Public API and references
27
+
28
+ Main helpers are re-exported from `medmlx_core`. Errors derive from the real
29
+ `MedmlxError` base and retain their built-in catch types: `InvalidInputError`
30
+ (`ValueError`), `MissingDependencyError` (`ImportError`), `ModelExecutionError`
31
+ (`RuntimeError`), and `AssetNotReadyError` (`FileNotFoundError`).
32
+
33
+ The canonical reference pins are in `tests/reference_spec.json`. MONAI is
34
+ 1.6.0, revision [`eccefc57550b111ed781d82249dfe77872a0e918`](https://github.com/Project-MONAI/MONAI/tree/eccefc57550b111ed781d82249dfe77872a0e918).
35
+ PyTorch is 2.14.1, revision [`5c4886908584029761b579af026dcfb627c84070`](https://github.com/pytorch/pytorch/tree/5c4886908584029761b579af026dcfb627c84070).
36
+ Development dependencies pin these package versions; generators reject a different
37
+ version or source revision. Reference frameworks are absent from runtime imports.
38
+
39
+ | Module | API / independent reference |
40
+ | --- | --- |
41
+ | `runtime.py` | Host readiness, Metal admission and peak-memory counters; direct platform/API contract tests |
42
+ | `errors.py` | Exception identity, messages and metadata; direct public contract tests |
43
+ | `checkpoints.py`, `conversion.py` | Duplicate-safe host mappings and optional Torch deserialization; original seeded arrays and Torch save/load round trips |
44
+ | `layout.py` | NCDHW/NDHWC, convolution-weight and token permutations; plain NumPy float64 reference |
45
+ | `ops.py` | SiLU, linear, conv3d, conv_transpose3d, GroupNorm, pooling, interpolation, padding and concatenation; PyTorch functional operations |
46
+ | `ops.py` split convolutions | Actual MONAI `MaisiConvolution`, including its overlap, integer size-ratio checks and crop semantics |
47
+ | `upsample.py` | Kernel=stride=2 transpose convolution and fused trilinear interpolation + skip addition; PyTorch functional operations |
48
+ | `precision.py` | Metal FP32 convolution and GroupNorm versus PyTorch; custom epsilon decomposition and Welford moments versus NumPy float64 |
49
+ | `sliding_window.py` | MONAI `inferers/utils.py` and `data/utils.py` |
50
+ | `channel_last.py` | PyTorch layers, MONAI Swish and `UnetResBlock`; individual layers and complete seeded 2D/3D graphs |
51
+
52
+ Inference helpers run on Metal. Model topology, checkpoint keys, label meanings and
53
+ pre/postprocessing belong to each model package. Channel-last references use the
54
+ core's MONAI 1.6.0 pin; they do not qualify consumer models pinned to another release.
55
+
56
+ ## Sliding-window inference
57
+
58
+ ```python
59
+ from medmlx_core import sliding_window_inference
60
+
61
+ logits = sliding_window_inference(
62
+ image, # NumPy or MLX (N, C, *spatial), float32/float16
63
+ roi_size=(128, 128, 128),
64
+ sw_batch_size=1,
65
+ predictor=network, # MLX array -> MLX array, same spatial resolution
66
+ overlap=0.25,
67
+ mode="constant",
68
+ padding_mode="replicate",
69
+ )
70
+ ```
71
+
72
+ The result is a host NumPy array with the input dtype and original spatial size;
73
+ output channels follow the predictor. Public helpers include `dense_patch_slices`
74
+ and `compute_importance_map`. Scalar/per-axis overlap and sigma, fallback ROI
75
+ components, all four Torch padding modes, supplied ROI maps, window batching
76
+ across images, predictor arguments, and `with_coord` are supported. Batch size
77
+ is used as supplied. The existing `sw_device` parameter accepts None, `"gpu"`
78
+ or an MLX GPU device; CPU prediction is rejected. `device` is None or `"cpu"`
79
+ because NumPy accumulation stays on the host. `import_mlx()` admits Metal before
80
+ the predictor is invoked.
81
+
82
+ Window stride is `max(int(roi * (1-overlap)), 1)`, with the final window shifted
83
+ onto the edge. Small inputs receive symmetric padding, with an extra voxel on
84
+ the right for odd deficits. Gaussian maps use FP32 separable factors, center
85
+ `(size-1)/2`, sigma `size*sigma_scale`, no peak normalization, and minimum 0.001.
86
+ Predictions are weighted in predictor dtype before ordered input-dtype accumulation.
87
+
88
+ Multiple/scaled outputs, positive `buffer_steps`, `process_fn`, progress bars,
89
+ and MetaTensor metadata are unsupported and fail explicitly where applicable.
90
+ No background threshold or class-ID mapping is applied by the shared stitcher.
91
+ Recorded postprocessing references use MONAI `AsDiscrete(argmax=True)` only.
92
+
93
+ ## Reference fixtures and regeneration
94
+
95
+ Seven small upstream-reference archives are included under
96
+ `tests/fixtures/darwin-arm64/` (each below 80 KB). Inputs retain the original
97
+ seeds: 7081 for helpers and 20261007 for channel-last/window graphs. Coverage
98
+ includes input/layout preparation, every helper, padding/cropping, window logits,
99
+ coordinates, importance maps, argmax labels, and complete synthetic layer graphs.
100
+ Each archive has `schema_version=2`, upstream versions/revisions, seed, reference
101
+ device, NumPy version, Torch build and recording platform. Generators use Torch
102
+ CPU and NumPy, import neither MedMLX nor MLX, and are never imported by the
103
+ numerical tests.
104
+ The reference frameworks execute on the CPU of this Mac; production inference
105
+ uses MLX on Metal. Tests load the one recording directory directly, without
106
+ backend selection. To regenerate on another Apple Silicon Mac:
107
+
108
+ ```sh
109
+ export UV_CACHE_DIR="$TMPDIR/uv-cache"
110
+ uv venv "$TMPDIR/venv-$(basename "$PWD")" --python 3.12
111
+ uv pip install --python "$TMPDIR/venv-$(basename "$PWD")/bin/python" -e ".[dev,conversion]"
112
+ export PYTHONDONTWRITEBYTECODE=1
113
+ PYTHON="$TMPDIR/venv-$(basename "$PWD")/bin/python"
114
+
115
+ "$PYTHON" scripts/make_reference_fixtures.py
116
+ "$PYTHON" scripts/make_channel_last_fixtures.py
117
+ "$PYTHON" scripts/make_sliding_window_fixtures.py
118
+
119
+ RUFF_CACHE_DIR="$TMPDIR/ruff-cache-medmlx-core" "$PYTHON" -m ruff check .
120
+ "$PYTHON" -m pytest -q -o cache_dir="$TMPDIR/pytest-cache-medmlx-core"
121
+ ```
122
+
123
+ Generators require macOS arm64. `--output-dir` permits regeneration in a temporary
124
+ directory for review. Missing references fail with a regeneration hint.
125
+
126
+ Numerical policy is versioned in `tests/numerical_contract.json`. Every comparison
127
+ checks shape, FP32 dtype and finite values. Copies, permutations, nearest-neighbor
128
+ replication and discrete outputs compare exactly. Arithmetic compares with
129
+ operation-specific absolute and relative bounds, accounting for reduction order,
130
+ FMA and exponential rounding. Each channel-last layer is checked on its upstream
131
+ stage input; complete graphs have a separate cumulative budget. The triage and
132
+ bound rationale are in `tests/NUMERICAL_PARITY.md`. The repeated 48-cubed decoder
133
+ test uses seeded weights and requires no external checkpoint.
134
+
135
+ Independent sliding-window tests retain exact comparison for representable
136
+ constant-blending inputs, maximum absolute error 1e-6 for Gaussian logits and
137
+ 1e-7 for Gaussian maps because NumPy/Torch FP32 exponential kernels round differently.
138
+
139
+ **Qualification:** The reviewer ran the previous suite on Metal and reported
140
+ 159 passes, 35 failures from cross-framework bitwise arithmetic assertions and
141
+ two skips. Those assertions and environment-dependent tests have been replaced;
142
+ the revised numerical gates still require reviewer Metal verification. Host
143
+ checks and reference generation do not qualify MLX execution. Reference arrays
144
+ retain the independently recorded upstream outputs and provenance.
145
+
146
+ Fixtures are synthetic helper references. Clinical accuracy, released model
147
+ weights and full-volume memory/performance are not qualified by these fixtures.
148
+
149
+ ## License
150
+
151
+ Apache-2.0; see [LICENSE](LICENSE). The built wheel includes the upstream MONAI
152
+ license and existing third-party notices alongside the runtime modules.