mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 23:22:01 +00:00
fab1eebe2c
* First draft of Eigen::Tensor support * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix build errors * Weird allocator stuff? * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove unused + additional allocator junk * Disable warning * Use constexpr * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * clang tidy fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Resolve comments * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove auto constexpr function * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Try again for older C++ * Oops forgot constexpr * Move to new files as suggested * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix weird tests * Fix nits * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Oops, forgot import * Fix clang 3.6 bug * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * More comprehensive test suite * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor allocators to make things more clear * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Switch to std::copy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Switch to DSizes instead of array * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Address feedback * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix python + dummy c++ change to trigger build * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Alignment * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add include guard * Forgot inline * Fix compiler warning * Remove bad test * Better type signatures * Add guards to make compiler requirements more explicit * style: pre-commit fixes * Force rerun of tests due to flake * style: pre-commit fixes * Keep pragmas & all related comments together, add PLEASE KEEP IN SYNC * Move headers out of detail * style: pre-commit fixes * Fix cmake * Improve casting * style: pre-commit fixes * Add a ton more tests + refactor * Improve names * style: pre-commit fixes * Update include/pybind11/eigen/tensor.h Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com> * Fix tests * style: pre-commit fixes * Update * Add a test to verify that strange numpy arrays work * Fix dumb compiler warning * Better tests * Better tests * Fix tests * style: pre-commit fixes * More test fixes * style: pre-commit fixes * A ton more test coverage * Fix tests * style: pre-commit fixes * style: pre-commit fixes * Add back constexpr * Another test * style: pre-commit fixes * Improve tests * Whoops * Less magic numbers * Update tests/test_eigen_tensor.py Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com> * Update tests/test_eigen_tensor.py Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com> * style: pre-commit fixes * Fix tests * style: pre-commit fixes * Fix memory leak * style: pre-commit fixes * Fix order * style: pre-commit fixes * Add test to make sure unsafe casts fail * Minor bug fix to work on 32 bit machines * Implement convert flag * style: pre-commit fixes * Switch to correct TensorMap const use * style: pre-commit fixes * Support older versions of eigen * Weird c++ compilers * Fix Eigen bug * Fix another eigen bug * Yet another eigen bug * Potential flakes? * style: pre-commit fixes * Rerun tests with dummy exception to find out what is going on * Another dummy test run * Ablate more * Found the broken test? * One step closer * one step further * Double check * one thing at a time * Give up and disable the test * Clang lies about being gcc * Oops, fix matrix test * style: pre-commit fixes * Add tests to verify scalar conversions * style: pre-commit fixes * Fix nits * Support no_array * Fix tests * style: pre-commit fixes * Silence compiler warning * Improve build system for ancient compilers * Make clang happy * Make gcc happy * Implement Skylion's suggestions * Fix warning * Inline const pointer check * Implement suggestions * style: pre-commit fixes * Improve tests * Typo * style: pre-commit fixes * Support Google's build environment * style: pre-commit fixes * Update include/pybind11/eigen/tensor.h Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com> * style: pre-commit fixes * Test cleanup per Skylion * Switch to remvove_cv_t * Cleaner test * style: pre-commit fixes * Remove tensor from eigen.h, update tests * style: pre-commit fixes Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com> Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com> Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com> Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
334 lines
10 KiB
C++
334 lines
10 KiB
C++
/*
|
|
tests/eigen_tensor.cpp -- automatic conversion of Eigen Tensor
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#include <pybind11/eigen/tensor.h>
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
namespace PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE {
|
|
|
|
template <typename M>
|
|
void reset_tensor(M &x) {
|
|
for (int i = 0; i < x.dimension(0); i++) {
|
|
for (int j = 0; j < x.dimension(1); j++) {
|
|
for (int k = 0; k < x.dimension(2); k++) {
|
|
x(i, j, k) = i * (5 * 2) + j * 2 + k;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename M>
|
|
bool check_tensor(M &x) {
|
|
for (int i = 0; i < x.dimension(0); i++) {
|
|
for (int j = 0; j < x.dimension(1); j++) {
|
|
for (int k = 0; k < x.dimension(2); k++) {
|
|
if (x(i, j, k) != (i * (5 * 2) + j * 2 + k)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template <int Options>
|
|
Eigen::Tensor<double, 3, Options> &get_tensor() {
|
|
static Eigen::Tensor<double, 3, Options> *x;
|
|
|
|
if (!x) {
|
|
x = new Eigen::Tensor<double, 3, Options>(3, 5, 2);
|
|
reset_tensor(*x);
|
|
}
|
|
|
|
return *x;
|
|
}
|
|
|
|
template <int Options>
|
|
Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> &get_tensor_map() {
|
|
static Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> *x;
|
|
|
|
if (!x) {
|
|
x = new Eigen::TensorMap<Eigen::Tensor<double, 3, Options>>(get_tensor<Options>());
|
|
}
|
|
|
|
return *x;
|
|
}
|
|
|
|
template <int Options>
|
|
Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options> &get_fixed_tensor() {
|
|
static Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options> *x;
|
|
|
|
if (!x) {
|
|
Eigen::aligned_allocator<Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options>>
|
|
allocator;
|
|
x = new (allocator.allocate(1))
|
|
Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options>();
|
|
reset_tensor(*x);
|
|
}
|
|
|
|
return *x;
|
|
}
|
|
|
|
template <int Options>
|
|
const Eigen::Tensor<double, 3, Options> &get_const_tensor() {
|
|
return get_tensor<Options>();
|
|
}
|
|
|
|
template <int Options>
|
|
struct CustomExample {
|
|
CustomExample() : member(get_tensor<Options>()), view_member(member) {}
|
|
|
|
Eigen::Tensor<double, 3, Options> member;
|
|
Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> view_member;
|
|
};
|
|
|
|
template <int Options>
|
|
void init_tensor_module(pybind11::module &m) {
|
|
const char *needed_options = "";
|
|
if (PYBIND11_SILENCE_MSVC_C4127(Options == Eigen::ColMajor)) {
|
|
needed_options = "F";
|
|
} else {
|
|
needed_options = "C";
|
|
}
|
|
m.attr("needed_options") = needed_options;
|
|
|
|
m.def("setup", []() {
|
|
reset_tensor(get_tensor<Options>());
|
|
reset_tensor(get_fixed_tensor<Options>());
|
|
});
|
|
|
|
m.def("is_ok", []() {
|
|
return check_tensor(get_tensor<Options>()) && check_tensor(get_fixed_tensor<Options>());
|
|
});
|
|
|
|
py::class_<CustomExample<Options>>(m, "CustomExample")
|
|
.def(py::init<>())
|
|
.def_readonly(
|
|
"member", &CustomExample<Options>::member, py::return_value_policy::reference_internal)
|
|
.def_readonly("member_view",
|
|
&CustomExample<Options>::view_member,
|
|
py::return_value_policy::reference_internal);
|
|
|
|
m.def(
|
|
"copy_fixed_tensor",
|
|
[]() { return &get_fixed_tensor<Options>(); },
|
|
py::return_value_policy::copy);
|
|
|
|
m.def(
|
|
"copy_tensor", []() { return &get_tensor<Options>(); }, py::return_value_policy::copy);
|
|
|
|
m.def(
|
|
"copy_const_tensor",
|
|
[]() { return &get_const_tensor<Options>(); },
|
|
py::return_value_policy::copy);
|
|
|
|
m.def(
|
|
"move_fixed_tensor_copy",
|
|
[]() -> Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options> {
|
|
return get_fixed_tensor<Options>();
|
|
},
|
|
py::return_value_policy::move);
|
|
|
|
m.def(
|
|
"move_tensor_copy",
|
|
[]() -> Eigen::Tensor<double, 3, Options> { return get_tensor<Options>(); },
|
|
py::return_value_policy::move);
|
|
|
|
m.def(
|
|
"move_const_tensor",
|
|
[]() -> const Eigen::Tensor<double, 3, Options> & { return get_const_tensor<Options>(); },
|
|
py::return_value_policy::move);
|
|
|
|
m.def(
|
|
"take_fixed_tensor",
|
|
|
|
[]() {
|
|
Eigen::aligned_allocator<
|
|
Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options>>
|
|
allocator;
|
|
return new (allocator.allocate(1))
|
|
Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options>(
|
|
get_fixed_tensor<Options>());
|
|
},
|
|
py::return_value_policy::take_ownership);
|
|
|
|
m.def(
|
|
"take_tensor",
|
|
[]() { return new Eigen::Tensor<double, 3, Options>(get_tensor<Options>()); },
|
|
py::return_value_policy::take_ownership);
|
|
|
|
m.def(
|
|
"take_const_tensor",
|
|
[]() -> const Eigen::Tensor<double, 3, Options> * {
|
|
return new Eigen::Tensor<double, 3, Options>(get_tensor<Options>());
|
|
},
|
|
py::return_value_policy::take_ownership);
|
|
|
|
m.def(
|
|
"take_view_tensor",
|
|
[]() -> const Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> * {
|
|
return new Eigen::TensorMap<Eigen::Tensor<double, 3, Options>>(get_tensor<Options>());
|
|
},
|
|
py::return_value_policy::take_ownership);
|
|
|
|
m.def(
|
|
"reference_tensor",
|
|
[]() { return &get_tensor<Options>(); },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_tensor_v2",
|
|
[]() -> Eigen::Tensor<double, 3, Options> & { return get_tensor<Options>(); },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_tensor_internal",
|
|
[]() { return &get_tensor<Options>(); },
|
|
py::return_value_policy::reference_internal);
|
|
|
|
m.def(
|
|
"reference_fixed_tensor",
|
|
[]() { return &get_tensor<Options>(); },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_const_tensor",
|
|
[]() { return &get_const_tensor<Options>(); },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_const_tensor_v2",
|
|
[]() -> const Eigen::Tensor<double, 3, Options> & { return get_const_tensor<Options>(); },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_view_of_tensor",
|
|
[]() -> Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> {
|
|
return get_tensor_map<Options>();
|
|
},
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_view_of_tensor_v2",
|
|
// NOLINTNEXTLINE(readability-const-return-type)
|
|
[]() -> const Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> {
|
|
return get_tensor_map<Options>(); // NOLINT(readability-const-return-type)
|
|
}, // NOLINT(readability-const-return-type)
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_view_of_tensor_v3",
|
|
[]() -> Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> * {
|
|
return &get_tensor_map<Options>();
|
|
},
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_view_of_tensor_v4",
|
|
[]() -> const Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> * {
|
|
return &get_tensor_map<Options>();
|
|
},
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_view_of_tensor_v5",
|
|
[]() -> Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> & {
|
|
return get_tensor_map<Options>();
|
|
},
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_view_of_tensor_v6",
|
|
[]() -> const Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> & {
|
|
return get_tensor_map<Options>();
|
|
},
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"reference_view_of_fixed_tensor",
|
|
[]() {
|
|
return Eigen::TensorMap<
|
|
Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options>>(
|
|
get_fixed_tensor<Options>());
|
|
},
|
|
py::return_value_policy::reference);
|
|
|
|
m.def("round_trip_tensor",
|
|
[](const Eigen::Tensor<double, 3, Options> &tensor) { return tensor; });
|
|
|
|
m.def(
|
|
"round_trip_tensor_noconvert",
|
|
[](const Eigen::Tensor<double, 3, Options> &tensor) { return tensor; },
|
|
py::arg("tensor").noconvert());
|
|
|
|
m.def("round_trip_tensor2",
|
|
[](const Eigen::Tensor<int32_t, 3, Options> &tensor) { return tensor; });
|
|
|
|
m.def("round_trip_fixed_tensor",
|
|
[](const Eigen::TensorFixedSize<double, Eigen::Sizes<3, 5, 2>, Options> &tensor) {
|
|
return tensor;
|
|
});
|
|
|
|
m.def(
|
|
"round_trip_view_tensor",
|
|
[](Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> view) { return view; },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"round_trip_view_tensor_ref",
|
|
[](Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> &view) { return view; },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"round_trip_view_tensor_ptr",
|
|
[](Eigen::TensorMap<Eigen::Tensor<double, 3, Options>> *view) { return view; },
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"round_trip_aligned_view_tensor",
|
|
[](Eigen::TensorMap<Eigen::Tensor<double, 3, Options>, Eigen::Aligned> view) {
|
|
return view;
|
|
},
|
|
py::return_value_policy::reference);
|
|
|
|
m.def(
|
|
"round_trip_const_view_tensor",
|
|
[](Eigen::TensorMap<const Eigen::Tensor<double, 3, Options>> view) {
|
|
return Eigen::Tensor<double, 3, Options>(view);
|
|
},
|
|
py::return_value_policy::move);
|
|
|
|
m.def(
|
|
"round_trip_rank_0",
|
|
[](const Eigen::Tensor<double, 0, Options> &tensor) { return tensor; },
|
|
py::return_value_policy::move);
|
|
|
|
m.def(
|
|
"round_trip_rank_0_noconvert",
|
|
[](const Eigen::Tensor<double, 0, Options> &tensor) { return tensor; },
|
|
py::arg("tensor").noconvert(),
|
|
py::return_value_policy::move);
|
|
|
|
m.def(
|
|
"round_trip_rank_0_view",
|
|
[](Eigen::TensorMap<Eigen::Tensor<double, 0, Options>> &tensor) { return tensor; },
|
|
py::return_value_policy::reference);
|
|
}
|
|
|
|
void test_module(py::module_ &);
|
|
test_initializer name(test_eigen_tensor_module_name, test_module);
|
|
void test_module(py::module_ &m) {
|
|
auto f_style = m.def_submodule("f_style");
|
|
auto c_style = m.def_submodule("c_style");
|
|
|
|
init_tensor_module<Eigen::ColMajor>(f_style);
|
|
init_tensor_module<Eigen::RowMajor>(c_style);
|
|
}
|
|
|
|
} // namespace PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE
|