Skip to content

Segmentation fault when GPUs are already used #152

Closed

Description

@nouiz

When I set the nvidia driver in exclusive mode and one of the GPU is already used by another process, I get a segmentation fault:

$ python -c "import tensorflow as tf;tf.InteractiveSession()"
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 12
Segmentation fault (core dumped)

If I limit the visible GPUs to only GPUs that have nothing running on them, it don't segfault:

$CUDA_VISIBLE_DEVICES=1 python -c "import tensorflow as tf;tf.InteractiveSession()"
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 12
I tensorflow/core/common_runtime/gpu/gpu_init.cc:88] Found device 0 with properties: 
name: GeForce GTX TITAN X
major: 5 minor: 2 memoryClockRate (GHz) 1.076
pciBusID 0000:09:00.0
Total memory: 12.00GiB
Free memory: 11.87GiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:112] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:122] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:643] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX TITAN X, pci bus id: 0000:09:00.0)
I tensorflow/core/common_runtime/gpu/gpu_region_allocator.cc:47] Setting region size to 12105628263
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 12

Here is my output of nvidia-smi:

$ nvidia-smi 
Wed Nov 11 16:48:27 2015       
+------------------------------------------------------+                       
| NVIDIA-SMI 352.39     Driver Version: 352.39         |                       
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 750     Off  | 0000:05:00.0      On |                  N/A |
| N/A   48C    P8     0W /  38W |     25MiB /  2047MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX TIT...  Off  | 0000:06:00.0     Off |                  N/A |
| 42%   82C    P2   127W / 250W |    262MiB / 12287MiB |     39%   E. Process |
+-------------------------------+----------------------+----------------------+
|   2  GeForce GTX TIT...  Off  | 0000:09:00.0     Off |                  N/A |
| 22%   46C    P8    17W / 250W |     23MiB / 12287MiB |      0%   E. Process |
+-------------------------------+----------------------+----------------------+
|   3  GeForce GTX TIT...  Off  | 0000:0A:00.0     Off |                  N/A |
| 22%   37C    P8    15W / 250W |    361MiB / 12287MiB |      0%   E. Process |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0      1171    G   /usr/bin/X                                      17MiB |
|    1     32740    C   python                                         209MiB |
|    3      9429    C   python                                         336MiB |
+-----------------------------------------------------------------------------+

I suppose that in the code that check the available GPUs, it don't handle correctly a case when one of the GPUs can't be used.

