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

Function TEST

src/opr/test/basic_arith/others.cpp:18–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16using namespace mgb;
17
18TEST(TestOprBasicArith, AddUpdate) {
19 constexpr size_t SIZE = 123456;
20 opr::AddUpdate::Param param{2, -1, 0.5f};
21 HostTensorGenerator<> gen;
22 auto host_x = gen({SIZE}), host_y = gen({SIZE});
23 auto dev_x = std::make_shared<DeviceTensorND>(CompNode::load("xpu0"));
24 dev_x->copy_from(*host_x);
25
26 auto graph = ComputingGraph::make();
27 SymbolVar dev_x_shared = opr::SharedDeviceTensor::make(*graph, dev_x, {"x"}),
28 dev_y = opr::Host2DeviceCopy::make(*graph, host_y, {"y"}),
29 dev_x_updated = opr::AddUpdate::make(dev_x_shared, dev_y, param);
30 auto func = graph->compile({{dev_x_updated, [&](DeviceTensorND&) {}}});
31 func->execute();
32 ASSERT_EQ(dev_x->raw_ptr(), dev_x_updated.node()->prev_dev_ptr());
33
34 func->to_json()->writeto_fpath(output_file("add_update_graph.json"));
35
36 HostTensorND get{CompNode::load("xpu0")};
37 get.copy_from(*dev_x).sync();
38 ASSERT_TRUE(get.layout().eq_layout(host_x->layout()));
39
40 auto x = host_x->ptr<float>(), y = host_y->ptr<float>(), z = get.ptr<float>();
41 for (size_t i = 0; i < SIZE; i++) {
42 auto expect = x[i] * param.alpha->get_cast<float>() +
43 y[i] * param.beta->get_cast<float>() +
44 param.bias->get_cast<float>();
45 MGB_ASSERT_FLOAT_EQ(expect, z[i]);
46 }
47}
48
49TEST(TestOprBasicArith, AddUpdateInt) {
50 constexpr size_t SIZE = 123;

Callers

nothing calls this directly

Calls 15

output_fileFunction · 0.85
is_static_var_shapeFunction · 0.85
make_no_fwdFunction · 0.85
addFunction · 0.85
make_callback_copyFunction · 0.85
dev_ptrFunction · 0.85
maxFunction · 0.85
gradFunction · 0.85
TensorShapeClass · 0.85
prev_dev_ptrFunction · 0.85
prev_dev_ptrMethod · 0.80
eq_layoutMethod · 0.80

Tested by

no test coverage detected