faster-diffbloch 0.1.7__tar.gz → 0.2.1__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 (19) hide show
  1. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/.gitignore +8 -0
  2. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/PKG-INFO +42 -7
  3. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/README.md +39 -3
  4. faster_diffbloch-0.2.1/hatch_build.py +26 -0
  5. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/pyproject.toml +9 -7
  6. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/__init__.py +1 -1
  7. faster_diffbloch-0.2.1/src/faster_diffbloch/cli.py +72 -0
  8. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/native/metal_batch_cgemm.m +1 -2
  9. faster_diffbloch-0.1.7/src/faster_diffbloch/cli.py +0 -21
  10. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/LICENSE +0 -0
  11. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/backend.py +0 -0
  12. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/builder.py +0 -0
  13. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/native/batch_cgemm.c +0 -0
  14. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/native/batch_cgemm.h +0 -0
  15. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/native/batch_cgemm.metal +0 -0
  16. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/native/bridge_lib.c +0 -0
  17. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/native/native_scattering.c +0 -0
  18. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/native/native_scattering.h +0 -0
  19. {faster_diffbloch-0.1.7 → faster_diffbloch-0.2.1}/src/faster_diffbloch/scattering.py +0 -0
@@ -22,3 +22,11 @@ bench/build/
22
22
  *.metallib
23
23
  *.dylib
24
24
  *.so
25
+
26
+ # Flow build outputs.
27
+ build/
28
+ flow/.flow_cache/
29
+
30
+ # Staged by packages/faster-diffbloch/build_exact.sh; built files.
31
+ packages/faster-diffbloch/src/faster_diffbloch/exact/
32
+ packages/faster-diffbloch/dist/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: faster-diffbloch
3
- Version: 0.1.7
3
+ Version: 0.2.1
4
4
  Summary: Drop-in Metal GPU (macOS) and optimized CPU (macOS/Linux) acceleration for diffBloch
5
5
  Project-URL: Homepage, https://godofecht.github.io/diffFlow/
6
6
  Project-URL: Documentation, https://godofecht.github.io/diffFlow/
@@ -24,11 +24,10 @@ Classifier: Programming Language :: Python :: 3.10
24
24
  Classifier: Programming Language :: Python :: 3.11
25
25
  Classifier: Programming Language :: Python :: 3.12
26
26
  Classifier: Topic :: Scientific/Engineering :: Physics
27
- Requires-Python: >=3.10
27
+ Requires-Python: >=3.12
28
+ Requires-Dist: diffbloch>=0.2.0rc1
28
29
  Requires-Dist: numpy>=1.24
29
30
  Requires-Dist: torch>=2.0
30
- Provides-Extra: diffbloch
31
- Requires-Dist: diffbloch; extra == 'diffbloch'
32
31
  Description-Content-Type: text/markdown
33
32
 
34
33
  # faster-diffbloch
@@ -132,19 +131,26 @@ configurations and beam counts will differ.
132
131
  pip install faster-diffbloch
133
132
  ```
134
133
 
134
+ This installs the compatible `diffBloch` release and provides the same `diffbloch` command. The
135
+ accelerated backend is enabled automatically. The `faster-diffbloch` and `diffbloch-fast` commands
136
+ remain available as aliases.
137
+
135
138
  ---
136
139
 
137
140
  ## Usage
138
141
 
139
142
  ### 1. Drop-in CLI
140
143
 
141
- Use `diffbloch-fast` or `faster-diffbloch` anywhere you would use `diffbloch`:
144
+ Use the normal `diffbloch` command:
142
145
 
143
146
  ```bash
144
- diffbloch-fast infer examples/Colmey_et_al_2026/data/quartz-no-abs
145
- diffbloch-fast refine examples/Colmey_et_al_2026/data/quartz-no-abs
147
+ diffbloch infer examples/Colmey_et_al_2026/data/quartz-no-abs
148
+ diffbloch refine examples/Colmey_et_al_2026/data/quartz-no-abs
146
149
  ```
147
150
 
151
+ The wrapper uses CPU tensors by default. The native backend selects Metal on Apple Silicon and the
152
+ optimized CPU path elsewhere. Pass `--device` to preserve an explicit diffBloch device choice.
153
+
148
154
  ### 2. Python API Injection
149
155
 
150
156
  Enable acceleration inside any existing diffBloch script:
@@ -162,3 +168,32 @@ faster_diffbloch.enable(device="cpu")
162
168
  import diffBloch
163
169
  # All propagate and matrix_exp calls now route through faster-diffBloch
164
170
  ```
