2022-08-17 14:16:01 +08:00
|
|
|
#include "core/graph.h"
|
2022-08-29 21:37:07 +08:00
|
|
|
#include "core/kernel.h"
|
2022-08-22 15:01:03 +08:00
|
|
|
#include "core/runtime.h"
|
2022-08-17 14:16:01 +08:00
|
|
|
#include "operators/conv.h"
|
2022-08-29 21:37:07 +08:00
|
|
|
|
2022-08-17 14:16:01 +08:00
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
namespace infini {
|
|
|
|
|
|
|
|
TEST(Conv, ShapeInference) {
|
2023-03-27 21:28:49 +08:00
|
|
|
Runtime runtime = NativeCpuRuntimeObj::getInstance();
|
2022-08-17 14:16:01 +08:00
|
|
|
// Padding modes
|
|
|
|
{
|
2022-08-22 15:01:03 +08:00
|
|
|
Graph g = make_ref<GraphObj>(runtime);
|
2022-08-17 14:16:01 +08:00
|
|
|
Tensor i0 = g->addTensor({1, 3, 4, 4}, DataType::UInt32);
|
|
|
|
Tensor w0 = g->addTensor({2, 3, 3, 3}, DataType::UInt32);
|
|
|
|
auto conv = g->addOp<ConvObj>(i0, w0, nullptr, 1, 1);
|
|
|
|
EXPECT_EQ(conv->getOutput()->getDims(), (Shape{1, 2, 4, 4}));
|
|
|
|
}
|
|
|
|
{
|
2022-08-22 15:01:03 +08:00
|
|
|
Graph g = make_ref<GraphObj>(runtime);
|
2022-08-17 14:16:01 +08:00
|
|
|
Tensor i0 = g->addTensor({1, 3, 4, 4}, DataType::UInt32);
|
|
|
|
Tensor w0 = g->addTensor({2, 3, 3, 3}, DataType::UInt32);
|
|
|
|
auto conv =
|
|
|
|
g->addOp<ConvObj>(i0, w0, nullptr, ConvObj::PaddingMode::Same);
|
|
|
|
EXPECT_EQ(conv->getOutput()->getDims(), (Shape{1, 2, 4, 4}));
|
|
|
|
}
|
|
|
|
{
|
2022-08-22 15:01:03 +08:00
|
|
|
Graph g = make_ref<GraphObj>(runtime);
|
2022-08-17 14:16:01 +08:00
|
|
|
Tensor i0 = g->addTensor({1, 3, 4, 4}, DataType::UInt32);
|
|
|
|
Tensor w0 = g->addTensor({2, 3, 3, 3}, DataType::UInt32);
|
|
|
|
auto conv =
|
|
|
|
g->addOp<ConvObj>(i0, w0, nullptr, ConvObj::PaddingMode::Valid);
|
|
|
|
EXPECT_EQ(conv->getOutput()->getDims(), (Shape{1, 2, 2, 2}));
|
|
|
|
}
|
|
|
|
{ // dilation & stride
|
2022-08-22 15:01:03 +08:00
|
|
|
Graph g = make_ref<GraphObj>(runtime);
|
2022-08-17 14:16:01 +08:00
|
|
|
Tensor i0 = g->addTensor({1, 3, 4, 4}, DataType::UInt32);
|
|
|
|
Tensor w0 = g->addTensor({2, 3, 3, 3}, DataType::UInt32);
|
|
|
|
auto conv = g->addOp<ConvObj>(i0, w0, nullptr, 1, 1, 2, 1, 1, 2);
|
|
|
|
EXPECT_EQ(conv->getOutput()->getDims(), (Shape{1, 2, 2, 2}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Conv, NaiveCPU) {
|
2023-03-27 21:28:49 +08:00
|
|
|
Runtime runtime = NativeCpuRuntimeObj::getInstance();
|
2022-08-22 15:01:03 +08:00
|
|
|
Graph g = make_ref<GraphObj>(runtime);
|
2022-08-17 14:16:01 +08:00
|
|
|
Tensor i0 = g->addTensor({1, 3, 4, 4}, DataType::UInt32);
|
|
|
|
Tensor w0 = g->addTensor({2, 3, 3, 3}, DataType::UInt32);
|
|
|
|
auto conv = g->addOp<ConvObj>(i0, w0, nullptr, 1, 1, 2, 1, 1, 2);
|
|
|
|
|
|
|
|
g->dataMalloc();
|
|
|
|
i0->setData(IncrementalGenerator());
|
|
|
|
w0->setData(IncrementalGenerator());
|
2022-08-22 15:01:03 +08:00
|
|
|
runtime->run(g, true, true);
|
2022-08-17 14:16:01 +08:00
|
|
|
// check answer
|
2022-08-23 16:55:59 +08:00
|
|
|
auto ans =
|
|
|
|
make_ref<TensorObj>(Shape{1, 2, 2, 2}, DataType::UInt32, runtime);
|
2022-08-25 11:29:16 +08:00
|
|
|
ans->dataMalloc();
|
2023-03-21 14:00:04 +08:00
|
|
|
ans->copyin(
|
2022-08-22 15:01:03 +08:00
|
|
|
vector<uint32_t>{4794, 4386, 8199, 7506, 11274, 10542, 20835, 19656});
|
2022-08-17 14:16:01 +08:00
|
|
|
EXPECT_TRUE(conv->getOutput()->equalData(ans));
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:29:28 +08:00
|
|
|
} // namespace infini
|