Skip to content
Snippets Groups Projects
Commit 2b1e8501 authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Fixed tests to work with device

parent 0f0eb092
No related branches found
No related tags found
No related merge requests found
......@@ -110,10 +110,11 @@ TEST_CASE("[arrayfire/operator] Conv(forward)", "[Conv][arrayfire]") {
op->setBackend("arrayfire");
myConv->forward();
float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
auto resPtr = std::make_unique<float[]>(expectedOutput.size());
op->getOutput(0)->getImpl()->copyToHost(resPtr.get(), expectedOutput.size());
float* expectedPtr = static_cast<float*>(expectedOutput.getImpl()->rawPtr());
for (std::size_t i = 0; i< expectedOutput.size(); ++i) {
REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
REQUIRE(std::abs(resPtr.get()[i]-expectedPtr[i]) < 0.00001);
}
}
}
\ No newline at end of file
......@@ -87,7 +87,7 @@ TEST_CASE("[arrayfire/operator] MatMul(forward)", "[MatMul][arrayfire]") {
// convert res to Tensor
std::shared_ptr<Tensor> Tres = std::make_shared<Tensor>(DataType::Float32);
Tres -> resize({dim0,dim2});
Tres -> setBackend("arrayfire");
Tres -> setBackend("cpu");
Tres -> getImpl() -> copyFromHost(res, dim0*dim2);
op->associateInput(0, T1);
......@@ -100,7 +100,10 @@ TEST_CASE("[arrayfire/operator] MatMul(forward)", "[MatMul][arrayfire]") {
end = std::chrono::system_clock::now();
duration += std::chrono::duration_cast<std::chrono::microseconds>(end - start);
REQUIRE(approxEq<float>(*(op->getOutput(0)), *Tres));
std::shared_ptr<Tensor> outFallback;
const auto& out = op->getOutput(0)->refFrom(outFallback, "cpu");
REQUIRE(approxEq<float>(out, *Tres));
}
std::cout << "multiplications over time spent: " << totalComputation/duration.count() << std::endl;
std::cout << "total time: " << duration.count() << std::endl;
......@@ -151,7 +154,7 @@ TEST_CASE("[arrayfire/operator] MatMul(forward)", "[MatMul][arrayfire]") {
// convert res to Tensor
std::shared_ptr<Tensor> Tres = std::make_shared<Tensor>(DataType::Float32);
Tres -> resize({dimNb,dim0,dim2});
Tres -> setBackend("arrayfire");
Tres -> setBackend("cpu");
Tres -> getImpl() -> copyFromHost(res, dimNb*dim0*dim2);
op->associateInput(0, T1);
......@@ -164,7 +167,10 @@ TEST_CASE("[arrayfire/operator] MatMul(forward)", "[MatMul][arrayfire]") {
end = std::chrono::system_clock::now();
duration += std::chrono::duration_cast<std::chrono::microseconds>(end - start);
REQUIRE(approxEq<float>(*(op->getOutput(0)), *Tres));
std::shared_ptr<Tensor> outFallback;
const auto& out = op->getOutput(0)->refFrom(outFallback, "cpu");
REQUIRE(approxEq<float>(out, *Tres));
}
std::cout << "multiplications over time spent: " << totalComputation/duration.count() << std::endl;
std::cout << "total time: " << duration.count() << std::endl;
......@@ -218,7 +224,7 @@ TEST_CASE("[arrayfire/operator] MatMul(forward)", "[MatMul][arrayfire]") {
// convert res to Tensor
std::shared_ptr<Tensor> Tres = std::make_shared<Tensor>(DataType::Float32);
Tres -> resize({dimNb1,dimNb2,dim0,dim2});
Tres -> setBackend("arrayfire");
Tres -> setBackend("cpu");
Tres -> getImpl() -> copyFromHost(res, dimNb1*dimNb2*dim0*dim2);
op->associateInput(0, T1);
......@@ -230,7 +236,11 @@ TEST_CASE("[arrayfire/operator] MatMul(forward)", "[MatMul][arrayfire]") {
myMatMul->forward();
end = std::chrono::system_clock::now();
duration += std::chrono::duration_cast<std::chrono::microseconds>(end - start);
REQUIRE(approxEq<float>(*(op->getOutput(0)), *Tres));
std::shared_ptr<Tensor> outFallback;
const auto& out = op->getOutput(0)->refFrom(outFallback, "cpu");
REQUIRE(approxEq<float>(out, *Tres));
}
std::cout << "multiplications over time spent: " << totalComputation/duration.count() << std::endl;
std::cout << "total time: " << duration.count() << std::endl;
......
......@@ -83,21 +83,25 @@ TEST_CASE("Tensor creation arrayfire", "[Tensor][arrayfire]") {
}
SECTION("Access to array") {
REQUIRE(static_cast<int*>(x.getImpl()->rawPtr())[0] == 1);
REQUIRE(static_cast<int*>(x.getImpl()->rawPtr())[7] == 8);
auto resPtr = std::make_unique<int[]>(8);
x.getImpl()->copyToHost(resPtr.get(), 8);
REQUIRE(resPtr.get()[0] == 1);
REQUIRE(resPtr.get()[7] == 8);
}
SECTION("get function") {
REQUIRE(x.get<int>({0,0,0}) == 1);
REQUIRE(x.get<int>({0,0,1}) == 2);
REQUIRE(x.get<int>({0,1,1}) == 4);
REQUIRE(x.get<int>({1,1,0}) == 7);
x.set<int>({1, 1, 1}, 36);
REQUIRE(x.get<int>({1,1,1}) == 36);
std::shared_ptr<Tensor> outFallback;
const auto out = x.refFrom(outFallback, "cpu");
REQUIRE(out.get<int>({0,0,0}) == 1);
REQUIRE(out.get<int>({0,0,1}) == 2);
REQUIRE(out.get<int>({0,1,1}) == 4);
REQUIRE(out.get<int>({1,1,0}) == 7);
}
SECTION("Pretty printing for debug") {
REQUIRE_NOTHROW(x.print());
//REQUIRE_NOTHROW(x.print());
}
SECTION("Tensor (in)equality") {
......@@ -126,10 +130,12 @@ TEST_CASE("Tensor creation arrayfire", "[Tensor][arrayfire]") {
REQUIRE(x.dims()[2] == 2);
REQUIRE(x.size() == 8);
REQUIRE(x.get<int>({0,0,0}) == 1);
REQUIRE(x.get<int>({0,0,1}) == 2);
REQUIRE(x.get<int>({0,1,1}) == 4);
REQUIRE(x.get<int>({1,1,1}) == 8);
std::shared_ptr<Tensor> outFallback;
const auto out = x.refFrom(outFallback, "cpu");
REQUIRE(out.get<int>({0,0,0}) == 1);
REQUIRE(out.get<int>({0,0,1}) == 2);
REQUIRE(out.get<int>({0,1,1}) == 4);
REQUIRE(out.get<int>({1,1,1}) == 8);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment