Skip to content

Conversation

@li-roy
Copy link
Contributor

@li-roy li-roy commented Jun 19, 2019

Stack from ghstack:

These functions only work on variables; calling it on a Tensor errors out. I moved the Tensor error stubs to native functions, and changed the variable implementations to be manual overrides in VariableType.

Differential Revision: D15897423

@pytorchbot pytorchbot added module: autograd Related to torch.autograd, and the autograd engine in general module: cpp Related to C++ API module: cpp-extensions Related to torch.utils.cpp_extension module: cuda Related to torch.cuda, and CUDA support in general module: internals Related to internal abstractions in c10 and ATen module: operators module: pybind Related to our Python bindings / interactions with other Python libraries labels Jun 19, 2019
royboy added 2 commits June 19, 2019 10:46
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
@li-roy li-roy changed the title Stop using Type in Python bindings Move backward and set_data off of Type Jun 19, 2019

//example
//Tensor * add(Tensor & b);
void backward(const Tensor & gradient={}, bool keep_graph=false, bool create_graph=false) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the original signature was intentionally optional<Tensor>, to make it clear that passing nullopt is valid (it's not obviously valid for other cases.) Does the codegen choke on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've been planning to make Tensor? translate to optional<Tensor> for awhile now -- maybe now is a good time to do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this doesn't work on codegen yet.

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature change to backward seems harmless enough since it was defaulted, but I would like to understand a little more. Everything else looks fine.

std::vector<Variable> inputs;
if (!gradient.has_value()) {
gradient = at::ones_like(*this);
Tensor gradient_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, where do you assign gradient to gradient_?

}

THPUtils_assertRet(-1, grad.type() == var.type() || gradIsSparse,
THPUtils_assertRet(-1, grad.type() == var.type() || grad.is_sparse(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I'm missing something, this doesn't look equivalent? The new code allows you to assign a sparse gradient, even if it's the wrong dtype.

const Tensor& gradient,
bool keep_graph,
bool create_graph) const {
bool create_graph) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there some reason the const disappeared on this backward and set_data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are static functions now.

// all static inline to allow for inlining of the non-dynamic part of dispatch
inline void Tensor::backward(const Tensor & gradient, bool keep_graph, bool create_graph) const {
static auto table = globalATenDispatch().getOpTable("aten::backward(Tensor(a!) self, Tensor? gradient=None, bool keep_graph=False, bool create_graph=False) -> void");
return table->getOp<void (const Tensor &, const Tensor &, bool, bool)>(tensorTypeIdToBackend(type_id()), is_variable())(*this, gradient, keep_graph, create_graph);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool.

- func: _cast_Half(Tensor self, bool non_blocking=False) -> Tensor
variants: function

- func: backward(Tensor(a!) self, Tensor? gradient=None, bool keep_graph=False, bool create_graph=False) -> void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you choose these annotations? For example, why is backward's self marked as writeable, but set_data isn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I guess I have these reversed. I guess technically set_data should be set_data_, but not sure I want to make that change here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not even really sure what these annotations mean in this context, as they are pretty different than the usual meanings.

For example, backward is the only place where we mutate self.grad -- does that count as mutable? self itself doesn't actually change.

Maybe @suo can give some guidance here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backward() is a special case that isn't expressible in the alias annotation language—it writes to every tensor in the autograd graph as the gradients are accumulated to the leaves. So if we enabled in the JIT it would have to be a special case in the alias analysis pass.

The bit about mutating the grad property only isn't that relevant—for the purpose of optimization, you can't reorder a call to backward() across a read of any tensor in the autograd graph anyway, so it's fine to consider self.grad part of the self's memory.

Copy link
Contributor

@gchanan gchanan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments.

royboy added 9 commits June 21, 2019 15:27
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
@li-roy li-roy mentioned this pull request Jun 24, 2019
@pytorchbot pytorchbot added the module: typing Related to mypy type annotations label Jun 24, 2019
royboy added 10 commits June 24, 2019 12:54
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
Move backward and set_data off of Type

gh-metadata: pytorch pytorch 21963 gh/li-roy/31/head
@zou3519 zou3519 deleted the gh/li-roy/31/head branch June 30, 2019 11:14
@facebook-github-bot
Copy link
Contributor

@li-roy merged this pull request in 6c454ff.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Jun 30, 2019
Summary:
Pull Request resolved: pytorch/pytorch#21963
ghimport-source-id: 4d9d66ba2c8587503d892b67f535cc2a62e2d19e

Test Plan: Imported from OSS

Differential Revision: D15897423

Pulled By: li-roy

fbshipit-source-id: 2dd55ceb80971df7c86545b7bfff733387f13572
xzhu1900 pushed a commit to xzhu1900/pytorch that referenced this pull request Jul 5, 2019
Summary:
Pull Request resolved: pytorch#21963
ghimport-source-id: 4d9d66b

Test Plan: Imported from OSS

Differential Revision: D15897423

Pulled By: li-roy

fbshipit-source-id: 2dd55ceb80971df7c86545b7bfff733387f13572
gradIsSparse = gradType == sparseType;
}

bool gradIsSparse = var.dtype() == grad.dtype() && toSparse(tensorTypeIdToBackend(var.type_id())) == tensorTypeIdToBackend(grad.type_id());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@li-roy this change is breaking backend extensions in that when the var backend type is a backend extension type, toSparse() just throws a runtime error. https://github.com/pytorch/pytorch/blob/master/c10/core/Backend.h#L31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: autograd Related to torch.autograd, and the autograd engine in general module: cpp Related to C++ API module: cpp-extensions Related to torch.utils.cpp_extension module: cuda Related to torch.cuda, and CUDA support in general module: internals Related to internal abstractions in c10 and ATen module: pybind Related to our Python bindings / interactions with other Python libraries module: typing Related to mypy type annotations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants