Skip to content

Conversation

@jokerkeny
Copy link
Contributor

Summary:
Adds C++ API clip_grad_value_ for torch::nn:utils module.
Also, fix the for indent level error in the original test/test_nn.py.

Issue: #25883

Reviewer: @yf225

@jokerkeny
Copy link
Contributor Author

Hi, I wonder how to create an undefined tensor in C++ PyTorch as None in Python.

As test_nn.py has:

for grad_list in [[grad_w, grad_b], [grad_w, None]]:
    for p, g in zip(l.parameters(), grad_list):
        p._grad = g.clone().view_as(p.data) if g is not None else g

I think the best way to check None in C++ is by Tensor.define(), right? However, I'm not sure how to create an "undefined Tensor". If I use c10::optional<torch::Tensor> object; then use object.has_value() to check, it seems not nice.

Copy link
Contributor

@yf225 yf225 left a comment

Choose a reason for hiding this comment

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

@jokerkeny Thanks so much for the awesome work! I left some minor comments.

clip_grad_value_(l.parameters(), clip_value)
for p in filter(lambda p: p.grad is not None, l.parameters()):
self.assertLessEqual(p.grad.data.max(), clip_value)
self.assertGreaterEqual(p.grad.data.min(), -clip_value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious what's the reason we need to indent these lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I think they should be wrapped under for grad_list in [[grad_w, grad_b], [grad_w, None]]:
The original code is:

for grad_list in [[grad_w, grad_b], [grad_w, None]]:
    for p, g in zip(l.parameters(), grad_list):
        p._grad = g.clone().view_as(p.data) if g is not None else g

clip_grad_value_(l.parameters(), clip_value)
for p in filter(lambda p: p.grad is not None, l.parameters()):
    self.assertLessEqual(p.grad.data.max(), clip_value)
    self.assertGreaterEqual(p.grad.data.min(), -clip_value)

I checked that, only [grad_w, None] would be test.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah I see, thanks so much for catching it! :D

@jokerkeny jokerkeny requested a review from yf225 October 29, 2019 20:36
Copy link
Contributor

@yf225 yf225 left a comment

Choose a reason for hiding this comment

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

@jokerkeny Thanks so much for the awesome work!

clip_grad_value_(l.parameters(), clip_value)
for p in filter(lambda p: p.grad is not None, l.parameters()):
self.assertLessEqual(p.grad.data.max(), clip_value)
self.assertGreaterEqual(p.grad.data.min(), -clip_value)
Copy link
Contributor

Choose a reason for hiding this comment

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

ah I see, thanks so much for catching it! :D

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@yf225 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@yf225 merged this pull request in aa30176.

@yf225 yf225 added the module: cpp Related to C++ API label Nov 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: cpp Related to C++ API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants