Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lang] User-friendly exception when copying between ti.field #3442

Merged
merged 6 commits into from
Dec 20, 2021

Conversation

ntlm1686
Copy link
Contributor

Related issue = #2846

Test 0

x = ti.field(ti.f32, (2, 3))
y = ti.field(ti.f32, (2, 3, 4))
x.copy_from(y)

Result 0

Traceback (most recent call last):
ValueError: ti.field shape (2, 3) does not match the source field shape (2, 3, 4)

Test 1

x = ti.field(ti.f32, (2, 3))
y = np.zeros((2, 3))
x.copy_from(y)

Result 1

Traceback (most recent call last):
TypeError: Cannot copy from a non-field object

@netlify
Copy link

netlify bot commented Nov 10, 2021

✔️ Deploy Preview for jovial-fermat-aa59dc canceled.

🔨 Explore the source changes: 97b7826

🔍 Inspect the deploy log: https://app.netlify.com/sites/jovial-fermat-aa59dc/deploys/61bec90737e4e80007b63a60

@ntlm1686
Copy link
Contributor Author

/format

@ntlm1686
Copy link
Contributor Author

@ailzhang

@ailzhang
Copy link
Contributor

/format

Copy link
Contributor

@ailzhang ailzhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @ljjsalt the test failures on GPU seem related?

python/taichi/lang/field.py Outdated Show resolved Hide resolved
@ti.test()
@pytest.mark.parametrize('shape', field_shapes)
def test_field_copy_from(shape):
shapes = [ti.i32, ti.f32] # Metal kernel only supports <= 32-bit data
Copy link
Contributor

@ailzhang ailzhang Nov 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name shape doesn't match what's inside the array :P
Using parameterize for dtype should work and pass CI :D

+@ti.test()
+@pytest.mark.parametrize('shape', field_shapes)
+@pytest.mark.parametrize('dtype', [ti.i32, ti.f32])
+def test_field_copy_from(shape, dtype):
+    x = ti.field(dtype=ti.f32, shape=shape)
+    other = ti.field(dtype=dtype, shape=shape)
+    other.fill(1)
+    x.copy_from(other)
+    convert = lambda arr: arr[0] if len(arr) == 1 else arr
+    assert (convert(x.shape) == shape)
+    assert (x.dtype == ti.f32)
+    assert ((x.to_numpy() == 1).all())

Copy link
Contributor Author

@ntlm1686 ntlm1686 Dec 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ailzhang
Hi, I finally have time to keep working on this PR, how can I test locally? I have run the pytest script.
Sorry for taking you so long for this PR, this commit should work.

Copy link
Contributor

@ailzhang ailzhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks a lot for contributing!

@ailzhang ailzhang merged commit 420e6b6 into taichi-dev:master Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants