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

Method run

test/grad/GridSampleGradTest.cpp:23–867  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21 virtual ~GridSampleGradTest() = default;
22
23 virtual bool run(int precision) {
24 std::vector<int> shape = {2, 3, 2, 3};
25 const int inputLen = shape[0] * shape[1] * shape[2] * shape[3];
26 auto input = _Input(shape, NCHW);
27 std::vector<float> inpuData = { 0.5500, 0.6721, 0.4343, 0.8518, 0.9456, 0.6444, 0.5927, 0.4439, 0.9329,
28 0.1434, 0.6933, 0.0180, 0.3173, 0.2903, 0.4159, 0.8706, 0.1812, 0.5890,
29 0.3834, 0.0335, 0.9997, 0.7504, 0.5379, 0.9836, 0.3202, 0.4824, 0.9982,
30 0.8029, 0.2889, 0.8386, 0.2282, 0.6912, 0.2678, 0.9031, 0.7055, 0.9389};
31 auto inputPtr = input->writeMap<float>();
32 memcpy(inputPtr, inpuData.data(), inputLen * sizeof(float));
33
34 std::vector<int> gridShape = {2, 6, 6, 2};
35 const int gridLen = gridShape[0] * gridShape[1] * gridShape[2] * gridShape[3];
36 auto grid = _Input(gridShape, NCHW);
37 std::vector<float> gridData = { -1.0000, -1.0000, -0.6000, -1.0000, -0.2000, -1.0000, 0.2000, -1.0000,
38 0.6000, -1.0000, 1.0000, -1.0000, -1.0000, -0.6000, -0.6000, -0.6000,
39 -0.2000, -0.6000, 0.2000, -0.6000, 0.6000, -0.6000, 1.0000, -0.6000,
40 -1.0000, -0.2000, -0.6000, -0.2000, -0.2000, -0.2000, 0.2000, -0.2000,
41 0.6000, -0.2000, 1.0000, -0.2000, -1.0000, 0.2000, -0.6000, 0.2000,
42 -0.2000, 0.2000, 0.2000, 0.2000, 0.6000, 0.2000, 1.0000, 0.2000,
43 -1.0000, 0.6000, -0.6000, 0.6000, -0.2000, 0.6000, 0.2000, 0.6000,
44 0.6000, 0.6000, 1.0000, 0.6000, -1.0000, 1.0000, -0.6000, 1.0000,
45 -0.2000, 1.0000, 0.2000, 1.0000, 0.6000, 1.0000, 1.0000, 1.0000,
46 -1.0000, -1.0000, -0.6000, -1.0000, -0.2000, -1.0000, 0.2000, -1.0000,
47 0.6000, -1.0000, 1.0000, -1.0000, -1.0000, -0.6000, -0.6000, -0.6000,
48 -0.2000, -0.6000, 0.2000, -0.6000, 0.6000, -0.6000, 1.0000, -0.6000,
49 -1.0000, -0.2000, -0.6000, -0.2000, -0.2000, -0.2000, 0.2000, -0.2000,
50 0.6000, -0.2000, 1.0000, -0.2000, -1.0000, 0.2000, -0.6000, 0.2000,
51 -0.2000, 0.2000, 0.2000, 0.2000, 0.6000, 0.2000, 1.0000, 0.2000,
52 -1.0000, 0.6000, -0.6000, 0.6000, -0.2000, 0.6000, 0.2000, 0.6000,
53 0.6000, 0.6000, 1.0000, 0.6000, -1.0000, 1.0000, -0.6000, 1.0000,
54 -0.2000, 1.0000, 0.2000, 1.0000, 0.6000, 1.0000, 1.0000, 1.0000};
55 auto gridPtr = grid->writeMap<float>();
56 memcpy(gridPtr, gridData.data(), gridLen * sizeof(float));
57
58
59 // TODO: inference of this arguments combination is wrong
60 auto mode = InterpolationMethod::NEAREST;
61 auto paddingMode = GridSamplePaddingMode::GRID_SAMPLE_PADDING_ZEROS;
62 auto alignCorners = false;
63 auto output = _GridSample(_Convert(input, NC4HW4), grid, mode, paddingMode, alignCorners);
64 auto outputNCHW = _Convert(output, NCHW);
65
66 auto outputPtr = outputNCHW->readMap<float>();
67 const int outputLen = shape[0] * shape[1] * gridShape[1] * gridShape[2];
68
69 std::vector<float> outputTorch = { 0.5500, 0.5500, 0.6721, 0.6721, 0.4343, 0.4343, 0.5500, 0.5500, 0.6721,
70 0.6721, 0.4343, 0.4343, 0.5500, 0.5500, 0.6721, 0.6721, 0.4343, 0.4343,
71 0.8518, 0.8518, 0.9456, 0.9456, 0.6444, 0.6444, 0.8518, 0.8518, 0.9456,
72 0.9456, 0.6444, 0.6444, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
73 0.5927, 0.5927, 0.4439, 0.4439, 0.9329, 0.9329, 0.5927, 0.5927, 0.4439,
74 0.4439, 0.9329, 0.9329, 0.5927, 0.5927, 0.4439, 0.4439, 0.9329, 0.9329,
75 0.1434, 0.1434, 0.6933, 0.6933, 0.0180, 0.0180, 0.1434, 0.1434, 0.6933,
76 0.6933, 0.0180, 0.0180, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
77 0.3173, 0.3173, 0.2903, 0.2903, 0.4159, 0.4159, 0.3173, 0.3173, 0.2903,
78 0.2903, 0.4159, 0.4159, 0.3173, 0.3173, 0.2903, 0.2903, 0.4159, 0.4159,
79 0.8706, 0.8706, 0.1812, 0.1812, 0.5890, 0.5890, 0.8706, 0.8706, 0.1812,
80 0.1812, 0.5890, 0.5890, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,

Callers

nothing calls this directly

Calls 9

_InputFunction · 0.85
_GridSampleFunction · 0.85
_ConvertFunction · 0.85
_ConstFunction · 0.85
getFunction · 0.50
dataMethod · 0.45
typeMethod · 0.45
getMethod · 0.45
onGradMethod · 0.45

Tested by

no test coverage detected