developer tip

AMD GPU와 함께 Keras 및 Tensorflow 사용

copycodes 2020. 11. 27. 08:22
반응형

AMD GPU와 함께 Keras 및 Tensorflow 사용


나는 Tensorflow와 Theano의 최상위 레이어라고 믿는 Keras를 배우기 시작했습니다. 하지만 AMD R9 280X와 같은 AMD GPU에만 액세스 할 수 있습니다.

OpenCL에 대한 Keras / Tensorflow 지원을 통해 AMD GPU를 사용할 수 있도록 Python 환경을 설정하려면 어떻게해야합니까?

OSX에서 실행 중입니다.


https://github.com/hughperkins/tensorflow-cl 에서 Tensorflow 용 OpenCL 1.2 백엔드를 작성하고 있습니다.

이 OpenCL 용 tensorflow 포크에는 다음과 같은 특성이 있습니다.

  • 모든 / 모든 OpenCL 1.2 장치를 대상으로합니다. OpenCL 2.0, SPIR-V 또는 SPIR이 필요하지 않습니다. 공유 가상 메모리가 필요하지 않습니다. 등등 ...
  • 'cuda-on-cl', https://github.com/hughperkins/cuda-on-cl 이라는 기본 라이브러리를 기반으로합니다.
    • cuda-on-cl 타겟은 모든 NVIDIA® CUDA ™ 소스 코드 를 가져와 OpenCL 1.2 장치 용으로 컴파일 할 수 있습니다. 이것은 매우 일반적인 목표이며 매우 일반적인 컴파일러입니다.
  • 지금은 다음 기능이 구현됩니다.
  • Ubuntu 16.04 (Intel HD5500 및 NVIDIA GPU 사용) 및 Mac Sierra (Intel HD 530 및 Radeon Pro 450 사용)에서 개발되었습니다.

이것은 Tensorflow의 유일한 OpenCL 포크가 아닙니다. Computecpp, https://www.codeplay.com/products/computesuite/computecpp를 사용하여 Codeplay https://www.codeplay.com 에서 개발중인 포크도 있습니다. 그들의 포크는 저보다 더 강력한 요구 사항을 가지고 있습니다. 어떤 특정 GPU 장치에서 작동하는지 알 수 있습니다. 장치가 지원되는지 확인하려면 플랫폼 지원 정보 (computecpp 페이지 하단)를 확인해야합니다. 코드 플레이 포크는 실제로 공식 Google 포크입니다. https://github.com/benoitsteiner/tensorflow-opencl


이 게시물의 원래 질문은 Keras 및 Tensorflow를 AMD GPU로 실행하는 방법이었습니다.

이 질문에 대한 답은 다음과 같습니다.

1.) Tensorflow가 올바르게 작동하도록 할 수 있으면 Keras가 작동합니다 (선택적으로 가상 / 콘다 환경 내에서).

2.) 다른 사람들이 말했듯이 Tensorflow가 AMD GPU에서 작동하도록하려면, 이것이 작동 할 수있는 한 가지 방법은 Tensorflow를 컴파일하여 OpenCl을 사용하는 것입니다. 그렇게하려면 아래 링크를 읽으십시오. 그러나 간결성을 위해 여기에 필요한 단계를 요약하겠습니다.

  • AMD의 독점 드라이버가 필요합니다. 현재 Ubuntu 14.04에서만 사용할 수 있습니다 (Ubuntu가 UI 렌더링 방식을 변경하기로 결정하기 이전 버전). Ubuntu 16.04에 대한 지원은 AMDProDrivers를 통해 몇 개의 GPU로 제한되는이 게시물 작성 시점에 있습니다. AMD GPU에서 딥 러닝을 수행하려는 독자는 이것을 알고 있어야합니다!

  • OpenCl 지원을 사용하여 Tensorflow를 컴파일하려면 OpenCl 헤더, ComputeCpp와 같은 전제 조건을 획득하고 설치해야합니다.

  • 전제 조건이 충족되면 빌드를 구성하십시오. Tensorflow를 컴파일하는 데에는 3 가지 옵션이 있습니다. 또한 opencl 버전 중 하나에서 빌드하기로 결정한 경우 opencl을 사용하고 있다고 가정하므로 opencl 사용에 대한 질문이 누락됩니다. 반대로 표준 텐서 플로우에서 구성하는 경우 구성 스크립트가 CUDA에 대해 opencl을 사용하고 "NO"를 사용하도록 요청할 때 "예"를 선택해야합니다.

  • 그런 다음 다음과 같이 테스트를 실행합니다.

    $ bazel test --config = sycl -k --test_timeout 1600-// tensorflow / ...-// tensorflow / contrib / ...-// tensorflow / java / ...-// tensorflow / compiler / ...

업데이트 : 내 설정에서이 작업을 수행하는 데 너무 오래 걸립니다. 오래 걸리는 부분은 실행중인 모든 테스트입니다. 이것이 무엇을 의미하는지 잘 모르겠지만 많은 테스트가 1600 초에 시간 초과됩니다. 더 많은 테스트 시간이 초과되면 기간이 단축 될 수 있습니다. 또는 테스트없이 텐서 흐름을 빌드 할 수 있습니다. 이 글을 쓰는 시점에서 테스트를 실행하는 데 이미 2 일이 걸렸습니다.

