问题

什么原因导致“检查失败:ret==0(11对0)通过 pthread_create()创建线程失败”在张量流中


我用 python 制作了一个 discord 机器人,现在使用 Tensorflow 和 NLTK 在其中添加了“聊天机器人”功能。当我在本地运行 bot 时,它绝对可以正常工作,没有任何问题,但当我将其移动到我的 namecheap 托管包中,在那里托管我的投资组合时,它开始给出一个错误,说:

OpenBLAS blas_thread_init: pthread_create failed for thread 29 of 64: Resource temporarily unavailable

NLTK 和 tensorflow 没有导入,机器人就崩溃了。

我用谷歌搜索并找到了一个解决方案,它告诉在使用任何导入之前使用 os.environ['OPENBLAS_NUM_THREADS'] = '1' 。这解决了以前的错误,但现在它给出了另一个错误:

Check failed: ret == 0 (11 vs. 0)Thread creation via pthread_create() failed.

现在运行 python main.py 的完整输出是:

2021-06-10 11:18:19.606471: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2021-06-10 11:18:19.606497: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-06-10 11:18:21.090650: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2021-06-10 11:18:21.090684: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-06-10 11:18:21.090716: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (server270.web-hosting.com): /proc/driver/nvidia/version does not exist
2021-06-10 11:18:21.091042: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-06-10 11:18:21.092409: F tensorflow/core/platform/default/env.cc:73] Check failed: ret == 0 (11 vs. 0)Thread creation via pthread_create() failed.

为了不让这个问题太长,源文件已经托管在 GitHub 上:https://github.com/Nalin-2005/The2020CoderBot 并且 README.md 告诉哪些文件包含哪些部分的机器人。

该机器人托管在 Namecheap 共享主机上,有关服务器的详细信息和技术规格如下:

  • RAM:1GB
  • 存储:20GB SSD
  • CPU(使用 cat /proc/cpuinfo | grep 'model name' | uniq):Intel(R) Xeon(R) Gold 6140 CPU @ 2.30GHz

据我所知,这两个问题都是由于 RAM 或 CPU 使用率有限造成的。但现在,Python 脚本本身阻止了使用。
那么,是什么导致了这一点(如果我是错的),我如何解决这个问题?

推荐答案

经过一段时间的头脑风暴和谷歌搜索,我发现 Tensorflow Lite,它消耗更少的资源,但提供相同的性能*在我的服务器上,我可以很容易地将它与之前的代码集成,以产生一个更节省资源的模型。对于想要知道如何将 keras 模型转换为 Tensorflow lite 的用户,下面是说明。

  1. 在训练时,将 model.save("/path/to/model.h5") 替换为:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open("/path/to/model.tflite", "wb") as f:
    f.write(tflite_model)
  1. 使用时,请使用:
model = tf.lite.Interpreter("/path/to/model.tflite")
model.allocate_tensors()
input_details = model.get_input_details()
output_details = model.get_output_details()

# prepare input data

model.set_tensor(input_details[0]['index'],input_data)
model.invoke()
output_data = model.get_tensor(output_details[0]['index'])
results = np.squeeze(output_data)