diffct 2.4.3__py3-none-any.whl → 2.5.0__py3-none-any.whl
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.
- diffct/__init__.py +1 -1
- diffct/_solver_state.py +34 -7
- diffct/analytical.py +12 -9
- diffct/analytical_execution.py +9 -3
- diffct/backends.py +544 -121
- diffct/chunking.py +3 -2
- diffct/constants.py +12 -0
- diffct/geometry.py +57 -5
- diffct/kernels/analytical_transpose.py +40 -16
- diffct/kernels/cone_beam.py +117 -63
- diffct/kernels/fan_beam.py +49 -33
- diffct/kernels/geometry_vjp.py +153 -51
- diffct/kernels/parallel_beam.py +45 -15
- diffct/kernels/spacing.py +79 -0
- diffct/kernels/surface_geometry.py +15 -4
- diffct/operators.py +102 -28
- diffct/projectors.py +13 -6
- diffct/reconstruction.py +22 -7
- diffct/streaming.py +7 -0
- diffct/surfaces.py +13 -4
- diffct/utils.py +20 -0
- {diffct-2.4.3.dist-info → diffct-2.5.0.dist-info}/METADATA +13 -9
- diffct-2.5.0.dist-info/RECORD +34 -0
- {diffct-2.4.3.dist-info → diffct-2.5.0.dist-info}/licenses/NOTICE +2 -0
- diffct-2.4.3.dist-info/RECORD +0 -33
- {diffct-2.4.3.dist-info → diffct-2.5.0.dist-info}/WHEEL +0 -0
- {diffct-2.4.3.dist-info → diffct-2.5.0.dist-info}/licenses/LICENSE +0 -0
diffct/__init__.py
CHANGED
diffct/_solver_state.py
CHANGED
|
@@ -12,6 +12,7 @@ import torch.distributed as dist
|
|
|
12
12
|
from .pipeline import _fragments
|
|
13
13
|
from .storage import NpyStore, TensorStore
|
|
14
14
|
from .distribution import control_status, preflight
|
|
15
|
+
from .utils import _work_dtype
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
def count(value, name, positive=False):
|
|
@@ -65,20 +66,46 @@ def scalar_sum(operator, value, kind, *, return_tensor=False):
|
|
|
65
66
|
dist.all_reduce(accumulator, group=group)
|
|
66
67
|
return accumulator if return_tensor else accumulator.item()
|
|
67
68
|
if return_tensor:
|
|
68
|
-
return torch.as_tensor(value, dtype=
|
|
69
|
+
return torch.as_tensor(value, dtype=_work_dtype(result_device), device=result_device)
|
|
69
70
|
return float(value)
|
|
70
71
|
|
|
71
72
|
|
|
72
73
|
def squared_norm(operator, value, kind, elements=1048576, *, return_tensor=False):
|
|
73
74
|
store = TensorStore(value) if isinstance(value, torch.Tensor) else value
|
|
74
|
-
|
|
75
|
+
# Scale every block by its own peak before squaring and track the
|
|
76
|
+
# running maximum on the device: unscaled FP32 squares overflow for
|
|
77
|
+
# large measurements and underflow for tiny geometries.
|
|
78
|
+
device = getattr(store, "device", torch.device("cpu"))
|
|
79
|
+
if _work_dtype(device) == torch.float64:
|
|
80
|
+
total = torch.zeros((), dtype=torch.float64, device=device)
|
|
81
|
+
if owns_values(operator, kind):
|
|
82
|
+
for selected in _fragments(tuple(store.shape), elements):
|
|
83
|
+
# Clone even an FP64 view: norms must never alter caller measurements.
|
|
84
|
+
block = store.read(selected).to(dtype=torch.float64).clone()
|
|
85
|
+
# Preserve the block reductions and their sequential FP64 order.
|
|
86
|
+
total.add_(block.square_().sum().to(device=total.device))
|
|
87
|
+
return scalar_sum(operator, total, kind, return_tensor=return_tensor)
|
|
88
|
+
# FP32 devices (MPS): squares of large measurements overflow and tiny
|
|
89
|
+
# geometries underflow, so every block is rescaled by its peak and the
|
|
90
|
+
# running sum follows the largest peak seen. All block math stays on the
|
|
91
|
+
# device; the two closing scalar reads fit the per-iteration reduction
|
|
92
|
+
# budget, and their product leaves FP32 before it can overflow.
|
|
93
|
+
peak = torch.zeros((), device=device, dtype=torch.float32)
|
|
94
|
+
total = torch.zeros((), device=device, dtype=torch.float32)
|
|
75
95
|
if owns_values(operator, kind):
|
|
76
96
|
for selected in _fragments(tuple(store.shape), elements):
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
97
|
+
block = store.read(selected)
|
|
98
|
+
if not block.numel():
|
|
99
|
+
continue
|
|
100
|
+
block_peak = block.detach().abs().amax()
|
|
101
|
+
safe = torch.where(block_peak > 0, block_peak, torch.ones_like(block_peak))
|
|
102
|
+
combined = torch.maximum(peak, block_peak)
|
|
103
|
+
guard = torch.where(combined > 0, combined, torch.ones_like(combined))
|
|
104
|
+
total = (total * (peak / guard).square()
|
|
105
|
+
+ (block.detach() / safe).square().sum() * (block_peak / guard).square())
|
|
106
|
+
peak = combined
|
|
107
|
+
return scalar_sum(operator, peak.item() ** 2 * total.item(), kind,
|
|
108
|
+
return_tensor=return_tensor)
|
|
82
109
|
|
|
83
110
|
|
|
84
111
|
def copy_blocks(source, target, elements):
|
diffct/analytical.py
CHANGED
|
@@ -27,7 +27,7 @@ import math
|
|
|
27
27
|
|
|
28
28
|
import torch
|
|
29
29
|
|
|
30
|
-
from .constants import _DTYPE
|
|
30
|
+
from .constants import _DTYPE, _native_spacing
|
|
31
31
|
from .utils import (
|
|
32
32
|
DeviceManager,
|
|
33
33
|
TorchCUDABridge,
|
|
@@ -431,7 +431,7 @@ def _parallel_weighted_backproject_raw(sinogram, ray_dir, det_origin, det_u_vec,
|
|
|
431
431
|
_resolve_kernel('_parallel_2d_fbp_backproject_kernel')[grid, tpb, numba_stream](
|
|
432
432
|
d_sino, n_views, n_det, d_reco, Nx, Ny,
|
|
433
433
|
_DTYPE(detector_spacing), d_ray_dir, d_det_origin, d_det_u_vec,
|
|
434
|
-
cx, cy,
|
|
434
|
+
cx, cy, _native_spacing(voxel_spacing),
|
|
435
435
|
)
|
|
436
436
|
|
|
437
437
|
return reco * (1.0 / (2.0 * math.pi))
|
|
@@ -522,8 +522,9 @@ def _fan_weighted_backproject_raw(sinogram, src_pos, det_center, det_u_vec,
|
|
|
522
522
|
_resolve_kernel('_fan_2d_fbp_backproject_kernel')[grid, tpb, numba_stream](
|
|
523
523
|
d_sino, n_views, n_det, d_reco, Nx, Ny,
|
|
524
524
|
_DTYPE(detector_spacing), d_src_pos, d_det_center, d_det_u_vec,
|
|
525
|
-
cx, cy,
|
|
526
|
-
*(_DTYPE(value
|
|
525
|
+
cx, cy, _native_spacing(voxel_spacing),
|
|
526
|
+
*(_DTYPE(value if isinstance(voxel_spacing, (tuple, list)) else value / voxel_spacing)
|
|
527
|
+
for value in isocenter.tolist()),
|
|
527
528
|
)
|
|
528
529
|
|
|
529
530
|
return reco
|
|
@@ -584,8 +585,9 @@ def _cone_weighted_backproject_raw(sinogram, src_pos, det_center, det_u_vec, det
|
|
|
584
585
|
d_sino, n_views, n_u, n_v, d_reco, Nx, Ny, Nz,
|
|
585
586
|
_DTYPE(du), _DTYPE(dv),
|
|
586
587
|
d_src_pos, d_det_center, d_det_u_vec, d_det_v_vec,
|
|
587
|
-
cx, cy, cz,
|
|
588
|
-
*(_DTYPE(value
|
|
588
|
+
cx, cy, cz, _native_spacing(voxel_spacing),
|
|
589
|
+
*(_DTYPE(value if isinstance(voxel_spacing, (tuple, list)) else value / voxel_spacing)
|
|
590
|
+
for value in isocenter.tolist()),
|
|
589
591
|
)
|
|
590
592
|
|
|
591
593
|
# Convert back to (D, H, W) layout for the user.
|
|
@@ -637,7 +639,8 @@ def _analytical_transpose_raw(image, beam, geometry, parameters, isocenter, sino
|
|
|
637
639
|
scale = (sdd_n / (2.0 * math.pi * sid_n)).view(
|
|
638
640
|
(-1,) + (1,) * (len(sinogram_shape) - 1)
|
|
639
641
|
)
|
|
640
|
-
iso = tuple(_DTYPE(value
|
|
642
|
+
iso = tuple(_DTYPE(value if isinstance(parameters[-1], (tuple, list)) else value / parameters[-1])
|
|
643
|
+
for value in isocenter.tolist())
|
|
641
644
|
|
|
642
645
|
if beam == "cone":
|
|
643
646
|
D, H, W, du, dv, spacing = parameters
|
|
@@ -647,7 +650,7 @@ def _analytical_transpose_raw(image, beam, geometry, parameters, isocenter, sino
|
|
|
647
650
|
_resolve_kernel('_cone_3d_fdk_transpose_kernel')[grid, tpb, stream](
|
|
648
651
|
d_sino, *sinogram_shape, d_image, W, H, D, _DTYPE(du), _DTYPE(dv),
|
|
649
652
|
*d_geometry, _DTYPE(W * 0.5), _DTYPE(H * 0.5), _DTYPE(D * 0.5),
|
|
650
|
-
|
|
653
|
+
_native_spacing(spacing), *iso,
|
|
651
654
|
)
|
|
652
655
|
else:
|
|
653
656
|
pitch, H, W, spacing = parameters
|
|
@@ -657,7 +660,7 @@ def _analytical_transpose_raw(image, beam, geometry, parameters, isocenter, sino
|
|
|
657
660
|
else '_fan_2d_fbp_transpose_kernel')
|
|
658
661
|
kernel[grid, tpb, stream](
|
|
659
662
|
d_sino, *sinogram_shape, d_image, W, H, _DTYPE(pitch), *d_geometry,
|
|
660
|
-
_DTYPE(W * 0.5), _DTYPE(H * 0.5),
|
|
663
|
+
_DTYPE(W * 0.5), _DTYPE(H * 0.5), _native_spacing(spacing), *iso,
|
|
661
664
|
)
|
|
662
665
|
return sinogram * scale
|
|
663
666
|
|
diffct/analytical_execution.py
CHANGED
|
@@ -16,6 +16,7 @@ from .analytical import (
|
|
|
16
16
|
fan_weighted_backproject, parallel_weighted_backproject,
|
|
17
17
|
)
|
|
18
18
|
from .backends import _zero_dependency, select_backend, voxel_spacing_tuple
|
|
19
|
+
from .constants import _native_spacing
|
|
19
20
|
from .chunking import _memory_budget, _pixel_tiles
|
|
20
21
|
from .distribution import Partition, preflight
|
|
21
22
|
from .operators import _detector_spacing, _normalize_devices, _positive_int, _shape, _validate_trajectory
|
|
@@ -143,6 +144,9 @@ class AnalyticalExecution:
|
|
|
143
144
|
self.rank = 3 if beam == "cone" else 2
|
|
144
145
|
self.global_volume_shape = _shape(volume_shape, self.rank, "volume_shape")
|
|
145
146
|
self.spacing = voxel_spacing_tuple(voxel_spacing, self.rank)
|
|
147
|
+
self.voxel_spacing = self.spacing if isinstance(voxel_spacing, (tuple, list)) else self.spacing[0]
|
|
148
|
+
if self.backend == "numba" and isinstance(self.voxel_spacing, tuple):
|
|
149
|
+
_native_spacing(self.voxel_spacing)
|
|
146
150
|
self.pitch = _detector_spacing(detector_spacing, beam)
|
|
147
151
|
self.devices = _normalize_devices(devices)
|
|
148
152
|
if not isinstance(source, torch.Tensor):
|
|
@@ -156,6 +160,8 @@ class AnalyticalExecution:
|
|
|
156
160
|
raise TypeError("sinogram store must have dtype float32 or float64")
|
|
157
161
|
self.source = source
|
|
158
162
|
self.output_device = torch.device(getattr(source, "device", "cpu"))
|
|
163
|
+
if self.dtype == torch.float64 and self.output_device.type == "mps":
|
|
164
|
+
raise ValueError("precision must be 'fast' or 'float32' on MPS; MPS provides no float64")
|
|
159
165
|
self.detector_shape = tuple(source.shape[1:])
|
|
160
166
|
for component in trajectory:
|
|
161
167
|
_fixed(component, "trajectory")
|
|
@@ -336,14 +342,14 @@ class AnalyticalExecution:
|
|
|
336
342
|
device=data.device, dtype=self.dtype)
|
|
337
343
|
if self.beam == "parallel":
|
|
338
344
|
geometry = (geometry[0], geometry[1] - centre, geometry[2])
|
|
339
|
-
return parallel_weighted_backproject(data, *geometry, self.pitch[0], *shape, self.
|
|
345
|
+
return parallel_weighted_backproject(data, *geometry, self.pitch[0], *shape, self.voxel_spacing)
|
|
340
346
|
geometry = (geometry[0] - centre, geometry[1] - centre, *geometry[2:])
|
|
341
347
|
isocenter = self.isocenter.to(data.device) - centre
|
|
342
348
|
if self.beam == "fan":
|
|
343
349
|
return fan_weighted_backproject(data, *geometry, self.pitch[0], *shape,
|
|
344
|
-
self.
|
|
350
|
+
self.voxel_spacing, isocenter=isocenter)
|
|
345
351
|
return cone_weighted_backproject(data, *geometry, *shape, *self.pitch,
|
|
346
|
-
self.
|
|
352
|
+
self.voxel_spacing, isocenter=isocenter)
|
|
347
353
|
|
|
348
354
|
def _tile(self, spatial):
|
|
349
355
|
shape = tuple(part.stop - part.start for part in spatial)
|