Activity

  1. zheng-xq commented on Nov 11, 2015

    @zheng-xq
    Contributor

    @nouiz, you are correct in your analysis. TensorFlow tries to grab all the GPU it sees from the system that passes its criteria. CUDA_VISIBLE_DEVICES is the solution I would suggest. Please let us know if that is not enough.

  2. skearnes commented on Nov 12, 2015

    @skearnes

    @zheng-xq +1 on CUDA_VISIBLE_DEVICES; this fixes the issue for me.

  3. vrv commented on Nov 13, 2015

    @vrv

    Re-open if CUDA_VISIBLE_DEVICES isn't good enough

  4. noisychannel commented on Dec 11, 2015

    @noisychannel

    @vrv This is still a problem when there are multiple GPUs (in exclusive mode) on the machine and some of them have processes running on them. Setting CUDA_VISIBLE_DEVICES is not ideal if you do not actually know which GPU is available. nvidia-smi will only show the current state of the GPUs and there's no way of ensuring that while you spawn your process, no other process occupied the GPU you set for use. Is there a way of polling GPUs and assigning the process to the first available, compatible GPU on the machine instead of having to manually specify the GPU id?

    PS: I cannot reopen this issue.

  5. reopened this on Dec 11, 2015
  6. vrv commented on Dec 11, 2015

    @vrv

    @zheng-xq: is it possible to query the status of the exclusive mode bit?

  7. zheng-xq commented on Dec 11, 2015

    @zheng-xq
    Contributor

    @noisychannel, even if we can query which GPU is available, and by the time we actually try to assign the GPU, it could be taken by other processes. This is not very different from having a script query GPU status through nvidia-smi, and set the GPU id accordingly.

    The only way I can think of that makes your case easier is actually to create the context, and if it fails, ignore that failure and move on to the next. However, this is undesirable for many more common cases, where such context creation errors are signs of bigger problems.

  8. noisychannel commented on Dec 11, 2015

    @noisychannel

    The established way to do this seems to be:

    1. Set the GPUs to operate in exclusive mode.
    2. Do not call cudaSetDevice().
    3. When you run your program, it will try and create the context on the first GPU and fail if the GPU is busy. Assuming you're using CUDART, it should silently try and create the context on the next GPUs and fail if all GPUs are busy.

    As for exception handling while creating the context, it seems like the this scenario can be handled with the exceptions returned by cuCtxCreate().

    A solution like this is optimal when you delegate the scheduling of your jobs to something like SGE and cannot trust the script based querying approach which I think is common for large clusters.

  9. zheng-xq commented on Dec 11, 2015

    @zheng-xq
    Contributor

    This would work fine if the framework only wants to use a single GPU. TensorFlow is designed to use multiple GPU seamlessly at the same time. So at the initialization stage, it grabs all the visible devices that are compatible, since the client program might use all of them. The actual device used is only known at the graph construction/execution time, which is at a much later stage.

    For most of our existing users, the GPUs that are available to a particular job is known when it starts. So it is okay for TensorFlow to take all of them. A few options to think about:

    1. Is it possible for the job scheduler to reserve the GPUs in your clusters? That would work best with TensorFlow.
    2. If you only know the list of candidate GPUs, and the number of GPUs to use, we can add a special mode where we try to create context in the candidate list, and stops if desired number of GPUs are taken. This requires more plumbing to both TensorFlow and the underlying Stream-Executor that actually manages GPUs. We would much prefer Add support for Python 3.x #1 if that is possible for you.
  10. noisychannel commented on Dec 11, 2015

    @noisychannel

    @zheng-xq For 1: AFAIK, the only responsibility of most job schedulers is to ensure that you will have access to the number of GPUs you have asked for on the machine which has been assigned to you. It leaves the actual CPU/GPU scheduling of the processes on that machine to the OS/Cuda driver. Also, hypothetically, if the scheduler was to attempt something of this sort, it would not be able to block the GPU without creating something like a context.

    For 2 : I understand that this is probably painful. However, it does add a more general set of features. The case where I want k out of n GPUs on the machine is most prominent. Eg,, it would be able to handle the case where the candidate list is the set of all GPUs on the machine and I want only 2 available and compatible GPUs from that list. Having the ability to specify how many GPUs you want to use will be a great addition and this will tie in perfectly with most job schedulers. So to tensorflow, I specify, I want to run this thing on 2 GPUs and I tell the job scheduler that I need 2 GPUs.

  11. skearnes commented on Dec 11, 2015

    @skearnes

    Drive-by comment: the sysadmin for my local SGE cluster was able to have an environment variable set that tells me which GPUs my jobs have been assigned. I then use this variable to set CUDA_VISIBLE_DEVICES. I would guess that most common schedulers have similar capabilities, although they may require root-level tweaking.

  12. noisychannel commented on Dec 11, 2015

    @noisychannel

    This is useful to know. However, something like this would require all jobs on the cluster to respect that environment variable when using GPUs. And it takes away some of the scheduling responsibility that should ideally belong to the CUDA driver, adding another layer of complexity. In fact, most other toolkits that are GPU capable will not require something like this and as long as the GPUs are set in exclusive mode, they work just fine.

  13. jtrmal commented on Dec 11, 2015

    @jtrmal

    Another drive-by comment: when the allocation fails, the app actually segfaults on our machines. That does not provide any possibility for the user to recover from it at the python level.
    A good-enough solution would be
    a) throw an exception so that user can it catch and try (for example) another device
    b) ideally, adding /gpu:auto which would just iterate through the devices and allocate the one that is available.

  14. zheng-xq commented on Dec 14, 2015

    @zheng-xq
    Contributor

    @noisychannel, please see if you can work around this issue at the moment. Meanwhile, I will investigate if we can officially support the soft-try-and-device-limit approach in TensorFlow.

  15. 24 remaining items

  16. noisychannel commented on Jan 18, 2017

    @noisychannel

    This may be helpful to some users who are on a cluster. Often, you don't have information about which GPU has been allocated to you or which one to occupy if free, with CUDA_VISIBLE_DEVICES.

    I use this script to (https://gist.github.com/noisychannel/cdf57e2f177e98ae653230323a093d1e) to return the first free GPU and then use something like :

    CUDA_VISIBLE_DEVICES=`scripts/free-gpu` python 
    

    Edit : This is the parsed nvidia-smi output that @yaroslavvb mentioned earlier in this thread.

  17. cbquillen commented on Jan 19, 2017

    @cbquillen

    @yaroslavvb
    Looking at the NVIDIA docs more, I think you can get your setup_one_gpu script to work reliably if you set the CUDA_DEVICE_ORDER environment variable to 'PCI_BUS_ID'.

    @zheng-xq A cheezy solution that would probably reduce the likelihood of complaints (but create some less-likely new problems) would be to force set CUDA_DEVICE_ORDER='PCI_BUS_ID' within Tensorflow.

  18. added a commit that references this issue on Jun 20, 2017
  19. tensorflowbutler commented on Dec 22, 2017

    @tensorflowbutler
    Member

    It has been 14 days with no activity and this issue has an assignee.Please update the label and/or status accordingly.

  20. added
    stat:contribution welcome
    on Dec 22, 2017
  21. removed their assignment
    on Dec 22, 2017
  22. zheng-xq commented on Dec 22, 2017

    @zheng-xq
    Contributor

    If someone is interested in adding CUDA_DEVICE_ORDER='PCI_BUS_ID' support in TensorFlow, a contribution is welcome. But note that we can only override it if it is not already set. And it has to been before any potential Cuda calls are made.

  23. cliffwoolley commented on Dec 22, 2017

    @cliffwoolley
    Contributor

    I don't think I've actually seen an application that sets CUDA_DEVICE_ORDER on its own. Usually this would be set in the environment before launching the application, perhaps in combination with CUDA_VISIBLE_DEVICES.

  24. cbquillen commented on Dec 22, 2017

    @cbquillen

    You can certainly set environment variables within an application before the first call to tf.Session().

  25. cliffwoolley commented on Dec 22, 2017

    @cliffwoolley
    Contributor

    Of course it's possible. Just as in C you'd use setenv(). I was just saying that for those particular environment variables, the choice is a bit specific to the combination of application and machine the application is run on. So usually the user (or script) invoking the application would set those env vars rather than having the application set them unilaterally. I'd argue for example that it's unwise for TF itself to set CUDA_DEVICE_ORDER, because it would undo the CUDA feature that this variable sits on top of, which is to, by default, have the "best" device be device 0; this is handy when you don't know much about the system you're running on and are just trying things out. More sophisticated users just pick a GPU or, if really fancy, pick one based on NVML output as we're discussing here. But we shouldn't, IMO, assume all users are so sophisticated.

  26. imagefun commented on Apr 12, 2018

    @imagefun

    export CUDA_VISIBLE_DEVICES=0 solves my issue!

  27. wt-huang commented on Nov 10, 2018

    @wt-huang

    Closing as this is resolved, free to reopen if problem persists.

  28. saikrishnadas commented on Apr 4, 2020

    @saikrishnadas

    Python 3.6.9
    Tensorflow 2.1.0 gpu supported
    Cuda 10.0
    TensorRT 6.0
    GPU - 1
    Is still get the error even after export CUDA_VISIBLE_DEVICES=0 or export CUDA_VISIBLE_DEVICES=1

    Error log :

    Can't identify the cuda device. Running on device 0
    Segmentation fault (core dumped)
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:contribution welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions