Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sparse computation for DirichletMultinomial.log_prob() #1740

Merged
merged 1 commit into from
Feb 8, 2019

Conversation

fritzo
Copy link
Member

@fritzo fritzo commented Feb 8, 2019

As recommended by @fehiepsi , this PR implements a sparse version of the DirichletMultinomial.log_prob() computation, toggled by a constructor arg is_sparse.

Note this works with the jit and with cuda, but not with jit+cuda at the same time.

Note we could further speed things up by using a sparse data representation, but that is beyond the scope of this PR.

Tested

  • added a unit test for correctness
  • profiled on real sparse data, where this offers 2x speedup on my entire model (most of the time was being taken by torch.lgamma)

Copy link
Member

@fehiepsi fehiepsi left a comment

Choose a reason for hiding this comment

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

LGTM!

mask = (value != 0)
value = value[mask]
alpha = alpha[mask]
elementwise[mask] = torch.lgamma(value + alpha) - _log_factorial(value) - torch.lgamma(alpha)
Copy link
Member

Choose a reason for hiding this comment

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

neat!

Copy link
Member

@neerajprad neerajprad left a comment

Choose a reason for hiding this comment

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

Looks great!

@neerajprad
Copy link
Member

Good to merge?

@fritzo
Copy link
Member Author

fritzo commented Feb 8, 2019

Yes, ready to merge.

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

Successfully merging this pull request may close these issues.

3 participants