From be6aaa9f60bae83b335e9cf4a33df44448856cb5 Mon Sep 17 00:00:00 2001 From: zhichengxiong Date: Thu, 4 Aug 2022 17:32:40 +0800 Subject: [PATCH 1/5] [autodiff] Print more specific error message that autodiff does not surpport to_numpy --- taichi/transforms/auto_diff.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/taichi/transforms/auto_diff.cpp b/taichi/transforms/auto_diff.cpp index af42eb689298b..b083e16161022 100644 --- a/taichi/transforms/auto_diff.cpp +++ b/taichi/transforms/auto_diff.cpp @@ -1008,6 +1008,7 @@ class MakeAdjoint : public ADTransform { void visit(GlobalStoreStmt *stmt) override { // erase and replace with global load adjoint + TI_ASSERT(!stmt->dest->is()); GlobalPtrStmt *dest = stmt->dest->as(); TI_ASSERT(dest->width() == 1); auto snodes = dest->snodes; From 61abcb3669ac33097745b2b3262fb1c9ffb17dbe Mon Sep 17 00:00:00 2001 From: zhichengxiong Date: Fri, 5 Aug 2022 15:35:44 +0800 Subject: [PATCH 2/5] [autodiff] Print not supprt error for to_numpy --- taichi/transforms/auto_diff.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/taichi/transforms/auto_diff.cpp b/taichi/transforms/auto_diff.cpp index b083e16161022..1d3333d2727d7 100644 --- a/taichi/transforms/auto_diff.cpp +++ b/taichi/transforms/auto_diff.cpp @@ -1008,7 +1008,9 @@ class MakeAdjoint : public ADTransform { void visit(GlobalStoreStmt *stmt) override { // erase and replace with global load adjoint - TI_ASSERT(!stmt->dest->is()); + if (stmt->dest->is()) { + TI_NOT_IMPLEMENTED; + } GlobalPtrStmt *dest = stmt->dest->as(); TI_ASSERT(dest->width() == 1); auto snodes = dest->snodes; From 016ac6c763551b756c1ea4f874aa2f6ba96037c5 Mon Sep 17 00:00:00 2001 From: zhichengxiong Date: Fri, 5 Aug 2022 16:24:50 +0800 Subject: [PATCH 3/5] [autodiff] Print error message that GlobalStoreStmt and GlobalLoadStmt do not support ExternalPtrStmt now --- taichi/transforms/auto_diff.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/taichi/transforms/auto_diff.cpp b/taichi/transforms/auto_diff.cpp index 1d3333d2727d7..d44172d14b2a5 100644 --- a/taichi/transforms/auto_diff.cpp +++ b/taichi/transforms/auto_diff.cpp @@ -989,6 +989,9 @@ class MakeAdjoint : public ADTransform { void visit(GlobalLoadStmt *stmt) override { // issue global store to adjoint + if (stmt->src->is()) { + TI_ERROR("Src of GlobalLoadStmt does not support ExternalPtrStmt now.") + } GlobalPtrStmt *src = stmt->src->as(); TI_ASSERT(src->width() == 1); auto snodes = src->snodes; @@ -1009,7 +1012,7 @@ class MakeAdjoint : public ADTransform { void visit(GlobalStoreStmt *stmt) override { // erase and replace with global load adjoint if (stmt->dest->is()) { - TI_NOT_IMPLEMENTED; + TI_ERROR("Dest of GlobalStoreStmt does not support ExternalPtrStmt now.") } GlobalPtrStmt *dest = stmt->dest->as(); TI_ASSERT(dest->width() == 1); From 3761f2190b14fcced52b86999e5d0ca908445da6 Mon Sep 17 00:00:00 2001 From: zhichengxiong Date: Fri, 5 Aug 2022 19:20:03 +0800 Subject: [PATCH 4/5] [autodiff] modify error message of to_numpy and from_numpy, add test_ad_numpy.py --- taichi/transforms/auto_diff.cpp | 8 ++++++-- tests/python/test_ad_numpy.py | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/python/test_ad_numpy.py diff --git a/taichi/transforms/auto_diff.cpp b/taichi/transforms/auto_diff.cpp index d44172d14b2a5..b6cd8f5f805c0 100644 --- a/taichi/transforms/auto_diff.cpp +++ b/taichi/transforms/auto_diff.cpp @@ -990,7 +990,9 @@ class MakeAdjoint : public ADTransform { void visit(GlobalLoadStmt *stmt) override { // issue global store to adjoint if (stmt->src->is()) { - TI_ERROR("Src of GlobalLoadStmt does not support ExternalPtrStmt now.") + TI_ERROR( + "Importing data from external array (such as numpy array) not " + "supported in AutoDiff for now") } GlobalPtrStmt *src = stmt->src->as(); TI_ASSERT(src->width() == 1); @@ -1012,7 +1014,9 @@ class MakeAdjoint : public ADTransform { void visit(GlobalStoreStmt *stmt) override { // erase and replace with global load adjoint if (stmt->dest->is()) { - TI_ERROR("Dest of GlobalStoreStmt does not support ExternalPtrStmt now.") + TI_ERROR( + "Exporting data to external array (such as numpy array) not " + "supported in AutoDiff for now") } GlobalPtrStmt *dest = stmt->dest->as(); TI_ASSERT(dest->width() == 1); diff --git a/tests/python/test_ad_numpy.py b/tests/python/test_ad_numpy.py new file mode 100644 index 0000000000000..fae7966f5f323 --- /dev/null +++ b/tests/python/test_ad_numpy.py @@ -0,0 +1,35 @@ +import numpy as np +import pytest + +import taichi as ti +from tests import test_utils + + +@test_utils.test() +def test_to_numpy(): + a = ti.field(dtype=float, shape=(), needs_grad=True) + loss = ti.field(dtype=float, shape=(), needs_grad=True) + + def func(): + b = a.to_numpy() + + with pytest.raises(RuntimeError) as e: + with ti.ad.Tape(loss): + func() + assert 'Exporting data to external array (such as numpy array) not supported in AutoDiff for now' in e.value.args[ + 0] + + +@test_utils.test() +def test_from_numpy(): + a = ti.field(dtype=float, shape=(), needs_grad=True) + loss = ti.field(dtype=float, shape=(), needs_grad=True) + + def func(): + a.from_numpy(np.asarray(1)) + + with pytest.raises(RuntimeError) as e: + with ti.ad.Tape(loss): + func() + assert 'Importing data from external array (such as numpy array) not supported in AutoDiff for now' in e.value.args[ + 0] From a4d4ec35f030de9e4ad76900b0243d3eccb0a035 Mon Sep 17 00:00:00 2001 From: zhichengxiong Date: Fri, 5 Aug 2022 19:27:46 +0800 Subject: [PATCH 5/5] rename test_ad_array.py to test_ad_external_array.py --- tests/python/{test_ad_numpy.py => test_ad_external_array.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/python/{test_ad_numpy.py => test_ad_external_array.py} (100%) diff --git a/tests/python/test_ad_numpy.py b/tests/python/test_ad_external_array.py similarity index 100% rename from tests/python/test_ad_numpy.py rename to tests/python/test_ad_external_array.py