def _create_cc(self, x, gy, hint, e=Engine()): if x.ndim == 2: fmt = m.memory.nc else: fmt = m.memory.nchw x = array(x, fmt, e) gy = array(gy, fmt, e) diff_pd = gy.memory.get_primitive_desc() outputs = CC.reorder_if_must(x, diff_pd, e, self.dag) if len(outputs) == 2: x, self.itm_arr = outputs[:2] else: x = outputs[0] mem_pd = x.memory.get_primitive_desc() cc_d = eltwise_backward.desc(eltwise_relu, diff_pd.desc(), mem_pd.desc(), 0.0, 0.0) cc_pd = eltwise_backward.primitive_desc(cc_d, e, hint) # gx = mdarray(cc_pd.diff_src_primitive_desc()) # print("gx.format=", m.get_fmt(cc_pd.diff_src_primitive_desc())) gx = gy self.dag.push_back(eltwise_backward.eltwise_backward(cc_pd, at(x.memory), at(gy.memory), gx.memory)) self.x = x self.gy = gy self._hint = hint self.outputs = gx,
def _create_cc(self, x, gy, stride, pad, outsize, cover_all, hint, e): super(ConvolutionBackwardWeights, self).__init__() o = gy.shape[1] i = x.shape[1] kh, kw = outsize g = conv.conv_geometry(x.shape, (o, i, kh, kw), stride, pad, cover_all) x_d = m.desc(x.shape, m.memory.f32, m.memory.any) gy_d = m.desc(gy.shape, m.memory.f32, m.memory.any) W_d = m.desc((o, i, kh, kw), m.memory.f32, m.memory.any) b_d = m.desc((o, ), m.memory.f32, m.memory.any) cc_d = conv_backweights.desc(convolution_direct, x_d, W_d, b_d, gy_d, g.geometry[0], g.geometry[1], g.geometry[2], zero) cc_pd = conv_backweights.primitive_desc(cc_d, e, hint) self.x = array(x, m.memory.nchw, e) self.gy = array(gy, m.memory.nchw, e) self._hint = hint gW = conv_bwb_op(cc_pd, self.x, self.gy, self.dag) gb = gW.extra self.outputs = gW, gb
def _create_cc(self, x, W, gy, hint, fwd_W, e=Engine()): # Create primitive descriptor cc_d = create_backward_desc(ip_backdata.desc, x, W, gy) cc_pd = ip_backdata.primitive_desc(cc_d, e, hint) # Transform inputs self.W = fwd_W self.gy = array(gy, m.memory.nc, e) gx = linear_bd_op(cc_pd, self.gy, self.W, self.dag) # # Prepare output mdarray # gx = mdarray(cc_pd.diff_src_primitive_desc()) # dag = self.dag_ # # Reorder if must # gy_m = reorder_if_must(self.gy.memory, cc_pd.diff_dst_primitive_desc(), dag) # W_m = reorder_if_must(self.W.memory, cc_pd.weights_primitive_desc(), dag) # dag.push_back(ip_backdata.inner_product_backward_data(cc_pd, # at(gy_m), at(W_m), gx.memory)) # self.gy_m = gy_m # self.W_m = W_m self._hint = hint self.outputs = gx,
def _create_cc(self, x, W, b, e=Engine()): y_d = m.desc((x.shape[0], W.shape[0]), m.memory.f32, m.memory.any) # Create primitive_desc from any cc_d = create_forward_desc(ip_forward.desc, y_d, x, W, b) cc_pd = ip_forward.primitive_desc(cc_d, e) # Transform inputs self.x = array(x, _x_format(x.ndim), e) w_mpd = cc_pd.weights_primitive_desc() self.usr_w = array(W, _W_format(W.ndim), e) outputs = CC.reorder_if_must(self.usr_w, w_mpd, e, self.dag) if len(outputs) == 2: self.W, self.itm_arr = outputs[:2] else: self.W = outputs[0] if b is not None: self.b = array(b, m.memory.x, e) y = linear_f_op(cc_pd, self.x, self.W, self.b, self.dag) else: y = linear_f_op(cc_pd, self.x, self.W, self.dag) # Prepare output # y = mdarray(cc_pd.dst_primitive_desc()) # dag = self.dag_ # # Reorder if must # x_m = reorder_if_must(self.x.memory, # cc_pd.src_primitive_desc(), dag) # W_m = reorder_if_must(self.W.memory, # cc_pd.weights_primitive_desc(), dag) # if b is None: # dag.push_back(ip_forward.inner_product_forward(cc_pd, # at(x_m), at(W_m), y.memory)) # else: # dag.push_back(ip_forward.inner_product_forward(cc_pd, # at(x_m), at(W_m), at(self.b.memory), y.memory)) # self.x_m = x_m # self.W_m = W_m self._hint = cc_pd self.outputs = y,
def _create_cc(self, x, W, b, gy, hint, e): cc_d = create_backward_desc(ip_backweights.desc, x, W, b, gy) cc_pd = ip_backweights.primitive_desc(cc_d, e, hint) # Transfer inputs to mdarray self.x = array(x, _x_format(x.ndim), e) self.gy = array(gy, m.memory.nc, e) if b is None: gW = linear_bw_op(cc_pd, self.x, self.gy, self.dag) else: gW = linear_bwb_op(cc_pd, self.x, self.gy, self.dag) gb = gW.extra # Prepare outputs mdarray # gW = mdarray(cc_pd.diff_weights_primitive_desc()) # if b is not None: # gb = mdarray(cc_pd.diff_bias_primitive_desc()) # self.has_b = True # else: # self.has_b = False # dag = self.dag_ # # Reorder if must # gy_m = reorder_if_must(self.gy.memory, cc_pd.diff_dst_primitive_desc(), dag) # x_m = reorder_if_must(self.x.memory, cc_pd.src_primitive_desc(), dag) # if b is not None: # dag.push_back(ip_backweights.inner_product_backward_weights(cc_pd, # at(x_m), at(self.gy.memory), gW.memory, gb.memory)) # else: # dag.push_back(ip_backweights.inner_product_backward_weights(cc_pd, # at(x_m), at(self.gy.memory), gW.memory)) # self.x_m = x_m self._hint = hint if b is None: self.outputs = gW, else: self.outputs = gW, gb
def _create_cc(self, x, W, b, stride, pad, cover_all, e): super(ConvolutionForward, self).__init__() g = conv.conv_geometry(x.shape, W.shape, stride, pad, cover_all) y_d = m.desc(g.out_shape, m.memory.f32, m.memory.any) # Create primitive_desc from any cc_d = create_forward_desc(conv_forward.desc, y_d, (x, W, b), g.geometry) cc_pd = conv_forward.primitive_desc(cc_d, e) w_mpd = cc_pd.weights_primitive_desc() self.usr_w = array(W, m.memory.oihw, e) outputs = CC.reorder_if_must(self.usr_w, w_mpd, e, self.dag) if len(outputs) == 2: self.W, self.itm_arr = outputs[:2] else: self.W = outputs[0] # Record weight reorder primitive hint if self.usr_w is not self.W: wro = WeightReorderOptimization() wro.reorder = self.dag.size() - 1 wro.optimized = False self.weight_reorder_opt = wro else: self.weight_reorder_opt = None self.x = array(x, m.memory.nchw, e) if b is not None: self.b = array(b, m.memory.x, e) if b is None: y = conv_f_op(cc_pd, self.x, self.W, self.dag) else: y = conv_f_op(cc_pd, self.x, self.W, self.b, self.dag) self._hint = cc_pd self.outputs = y,
def _create_cc(self, gy, W, stride, pad, outsize, cover_all, hint, e): super(ConvolutionBackwardData, self).__init__() # Get information g = conv.deconv_geometry(gy.shape, W.shape, stride, pad, outsize) gy_d = m.desc(gy.shape, m.memory.f32, m.memory.any) W_d = m.desc(W.shape, m.memory.f32, m.memory.any) x_d = m.desc(g.in_shape, m.memory.f32, m.memory.any) cc_d = conv_backdata.desc(convolution_direct, x_d, W_d, gy_d, g.geometry[0], g.geometry[1], g.geometry[2], zero) cc_pd = conv_backdata.primitive_desc(cc_d, e, hint) self.gy = array(gy, m.memory.nchw, e) self.W = array(W, m.memory.oihw, e) gx = conv_bd_op(cc_pd, self.gy, self.W, self.dag) self._hint = hint self.outputs = gx,
def _create_cc(self, x, e=Engine()): if x.ndim == 2: fmt = m.memory.nc elif x.ndim == 4: fmt = m.memory.nchw x = array(x, fmt, e) mem_pd = x.memory.get_primitive_desc() cc_d = eltwise_forward.desc( forward, eltwise_relu, mem_pd.desc(), 0.0, 0.0) cc_pd = eltwise_forward.primitive_desc(cc_d, e) y = mdarray(cc_pd.dst_primitive_desc()) self.x = x self.dag.push_back(eltwise_forward.eltwise_forward(cc_pd, at(x.memory), y.memory)) self._hint = cc_pd self.outputs = y,