-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[autodiff] Print more specific error message that autodiff does not s…
…upport to_numpy (#5630)
- Loading branch information
1 parent
bee0e63
commit aefa42b
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |