You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/07/30 07:31:07 UTC

[GitHub] [incubator-mxnet] access2rohit commented on a change in pull request #18825: [v1.x][LT] Add forward & backward linalg.gemm test for large size

access2rohit commented on a change in pull request #18825:
URL: https://github.com/apache/incubator-mxnet/pull/18825#discussion_r462740140



##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y-(1-perturbation))

Review comment:
       nit: space around '-'

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y-(1-perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        perturbation = 0.2
+        inp1[0][0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        inp3 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, SMALL_Y))
+        inp1_grad, inp2_grad, inp3_grad, out= run_gemm(inp1, inp2, inp3)

Review comment:
       nit: space before =

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y-(1-perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        perturbation = 0.2
+        inp1[0][0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        inp3 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, SMALL_Y))
+        inp1_grad, inp2_grad, inp3_grad, out= run_gemm(inp1, inp2, inp3)
+        assert_almost_equal(out.asnumpy()[0][0][0], MEDIUM_X+perturbation)
+        assert out.shape == inp3.shape
+        out.backward()
+        assert inp1_grad.shape == (MEDIUM_X, SMALL_Y, MEDIUM_X)
+        assert inp2_grad.shape == (MEDIUM_X, SMALL_Y, MEDIUM_X)
+        assert inp3_grad.shape == (MEDIUM_X, SMALL_Y, SMALL_Y)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0][0], SMALL_Y-(1-perturbation))

Review comment:
       nit: space around '-'

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y-(1-perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        perturbation = 0.2
+        inp1[0][0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        inp3 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, SMALL_Y))
+        inp1_grad, inp2_grad, inp3_grad, out= run_gemm(inp1, inp2, inp3)

Review comment:
       still 
   ```
   out= run_gemm
   ```
   can you do ?
   ```
   out = run_gemm
   ```

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y - (1 - perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))

Review comment:
       spaces around `=` 

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y - (1 - perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        perturbation = 0.2
+        inp1[0][0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))

Review comment:
       spaces around `=` 

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y - (1 - perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        perturbation = 0.2
+        inp1[0][0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        inp3 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, SMALL_Y))

Review comment:
       same 

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y-(1-perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        perturbation = 0.2
+        inp1[0][0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        inp3 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, SMALL_Y))
+        inp1_grad, inp2_grad, inp3_grad, out= run_gemm(inp1, inp2, inp3)

Review comment:
       ```
   out= run_gemm
   ```
   to
   ```
   out = run_gemm
   ```

##########
File path: tests/nightly/test_large_array.py
##########
@@ -1211,23 +1211,49 @@ def check_syrk_batch():
         assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5)
 
     def check_gemm2():
-        def run_gemm2(inp1,inp2):
+        def run_gemm2(inp1, inp2):
             inp1.attach_grad()
             inp2.attach_grad()
             with mx.autograd.record():
-                out = mx.nd.linalg.gemm2(inp1,inp2)
+                out = mx.nd.linalg.gemm2(inp1, inp2)
             return inp1.grad, inp2.grad, out
 
-        inp1=mx.nd.ones(shape=(SMALL_Y, LARGE_X))
-        inp1[0][0]=0.1
-        inp2=mx.nd.ones(shape=(LARGE_X, SMALL_Y))
-        inp1_grad, inp2_grad, out= run_gemm2(inp1,inp2)
+        inp1 = mx.nd.ones(shape=(SMALL_Y, LARGE_X))
+        perturbation = 0.2
+        inp1[0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(LARGE_X, SMALL_Y))
+        inp1_grad, inp2_grad, out = run_gemm2(inp1, inp2)
         assert out.asnumpy()[0][0] == LARGE_X
         assert out.shape == (SMALL_Y, SMALL_Y)
         out.backward()
         assert inp1_grad.shape == (SMALL_Y, LARGE_X)
         assert inp2_grad.shape == (LARGE_X, SMALL_Y)
-        assert_almost_equal(inp2_grad.asnumpy()[0][0],49.1)
+        assert_almost_equal(inp1_grad.asnumpy()[0][0], SMALL_Y)
+        assert_almost_equal(inp2_grad.asnumpy()[0][0], SMALL_Y-(1-perturbation))
+
+    def check_gemm():
+        def run_gemm(inp1,inp2, inp3):
+            inp1.attach_grad()
+            inp2.attach_grad()
+            inp3.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.gemm(inp1, inp2, inp3, transpose_b=True)
+            return inp1.grad, inp2.grad, inp3.grad, out
+
+        inp1 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        perturbation = 0.2
+        inp1[0][0][0] = perturbation
+        inp2 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, MEDIUM_X))
+        inp3 = mx.nd.ones(shape=(MEDIUM_X, SMALL_Y, SMALL_Y))
+        inp1_grad, inp2_grad, inp3_grad, out= run_gemm(inp1, inp2, inp3)

Review comment:
       ```
   out= run_gemm
   ```
   to
   ```
   out = run_gemm
   ```




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