def _(out_y, out_x, out_c): in_x_origin = (out_x * stride_w) - padding_w in_y_origin = (out_y * stride_h) - padding_h iv = [] wv = [] for filter_y in range(weights_h): in_y = in_y_origin + filter_y inside_y = (0 <= in_y) * (in_y < inputs_h) for filter_x in range(weights_w): in_x = in_x_origin + filter_x inside_x = (0 <= in_x) * (in_x < inputs_w) inside = inside_y * inside_x if is_zero(inside): continue for in_c in range(n_channels_in): iv += [ self.X[0][in_y * inside_y][in_x * inside_x][in_c] ] wv += [self.weights[out_c][filter_y][filter_x][in_c]] wv[-1] *= inside if self.fewer_rounds: inputs[out_y][out_x][out_c].assign(iv) weights[out_y][out_x][out_c].assign(wv) else: self.dot_product(iv, wv, out_y, out_x, out_c)
def __xor__(self, other): if util.is_zero(other): return self elif self.is_long_one(other): return ~self else: return self._xor(other)
def __and__(self, other): if util.is_zero(other): return 0 elif self.is_long_one(other): return self else: return self._and(other)
def __add__(self, other): if util.is_zero(other): return self other = self.coerce(other) assert(len(self.v) == len(other.v)) v = sbitint.bit_adder(self.v, other.v) return self.from_vec(v)
def popcnt(self): """ Population count / Hamming weight. :return: :py:obj:`sbitintvec` of required length """ res = sbitint.wallace_tree([[b] for b in self.v]) while util.is_zero(res[-1]): del res[-1] return sbitintvec.get_type(len(res)).from_vec(res)
def __and__(self, other): if util.is_zero(other): return 0 elif util.is_all_ones(other, self.n): return self assert (self.n == other.n) res = self.new(n=self.n) inst.ands(self.n, res, self, other) return res
def __and__(self, other): if util.is_zero(other): return 0 elif util.is_all_ones(other, self.n): return self assert(self.n == other.n) res = self.new(n=self.n) inst.ands(self.n, res, self, other) return res
def __and__(self, other): if util.is_zero(other): return 0 elif util.is_all_ones(other, self.n): return self res = self.new(n=self.n) if not isinstance(other, sbits): other = cbits.get_type(self.n).conv(other) inst.andm(self.n, res, self, other) return res other = self.conv(other) assert(self.n == other.n) inst.ands(self.n, res, self, other) return res
def get_bit_matrix(cls, self_bits, other): n = len(self_bits) assert n == other.n res = [] for i, bit in enumerate(self_bits): if util.is_zero(bit): res.append([0] * (n - i)) else: if cls.vector_mul: x = sbits.get_type(n - i)() inst.andrs(n - i, x, other, bit) res.append(x.bit_decompose(n - i)) else: res.append([(x & bit) for x in other.bit_decompose(n - i)]) return res
def _(j): w_base = self.strides[2] * j pool = [] for ii in range(self.ksize[1]): h = h_base + ii if need_padding[1]: h_in = h < self.X.sizes[1] else: h_in = True for jj in range(self.ksize[2]): w = w_base + jj if need_padding[2]: w_in = w < self.X.sizes[2] else: w_in = True if not is_zero(h_in * w_in): pool.append(h_in * w_in * self.X[bi][h_in * h][w_in * w][k]) self.Y[bi][i][j][k] = util.tree_reduce( lambda a, b: a.max(b), pool)
def _(out_y, out_x, in_c): for m in range(depth_multiplier): oc = m + in_c * depth_multiplier in_x_origin = (out_x * stride_w) - padding_w in_y_origin = (out_y * stride_h) - padding_h iv = [] wv = [] for filter_y in range(weights_h): for filter_x in range(weights_w): in_x = in_x_origin + filter_x in_y = in_y_origin + filter_y inside = (0 <= in_x) * (in_x < inputs_w) * \ (0 <= in_y) * (in_y < inputs_h) if is_zero(inside): continue iv += [self.X[0][in_y][in_x][in_c]] wv += [self.weights[0][filter_y][filter_x][oc]] wv[-1] *= inside if self.fewer_rounds: inputs[out_y][out_x][oc].assign(iv) weights[out_y][out_x][oc].assign(wv) else: self.dot_product(iv, wv, out_y, out_x, oc)
def popcnt(self): res = sbitint.wallace_tree([[b] for b in self.v]) while util.is_zero(res[-1]): del res[-1] return self.from_vec(res)
def __xor__(self, other): if util.is_zero(other): return self return self._new(self.v ^ other.v)
def __radd__(self, other): if util.is_zero(other): return self else: return NotImplemented