Cannot import name 'random_split'

I got this

>>> from torch.utils.data import Dataset, DataLoader, random_split
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'random_split'

error
It means my installation of torch exists some issue?

torchvision               0.2.1                    py35_0
python                    3.5.6                hc3d631a_0
cudatoolkit               9.0                  h13b8566_0
pytorch                   0.4.0

I used conda and tried to install the updated version:

conda install pytorch==1.1.0 torchvision==0.3.0 cudatoolkit=9.0 -c pytorch

The problem solved.

Good to hear a newer version solved this import error.
However, PyTorch 1.1.0 is also quite old by now so I would recommend to install the latest stable version including the latest torchvision. Is there a reason you are installing this old lib stack?

1 Like

Thank you @ptrblck. Sometimes I recieve an error indicating the cuda version is incompetitive with torchvision. So what do you suggest the installation versions? I encountered the “version” issues many times.

What does “sometimes” mean in this context? Is torchvision working in some scripts and raises an error regarding the CUDA version in other scripts?

You should be able to install PyTorch and torchvision using the binary install instructions from here and specifying one CUDA version, which would be compatible with your NVIDIA drivers.

 Is torchvision working in some scripts and raises an error regarding the CUDA version in other scripts?

Yes, I encountered this kind of compatibility issue and

nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.176

This means I should install cudatoolkit == 9.0 right?

And in order to use other updated version of cudatoolkit. I need to upgrade my Driver, right?

Thank you again @ptrblck

If you install the binaries, your local CUDA installation won’t be used and the shipped cudatoolkit will be used instead.
You would only have to make sure your driver is up to date or has at least the minimal version as seen in this table.

You can check the driver version via nvidia-smi.

Your local CUDA 9.0 version (or rather the nvcc compiler) would be used, if you are building PyTorch from source or a custom extension.

nvidia-smi
Mon Jul 27 12:49:25 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.67       Driver Version: 418.67       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla V100-DGXS...  On   | 00000000:07:00.0  On |                    0 |
| N/A   32C    P0    38W / 300W |    351MiB / 32478MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla V100-DGXS...  On   | 00000000:08:00.0 Off |                    0 |
| N/A   32C    P0    37W / 300W |      1MiB / 32478MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Tesla V100-DGXS...  On   | 00000000:0E:00.0 Off |                    0 |
| N/A   33C    P0    38W / 300W |      1MiB / 32478MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   3  Tesla V100-DGXS...  On   | 00000000:0F:00.0 Off |                    0 |
| N/A   32C    P0    39W / 300W |      1MiB / 32478MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0       382      G   /usr/bin/gnome-shell                         107MiB |
|    0      1830      G   /usr/lib/xorg/Xorg                            18MiB |
|    0      1857      G   /usr/bin/gnome-shell                          86MiB |
|    0     40890      G   /usr/lib/xorg/Xorg                           136MiB |
+-----------------------------------------------------------------------------+

The CUDA version showed here is 10.1

And I encontered this error:

The NVIDIA driver on your system is too old (found version 10010).
Please update your GPU driver by downloading and installing a new
version from the URL: http://www.nvidia.com/Download/index.aspx
Alternatively, go to: https://pytorch.org to install
a PyTorch version that has been compiled with your version
of the CUDA driver.

what should i do?

If you are trying to install the CUDA10.2 binaries, your would have to update your NVIDIA driver to >=440.33 as given in the table.
Your current driver would support CUDA10.1 and older.

I created 2 conda env

cudatoolkit               10.0.130 
pytorch                      1.2.0
torchvision                 0.5.0

will give me the

The NVIDIA driver on your system is too old (found version 10010).
Please update your GPU driver by downloading and installing a new
version from the URL: http://www.nvidia.com/Download/index.aspx
Alternatively, go to: https://pytorch.org to install
a PyTorch version that has been compiled with your version
of the CUDA driver.

error.

While

torchvision               0.2.1                    py35_0
python                    3.5.6                hc3d631a_0
cudatoolkit               9.0                  h13b8566_0
pytorch                   0.4.0

has no error.

So this means the cudatoolkit 9.0 works fine on my driver? which config causes the compile error?

Unsure, where this error is coming from.
Could you update the driver to 440.33 or newer, create a new conda env, and install the latest PyTorch version?

File "finetune_xy.py", line 166, in <module>
    torch.cuda.set_device(gpu_ids[0])
  File "/home/song/.local/lib/python3.7/site-packages/torch/cuda/__init__.py", line 245, in set_device
    torch._C._cuda_setDevice(device)
  File "/home/song/.local/lib/python3.7/site-packages/torch/cuda/__init__.py", line 149, in _lazy_init
    _check_driver()
  File "/home/song/.local/lib/python3.7/site-packages/torch/cuda/__init__.py", line 63, in _check_driver
    of the CUDA driver.""".format(str(torch._C._cuda_getDriverVersion())))

Currently the 9.0 cuda toolkit works fine. I will update the driver when I have time :wink: