Skip to content

[proposal] Numerical compliance #1716

Closed

Description

@alan-baker

Proposal

Follow IEEE-754 except:

  • No signalling NaNs
  • No floating-point exceptions
  • Implementations may flush denormals on arithmetic instructions
    • we can either define the arithmetic instructions that flush them or the data movement instructions that must preserve them
  • Implementation may assume that NaNs and infinities are not present
  • Implementations may ignore the sign of a zero
  • Use Vulkan accuracy
  • Min and max on zeroes can return either operand
  • No rounding mode is specified

The spec will need to define ULP and correctly rounded. Min and max definitions should be updated. Perhaps notes added about tests like isNan, isInf, etc that may not provide the expected results.

Survey

HLSL

HLSL appears to be quite strict in terms of numerical accuracy (see docs). Honours IEEE-754 generally. Uses round to even rounding mode.

Exceptions:

  • No floating-point exceptions
  • Denorms are flushed to appropriate signed zero, except for data movement
  • Min/max on denorms flush inputs, but may not flush outputs
  • Accuracy is generally 0.5 ULP, except:
    • sqrt is 1 ULP (shader assembly sqrt is absolute error of 2-21)
    • 1/x is 1 ULP (x/y must be at least as accurate as x * (1/y))
    • rcp has absolute error of 2-21
  • Following formulae must result in x (modulo denorm flushing):
    • x * 1.0f must be x
    • x / 1.0f must be x
    • x +/- 0.0f must be x
  • min(-0.0, +0.0) and min(+0.0, -0.0) , may be +/-0.0 (same for max)
  • NaN on input produces NaN on output (though bit pattern may change), except for:
    • min/max(SNaN, x) == nan, min/max(x, SNan) == nan
    • min/max(QNaN, x) == x, min/max(x, QNaN) == x

MSL

The MSL compiler defaults to using fast relaxed math, so those conditions are documented here. I think fast relaxed math is what we should be aiming for in MVP. Uses round-to-even or round-to-zero rounding modes.

IEEE-754 Exceptions:

  • May assume NaNs and infinities are not present
  • Denormals may be flushed to zero on arithmetic inputs and outputs
  • No floating-point exceptions
  • Accuracy is given in table 7.2
    • Some are ULP, some are derived
    • Very similar to Vulkan, but a bit stricter in some cases

Vulkan

Vulkan basically assumes fast relaxed math.

IEEE-754 Exceptions

  • May assume nans and infinities are not present
  • Denorms may be flushed (except for data movement)
  • No floating-point exceptions
  • Signed zeroes and infinities are generated, but optimizations can ignore them so effectively not supported
  • Accuracy defined or derived in tables 85 and 86

Activity

  1. added
    wgsl
    on May 7, 2021
  2. mmgeorge commented on May 7, 2021

    @mmgeorge

    Somewhat related to this, is there any plan to add precision specifiers to WebGPU? Seems like maybe this is being discussed in #674? If so, it would be great if these were also well-defined types.

    Related Section 4.5.2 of the GLSL Spec which states that:

    • It is strongly advised that the vertex language provide a floating point range and precision matching that
      of an IEEE single precision floating point number, or better

    • It is useful, but not required, for the fragment language to provide the same floating point range and
      precision as is required for the vertex shader.

  3. alan-baker commented on May 7, 2021

    @alan-baker
    ContributorAuthor

    I believe mediump and the like are out of scope for MVP. I would anticipate separate extensions for both fp16 and relaxed precision after MVP.

  4. mmgeorge commented on May 7, 2021

    @mmgeorge

    @alan-baker does that mean that all implementations will be required to have FP32, or will certain implementations (e.g. mobile) be allowed to have less precision?

  5. alan-baker commented on May 10, 2021

    @alan-baker
    ContributorAuthor

    Yes, all implementations will be 32-bit float only for MVP.

  6. mmgeorge commented on May 10, 2021

    @mmgeorge

    Thanks @alan-baker for the response! That was my only main concern.

  7. litherum commented on May 11, 2021

    @litherum
    Contributor

    Yes, all implementations will be 32-bit float only for MVP.

    The group agreed (a while ago) to have a FP16 extension in MVP. I just haven't written the PR yet.

  8. kdashg commented on May 17, 2021

    @kdashg
    Contributor
    WGSL meeting minutes 2021-05-11
    • RM: Should be the least precise of the underlying platforms. Question. How does fast math work, and is it independent of accuracy. Can fast-math reduce accuracy below.
    • AB: Vulkan doesn’t specify it as an option. It’s enabled-by-default. E.g. in Vulkan for fast-math the accuracy says “must be no worse than the broken-down operations”.
    • AB: fast-math can be explained by “these are the things you’re allowed to do to rearrange.”
    • MM: Metal fast-math is a distinct opt-in. Scientific computing needs stronger guarantees.
    • AB: My intent is fast-math is the default. Could have post-MVP option to add stronger guarantees.
    • DN: Vulkan SPIR-V has “NoContraction” decoration to apply stronger guarantees.
    • MM: Ok to have an extension for stronger guarantees.
    • AB: Vulkan will get some way there, but not all the way.
    • MM: Want to be open to updating the “fast math” definition in WGSL to match what the underlying APIs do natively and most quickly.
    • TR: HLSL, is not strict by default. It has “precise”, but that’s not the default. Strictness as documented is strictness of the operations themselves when marked IEEE safe mode and “precise” and other assumptions like no infinities and NaNs. By default it’s more like Vulkan’s behaviour.
    • AB: I’ll pick it up from here.
  9. added a commit that references this issue on May 18, 2021
    2b51191
  10. added a commit that references this issue on May 27, 2021
    7578481
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgsl

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions