【问题标题】:Tensorflow stops training and hanged on GPU randomlyTensorflow 停止训练并随机挂在 GPU 上
【发布时间】:2021-08-07 03:50:37
【问题描述】:

当我在 GPU 上运行以下代码时,它在某些时期训练得很好,然后就挂了。 虽然挂起的进程仍然存在,但 GPU 使用率变为 0%。 在下面的代码中,我使用来自 tf.contrib.data.Dataset 的数据集 API。但我也尝试使用占位符和提要字典方法,该方法在训练期间也挂在随机时期。 在过去的 2-3 周里,我一直在努力解决这个问题,但找不到出路。 我在远程 GPU 集群上运行代码。以下是有关集群节点的一些信息, 使用 tensorflow gpu 1.4 版

NodeName=node050 Arch=x86_64 CoresPerSocket=1 CPUAlloc=0 CPUErr=0 CPUTot=24 CPULoad=12.03 Features=Proc24,GPU4 Gres=gpu:4
NodeAddr=node050 NodeHostName=node050 版本=15.08 OS=Linux RealMemory=129088 AllocMem=0 FreeMem=125664 Sockets=24 Boards=1
State=IDLE ThreadsPerCore=1 TmpDisk=0 Weight=1 Owner=N/A
启动时间=2017-11-07T08:20:00 SlurmdStartTime=2017-11-07T08:24:06
CapWatts=n/a CurrentWatts=0 LowestJoules=0 ConsumedJoules=0
ExtSensorsJoules=n/s ExtSensorsWatts=0 ExtSensorsTemp=n/s

代码

dat_split = np.load('data/dat_split2.npy')
X_train = dat_split[0].astype(np.float32)
X_test = dat_split[1].astype(np.float32)
y_train = dat_split[2].astype(np.int32)
y_test = dat_split[3].astype(np.int32)

num_epochs = 100


train_data_len = X_train.shape[0]
test_data_len = X_test.shape[0]
num_joints = len(considered_joints)
num_classes = len(classes)


############ taking batch_size even data##########
even_train_len = (train_data_len//batch_size)*batch_size
even_test_len = (test_data_len//batch_size)*batch_size

X_train = X_train[:even_train_len]
X_test = X_test[:even_test_len]
y_train = y_train[:even_train_len]
y_test = y_test[:even_test_len]


train_dat = Dataset.from_tensor_slices((X_train, y_train))
train_dat = train_dat.batch(batch_size)

test_dat  = Dataset.from_tensor_slices((X_test, y_test))
test_dat = test_dat.batch(batch_size)

iterator = Iterator.from_structure(train_dat.output_types, train_dat.output_shapes)

trainig_iterator_init = iterator.make_initializer(train_dat)
test_iterator_init = iterator.make_initializer(test_dat)

if __name__ == '__main__':

    global_cell = GlobalLSTM(num_units=num_units_each_cell, num_joints=num_joints)   #GlobalLSTM is a subtype of RNNCell
    next_element = iterator.get_next()
    X_loaded2, Y_loaded = next_element
    X_loaded = tf.where(tf.is_nan(X_loaded2), tf.zeros_like(X_loaded2), X_loaded2)

    init_state = global_cell.zero_state((batch_size), tf.float32)
    rnn_ops, rnn_state = tf.nn.dynamic_rnn(global_cell, X_loaded, dtype=tf.float32)

    with tf.variable_scope('softmax__'):
        W = tf.get_variable('W', [(num_joints)*num_units_each_cell, num_classes], initializer=tf.truncated_normal_initializer(0.0, 1.0))
        b = tf.get_variable('b', [num_classes], initializer=tf.truncated_normal_initializer(0.0, 1.0))



    final_logits = tf.matmul(rnn_state[1], W) + b       # taking h state of rnn 
    with tf.name_scope("loss_comp"):
        total_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=final_logits, labels=tf.one_hot(Y_loaded, num_classes)))
    with tf.name_scope("train_step"):
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss)

    with tf.name_scope("pred_accu"):
        predictions = tf.nn.softmax(final_logits)
        pred2 = tf.reshape(tf.argmax(predictions, 1), [-1, 1])
        correct_pred = tf.equal(pred2, tf.cast(Y_loaded, tf.int64))
        accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32))



    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        tic = time.clock()    
        for step in range(num_epochs):
            sess.run(trainig_iterator_init)
            batch_cnt = train_data_len//batch_size
            epch_loss = 0.0
            epch_acc = 0.0
            for bt in range(batch_cnt):
                _, loss_, acc = sess.run([train_step, total_loss, accuracy_])
                epch_loss += loss_
                epch_acc += acc
            print ('loss after epoch, ', step,': ', epch_loss/batch_cnt, ' ## accuracy : ', epch_acc/batch_cnt)

        print ("optimization finished, time required: ", time.clock()-tic)


        #############test accuracy##############
        batch_cnt = test_data_len//batch_size
        sess.run(test_iterator_init)
        print ('testing accuracy on test data : batch number', batch_cnt)
        epch_acc = 0.0
        for bt in range(batch_cnt):
            acc = sess.run(accuracy_)
            epch_acc += acc
        print ('testing accuracy : ', epch_acc/batch_cnt)  

这里有一些不同挂起的屏幕截图, 挂在一个时代 当时的 GPU 使用率 运行时 GPU 使用情况(未挂起) 挂在另一个时代

这种随机悬挂行为会在每次运行时不断重复。 每次它挂在一个随机的时期。这就是为什么我无法弄清楚出了什么问题。 通过查看代码或其他设置,任何人都可以告诉我发生了什么问题或如何调试它?谢谢

【问题讨论】:

  • 嗯,开始调试的一种方法是在 TensorFlow 挂起时提取堆栈跟踪(例如,在 gdb 中运行 Python 并在卡住时向其发送 SIGINT)。你能在单个 GPU 上重现吗?

标签: tensorflow slurm


【解决方案1】:

我遇到了同样的问题。当 GPU 挂起时,一个 CPU 内核的工作负载为 100%。我认为这是锁的问题,并检查了 tensorflow gpu_event_mgr.cc 的代码。 "queue_empty" 不受 mutex_lock 保护,因此 CPU 会挂起,无法向 GPU 发送数据。

我的临时解决方案是设置参数

gpu_options.polling_inactive_delay_msecs = 10 (default value is 1)

或会话的 ConfigProto 中更大的数字。这将使 CPU 在队列为空时等待更长的时间并填充更多数据,这些数据将被发送到 GPU。它将防止死锁。该解决方案只是降低了 GPU 挂起的可能性,并不是最终解决方案。但是,它让我的深度神经网络训练大部分时间都完成了。

【讨论】:

  • 感谢您的回复。在我的情况下也是如此,如果我使用“kill -stop p_id”暂停该过程,然后使用“kill -cont p_id”恢复它,则培训将从它停止的确切位置开始。我编写了另一个脚本,它以 60 秒的间隔执行此操作,以便可以继续训练。
【解决方案2】:

我解决了一个类似的问题如下:

我在使用 GTX 1080 Ti GPU、Python 3.7 和最新版本的 tensorflow-gpu 2.1 的虚拟设备上运行我的代码。我有一个星期同样的问题。似乎没有任何解决方案和变通方法对我有用。

我降级了我的 tensorflow-gpu。我还不得不降级我的 Python,因为 tensorflow-gpu 1.* 版本在 Python 3.7 上不起作用。

  • Python 从 3.7 到 3.6
  • Tensorflow-gpu 从 2.1 到 1.12

当然,我不得不更改我的代码的某些部分以与旧版本的 tensorflow-gpu 兼容,但它摆脱了 GPU 挂起问题。

【讨论】:

    【解决方案3】:

    总是需要与 CUDA 内存相同或更高的 RAM 内存。

    1. 尝试检查 num_workers

    【讨论】:

      【解决方案4】:

      Halo9Pan 的回答很棒,让我走上了正确的道路。我添加了代码

      configproto = tf.compat.v1.ConfigProto() 
      configproto.gpu_options.allow_growth = True
      configproto.gpu_options.polling_inactive_delay_msecs = 10
      sess = tf.compat.v1.Session(config=configproto) 
      tf.compat.v1.keras.backend.set_session(sess)
      

      到我的网络开头,如this question 中所述。这为我解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-13
        • 1970-01-01
        • 1970-01-01
        • 2017-03-30
        • 2017-09-10
        • 2020-11-27
        • 1970-01-01
        • 2021-04-19
        相关资源
        最近更新 更多