MCPcopy Create free account
hub / github.com/alibaba/MNN / run

Method run

test/grad/ReLU6GradTest.cpp:23–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21 virtual ~ReLU6GradTest() = default;
22
23 virtual bool run(int precision) {
24 {
25 const int len = 4;
26 auto input = _Input({len}, NCHW);
27 const float inpudata[] = {-1.0, -2.0, 3.0, 6.0};
28 auto inputPtr = input->writeMap<float>();
29 memcpy(inputPtr, inpudata, len * sizeof(float));
30
31 auto output = _Relu6(input);
32 auto opExpr = output->expr().first;
33
34 auto grad = OpGrad::get(opExpr->get()->type());
35 float outputDiff[] = {0.1, -0.2, -0.3, 0.4};
36 auto inputGrad = grad->onGrad(opExpr, {_Const(outputDiff, {len})});
37
38 const std::vector<float> expectedOutput = {0.0f, 0.0f, -0.3f, 0.0f};
39 auto gotOutput = inputGrad[0]->readMap<float>();
40
41 for (int i = 0; i < len; ++i) {
42 auto diff = ::fabsf(gotOutput[i] - expectedOutput[i]);
43 if (diff > 0.000001) {
44 MNN_ERROR("%s grad test failed, expected: %f, but got: %f!\n", name, expectedOutput[i], gotOutput[i]);
45 return false;
46 }
47 }
48 }
49 {
50 float minValue = -3.0f;
51 float maxValue = 1.0f;
52 const int len = 4;
53 auto input = _Input({len}, NCHW);
54 const float inpudata[] = {-1.0f, -2.0f, 3.0f, 6.0f};
55 auto inputPtr = input->writeMap<float>();
56 memcpy(inputPtr, inpudata, len * sizeof(float));
57
58 auto output = _Relu6(input, minValue, maxValue);
59 auto opExpr = output->expr().first;
60
61 auto grad = OpGrad::get(opExpr->get()->type());
62 float outputDiff[] = {0.1f, -0.2f, -0.3f, 0.4f};
63 auto inputGrad = grad->onGrad(opExpr, {_Const(outputDiff, {len})});
64
65 const std::vector<float> expectedOutput = {0.1f, -0.2f, 0.0f, 0.0f};
66 auto gotOutput = inputGrad[0]->readMap<float>();
67
68 for (int i = 0; i < len; ++i) {
69 auto diff = ::fabsf(gotOutput[i] - expectedOutput[i]);
70 if (diff > 0.000001) {
71 MNN_ERROR("%f-%f, %s grad test failed, expected: %f, but got: %f!\n", minValue, maxValue, name, expectedOutput[i], gotOutput[i]);
72 return false;
73 }
74 }
75 }
76 return true;
77 }
78};
79
80MNNTestSuiteRegister(ReLU6GradTest, "grad/relu6");

Callers

nothing calls this directly

Calls 7

_InputFunction · 0.85
_Relu6Function · 0.85
_ConstFunction · 0.85
getFunction · 0.50
typeMethod · 0.45
getMethod · 0.45
onGradMethod · 0.45

Tested by

no test coverage detected