# Generated from the displayed GPU Lab example; no GPU timings are measured.
# Run: python example.py --device cpu  (default; no CPU/GPU transfers)
# Or:  python example.py --device cuda (compatible NVIDIA GPU and PyTorch required)
# Explicit CPU-only calculations remain on CPU in either mode.
import argparse
import torch

_lab_parser = argparse.ArgumentParser(description="Run this GPU Lab PyTorch example.")
_lab_parser.add_argument("--device", choices=["cpu", "cuda"], default="cpu")
_lab_device = _lab_parser.parse_args().device
if _lab_device == "cuda" and not torch.cuda.is_available():
    _lab_parser.error("CUDA mode needs an available NVIDIA GPU and compatible PyTorch installation.")
if _lab_device == "cpu":
    print("CPU mode checks values and layouts; it does not perform CPU-GPU transfers.")

# The displayed example, with its CUDA device requests selected by --device.
import torch

n, d = 8, 4
q = torch.tensor([[(i + j) % 5 - 2 for j in range(d)]
                  for i in range(n)], dtype=torch.float32, device=_lab_device) * 1
k = torch.tensor([[((2 * i + j) % 7 - 3) / 2 for j in range(d)]
                  for i in range(n)], dtype=torch.float32, device=_lab_device)
v = torch.tensor([[(i + j) % 9 - 4 for j in range(d)]
                  for i in range(n)], dtype=torch.float32, device=_lab_device)
scores = q @ k.T / d**0.5
weights = torch.softmax(scores, dim=-1)
out = weights @ v
print(out[0])
