You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@singa.apache.org by GitBox <gi...@apache.org> on 2020/03/24 05:49:51 UTC

[GitHub] [singa] joddiy opened a new issue #634: cannot do matmul for high-dim tensors

joddiy opened a new issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634
 
 
   Hi, @dcslin , we ccannot do matmul for high-dim tensors:
   
   The error is:
   > F0324 05:47:19.174587 15611 tensor.cc:1413] Check failed: A.shape().size() == 2u (4 vs. 2)
   
   please use this test case:
   ```python3
   x1 = np.random.randn(1, 12, 256, 64).astype(np.float32)
   x2 = np.random.randn(1, 12, 64, 256).astype(np.float32)
   x1 = tensor.from_numpy(x1)
   x1.to_device(gpu_dev)
   x2 = tensor.from_numpy(x2)
   x2.to_device(gpu_dev)
   
   y = autograd.Matmul()(x1, x2)
   print(tensor.to_numpy(y[0]))
   ```

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] dcslin commented on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
dcslin commented on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-604209813
 
 
   fixed in https://github.com/apache/singa/pull/639
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] joddiy commented on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
joddiy commented on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-603638731
 
 
   Hi, shicong, according to the np's definition, the matmul for high-dim matrix should be:
   
   ```python3
   x1 = np.random.randn(1, 12, 256, 64).astype(np.float32)
   x2 = np.random.randn(1, 12, 64, 256).astype(np.float32)
   print(np.matmul(x1, x2).shape)
   ```
   
   Output:
   > (1, 12, 256, 256)
   
   Actually, it does the matmul for (256, 64) * (64, 256) for all cells of the first axes (1, 12).

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] dcslin commented on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
dcslin commented on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-605449549
 
 
   Hi @joddiy , Thank you for pointing out the issue, kindly help to check the lastest code 
   PR:https://github.com/apache/singa/pull/639
   your test is added: https://github.com/apache/singa/blob/fc1de1d699969e063c47491ded14ef034ec30a0d/test/python/test_tensor.py#L409

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] joddiy closed issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
joddiy closed issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634
 
 
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] joddiy commented on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
joddiy commented on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-605074280
 
 
   Hi, @dcslin , thanks for your help, this PR works fine for basic test cases. However, for some special cases in onnx, it has an incorrect result, I've extracted this test case for you, please use this:
   
   ```python3
   for dev in [cpu_dev, gpu_dev]:
   
         X = np.random.random((1, 256, 12, 64)).astype(np.float32)
         x = tensor.from_numpy(X)
         x.to_device(dev)
   
         W = np.random.random((1, 256, 12, 64)).astype(np.float32)
         w = tensor.from_numpy(W)
         w.to_device(dev)
   
         X = np.transpose(X, (0, 2, 1, 3))
         W = np.transpose(W, (0, 2, 1, 3))
         W = np.transpose(W, (0, 1, 3, 2))
         Y = np.matmul(X, W)
   
         x = autograd.transpose(x, (0, 2, 1, 3))
         w = autograd.transpose(w, (0, 2, 1, 3))
         w = autograd.transpose(w, (0, 1, 3, 2))
         y = autograd.matmul(x, w)
   
         np.testing.assert_array_almost_equal(tensor.to_numpy(x), X)
         np.testing.assert_array_almost_equal(tensor.to_numpy(w), W)
         np.testing.assert_array_almost_equal(tensor.to_numpy(y), Y)
   ```
   
   This test case reports:
   > Traceback (most recent call last):
     File "../../test/python/test_tensor.py", line 389, in test_matmul
       np.testing.assert_array_almost_equal(tensor.to_numpy(y), Y)
     File "/usr/local/lib/python3.5/dist-packages/numpy/testing/_private/utils.py", line 1015, in assert_array_almost_equal
       precision=decimal)
     File "/usr/local/lib/python3.5/dist-packages/numpy/testing/_private/utils.py", line 827, in assert_array_compare
       raise AssertionError(msg)
   AssertionError:
   Arrays are not almost equal to 6 decimals
   
   > Mismatch: 100%
   Max absolute difference: 11.377065
   Max relative difference: 1.148434
    x: array([[[[14.145736, 13.506734, 13.252323, ..., 14.569139, 15.795746,
             15.196916],
            [18.76216 , 17.117498, 14.830437, ..., 17.131226, 17.974703,...
    y: array([[[[16.710552, 17.515999, 15.49446 , ..., 15.438944, 18.269606,
             15.611665],
            [14.75556 , 14.552402, 14.04308 , ..., 14.70639 , 15.604133,...

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] joddiy commented on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
joddiy commented on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-605463331
 
 
   Hi @dcslin , it works now, thanks for your hlep.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] dcslin commented on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
dcslin commented on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-603190533
 
 
   Hi @joddiy , for tensor mul >2d, please use `tensor.tensordot`, it depends on which axis to perform the `tensordot`
   ```python
       def test_4d_tensor_dot(self):
           for dev in [cpu_dev, gpu_dev]:
               x1 = np.random.randn(1, 12, 256, 64).astype(np.float32)
               x2 = np.random.randn(1, 12, 64, 256).astype(np.float32)
               x1 = tensor.from_numpy(x1)
               x1.to_device(dev)
               x2 = tensor.from_numpy(x2)
               x2.to_device(dev)
   
               y = tensor.tensordot(x1, x2, axes=([3],[2]))
               print(y.shape)
   
               y = tensor.tensordot(x1, x2, axes=([2,3],[3,2]))
               print(y.shape)
   
   ```
   output:
   ```
   (1, 12, 256, 1, 12, 256)
   (1, 12, 1, 12)
   (1, 12, 256, 1, 12, 256)
   (1, 12, 1, 12)
   ...........................
   ----------------------------------------------------------------------
   Ran 27 tests in 0.121s
   
   OK
   ```
   
   reference: https://docs.scipy.org/doc/numpy/reference/generated/numpy.tensordot.html

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] joddiy commented on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
joddiy commented on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-605075888
 
 
   And please check the subgraph for this test case:
   
   ![image](https://user-images.githubusercontent.com/14108933/77774443-09c42700-7086-11ea-8c27-2ccbd2085851.png)
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [singa] joddiy edited a comment on issue #634: cannot do matmul for high-dim tensors

Posted by GitBox <gi...@apache.org>.
joddiy edited a comment on issue #634: cannot do matmul for high-dim tensors
URL: https://github.com/apache/singa/issues/634#issuecomment-603638731
 
 
   Hi, shicong, according to the np's definition, the matmul for high-dim matrix should be:
   
   ```python3
   x1 = np.random.randn(1, 12, 256, 64).astype(np.float32)
   x2 = np.random.randn(1, 12, 64, 256).astype(np.float32)
   print(np.matmul(x1, x2).shape)
   ```
   
   Output:
   > (1, 12, 256, 256)
   
   Actually, it does the matmul for (256, 64) * (64, 256) for all cells of the first two axes (1, 12).

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services