Skip to content

Conversation

@weiyangfb
Copy link
Contributor

@weiyangfb weiyangfb commented May 19, 2018

Summary:

  1. fix Implement CUDA Hardshrink #4154, implemented hard_shrink in ATen (CPU + CUDA)
  2. performance test results:
large_data = torch.zeros(1000, 1000, 1000).fill_(0.3).requires_grad_()
def origfn(data):
  f = torch.nn.Hardshrink(0.3)
  large_out = f(data)
%timeit origfn(large_data) 

=> 1 loop, best of 3: 3.99 s per loop

large_data = torch.zeros(1000, 1000, 1000).fill_(0.3).requires_grad_()
def newfn(data):
    large_out = data.hard_shrink(0.3)
%timeit newfn(large_data)

=> 1 loop, best of 3: 4.04 s per loop

large_data = torch.zeros(1000, 1000, 1000).fill_(0.3).requires_grad_()
def origfn_backward(data):
    f = torch.nn.Hardshrink(0.3)
    large_out = f(data)
    large_out.sum().backward()
%timeit origfn_backward(large_data)

=> 1 loop, best of 3: 10 s per loop

large_data = torch.zeros(1000, 1000, 1000).fill_(0.3).requires_grad_()
def newfn_backward(data):
    large_out = data.hard_shrink(0.3)
    large_out.sum().backward()
%timeit newfn_backward(large_data)

=> 1 loop, best of 3: 7.78 s per loop

TODO:

  1. fix bug in using default value of Scalar lambda=0.5, currently 0.5 doesn't get picked up as default value. Failing in testcase of test_torch.py

weiyangfb added 2 commits May 18, 2018 17:42
implement hardshrink with CPU/CUDA_tensor_apply*, similar performance as nn.Hardshrink
const scalar_t& lambda_t_val,
const scalar_t& zero_t_val) {
if (out_t_val >= -lambda_t_val && out_t_val <= lambda_t_val) {
out_t_val = zero_t_val;

This comment was marked as off-topic.

res = torch.LongTensor((-bignumber,))
self.assertGreater(res.abs()[0], 0)

def test_hardshrink(self):

This comment was marked as off-topic.

@ssnl
Copy link
Collaborator

ssnl commented Jun 1, 2018

Any updates on this?

@weiyangfb
Copy link
Contributor Author

For some reasons the compilation of this branch is not working correctly for me. One thing might be related to this was at some point I tried to build using a different version of python. I will close this and create a new PR.

@weiyangfb
Copy link
Contributor Author

weiyangfb commented Jun 1, 2018

Do we need to support HalfTensor type for hardshrink? @ssnl @zou3519

@weiyangfb weiyangfb closed this Jun 4, 2018
@ssnl
Copy link
Collaborator

ssnl commented Jun 4, 2018

@weiyangfb Not necessary, but it shouldn't be too hard, right?

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.

Implement CUDA Hardshrink

3 participants