You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: Ability to specify some GPUs and ignore all others #1888
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.
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:
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).
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?
@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.ccInitModule() 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.
@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.
@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.
@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.
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.
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.
(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...)
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!
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).
@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!!
On Thu, Aug 25, 2016 at 3:24 PM, Andrew Gibiansky [email protected]
wrote:
@vrvhttps://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!!
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.
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 :(
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.
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).
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.
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?
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)'
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".
@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.
Activity
gibiansky commentedon Aug 23, 2016
I would like to implement this, as it's important for some things besides just convenience; for example, you cannot use
CUDA_VISIBLE_DEVICESwith 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:
CUDA_VISIBLE_DEVICES, which tells Tensorflow which GPUs to use. For example,TENSORFLOW_CUDA_DEVICES, which must be a subset ofCUDA_VISIBLE_DEVICES(ifCUDA_VISIBLE_DEVICESis specified).GPUOptionsproto with arepeatedfield along the lines ofgpu_id, which select which of theCUDA_VISIBLE_DEVICESto use. This seems cleaner, but I'm not sure how invasive that change would be (when does Tensorflow allocate GPU memory, callcuGetDevice, and otherwise initialize the StreamExecutor? Is it upontf.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?
gibiansky commentedon Aug 24, 2016
@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.ccInitModule()is called, which eventually callsExecutorForDevice, which allocates the GPUs. The GPUs aren't stored in theSession, and are not freed after a session ends; theCUDAPlatformis 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
Sessiontrack which GPUs its using, and, when it's done, deallocate the GPUs and destroy theStreamExecutorcontexts. To do this we would probably have to move thePlatformfrom a global static variable into aSession-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.
vrv commentedon Aug 25, 2016
@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.
vrv commentedon Aug 25, 2016
@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.
gibiansky commentedon Aug 25, 2016
@vrv Yes! This would be perfect! (In fact, I started implementing literally the same thing today, in the same manner.)
Minor questions:
visible_gpu_devicesas a string, rather than as arepeated int?CUDA_VISIBLE_DEVICES. For example, if, on an 8-GPU system,CUDA_VISIBLE_DEVICES=5,2,0,6, and you setvisible_gpu_devices=0,3, Tensorflow will end up running on GPUs 5 and 6.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-smiwill show that even afterSession.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_devicesis 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.vrv commentedon Aug 25, 2016
gibiansky commentedon Aug 25, 2016
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.
vrv commentedon Aug 25, 2016
(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...)
gibiansky commentedon Aug 25, 2016
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!
vrv commentedon Aug 25, 2016
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).
gibiansky commentedon Aug 25, 2016
@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!!
vrv commentedon Aug 25, 2016
It'll probably in master tonight or tomorrow
On Thu, Aug 25, 2016 at 3:24 PM, Andrew Gibiansky [email protected]
wrote:
21 remaining items
gibiansky commentedon Sep 2, 2016
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 though0may not be in yourvisible_device_list.You can test this with:
And then if you look at
nvidia-smiyou 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 as.run)I'm not sure this is fixable without changing stream executor, but the patch isn't super helpful in its current state :(
vrv commentedon Sep 2, 2016
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.
gibiansky commentedon Sep 2, 2016
SGTM, I'll give it a shot.
gibiansky commentedon Sep 2, 2016
PR opened, I've confirmed that with that change
nvidia-smigives 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).Make CUDA host allocator fetch one of the available stream executors
cbquillen commentedon Jan 18, 2017
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.
RexYing commentedon Jul 22, 2017
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?
bravelywangexocr commentedon Nov 24, 2017
@RexYing, meet the same invalid allocator type issue, have you figured out the solution? thanks.
kitterive commentedon Jan 16, 2018
I met the same issue, any solutions? thanks.
mattdingmeng commentedon Jan 25, 2018
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
ChengshuLi commentedon Feb 28, 2018
@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
Kejie-Wang commentedon Mar 15, 2018
@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=monolithicoption and uselibtensorflow_ccandlibtensorflow_frameworkas a dynamic library linked into my project. Theis problem only arises when I compile with--config=monolithicoption to get a single dynamic librarylibtensorflow_cc.Merge pull request tensorflow#1888 from ROCmSoftwarePlatform/rocm_mio…