#cuFFT memory management

1 messages · Page 1 of 1 (latest)

hollow dust
#

In the current project I am working at, we are using tf2 but tensorflow.compat.v1 as tf.
I am using tf.signal.fft2d and tf.signal.ifft2d to filter frequencies from images.
It works but the program slowly starts using more and more memory during the training and when the GPU gets full it just stops the training.
I have tried to do many different things like memory allocator growth and others. I am sure that the fft2d and ifft2d are the problem since I isolated the code from them and the problem disappeared, so its something with them and cuFFT, I believe.
I am lost in what I can do to fix this.

The function in question:

def apply_fft_filter_tensor(images: tf.Tensor, mask: tf.Tensor) -> tf.Tensor:
"""
images: float32 Tensor, shape [B, H, W, C]
freq_ranges: float32 Tensor, shape [n_ranges, 2]
Returns: real-valued float32 Tensor [B, H, W, C]
"""
# 1. Shapes
B = tf.shape(images)[0]
H = tf.shape(images)[1]
W = tf.shape(images)[2]
C = images.shape[3] # static channel count

# 3. Flatten batch & channels → [B*C, H, W]
x = tf.reshape(images, [B * C, H, W])
x_c = tf.cast(x, tf.complex64)

# 4. FFT, mask, IFFT
Xf     = tf.signal.fft2d(x_c) #MEMORY GROWTH HERE
Xf_f   = Xf * mask
x_ifft = tf.signal.ifft2d(Xf_f) #MEMORY GROWTH HERE

# 5. Back to real, reshape → [B, H, W, C]
x_real = tf.math.real(x_ifft)
x_out  = tf.reshape(x_real, [B, H, W, C])

x_out  = tf.ensure_shape(x_out, [None, None, None, C])

return x_out

and the mask used is created outside the loop once.

tender cloak
#

Are you able to recreate this gpu memory issue with TF2? How does it behave a various batch sizes?

#

I notice you're creating a new variable to hold the output of each operation. Try using just one and overwriting it

tender cloak
#

The variable recommendation might not apply if not using eager execution (default on in TF2)

hollow dust
#

In a quick tensorflow 2 test, it does the same thing, created a random image batch of 8,128,128,3. Looped 1000000, used the function as shown above. Memory usage goes up in a constant way.

#

The number of channels, height and width don't seem to matter either.

#

I am using tensorflow 2.11, forgot to mention this.

#

Maybe updating it could solve it?

tender cloak
hollow dust
tender cloak
#

are you using api from the compat namespace?

hollow dust
#

I created a new environment and installed 2.15, I am running the code now and it seems to have stopped the problem

#

Thanks for helping!