#!/bin/bash
export HSA_OVERRIDE_GFX_VERSION=10.1.0
export HSA_ENABLE_SDMA=0
export HIP_VISIBLE_DEVICES=0

cd /home/fabian/ComfyUI
source venv/bin/activate

python3 << 'PYEOF'
import torch
import time
import os

print(f'PyTorch: {torch.__version__}')
print(f'CUDA: {torch.cuda.is_available()}')
if torch.cuda.is_available():
    print(f'Device: {torch.cuda.get_device_name(0)}')
else:
    print('NO CUDA')
    os._exit(1)

print()

# Test 1: Small matmul
print('=== Test 1: Small matmul 256x256 fp32 ===')
try:
    a = torch.randn(256, 256, device='cuda', dtype=torch.float32)
    b = torch.randn(256, 256, device='cuda', dtype=torch.float32)
    torch.cuda.synchronize()
    t = time.time()
    for _ in range(10):
        c = torch.mm(a, b)
    torch.cuda.synchronize()
    print(f'  OK: {time.time()-t:.3f}s')
    del a, b, c
except Exception as e:
    print(f'  FAIL: {e}')

# Test 2: Medium matmul
print('=== Test 2: matmul 1024x1024 fp32 ===')
try:
    a = torch.randn(1024, 1024, device='cuda', dtype=torch.float32)
    b = torch.randn(1024, 1024, device='cuda', dtype=torch.float32)
    torch.cuda.synchronize()
    t = time.time()
    c = torch.mm(a, b)
    torch.cuda.synchronize()
    print(f'  OK: {time.time()-t:.3f}s')
    del a, b, c
except Exception as e:
    print(f'  FAIL: {e}')

# Test 3: Large matmul
print('=== Test 3: matmul 2048x2048 fp32 ===')
try:
    a = torch.randn(2048, 2048, device='cuda', dtype=torch.float32)
    b = torch.randn(2048, 2048, device='cuda', dtype=torch.float32)
    torch.cuda.synchronize()
    t = time.time()
    c = torch.mm(a, b)
    torch.cuda.synchronize()
    print(f'  OK: {time.time()-t:.3f}s')
    del a, b, c
except Exception as e:
    print(f'  FAIL: {e}')

# Test 4: fp16
print('=== Test 4: matmul 2048x2048 fp16 ===')
try:
    a = torch.randn(2048, 2048, device='cuda', dtype=torch.float16)
    b = torch.randn(2048, 2048, device='cuda', dtype=torch.float16)
    torch.cuda.synchronize()
    t = time.time()
    c = torch.mm(a, b)
    torch.cuda.synchronize()
    print(f'  OK: {time.time()-t:.3f}s')
    del a, b, c
except Exception as e:
    print(f'  FAIL: {e}')

# Test 5: Conv2d
print('=== Test 5: Conv2d 32ch fp32 ===')
try:
    conv = torch.nn.Conv2d(32, 32, 3, padding=1).cuda().float()
    x = torch.randn(1, 32, 32, 32, device='cuda', dtype=torch.float32)
    torch.cuda.synchronize()
    t = time.time()
    y = conv(x)
    torch.cuda.synchronize()
    print(f'  OK: {time.time()-t:.3f}s')
    del conv, x, y
except Exception as e:
    print(f'  FAIL: {e}')

# Test 6: BMM (attention)
print('=== Test 6: BMM 4x512x64 fp32 ===')
try:
    q = torch.randn(1, 4, 512, 64, device='cuda', dtype=torch.float32)
    k = torch.randn(1, 4, 512, 64, device='cuda', dtype=torch.float32)
    torch.cuda.synchronize()
    t = time.time()
    attn = torch.matmul(q, k.transpose(-2, -1))
    torch.cuda.synchronize()
    print(f'  OK: {time.time()-t:.3f}s')
    del q, k, attn
except Exception as e:
    print(f'  FAIL: {e}')

# Test 7: Linear (typical model layer)
print('=== Test 7: Linear 3072->3072 fp32 ===')
try:
    lin = torch.nn.Linear(3072, 3072).cuda().float()
    x = torch.randn(1, 256, 3072, device='cuda', dtype=torch.float32)
    torch.cuda.synchronize()
    t = time.time()
    y = lin(x)
    torch.cuda.synchronize()
    print(f'  OK: {time.time()-t:.3f}s')
    del lin, x, y
except Exception as e:
    print(f'  FAIL: {e}')

print()
print('=== ALL TESTS COMPLETE ===')
torch.cuda.synchronize()
os._exit(0)
PYEOF
echo "Script exit: $?"
