/
vuron
/
adept
Обзор
Документация
Войти
/
vuron
/
adept
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v0.1.0
tests/create_tests.cpp
101 строка
4 KB
kolkir
Add broadcast test, rename tags
02 янв 2025, 15:33
02 янв 2025, 15:33
ee1048d
Код
Авторство
О чём код?
#include <adept/tensor.hpp> #include "adept/types.hpp" #include "catch.hpp" using namespace adept; TEST_CASE("Create empty CPU tensor", "[create]") { REQUIRE_NOTHROW( Tensor::empty({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Float32})); REQUIRE_NOTHROW( Tensor::empty({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Float64})); REQUIRE_NOTHROW( Tensor::empty({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Int32})); REQUIRE_NOTHROW( Tensor::empty({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Int8})); } TEST_CASE("Check empty CPU tensor props", "[create]") { auto a = Tensor::empty({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Float32}); REQUIRE(a.properties().device == device_t::CPU); REQUIRE(a.properties().dtype == dtype_t::Float32); REQUIRE(a.properties().shape.numel() == 64); } TEST_CASE("Check zero CPU tensor props", "[create]") { auto a = Tensor::zero({.shape = {3, 3}, .device = device_t::CPU, .dtype = dtype_t::Int32}); REQUIRE(a.properties().device == device_t::CPU); REQUIRE(a.properties().dtype == dtype_t::Int32); REQUIRE(a.properties().shape.numel() == 9); REQUIRE(a.at<int32_t>({0, 0}) == 0); REQUIRE(a.at<int32_t>({1, 0}) == 0); REQUIRE(a.at<int32_t>({2, 0}) == 0); REQUIRE(a.at<int32_t>({0, 1}) == 0); REQUIRE(a.at<int32_t>({1, 1}) == 0); REQUIRE(a.at<int32_t>({2, 1}) == 0); REQUIRE(a.at<int32_t>({0, 2}) == 0); REQUIRE(a.at<int32_t>({1, 2}) == 0); REQUIRE(a.at<int32_t>({2, 2}) == 0); } TEST_CASE("Create zero CPU tensor", "[create]") { REQUIRE_NOTHROW( Tensor::zero({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Float32})); REQUIRE_NOTHROW( Tensor::zero({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Float64})); REQUIRE_NOTHROW( Tensor::zero({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Int32})); REQUIRE_NOTHROW( Tensor::zero({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Int8})); } TEST_CASE("Create CPU tensor from blob", "[create]") { std::vector<int32_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; REQUIRE_NOTHROW(Tensor::from_blob( data.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = dtype_t::Int32})); } TEST_CASE("Check CPU tensor from blob", "[create]") { std::vector<int32_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; auto a = Tensor::from_blob(data.data(), {.shape = {3, 3}, .device = device_t::CPU, .dtype = dtype_t::Int32}); REQUIRE(a.properties().device == device_t::CPU); REQUIRE(a.properties().dtype == dtype_t::Int32); REQUIRE(a.properties().shape.numel() == 9); REQUIRE(a.at<int32_t>({0, 0}) == 1); REQUIRE(a.at<int32_t>({1, 0}) == 4); REQUIRE(a.at<int32_t>({2, 0}) == 7); REQUIRE(a.at<int32_t>({0, 1}) == 2); REQUIRE(a.at<int32_t>({1, 1}) == 5); REQUIRE(a.at<int32_t>({2, 1}) == 8); REQUIRE(a.at<int32_t>({0, 2}) == 3); REQUIRE(a.at<int32_t>({1, 2}) == 6); REQUIRE(a.at<int32_t>({2, 2}) == 9); } // --- Fail cases ------------------------------------ TEST_CASE("Create empty GPU tensor", "[create]") { REQUIRE_THROWS( Tensor::empty({.shape = {4, 4, 4}, .device = device_t::GPU, .dtype = dtype_t::Float32})); } TEST_CASE("Create tensor with empty shape", "[create]") { REQUIRE_THROWS(Tensor::empty({.shape = {}, .device = device_t::CPU, .dtype = dtype_t::Float32})); } TEST_CASE("Access tensor with incorrect type", "[create]") { auto a = Tensor::empty({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Float32}); REQUIRE_THROWS(a.at<int32_t>({0})); } TEST_CASE("Access tensor with incorrect index", "[create]") { auto a = Tensor::empty({.shape = {4, 4, 4}, .device = device_t::CPU, .dtype = dtype_t::Float32}); REQUIRE_THROWS(a.at<float32_t>({0})); }