/
vuron
/
adept
Обзор
Документация
Войти
/
vuron
/
adept
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
with_cpu
tests/create_tests.cpp
96 строк
4 KB
kolkir
Make create tests supoort multiple backends
06 апр 2025, 20:04
06 апр 2025, 20:04
4e0d26d
Код
Авторство
О чём код?
#include <adept/tensor.hpp> #include "catch.hpp" #include "test_utils.hpp" using namespace adept; using namespace adept::test; TEMPLATE_TEST_CASE_SIG("Create empty tensor", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); REQUIRE_NOTHROW(Tensor::empty({.shape = {4, 4, 4}, .device = Device, .dtype = to_dtype<T>()})); } TEMPLATE_TEST_CASE_SIG("Check empty tensor props", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); auto a = Tensor::empty({.shape = {4, 4, 4}, .device = Device, .dtype = to_dtype<T>()}); REQUIRE(a.properties().device == Device); REQUIRE(a.properties().dtype == to_dtype<T>()); REQUIRE(a.properties().shape.numel() == 64); } TEMPLATE_TEST_CASE_SIG("Check zero tensor props", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); Tensor a = Tensor::zero({.shape = {3, 3}, .device = Device, .dtype = to_dtype<T>()}); REQUIRE(a.properties().device == Device); REQUIRE(a.properties().dtype == to_dtype<T>()); REQUIRE(a.properties().shape.numel() == 9); a = a.cpu(); REQUIRE_THAT(a.at<T>({0, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({1, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({2, 0}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({0, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({1, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({2, 1}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({0, 2}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({1, 2}), Catch::WithinRel(0.f, 0.001f)); REQUIRE_THAT(a.at<T>({2, 2}), Catch::WithinRel(0.f, 0.001f)); } TEMPLATE_TEST_CASE_SIG("Create zero tensor", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); REQUIRE_NOTHROW(Tensor::zero({.shape = {4, 4, 4}, .device = Device, .dtype = to_dtype<T>()})); } TEMPLATE_TEST_CASE_SIG("Create tensor from blob", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); std::vector<T> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; REQUIRE_NOTHROW( Tensor::from_blob(data.data(), {.shape = {3, 3}, .device = Device, .dtype = to_dtype<T>()})); } TEMPLATE_TEST_CASE_SIG("Check tensor from blob", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); std::vector<T> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Tensor a = Tensor::from_blob(data.data(), {.shape = {3, 3}, .device = Device, .dtype = to_dtype<T>()}); REQUIRE(a.properties().device == Device); REQUIRE(a.properties().dtype == to_dtype<T>()); REQUIRE(a.properties().shape.numel() == 9); a = a.cpu(); REQUIRE(a.at<T>({0, 0}) == 1); REQUIRE(a.at<T>({1, 0}) == 4); REQUIRE(a.at<T>({2, 0}) == 7); REQUIRE(a.at<T>({0, 1}) == 2); REQUIRE(a.at<T>({1, 1}) == 5); REQUIRE(a.at<T>({2, 1}) == 8); REQUIRE(a.at<T>({0, 2}) == 3); REQUIRE(a.at<T>({1, 2}) == 6); REQUIRE(a.at<T>({2, 2}) == 9); } // --- Fail cases ------------------------------------ TEMPLATE_TEST_CASE_SIG("Create tensor with empty shape", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); REQUIRE_THROWS(Tensor::empty({.shape = {}, .device = Device, .dtype = to_dtype<T>()})); } TEMPLATE_TEST_CASE_SIG("Access tensor with incorrect type", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); Tensor a = Tensor::empty({.shape = {4, 4, 4}, .device = Device, .dtype = to_dtype<T>()}); REQUIRE_THROWS(a.at<int8_t>({0})); } TEMPLATE_TEST_CASE_SIG("Access tensor with incorrect index", "[create]", TEST_VARIANTS) { apply_backend(Device, Backend); Tensor a = Tensor::empty({.shape = {4, 4, 4}, .device = Device, .dtype = to_dtype<T>()}); REQUIRE_THROWS(a.at<T>({0})); }