Skip to content

Use blocks machinery to simplify bookkeeping in autodiff#5036

Merged
ezyang merged 3 commits intopytorch:masterfrom
zdevito:pr/use_blocks_autodiff
Feb 5, 2018
Merged

Use blocks machinery to simplify bookkeeping in autodiff#5036
ezyang merged 3 commits intopytorch:masterfrom
zdevito:pr/use_blocks_autodiff

Conversation

@zdevito
Copy link
Copy Markdown
Contributor

@zdevito zdevito commented Feb 4, 2018

Using @ezyang's suggestion, this change uses a block rather than
staging annotations to represent the reverse pass. This allows us to reuse the machinery to copy graphs/blocks to extract the reverse pass concisely, eliminating ~50 lines of code.

This also changes the input order of Gradients df to:
[output vjps][temporary vjps][captures]

In addition to being simpler to generate in this order, it also
will allow ExecutionPlan to append the captures onto the already-
existing input list of vjps that are given by the autograd,
rather than have to prepend them, which should be slightly cheaper.

This also changes the Gradient struct to enforce that input
captures appear before output captures in the capture list,
which makes it easier to use in ExecutionPlan.

Using @ezyang's suggestion, this change uses a block rather than
staging annotations to represent the reverse pass. This allows us
to reuse the machinery to copy graphs/blocks to extract the
reverse pass concisely.

This also change the input order of Gradients df to:
   [output vjps][temporary vjps][captures]

In addition to being simpler to generate in this order, it also
will allow ExecutionPlan to append the captures onto the already-
existing input list of vjps that are given by the autograd,
rather than have to prepend them, which should be slightly cheaper.
This changes the Gradient struct to enforce that input
captures appear before output captures in the capture list,
which makes it easier to use in ExecutionPlan.
@zdevito zdevito force-pushed the pr/use_blocks_autodiff branch from 71b4c33 to 6f27a41 Compare February 4, 2018 08:35
@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Feb 5, 2018

I did only a cursory look but it all seems fine.

@ezyang ezyang merged commit b044c95 into pytorch:master Feb 5, 2018
@apaszke
Copy link
Copy Markdown
Contributor

apaszke commented Feb 5, 2018

@ezyang thanks for letting me take a look :P

// note: reverse_node is intentionally not inserted to avoid
// accidentally acting on it (e.g. in elminate dead code),
// std::cout << *reverse_node << to view its state.
auto reverse_node = graph.create("Reverse"_sym, 0);

This comment was marked as off-topic.

primal_outputs.emplace_back(capture_val);
grad_desc.df_input_captures.emplace_back(Capture::Kind::Output,
primal_outputs.size() - 1);
// we need to create a new temporary output for this capture because it wasn't availiable.

This comment was marked as off-topic.

// XXX: Take care when handling outputs - they can be duplicated!
Gradient grad_desc;

WithInsertPoint guard(*grad_desc.f, grad_desc.f->block());

This comment was marked as off-topic.

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
* Remove addValues and use WithInsertPoint

* Use blocks to simplify differentiate

Using @ezyang's suggestion, this change uses a block rather than
staging annotations to represent the reverse pass. This allows us
to reuse the machinery to copy graphs/blocks to extract the
reverse pass concisely.

This also change the input order of Gradients df to:
   [output vjps][temporary vjps][captures]

In addition to being simpler to generate in this order, it also
will allow ExecutionPlan to append the captures onto the already-
existing input list of vjps that are given by the autograd,
rather than have to prepend them, which should be slightly cheaper.

* Enforce that input capture are before outputs

This changes the Gradient struct to enforce that input
captures appear before output captures in the capture list,
which makes it easier to use in ExecutionPlan.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants