Exemple #1
0
def _normalize_float(x):
    info = dtypes.finfo(dtypes.dtype(x))
    int_type = _INT_DTYPES[info.bits]
    cond = lax.abs(x) < info.tiny
    x1 = _where(cond, x * _lax_const(x, 1 << info.nmant), x)
    x2 = _where(cond, int_type(-info.nmant), int_type(0))
    return lax.bitcast_convert_type(x1, int_type), x2
Exemple #2
0
def _normalize_float(x):
  info = dtypes.finfo(dtypes.dtype(x))
  cond = lax.abs(x) < info.tiny
  x1 = _where(cond, x * _lax_const(x, 1 << info.nmant), x)
  x2 = _where(cond, lax.full_like(x, -info.nmant, dtype=np.int32), lax.full_like(x, 0, dtype=np.int32))
  int_type = _INT_DTYPES[info.bits]
  return lax.bitcast_convert_type(x1, int_type), x2
Exemple #3
0
    def testNumpyAndXLAAgreeOnFloatEndianness(self, dtype):
        if not FLAGS.jax_enable_x64 and onp.issubdtype(dtype, onp.float64):
            raise SkipTest("can't test float64 agreement")

        bits_dtype = onp.uint32 if onp.finfo(dtype).bits == 32 else onp.uint64
        numpy_bits = onp.array(1., dtype).view(bits_dtype)
        xla_bits = api.jit(lambda: lax.bitcast_convert_type(
            onp.array(1., dtype), bits_dtype))()
        self.assertEqual(numpy_bits, xla_bits)
Exemple #4
0
 def testBitcastElementType(
     self,
     shape,
     from_dtype,
     to_dtype,
     bdims,
 ):
     rng = jtu.rand_default(self.rng())
     op = lambda x: lax.bitcast_convert_type(x, to_dtype)
     self._CheckBatching(op, 10, bdims, (shape, ), (from_dtype, ), rng)
Exemple #5
0
def ldexp(x1, x2):
    _check_arraylike("ldexp", x1, x2)
    x1_dtype = dtypes.dtype(x1)
    x2_dtype = dtypes.dtype(x2)
    if (dtypes.issubdtype(x1_dtype, np.complexfloating)
            or dtypes.issubdtype(x2_dtype, np.inexact)):
        raise ValueError(
            f"ldexp not supported for input types {(x1_dtype, x2_dtype)}")

    x1, x2 = _promote_shapes("ldexp", x1, x2)

    dtype = dtypes.canonicalize_dtype(dtypes._to_inexact_dtype(x1_dtype))
    info = dtypes.finfo(dtype)
    int_type = _INT_DTYPES[info.bits]

    x1 = lax.convert_element_type(x1, dtype)
    x2 = lax.convert_element_type(x2, int_type)

    mask = (1 << info.nexp) - 1
    bias = ((1 << info.nexp) - 1) >> 1
    x, e = _normalize_float(x1)
    x2 += e + ((x >> info.nmant) & mask) - bias

    # find underflow/overflow before denormalization
    underflow_cond = x2 < -(bias + info.nmant)
    overflow_cond = x2 > bias

    m = lax.full_like(x, 1, dtype=dtype)

    # denormals
    cond = x2 < -bias + 1
    x2 = _where(cond, x2 + info.nmant, x2)
    m = _where(cond, m / (1 << info.nmant), m)

    x2 = lax.convert_element_type(x2, np.int32)
    x &= ~(mask << info.nmant)
    x |= ((lax.convert_element_type(x2, int_type) + bias) << info.nmant)

    x = lax.convert_element_type(m, dtype) * lax.bitcast_convert_type(x, dtype)

    # underflow
    x = _where(underflow_cond, lax.full_like(x, 0, dtype=dtype), x)
    # overflow
    x = _where(overflow_cond, lax.sign(x1) * lax.full_like(x, np.inf), x)
    # ldexp(x1, x2) = x1 for x1 = inf, -inf, nan, 0
    return _where(isinf(x1) | isnan(x1) | (x1 == 0), x1, x)
