Home >Backend Development >Python Tutorial >How to use gpu acceleration in python
I have read many tutorials and mentioned using os.environ["CUDA_VISIBLE_DEVICES"] = "1" or export CUDA_VISIBLE_DEVICES=1
However, the speed did not improve when I added it to the code. It took me a long time to find it. The problem is, the above two lines of code only work when your computer has more than two GPUs!
Because my computer only has one GPU, I need to change the parameter "1" to "0", otherwise the device "1" cannot be found. The CPU will be used by default and the speed will not be improved.
If you are running the python program in the terminal, use the command: CUDA_VISIBLE_DEVICES=0 python filename.py
If you have multiple cards and want to specify multiple gpu training, you can set it like this :
def set_gpus(gpu_index): if type(gpu_index) == list: gpu_index = ','.join(str(_) for _ in gpu_index) if type(gpu_index) ==int: gpu_index = str(gpu_index) os.environ["CUDA_VISIBLE_DEVICES"] = gpu_index
This can be achieved by calling the set_gpu function.
It must be noted that CUDA_VISIBLE_DEVICES, every! one! indivual! Character! mother! It must be written completely correctly. If you accidentally write CUDA_VISIBLE_DIVICES or CUDA_VISIABLE_DEVICES, the code will not report an error, but the GPU call will not succeed,
The above is the detailed content of How to use gpu acceleration in python. For more information, please follow other related articles on the PHP Chinese website!