171
+
172
+ ### 3. Exact refinement (macOS Apple Silicon)
173
+
174
+ `refine --exact` runs diffBloch's refinement with its optimizer loop compiled from Flow.
175
+ Its output matches `diffbloch refine` in every bit: the loss, every parameter and every
176
+ gradient at every step, and the console line for line. The macOS arm64 wheel ships the prebuilt binary, so nothing is compiled on install.
177
+
178
+ ```bash
179
+ pip install faster-diffbloch "torch==2.14.*" \
180
+ "diffBloch @ git+https://github.com/Differentiable-Electron-Crystallography/diffBloch@12913566c7598f975b0529d0b2f11bc862dbffbc"
181
+ faster-diffbloch refine --exact path/to/experiment
182
+ ```
183
+
184
+ The bits were checked against that diffBloch commit on an M4 Max under macOS 26: torch
185
+ 2.14.0 for the table below, and torch 2.14.1 from a clean install of this wheel on both
186
+ quartz experiments. Another torch prints a warning, since its kernels round differently.
187
+ The diffBloch on PyPI (0.2.0rc1) is an older snapshot with a different refinement loop.
188
+ diffBloch writes its visualization notebook only from a source checkout, so an installed
189
+ diffBloch prints one line fewer, under `diffbloch refine` and `refine --exact` alike.
190
+
191
+ | Experiment (Colmey et al. 2026) | Steps compared | Result |
192
+ |---|---|---|
193
+ | quartz-absorption | 40 | every step bit-equal, console identical |
194
+ | quartz-no-abs | 40 | every step bit-equal, console identical |
195
+ | cspbbr3-absorption | 40 | every step bit-equal |
196
+
197
+ CsPbBr3 takes 6.3 h for 40 steps and peaks at 5.6 GB, against 12.8 h and 15.3 GB for
198
+ diffBloch on the CPU. `DIFFFLOW_THREADS` sets the worker threads, default 8; the count
199
+ cannot change a bit.
@@ -99,19 +99,26 @@ configurations and beam counts will differ.
99
99
  pip install faster-diffbloch
100
100
  ```
101
101
 
102
+ This installs the compatible `diffBloch` release and provides the same `diffbloch` command. The
103
+ accelerated backend is enabled automatically. The `faster-diffbloch` and `diffbloch-fast` commands
104
+ remain available as aliases.
105
+
102
106
  ---
103
107
 
104
108
  ## Usage
105
109
 
106
110
  ### 1. Drop-in CLI
107
111
 
108
- Use `diffbloch-fast` or `faster-diffbloch` anywhere you would use `diffbloch`:
112
+ Use the normal `diffbloch` command:
109
113
 
110
114
  ```bash
111
- diffbloch-fast infer examples/Colmey_et_al_2026/data/quartz-no-abs
112
- diffbloch-fast refine examples/Colmey_et_al_2026/data/quartz-no-abs
115
+ diffbloch infer examples/Colmey_et_al_2026/data/quartz-no-abs
116
+ diffbloch refine examples/Colmey_et_al_2026/data/quartz-no-abs
113
117
  ```
114
118
 
119
+ The wrapper uses CPU tensors by default. The native backend selects Metal on Apple Silicon and the
120
+ optimized CPU path elsewhere. Pass `--device` to preserve an explicit diffBloch device choice.
121
+
115
122
  ### 2. Python API Injection
116
123
 
117
124
  Enable acceleration inside any existing diffBloch script:
@@ -129,3 +136,32 @@ faster_diffbloch.enable(device="cpu")
129
136
  import diffBloch
130
137
  # All propagate and matrix_exp calls now route through faster-diffBloch
131
138
  ```
