def create(self): if isinstance(self.outputs, enc_vec): rel_out = self.outputs[0] else: rel_out = self.outputs temp1 = enc_vec(name=self.name + 'out_bit', nb=len(self.inputs)) self.add( bi_assign(ass_type='and', lhs=[self.inputs[0], self.zero], rhs=temp1[0])) temp2 = enc_vec(name=self.name + 'flag_bit', nb=len(self.inputs)) for idx in range(len(self.inputs)): if idx < len(self.plaintext) and int(self.plaintext[idx]) == 1: self.add( tri_assign(ass_type='mux', lhs=[self.inputs[idx], temp1[idx], self.zero], rhs=temp2[idx])) else: self.add( tri_assign(ass_type='mux', lhs=[self.inputs[idx], self.one, temp1[idx]], rhs=temp2[idx])) if idx < len(self.inputs) - 1: self.add( mono_assign(ass_type='alias', lhs=[temp2[idx]], rhs=temp1[idx + 1])) else: self.add( mono_assign(ass_type='alias', lhs=[temp2[idx]], rhs=rel_out))
def count_arr(self, lvl): inp_len = len(self.inputs) lvl_idx = 1 inp_vec = self.inputs while(lvl_idx <= lvl): num_parts = int(math.ceil(inp_len * 1.0 / lvl_idx)) left_out = [] if num_parts % 2 == 1: left_out = inp_vec[(num_parts - 1) * (lvl_idx):] num_parts -= 1 temp_out = enc_vec(name=self.name + '_red_out_' + str(lvl_idx), nb=num_parts / 2 * (lvl_idx + 1), randomize_name=10) for part_id in range(num_parts / 2): relevant_input = inp_vec[part_id * (2 * lvl_idx): (part_id + 1) * (2 * lvl_idx)] self.sum_part( (relevant_input[0:lvl_idx], relevant_input[lvl_idx:]), temp_out[part_id * (lvl_idx + 1): (part_id + 1) * (lvl_idx + 1)], part_id=part_id) if len(left_out) == 0: inp_vec = temp_out else: inp_vec = temp_out + left_out inp_len = len(inp_vec) lvl_idx += 1 return inp_vec
def create(self): _tmp = enc_vec(nb=len(self.inputs), name=self.name + '_xnor_tmp_') self.add( vec_mono_op_cond(name=self.name, cond=self.weight, ass_types=('not', 'alias'), inputs=self.inputs, outputs=_tmp)) depth = int(math.floor(math.log(self.nb, 2))) _tmp_out = enc_vec(nb=depth + 1, name=self.name + '_mult_out') self.add( reduce_add(name=self.name + '_ra1_', inputs=_tmp, outputs=_tmp_out)) self.add( sign_fn(name=self.name + 'sgn', inputs=_tmp_out, outputs=self.outputs, input_length=len(self.inputs)))
temp_out[part_id * (lvl_idx + 1): (part_id + 1) * (lvl_idx + 1)], part_id=part_id) if len(left_out) == 0: inp_vec = temp_out else: inp_vec = temp_out + left_out inp_len = len(inp_vec) lvl_idx += 1 return inp_vec def sum_part(self, inp_vec, out_vec, part_id, carry=False): ''' Calculates the binary encoding of the number of bits in the input vector. uses out_vec to output the results. ''' self.add(nb_adder_xy(name=out_vec.name + '_adder_' + str(part_id), inputs=inp_vec, outputs=out_vec, nb=len(inp_vec[0]))) if __name__ == '__main__': inp = enc_vec(name='in', nb=4) out = enc_vec(name='out', nb=3) ra = reduce_add('adder', inp, out) print(ra)
cpus = mp.cpu_count() p = mp.Pool(cpus) queue = mp.Manager().Queue() p.map(partial(create_ith_ll, self.name, filter_col, im_col, out), [(queue, x) for x in range(im_col.size[0])]) p.close() p.join() for col_idx in range(im_col.size[0]): self.add(queue.get()) end = time.time() #print("Exiting im2col after " + str(end - beg) + str("sec.")) #print("Added Linear Layers") # print() #print("Entering col2im") beg = time.time() col2im(self, out, H_prime, W_prime, 1, self.outputs) end = time.time() #print("Exiting im2col after " + str(end - beg) + str("sec.")) #print("Exiting col2im") if __name__ == '__main__': weight = np.asarray([0, 1, 1, 1]) inputs = enc_vec(name='nn_input', nb=4) outputs = enc_vec(name='nn_outputs', nb=4) layer = linear_layer_1d(name='linear', weight=weight, inputs=inputs, outputs=outputs) print(layer)