Create GPU_test.py
Browse files- GPU_test.py +36 -0
GPU_test.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import paddle
|
| 2 |
+
|
| 3 |
+
# Check if GPU is available
|
| 4 |
+
if paddle.is_compiled_with_cuda():
|
| 5 |
+
# Define the device configuration
|
| 6 |
+
paddle.set_device('gpu')
|
| 7 |
+
print("CUDA is available. Using GPU for computations.")
|
| 8 |
+
else:
|
| 9 |
+
paddle.set_device('cpu')
|
| 10 |
+
print("CUDA is not available. Using CPU for computations.")
|
| 11 |
+
|
| 12 |
+
# Test code to verify GPU usage
|
| 13 |
+
# Define a simple neural network example
|
| 14 |
+
class SimpleNet(paddle.nn.Layer):
|
| 15 |
+
def __init__(self):
|
| 16 |
+
super(SimpleNet, self).__init__()
|
| 17 |
+
self.linear = paddle.nn.Linear(10, 1)
|
| 18 |
+
|
| 19 |
+
def forward(self, x):
|
| 20 |
+
return self.linear(x)
|
| 21 |
+
|
| 22 |
+
# Create a random input tensor
|
| 23 |
+
input_data = paddle.randn([1, 10])
|
| 24 |
+
|
| 25 |
+
# Instantiate the network
|
| 26 |
+
model = SimpleNet()
|
| 27 |
+
|
| 28 |
+
# Move model to GPU explicitly if not set by default
|
| 29 |
+
if paddle.is_compiled_with_cuda():
|
| 30 |
+
model = model.cuda()
|
| 31 |
+
|
| 32 |
+
# Perform a forward pass
|
| 33 |
+
output = model(input_data)
|
| 34 |
+
|
| 35 |
+
# Print the output to verify GPU usage
|
| 36 |
+
print("Output:", output)
|