Skip to content

JAX log accuracy and correctness #46

Open

Description

@pearu
  -inf    E11111111111111111111111111111111111111111111111111==========================================111=111E
  -3e35   111111111111111111111111111111111111111111111111111=========================================111111111
  -3e32   ==1111111111111111111111111111111111111111111111111=======================================111=111111=
  -3e29   ====11111111111111111111111111111111111111111111111=====================================111=111111===
  -3e26   =======11111111111111111111111111111111111111111111===================================1111111111=====
  -3e23   =======11111111111111111111111111111111111111111111=================================111=111111=======
  -3e20   ==========11111111111111111111111111111111111111111===============================1111111111=========
  -3e17   ============111111111111111111111111111111111111111=============================1111111111===========
  -3e14   =============11111111111111111111111111111111111111===========================1111111111=============
  -3e11   ================1111111111111111111111111111111111111111111111111111111111111111111111===============
  -2e8    ==================111111111111111111111111111111111=======================1111111111=================
  -2e5    ===================11111111111111111111111111111111=====================1111111111===================
  -2e2    =====================1111!!!111111111111111111111111111111111111111111111!!!1111=====================
  -2e-1   ======================111!1111111111111111111111111=================1111111E11=======================
  -2e-4   ======================1=131111111111111111111111111===============111111111!=========================
  -2e-7   ======================1=11=111111111111111111111111=============1111111111=1=========================
  -2e-10  ======================1=11==11111111111111111111111111111111111111111111===1=========================
  -2e-13  ======================1=11==1===111111111111111111111111111111111111111====1=========================
  -2e-16  ======================1=11==1====111111111111111111=======111=1111111=1====1=========================
  -2e-19  ======================1=11==1======1111111111111111=====1111111111====1====1=========================
  -2e-22  ======================1=11==1=========1111111111111===1111111111======1====1=========================
  -2e-25  ======================1=11==1==========111111111111=111=1111111=======1====1=========================
  -2e-28  ======================1=11==1============111111111111=111111==========1====1=========================
  -2e-31  ======================1=11==1===============111111E=1111111===========1====1=========================
  -1e-34  ======================1=11==1=================1111E111111=============1====1=========================
  -1e-37  ======================1=11==1================1!EEEEEEEEEEEEEEEEEEEEEEEEEEEEE=========================
  1e-37   ======================1=11==1================11111E11111==============1====1=========================
  1e-34   ======================1=11==1==============1111111E=111111============1====1=========================
  2e-31   ======================1=11==1============1111111111111111111==========1====1=========================
  2e-28   ======================1=11==1===========11111111111=1111111111========1====1=========================
  2e-25   ======================1=11==1=========1111111111111===1111111111======1====1=========================
  2e-22   ======================1=11==1=======111111111111111=====1111111111====1====1=========================
  2e-19   ======================1=11==1====111111111111111111=======1111111111==1====1=========================
  2e-16   ======================1=11==1===1111111111111111111=========11111111111====1=========================
  2e-13   ======================1=11==11111111111111111111111===========1111111111===1=========================
  2e-10   ======================1=11=111111111111111111111111=============1111111111=1=========================
  2e-7    ======================1=121111111111111111111111111===============111111111!=========================
  2e-4    ======================111!1111111111111111111111111111111111111111111111111E11=======================
  2e-1    =====================1111!EE!1111111111111111111111111111111111111111111!EEE1111=====================
  3e2     ===================11111111111111111111111111111111=====================1111111111===================
  3e5     =================1111111111111111111111111111111111=======================1111111111=================
  3e8     ================11111111111111111111111111111111111=========================1111111111===============
  3e11    ==============11111111111111111111111111111111111111111111111111111111111111111111111111=============
  3e14    ============111111111111111111111111111111111111111=============================1111111111===========
  4e17    ===========1111111111111111111111111111111111111111===============================1111111111=========
  4e20    =======11111111111111111111111111111111111111111111=================================1111111111=======
  4e23    ======111111111111111111111111111111111111111111111===================================1111111111=====
  4e26    ====11111111111111111111111111111111111111111111111=====================================111=111111===
  4e29    ==1111111111111111111111111111111111111111111111111=======================================1111111111=
  5e32    111111111111111111111111111111111111111111111111111=========================================111=11111
  5e35    E11111111111111111111111111111111111111111111111111==========================================111==11E
          -inf  -3e29 -2e20 -1e11 -1e2  -8e-8 -6e-1 -4e-2 -3e-3 1e-33 1e-24 2e-15 3e-6  4e3   5e12  8e21  1e31