또는 다음과 같이 pip 패키지를 빌드하십시오.

bazel build --local_resources 2048,.5,1.0 -c opt --config=sycl //tensorflow/tools/pip_package:build_pip_package

실제로 Codeplay에서 블로그 게시물을 읽어보세요. Lukas Iwansky가 2017 년 3 월 30 일에 Tensorflow를 OpenCl과 함께 작동하도록하는 방법에 대한 포괄적 인 자습서 게시물을 게시했습니다. 이것은 매우 최근 게시물입니다. 여기에 쓰지 않은 세부 사항도 있습니다.

위의 많은 게시물에서 알 수 있듯이, 인터넷을 통해 약간의 정보가 확산됩니다. Lukas의 게시물이 가치 측면에서 추가 한 것은 모든 정보가 한곳에 모였기 때문에 Tensforflow 및 OpenCl을 설정하는 것이 조금 덜 부담 스럽습니다. 여기에 링크 만 제공합니다.

https://www.codeplay.com/portal/03-30-17-setting-up-tensorflow-with-opencl-using-sycl

조금 더 완전한 연습이 여기에 게시되었습니다.

http://deep-beta.co.uk/setting-up-tensorflow-with-opencl-using-sycl/

주로 사용자에게 다음을 수행해야 함을 명시 적으로 알리는 방식이 다릅니다.

  • 하위 폴더에 대한 심볼릭 링크 만들기
  • 그런 다음 "python setup.py develop"명령을 통해 실제로 tensorflow를 설치합니다.

위에서 tensorflow-cl을 사용하여 대체 접근 방식이 언급되었습니다.

https://github.com/hughperkins/tensorflow-cl

이 접근 방식이 덜 활성화 된 것처럼 보이지만 현재로서는 어떤 접근 방식이 더 나은지 식별 할 수 없습니다. 더 적은 문제가 게시되고 이러한 문제를 해결하기위한 대화가 더 적게 발생합니다. 작년에 큰 압박이있었습니다. Hugh는이 게시물을 쓰는 시점에서 며칠 전에 몇 가지 업데이트를 푸시 한 것으로 보이지만 2016 년 11 월 이후 추가 푸시가 사라졌습니다. (업데이트 : 문서 readme 중 일부를 읽으면이 버전의 tensorflowo는 이제 메인 개발자가 삶에 바쁘기 때문에 커뮤니티 지원에만 의존합니다.)

업데이트 (2017-04-25) : 아래 tensorflow-opencl 테스트를 기반으로 한 메모가 있습니다.

  • 이 패키지의 미래 사용자는 opencl을 사용한다는 것은 컴퓨팅 측면에서 모든 무거운 작업이 GPU로 이동한다는 것을 의미합니다. 저는 개인적으로 컴퓨팅 작업 부하가 CPU와 iGPU간에 공유 될 것이라고 생각했기 때문에 이것을 언급했습니다. 즉, GPU의 성능이 매우 중요합니다 (특히 대역폭 및 사용 가능한 VRAM).

다음은 MY SETUP (iGPU 포함 A10-7850)에 대한 CIFAR10 데이터 세트를 사용하여 1 epoch를 계산하는 몇 가지 숫자입니다. 귀하의 마일리지는 거의 확실하게 다를 것입니다!

  • Tensorflow (pip 설치를 통해) : ~ 1700 초 / epoch
  • Tensorflow (SSE + AVX 포함) : ~ 1100 초 / 에폭
  • Tensorflow (opencl 및 iGPU 포함) : ~ 5800 초 / epoch

이 특별한 경우 성능이 더 나쁘다는 것을 알 수 있습니다. 나는 이것을 다음 요인에 기인합니다.

  • iGPU에는 1GB 만 있습니다. 이로 인해 CPU와 GPU간에 많은 양의 복사가 발생합니다. (Opencl 1.2에는 아직 포인터를 통해 데이터를 전달할 수있는 기능이 없습니다. 대신 데이터를 앞뒤로 복사해야합니다.)
  • iGPU에는 512 개의 스트림 프로세서 (및 32Gb / s 메모리 대역폭) 만 있으며이 경우 SSE4 + AVX 명령 세트를 사용하는 4 개의 CPU보다 느립니다.
  • tensorflow-opencl의 개발은 시작 단계에 있으며 SYCL 등의 많은 최적화가 아직 완료되지 않았습니다.

더 많은 VRAM과 더 많은 스트림 프로세서와 함께 AMD GPU를 사용하는 경우 훨씬 더 나은 성능 수치를 얻을 수 있습니다. 나는 사람들이 무엇이 가능한지 알기 위해 어떤 숫자를 달성하는지 읽고 싶습니다.

업데이트가 푸시되면이 답변을 계속 유지할 것입니다.

