@triton.jit
def dynamic_scaled_cast(inpt_ptr: torch.Tensor, output_ptr: torch.Tensor,
    abs_max_ptr: torch.Tensor, spin_lock: torch.Tensor, numel: int, XBLOCK:
    tl.constexpr, float8_dtype: tl.constexpr, max_val: tl.constexpr):
    """Quantize tensor to fp8 using current global absmax"""
    n_blocks = tl.num_programs(0)
    offset = tl.program_id(0) * XBLOCK
    index = offset + tl.arange(0, XBLOCK)[:]
    index = tl.max_contiguous(tl.multiple_of(index, XBLOCK), XBLOCK)
    mask = index < numel
    inpt = tl.load(inpt_ptr + index, mask=mask)
    block_max = tl.max(tl.abs(inpt))
    tl.atomic_max(abs_max_ptr, block_max)
    tl.atomic_add(spin_lock, 1, sem='release')
    while tl.load(spin_lock, volatile=True) < n_blocks:
        pass
    scale = max_val / tl.clamp(tl.load(abs_max_ptr), -1000000000000.0,
        float('inf'))
    scaled_inpt = inpt * scale
    scaled_inpt = tl.clamp(scaled_inpt, -1 * max_val, max_val)
    tl.store(output_ptr + index, scaled_inpt.to(float8_dtype), mask=mask)
