How to use gpu in .py files Google Colab?

Viewed 1376

I have a notebook in GC with configured gpu computing. When I run in this notebook:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

I can see GPU in devices:

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 2881683908768157563
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 14433918882325490908
physical_device_desc: "device: XLA_CPU device"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 8857856280193037152
physical_device_desc: "device: XLA_GPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 15701463552
locality {
  bus_id: 1
  links {
  }
}
incarnation: 13142570581108506915
physical_device_desc: "device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0"
]

but when I put this code(nothing else) in .py file (in drive), which I run from shell using

!python3 file.py

I can't see GPU in devices:

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 5088676628756383267
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 550355900248427842
physical_device_desc: "device: XLA_CPU device"
]

How to solve it? I.e. how to use GPU computing in .py files which run from notebook terminal?

2 Answers

I am actually not sure why this is happening but still, I think the file you are running does not demand GPU use and hence no GPU are used. Please correct me if I am wrong.

You can use @Anwarvic's suggestion:

%run file.py

It worked perfectly for me.

Related