3.) An alternative way is currently being hinted at which is using AMD's RocM initiative, and miOpen (cuDNN equivalent) library. These are/will be open-source libraries that enable deep learning. The caveat is that RocM support currently only exists for Linux, and that miOpen has not been released to the wild yet, but Raja (AMD GPU head) has said in an AMA that using the above, it should be possible to do deep learning on AMD GPUs. In fact, support is planned for not only Tensorflow, but also Cafe2, Cafe, Torch7 and MxNet.


One can use AMD GPU via the PlaidML Keras backend.

Fastest: PlaidML is often 10x faster (or more) than popular platforms (like TensorFlow CPU) because it supports all GPUs, independent of make and model. PlaidML accelerates deep learning on AMD, Intel, NVIDIA, ARM, and embedded GPUs.

Easiest: PlaidML is simple to install and supports multiple frontends (Keras and ONNX currently)

Free: PlaidML is completely open source and doesn't rely on any vendor libraries with proprietary and restrictive licenses.

For most platforms, getting started with accelerated deep learning is as easy as running a few commands (assuming you have Python (v2 or v3) installed):

virtualenv plaidml
source plaidml/bin/activate
pip install plaidml-keras plaidbench

Choose which accelerator you'd like to use (many computers, especially laptops, have multiple):

plaidml-setup

Next, try benchmarking MobileNet inference performance:

plaidbench keras mobilenet

Or, try training MobileNet:

plaidbench --batch-size 16 keras --train mobilenet

To use it with keras set

os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"

For more information

https://github.com/plaidml/plaidml

https://github.com/rstudio/keras/issues/205#issuecomment-348336284


This is an old question, but since I spent the last few weeks trying to figure it out on my own:

  1. OpenCL support for Theano is hit and miss. They added a libgpuarray back-end which appears to still be buggy (i.e., the process runs on the GPU but the answer is wrong--like 8% accuracy on MNIST for a DL model that gets ~95+% accuracy on CPU or nVidia CUDA). Also because ~50-80% of the performance boost on the nVidia stack comes from the CUDNN libraries now, OpenCL will just be left in the dust. (SEE BELOW!) :)
  2. ROCM appears to be very cool, but the documentation (and even a clear declaration of what ROCM is/what it does) is hard to understand. They're doing their best, but they're 4+ years behind. It does NOT NOT NOT work on an RX550 (as of this writing). So don't waste your time (this is where 1 of the weeks went :) ). At first, it appears ROCM is a new addition to the driver set (replacing AMDGPU-Pro, or augmenting it), but it is in fact a kernel module and set of libraries that essentially replace AMDGPU-Pro. (Think of this as the equivalent of Nvidia-381 driver + CUDA some libraries kind of). https://rocm.github.io/dl.html (Honestly I still haven't tested the performance or tried to get it to work with more recent Mesa drivers yet. I will do that sometime.
  3. Add MiOpen to ROCM, and that is essentially CUDNN. They also have some pretty clear guides for migrating. But better yet.
  4. They created "HIP" which is an automagical translator from CUDA/CUDNN to MiOpen. It seems to work pretty well since they lined the API's up directly to be translatable. There are concepts that aren't perfect maps, but in general it looks good.

Now, finally, after 3-4 weeks of trying to figure out OpenCL, etc, I found this tutorial to help you get started quickly. It is a step-by-step for getting hipCaffe up and running. Unlike nVidia though, please ensure you have supported hardware!!!! https://rocm.github.io/hardware.html. Think you can get it working without their supported hardware? Good luck. You've been warned. Once you have ROCM up and running (AND RUN THE VERIFICATION TESTS), here is the hipCaffe tutorial--if you got ROCM up you'll be doing an MNIST validation test within 10 minutes--sweet! https://rocm.github.io/ROCmHipCaffeQuickstart.html


Theano does have support for OpenCL but it is still in its early stages. Theano itself is not interested in OpenCL and relies on community support.

Most of the operations are already implemented and it is mostly a matter of tuning and optimizing the given operations.

To use the OpenCL backend you have to build libgpuarray yourself.

From personal experience I can tell you that you will get CPU performance if you are lucky. The memory allocation seems to be very naively implemented (therefore computation will be slow) and will crash when it runs out of memory. But I encourage you to try and maybe even optimize the code or help reporting bugs.


If you have access to other AMD gpu's please see here: https://github.com/ROCmSoftwarePlatform/hiptensorflow/tree/hip/rocm_docs

This should get you going in the right direction for tensorflow on the ROCm platform, but Selly's post about https://rocm.github.io/hardware.html is the deal with this route. That page is not an exhaustive list, I found out on my own that the Xeon E5 v2 Ivy Bridge works fine with ROCm even though they list v3 or newer, graphics cards however are a bit more picky. gfx8 or newer with a few small exceptions, polaris and maybe others as time goes on.

UPDATE - It looks like hiptensorflow has an option for opencl support during configure. I would say investigate the link even if you don't have gfx8+ or polaris gpu if the opencl implementation works. It is a long winded process but an hour or three (depending on hardware) following a well written instruction isn't too much to lose to find out.


Tensorflow 1.3 has been supported on AMD ROCm stack:

A pre-built docker image has also been posted publicly:

참고URL : https://stackoverflow.com/questions/37892784/using-keras-tensorflow-with-amd-gpu

반응형