yzhliu commented on a change in pull request #15855: [Numpy] cumprod
URL: https://github.com/apache/incubator-mxnet/pull/15855#discussion_r313238460
 
 

 ##########
 File path: contrib/tvmop/basic/ufunc.py
 ##########
 @@ -48,3 +51,186 @@ def vadd_gpu(dtype, ndim):
     s[C].bind(bx, tvm.thread_axis("blockIdx.x"))
     s[C].bind(tx, tvm.thread_axis("threadIdx.x"))
     return s, [A, B, C]
+
+
+def cumprod(X, ishape, dtype, ndim, axis):
+    def swapaxis(idx, axis1, axis2):
+        ret = list(idx)
+        if axis1 != axis2:
+            ret[axis1], ret[axis2] = ret[axis2], ret[axis1]
+        return ret
+
+    sshape = swapaxis(ishape, 0, axis)
+    s_state = tvm.placeholder(sshape, dtype=dtype)
+    s_init = tvm.compute([1] + sshape[1:], lambda *idx: X[tuple(swapaxis(idx, 
0, axis))])
+    s_update = tvm.compute(sshape, lambda *idx: s_state[(idx[0] - 1, ) + 
idx[1:]] * X[tuple(swapaxis(idx, 0, axis))])
+    s_scan = tvm.scan(s_init, s_update, s_state)
+    ret = tvm.compute(ishape, lambda *idx: s_scan[tuple(swapaxis(idx, 0, 
axis))])
+    return ret, [ret], [s_init, s_update]
+
+
+def compute_cumprod(dtype, ndim, axis):
+    ishape = [tvm.var() for _ in range(ndim)]
+    X = tvm.placeholder(ishape, name='X', dtype=dtype)
+    if ndim == 0:
+        tshape = [tvm.var()]
+        ret = tvm.compute(tshape, lambda *idx:X[()])
+        c_list, s_list = [ret], []
+    elif axis is None:
+        tshape = [tvm.var()]
+        Y = topi.reshape(X, tshape)
+        ret, c_list, s_list = cumprod(Y, tshape, dtype, 1, 0)
+        c_list.append(Y)
+    else:
+        ret, c_list, s_list = cumprod(X, ishape, dtype, ndim, axis)
+    s = tvm.create_schedule(ret.op)
+    return s, X, ret, c_list, s_list
+
+
+@defop(name="vcumprod", target="cpu", auto_broadcast=False,
+       dtype=AllTypesButHalf, ndim=list(range(0, 6)), axis=list(range(0, 5)) + 
[None],
+       attrs=["axis"], attrs_valid=lambda **kwargs: kwargs['axis'] is None or 
kwargs['axis'] < kwargs['ndim'])
+def vcumprod(dtype, ndim, axis):
 
 Review comment:
   should be just `cumprod`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to