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] Make tensor indicing automatically cast to int32 and raise an warning #1453

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions python/taichi/lang/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ def subscript(value, *indices):
else:
if isinstance(indices,
tuple) and len(indices) == 1 and indices[0] is None:
indices_expr_group = make_expr_group()
else:
indices_expr_group = make_expr_group(*indices)
indices = []
indices_expr_group = make_expr_group(*indices)
tensor_dim = int(value.ptr.get_attribute("dim"))
index_dim = indices_expr_group.size()
assert tensor_dim == index_dim, f'Tensor with dim {tensor_dim} accessed with indices of dim {index_dim}'
Expand Down
24 changes: 12 additions & 12 deletions taichi/transforms/type_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ class TypeCheck : public IRVisitor {
}
if (stmt->ptr->ret_type.data_type != common_container_type) {
TI_WARN(
"[{}] Local store may lose precision (target = {}, value = {}, at",
"[{}] Local store may lose precision (target = {}, value = {}) at",
stmt->name(), stmt->ptr->ret_data_type_name(),
old_data->ret_data_type_name(), stmt->id);
fmt::print(stmt->tb);
TI_WARN("\n{}", stmt->tb);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I change them as OFT, so that users don't see them when log_level is 'error'.

}
stmt->ret_type = stmt->ptr->ret_type;
}
Expand Down Expand Up @@ -127,13 +127,13 @@ class TypeCheck : public IRVisitor {
}
}
for (int i = 0; i < stmt->indices.size(); i++) {
TI_ASSERT_INFO(
is_integral(stmt->indices[i]->ret_type.data_type),
"[{}] Taichi tensors must be accessed with integral indices (e.g., "
"i32/i64). It seems that you have used something else as "
"an index. You can cast a float-pointer number to an integer using "
"int(). Also note that ti.floor(ti.f32) returns f32.",
stmt->name());
if (!is_integral(stmt->indices[i]->ret_type.data_type)) {
TI_WARN(
"[{}] Tensor index {} not integral, casting into int32 implicitly",
stmt->name(), i);
stmt->indices[i] =
insert_type_cast_before(stmt, stmt->indices[i], DataType::i32);
}
TI_ASSERT(stmt->indices[i]->ret_type.width == stmt->snodes.size());
}
}
Expand All @@ -148,8 +148,8 @@ class TypeCheck : public IRVisitor {
}
if (stmt->ptr->ret_type.data_type != promoted) {
TI_WARN("[{}] Global store may lose precision: {} <- {}, at",
stmt->name(), stmt->ptr->ret_data_type_name(), input_type,
stmt->tb);
stmt->name(), stmt->ptr->ret_data_type_name(), input_type);
TI_WARN("\n{}", stmt->tb);
}
stmt->ret_type = stmt->ptr->ret_type;
}
Expand Down Expand Up @@ -229,7 +229,7 @@ class TypeCheck : public IRVisitor {
} else {
TI_WARN("[{}] {} at", stmt->name(), comment);
}
fmt::print(stmt->tb);
TI_WARN("\n{}", stmt->tb);
TI_WARN("Compilation stopped due to type mismatch.");
throw std::runtime_error("Binary operator type mismatch");
};
Expand Down
19 changes: 19 additions & 0 deletions tests/python/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ def test_indices():
assert mapping_b == {0: 1, 1: 0}
# Note that b is column-major:
# the virtual first index exposed to the user comes second in memory layout.


@ti.host_arch_only
def test_float_as_index():
a = ti.var(ti.f32, (8, 5))

@ti.kernel
def func():
i = 6.66
j = 3
I = ti.Vector([2, 1])
for _ in range(1): # prevent constant fold
a[i, j] = 233
a[I + ti.Vector([1, 3.0])] = 666

func()

assert a[6, 3] == 233
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it guaranteed that int32(6.66) == 6 on all platforms?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's how a Python int does, and I believe backends should follow that too.

assert a[3, 4] == 666