forked from jiuyuan/InfiniTensor
49 lines
1.5 KiB
C++
49 lines
1.5 KiB
C++
#include "bang/bang_runtime.h"
|
|
#include "core/graph.h"
|
|
#include "core/kernel.h"
|
|
#include "core/runtime.h"
|
|
#include "operators/unary.h"
|
|
|
|
#include "test.h"
|
|
|
|
namespace infini {
|
|
|
|
template <class T>
|
|
void testUnary(const std::function<void(void *, size_t, DataType)> &generator,
|
|
const Shape &shape) {
|
|
// Runtime
|
|
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
|
|
auto bangRuntime = make_ref<BangRuntimeObj>();
|
|
|
|
// Build input data on CPU
|
|
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
|
|
|
|
// GPU
|
|
Graph bangGraph = make_ref<GraphObj>(bangRuntime);
|
|
auto inputGpu = bangGraph->cloneTensor(inputCpu);
|
|
auto gpuOp = bangGraph->addOp<T>(inputGpu, nullptr);
|
|
bangGraph->dataMalloc();
|
|
inputGpu->setData(generator);
|
|
bangRuntime->run(bangGraph);
|
|
auto outputGpu = gpuOp->getOutput();
|
|
auto outputGpu2Cpu = outputGpu->clone(cpuRuntime);
|
|
// CPU
|
|
Graph cpuGraph = make_ref<GraphObj>(cpuRuntime);
|
|
auto cpuOp = cpuGraph->addOp<T>(inputCpu, nullptr);
|
|
cpuGraph->addTensor(inputCpu);
|
|
cpuGraph->dataMalloc();
|
|
inputCpu->setData(generator);
|
|
cpuRuntime->run(cpuGraph);
|
|
auto outputCpu = cpuOp->getOutput();
|
|
// Check
|
|
EXPECT_TRUE(outputCpu->equalData(outputGpu2Cpu));
|
|
}
|
|
|
|
TEST(cnnl_Unary, run) {
|
|
testUnary<ReluObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
|
|
testUnary<SigmoidObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
|
|
testUnary<TanhObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
|
|
}
|
|
|
|
} // namespace infini
|