@triton.jit
def scaled_cast(inpt_ptr: torch.Tensor, output_ptr: torch.Tensor, scale_ptr:
    torch.Tensor, abs_max_ptr: torch.Tensor, numel: int, XBLOCK: tl.
    constexpr, float8_dtype: tl.constexpr, max_val: tl.constexpr):
    """Quantize tensor to fp8 using a delayed scaled and calculate abs_max"""
    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)
    scale = tl.load(scale_ptr)
    scaled_inpt = inpt * scale
    if max_val != 0.0:
        tl.where(scaled_inpt > max_val, max_val, scaled_inpt)
        tl.where(scaled_inpt < -1 * max_val, -1 * max_val, scaled_inpt)
    tl.store(output_ptr + index, scaled_inpt.to(float8_dtype), mask=mask)