Exemple #6
0
def frexp(x):
    _check_arraylike("frexp", x)
    x, = _promote_dtypes_inexact(x)
    if dtypes.issubdtype(x.dtype, np.complexfloating):
        raise TypeError("frexp does not support complex-valued inputs")

    dtype = dtypes.dtype(x)
    info = dtypes.finfo(dtype)
    mask = (1 << info.nexp) - 1
    bias = ((1 << info.nexp) - 1) >> 1

    x1, x2 = _normalize_float(x)
    x2 += ((x1 >> info.nmant) & mask) - bias + 1
    x1 &= ~(mask << info.nmant)
    x1 |= (bias - 1) << info.nmant
    x1 = lax.bitcast_convert_type(x1, dtype)

    cond = isinf(x) | isnan(x) | (x == 0)
    x2 = _where(cond, lax_internal._zeros(x2), x2)
    return _where(cond, x, x1), lax.convert_element_type(x2, np.int32)
Exemple #7
0
def ldexp(x1, x2):
  _check_arraylike("ldexp", x1, x2)
  dtype = dtypes.canonicalize_dtype(_result_dtype(np.ldexp, x1, x2))
  x1, x2 = _promote_shapes("ldexp", x1, x2)
  x1 = lax.convert_element_type(x1, dtype)

  info = dtypes.finfo(dtype)
  mask = (1 << info.nexp) - 1
  bias = ((1 << info.nexp) - 1) >> 1

  int_type = _INT_DTYPES[info.bits]

  x, e = _normalize_float(x1)
  x2 += e + ((x >> info.nmant) & mask) - bias

  # find underflow/overflow before denormalization
  underflow_cond = x2 < -(bias + info.nmant)
  overflow_cond = x2 > bias

  m = lax.full_like(x, 1, dtype=dtype)

  # denormals
  cond = x2 < -bias + 1
  x2 = _where(cond, x2 + info.nmant, x2)
  m = _where(cond, m / (1 << info.nmant), m)

  x2 = lax.convert_element_type(x2, np.int32)
  x &= ~(mask << info.nmant)
  x |= ((lax.convert_element_type(x2, int_type) + bias) << info.nmant)

  x = lax.convert_element_type(m, dtype) * lax.bitcast_convert_type(x, dtype)

  # underflow
  x = _where(underflow_cond, lax.full_like(x, 0, dtype=dtype), x)
  # overflow
  x = _where(overflow_cond, lax.sign(x1) * lax.full_like(x, np.inf), x)
  # ldexp(x1, x2) = x1 for x1 = inf, -inf, nan, 0
  return _where(isinf(x1) | isnan(x1) | (x1 == 0), x1, x)
Exemple #8
0
def signbit(x):
    x, = _promote_args("signbit", x)
    dtype = dtypes.dtype(x)
    if dtypes.issubdtype(dtype, np.integer):
        return lax.lt(x, _constant_like(x, 0))
    elif dtypes.issubdtype(dtype, np.bool_):
        return lax.full_like(x, False, dtype=np.bool_)
    elif not dtypes.issubdtype(dtype, np.floating):
        raise ValueError("jax.numpy.signbit is not well defined for %s" %
                         dtype)

    # TPU supports BF16 but not S16 types, so as a workaround, convert BF16 to
    # F32.
    if dtype == dtypes.bfloat16:
        dtype = np.float32
        x = lax.convert_element_type(x, np.float32)

    info = dtypes.finfo(dtype)
    if info.bits not in _INT_DTYPES:
        raise NotImplementedError(
            "jax.numpy.signbit only supports 16, 32, and 64-bit types.")
    int_type = _INT_DTYPES[info.bits]
    x = lax.bitcast_convert_type(x, int_type)
    return lax.convert_element_type(x >> (info.nexp + info.nmant), np.bool_)
Exemple #9
0
 def testNumpyAndXLAAgreeOnFloatEndianness(self, dtype):
   bits_dtype = np.uint32 if jnp.finfo(dtype).bits == 32 else np.uint64
   numpy_bits = np.array(1., dtype).view(bits_dtype)
   xla_bits = api.jit(
       lambda: lax.bitcast_convert_type(np.array(1., dtype), bits_dtype))()
   self.assertEqual(numpy_bits, xla_bits)
Exemple #10
0
 def testBitcastElementType(self, shape, from_dtype, to_dtype, bdims,
                            rng_factory):
     rng = rng_factory(self.rng())
     op = lambda x: lax.bitcast_convert_type(x, to_dtype)
     self._CheckBatching(op, 10, bdims, (shape, ), (from_dtype, ), rng)