failed to query event: CUDA_ERROR_ILLEGAL_INSTRUCTION: an illegal instruction was encountered

在这里插入图片描述
报错信息:
Error polling for event status: failed to query event: CUDA_ERROR_ILLEGAL_INSTRUCTION: an illegal instruction was encountered
2022-03-24 23:32:13.170887: F tensorflow/core/common_runtime/gpu/gpu_event_mgr.cc:273] Unexpected Event status: 1

情况描述:
我自定义了一个损失函数

        def amp_loss(y_true, y_pred):  #其实就是幅频特性的损失
            #tf.squeeze先去掉axis=1的维度,因为Computes the 1-dimensional discrete Fourier transform of a real-valued signal over the inner-most dimension of input.
            #tf.signal.rfft做DFT
            #tf.math.abs求幅值
            #tf.expand_dims还原原来的axis=1的维度
            
                amplitude_true = tf.expand_dims(tf.math.abs( tf.signal.rfft(tf.squeeze(y_true))),-1)
                amplitude_pred = tf.expand_dims(tf.math.abs( tf.signal.rfft(tf.squeeze(y_pred))),-1)

                amplitude_loss = tf.math.reduce_mean(tf.math.square(amplitude_true - amplitude_pred))

                return amplitude_loss

报错是在model.fit的时候出现的,考虑是以为损失函数中的某些算术操作我的cuda不支持

1.检查cudnn版本有无问题
像我的笔记本是tensorflow2.2 GPU版本,cudnn好像是7.6.5

我尝试在aws sagemaker studio lab可以运行
上面的是tensorflow-gpu 2.6.2 cudnn=8.2.1

2.尝试使用CPU运行
在import tensorflow as tf 前面
加入

import os

#用CPU跑
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

以及尝试使用

with tf.device('/cpu:0'):

强制模型运行在CPU上,实测无问题。

猜你喜欢

转载自blog.csdn.net/aa2962985/article/details/123720909