SIGN IN SIGN UP
taichi-dev / taichi UNCLAIMED

Productive, portable, and performant GPU programming in Python.

0 0 1 C++
#include "gtest/gtest.h"
#include "taichi/ir/analysis.h"
#include "taichi/ir/ir_builder.h"
#include "taichi/ir/statements.h"
namespace taichi::lang {
namespace irpass {
namespace analysis {
TEST(ValueDiffPtrIndex, ConstI32) {
IRBuilder builder;
auto *const1 = builder.get_int32(42);
auto *const2 = builder.get_int32(2);
const auto diff = value_diff_ptr_index(const1, const2);
EXPECT_TRUE(diff.is_diff_certain);
EXPECT_EQ(diff.diff_range, 40);
}
TEST(ValueDiffPtrIndex, ConstF32) {
IRBuilder builder;
auto *const1 = builder.get_float32(1.0f);
auto *const2 = builder.get_float32(1.0f);
const auto diff = value_diff_ptr_index(const1, const2);
// We don't check floating-point numbers since the pass is for pointer indices
EXPECT_FALSE(diff.is_diff_certain);
}
TEST(ValueDiffPtrIndex, BinOp) {
IRBuilder builder;
auto *alloca = builder.create_local_var(PrimitiveType::i32);
auto *load = builder.create_local_load(alloca);
auto *const1 = builder.get_int32(1);
auto *bin1 = builder.create_add(load, const1);
auto *bin2 = builder.create_sub(load, const1);
const auto diff = value_diff_ptr_index(bin1, bin2);
EXPECT_TRUE(diff.is_diff_certain);
EXPECT_EQ(diff.diff_range, 2);
}
TEST(ValueDiffPtrIndex, BinOpButDiff) {
IRBuilder builder;
auto *val1_1 = builder.get_int32(1);
auto *val1_2 = builder.get_int32(1);
auto *val2 = builder.get_int32(3);
auto *bin1 = builder.create_add(val1_1, val2);
auto *bin2 = builder.create_add(val1_2, val2);
const auto diff = value_diff_ptr_index(bin1, bin2);
EXPECT_FALSE(diff.is_diff_certain);
}
TEST(DiffRangeTest, Add) {
IRBuilder builder;
auto for_stmt = std::make_unique<OffloadedStmt>(
/*task_type=*/OffloadedTaskType::struct_for,
[Lang] Fix numerical issue with auto_diff() (#8025) Issue: # bug fix ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at 296454f</samp> This pull request improves the auto-differentiation feature in Taichi by refactoring the internal logic and adding support for ndarray arguments. It also adds a new test case to check the functionality and correctness of the new feature using a hash encoder kernel. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at 296454f</samp> * Simplify and refactor the auto-differentiation logic for matrix pointer statements by removing the use of adstack and replacing it with normal alloca and load/store statements ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L540-R547), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L561-R561), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L602-R610), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L619-R619)) * Avoid data race issues for shared matrix pointer statements in auto-differentiation by duplicating them for local and global load statements using a new function template `process_load_stmt` ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R672-R721), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R730-R737)) * Enable auto-differentiation for ndarray arguments in Taichi kernels by adding support for external pointer statements in atomic operation statements and handling the matrix pointer offset and type ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1648-R1717), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1668-R1773)) * Add a new test case in `tests/python/test_ad_ndarray.py` for verifying the functionality and correctness of the new feature using a simple hash encoder kernel ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-015ebf0e792298ff88ac9c4d87efba82414b3a19a4d85d0887d16099dfe2fc09R1466-R1501))
2023-05-22 09:30:38 +08:00
/*arch=*/Arch::x64, nullptr);
auto *loop_idx = builder.get_loop_index(for_stmt.get(), /*index=*/0);
auto *c1 = builder.get_int32(4);
auto *loop_idx2 = builder.create_add(loop_idx, c1);
auto diff = value_diff_loop_index(loop_idx2, for_stmt.get(), /*index=*/0);
EXPECT_TRUE(diff.related());
EXPECT_EQ(diff.coeff, 1);
EXPECT_EQ(diff.low, 4);
EXPECT_EQ(diff.high, 5);
}
TEST(DiffRangeTest, Sub) {
IRBuilder builder;
auto for_stmt = std::make_unique<OffloadedStmt>(
/*task_type=*/OffloadedTaskType::struct_for,
[Lang] Fix numerical issue with auto_diff() (#8025) Issue: # bug fix ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at 296454f</samp> This pull request improves the auto-differentiation feature in Taichi by refactoring the internal logic and adding support for ndarray arguments. It also adds a new test case to check the functionality and correctness of the new feature using a hash encoder kernel. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at 296454f</samp> * Simplify and refactor the auto-differentiation logic for matrix pointer statements by removing the use of adstack and replacing it with normal alloca and load/store statements ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L540-R547), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L561-R561), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L602-R610), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L619-R619)) * Avoid data race issues for shared matrix pointer statements in auto-differentiation by duplicating them for local and global load statements using a new function template `process_load_stmt` ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R672-R721), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R730-R737)) * Enable auto-differentiation for ndarray arguments in Taichi kernels by adding support for external pointer statements in atomic operation statements and handling the matrix pointer offset and type ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1648-R1717), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1668-R1773)) * Add a new test case in `tests/python/test_ad_ndarray.py` for verifying the functionality and correctness of the new feature using a simple hash encoder kernel ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-015ebf0e792298ff88ac9c4d87efba82414b3a19a4d85d0887d16099dfe2fc09R1466-R1501))
2023-05-22 09:30:38 +08:00
/*arch=*/Arch::x64, nullptr);
auto *loop_idx = builder.get_loop_index(for_stmt.get(), /*index=*/0);
auto *c1 = builder.get_int32(4);
auto *loop_idx2 = builder.create_sub(loop_idx, c1);
auto diff = value_diff_loop_index(loop_idx2, for_stmt.get(), /*index=*/0);
EXPECT_TRUE(diff.related());
EXPECT_EQ(diff.coeff, 1);
EXPECT_EQ(diff.low, -4);
EXPECT_EQ(diff.high, -3);
}
TEST(DiffRangeTest, Mul) {
IRBuilder builder;
auto for_stmt = std::make_unique<OffloadedStmt>(
/*task_type=*/OffloadedTaskType::struct_for,
[Lang] Fix numerical issue with auto_diff() (#8025) Issue: # bug fix ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at 296454f</samp> This pull request improves the auto-differentiation feature in Taichi by refactoring the internal logic and adding support for ndarray arguments. It also adds a new test case to check the functionality and correctness of the new feature using a hash encoder kernel. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at 296454f</samp> * Simplify and refactor the auto-differentiation logic for matrix pointer statements by removing the use of adstack and replacing it with normal alloca and load/store statements ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L540-R547), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L561-R561), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L602-R610), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L619-R619)) * Avoid data race issues for shared matrix pointer statements in auto-differentiation by duplicating them for local and global load statements using a new function template `process_load_stmt` ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R672-R721), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R730-R737)) * Enable auto-differentiation for ndarray arguments in Taichi kernels by adding support for external pointer statements in atomic operation statements and handling the matrix pointer offset and type ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1648-R1717), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1668-R1773)) * Add a new test case in `tests/python/test_ad_ndarray.py` for verifying the functionality and correctness of the new feature using a simple hash encoder kernel ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-015ebf0e792298ff88ac9c4d87efba82414b3a19a4d85d0887d16099dfe2fc09R1466-R1501))
2023-05-22 09:30:38 +08:00
/*arch=*/Arch::x64, nullptr);
auto *loop_idx = builder.get_loop_index(for_stmt.get(), /*index=*/0);
auto *c1 = builder.get_int32(4);
auto *loop_idx2 = builder.create_mul(loop_idx, c1);
auto diff = value_diff_loop_index(loop_idx2, for_stmt.get(), /*index=*/0);
EXPECT_TRUE(diff.related());
EXPECT_EQ(diff.coeff, 4);
EXPECT_EQ(diff.low, 0);
EXPECT_EQ(diff.high, 1);
}
TEST(DiffRangeTest, Shl) {
IRBuilder builder;
auto for_stmt = std::make_unique<OffloadedStmt>(
/*task_type=*/OffloadedTaskType::struct_for,
[Lang] Fix numerical issue with auto_diff() (#8025) Issue: # bug fix ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at 296454f</samp> This pull request improves the auto-differentiation feature in Taichi by refactoring the internal logic and adding support for ndarray arguments. It also adds a new test case to check the functionality and correctness of the new feature using a hash encoder kernel. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at 296454f</samp> * Simplify and refactor the auto-differentiation logic for matrix pointer statements by removing the use of adstack and replacing it with normal alloca and load/store statements ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L540-R547), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L561-R561), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L602-R610), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L619-R619)) * Avoid data race issues for shared matrix pointer statements in auto-differentiation by duplicating them for local and global load statements using a new function template `process_load_stmt` ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R672-R721), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583R730-R737)) * Enable auto-differentiation for ndarray arguments in Taichi kernels by adding support for external pointer statements in atomic operation statements and handling the matrix pointer offset and type ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1648-R1717), [link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-bd5cf26b80ac9337253eb1e63d32a4e9a5a90f393fc8eca8fc898d8cce037583L1668-R1773)) * Add a new test case in `tests/python/test_ad_ndarray.py` for verifying the functionality and correctness of the new feature using a simple hash encoder kernel ([link](https://github.com/taichi-dev/taichi/pull/8025/files?diff=unified&w=0#diff-015ebf0e792298ff88ac9c4d87efba82414b3a19a4d85d0887d16099dfe2fc09R1466-R1501))
2023-05-22 09:30:38 +08:00
/*arch=*/Arch::x64, nullptr);
auto *loop_idx = builder.get_loop_index(for_stmt.get(), /*index=*/0);
auto *c1 = builder.get_int32(2);
auto *loop_idx2 = builder.create_shl(loop_idx, c1);
auto diff = value_diff_loop_index(loop_idx2, for_stmt.get(), /*index=*/0);
EXPECT_TRUE(diff.related());
EXPECT_EQ(diff.coeff, 4);
EXPECT_EQ(diff.low, 0);
EXPECT_EQ(diff.high, 1);
}
} // namespace analysis
} // namespace irpass
} // namespace taichi::lang