44using namespace refactor ;
55using namespace kernel ;
66
7+ using VecFloat = std::vector<float >;
8+
79static void testOp (SimpleUnaryType opType, float check (float )) {
810 // build routine
911 auto dataTensor = Tensor::share (DataType::F32, Shape{20 , 30 , 50 });
@@ -12,7 +14,7 @@ static void testOp(SimpleUnaryType opType, float check(float)) {
1214 auto res = runtime::Resources ();
1315 auto routine = kernel->lower (res).routine ;
1416 // put input data
15- std::vector< float > data (dataTensor->elementsSize ());
17+ VecFloat data (dataTensor->elementsSize ());
1618 for (auto i : range0_ (data.size ())) { data[i] = i * 1e-4f ; }
1719 auto result = data;
1820 // inference
@@ -27,9 +29,34 @@ static void testOp(SimpleUnaryType opType, float check(float)) {
2729 }
2830}
2931
32+ static void testOpWithData (SimpleUnaryType opType, const VecFloat &data) {
33+ // build routine
34+ auto dataTensor = Tensor::share (DataType::F32, Shape{2 , 3 });
35+ auto kernel = SimpleUnaryCpu::build (opType, *dataTensor);
36+ ASSERT_TRUE (kernel);
37+ auto res = runtime::Resources ();
38+ auto routine = kernel->lower (res).routine ;
39+ // put input data
40+ VecFloat inputdata (dataTensor->elementsSize ());
41+ for (auto i : range0_ (inputdata.size ())) { inputdata[i] = i; }
42+ auto result = inputdata;
43+ // inference
44+ {
45+ void const *inputs[]{result.data ()};
46+ void *outputs[]{result.data ()};
47+ routine (res, nullptr , inputs, outputs);
48+ }
49+ // check
50+ for (auto i : range0_ (inputdata.size ())) {
51+ EXPECT_NEAR (data[i], result[i], 1e-5 );
52+ }
53+ }
54+
3055TEST (kernel, SimpleUnaryCpu) {
3156 testOp (SimpleUnaryType::Abs, std::abs);
3257 testOp (SimpleUnaryType::Sqrt, std::sqrt);
3358 testOp (SimpleUnaryType::Tanh, std::tanh);
3459 testOp (SimpleUnaryType::Erf, std::erf);
60+ testOpWithData (SimpleUnaryType::HardSwish,
61+ VecFloat{0.000000 , 0.666667 , 1.666667 , 3.000000 , 4.000000 , 5.000000 });
3562}
0 commit comments