/
vuron
/
adept
Обзор
Документация
Войти
/
vuron
/
adept
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v0.1.0
tests/arithmetic_tests.cpp
241 строка
9 KB
kolkir
Update tests remove int8 type
28 фев 2025, 14:13
28 фев 2025, 14:13
4016935
Код
Авторство
О чём код?
#include <adept/tensor.hpp> #include <adept/tensor_print.hpp> #include <adept/types.hpp> #include "catch.hpp" #include "test_utils.hpp" using namespace adept; using namespace adept::test; TEMPLATE_TEST_CASE("Tensors add", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_test_data<TestType, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto y = Tensor::from_blob( dy.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x + y; } TEMPLATE_TEST_CASE("Tensors add not pow2 aligned", "[arithmetics]", float32_t, float64_t, int32_t) { std::vector<TestType> dx(1 * 10 * 10, 0); auto x = Tensor::from_blob( dx.data(), {.shape = {1, 10, 10}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); std::vector<TestType> dy(10, 5); auto y = Tensor::from_blob( dy.data(), {.shape = {1, 10}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); REQUIRE_NOTHROW(x + y); } TEMPLATE_TEST_CASE("Tensor add scalar", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_scalar_test_data<TestType, std::plus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x + dy[0]; equals(dz, z); x += dy[0]; equals(dz, x); } TEMPLATE_TEST_CASE("Tensors sub", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_test_data<TestType, std::minus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto y = Tensor::from_blob( dy.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x - y; equals(dz, z); x -= y; equals(dz, x); } TEMPLATE_TEST_CASE("Tensor sub scalar", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_scalar_test_data<TestType, std::minus>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x - dy[0]; equals(dz, z); x -= dy[0]; equals(dz, x); } TEMPLATE_TEST_CASE("Tensors mul", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_test_data<TestType, std::multiplies>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto y = Tensor::from_blob( dy.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x * y; equals(dz, z); x *= y; equals(dz, x); } TEMPLATE_TEST_CASE("Tensor mul scalar", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_scalar_test_data<TestType, std::multiplies>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x * dy[0]; equals(dz, z); x *= dy[0]; equals(dz, x); } TEMPLATE_TEST_CASE("Tensors div", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_test_data<TestType, std::divides>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto y = Tensor::from_blob( dy.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x / y; equals(dz, z); x /= y; equals(dz, x); } TEMPLATE_TEST_CASE("Tensor div scalar", "[arithmetics]", float32_t, float64_t, int32_t) { auto [dx, dy, dz] = make_scalar_test_data<TestType, std::divides>(); auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); auto z = x / dy[0]; equals(dz, z); x /= dy[0]; equals(dz, x); } TEMPLATE_TEST_CASE("Tensor gt scalar", "[arithmetics]", float32_t, float64_t, int32_t) { Data<TestType> dx = {1, 2, 1, 2, 1, 2, 1, 2, 1}; auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); Tensor z = x > 1.0f; REQUIRE_THAT(z.at<TestType>({0, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 2}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 2}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 2}), Catch::WithinRel(0.f, 0.001f)); } TEMPLATE_TEST_CASE("Tensor gt scalar 2", "[arithmetics]", float32_t, float64_t, int32_t) { Data<TestType> dx(2048, 1); auto x = Tensor::from_blob( dx.data(), {.shape = {32, 64, 1}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); Tensor z = x > 2.0f; for (size_t n = 0; n < 32; ++n) { for (size_t r = 0; r < 64; ++r) { REQUIRE_THAT(z.at<TestType>({n, r, 0}), Catch::WithinRel(0.f, 0.001f)); } } } TEMPLATE_TEST_CASE("Tensor ge scalar", "[arithmetics]", float32_t, float64_t, int32_t) { Data<TestType> dx = {-1, 2, 0, 2, -1, 2, 0, 2, -1}; auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); Tensor z = x >= 0.0f; REQUIRE_THAT(z.at<TestType>({0, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 2}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 2}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 2}), Catch::WithinRel(0.f, 0.001f)); } TEMPLATE_TEST_CASE("Tensor lt scalar", "[arithmetics]", float32_t, float64_t, int32_t) { Data<TestType> dx = {1, 2, 1, 2, 1, 2, 1, 2, 1}; auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); Tensor z = x < 2.0f; REQUIRE_THAT(z.at<TestType>({0, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 2}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 2}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 2}), Catch::WithinRel(1.f, 0.001f)); } TEMPLATE_TEST_CASE("Tensor le scalar", "[arithmetics]", float32_t, float64_t, int32_t) { Data<TestType> dx = {-1, 2, 0, 2, -1, 2, 0, 2, -1}; auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); Tensor z = x <= 0.0f; REQUIRE_THAT(z.at<TestType>({0, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 2}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 2}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 2}), Catch::WithinRel(1.f, 0.001f)); } TEMPLATE_TEST_CASE("Tensor neg", "[arithmetics]", float32_t, float64_t, int32_t) { Data<TestType> dx = {-1, 2, 0, 2, -1, 2, 0, 2, -1}; auto x = Tensor::from_blob( dx.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = to_dtype<TestType>()}); Tensor z = -x; REQUIRE_THAT(z.at<TestType>({0, 0}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 0}), Catch::WithinRel(-2.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 1}), Catch::WithinRel(-2.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 1}), Catch::WithinRel(-2.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({0, 2}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({1, 2}), Catch::WithinRel(-2.f, 0.001f)); REQUIRE_THAT(z.at<TestType>({2, 2}), Catch::WithinRel(1.f, 0.001f)); }