Exemplo n.º 1
0
    def __init__(self, inputs, dropout_ratio, pos=(0, 0), e=Engine()):
        super(DropoutForward, self).__init__()

        self.x = array(inputs[0], _format(inputs[0].ndim), Engine())

        if self.new:
            self._create_cc(inputs[0], dropout_ratio, e)
Exemplo n.º 2
0
    def __init__(self, xs, gy, hint, axis, pos=None, e=Engine()):
        super(ConcatBackward, self).__init__()

        if self.new:
            self._create_cc(xs, gy, hint, axis, e)
        else:
            self._reuse(xs, gy)
Exemplo n.º 3
0
    def __init__(self, inputs, pos=None, e=Engine()):
        super(AddForward, self).__init__()

        if self.new:
            self._create_cc(inputs, e)
        else:
            self._reuse(inputs)
Exemplo n.º 4
0
    def _create_cc(self, x, dropout_ratio, e=Engine()):
        self.dropout_op = dropout_f32(dropout_ratio)

        self.mask = np.ndarray(shape=x.shape, dtype=np.float32)
        self._mask = array(self.mask, _format(self.mask.ndim), e)

        self._hint = mdarray(self.x.memory.get_primitive_desc())
Exemplo n.º 5
0
    def cpu_cosim_dump_inner(self, in_data, out_grad=None):
        cd = None
        if out_grad is None:
            cd = cdump.cosim_dump(cdump.cdump_op_lrn_forward)
        else:
            cd = cdump.cosim_dump(cdump.cdump_op_lrn_backward)

        x = array(in_data[0], m.memory.nchw, Engine())
        cd.dump_memory(cdump.cdump_src_memory, x.memory)

        if out_grad is not None:
            gy = array(out_grad[0], m.memory.nchw, Engine())
            cd.dump_memory(cdump.cdump_diff_dst_memory, gy.memory)

        cd.dump_int_parms(cdump.cdump_lrn_local_size, 1, self.n)
        cd.dump_double_parms(cdump.cdump_lrn_doulbe_parms, 3, self.k,
                             self.alpha, self.beta)
Exemplo n.º 6
0
    def __init__(self, dropout_op, mask, gy, hint, pos=(0, 0), e=Engine()):
        super(DropoutBackward, self).__init__()
        self._dropout_op = dropout_op
        self._mask = mask
        self.gy = array(gy[0], _format(gy[0].ndim), e)

        if self.new:
            self._create_cc(hint)
Exemplo n.º 7
0
    def cpu_cosim_dump_inner(self, in_data, out_grad=None):
        cd = None
        if out_grad is None:
            cd = cdump.cosim_dump(cdump.cdump_op_max_pooling_forward)
        else:
            cd = cdump.cosim_dump(cdump.cdump_op_max_pooling_backward)

        x = array(in_data[0], m.memory.nchw, Engine())
        cd.dump_memory(cdump.cdump_src_memory, x.memory)

        if out_grad is not None:
            gy = array(out_grad[0], m.memory.nchw, Engine())
            cd.dump_memory(cdump.cdump_diff_dst_memory, gy.memory)

        cd.dump_int_parms(cdump.cdump_max_pooling_int_parms, 8,
                          self.kh, self.kw, self.sy, self.sx, self.ph, self.pw,
                          pooling_max, 1 if self.cover_all else 0)
Exemplo n.º 8
0
    def __init__(self,
                 inputs,
                 eps=2e-5,
                 mean=None,
                 var=None,
                 pos=None,
                 e=Engine()):
        super(BnForward, self).__init__()

        if self.new:
            self._create_cc(inputs, eps, mean, var, e)
        else:
            self._reuse(inputs, mean, var)
Exemplo n.º 9
0
    def __init__(self,
                 inputs,
                 n=5,
                 k=2,
                 alpha=1e-4,
                 beta=.75,
                 pos=None,
                 e=Engine()):
        super(LrnForward, self).__init__()

        x = inputs[0]
        if self.new:
            self._create_cc(x, n, k, alpha, beta, e)
        else:
            self._reuse(x)
Exemplo n.º 10
0
    def __init__(self,
                 inputs,
                 fwd_x,
                 gy,
                 hint,
                 flags,
                 eps,
                 mean,
                 var,
                 pos=None,
                 e=Engine()):
        super(BnBackward, self).__init__()

        if self.new:
            self._create_cc(inputs, fwd_x, gy, hint, flags, eps, mean, var, e)
        else:
            self._reuse(inputs, gy, mean, var)
Exemplo n.º 11
0
 def __init__(self,
              inputs,
              alg_kind,
              ksize,
              stride=None,
              pad=0,
              cover_all=True,
              pos=None,
              e=Engine()):
     super(Pooling2DForward, self).__init__()
     self.alg_kind = alg_kind
     # super(Pooling2DForward, self).__init__()
     x = inputs[0]
     if self.new:
         self._create_cc(x, ksize, stride, pad, cover_all, e)
     else:
         self._reuse(x)
Exemplo n.º 12
0
def mkl_sum(xs, func=None):
    e = Engine()

    xarrays = ()  # prevent the obj from gc
    xs_arrays = ()  # prevent the obj from gc
    itm_arr = None  # prvent the obj from gc
    xs_mpdl = m.mpd_list()
    xs_pl = ()
    scales = m.vectord()
    pl = primitive_list()
    for i in range(len(xs)):
        xarray = array(xs[i], _x_format(xs[i].ndim), e)
        xmpd = xarray.memory.get_primitive_desc()
        if i == 0:
            xmpd_best = xmpd
        else:
            if m.get_fmt(xmpd) > m.get_fmt(xmpd_best):
                xmpd_best = xmpd
        xs_arrays += (xarray,)
    for x in xs_arrays:
        outputs = reorder_if_must(x, xmpd_best, e, pl)
        if len(outputs) == 2:
            xarray, itm_arr = outputs[:2]
        else:
            xarray = outputs[0]
        xarrays += (xarray,)
        scales.push_back(1.0)
        xs_mpdl.push_back(xarray.memory.get_primitive_desc())
        xs_pl += (at(xarray.memory), )

    cc_pd = sum.primitive_desc(scales, xs_mpdl)
    if func is not None and hasattr(func, 'hint'):  # this is only used for grad accumulate currently
        cc = ComputeComplex.get_bd_cc(func.hint, pos=(func.rank, func.fanout))
        if cc is not None:
            y = cc.gy
        else:
            y = mdarray(cc_pd.dst_primitive_desc())
    else:
        y = mdarray(cc_pd.dst_primitive_desc())
    pl.push_back(sum.sum(cc_pd, xs_pl, y.memory))
    s = Stream()
    s.submit(pl)
    s.wait()

    return y
Exemplo n.º 13
0
    def __init__(self, src, dst_shape, dst_dtype, dst_format):
        self.src = src

        self.fwd_dag = primitive_list()
        self.bwd_dag = primitive_list()

        self.src_mpd = src.memory.get_primitive_desc()
        dst_dtype = m.memory.f32 if dst_dtype is numpy.float32 or \
            dst_dtype.kind is 'f' else m.memory.s32
        self.expected_mpd = m.primitive_desc(
            m.desc(dst_shape, dst_dtype, dst_format), Engine())

        if self.src_mpd != self.expected_mpd:
            self.dst = mdarray(self.expected_mpd)
            self.fwd_dag.push_back(
                r.reorder(at(self.src.memory), self.dst.memory))
            self.bwd_dag.push_back(
                r.reorder(at(self.dst.memory), self.src.memory))
        else:
            self.dst = self.src