def inject_error(weight, mask0, mask1, num_bits=32): if num_bits == 32: dtype = cp.uint32 ftype = cp.float32 shape = weight.shape weight_flatten = cp.ravel(weight).view(dtype) mask0, mask0_bit = mask0 mask1, mask1_bit = mask1 zero = cp.zeros(1, dtype=dtype) if (mask0.__len__() is not 0) or (mask1.__len__() is not 0): for b in range(num_bits): fault = cp.full(weight_flatten.size, 2**b, dtype=dtype) bit_loc0 = cp.where(mask0_bit == b, mask0, zero).nonzero()[0] bit_loc1 = cp.where(mask1_bit == b, mask1, zero).nonzero()[0] uniform0 = cp.zeros(weight_flatten.size, dtype=dtype) uniform1 = cp.zeros(weight_flatten.size, dtype=dtype) # Inject bit error if bit_loc0.__len__() > 0: cp.put(uniform0, mask0[bit_loc0], fault) cp.put(uniform1, mask1[bit_loc1], fault) # Stuck at 0 not_mask0 = cp.invert(uniform0) weight_flatten = cp.bitwise_and(weight_flatten, not_mask0) # Stuck at 1 weight_flatten = cp.bitwise_or(weight_flatten, uniform1) weight_float = weight_flatten.view(ftype) return cp.reshape(weight_float, shape) else: return weight
def jaccard(im1, im2): im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # cupy im1_cp = cp.asarray(im1) im2_cp = cp.asarray(im2) JI = np.double(cp.sum(cp.bitwise_and(im1_cp, im2_cp))) / np.double(cp.sum(cp.bitwise_or(im1_cp, im2_cp))) return JI
def bitwise_or(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.bitwise_or <numpy.bitwise_or>`. See its docstring for more information. """ if (x1.dtype not in _integer_or_boolean_dtypes or x2.dtype not in _integer_or_boolean_dtypes): raise TypeError( "Only integer or boolean dtypes are allowed in bitwise_or") # 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) return Array._new(np.bitwise_or(x1._array, x2._array))
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
def __ror__(self, other): return cupy.bitwise_or(other, self)
def __or__(self, other): return cupy.bitwise_or(self, other)
def time_char_or(self): np.bitwise_or(self.c, 0, out=self.c) np.bitwise_or(0, self.c, out=self.c)
def time_int_or(self): np.bitwise_or(self.i, 0, out=self.i) np.bitwise_or(0, self.i, out=self.i)