ULP-difference z jax:log(z) mpmath:log(z) numpy:log(z)
4294967296 (-inf-infj) (inf-2.3561945j) (inf+nanj) (inf-2.3561945j)
4294967296 (inf-infj) (inf-0.7853982j) (inf+nanj) (inf-0.7853982j)
4294967296 (-8.912321e-39-5.88902e-39j) (-inf+nanj) (-87.432236-2.5576816j) (-87.432236-2.5576816j)
4294967296 (-2.9728335e+38+1.6980625e+38j) (inf+2.622624j) (88.72893+2.622624j) (88.72893+2.622624j)
4294967296 (2.9724068e+38+1.6980625e+38j) (inf+0.5190306j) (88.72882+0.5190305j) (88.72882+0.5190306j)
1044824631 (3.0017088e-38+5.900613e-39j) (-86.399055+0j) (-86.3801+0.19410025j) (-86.3801+0.19410025j)
1002459918 (1.0053276e-36+5.900613e-39j) (-82.88775+0j) (-82.88773+0.005869276j) (-82.88773+0.005869276j)
959979525 (3.3603466e-35+5.900613e-39j) (-79.37843+0j) (-79.37843+0.00017559536j) (-79.37843+0.00017559536j)
917542955 (1.1211664e-33+5.900613e-39j) (-75.87094+0j) (-75.87094+5.2629234e-06j) (-75.87094+5.2629234e-06j)
875145169 (3.73447e-32+5.900613e-39j) (-72.36512+0j) (-72.36512+1.5800403e-07j) (-72.36512+1.5800403e-07j)
832781768 (1.2419864e-30+5.900613e-39j) (-68.86084+0j) (-68.86084+4.750948e-09j) (-68.86084+4.750948e-09j)
790448994 (4.1246158e-29+5.900613e-39j) (-65.357994+0j) (-65.357994+1.4305848e-10j) (-65.357994+1.4305848e-10j)
748143618 (1.36796e-27+5.900613e-39j) (-61.856476+0j) (-61.856476+4.3134394e-12j) (-61.856476+4.3134394e-12j)
705862850 (4.5313372e-26+5.900613e-39j) (-58.356194+0j) (-58.356194+1.3021791e-13j) (-58.356194+1.3021791e-13j)
663604264 (1.4992649e-24+5.900613e-39j) (-54.857067+0j) (-54.857067+3.935671e-15j) (-54.857067+3.935671e-15j)
621365744 (4.9552058e-23+5.900613e-39j) (-51.35902+0j) (-51.35902+1.1907907e-16j) (-51.35902+1.1907907e-16j)
579145435 (1.6360845e-21+5.900613e-39j) (-47.86198+0j) (-47.86198+3.606545e-18j) (-47.86198+3.606545e-18j)
536941705 (5.39681e-20+5.900613e-39j) (-44.365894+0j) (-44.365894+1.093352e-19j) (-44.365894+1.093352e-19j)
494182778 (1.8224923e-18+5.900613e-39j) (-40.846325+0j) (-40.846325+3.2376613e-21j) (-40.846325+3.2376613e-21j)
451380107 (6.162399e-17+5.900613e-39j) (-37.32548+0j) (-37.32548+9.575188e-23j) (-37.32548+9.575188e-23j)
... ... ... ... ...

