File size: 1,242 Bytes
5b90392
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Reference implementation — the CORRECTNESS SPEC for `mixed_precision_gemm_split`.

This is correct but slow. It defines exactly what your kernel must reproduce; its speed has no
bearing on your score, which is an absolute TFLOP/s number. GENERATED by _factory/build.py.
"""
import torch


def mixed_precision_gemm_split(a_q, a_scale, b_q, b_scale, a_hi, b_hi):
    """Exact int32 branch + affine rescale, plus a plain bf16 branch. The numerical SPECIFICATION.

    The int8 contraction is done in float64 purely as a convenient EXACT integer accumulator: every partial
    sum is bounded by 127*127*K < 2^53, so fp64 reproduces the int32 dot product bit for bit (this is
    checked against torch._int_mm). fp32 would NOT be exact here -- the sums exceed 2^24.

    The outlier branch is a plain fp32-accumulated contraction of the two bf16 operands. It carries NO
    scales: a_hi and b_hi are already real values.
    """
    acc = a_q.double() @ b_q.double().t()                                     # (M, N), exact integers
    lo = acc * a_scale.double().unsqueeze(1) * b_scale.double().unsqueeze(0)
    hi = (a_hi.float() @ b_hi.float().t()).double()                           # the bf16 outlier branch
    return (lo + hi).float()