Giter VIP home page Giter VIP logo

Comments (7)

dcslin avatar dcslin commented on July 4, 2024

Hi @joddiy , for tensor mul >2d, please use tensor.tensordot, it depends on which axis to perform the tensordot

    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

from singa.

joddiy avatar joddiy commented on July 4, 2024

Hi, shicong, according to the np's definition, the matmul for high-dim matrix should be:

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).

from singa.

dcslin avatar dcslin commented on July 4, 2024

fixed in #639

from singa.

joddiy avatar joddiy commented on July 4, 2024

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:

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,...

from singa.

joddiy avatar joddiy commented on July 4, 2024

And please check the subgraph for this test case:

image

from singa.

dcslin avatar dcslin commented on July 4, 2024

Hi @joddiy , Thank you for pointing out the issue, kindly help to check the lastest code
PR:#639
your test is added:

def test_matmul_transpose(self):

from singa.

joddiy avatar joddiy commented on July 4, 2024

Hi @dcslin , it works now, thanks for your hlep.

from singa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.