/
vuron
/
adept
Обзор
Документация
Войти
/
vuron
/
adept
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
with_cpu
tests/transpose_tests.cpp
150 строк
5 KB
kolkir
revert compiler compatibility to gcc12
03 мар 2025, 23:52
03 мар 2025, 23:52
26a9274
Код
Авторство
О чём код?
#include <adept/tensor.hpp> #include <adept/tensor_print.hpp> #include <iterator> #include <tuple> #include <vector> #include "adept/tensor_print.hpp" #include "adept/types.hpp" #include "catch.hpp" using namespace adept; namespace { template <typename T> using Data = std::vector<T>; template <typename T, template <class> class Op> std::tuple<Data<T>, Data<T>, Data<T>> make_test_data() { Data<T> x = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Data<T> y = {7, 8, 1, 3, 9, 4, 5, 2, 6}; Data<T> z; std::transform(x.begin(), x.end(), y.begin(), std::back_inserter(z), Op<T>()); return {x, y, z}; } } // namespace TEST_CASE("Tensor transpose 1xN", "[transpose]") { auto [dx, dy, dz] = make_test_data<float32_t, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {1, 9}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(1)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(0)); for (index_t i = 0; i < x.properties().shape.numel(); ++i) { REQUIRE_THAT(y.at<float32_t>({i, 0}), Catch::WithinRel(x.at<float32_t>({0, i}), 0.001f)); } } TEST_CASE("Tensor transpose Nx1", "[transpose]") { auto [dx, dy, dz] = make_test_data<float32_t, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {9, 1}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(1)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(0)); for (index_t i = 0; i < x.properties().shape.numel(); ++i) { REQUIRE_THAT(y.at<float32_t>({0, i}), Catch::WithinRel(x.at<float32_t>({i, 0}), 0.001f)); } } TEST_CASE("Tensor transpose NxN", "[transpose]") { auto [dx, dy, dz] = make_test_data<float32_t, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(1)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(0)); for (index_t i = 0; i < 3; ++i) { for (index_t j = 0; j < 3; ++j) { REQUIRE_THAT(y.at<float32_t>({i, j}), Catch::WithinRel(x.at<float32_t>({j, i}), 0.001f)); } } } TEST_CASE("Tensor transpose MxN", "[transpose]") { auto [dx, dy, dz] = make_test_data<float32_t, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {2, 3}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(1)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(0)); for (index_t i = 0; i < 3; ++i) { for (index_t j = 0; j < 2; ++j) { REQUIRE_THAT(y.at<float32_t>({i, j}), Catch::WithinRel(x.at<float32_t>({j, i}), 0.001f)); } } } TEST_CASE("Tensor transpose NxM", "[transpose]") { auto [dx, dy, dz] = make_test_data<float32_t, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 2}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(1)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(0)); for (index_t i = 0; i < 2; ++i) { for (index_t j = 0; j < 3; ++j) { REQUIRE_THAT(y.at<float32_t>({i, j}), Catch::WithinRel(x.at<float32_t>({j, i}), 0.001f)); } } } TEST_CASE("Tensor transpose NxM batched", "[transpose]") { auto [dx, dy, dz] = make_test_data<float32_t, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {2, 1, 2}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(0)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(2)); REQUIRE(y.properties().shape.dim(2) == x.properties().shape.dim(1)); for (index_t n = 0; n < 2; ++n) { for (index_t i = 0; i < 2; ++i) { for (index_t j = 0; j < 1; ++j) { REQUIRE_THAT(y.at<float32_t>({n, i, j}), Catch::WithinRel(x.at<float32_t>({n, j, i}), 0.001f)); } } } } TEST_CASE("Tensor transpose NxM 2", "[transpose]") { Data<float32_t> dx = {1, 2, 3, 4, 5, 6, 7, 8}; auto x = Tensor::from_blob( dx.data(), {.shape = {2, 4}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(1)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(0)); for (index_t i = 0; i < 4; ++i) { for (index_t j = 0; j < 2; ++j) { REQUIRE_THAT(y.at<float32_t>({i, j}), Catch::WithinRel(x.at<float32_t>({j, i}), 0.001f)); } } } TEST_CASE("Tensor transpose NxM batched 2", "[transpose]") { Data<float32_t> dx = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}; auto x = Tensor::from_blob( dx.data(), {.shape = {2, 2, 4}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto y = x.transpose2d(); REQUIRE(y.properties().shape.dim(0) == x.properties().shape.dim(0)); REQUIRE(y.properties().shape.dim(1) == x.properties().shape.dim(2)); REQUIRE(y.properties().shape.dim(2) == x.properties().shape.dim(1)); for (index_t n = 0; n < 2; ++n) { for (index_t i = 0; i < 4; ++i) { for (index_t j = 0; j < 2; ++j) { REQUIRE_THAT(y.at<float32_t>({n, i, j}), Catch::WithinRel(x.at<float32_t>({n, j, i}), 0.001f)); } } } }