Skip to content

Gradient of pt.minimum is wrong in numba mode due to fast_math assoc flag #2187

@jessegrabowski

Description

@jessegrabowski

MWE:

  import numpy as np
  import pytensor
  import pytensor.tensor as pt
  from pytensor.compile.mode import Mode

  x, y, z, w, b = pt.vectors("x", "y", "z", "w", "b")
  m = pt.minimum(x * y * z * w, b).sum()
  gx = pytensor.grad(m, x)

  xv = np.array([0.9887127230177662])
  yv = np.array([1.000617328480952])
  zv = np.array([0.9995082121912299])
  wv = np.array([404.146799095179])
  bv = np.array([((xv[0] * yv[0]) * zv[0]) * wv[0]])   # exactly the left-assoc bits of x*y*z*w

  cvm = pytensor.function([x, y, z, w, b], gx, mode=Mode(linker="cvm", optimizer="fast_run"))
  nb  = pytensor.function([x, y, z, w, b], gx, mode="NUMBA")
  print(f"cvm: {cvm(xv, yv, zv, wv, bv)}")   # [404.19741326]
  print(f"nb:  {nb(xv, yv, zv, wv, bv)}")    # [0.]

The robot claims that we should be storing the floating-point result of the forward for re-use in the backward to avoid this. Obviously I can just disable fastmath, but I want my math to be fast :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions