def generate_c(self, link_idx, inp_shape): name = self.cname + str(link_idx) text = [] # BinaryLinear bl l = self.bl lName = l.name lname=name+'_'+lName for p in l.params(): pname=p.name if pname == 'W': text += [bu.np_to_uint8C(bu.binarize_real(p.data.T), lname+'_'+pname, 'col_major', pad='1')] num_classes = p.data.shape[0] fc_size = p.data.shape[1] elif pname == 'b': text += [bu.np_to_floatC(p.data, lname+'_'+pname, 'row_major')] text = "\n".join(text) m = 1 n = fc_size k = num_classes ftext = "void {name}(uint8_t* input, uint8_t* output){{\n" ftext += " blinear_layer(input, {name}_bl_W, output, {name}_bl_b, {m}, {n}, {k}); \n}}\n\n" ftext = ftext.format(name=name, m=m, n=n, k=k) text += ftext return text
def generate_c(self, link_idx, inp_shape): name = self.cname + str(link_idx) text = [] # BinaryLinear bl l = self.bl lName = l.name lname=name+'_'+lName for p in l.params(): pname=p.name if pname == 'W': text += [bu.np_to_uint8C(bu.binarize_real(p.data.T), lname+'_'+pname, 'col_major', pad='1')] num_classes = p.data.shape[0] fc_size = p.data.shape[1] elif pname == 'b': text += [bu.np_to_floatC(p.data, lname+'_'+pname, 'row_major')] # BatchNormalization bn l = self.bn lName = l.name lname=name+'_'+lName for p in l.params(): pname=p.name if pname == 'gamma': text += [bu.np_to_floatC(p.data, lname+'_'+pname, 'row_major')] elif pname == 'beta': text += [bu.np_to_floatC(p.data, lname+'_'+pname, 'row_major')] for p in l._persistent: pname=p persistent = l.__dict__[p] if pname == 'avg_mean': text += [bu.np_to_floatC(persistent, lname+'_mean', 'row_major')] elif pname == 'avg_var': text += [bu.np_to_floatC(np.sqrt(persistent, dtype=persistent.dtype), lname+'_std', 'row_major')] text = "\n".join(text)+'\n' m = 1 n = fc_size k = num_classes ftext = "void {name}(uint8_t* input, uint8_t* output){{\n" ftext += " blinear_layer(input, {name}_bl_W, output, {name}_bl_b, {name}_bn_gamma, {name}_bn_beta, {name}_bn_mean, {name}_bn_std, {m}, {n}, {k}); \n}}\n\n" ftext = ftext.format(name=name, m=m, n=n, k=k) text += ftext return text
def generate_c(self, link_idx, inp_shape): #if not hasattr(self,'inp_shape'): # raise Exception("no input shape found") # return "" w, h = inp_shape[2:4] name = self.cname + str(link_idx) text = [] m = 1 sw, sh = self.bconv.stride pw, ph = self.bconv.pad pl_w, pl_h = 1, 1 pl_sw, pl_sh = 1, 1 pl_pw, pl_ph = 0, 0 # Bconv l = self.bconv lname = name + '_' + l.name for p in l.params(): pname = p.name if pname == 'W': num_f, n, kw, kh = p.data.shape bin_data = bu.binarize_real(p.data).reshape( p.data.shape[0] * p.data.shape[1], -1) text += [ bu.np_to_uint8C(bin_data, lname + '_' + pname, 'row_major', pad='1') ] elif pname == 'b': text += [ bu.np_to_floatC(p.data, lname + '_' + pname, 'row_major') ] # BatchNormalization bn l = self.bn lName = l.name lname = name + '_' + lName for p in l.params(): pname = p.name if pname == 'gamma': text += [ bu.np_to_floatC(p.data, lname + '_' + pname, 'row_major') ] elif pname == 'beta': text += [ bu.np_to_floatC(p.data, lname + '_' + pname, 'row_major') ] for p in l._persistent: pname = p persistent = l.__dict__[p] if pname == 'avg_mean': text += [ bu.np_to_floatC(persistent, lname + '_mean', 'row_major') ] elif pname == 'avg_var': text += [ bu.np_to_floatC( np.sqrt(persistent, dtype=persistent.dtype), lname + '_std', 'row_major') ] text = "\n".join(text) + '\n' ftext = "void {name}(uint8_t* input, uint8_t* output){{\n" ftext += " bconv_layer(input, {name}_bconv_W, output, {name}_bconv_b, {name}_bn_gamma, {name}_bn_beta, {name}_bn_mean, {name}_bn_std, {m}, {num_f}, {w}, {h}, {n}, {kw}, {kh}, {sw}, {sh}, {pw}, {ph}, {pl_w}, {pl_h}, {pl_sw}, {pl_sh}, {pl_pw}, {pl_ph});\n}}\n\n" ftext = ftext.format(name=name, m=m, n=n, w=w, h=h, num_f=num_f, kw=kw, kh=kh, sw=sw, sh=sh, pw=pw, ph=ph, pl_w=pl_w, pl_h=pl_h, pl_sw=pl_sw, pl_sh=pl_sh, pl_pw=pl_pw, pl_ph=pl_ph) text += ftext return text