Activity

  1. pearu commented on Dec 30, 2024

    @pearu
    OwnerAuthor

    With https://github.com/pearu/stablehlo/tree/pearu/log, we'll have

    test_unary[log-jax-cuda-complex64-default] maximal ULP difference: 111
    ULP difference == 0: 1775842
    ULP difference == 1: 324169
    ULP difference == 2: 732
    ULP difference == 3: 6
    ULP difference == 4: 5
    ULP difference == 5: 2
    ULP difference == 6: 1
    ULP difference == 8: 2
    ULP difference == 10: 4
    ULP difference == 12: 3
    ULP difference == 18: 2
    ULP difference >= 20: 32
    
      -inf    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112
      -4e35   11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112221
      -4e32   11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222121
      -5e29   11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112111211
      -5e26   11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112222111211
      -6e23   11=11111111111111111111111111111111111111111111111111111111111111111111111111111111111111122111111111
      -6e20   11=1==1111111111111===============================================================1111122122111111111
      -7e17   11=1==111111111111111===========================================================111112222222111111111
      -8e14   11=1==111=1111111111111111111111111111111111111111111111111111111111111111111111111122222122111111111
      -9e11   11=1==111===11111111111111111111111111111111111111111111111111111111111111111111111221122122111111111
      -9e8    11=1==111===1=111111111111111111111111111111111111111111111111111111111111111112211121112122111121211
      -1e6    11=1==111===1=1=111111111111============================================11111122111121122122111121121
      -1e3    11=1==111===1=1=1111111111111111111111111111111111111111111111111111111111112211111111121122111111211
      -1      11=1==111===1=1=111111111!1111111111111111111111111111111111111111111111112!2111111121212122111111211
      -1e-3   11=1==111===1=1=1111=1111311111111================================111111122!111111111111211211111111=
      -2e-6   11=1==111===1=1=1111=1=111111111111111111111111111111111111111111111122211211111111111121122111111=11
      -2e-9   11=1==111===1=1=1111=1=11111111111111111111111111111111111111111111122221121111111112211212211111111=
      -2e-12  11=1==111===1=1=1111=1=111=1111111111111111111111111111111111111112212221121111111112112212211=111=1=
      -2e-15  11=1==111===1=1=1111=1=111=1==111111111111=================1111112121112112111111111211221111111=1=1=
      -2e-18  11=1==111===1=1=1111=1=111=1====1111111111111111111111111111111211111112112111111111111111=111=1=1=1=
      -2e-21  11=1==111===1=1=1111=1=111=1=====1111111111111=========111112221111111221111111111112111=1111==1=1=1=
      -3e-24  11=1==111===1=1=1111=1=111=1=====111111111111111111111111121211111121112112111111111111111=11==1=1=1=
      -3e-27  11=1==111===1=1=1111=1=111=1=====11==111111111111111111122111111111112121121111111111111===11==1=1=1=
      -3e-30  11=1==111===1=1=1111=1=111=1=====11===11111111111111111122111111111211221121111111111111===11==1=1=1=
      -4e-33  11=1==111===1=1=1111=1=111=1=====11===11=111111111111221111111111112121211211111=1111=11===11==1=1=1=
      -4e-36  11=1==111===1=1=1111=1=111=1=====11===11=11111111112222111111111111111121121111=111=1=11===11==1=1=1=
      0       11=1==111===1=1=1111=1=111=1=====11===11=11111111112122111111111111111121121111=11==1=11===11==1=1=1=
      4e-36   11=1==111===1=1=1111=1=111=1=====11===11=111111111111221111111111112121111211111=1111=11===11==1=1=1=
      4e-33   11=1==111===1=1=1111=1=111=1=====11===11111111111111111222111111111211121121111111=11111===11==1=1=1=
      3e-30   11=1==111===1=1=1111=1=111=1=====11==111111111111111111112211111111111121121111111111111===11==1=1=1=
      3e-27   11=1==111===1=1=1111=1=111=1=====11=111111111111111111111121111111121112112111111111111111=11==1=1=1=
      3e-24   11=1==111===1=1=1111=1=111=1=====1111111111111=========111112121111112221121111111111111=1111==1=1=1=
      3e-21   11=1==111===1=1=1111=1=111=1====1111111111111111111111111111111211111112112111111111111121=111=1=1=1=
      2e-18   11=1==111===1=1=1111=1=111=1==111111111111=================1111122221212112111111111121121111111=1=1=
      2e-15   11=1==111===1=1=1111=1=111=11111111111111111111111111111111111111122122211211111111112222122111111=1=
      2e-12   11=1==111===1=1=1111=1=11111111111111111111111111111111111111111111112221121111111112112112211111111=
      2e-9    11=1==111===1=1=1111=1=111111111111111111111111111111111111111111111111211211111111111212122111111=11
      2e-6    11=1==111===1=1=1111=111131111111111111111111111111111111111111111111111222!111111111221212211111111=
      2e-3    11=1==111===1=1=1111111113111111====================================1111111!2111111121121122111111111
      2       11=1==111===1=1=1111111111111111111111111111111111111111111111111111111111111111111121112122111121121
      1e3     11=1==111===1=1=1111111111111===========================================11111122211122112122111111111
      1e6     11=1==111===1=111111111111111111111111111111111111111111111111111111111111111111221112121122111111211
      1e9     11=1==111===1111111111111===================================================1111122221112122111111111
      1e12    11=1==111=1111111111111=======================================================11111122222122111111211
      1e15    11=1==11111111111111111111111111111111111111111111111111111111111111111111111111111111122122111111111
      9e17    11=1==1111111111111===============================================================1111122122111111111
      8e20    11=11111111111111111111111111111111111111111111111111111111111111111111111111111111111111222121121111
      8e23    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112222111211
      7e26    1111111111111===========================================================================1111122221111
      6e29    11111111111===============================================================================11111222221
      6e32    111111111===================================================================================111112211
      5e35    1111111=======================================================================================1111112
              -inf  -3e29 -2e20 -1e11 -1e2  -8e-8 -6e-1 -4e-2 -3e-3 1e-33 1e-24 2e-15 3e-6  4e3   5e12  8e21  1e31
    
    Details
    | ULP-difference |                          z |                     jax:log(z) |                  mpmath:log(z) |                   numpy:log(z) |                      fa:log(z) |
    | -------------- | -------------------------- | ------------------------------ | ------------------------------ | ------------------------------ | ------------------------------ |
    |            111 |    (0.9970238-0.08273122j) |   (0.00045027726-0.082788505j) |    (0.00045027403-0.08278851j) |    (0.00045027403-0.08278851j) |    (0.00045027403-0.08278852j) |
    |            108 |    (0.9970238+0.06684363j) |   (-0.0007382534+0.066942975j) |    (-0.0007382597+0.06694298j) |    (-0.0007382597+0.06694298j) |    (-0.0007382597+0.06694298j) |
    |             59 | (0.9970238-0.00018063583j) | (-0.0029805866-0.00018117503j) | (-0.0029806003-0.00018117504j) | (-0.0029806003-0.00018117504j) | (-0.0029806003-0.00018117504j) |
    |             58 | (0.9970238+0.00057500304j) |  (-0.002980437+0.00057671935j) |  (-0.0029804504+0.0005767194j) |  (-0.0029804504+0.0005767194j) |  (-0.0029804504+0.0005767194j) |
    |              4 |   (-0.9340932-0.32225433j) |      (-0.011952775-2.8093867j) |      (-0.011952778-2.8093865j) |      (-0.011952778-2.8093865j) |      (-0.011952778-2.8093867j) |
    
    
    test_unary[log-jax-cpu-complex64-default] maximal ULP difference: 304
    ULP difference == 0: 1581005
    ULP difference == 1: 519672
    ULP difference == 2: 57
    ULP difference == 3: 12
    ULP difference == 4: 6
    ULP difference == 5: 3
    ULP difference == 6: 7
    ULP difference == 8: 3
    ULP difference == 9: 2
    ULP difference == 11: 1
    ULP difference == 12: 3
    ULP difference >= 13: 29
    
      -inf    111111111111111111111111111111111111111111111111111===========================================11=1111
      -4e35   111111111111111111111111111111111111111111111111111=========================================111111111
      -5e32   ==1111111111111111111111111111111111111111111111111=======================================11111111111
      -5e29   ====11111111111111111111111111111111111111111111111=====================================1111111111==1
      -6e26   =====1111111111111111111111111111111111111111111111===================================1111111111=1==1
      -6e23   =======11111111111111111111111111111111111111111111=================================1111111111===1==1
      -8e20   =========111111111111111111111111111111111111111111===============================1111111111=====1==1
      -9e17   ==========11111111111111111111111111111111111111111=============================1111111111=======1==1
      -1e15   ==========1==11111111111111111111111111111111111111===========================11111111111========1==1
      -1e12   ==========1====111111111111111111111111111111111111=========================1111111111===========1==1
      -1e9    ==========1======1111111111111111111111111111111111=======================1111111111=1===========1==1
      -1e6    ==========1======1=11111111111111111111111111111111=====================1111111111===1===========1==1
      -2e3    ==========1======1===11111111111111111111111111111111111111111111111111111111111=====1===========1==1
      -2      ==========1======1=====11!1111111111111111111111111111111111111111111111111E111======1===========1111
      -2e-3   ==========1======1======121111111111111111111111111===============111111111!1========1==========111=1
      -2e-6   ==========1======1=======1=111111111111111111111111=============111=111111111========1=========111==1
      -3e-9   ==========1======1=======1==11111111111111111111111===========111=1111111==11========1=======111=1==1
      -3e-12  ==========1======1=======1==1=111111111111111111111=========111=1111111====11========1=====111===1==1
      -3e-15  ==========1======1=======1==1====111111111111111111========1111111111======11========1===111=====1==1
      -4e-18  ==========1======1=======1==1=====11111111111111111======1111111111========11========1=111=======1==1
      -4e-21  ==========1======1=======1==1=======111111111111111====111111111111========11========111=========1==1
      -5e-24  ==========1======1=======1==1==========111111111111==1111111111===1========11======111===========1==1
      -5e-27  ==========1======1=======1==1===========111111111111111111111=====1========11====111=1===========1==1
      -6e-30  ==========1======1=======1==1=============11111111111111111=======1========11==111===1===========1==1
      -7e-33  ==========1======1=======1==1===============1111111111111=========1========11111=====1===========1==1
      -8e-36  ==========1======1=======1==1=================111111111===========1========111=======1===========1==1
      0       ==========1======1=======1==1=================111111111===========1========111=======1===========1==1
      8e-36   ==========1======1=======1==1===============1111111111111=========1========11111=====1===========1==1
      7e-33   ==========1======1=======1==1=============11111111111111111=======1========11==111===1===========1==1
      6e-30   ==========1======1=======1==1===========111111111111111111111=====1========11====111=1===========1==1
      6e-27   ==========1======1=======1==1==========111111111111==1111111111===1========11======111===========1==1
      5e-24   ==========1======1=======1==1=======11111111111111111111111111111=1========11========111=========1==1
      5e-21   ==========1======1=======1==1=====11111111111111111======1111111111========11========1=111=======1==1
      4e-18   ==========1======1=======1==1====111111111111111111=======11111111111======11========1===111=====1==1
      3e-15   ==========1======1=======1==1=111111111111111111111=========111=1111111====11========1=====111===1==1
      3e-12   ==========1======1=======1==11111111111111111111111===========111=1111111==11========1=======111=1==1
      3e-9    ==========1======1=======1=111111111111111111111111=============111=111111111========1========1111==1
      3e-6    ==========1======1======121111111111111111111111111===============111111111!1========1==========111=1
      2e-3    ==========1======1=====11!2211111111111111111111111111111111111111111111122!11=======1===========1111
      2       ==========1======1===111111111111111111111111111111===================11111111111====1===========1==1
      2e3     ==========1======1=11111111111111111111111111111111=====================11111111111==1===========1==1
      2e6     ==========1======1111111111111111111111111111111111=======================1111111111=1===========1==1
      2e9     ==========1====111111111111111111111111111111111111=========================1111111111===========1==1
      1e12    ==========1==11111111111111111111111111111111111111===========================1111111111=========1==1
      1e15    ==========11111111111111111111111111111111111111111=============================1111111111=======1==1
      1e18    =========111111111111111111111111111111111111111111===============================1111111111=====1==1
      1e21    =======11111111111111111111111111111111111111111111=================================1111111111===1==1
      9e23    =====1111111111111111111111111111111111111111111111===================================1111111111=1==1
      8e26    ====11111111111111111111111111111111111111111111111=====================================11111111111=1
      7e29    ==1111111111111111111111111111111111111111111111111=======================================11111111111
      6e32    111111111111111111111111111111111111111111111111111=========================================111111111
      6e35    111111111111111111111111111111111111111111111111111===========================================11=1111
              -inf  -3e29 -2e20 -2e11 -1e2  -1e-7 -1e-1 -8e-2 -6e-3 2e-33 3e-24 3e-15 4e-6  5e3   7e12  9e21  1e31
    
    
    Details
    | ULP-difference |                          z |                    jax:log(z) |                 mpmath:log(z) |                  numpy:log(z) |                     fa:log(z) |
    | -------------- | -------------------------- | ----------------------------- | ----------------------------- | ----------------------------- | ----------------------------- |
    |            304 |    (0.9960317-0.08441714j) |   (-0.0003974469-0.08455141j) |  (-0.00039745573-0.08455141j) |  (-0.00039745573-0.08455141j) |  (-0.00039745573-0.08455141j) |
    |            224 |   (0.9960317+0.099179536j) |    (0.0009569361+0.09924752j) |   (0.00095694914+0.09924752j) |    (0.0009569491+0.09924752j) |    (0.0009569491+0.09924753j) |
    |             31 |  (0.9960317+0.0011839406j) |  (-0.003975472+0.0011886569j) | (-0.0039754864+0.0011886569j) | (-0.0039754864+0.0011886569j) | (-0.0039754864+0.0011886569j) |
    |             26 | (0.9960317-0.00018816232j) | (-0.003976163-0.00018891197j) | (-0.003976175-0.00018891197j) | (-0.003976175-0.00018891197j) | (-0.003976175-0.00018891197j) |
    |              6 |  (-0.93359756+0.26447874j) |     (-0.030112086+2.8655357j) |      (-0.030112097+2.865536j) |     (-0.030112097+2.8655357j) |      (-0.030112099+2.865536j) |
    |              5 |   (-0.8077304-0.64354527j) |        (0.03222809-2.468847j) |        (0.03222811-2.468847j) |       (0.032228105-2.468847j) |      (0.032228105-2.4688473j) |
    
    
  2. pearu commented on Dec 30, 2024

    @pearu
    OwnerAuthor

    The result of

        import jax.numpy as jnp
        dtype = jnp.complex64
        ctx = fa.Context(paths=[fa.algorithms])
        graph = ctx.trace(getattr(fa.algorithms, "log"), dtype)
        graph2 = graph.rewrite(fa.targets.numpy, fa.rewrite)
        func = fa.targets.numpy.as_function(graph2, debug=2, numpy=jnp)
        z = jnp.array(0.9970238-0.08273122j, dtype=jnp.complex64)
        v = func(z)
        print(f'{func.__name__}({str(z)}) -> {str(v)}')

    is

    log((0.9970238-0.08273122j)) -> (0.00045027403-0.082788505j)
    

    that is, using jax functions directly will produce equivalent result to numpy functions.

  3. pearu commented on Dec 31, 2024

    @pearu
    OwnerAuthor

    With the following scripts:

    # run_pearu_log_branch_test.py in jax repo
    import numpy as np
    import jax.numpy as jnp
    import functional_algorithms as fa
    
    def main():
        dtype = jnp.complex64
        ctx = fa.Context(paths=[fa.algorithms])
        graph = ctx.trace(getattr(fa.algorithms, "log"), dtype)
        graph2 = graph.rewrite(fa.targets.numpy, fa.rewrite)
        func = fa.targets.numpy.as_function(graph2, debug=0)
        
        z = jnp.array(0.9970238-0.08273122j, dtype=dtype)
    
        print(jnp.log.lower(z).compile().as_text())
        
        print(f'EXPECTED (0.00090095366 or 0.0009009537): {str(func(z))}')
        print(f'JAX: {str(jnp.log(z))}')
        
    if __name__ == "__main__":
        main()
    #!/bin/bash
    # update_pearu_log_branch.sh in stablehlo repo
    
    PYTHONPATH=../functional_algorithms-4 python build_tools/math/generate_ChloDecompositionPatternsMath.py
    
    git add stablehlo/transforms/StablehloComplexMathExpanderPatterns.td
    git commit --amend --no-edit
    git push -uf origin pearu/log
    sleep 5
    STABLEHLO_SHA256=`curl -sL https://github.com/pearu/stablehlo/archive/refs/heads/pearu/log.zip | sha256sum | cut -d' ' -f1`
    
    cat > ../xla/third_party/stablehlo/workspace.bzl << EndOfMessage
    load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")
    def repo():
        tf_http_archive(
            name = "stablehlo",
            strip_prefix = "stablehlo-pearu-log",
            sha256 = "${STABLEHLO_SHA256}",
            urls = tf_mirror_urls("https://github.com/pearu/stablehlo/archive/refs/heads/pearu/log.zip"),
            patch_file = [],
        )
    EndOfMessage
    
    echo "Created ../xla/third_party/stablehlo/workspace.bzl:"
    cat ../xla/third_party/stablehlo/workspace.bzl
    

    it was established that in the stablehlo->xla->jax path, the complex_log algorithm transforms such that sum_2sum([-one, yyh, xxh, yyl, xxl], fast=fast) gives different result in the beginning and end of the path.

    A more stable algorithm is obtained by using sum_2sum([-one, max(yyh, xxh), min(yyh, xxh), yyl, xxl], fast=fast)

  4. pearu commented on Dec 31, 2024

    @pearu
    OwnerAuthor

    Applying the above fix to https://github.com/pearu/stablehlo/tree/pearu/log, we'll have

    test_unary[log-jax-cuda-complex64-default] maximal ULP difference: 2
    ULP difference == 0: 1775914
    ULP difference == 1: 324185
    ULP difference == 2: 701
    
      -inf    1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111!
      -4e35   1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111!!!1
      -4e32   11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111!!!1!1
      -5e29   1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111!111!11
      -5e26   1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111!!!!111!11
      -6e23   11=111111111111111111111111111111111111111111111111111111111111111111111111111111111111111!!111111111
      -6e20   11=1==1111111111111===============================================================11111!!1!!111111111
      -7e17   11=1==111111111111111===========================================================11111!!!!!!!111111111
      -8e14   11=1==111=11111111111111111111111111111111111111111111111111111111111111111111111111!!!!!1!!111111111
      -9e11   11=1==111===11111111111111111111111111111111111111111111111111111111111111111111111!!11!!1!!111111111
      -9e8    11=1==111===1=11111111111111111111111111111111111111111111111111111111111111111!!111!111!1!!1111!1!11
      -1e6    11=1==111===1=1=111111111111============================================111111!!1111!11!!1!!1111!11!1
      -1e3    11=1==111===1=1=111111111111111111111111111111111111111111111111111111111111!!111111111!11!!111111!11
      -1      11=1==111===1=1=1111111111111111111111111111111111111111111111111111111111!!!1111111!1!1!1!!111111!11
      -1e-3   11=1==111===1=1=1111=1111111111111================================1111111!!1111111111111!11!11111111=
      -2e-6   11=1==111===1=1=1111=1=1111111111111111111111111111111111111111111111!!!11!111111111111!11!!111111=11
      -2e-9   11=1==111===1=1=1111=1=111111111111111111111111111111111111111111111!!!!11!111111111!!11!1!!11111111=
      -2e-12  11=1==111===1=1=1111=1=111=111111111111111111111111111111111111111!!1!!!11!111111111!11!!1!!11=111=1=
      -2e-15  11=1==111===1=1=1111=1=111=1==111111111111=================111111!1!111!11!111111111!11!!1111111=1=1=
      -2e-18  11=1==111===1=1=1111=1=111=1====1111111111111111111111111111111!1111111!11!111111111111111=111=1=1=1=
      -2e-21  11=1==111===1=1=1111=1=111=1=====1111111111111=========11111!!!1111111!!111111111111!111=1111==1=1=1=
      -3e-24  11=1==111===1=1=1111=1=111=1=====1111111111111111111111111!1!111111!111!11!111111111111111=11==1=1=1=
      -3e-27  11=1==111===1=1=1111=1=111=1=====11==1111111111111111111!!11111111111!1!11!1111111111111===11==1=1=1=
      -3e-30  11=1==111===1=1=1111=1=111=1=====11===111111111111111111!!111111111!11!!11!1111111111111===11==1=1=1=
      -4e-33  11=1==111===1=1=1111=1=111=1=====11===11=111111111111!!111111111111!1!1!11!11111=1111=11===11==1=1=1=
      -4e-36  11=1==111===1=1=1111=1=111=1=====11===11=1111111111!!!!1111111111111111!11!1111=111=1=11===11==1=1=1=
      0       11=1==111===1=1=1111=1=111=1=====11===11=1111111111!1!!1111111111111111!11!1111=11==1=11===11==1=1=1=
      4e-36   11=1==111===1=1=1111=1=111=1=====11===11=111111111111!!111111111111!1!1111!11111=1111=11===11==1=1=1=
      4e-33   11=1==111===1=1=1111=1=111=1=====11===11111111111111111!!!111111111!111!11!1111111=11111===11==1=1=1=
      3e-30   11=1==111===1=1=1111=1=111=1=====11==11111111111111111111!!111111111111!11!1111111111111===11==1=1=1=
      3e-27   11=1==111===1=1=1111=1=111=1=====11=1111111111111111111111!11111111!111!11!111111111111111=11==1=1=1=
      3e-24   11=1==111===1=1=1111=1=111=1=====1111111111111=========11111!1!111111!!!11!1111111111111=1111==1=1=1=
      3e-21   11=1==111===1=1=1111=1=111=1====1111111111111111111111111111111!1111111!11!1111111111111!1=111=1=1=1=
      2e-18   11=1==111===1=1=1111=1=111=1==111111111111=================11111!!!!1!1!11!1111111111!11!1111111=1=1=
      2e-15   11=1==111===1=1=1111=1=111=111111111111111111111111111111111111111!!1!!!11!1111111111!!!!1!!111111=1=
      2e-12   11=1==111===1=1=1111=1=1111111111111111111111111111111111111111111111!!!11!111111111!11!11!!11111111=
      2e-9    11=1==111===1=1=1111=1=111111111111111111111111111111111111111111111111!11!11111111111!1!1!!111111=11
      2e-6    11=1==111===1=1=1111=111111111111111111111111111111111111111111111111111!!!1111111111!!1!1!!11111111=
      2e-3    11=1==111===1=1=1111111111111111====================================1111111!!1111111!11!11!!111111111
      2       11=1==111===1=1=11111111111111111111111111111111111111111111111111111111111111111111!111!1!!1111!11!1
      1e3     11=1==111===1=1=1111111111111===========================================111111!!!111!!11!1!!111111111
      1e6     11=1==111===1=111111111111111111111111111111111111111111111111111111111111111111!!111!1!11!!111111!11
      1e9     11=1==111===1111111111111===================================================11111!!!!111!1!!111111111
      1e12    11=1==111=1111111111111=======================================================111111!!!!!1!!111111!11
      1e15    11=1==111111111111111111111111111111111111111111111111111111111111111111111111111111111!!1!!111111111
      9e17    11=1==1111111111111===============================================================11111!!1!!111111111
      8e20    11=11111111111111111111111111111111111111111111111111111111111111111111111111111111111111!!!1!11!1111
      8e23    1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111!!!!111!11
      7e26    1111111111111===========================================================================11111!!!!1111
      6e29    11111111111===============================================================================11111!!!!!1
      6e32    111111111===================================================================================11111!!11
      5e35    1111111=======================================================================================111111!
              -inf  -3e29 -2e20 -1e11 -1e2  -8e-8 -6e-1 -4e-2 -3e-3 1e-33 1e-24 2e-15 3e-6  4e3   5e12  8e21  1e31
    
    Details
    | ULP-difference |                               z |                  jax:log(z) |               mpmath:log(z) |                numpy:log(z) |                   fa:log(z) |
    | -------------- | ------------------------------- | --------------------------- | --------------------------- | --------------------------- | --------------------------- |
    |              2 |   (1.4346998e+23-219266760000j) |   (53.32041-1.5283112e-12j) |    (53.32041-1.528311e-12j) |    (53.32041-1.528311e-12j) |    (53.32041-1.528311e-12j) |
    |              2 |         (14.309524-0.44508672j) |     (2.661409-0.031094208j) |     (2.661409-0.031094205j) |     (2.661409-0.031094206j) |     (2.661409-0.031094208j) |
    |              2 |       (8.076745e+31-1981990.5j) |  (73.469124-2.4539474e-26j) |   (73.469124-2.453947e-26j) |   (73.469124-2.453947e-26j) |   (73.469124-2.453947e-26j) |
    |              2 |      (1.5527439e+35-105.06358j) |     (81.0305-6.766318e-34j) |     (81.0305-6.766317e-34j) |     (81.0305-6.766317e-34j) |     (81.0305-6.766317e-34j) |
    |              2 |    (26702424000000-0.63583815j) |  (30.915775-2.3812004e-14j) |     (30.915775-2.3812e-14j) |     (30.915775-2.3812e-14j) |     (30.915775-2.3812e-14j) |
    |              2 |  (4.7612758e-07-2.0117278e-19j) |   (-14.55758-4.225186e-13j) |  (-14.55758-4.2251866e-13j) |  (-14.55758-4.2251866e-13j) |  (-14.55758-4.2251866e-13j) |
    |              2 | (26702424000000-3.3908908e-13j) |  (30.915775-1.2698814e-26j) | (30.915775-1.26988125e-26j) | (30.915775-1.26988125e-26j) | (30.915775-1.26988125e-26j) |
    |              2 |   (5.9872124e-22-8.445849e-22j) |     (-48.319622-0.9541262j) |    (-48.319622-0.95412606j) |    (-48.319622-0.95412606j) |    (-48.319622-0.95412606j) |
    |              2 | (26702424000000-2.4872412e-21j) |  (30.915775-9.3146654e-35j) |  (30.915775-9.3146643e-35j) |  (30.915775-9.3146643e-35j) |  (30.915775-9.3146643e-35j) |
    |            ... |                             ... |                         ... |                         ... |                         ... |                         ... |
    |              2 |  (4.5783826e-35-4.0181524e-36j) |     (-79.06529-0.08753929j) |     (-79.06529-0.08753928j) |    (-79.06529-0.087539285j) |     (-79.06529-0.08753928j) |
    |              2 |  (4.7632175e-29+5.0048504e-29j) |     (-64.84212+0.81013024j) |      (-64.84212+0.8101301j) |      (-64.84212+0.8101301j) |      (-64.84212+0.8101302j) |
    |              2 |  (2.4685381e-37-3.5658927e-37j) |      (-83.72847-0.9652815j) |     (-83.72847-0.96528137j) |     (-83.72847-0.96528137j) |     (-83.72847-0.96528137j) |
    |              2 |   (1.9861361e-13+1.591428e-31j) | (-29.247416+8.0126825e-19j) | (-29.247416+8.0126835e-19j) | (-29.247416+8.0126835e-19j) | (-29.247416+8.0126835e-19j) |
    |              2 |  (4.7111167e-20+8.7789736e-23j) |  (-44.501778+0.0018634568j) |   (-44.501774+0.001863457j) |  (-44.501774+0.0018634569j) |   (-44.501778+0.001863457j) |
    |              2 |  (2.0724164e-14+5.7868767e-16j) |    (-31.507088+0.02791608j) |   (-31.507086+0.027916076j) |   (-31.507086+0.027916078j) |   (-31.507088+0.027916078j) |
    |              2 |      (2.3530877e+24+7401.2666j) |   (56.11777+3.1453428e-21j) |   (56.11777+3.1453424e-21j) |   (56.11777+3.1453424e-21j) |   (56.11777+3.1453424e-21j) |
    |              2 |   (4175212000000000+7401.2666j) |   (35.96794+1.7726686e-12j) |   (35.96794+1.7726684e-12j) |   (35.96794+1.7726684e-12j) |   (35.96794+1.7726684e-12j) |
    |              2 |   (8.076745e+31+1.5738567e+29j) |   (73.469124+0.0019486252j) |     (73.46913+0.001948625j) |    (73.46913+0.0019486251j) |   (73.469124+0.0019486251j) |
    |              2 |  (1.8419294e+19+3.3287938e-18j) |  (44.359932+1.8072319e-37j) |  (44.359932+1.8072321e-37j) |  (44.359932+1.8072321e-37j) |  (44.359932+1.8072321e-37j) |
    
    
    test_unary[log-jax-cpu-complex64-default] maximal ULP difference: 3
    ULP difference == 0: 1581126
    ULP difference == 1: 519652
    ULP difference == 2: 19
    ULP difference == 3: 3
    
      -inf    111111111111111111111111111111111111111111111111111===========================================11=1111
      -4e35   111111111111111111111111111111111111111111111111111=========================================111111111
      -5e32   ==1111111111111111111111111111111111111111111111111=======================================11111111111
      -5e29   ====11111111111111111111111111111111111111111111111=====================================1111111111==1
      -6e26   =====1111111111111111111111111111111111111111111111===================================1111111111=1==1
      -6e23   =======11111111111111111111111111111111111111111111=================================1111111111===1==1
      -8e20   =========111111111111111111111111111111111111111111===============================1111111111=====1==1
      -9e17   ==========11111111111111111111111111111111111111111=============================1111111111=======1==1
      -1e15   ==========1==11111111111111111111111111111111111111===========================11111111111========1==1
      -1e12   ==========1====111111111111111111111111111111111111=========================1111111111===========1==1
      -1e9    ==========1======1111111111111111111111111111111111=======================1111111111=1===========1==1
      -1e6    ==========1======1=11111111111111111111111111111111=====================1111111111===1===========1==1
      -2e3    ==========1======1===11111111111111111111111111111111111111111111111111111111111=====1===========1==1
      -2      ==========1======1=====11!1111111111111111111111111111111111111111111111111!111======1===========1111
      -2e-3   ==========1======1======111111111111111111111111111===============11111111111========1==========111=1
      -2e-6   ==========1======1=======1=111111111111111111111111=============111=111111111========1=========111==1
      -3e-9   ==========1======1=======1==11111111111111111111111===========111=1111111==11========1=======111=1==1
      -3e-12  ==========1======1=======1==1=111111111111111111111=========111=1111111====11========1=====111===1==1
      -3e-15  ==========1======1=======1==1====111111111111111111========1111111111======11========1===111=====1==1
      -4e-18  ==========1======1=======1==1=====11111111111111111======1111111111========11========1=111=======1==1
      -4e-21  ==========1======1=======1==1=======111111111111111====111111111111========11========111=========1==1
      -5e-24  ==========1======1=======1==1==========111111111111==1111111111===1========11======111===========1==1
      -5e-27  ==========1======1=======1==1===========111111111111111111111=====1========11====111=1===========1==1
      -6e-30  ==========1======1=======1==1=============11111111111111111=======1========11==111===1===========1==1
      -7e-33  ==========1======1=======1==1===============1111111111111=========1========11111=====1===========1==1
      -8e-36  ==========1======1=======1==1=================111111111===========1========111=======1===========1==1
      0       ==========1======1=======1==1=================111111111===========1========111=======1===========1==1
      8e-36   ==========1======1=======1==1===============1111111111111=========1========11111=====1===========1==1
      7e-33   ==========1======1=======1==1=============11111111111111111=======1========11==111===1===========1==1
      6e-30   ==========1======1=======1==1===========111111111111111111111=====1========11====111=1===========1==1
      6e-27   ==========1======1=======1==1==========111111111111==1111111111===1========11======111===========1==1
      5e-24   ==========1======1=======1==1=======11111111111111111111111111111=1========11========111=========1==1
      5e-21   ==========1======1=======1==1=====11111111111111111======1111111111========11========1=111=======1==1
      4e-18   ==========1======1=======1==1====111111111111111111=======11111111111======11========1===111=====1==1
      3e-15   ==========1======1=======1==1=111111111111111111111=========111=1111111====11========1=====111===1==1
      3e-12   ==========1======1=======1==11111111111111111111111===========111=1111111==11========1=======111=1==1
      3e-9    ==========1======1=======1=111111111111111111111111=============111=111111111========1========1111==1
      3e-6    ==========1======1======111111111111111111111111111===============11111111111========1==========111=1
      2e-3    ==========1======1=====11!!!111111111111111111111111111111111111111111111!!!11=======1===========1111
      2       ==========1======1===111111111111111111111111111111===================11111111111====1===========1==1
      2e3     ==========1======1=11111111111111111111111111111111=====================11111111111==1===========1==1
      2e6     ==========1======1111111111111111111111111111111111=======================1111111111=1===========1==1
      2e9     ==========1====111111111111111111111111111111111111=========================1111111111===========1==1
      1e12    ==========1==11111111111111111111111111111111111111===========================1111111111=========1==1
      1e15    ==========11111111111111111111111111111111111111111=============================1111111111=======1==1
      1e18    =========111111111111111111111111111111111111111111===============================1111111111=====1==1
      1e21    =======11111111111111111111111111111111111111111111=================================1111111111===1==1
      9e23    =====1111111111111111111111111111111111111111111111===================================1111111111=1==1
      8e26    ====11111111111111111111111111111111111111111111111=====================================11111111111=1
      7e29    ==1111111111111111111111111111111111111111111111111=======================================11111111111
      6e32    111111111111111111111111111111111111111111111111111=========================================111111111
      6e35    111111111111111111111111111111111111111111111111111===========================================11=1111
              -inf  -3e29 -2e20 -2e11 -1e2  -1e-7 -1e-1 -8e-2 -6e-3 2e-33 3e-24 3e-15 4e-6  5e3   7e12  9e21  1e31
    
    Details
    | ULP-difference |                           z |                jax:log(z) |             mpmath:log(z) |              numpy:log(z) |                 fa:log(z) |
    | -------------- | --------------------------- | ------------------------- | ------------------------- | ------------------------- | ------------------------- |
    |              3 |     (0.6180555-0.64354527j) | (-0.11398816-0.80559975j) |  (-0.11398814-0.8055997j) | (-0.11398814-0.80559975j) |  (-0.11398814-0.8055997j) |
    |              3 |    (-0.6818632+0.26447874j) |   (-0.31285036+2.771581j) |   (-0.31285045+2.771581j) |   (-0.31285042+2.771581j) |  (-0.31285042+2.7715812j) |
    |              2 |      (-1.370664-0.8882466j) |   (0.49060863-2.5665958j) |    (0.4906087-2.5665958j) |    (0.4906087-2.5665958j) |    (0.4906087-2.5665958j) |
    |              2 |  (-0.033882555+0.76447874j) |   (-0.2675798+1.6150885j) |  (-0.26757985+1.6150885j) |  (-0.26757982+1.6150885j) |  (-0.26757982+1.6150885j) |
    |              2 |    (0.01125372+0.76447874j) |  (-0.26845267+1.5560766j) |  (-0.26845273+1.5560766j) |   (-0.2684527+1.5560766j) |   (-0.2684527+1.5560766j) |
    |              2 | (-0.0035384663+0.76447874j) |   (-0.2685504+1.5754249j) |  (-0.26855034+1.5754249j) |  (-0.26855034+1.5754249j) |  (-0.26855037+1.5754249j) |
    |              2 |     (0.7440476+0.06853282j) | (-0.29142618+0.09184895j) | (-0.29142624+0.09184895j) |  (-0.2914262+0.09184895j) | (-0.2914262+0.091848955j) |
    |              2 |  (0.0003419906+0.76447874j) |     (-0.268561+1.570349j) |   (-0.26856095+1.570349j) |   (-0.26856095+1.570349j) |   (-0.26856095+1.570349j) |
    
    
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions