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

Method attach

imperative/python/src/grad.cpp:25–48  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23GradKeyWrapper::GradKeyWrapper() {}
24
25void GradKeyWrapper::attach(PyObject* const* args, size_t nargs) {
26 if (nargs != 2) {
27 throw py::type_error("expect 2 arguments");
28 }
29 auto* tw = TensorWrapper::try_cast(args[0]);
30 if (!tw) {
31 throw py::type_error("argument 1 must be Tensor");
32 }
33 py::object callback;
34 if (args[1] != Py_None) {
35 callback = py::reinterpret_borrow<py::object>(args[1]);
36 }
37 GenericFunction generic_callback = [=](Span<ValueRef> inputs) -> ValueRefList {
38 mgb_assert(inputs.size() == 1);
39 if (callback) {
40 callback(TensorWrapper::make(py_tensor_type, inputs[0]));
41 }
42 return {};
43 };
44 auto attached_value = imperative::apply(
45 AttachGrad(m_key), tw->m_tensor->data(),
46 FunctionValue::make(generic_callback))[0];
47 tw->m_tensor->reset(attached_value);
48}
49
50void GradKeyWrapper::backward(GradKeyWrapper* self, py::list tensors, py::list grads) {
51 std::vector<ValueRef> args;

Callers 15

test_advance_indexingFunction · 0.45
update_modelFunction · 0.45
workerFunction · 0.45
test_save_loadFunction · 0.45
run_dtr_resnet1202Function · 0.45
workerFunction · 0.45
test_xornet_trace_dumpFunction · 0.45
runnerFunction · 0.45
test_detachFunction · 0.45
_test_optimizerFunction · 0.45

Calls 8

try_castFunction · 0.85
AttachGradClass · 0.85
callbackFunction · 0.50
makeFunction · 0.50
applyFunction · 0.50
sizeMethod · 0.45
dataMethod · 0.45
resetMethod · 0.45

Tested by 15

test_advance_indexingFunction · 0.36
update_modelFunction · 0.36
workerFunction · 0.36
test_save_loadFunction · 0.36
run_dtr_resnet1202Function · 0.36
workerFunction · 0.36
test_xornet_trace_dumpFunction · 0.36
runnerFunction · 0.36
test_detachFunction · 0.36
_test_optimizerFunction · 0.36