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

Function indexing_grad_rule

imperative/python/src/grad_override.cpp:522–555  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

520}
521
522std::optional<ValueRefList> indexing_grad_rule(
523 const OpDef& op, Span<ValueRef> inputs, Span<bool> inputs_require_grad,
524 CustomBackward& backward) {
525 auto&& indexing = op.cast_final_safe<IndexingOneHot>();
526 mgb_assert(inputs.size() == 2);
527 bool flag = inputs_require_grad[0];
528 auto&& grad_op = IndexingSetOneHot::make(indexing.axis, indexing.ndim);
529 SmallVector<ValueRef> inputs2;
530 if (flag) {
531 inputs2.push_back(get_shape(inputs[0]));
532 for (size_t i = 1; i < inputs.size(); ++i) {
533 inputs2.push_back(inputs[i]);
534 }
535 }
536 auto maker = CustomGradMaker(backward, inputs.size());
537 maker.output_size(1).output_captured(0, false);
538 maker.backward([inputs = std::move(inputs2),
539 grad_op_ = std::move(grad_op)](Span<ValueRef> grads) {
540 mgb_assert(grads.size() == 1);
541 ValueRef grad = grads[0];
542 SmallVector<ValueRef> ret(1);
543 if (grad && inputs[0]) {
544 ValueRefList args_(inputs.size() + 1);
545 auto&& zeros = make_empty_tensor(grad.device(), inputs[0], grad.dtype());
546 args_[0] = zeros;
547 args_[1] = inputs[1];
548 args_[2] = grads[0];
549 ret[0] = imperative::apply(*grad_op_, args_)[0];
550 }
551 return ret;
552 });
553 maker.finalize();
554 return imperative::apply(op, inputs);
555}
556
557std::optional<ValueRefList> indexing_set_one_hot_grad_rule(
558 const OpDef& op, Span<ValueRef> inputs, Span<bool> inputs_require_grad,

Callers

nothing calls this directly

Calls 13

get_shapeFunction · 0.85
CustomGradMakerClass · 0.85
make_empty_tensorFunction · 0.85
output_sizeMethod · 0.80
makeFunction · 0.50
applyFunction · 0.50
sizeMethod · 0.45
push_backMethod · 0.45
output_capturedMethod · 0.45
backwardMethod · 0.45
deviceMethod · 0.45
dtypeMethod · 0.45

Tested by

no test coverage detected