Skip to content

Commit ea0718f

Browse files
authored
Merge pull request #248 from DiamondLightSource/radway_69_iterative_lprec_estimator
Add flag to LPRec to turn on/off memory estimator
2 parents 51e8710 + 4c080c0 commit ea0718f

4 files changed

Lines changed: 203 additions & 80 deletions

File tree

httomolibgpu/memory_estimator_helpers.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

httomolibgpu/prep/phase.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import numpy as np
2424
from httomolibgpu import cupywrapper
25-
from httomolibgpu.memory_estimator_helpers import _DeviceMemStack
25+
from tomobar.supp.memory_estimator_helpers import DeviceMemStack
2626

2727
cp = cupywrapper.cp
2828
cupy_run = cupywrapper.cupy_run
@@ -117,7 +117,7 @@ def paganin_filter(
117117
)
118118
###################################
119119

120-
mem_stack = _DeviceMemStack() if calc_peak_gpu_mem else None
120+
mem_stack = DeviceMemStack() if calc_peak_gpu_mem else None
121121
if not mem_stack:
122122
# Check the input data is valid
123123
__check_if_data_3D_array(data, methods_name)
@@ -327,7 +327,7 @@ def _pad_projections(
327327
"next_power_of_2", "next_fast_length", "use_pad_x_y"
328328
],
329329
pad_x_y: Optional[list],
330-
mem_stack: Optional[_DeviceMemStack],
330+
mem_stack: Optional[DeviceMemStack],
331331
) -> Tuple[cp.ndarray, Tuple[int, int]]:
332332
"""
333333
Performs padding of each projection to a size optimal for FFT.

httomolibgpu/prep/stripe.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from unittest.mock import Mock
3131

3232
if cupy_run:
33+
from tomobar.supp.memory_estimator_helpers import DeviceMemStack
3334
from cupyx.scipy.ndimage import median_filter, binary_dilation, uniform_filter1d
3435
from cupyx.scipy.fft import fft2, ifft2, fftshift
3536
from cupyx.scipy.fftpack import get_fft_plan
@@ -226,32 +227,8 @@ def _reflect(x: np.ndarray, minx: float, maxx: float) -> np.ndarray:
226227
return np.array(out, dtype=x.dtype)
227228

228229

229-
class _DeviceMemStack:
230-
def __init__(self) -> None:
231-
self.allocations = []
232-
self.current = 0
233-
self.highwater = 0
234-
235-
def malloc(self, bytes):
236-
self.allocations.append(bytes)
237-
allocated = self._round_up(bytes)
238-
self.current += allocated
239-
self.highwater = max(self.current, self.highwater)
240-
241-
def free(self, bytes):
242-
assert bytes in self.allocations
243-
self.allocations.remove(bytes)
244-
self.current -= self._round_up(bytes)
245-
assert self.current >= 0
246-
247-
def _round_up(self, size):
248-
ALLOCATION_UNIT_SIZE = 512
249-
size = (size + ALLOCATION_UNIT_SIZE - 1) // ALLOCATION_UNIT_SIZE
250-
return size * ALLOCATION_UNIT_SIZE
251-
252-
253230
def _mypad(
254-
x: cp.ndarray, pad: Tuple[int, int, int, int], mem_stack: Optional[_DeviceMemStack]
231+
x: cp.ndarray, pad: Tuple[int, int, int, int], mem_stack: Optional[DeviceMemStack]
255232
) -> cp.ndarray:
256233
"""Function to do numpy like padding on Arrays. Only works for 2-D
257234
padding.
@@ -294,7 +271,7 @@ def _conv2d(
294271
w: np.ndarray,
295272
stride: Tuple[int, int],
296273
groups: int,
297-
mem_stack: Optional[_DeviceMemStack],
274+
mem_stack: Optional[DeviceMemStack],
298275
) -> cp.ndarray:
299276
"""Convolution (equivalent pytorch.conv2d)"""
300277
b, ci, hi, wi = x.shape if not mem_stack else x
@@ -378,7 +355,7 @@ def _conv_transpose2d(
378355
stride: Tuple[int, int],
379356
pad: Tuple[int, int],
380357
groups: int,
381-
mem_stack: Optional[_DeviceMemStack],
358+
mem_stack: Optional[DeviceMemStack],
382359
) -> cp.ndarray:
383360
"""Transposed convolution (equivalent pytorch.conv_transpose2d)"""
384361
b, co, ho, wo = x.shape if not mem_stack else x
@@ -473,7 +450,7 @@ def _afb1d(
473450
h0: np.ndarray,
474451
h1: np.ndarray,
475452
dim: int,
476-
mem_stack: Optional[_DeviceMemStack],
453+
mem_stack: Optional[DeviceMemStack],
477454
) -> cp.ndarray:
478455
"""1D analysis filter bank (along one dimension only) of an image
479456
@@ -521,7 +498,7 @@ def _sfb1d(
521498
g0: np.ndarray,
522499
g1: np.ndarray,
523500
dim: int,
524-
mem_stack: Optional[_DeviceMemStack],
501+
mem_stack: Optional[DeviceMemStack],
525502
) -> cp.ndarray:
526503
"""1D synthesis filter bank of an image Array"""
527504

@@ -565,7 +542,7 @@ def __init__(self, wave: str):
565542
self.h1_row = np.array(h1_row).astype("float32")[::-1].reshape((1, 1, 1, -1))
566543

567544
def apply(
568-
self, x: cp.ndarray, mem_stack: Optional[_DeviceMemStack] = None
545+
self, x: cp.ndarray, mem_stack: Optional[DeviceMemStack] = None
569546
) -> Tuple[cp.ndarray, cp.ndarray]:
570547
"""Forward pass of the DWT.
571548
@@ -627,7 +604,7 @@ def __init__(self, wave: str):
627604
def apply(
628605
self,
629606
coeffs: Tuple[cp.ndarray, cp.ndarray],
630-
mem_stack: Optional[_DeviceMemStack] = None,
607+
mem_stack: Optional[DeviceMemStack] = None,
631608
) -> cp.ndarray:
632609
"""
633610
Args:
@@ -728,7 +705,7 @@ def remove_stripe_fw(
728705
sli_shape = [nz, 1, nproj_pad, ni]
729706

730707
if calc_peak_gpu_mem:
731-
mem_stack = _DeviceMemStack()
708+
mem_stack = DeviceMemStack()
732709
# A data copy is assumed when invoking the function
733710
mem_stack.malloc(np.prod(data) * np.float32().itemsize)
734711
mem_stack.malloc(np.prod(sli_shape) * np.float32().itemsize)

0 commit comments

Comments
 (0)