/
vuron
/
adept
Обзор
Документация
Войти
/
vuron
/
adept
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
tests/linear_tests.cpp
83 строки
4 KB
kolkir
Add tests for Linear layer and update matmul usage
28 мар 2025, 23:53
28 мар 2025, 23:53
bc788d8
Код
Авторство
О чём код?
#include <adept/nn/linear.hpp> #include "catch.hpp" using namespace adept; TEST_CASE("Linear construction", "[linear]") { Linear conv(32, 16); } TEST_CASE("Linear 4x2", "[linear]") { std::vector<float32_t> in_data = {1, 2, 3, 4}; std::vector<float32_t> weights_data = {7, 8, 1, 3, 7, 8, 1, 3}; std::vector<float32_t> bias_data = {3, 2}; auto in = Tensor::from_blob( in_data.data(), {.shape = {1, 4}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto weights = Tensor::from_blob(weights_data.data(), {.shape = {2, 4}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto bias = Tensor::from_blob( bias_data.data(), {.shape = {2}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); Linear fc(4, 2); fc->set_weights(weights); fc->set_bias(bias); auto res = fc(in); REQUIRE_THAT(res.data().at<float32_t>({0, 0}), Catch::WithinRel(41.f, 0.001f)); REQUIRE_THAT(res.data().at<float32_t>({0, 1}), Catch::WithinRel(40.f, 0.001f)); mean(res).backward(); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 0}), Catch::WithinRel(0.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 2}), Catch::WithinRel(1.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 3}), Catch::WithinRel(2.f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 0}), Catch::WithinRel(0.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 1}), Catch::WithinRel(1.f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 2}), Catch::WithinRel(1.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 3}), Catch::WithinRel(2.f, 0.001f)); REQUIRE_THAT(fc->bias().grad().at<float32_t>({0}), Catch::WithinRel(0.5f, 0.001f)); REQUIRE_THAT(fc->bias().grad().at<float32_t>({1}), Catch::WithinRel(0.5f, 0.001f)); } TEST_CASE("Linear batched 4x2", "[linear]") { std::vector<float32_t> in_data = {1, 2, 3, 4, 5, 6, 7, 8}; std::vector<float32_t> weights_data = {7, 8, 1, 3, 7, 8, 1, 3}; std::vector<float32_t> bias_data = {3, 2}; auto in = Tensor::from_blob( in_data.data(), {.shape = {2, 4}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto weights = Tensor::from_blob(weights_data.data(), {.shape = {2, 4}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); auto bias = Tensor::from_blob( bias_data.data(), {.shape = {2}, .device = device_t::CPU, .dtype = to_dtype<float32_t>()}); Linear fc(4, 2); fc->set_weights(weights); fc->set_bias(bias); auto res = fc(in); REQUIRE_THAT(res.data().at<float32_t>({0, 0}), Catch::WithinRel(41.f, 0.001f)); REQUIRE_THAT(res.data().at<float32_t>({0, 1}), Catch::WithinRel(40.f, 0.001f)); REQUIRE_THAT(res.data().at<float32_t>({1, 0}), Catch::WithinRel(117.f, 0.001f)); REQUIRE_THAT(res.data().at<float32_t>({1, 1}), Catch::WithinRel(116.f, 0.001f)); mean(res).backward(); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 0}), Catch::WithinRel(1.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 1}), Catch::WithinRel(2.f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 2}), Catch::WithinRel(2.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({0, 3}), Catch::WithinRel(3.f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 0}), Catch::WithinRel(1.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 1}), Catch::WithinRel(2.f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 2}), Catch::WithinRel(2.5f, 0.001f)); REQUIRE_THAT(fc->weights().grad().at<float32_t>({1, 3}), Catch::WithinRel(3.f, 0.001f)); REQUIRE_THAT(fc->bias().grad().at<float32_t>({0}), Catch::WithinRel(0.5f, 0.001f)); REQUIRE_THAT(fc->bias().grad().at<float32_t>({1}), Catch::WithinRel(0.5f, 0.001f)); }