mirror of
https://github.com/nomic-ai/kompute.git
synced 2026-05-12 01:19:58 +00:00
159 lines
3.9 KiB
C++
159 lines
3.9 KiB
C++
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "kompute/Kompute.hpp"
|
|
|
|
TEST(TestOpTensorCreate, CreateSingleTensorSingleOp)
|
|
{
|
|
|
|
kp::Manager mgr;
|
|
|
|
std::vector<float> testVecA{ 9, 8, 7 };
|
|
|
|
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
|
|
|
|
mgr.rebuildTensors({ tensorA });
|
|
|
|
EXPECT_TRUE(tensorA->isInit());
|
|
|
|
EXPECT_EQ(tensorA->data(), testVecA);
|
|
|
|
tensorA->freeMemoryDestroyGPUResources();
|
|
EXPECT_FALSE(tensorA->isInit());
|
|
}
|
|
|
|
TEST(TestOpTensorCreate, CreateMultipleTensorSingleOp)
|
|
{
|
|
|
|
kp::Manager mgr;
|
|
|
|
std::vector<float> testVecA{ 9, 8, 7 };
|
|
std::vector<float> testVecB{ 6, 5, 4 };
|
|
|
|
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
|
|
std::shared_ptr<kp::Tensor> tensorB{ new kp::Tensor(testVecB) };
|
|
|
|
mgr.rebuildTensors({ tensorA, tensorB });
|
|
|
|
EXPECT_TRUE(tensorA->isInit());
|
|
EXPECT_TRUE(tensorB->isInit());
|
|
|
|
EXPECT_EQ(tensorA->data(), testVecA);
|
|
EXPECT_EQ(tensorB->data(), testVecB);
|
|
}
|
|
|
|
TEST(TestOpTensorCreate, CreateMultipleTensorMultipleOp)
|
|
{
|
|
|
|
kp::Manager mgr;
|
|
|
|
std::vector<float> testVecA{ 9, 8, 7 };
|
|
std::vector<float> testVecB{ 6, 5, 4 };
|
|
|
|
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
|
|
std::shared_ptr<kp::Tensor> tensorB{ new kp::Tensor(testVecB) };
|
|
|
|
mgr.rebuildTensors({ tensorA });
|
|
mgr.rebuildTensors({ tensorB });
|
|
|
|
EXPECT_TRUE(tensorA->isInit());
|
|
EXPECT_TRUE(tensorB->isInit());
|
|
|
|
EXPECT_EQ(tensorA->data(), testVecA);
|
|
EXPECT_EQ(tensorB->data(), testVecB);
|
|
}
|
|
|
|
TEST(TestOpTensorCreate, TestTensorMemoryManagedByManagerDestroyed)
|
|
{
|
|
|
|
std::vector<float> testVecA{ 9, 8, 7 };
|
|
std::vector<float> testVecB{ 6, 5, 4 };
|
|
|
|
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
|
|
std::shared_ptr<kp::Tensor> tensorB{ new kp::Tensor(testVecB) };
|
|
|
|
{
|
|
kp::Manager mgr;
|
|
mgr.rebuildTensors({ tensorA });
|
|
mgr.rebuildTensors({ tensorB });
|
|
|
|
EXPECT_TRUE(tensorA->isInit());
|
|
EXPECT_TRUE(tensorB->isInit());
|
|
|
|
EXPECT_EQ(tensorA->data(), testVecA);
|
|
EXPECT_EQ(tensorB->data(), testVecB);
|
|
}
|
|
|
|
EXPECT_FALSE(tensorA->isInit());
|
|
EXPECT_FALSE(tensorB->isInit());
|
|
}
|
|
|
|
TEST(TestOpTensorCreate, TestTensorMemoryManagedByManagerNOTDestroyed)
|
|
{
|
|
|
|
std::vector<float> testVecA{ 9, 8, 7 };
|
|
std::vector<float> testVecB{ 6, 5, 4 };
|
|
|
|
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
|
|
std::shared_ptr<kp::Tensor> tensorB{ new kp::Tensor(testVecB) };
|
|
|
|
kp::Manager mgr;
|
|
|
|
{
|
|
mgr.rebuildTensors({ tensorA });
|
|
mgr.rebuildTensors({ tensorB });
|
|
|
|
EXPECT_TRUE(tensorA->isInit());
|
|
EXPECT_TRUE(tensorB->isInit());
|
|
|
|
EXPECT_EQ(tensorA->data(), testVecA);
|
|
EXPECT_EQ(tensorB->data(), testVecB);
|
|
}
|
|
|
|
EXPECT_TRUE(tensorA->isInit());
|
|
EXPECT_TRUE(tensorB->isInit());
|
|
}
|
|
|
|
TEST(TestOpTensorCreate, NoErrorIfTensorFreedBefore)
|
|
{
|
|
|
|
std::vector<float> testVecA{ 9, 8, 7 };
|
|
std::vector<float> testVecB{ 6, 5, 4 };
|
|
|
|
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
|
|
std::shared_ptr<kp::Tensor> tensorB{ new kp::Tensor(testVecB) };
|
|
|
|
kp::Manager mgr;
|
|
|
|
mgr.rebuildTensors({ tensorA });
|
|
mgr.rebuildTensors({ tensorB });
|
|
|
|
EXPECT_TRUE(tensorA->isInit());
|
|
EXPECT_TRUE(tensorB->isInit());
|
|
|
|
EXPECT_EQ(tensorA->data(), testVecA);
|
|
EXPECT_EQ(tensorB->data(), testVecB);
|
|
|
|
tensorA->freeMemoryDestroyGPUResources();
|
|
tensorB->freeMemoryDestroyGPUResources();
|
|
EXPECT_FALSE(tensorA->isInit());
|
|
EXPECT_FALSE(tensorB->isInit());
|
|
}
|
|
|
|
TEST(TestOpTensorCreate, ExceptionOnZeroSizeTensor)
|
|
{
|
|
std::vector<float> testVecA;
|
|
|
|
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
|
|
|
|
kp::Manager mgr;
|
|
|
|
try {
|
|
mgr.rebuildTensors({ tensorA });
|
|
} catch (const std::runtime_error& err) {
|
|
// check exception
|
|
ASSERT_TRUE(std::string(err.what()).find("zero-sized") !=
|
|
std::string::npos);
|
|
}
|
|
}
|