TorchLean API

NN.Floats.IEEEExec.Rules.TranscendentalRules

Deterministic transcendental functions (exp, log, tanh, sinh, cosh) #

IEEE-754 fixes the representation and the basic arithmetic operations, but it does not mandate exact bit-level behavior for transcendental functions like exp and log. In practice, libraries (libm, SVML, CUDA math, etc.) make slightly different choices, and results can differ across platforms.

In TorchLean, we still need executable transcendental operations in some examples and runtime experiments. So we give IEEE32Exec deterministic definitions for a small set of functions using fixed algorithms:

Here we prove the special-value rules for those definitions: how NaNs, infinities, signed zeros (+0 and -0), and sign checks behave. These are the facts we want in proofs without unfolding the full fixed-point implementations.

We do not claim that the finite-value approximations match any particular hardware expf/logf; the Lean definitions prioritize reproducibility and well-defined behavior.

Finite approximation contracts #

The deterministic functions in this module have proved special-value behavior below. A numerical accuracy claim on finite inputs requires a separate contract. Keeping that evidence explicit prevents reproducible execution from being mistaken for correct rounding.

structure TorchLean.Floats.IEEE754.IEEE32Exec.UnaryApproximationContract (impl : IEEE32ExecIEEE32Exec) (spec : ) (domain : Prop) (tolerance : ) :

A uniform real-error contract for an executable unary binary32 operation on a stated domain.

For every finite input whose real value lies in domain, the executable result must also be finite and differ from spec by at most tolerance.

Instances For
    theorem TorchLean.Floats.IEEE754.IEEE32Exec.UnaryApproximationContract.mono {impl : IEEE32ExecIEEE32Exec} {spec : } {domain : Prop} {tolerance tolerance' : } (h : UnaryApproximationContract impl spec domain tolerance) (hle : tolerance tolerance') :
    UnaryApproximationContract impl spec domain tolerance'

    A proved approximation contract remains valid when its tolerance is weakened.

    exp #

    Special values we follow (common libm / PyTorch behavior):

    For finite inputs, Exec32.lean defines a deterministic approximation. These lemmas only expose the early "special-case" branches, so proofs do not have to unfold the fixed-point core.

    exp propagates NaNs by returning the quieted NaN payload.

    log #

    The special cases match the usual real-analytic extension used by common libraries:

    PyTorch follows these conventions on IEEE hardware; we mirror them in IEEE32Exec.log.

    log propagates NaNs by returning the quieted NaN payload.

    $\log(-\infty)=\mathrm{NaN}$ (domain error for negative inputs).

    log returns canonicalNaN on negative finite inputs (domain error).

    This captures the standard real domain restriction $\log:(0,+\infty)\to\mathbb{R}$, transported to the float32 setting (excluding NaNs, infinities, and zeros, which are handled by earlier branches).

    tanh #

    Special values we follow:

    The bit patterns used below are the IEEE-754 binary32 encodings of +1.0f and -1.0f:

    tanh propagates NaNs by returning the quieted NaN payload (via chooseNaN1).

    sinh / cosh #

    These are defined in Exec32.lean in terms of exp (with a NaN short-circuit up front), so the special cases are the familiar ones:

    sinh propagates NaNs by returning the quieted NaN payload (via chooseNaN1).

    cosh propagates NaNs by returning the quieted NaN payload (via chooseNaN1).