sayakpaul HF Staff commited on
Commit
89c4c11
·
verified ·
1 Parent(s): a819216

Upload gpu_test.py

Browse files
Files changed (1) hide show
  1. strix-halo/gpu_test.py +87 -0
strix-halo/gpu_test.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Small PyTorch model + training step to verify AMD GPU (ROCm) execution.
3
+
4
+ Target hardware: AMD Radeon 8060S (Strix Halo APU, gfx1151), ROCm build of PyTorch.
5
+ On ROCm builds, the AMD GPU is exposed through the CUDA API, so device name is 'cuda'.
6
+ """
7
+ import os
8
+ import torch
9
+ import torch.nn as nn
10
+
11
+
12
+ def pick_device() -> torch.device:
13
+ if torch.cuda.is_available():
14
+ return torch.device("cuda")
15
+ raise SystemExit(
16
+ "No GPU visible to PyTorch. torch.cuda.is_available() is False.\n"
17
+ "For this Strix Halo (gfx1151) APU, try exporting "
18
+ "HSA_OVERRIDE_GFX_VERSION=11.0.0 before running."
19
+ )
20
+
21
+
22
+ class TinyNet(nn.Module):
23
+ """A minimal MLP: 64 -> 128 -> 10."""
24
+
25
+ def __init__(self, in_features=64, hidden=128, out_features=10):
26
+ super().__init__()
27
+ self.net = nn.Sequential(
28
+ nn.Linear(in_features, hidden),
29
+ nn.ReLU(),
30
+ nn.Linear(hidden, out_features),
31
+ )
32
+
33
+ def forward(self, x):
34
+ return self.net(x)
35
+
36
+
37
+ def main():
38
+ print("=" * 60)
39
+ print("PyTorch build :", torch.__version__)
40
+ print("Built with ROCm/HIP:", torch.version.hip)
41
+ print("CUDA-API available :", torch.cuda.is_available())
42
+ print("Device count :", torch.cuda.device_count())
43
+
44
+ device = pick_device()
45
+ if device.type == "cuda":
46
+ print("GPU name :", torch.cuda.get_device_name(0))
47
+ print("HSA_OVERRIDE_GFX_VERSION =", os.environ.get("HSA_OVERRIDE_GFX_VERSION", "(unset)"))
48
+ print("=" * 60)
49
+
50
+ torch.manual_seed(0)
51
+ model = TinyNet().to(device)
52
+ optimizer = torch.optim.Adam(model.parameters(), lr=1e-2)
53
+ loss_fn = nn.CrossEntropyLoss()
54
+
55
+ # Synthetic classification data, created directly on the GPU.
56
+ x = torch.randn(256, 64, device=device)
57
+ y = torch.randint(0, 10, (256,), device=device)
58
+
59
+ print("\nTraining 20 steps on", device, "...")
60
+ for step in range(20):
61
+ optimizer.zero_grad()
62
+ logits = model(x)
63
+ loss = loss_fn(logits, y)
64
+ loss.backward()
65
+ optimizer.step()
66
+ if step % 5 == 0 or step == 19:
67
+ print(f" step {step:2d} loss = {loss.item():.4f}")
68
+
69
+ # Confirm tensors and compute really lived on the GPU.
70
+ assert logits.is_cuda, "logits are not on the GPU!"
71
+ torch.cuda.synchronize()
72
+
73
+ # A quick matmul to exercise the GPU compute path directly.
74
+ a = torch.randn(1024, 1024, device=device)
75
+ b = torch.randn(1024, 1024, device=device)
76
+ c = a @ b
77
+ torch.cuda.synchronize()
78
+
79
+ print("\nResult tensor device :", logits.device)
80
+ print("Matmul output device :", c.device, "| shape:", tuple(c.shape))
81
+ mem = torch.cuda.memory_allocated() / (1024 ** 2)
82
+ print(f"GPU memory allocated : {mem:.1f} MiB")
83
+ print("\n✅ SUCCESS: model trained and ran on the AMD GPU via ROCm.")
84
+
85
+
86
+ if __name__ == "__main__":
87
+ main()