Approximate math with bitcast
approximate_math_with_bitcast
¶
LBs = {builtin.f16: (2 ** 10, 14), builtin.f32: (2 ** 23, 127), builtin.f64: (2 ** 52, 1023)}
module-attribute
¶
MakeBase2
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/approximate_math_with_bitcast.py
log: bool
instance-attribute
¶
exp: bool
instance-attribute
¶
__init__(log: bool, exp: bool) -> None
¶
match_and_rewrite(op: Operation, rewriter: PatternRewriter)
¶
Source code in xdsl/transforms/approximate_math_with_bitcast.py
MakeApprox
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/approximate_math_with_bitcast.py
log: bool
instance-attribute
¶
exp: bool
instance-attribute
¶
__init__(log: bool, exp: bool) -> None
¶
match_and_rewrite(op: Operation, rewriter: PatternRewriter)
¶
Source code in xdsl/transforms/approximate_math_with_bitcast.py
ApproximateMathWithBitcastPass
dataclass
¶
Bases: ModulePass
This pass applies approximations for some math operations (currently log and exp) and converts them to bitcasting-based approximations.
These are intended for environments that don't need high accuracy, and do not have specialized hardware support for expf and log in hardware.
It makes use of the fact that IEEE floating-point numbers are encoded as three base-2 numbers:
s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
With the final floating-point number being $\(x = (-1)^s * 2^{e-B} * (1 + m/L)\)$. Hence, $\(e\)$ encodes the $\(\lfloor log2(x)\rfloor\)$ of x, and can be accessed by bit-casting and left-shifting.
The following approximations are enabled through this:
1) $\(\log_2(x) \approx bc-int(x)/L - B + \varepsilon\)$ 2) $\(2^x \approx bc-float(L(B-\varepsilon + x))\)$
With $\(\varepsilon\)$ being a tunable constant that we initialize to 0.045 for simplicity
This pass first applies some rewrites to convert suitable arithmetic into base-2 format, and then applies the above approximations.
Inidividual rewrites can be controlled via pass arguments.