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] [refactor] deprecate @boardcast_if_scalar, all use @binary and @unary #943

Merged
merged 19 commits into from
May 12, 2020

Conversation

archibate
Copy link
Collaborator

@archibate archibate marked this pull request as draft May 10, 2020 13:42
@archibate
Copy link
Collaborator Author

========================================= FAILURES ==========================================
_____________________________________ test_mat_inverse ______________________________________
[gw2] linux -- Python 3.8.2 /usr/bin/python3

    def test_mat_inverse():
        for n in range(1, 5):
>           _test_mat_inverse_size(n)

tests/python/test_linalg.py:129: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
python/taichi/lang/__init__.py:351: in wrapped
    test(*test_args, **test_kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

n = 2

    @ti.all_archs
    def _test_mat_inverse_size(n):
        m = ti.Matrix(n, n, dt=ti.f32, shape=())
        M = np.empty(shape=(n, n), dtype=np.float32)
        for i in range(n):
            for j in range(n):
                M[i, j] = i * j + i * 3 + j + 1 + int(i == j) * 4
        assert np.linalg.det(M) != 0
    
        m.from_numpy(M)
    
        @ti.kernel
        def invert():
            m[None] = ti.inversed(m[None])
    
        invert()
    
        m_np = m.to_numpy()
>       np.testing.assert_almost_equal(m_np, np.linalg.inv(M))
E       AssertionError: 
E       Arrays are not almost equal to 7 decimals
E       
E       Mismatched elements: 1 / 4 (25%)
E       Max absolute difference: 0.11337869
E       Max relative difference: 0.95238096
E        x: array([[ 0.2380952, -0.047619 ],
E              [-0.0952381,  0.0056689]], dtype=float32)
E        y: array([[ 0.2380952, -0.047619 ],
E              [-0.0952381,  0.1190476]], dtype=float32)

tests/python/test_linalg.py:124: AssertionError
========================= 1 failed, 355 passed in 90.75s (0:01:30) ==========================

Why fail?

@archibate
Copy link
Collaborator Author

I found why: cannot self assign. If I change to y[None] = ti.inversed(m[None] the problem gone.

@archibate archibate marked this pull request as ready for review May 10, 2020 15:29
@archibate archibate requested review from yuanming-hu, KLozes and k-ye May 10, 2020 15:29
@archibate
Copy link
Collaborator Author

archibate commented May 10, 2020

Have to sleep to prevent a class drop, I believe CI will pass and you will like these changes, cutmr.
Update: opengl failed on pow:

========================================= FAILURES ==========================================
_______________________________________ test_pow_i32 ________________________________________
[gw2] linux -- Python 3.8.2 /usr/bin/python3

    @ti.all_archs
    def test_pow_i32():
>       _test_pow_i(ti.i32)

tests/python/test_pow.py:43: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

dt = DataType.int32

    def _test_pow_i(dt):
        z = ti.var(dt, shape=())
    
        @ti.kernel
        def func(x: dt, y: ti.template()):
            z[None] = x**y
    
        for x in range(-5, 5):
            for y in range(0, 4):
                func(x, y)
>               assert z[None] == x**y
E               assert 124 == -125
E                 -124
E                 +-125

tests/python/test_pow.py:27: AssertionError
=============================== 1 failed, 3 passed in 10.05s ================================

Will fix tmr.

python/taichi/lang/common_ops.py Show resolved Hide resolved
python/taichi/lang/expr.py Outdated Show resolved Hide resolved
python/taichi/lang/ops.py Outdated Show resolved Hide resolved
@archibate archibate requested a review from k-ye May 11, 2020 15:24
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Looks good in general! Just some minor issues.

python/taichi/lang/common_ops.py Outdated Show resolved Hide resolved
python/taichi/lang/common_ops.py Outdated Show resolved Hide resolved
python/taichi/lang/matrix.py Outdated Show resolved Hide resolved
taichi/backends/opengl/codegen_opengl.cpp Outdated Show resolved Hide resolved
@@ -12,7 +13,7 @@ def rand(dtype):
if ti.core.is_integral(dtype):
return randint(1, 5)
else:
return float(randint(1, 5)) / 5
return float(randint(1, 5)) / 5 - 0.01 # prevent floordiv step
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what this means...sorry.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because GLSL doesn't provide pow(int, int), it only provide pow(float, float), so:

pow(4.999, 3) = 124
pow(5.001, 3) = 125

And due to binary floating-point problems, 5 becames 4.99, therefore:

pow(5, 4) = 124

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. (Btw should the comment be something like "prevent integer operands in pow in GLSL")?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because it's the same case with floordiv... let's say prevent integer operands in pow and floordiv in GLSL then.

Co-authored-by: Yuanming Hu <yuanming-hu@users.noreply.github.com>
@archibate
Copy link
Collaborator Author

@k-ye I'm merging this now to prevent further conflict. Please try this out when you are free.

@archibate archibate merged commit 3b79511 into taichi-dev:master May 12, 2020
@k-ye
Copy link
Member

k-ye commented May 12, 2020

I can confirm that sdf_renderer.py is running fast again, thanks!

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