def test_triangle_flip(): # Depending on the skewness, the algorithm flips the histogram. # We check that the flip doesn't affect too much the result. img = camerad inv_img = cp.invert(img) t = threshold_triangle(inv_img) t_inv_img = inv_img > t t_inv_inv_img = cp.invert(t_inv_img) t = threshold_triangle(img) t_img = img > t # Check that most of the pixels are identical # See numpy #7685 for a future cp.testing API unequal_pos = cp.where(t_img.ravel() != t_inv_inv_img.ravel()) assert len(unequal_pos[0]) / t_img.size < 1e-2
def select_muons_opposite_sign(muons, in_mask): out_mask = cupy.invert(muons.make_mask()) select_opposite_sign_muons_cudakernel[32, 1024](muons.charge, muons.offsets, in_mask, out_mask) cuda.synchronize() return out_mask
def test_triangle_float_images(): text = cp.array(data.text()) int_bins = int(text.max() - text.min() + 1) # Set nbins to match the uint case and threshold as float. assert round(float(threshold_triangle( text.astype(float), nbins=int_bins))) == 104 # Check that rescaling image to floats in unit interval is equivalent. assert ( round(float(threshold_triangle(text / 255.0, nbins=int_bins) * 255)) == 104 ) # Repeat for inverted image. assert round(float(threshold_triangle( cp.invert(text).astype(float), nbins=int_bins))) == 151 assert round(float(threshold_triangle( cp.invert(text) / 255, nbins=int_bins) * 255)) == 151
def transform(self, X): """[summary]. Args: X (cupy.ndarray): [description]. Returns: cupy.ndarray: [description]. """ check_is_fitted(self, "class_means_") # TODO(smly): # X = column_or_1d(X, warn=True) # Label encoding if necessary if self._label_encoding_uniques is not None: X = self._label_encoding_uniques.get_indexer(X.to_pandas()) X = cupy.asarray(X) missing_mask = cupy.isnan(X) encode_mask = cupy.invert(missing_mask) unseen_mask = cupy.bitwise_xor( cupy.isin(X, self.classes_, invert=True), missing_mask) X = X.copy() X[unseen_mask] = cupy.max(self.classes_) indices = _get_index_cupy(self.classes_, X[encode_mask]) _classes_index_list = cupy.searchsorted(self.lut_[:, 0], self.classes_) encoded_values = cupy.zeros(X.shape[0], dtype=cupy.float32) encoded_values[encode_mask] = cupy.take( self.lut_[:, 1], cupy.take(_classes_index_list, indices)) encoded_values[unseen_mask] = self.default_unseen_ return encoded_values
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 bitwise_invert(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.invert <numpy.invert>`. See its docstring for more information. """ if x.dtype not in _integer_or_boolean_dtypes: raise TypeError( "Only integer or boolean dtypes are allowed in bitwise_invert") return Array._new(np.invert(x._array))
def mask_deltar_first(objs1, mask1, objs2, mask2, drcut): assert (mask1.shape == objs1.eta.shape) assert (mask2.shape == objs2.eta.shape) assert (objs1.offsets.shape == objs2.offsets.shape) mask_out = cupy.zeros_like(objs1.eta, dtype=cupy.bool) mask_deltar_first_cudakernel[32, 1024](objs1.eta, objs1.phi, mask1, objs1.offsets, objs2.eta, objs2.phi, mask2, objs2.offsets, drcut**2, mask_out) cuda.synchronize() mask_out = cupy.invert(mask_out) return mask_out
def mask_overlappingAK4(objs1, mask1, objs2, mask2, drcut, tau32cut, tau21cut): assert (mask1.shape == objs1.eta.shape) assert (mask2.shape == objs2.eta.shape) assert (objs1.offsets.shape == objs2.offsets.shape) mask_out = cupy.zeros_like(objs1.eta, dtype=cupy.bool) mask_overlappingAK4_cudakernel[32, 1024](objs1.eta, objs1.phi, mask1, objs1.offsets, objs2.eta, objs2.phi, mask2, objs2.offsets, objs2.tau32, objs2.tau21, drcut**2, tau32cut, tau21cut, mask_out) cuda.synchronize() mask_out = cupy.invert(mask_out) return mask_out
def solve(iarr, barr, periodic, prec, epoch, nepochs, stencil=stencil): iarr_pad = cupy.pad(cupy.array(iarr), 1) barr_pad = cupy.pad(cupy.array(barr), 1) bi_pad = cupy.pad(cupy.array(iarr * barr), 1) mutable_pad = cupy.pad(cupy.invert(cupy.array(barr)), 1) tmp_pad = cupy.zeros_like(iarr_pad) err = cupy.zeros_like(iarr) # Get indices of fixed boundary values and values themselves core = arrays.core_slices1(iarr_pad) prev = None for iepoch in range(nepochs): print(f'epoch: {iepoch}/{nepochs} x {epoch}') for istep in range(epoch): #print(f'step: {istep}/{epoch}') if epoch - istep == 1: # last in the epoch prev = cupy.array(iarr_pad) stencil(iarr_pad, tmp_pad) iarr_pad = bi_pad + mutable_pad * tmp_pad edge_condition(iarr_pad, *periodic) if epoch - istep == 1: # last in the epoch err = iarr_pad - prev maxerr = cupy.max(cupy.abs(err)) #print(f'maxerr: {maxerr}') if prec and maxerr < prec: print(f'fdm reach max precision: {prec} > {maxerr}') return (iarr_pad[core], err[core]) print( f'fdm reach max epoch {epoch} x {nepochs}, last prec {prec} < {maxerr}' ) res = (iarr_pad[core], err[core]) return tuple([r.get() for r in res])
def mask_deltar_first(objs1, mask1, objs2, mask2, drcut): assert mask1.shape == objs1["eta"].shape assert mask2.shape == objs2["eta"].shape assert mask1.shape == objs1["phi"].shape assert mask2.shape == objs2["phi"].shape assert objs1["offsets"].shape == objs2["offsets"].shape mask_out = cupy.zeros_like(objs1["eta"], dtype=cupy.bool) mask_deltar_first_cudakernel[32, 1024]( objs1["eta"], objs1["phi"], mask1, objs1["offsets"], objs2["eta"], objs2["phi"], mask2, objs2["offsets"], drcut**2, mask_out, ) cuda.synchronize() mask_out = cupy.invert(mask_out) return mask_out
s = time.time() Wb = _preprocess()(Wb, Wb.shape[0], cupy.zeros((Wb.shape[0], Wb.shape[1]//32)).astype("int32"), size=Wb.shape[1] ) preprocess_time += time.time()-s s = time.time() xb = _preprocess_vec()(xb, cupy.zeros((xb.shape[0]//32)).astype("int32"), size=xb.shape[0] ) preprocess_vec_time += time.time()-s s = time.time() yb = cupy.invert(cupy.bitwise_xor(Wb, xb)) xnor_time += time.time()-s s = time.time() yb = _popcount()(yb, axis=1) popcount_time += time.time()-s print "binarize_time: {0}".format(binarize_time) print "preprocess_time: {0}".format(preprocess_time) print "preprocess_vec_time: {0}".format(preprocess_vec_time) print "xnor_time: {0}".format(xnor_time) print "popcount_time: {0}".format(popcount_time) binarize_log.append(binarize_time) preprocess_log.append(preprocess_time) preprocess_vec_log.append(preprocess_vec_time)
def __invert__(self): return cupy.invert(self)
def solve(iarr, barr, periodic, prec, epoch, nepochs, stencil=stencil): ''' Solve boundary value problem Return (arr, err) - iarr gives array of initial values - barr gives bool array where True indicates value at that index is boundary (imutable). - periodic is list of Boolean. If true, the corresponding dimension is periodic, else it is fixed. - epoch is number of iteration per precision check - nepochs limits the number of epochs Returned arrays "arr" is like iarr with updated solution including fixed boundary value elements. "err" is difference between last and penultimate iteration. ''' iarr = cupy.array(iarr) barr = cupy.array(barr) bi_core = cupy.array(iarr * barr) mutable_core = cupy.invert(barr) tmp_core = cupy.zeros(iarr.shape) err = cupy.zeros_like(iarr) barr_pad = cupy.pad(barr, 1) iarr_pad = cupy.pad(iarr, 1) # Get indices of fixed boundary values and values themselves ifixed = barr_pad == True fixed = iarr_pad[ifixed] core = arrays.core_slices1(iarr_pad) prev = None for iepoch in range(nepochs): print(f'epoch: {iepoch}/{nepochs} x {epoch}') for istep in range(epoch): #print(f'step: {istep}/{epoch}') if epoch - istep == 1: # last in the epoch prev = cupy.array(iarr_pad[core]) stencil(iarr_pad, tmp_core) iarr_pad[core] = bi_core + mutable_core * tmp_core edge_condition(iarr_pad, *periodic) if epoch - istep == 1: # last in the epoch err = iarr_pad[core] - prev maxerr = cupy.max(cupy.abs(err)) #print(f'maxerr: {maxerr}') if prec and maxerr < prec: print(f'fdm reach max precision: {prec} > {maxerr}') return (iarr_pad[core], err) print( f'fdm reach max epoch {epoch} x {nepochs}, last prec {prec} < {maxerr}' ) res = (iarr_pad[core], err) return tuple([r.get() for r in res])
def test_triangle_uint_images(): text = cp.array(data.text()) assert threshold_triangle(cp.invert(text)) == 151 assert threshold_triangle(text) == 104 assert threshold_triangle(coinsd) == 80 assert threshold_triangle(cp.invert(coinsd)) == 175
s = time.time() Wb = _preprocess()(Wb, Wb.shape[0], cupy.zeros((Wb.shape[0], Wb.shape[1] // 32)).astype("int32"), size=Wb.shape[1]) preprocess_time += time.time() - s s = time.time() xb = _preprocess_vec()(xb, cupy.zeros( (xb.shape[0] // 32)).astype("int32"), size=xb.shape[0]) preprocess_vec_time += time.time() - s s = time.time() yb = cupy.invert(cupy.bitwise_xor(Wb, xb)) xnor_time += time.time() - s s = time.time() yb = _popcount()(yb, axis=1) popcount_time += time.time() - s print "binarize_time: {0}".format(binarize_time) print "preprocess_time: {0}".format(preprocess_time) print "preprocess_vec_time: {0}".format(preprocess_vec_time) print "xnor_time: {0}".format(xnor_time) print "popcount_time: {0}".format(popcount_time) binarize_log.append(binarize_time) preprocess_log.append(preprocess_time) preprocess_vec_log.append(preprocess_vec_time)