Ejemplo n.º 1
0
 def compress(self, tensor, name):
     shape = tensor.size()
     tensor_flatten = tensor.flatten()
     cupy_tensor = cupy.fromDlpack(to_dlpack(tensor_flatten))
     tensor_cast = cupy_tensor.view(cupy.int32)
     sign = tensor_cast & cupy.int32(0b10000000000000000000000000000000)
     exp = tensor_cast & cupy.int32(0b01111111100000000000000000000000)
     mantissa = tensor_cast & cupy.int32(0b00000000011111111111111111111111)
     exp_add_one = mantissa > cupy.random.randint(low=0, high=0b00000000011111111111111111111111,
                                                  size=cupy_tensor.shape,
                                                  dtype=cupy.int32)
     exponent = cupy.where(exp_add_one, exp + 0b00000000100000000000000000000000, exp)
     exp_shift = cupy.clip(exponent, a_min=0b00001001000000000000000000000000, a_max=0b01001000100000000000000000000000)
     exps = cupy.right_shift(exp_shift, 23)
     exps = cupy.bitwise_or(cupy.right_shift(sign, 24), exps - 18)
     tensor_compressed = exps.astype(cupy.uint8)
     return [from_dlpack(tensor_compressed.toDlpack())], shape
Ejemplo n.º 2
0
def int_value_noise_3d(x, y, z, seed):
    x = x.astype(int)
    y = y.astype(int)
    z = z.astype(int)
    c = (__BN_X_NOISE_GEN * x) + (__BN_Y_NOISE_GEN * y) + (
        __BN_Z_NOISE_GEN * z) + __BN_SEED_NOISE_GEN * seed
    c = cp.bitwise_and(c, 0x7fffffff)
    c = cp.bitwise_xor(cp.right_shift(c, 13), c)
    c = cp.bitwise_and((c * (c * c * 60493 + 19990303) + 1376312589),
                       0x7fffffff)
    return c
Ejemplo n.º 3
0
def bitwise_right_shift(x1: Array, x2: Array, /) -> Array:
    """
    Array API compatible wrapper for :py:func:`np.right_shift <numpy.right_shift>`.

    See its docstring for more information.
    """
    if x1.dtype not in _integer_dtypes or x2.dtype not in _integer_dtypes:
        raise TypeError(
            "Only integer dtypes are allowed in bitwise_right_shift")
    # Call result type here just to raise on disallowed type combinations
    _result_type(x1.dtype, x2.dtype)
    x1, x2 = Array._normalize_two_args(x1, x2)
    # Note: bitwise_right_shift is only defined for x2 nonnegative.
    if np.any(x2._array < 0):
        raise ValueError(
            "bitwise_right_shift(x1, x2) is only defined for x2 >= 0")
    return Array._new(np.right_shift(x1._array, x2._array))
Ejemplo n.º 4
0
def gradient_noise_3d(fx, fy, fz, ix, iy, iz, seed):
    vi = (__BN_X_NOISE_GEN * ix) + (__BN_Y_NOISE_GEN * iy) + (
        __BN_Z_NOISE_GEN * iz) + __BN_SEED_NOISE_GEN * seed
    vi = cp.bitwise_and(vi, 0xffffffff)
    vi = cp.bitwise_xor(vi, cp.right_shift(vi, __BN_SHIFT_NOISE_GEN))
    vi = cp.bitwise_and(vi, 0xff)

    vi_l2 = cp.left_shift(vi, 2)

    xvGrad = g_randomVectors[vi_l2]
    yvGrad = g_randomVectors[vi_l2 + 1]
    zvGrad = g_randomVectors[vi_l2 + 2]

    xvPoint = fx - ix
    yvPoint = fy - iy
    zvPoint = fz - iz

    return ((xvGrad * xvPoint) + (yvGrad * yvPoint) +
            (zvGrad * zvPoint)) * 2.12
Ejemplo n.º 5
0
 def __rrshift__(self, other):
     return cupy.right_shift(other, self)