Skip to content

Feature request: Ability to specify some GPUs and ignore all others #1888

Closed

Description

@rdipietro

Right now, to specify a particular GPU, I do something like this:

gpu = 0
available_devices = os.environ['CUDA_VISIBLE_DEVICES'].split(',')
os.environ['CUDA_VISIBLE_DEVICES'] = available_devices[gpu]

after which TensorFlow will only allocate resources on the first GPU device. It would be nice to include a native way to do this.

Side note: There isn't much documentation (any?) on device_count, so I'm not sure if it's meant to handle this. Either way, I have experimented with the device_count, but with no luck: if I use device_count = {'GPU': 1}, TensorFlow still allocates memory on all available GPUs.

Activity

  1. added
    stat:contribution welcome
    on Apr 13, 2016
  2. gibiansky commented on Aug 23, 2016

    @gibiansky
    Contributor

    I would like to implement this, as it's important for some things besides just convenience; for example, you cannot use CUDA_VISIBLE_DEVICES with CUDA IPC, so Tensorflow cannot be used with anything that needs access to the GPU memory in other processes (without copying it to CPU first).

    Is there a particular API that would make sense for this?

    Some options:

    1. Another environment variable, besides CUDA_VISIBLE_DEVICES, which tells Tensorflow which GPUs to use. For example, TENSORFLOW_CUDA_DEVICES, which must be a subset of CUDA_VISIBLE_DEVICES (if CUDA_VISIBLE_DEVICES is specified).
    2. Modify the GPUOptions proto with a repeated field along the lines of gpu_id, which select which of the CUDA_VISIBLE_DEVICES to use. This seems cleaner, but I'm not sure how invasive that change would be (when does Tensorflow allocate GPU memory, call cuGetDevice, and otherwise initialize the StreamExecutor? Is it upon tf.Session()? Or is it earlier? If it's earlier, this change may be tricky to implement.)

    Finally, if I were to implement option (2) or (1), what would I need to do in order to get the contribution accepted to Tensorflow?

  3. gibiansky commented on Aug 24, 2016

    @gibiansky
    Contributor

    @zffchen78 If I were to implement this, what strategy would be most likely to get accepted as a patch to tensorflow?

    The least invasive strategy is one which uses an environment variable, because GPUs are allocated statically; In gpu_init.cc InitModule() is called, which eventually calls ExecutorForDevice, which allocates the GPUs. The GPUs aren't stored in the Session, and are not freed after a session ends; the CUDAPlatform is also stored statically and initialized on load. Effectively the architecture was not designed to allow the choice of GPUs to happen dynamically at runtime, but since environment variables are also global and exist at startup, this would be straightforward to implement.

    An alternative route would be to fix the main design issue: Have a Session track which GPUs its using, and, when it's done, deallocate the GPUs and destroy the StreamExecutor contexts. To do this we would probably have to move the Platform from a global static variable into a Session-local one, and have it be initialized on session startup rather than global startup.

    Please, let me know which of these is most likely to be accepted by the Tensorflow team if this is implemented and tested. If this is not a viable modification, please remove the "contributions welcome" tag.

  4. vrv commented on Aug 25, 2016

    @vrv

    @gibiansky, independently for another reason I essentially implemented option 2 in your first post earlier this week. I haven't submitted it yet, but maybe it might work for you. Concretely, what I did was:

    GPUOptions has a field called 'visible_gpu_devices', which is a comma-separated string. It maps from "visible" devices to "logical" devices. As an example, let's say the TensorFlow process has 8 GPUs visible to it. One could pass visible_gpu_devices="5,3", and it would map visible device 5 as "/gpu:0" and visible device 3 as "/gpu:1", and it would leave all other devices untouched / uninitialized.

    So it's basically like CUDA_VISIBLE_DEVICES, except CUDA_VISIBLE_DEVICES acts prior to the process starting, and controls what I would call 'physical GPUs' (e.g., what the driver reports) to "visible GPUs", which is the order that the process sees it, and this new option would allow one to remap from "visible" to "logical", with the default empty string meaning that the "visible" to "logical" map is one to one.

    Let me know if you think this satisfies this feature request and I can try to send it for review.

  5. vrv commented on Aug 25, 2016

    @vrv

    @rdipietro in addition, I believe now the device_count approach should maybe work for your case, since we now only allocate memory lazily on first memory allocation on GPU, so if you never ran an op on GPUs, it shouldn't allocate any memory on it either.

  6. self-assigned this
    on Aug 25, 2016
  7. removed
    stat:contribution welcome
    on Aug 25, 2016
  8. gibiansky commented on Aug 25, 2016

    @gibiansky
    Contributor

    @vrv Yes! This would be perfect! (In fact, I started implementing literally the same thing today, in the same manner.)

    Minor questions:

    • Shouldn't that be part of ConfigProto not GPUOptions? (GPUOptions seems like it is settings that affect a single GPU, while ConfigProto has settings related to many GPUs, for example device_count)
    • Why pass visible_gpu_devices as a string, rather than as a repeated int?
    • To confirm: this interacts fluidly with CUDA_VISIBLE_DEVICES. For example, if, on an 8-GPU system, CUDA_VISIBLE_DEVICES=5,2,0,6, and you set visible_gpu_devices=0,3, Tensorflow will end up running on GPUs 5 and 6.
    • Does this change also mean that, since the GPU info is stored in the Session, Session.close() can now actually deallocate the GPU and destroy the context? If that's the case that would be awesome. (If you look right now, nvidia-smi will show that even after Session.close(), the GPU contexts still exist and thus you cannot launch more processes that use them.)

    The most important thing is that when visible_gpu_devices is used, the contexts are not created on all visible devices – not just that memory is not allocated on them, but that they are completely uninitialized, so that other concurrently running Tensorflow processes can use them.

  9. vrv commented on Aug 25, 2016

    @vrv
    • Yeah, we can put it in something other than GPUOptions (I'm loathe to put it top level, I'd rather scope it appropriately).
    • Because that's how CUDA_VISIBLE_DEVICES works, and I kind of wanted it to be 'familiar' to people :). I could make it repeated too.
    • Yes, exactly.
    • Unfortunately I think some part of the GPU initialization is still global (held by process_state), so destroying the session would not release it. However, if you never used it, it shouldn't establish any context. It would be nice to have it release on session close, but it will require some work to properly refcount the currently global state. Heck, if you wanted to work on that instead, that would be nice.
  10. gibiansky commented on Aug 25, 2016

    @gibiansky
    Contributor

    Great, SGTM. No strong opinions here wrt GPUOptions vs other locations; if it's in GPUOptions it may make sense to change the comment in the ConfigProto GPUOptions field to reflect that GPUOptions are not only per-GPU settings.

    Thanks! Please ping me / close this issue when this is merged. It will be very helpful for the work I am doing right now.

  11. vrv commented on Aug 25, 2016

    @vrv

    (I just checked, the comment on GPUOptions in ConfigProto explicitly mentions 'options that applies to all GPUs' -- maybe we'll add a repeated PerGPUOptions field in the future...)

  12. gibiansky commented on Aug 25, 2016

    @gibiansky
    Contributor

    Perfect, I just misinterpreted it then when I read it, for some reason I assumed that meant it was "single-GPU options that are applied to every single GPU uniformly". You're right though that my interpretation doesn't make sense given the comment. Oops, sorry!

  13. vrv commented on Aug 25, 2016

    @vrv

    Well, some of those options could apply individually with different settings for each GPU, so your interpretation is also reasonable -- we just haven't implemented it that way (yet).

  14. gibiansky commented on Aug 25, 2016

    @gibiansky
    Contributor

    @vrv BTW: By implementing a very hacky version of this patch, I have confirmed that with this fix will allow you to use Tensorflow with CUDA IPC. (My hacky version just edited cuda_platform.cc to always select just one GPU based on an environment variable, and to return 1 for VisibleDeviceCount()). So definitely looking forward to this patch landing in Tensorflow master – do you know a rough ETA for this? Thank you!!

  15. vrv commented on Aug 25, 2016

    @vrv

    It'll probably in master tonight or tomorrow

    On Thu, Aug 25, 2016 at 3:24 PM, Andrew Gibiansky [email protected]
    wrote:

    @vrv https://github.com/vrv BTW: By implementing a very hacky version
    of this patch, I have confirmed that with this fix will allow you to use
    Tensorflow with CUDA IPC. (My hacky version just edited cuda_platform.cc to
    always select just one GPU based on an environment variable, and to return
    1 for VisibleDeviceCount()). So definitely looking forward to this patch
    landing in Tensorflow master – do you know a rough ETA for this? Thank you!!

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub
    #1888 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAcTeV18AB9kj1oOIZL15pAyWHGD-V2iks5qjhYUgaJpZM4IFrBD
    .

  16. 21 remaining items

  17. gibiansky commented on Sep 2, 2016

    @gibiansky
    Contributor

    Just tested this out today.

    It's closer than the previous one... but still not quite there -- reopen the issue again? The bug here is insidious and somewhat unexpected:

    :(

    It comes from the following line: gpu/process_state.cc:188

    If you take a look at that, you see that when you allocate memory on the host that is known to CUDA, you always use ExecutorForDevice(0), even though 0 may not be in your visible_device_list.

    You can test this with:

    import time
    import tensorflow as tf
    
    c = tf.ConfigProto()
    c.gpu_options.visible_device_list="1"
    
    s = tf.Session(config=c)
    s.run(tf.constant(10))
    time.sleep(5)

    And then if you look at nvidia-smi you will see two contexts, one on 0 and one on 1, even though only one was requested. (The difference between this test and the previous one is that you use a s.run)

    I'm not sure this is fixable without changing stream executor, but the patch isn't super helpful in its current state :(

  18. vrv commented on Sep 2, 2016

    @vrv

    Ha, fun times. I think it is fixable without too much work:

    https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/common_runtime/gpu/process_state.cc#L99

    gpu_allocators_ is a sparse array indexed by visible gpu id. So instead of using '0', we need to find any non-null entry in gpu_allocators_ and use the index of that executor. I believe we are guaranteed that the gpu_allocators_ are allocated prior to the first call to GetCudaHostAllocator.

    If you want to give that a try, let me know, otherwise I will try to find time to get to it.

  19. reopened this on Sep 2, 2016
  20. gibiansky commented on Sep 2, 2016

    @gibiansky
    Contributor

    SGTM, I'll give it a shot.

  21. gibiansky commented on Sep 2, 2016

    @gibiansky
    Contributor

    PR opened, I've confirmed that with that change nvidia-smi gives the desired output. I don't know whether there is any code style that I should be observing but am not, so just let me know what needs to be done to get this merged (whether by merging my almost-trivial PR or something else).

  22. added a commit that references this issue on Sep 6, 2016
    5a96e4e
  23. cbquillen commented on Jan 18, 2017

    @cbquillen

    I am seeing a problem with this implementation on some machines, where the device ordering used by nvidia-smi (and the NVML library) doesn't match the ordering in cuDeviceGet(). On those machines you end up having to specify a different number than the obvious one that you might see via nvidia-smi or via the python pynvml library. Please see my comment in #152.

  24. RexYing commented on Jul 22, 2017

    @RexYing

    I could use config.gpu_options.visible_device_list in python, and it works.
    But when I try to set the visible device list in c++ using the protobuf methods (eg. set_visible_device_list), it not only changes the visible_device_list field, but also the allocator_type field), and I got the error "Invalid allocator type: 0,1,2,3".

    It seems that the two fields got mapped into the same string address. Anyone knows what might be going on?

  25. bravelywangexocr commented on Nov 24, 2017

    @bravelywangexocr

    @RexYing, meet the same invalid allocator type issue, have you figured out the solution? thanks.

  26. kitterive commented on Jan 16, 2018

    @kitterive

    I met the same issue, any solutions? thanks.

  27. mattdingmeng commented on Jan 25, 2018

    @mattdingmeng

    I have the same issue. When I use C++ api 'set_visible_device_list("0")', the program crash and said 'E tensorflow/core/common_runtime/gpu/process_state.cc:130] Invalid allocator type: 0
    Segmentation fault (core dumped)'

    I think it's a bug. @kitterive @bravelywangexocr @RexYing

  28. ChengshuLi commented on Feb 28, 2018

    @ChengshuLi

    @kitterive @bravelywangexocr @RexYing @mattdingmeng

    Just want to create more context, I tried to debug a little bit and I found that when I called set_visible_device_list("1,2"), it also set "allocator_type" to "1,2", which is an invalid value for allocator_type. That's why the error message is "Invalid allocator type: 1,2".

    In other words, allocator_type and visible_device seem to share the same memory location.
    https://github.com/tensorflow/tensorflow/blob/3378865c5509dfb6d18e6f95f28757437f67d3da/tensorflow/core/protobuf/config.proto

    Please see the code snippet below as an example

    auto options = tensorflow::SessionOptions();
    const auto& static_gpu_options = options.config.gpu_options();
    auto mutable_gpu_options = options.config.mutable_gpu_options();
    
    std::cout << static_gpu_options.visible_device_list() << std::endl;     // print ""
    std::cout << static_gpu_options.allocator_type() << std::endl;          // print ""
    
    mutable_gpu_options->set_visible_device_list("1");
    
    std::cout << static_gpu_options.visible_device_list() << std::endl;     // print "1"
    std::cout << static_gpu_options.allocator_type() << std::endl;          // print "1"
    
    mutable_gpu_options->set_allocator_type("");
    
    std::cout << static_gpu_options.visible_device_list() << std::endl;     // print ""
    std::cout << static_gpu_options.allocator_type() << std::endl;          // print ""
    
  29. Kejie-Wang commented on Mar 15, 2018

    @Kejie-Wang

    @ChengshuLi
    The same issue when I use c++ API and my tensorflow version is 1.5.0. I also find that it works if I compile the source codes without --config=monolithic option and use libtensorflow_cc and libtensorflow_framework as a dynamic library linked into my project. Theis problem only arises when I compile with --config=monolithic option to get a single dynamic library libtensorflow_cc.

  30. added a commit that references this issue on Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions