Repository navigation
Expand file tree
/
Copy pathtest_torch.py
More file actions
50 lines (41 loc) · 1.63 KB
/
Copy pathtest_torch.py
File metadata and controls
50 lines (41 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import torch
from torch.nn import functional as F
print(f"PyTorch {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"GPU: {torch.cuda.get_device_name(0)}")
print(f"CUDA version: {torch.version.cuda}")
# 基础 tensor 运算
a = torch.randn(3, 3, device="cuda", dtype=torch.float16)
b = torch.randn(3, 3, device="cuda", dtype=torch.float16)
c = a @ b
print(f"\nMatmul on GPU: {c.shape} {c.dtype} -> OK")
# layer_norm
x = torch.randn(1, 64, device="cuda", dtype=torch.float16)
w = torch.ones(64, device="cuda", dtype=torch.float16)
b = torch.zeros(64, device="cuda", dtype=torch.float16)
y = F.layer_norm(x, (64,), weight=w, bias=b)
print(f"LayerNorm: {y.shape} -> OK")
# softmax
logits = torch.randn(1, 65536, device="cuda", dtype=torch.float16)
probs = F.softmax(logits.float(), dim=-1)
print(f"Softmax: {probs.shape}, sum={probs.sum().item():.4f} -> OK")
# group_norm
x = torch.randn(1, 128, device="cuda", dtype=torch.float16)
y = F.group_norm(x.view(1, 128), num_groups=2)
print(f"GroupNorm: {y.shape} -> OK")
# relu squared
x = torch.randn(64, device="cuda", dtype=torch.float16)
y = torch.relu(x) ** 2
print(f"ReLU^2: {y.shape} -> OK")
# sigmoid
x = torch.randn(64, device="cuda", dtype=torch.float16)
y = torch.sigmoid(x)
print(f"Sigmoid: {y.shape} -> OK")
# topk
logits = torch.randn(65536, device="cuda")
_, indices = torch.topk(logits, 5)
print(f"TopK: {indices.tolist()} -> OK")
print("\nAll tests passed!")
else:
print("No CUDA GPU available.")