Repository navigation
Closed
Description
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes.
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): v1.12.1-2319-g81f2165 2.0.0-dev20190520
- Python version: 3.7
- Bazel version (if compiling from source):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version: 10.1/7.1
- GPU model and memory:
Describe the current behavior
When using tf.function, the cpu memory usage increase forever.
Describe the expected behavior
No cpu memory leak when training models on gpu device using 'tf.function'.
Code to reproduce the issue
A part of my code, since the whole source code is huge...
And the dataset_train in the following code is a tf.data.DataSet object.
DATA_TRAIN_SPEC = [
tf.TensorSpec(shape=(None, IMAGE_FIX_HEIGHT, None, 3), dtype=tf.float32),
tf.TensorSpec(shape=None, dtype=tf.int32),
tf.TensorSpec(shape=None, dtype=tf.int32),
tf.TensorSpec(shape=None, dtype=tf.int32),
]
# @tf.function(input_signature=DATA_TRAIN_SPEC)
def train_step(*batch_data):
imgs, labels, imgs_width, labels_len = batch_data
with tf.GradientTape() as tape:
logits = model(imgs, training=True)
logits_len = tf.cast(tf.math.ceil(
tf.divide(imgs_width, block_size)), tf.int32)
loss, accuracy = loss_fn(
labels, logits, labels_len, logits_len, CLASSES)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
return loss, accuracy
def train_epoch(epoch):
print(f'Epoch {epoch} training start ...')
for step, batch_data in enumerate(dataset_train.take(num)):
loss, accuracy = train_step(*batch_data)
if (step+1) % ARGS.show_per_iterations == 0:
loss, accuracy = get_numpy((loss, accuracy))
loss_mean = np.mean(loss)
accuracy_mean = np.mean(accuracy)*100
prefix = f'Epoch#{epoch}/Step#{step+1}/training'
print(f'Loss of {prefix} is: {"%.6f"%loss_mean} ...')
print(f'Accuracy of {prefix} is: {"%.3f"%accuracy_mean}% ...')
print(f'Epoch {epoch} training finish ...\n')
Activity
[-]Cpu memory leak when using `tf.function`.[/-][+]Cpu memory leak when using `tf.function` with gpu model.[/+]jiarenyf commentedon May 25, 2019
@muddham @ry @jmhodges @eggie5 Can anyone help me, please, thank you~
jvishnuvardhan commentedon Jun 11, 2019
@jiarenyf Can you check this issue which is similar to yours and try it with TF.2.0.0-beta0 and let us know how it works. Thanks!
guillaumekln commentedon Jun 14, 2019
Possibly similar to #29075.
jiarenyf commentedon Jun 16, 2019
@jvishnuvardhan Thank you for the suggestion.
However, the problem still occurs after
pip install tensorflow-gpu==2.0.0-beta0.I also tried
tf.keras.xxx.clear_session, and it took no effect for me.I have uploaded the code in the_codes, could you please help me to debug, thank you.
You could run the code by:
I have refactor the code, so it look different from the one I mentioned 20 days ago, while the problem is the same:
When running without
@tf.functionthe cpu_memory cost is as most 4.8GB and the gpu_util is 70%~100%, while running with@tf.functionthe cpu_memory keep increasing till over 10GB and the gpu_util is always 0%.Thank you.
jiarenyf commentedon Jun 19, 2019
Since no one answers this question, I will delete the codes uploaded in one drive and switch my codes to use pytorch instead. Bye bye.
tensorflow-bot commentedon Jun 19, 2019
Are you satisfied with the resolution of your issue?
Yes
No
Ain-Crad commentedon Aug 31, 2019
Check if batch_data which in the train_step() function has changed its dimention(or some data in batch_data is accumulating) every epoch. Because if that @tf.function will create a new graph every epoch.
(Have been troubled by this problem for a long time, and finally solved with help of a classmate)
Have a good day.
@jiarenyf
zxk19981227 commentedon Nov 21, 2019
Sir have you solved your problem? I also met a problem like this but some reshape is used in my programme but i have no idea how to fixed the leak of CPU memory.
Ain-Crad commentedon Nov 21, 2019
Hi,
The follow @tf.function instruction in Tensorflow may provide some clues:
More information about @tf.function.
So, check if have obey this, otherwise it will generate new graph each batch, and that will significantly raise your CPU memory.
Good luck.
moulicm111 commentedon Jun 8, 2020
Thank you @Ain-Crad . tf-function decorator is main culprit for increase of ram .
In my case
total_loss = loss_1(inp,tar) + lambda*loss_2(inp,tar)does lambda here cause a problem