代码之家  ›  专栏  ›  技术社区  ›  Basj

加速TensorFlow的初始启动

  •  1
  • Basj  · 技术社区  · 4 年前

    每次我使用TensorFlow(CPU)运行Python代码时,例如:

    import keras
    

    我看到这个:

    2020-10-30 15:27:20.518894: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
    2020-10-30 15:27:20.518894: 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.
    2020-10-30 15:27:23.713077: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
    2020-10-30 15:27:23.713077: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
    2020-10-30 15:27:23.716077: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: User1-PC
    2020-10-30 15:27:23.716077: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: User1-PC
    2020-10-30 15:27:23.729078: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x10cad0c0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
    2020-10-30 15:27:23.729078: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
    Using TensorFlow backend.
    

    如果我把所有的等待时间加起来,大约有10秒的等待时间。

    有没有办法加快这一进程? 特别是如果我使用TensorFlow进行推理(而不是训练),我不想在每次启动引擎时等待10秒。


    注意:当然,当我的代码准备就绪时,我会使用TensorFlow保持进程不断运行,并使用某种进程间通信,以避免重新启动整个程序。

    我在这里的问题主要是 原型制作 阶段,当你 经常 需要重新启动程序:在原型制作时,每次脚本启动都要等待10或15秒,这非常不方便。

    0 回复  |  直到 4 年前
        1
  •  3
  •   AKX Bryan Oakley    4 年前

    对于您的推理问题,您可能需要一个寿命更长的进程,您可以通过HTTP、gRPC、XML-RPC、命名管道、从目录读取文件等方式请求推理结果。。。?

    如果做不到这一点,那就换一台更快的机器或磁盘。在我的机器上,启动一个新的Python进程并导入Keras大约需要2秒:

    $ pip install tensorflow
    Collecting tensorflow
      Downloading tensorflow-2.3.1-cp38-cp38-macosx_10_14_x86_64.whl (165.2 MB)
    [...]
    Successfully installed absl-py-0.11.0 astunparse-1.6.3 cachetools-4.1.1 chardet-3.0.4 gast-0.3.3 google-auth-1.23.0 google-auth-oauthlib-0.4.2 google-pasta-0.2.0 grpcio-1.33.2 h5py-2.10.0 idna-2.10 keras-preprocessing-1.1.2 markdown-3.3.3 numpy-1.18.5 oauthlib-3.1.0 opt-einsum-3.3.0 packaging-20.4 protobuf-3.13.0 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.24.0 requests-oauthlib-1.3.0 rsa-4.6 tensorboard-2.3.0 tensorboard-plugin-wit-1.7.0 tensorflow-2.3.1 tensorflow-estimator-2.3.0 termcolor-1.1.0 werkzeug-1.0.1 wrapt-1.12.1
    $ time python -c 'import tensorflow.keras as keras'
    
    ________________________________________________________
    Executed in    2.02 secs   fish           external
       usr time    2.85 secs  118.00 micros    2.85 secs
       sys time    0.62 secs  946.00 micros    0.62 secs