Skip to content
Merged
Changes from 1 commit
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
26 changes: 23 additions & 3 deletions runtime/core/exec_aten/testing_util/tensor_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ class TensorFactory {
t = empty_strided(sizes, strides);
}
if (t.nbytes() > 0) {
memcpy(t.template data<true_ctype>(), data.data(), t.nbytes());
std::transform(
data.begin(), data.end(), t.template data<true_ctype>(), [](auto x) {
return static_cast<true_ctype>(x);
});
}
return t;
}
Expand Down Expand Up @@ -319,7 +322,10 @@ class TensorFactory {
t = empty_strided(sizes, strides);
}
if (t.nbytes() > 0) {
memcpy(t.template data<true_ctype>(), data.data(), t.nbytes());
std::transform(
data.begin(), data.end(), t.template data<true_ctype>(), [](auto x) {
return static_cast<true_ctype>(x);
});
}
return t;
}
Expand Down Expand Up @@ -721,6 +727,13 @@ class TensorFactory {
*/
using ctype = typename internal::ScalarTypeToCppTypeWrapper<DTYPE>::ctype;

/**
* The official C type for the scalar type. Used when accessing elements
* of a constructed Tensor.
*/
using true_ctype =
typename executorch::runtime::ScalarTypeToCppType<DTYPE>::type;

TensorFactory() = default;

/**
Expand Down Expand Up @@ -1019,7 +1032,14 @@ class TensorFactory {
data_.data(),
dim_order_.data(),
strides_.data(),
dynamism) {}
dynamism) {
// The only valid values for bool are 0 and 1; coerce!
if constexpr (std::is_same_v<true_ctype, bool>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems in ifdef aten section this transformation is unconditional while here it is applied only if it is bool. I dont fully understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it doesn't build with bits16 and we don't have bit_cast, otherwise I would make it unconditional here and use bit_cast.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am guessing bit_cast not being usable is related to it available only in c++20.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes; we will have access to c10::bit_cast once I get back to code sharing but currently we don't have it.

for (auto& x : data_) {
x = static_cast<true_ctype>(x);
}
}
}

std::vector<int32_t> sizes_;
std::vector<ctype> data_;
Expand Down
Loading