139
+
140
+ ### 3. Exact refinement (macOS Apple Silicon)
141
+
142
+ `refine --exact` runs diffBloch's refinement with its optimizer loop compiled from Flow.
143
+ Its output matches `diffbloch refine` in every bit: the loss, every parameter and every
144
+ gradient at every step, and the console line for line. The macOS arm64 wheel ships the prebuilt binary, so nothing is compiled on install.
145
+
146
+ ```bash
147
+ pip install faster-diffbloch "torch==2.14.*" \
148
+ "diffBloch @ git+https://github.com/Differentiable-Electron-Crystallography/diffBloch@12913566c7598f975b0529d0b2f11bc862dbffbc"
149
+ faster-diffbloch refine --exact path/to/experiment
150
+ ```
151
+
152
+ The bits were checked against that diffBloch commit on an M4 Max under macOS 26: torch
153
+ 2.14.0 for the table below, and torch 2.14.1 from a clean install of this wheel on both
154
+ quartz experiments. Another torch prints a warning, since its kernels round differently.
155
+ The diffBloch on PyPI (0.2.0rc1) is an older snapshot with a different refinement loop.
156
+ diffBloch writes its visualization notebook only from a source checkout, so an installed
157
+ diffBloch prints one line fewer, under `diffbloch refine` and `refine --exact` alike.
158
+
159
+ | Experiment (Colmey et al. 2026) | Steps compared | Result |
160
+ |---|---|---|
161
+ | quartz-absorption | 40 | every step bit-equal, console identical |
162
+ | quartz-no-abs | 40 | every step bit-equal, console identical |
163
+ | cspbbr3-absorption | 40 | every step bit-equal |
164
+
165
+ CsPbBr3 takes 6.3 h for 40 steps and peaks at 5.6 GB, against 12.8 h and 15.3 GB for
166
+ diffBloch on the CPU. `DIFFFLOW_THREADS` sets the worker threads, default 8; the count
167
+ cannot change a bit.
@@ -0,0 +1,26 @@
1
+ """Tag the wheel for macOS arm64 when build_exact.sh has staged the Flow binary.
2
+
3
+ Without src/faster_diffbloch/exact/_bin the wheel stays pure (py3-none-any), as before.
4
+ """
5
+
6
+ from pathlib import Path
7
+
8
+ from hatchling.builders.hooks.plugin.interface import BuildHookInterface
9
+
10
+ EXACT_BIN = Path(__file__).parent / "src" / "faster_diffbloch" / "exact" / "_bin"
11
+ EXACT_TAG = "py3-none-macosx_14_0_arm64"
12
+
13
+
14
+ class CustomBuildHook(BuildHookInterface):
15
+ def initialize(self, version, build_data):
16
+ if self.target_name != "wheel":
17
+ return
18
+ if (EXACT_BIN / "experiment_c").exists():
19
+ build_data["pure_python"] = False
20
+ build_data["tag"] = EXACT_TAG
21
+ # The staged tree is git-ignored, so name every file rather than rely on file selection.
22
+ exact = EXACT_BIN.parent
23
+ for path in sorted(exact.rglob("*")):
24
+ if path.is_file() and "__pycache__" not in path.parts:
25
+ rel = path.relative_to(exact.parent.parent) # faster_diffbloch/exact/...
26
+ build_data["force_include"][str(path)] = str(rel)
@@ -4,10 +4,10 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "faster-diffbloch"
7
- version = "0.1.7"
7
+ version = "0.2.1"
8
8
  description = "Drop-in Metal GPU (macOS) and optimized CPU (macOS/Linux) acceleration for diffBloch"
9
9
  readme = "README.md"
10
- requires-python = ">=3.10"
10
+ requires-python = ">=3.12"
11
11
  keywords = ["diffBloch", "electron diffraction", "electron crystallography", "structure refinement", "PyTorch acceleration", "Metal GPU"]
12
12
  license = "MIT"
