"""Optional GPU example; requires a compatible PyTorch + Triton environment."""
import torch
import triton
import triton.language as tl

@triton.jit
def add_kernel(X,Y,Z,N:tl.constexpr,BLOCK:tl.constexpr):
    offsets=tl.program_id(0)*BLOCK+tl.arange(0,BLOCK)
    mask=offsets<N
    x=tl.load(X+offsets,mask=mask,other=0)
    y=tl.load(Y+offsets,mask=mask,other=0)
    tl.store(Z+offsets,x+y,mask=mask)

def add(x,y):
    if x.shape!=y.shape or not x.is_contiguous() or not y.is_contiguous():
        raise ValueError('equal shape, contiguous tensors required')
    z=torch.empty_like(x)
    n=x.numel()
    if n: add_kernel[(triton.cdiv(n,256),)](x,y,z,n,BLOCK=256)
    return z

if __name__=='__main__':
    if not torch.cuda.is_available(): raise SystemExit('A compatible GPU is required; no GPU result produced.')
    torch.manual_seed(42)
    for n in [0,1,255,256,257,100003]:
        x=torch.randn(n,device='cuda'); y=torch.randn_like(x)
        torch.testing.assert_close(add(x,y),x+y)
    x=torch.randn(1_000_003,device='cuda'); y=torch.randn_like(x)
    for _ in range(20): add(x,y)
    torch.cuda.synchronize()
    start,end=torch.cuda.Event(enable_timing=True),torch.cuda.Event(enable_timing=True)
    start.record()
    for _ in range(100): result=add(x,y)
    end.record();end.synchronize()
    print('Correctness PASS. ms/call including repeated output allocation path:',start.elapsed_time(end)/100)
    print('Vector-add experiment only; not an LLM benchmark.')
