You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/05/12 23:20:06 UTC

[GitHub] [tvm] vinx13 opened a new pull request, #11303: Fix json serialization for NDArray

vinx13 opened a new pull request, #11303:
URL: https://github.com/apache/tvm/pull/11303

   cc @tqchen @areusch 


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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] vinx13 commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
vinx13 commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871901751


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   It will enter this path https://github.com/apache/tvm/blob/main/src/node/serialization.cc#L156 and call the registered `set_repr_bytes` during indexing, so it won't be saved into `b64array` section. The node is later saved via https://github.com/apache/tvm/blob/main/src/node/serialization.cc#L276



##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   It will enter this path https://github.com/apache/tvm/blob/main/src/node/serialization.cc#L156 and call the registered `set_repr_bytes` during indexing, so it won't be saved into `b64array` section. The node is later saved in https://github.com/apache/tvm/blob/main/src/node/serialization.cc#L276



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] vinx13 commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
vinx13 commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871898343


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   I've added a check of `alloc_const.data` against ref data



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] junrushao1994 merged pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
junrushao1994 merged PR #11303:
URL: https://github.com/apache/tvm/pull/11303


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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] MichaelJKlaiber commented on pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
MichaelJKlaiber commented on PR #11303:
URL: https://github.com/apache/tvm/pull/11303#issuecomment-1127408684

   Nice @vinx13 @junrushao1994 . Amazing. Thanks for the quick fix!


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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] areusch commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
areusch commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871900913


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   thanks @vinx13! did we ever figure out which mechanism is used to serialize the ndarray though?



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] areusch commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
areusch commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871895680


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   could you double-check that you're actually exporting an NDArray into JSON here? i think this might fix it , but it would be great if we could prove it through e.g. non-empty b64ndarray key



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] tqchen commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
tqchen commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871896368


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   My read is that it does not go through `b64ndarray` mechanism, but instead goes through `repr_bytes` mechanism as @vinx13 commented. The old  `b64ndarray` mechanism should still continue to work. At some time pt perhaps we can move to  `repr_bytes`  mechanism for all cases while keeping  `b64ndarray`  for one cycle



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] vinx13 commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
vinx13 commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871901751


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   It will enter this path https://github.com/apache/tvm/blob/main/src/node/serialization.cc#L156 https://github.com/apache/tvm/blob/main/src/node/serialization.cc#L276, and call the registered `set_repr_bytes`



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] vinx13 commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
vinx13 commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871901751


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   It will enter this path https://github.com/apache/tvm/blob/main/src/node/serialization.cc#L156, and call the registered `set_repr_bytes`



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] tqchen commented on a diff in pull request #11303: Fix json serialization for NDArray

Posted by GitBox <gi...@apache.org>.
tqchen commented on code in PR #11303:
URL: https://github.com/apache/tvm/pull/11303#discussion_r871896368


##########
tests/python/unittest/test_node_reflection.py:
##########
@@ -160,14 +162,35 @@ def test_dict():
     assert set(dir(x.__class__)) <= set(dir(x))
 
 
+def test_ndarray():
+    dev = tvm.cpu(0)
+    tvm_arr = tvm.nd.array(np.random.rand(4), device=dev)
+    tvm_arr2 = tvm.ir.load_json(tvm.ir.save_json(tvm_arr))
+    tvm.ir.assert_structural_equal(tvm_arr, tvm_arr2)
+    np.testing.assert_array_equal(tvm_arr.numpy(), tvm_arr2.numpy())
+
+
+def test_ndarray_dict():
+    dev = tvm.cpu(0)
+    m1 = {
+        "key1": tvm.nd.array(np.random.rand(4), device=dev),
+        "key2": tvm.nd.array(np.random.rand(4), device=dev),
+    }
+    m2 = tvm.ir.load_json(tvm.ir.save_json(m1))
+    tvm.ir.assert_structural_equal(m1, m2)
+
+
+def test_alloc_const():
+    dev = tvm.cpu(0)
+    dtype = "float32"
+    shape = (16,)
+    buf = tvm.tir.decl_buffer(shape, dtype)
+    data = tvm.nd.array(np.random.rand(*shape).astype(dtype), device=dev)
+    body = tvm.tir.Evaluate(0)
+    stmt = tvm.tir.AllocateConst(buf.data, dtype, shape, data, body)
+    stmt2 = tvm.ir.load_json(tvm.ir.save_json(stmt))

Review Comment:
   My read is that it does not go through `b64ndarray` mechanism, but instead goes through `repr_bytes` mechanism as @vinx13 commented. The old  `b64ndarray` mechanism should still continue to work. At some time pt perhaps we can move to  `repr_bytes`  mechanism for all cases while keeping  `b64ndarray`  for one cycle.
   
   The structural equality in the next line should proves the serialization correctness



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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org