13
13
  authors = [
@@ -27,16 +27,13 @@ classifiers = [
27
27
  "Topic :: Scientific/Engineering :: Physics",
28
28
  ]
29
29
  dependencies = [
30
+ "diffbloch>=0.2.0rc1",
30
31
  "numpy>=1.24",
31
32
  "torch>=2.0",
32
33
  ]
33
34
 
34
- [project.optional-dependencies]
35
- diffbloch = [
36
- "diffbloch",
37
- ]
38
-
39
35
  [project.scripts]
36
+ diffbloch = "faster_diffbloch.cli:main"
40
37
  faster-diffbloch = "faster_diffbloch.cli:main"
41
38
  diffbloch-fast = "faster_diffbloch.cli:main"
42
39
 
@@ -52,9 +49,14 @@ piwheels = "https://www.piwheels.org/project/faster-diffbloch/"
52
49
  [tool.hatch.build.targets.wheel]
53
50
  packages = ["src/faster_diffbloch"]
54
51
 
52
+ # build_exact.sh stages faster_diffbloch.exact; the hook then tags that wheel macOS arm64.
53
+ [tool.hatch.build.hooks.custom]
54
+
55
55
  [tool.hatch.build.targets.sdist]
56
56
  include = [
57
57
  "src/faster_diffbloch",
58
+ "hatch_build.py",
58
59
  "README.md",
59
60
  "LICENSE",
60
61
  ]
62
+ exclude = ["src/faster_diffbloch/exact"]
@@ -4,7 +4,7 @@ from __future__ import annotations
4
4
 
5
5
  from .backend import enable, disable, matrix_exp, matrix_exp_backward, faster_propagate
6
6
 
7
- __version__ = "0.1.7"
7
+ __version__ = "0.2.1"
8
8
  __all__ = [
9
9
  "enable",
10
10
  "disable",
@@ -0,0 +1,72 @@
1
+ """Drop-in CLI wrapper enabling faster-diffbloch before running diffbloch commands.
2
+
3
+ diffbloch <diffbloch command ...> Metal GPU acceleration, then diffBloch
4
+ faster-diffbloch <diffbloch command ...> Alias for the drop-in command
5
+ faster-diffbloch refine --exact <experiment> diffBloch's refinement run in Flow,
6
+ bit for bit with `diffbloch refine`
7
+
8
+ --exact needs the macOS arm64 wheel, which ships the prebuilt Flow binary.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import sys
14
+
15
+
16
+ def _default_device(argv: list[str]) -> list[str]:
17
+ """Keep the stock command line while avoiding diffBloch's CUDA default on this path.
18
+
19
+ The accelerated backend owns the native GPU choice. A CPU torch tensor is the portable
20
+ transport for the native Metal and CPU paths, and it also avoids MPS float64 restrictions.
21
+ An explicit ``--device`` remains authoritative for callers that need another torch device.
22
+ """
23
+ if not argv or argv[0] not in {"infer", "preprocess", "refine", "convergence-test"}:
24
+ return argv
25
+ if "--device" in argv:
26
+ return argv
27
+ return [argv[0], "--device", "cpu", *argv[1:]]
28
+
29
+ # The versions --exact was checked against, bit for bit, on an M4 Max under macOS 26.
30
+ EXACT_TORCH = "2.14."
31
+ EXACT_DIFFBLOCH = ("https://github.com/Differentiable-Electron-Crystallography/diffBloch",
32
+ "1291356")
33
+
34
+
35
+ def _exact(argv: list[str]) -> int:
36
+ try:
37
+ from .exact.cli import main as exact_main
38
+ from .exact.toolchain import bundled_backends
39
+ except ImportError as err:
40
+ print(f"faster-diffbloch: --exact is not in this install ({err}).\n"
41
+ "It ships in the macOS arm64 wheel only.", file=sys.stderr)
42
+ return 1
43
+ if not bundled_backends():
44
+ print("faster-diffbloch: --exact needs the macOS arm64 wheel, which ships the Flow binary.",
45
+ file=sys.stderr)
46
+ return 1
47
+ import torch
48
+ if not torch.__version__.startswith(EXACT_TORCH):
49
+ print(f"faster-diffbloch: warning: --exact matches diffBloch bit for bit under torch "
50
+ f"{EXACT_TORCH}x; this is torch {torch.__version__}, so the last bits may differ.",
51
+ file=sys.stderr)
52
+ return exact_main(argv)
53
+
54
+
55
+ def main() -> None:
56
+ argv = sys.argv[1:]
57
+ if argv[:1] == ["refine"] and "--exact" in argv:
58
+ # Exact mode runs diffBloch untouched apart from its optimizer loop: no acceleration.
59
+ sys.exit(_exact([a for a in argv if a != "--exact"]))
60
+ # Enable the native backend by default. The backend selects Metal on Apple Silicon.
61
+ from .backend import enable
62
+ enable(device="gpu")
63
+ try:
64
+ from diffBloch.app.cli import main as diffbloch_main
65
+ raise SystemExit(diffbloch_main(_default_device(argv)))
66
+ except ImportError:
67
+ print("Error: diffbloch is not installed. Install diffbloch or run 'pip install diffbloch'.", file=sys.stderr)
68
+ sys.exit(1)
69
+
70
+
71
+ if __name__ == "__main__":
72
+ main()
@@ -114,8 +114,7 @@ static NSString* get_metallib_path(void) {
114
114
  @"bench/build/batch_cgemm.metallib",
115
115
  @"src/batch_gemm/batch_cgemm.metallib",
116
116
  @"../bench/build/batch_cgemm.metallib",
117
- @"../../bench/build/batch_cgemm.metallib",
118
- @"/Users/abhishekshivakumar/diffFlow/bench/build/batch_cgemm.metallib"
117
+ @"../../bench/build/batch_cgemm.metallib"
119
118
  ];
120
119
  for (NSString* p in candidates) {
121
120
  if ([[NSFileManager defaultManager] fileExistsAtPath:p]) {
@@ -1,21 +0,0 @@
1
- """CLI wrapper enabling faster-diffbloch before running diffbloch commands."""
2
-
3
- from __future__ import annotations
4
-
5
- import sys
6
- from .backend import enable
7
-
8
-
9
- def main() -> None:
10
- # Enable Metal GPU acceleration by default
11
- enable(device="gpu")
12
- try:
13
- from diffBloch.app.cli import main as diffbloch_main
14
- diffbloch_main()
15
- except ImportError:
16
- print("Error: diffbloch is not installed. Install diffbloch or run 'pip install diffbloch'.", file=sys.stderr)
17
- sys.exit(1)
18
-
19
-
20
- if __name__ == "__main__":
21
- main()