MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / TEST

Function TEST

src/opr/test/misc.cpp:45–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43} // namespace
44
45TEST(TestOprMisc, Argmxx) {
46 auto run = [](bool is_max, int32_t axis, TensorShape sshape) {
47 auto dshape = sshape;
48 dshape.shape[axis] = 1;
49 using Checker = AutoOprChecker<1, 1>;
50 auto make_graph =
51 [&](const Checker::SymInpArray& inputs) -> Checker::SymOutArray {
52 if (is_max)
53 return {opr::Argmax::make(inputs[0], {axis})};
54 else
55 return {opr::Argmin::make(inputs[0], {axis})};
56 };
57 auto better_than = [&](float curr, float best) {
58 if (is_max)
59 return curr > best;
60 else
61 return curr < best;
62 };
63 auto fwd = [&](Checker::NumOutArray& out, Checker::NumInpArray inp) {
64 out[0].dtype(dtype::Int32()).resize(dshape);
65 size_t A, B, C;
66 shape_abc(sshape, axis, A, B, C);
67 for (size_t a = 0; a < A; ++a)
68 for (size_t c = 0; c < C; ++c) {
69 float best_val;
70 size_t best_arg = -1;
71 if (is_max)
72 best_val = std::numeric_limits<float>::lowest();
73 else
74 best_val = std::numeric_limits<float>::max();
75 for (size_t b = 0; b < B; ++b) {
76 float curr_val = inp[0]->ptr<float>()[(a * B + b) * C + c];
77 if (better_than(curr_val, best_val)) {
78 best_val = curr_val;
79 best_arg = b;
80 }
81 }
82 out[0].ptr<int>()[a * C + c] = best_arg;
83 }
84 };
85 Checker{make_graph, fwd}
86 .set_input_allow_grad(0, false)
87 .set_output_allow_grad(0, false)
88 .run({sshape})
89 .run({sshape})
90 .run({sshape});
91 };
92 run(true, 0, {5});
93 run(true, 1, {2, 3, 4, 5});
94 run(true, 2, {2, 3, 4, 5});
95 run(true, 3, {2, 3, 4, 5});
96 run(false, 0, {3, 4, 5});
97 run(false, 1, {2, 3, 4, 5});
98 run(false, 2, {2, 3, 4, 5});
99 run(false, 3, {2, 3, 4, 5});
100}
101
102TEST(TestOprMisc, Argsort) {

Callers

nothing calls this directly

Calls 15

shape_abcFunction · 0.85
maxFunction · 0.85
sortFunction · 0.85
make_callback_copyFunction · 0.85
invokeFunction · 0.85
prev_dev_ptrFunction · 0.85
gradFunction · 0.85
resizeMethod · 0.80
as_megdnnMethod · 0.80
makeFunction · 0.70
runFunction · 0.50
genFunction · 0.50

Tested by

no test coverage detected