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

[fix] torch.nn.functional.embedding -> padding_idx behavior #46714

Closed

Conversation

kshitij12345
Copy link
Collaborator

Reference #46585

Fix for second snippet in the mentioned issue.

predefined_weights = torch.rand(10, 3)
result = torch.nn.functional.embedding(torch.LongTensor([1,2,0]), predefined_weights, padding_idx=0)

@dr-ci
Copy link

dr-ci bot commented Oct 22, 2020

💊 CI failures summary and remediations

As of commit dc77bba (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 3 times.

@gchanan gchanan added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Oct 22, 2020
@codecov
Copy link

codecov bot commented Oct 23, 2020

Codecov Report

Merging #46714 into master will decrease coverage by 0.00%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master   #46714      +/-   ##
==========================================
- Coverage   68.49%   68.48%   -0.01%     
==========================================
  Files         413      413              
  Lines       54478    54478              
==========================================
- Hits        37312    37311       -1     
- Misses      17166    17167       +1     

@kshitij12345
Copy link
Collaborator Author

@albanD Gentle Ping.

With branching on padding_idx>=0, do you think we still need to benchmark it once?

@albanD
Copy link
Collaborator

albanD commented Oct 26, 2020

I think it would be nice to have an idea of what the perf drop is yes.
A simple check with %timeit for CPU and manual run on cuda with proper synchronization should be enough to get an idea.

We do have a benchmark op for embedding bag but I guess it does not cover the same code: https://github.com/pytorch/pytorch/blob/master/benchmarks/operator_benchmark/pt/embeddingbag_test.py ?

@kshitij12345
Copy link
Collaborator Author

@albanD

With some update to the reference script.

import operator_benchmark as op_bench
import torch
import numpy
from pt import configs

"""EmbeddingBag Operator Benchmark"""

class EmbeddingBenchmark(op_bench.TorchBenchmarkBase):
    def init(self, vocab, dim, input_size, padding, device):
        numpy.random.seed((1 << 32) - 1)
        self.weight = torch.randn(vocab, dim, device=device)
        self.input = torch.tensor(numpy.random.randint(0, vocab, input_size), device=device).long()

        if padding is not None:
            padding_mask = torch.rand(self.input.shape) > 0.5
            self.input[padding_mask] = padding

        self.padding = padding
        self.set_module_name('embedding')

    def forward(self):
        return torch.nn.functional.embedding(self.input, self.weight, padding_idx=self.padding)

op_bench.generate_pt_test(configs.embedding_short_configs, EmbeddingBenchmark)

if __name__ == "__main__":
    op_bench.benchmark_runner.main()

Relevant Config

embedding_short_configs = op_bench.cross_product_configs(
    vocab=[10000, 20000],
    dim=[64, 128],
    padding=[None, 2],
    input_size=[32, 48, 64],
    device=['cpu', 'cuda'],
    tags=['short']
)

Attached is the output of running the above code.

embedding.txt

Let me know if the benchmark code looks good.
Also do let me know, we should add that code in this PR?

@albanD
Copy link
Collaborator

albanD commented Oct 27, 2020

That looks good. Do you have the same timings without this change to be able to compare?

@kshitij12345
Copy link
Collaborator Author

Following is the benchmark without the change.

embedding_pre_change.txt

Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to report all the timings.
There is a small change but not too bad. Definitely acceptable to have the correct behavior.

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.

@albanD has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@albanD merged this pull request in 1d233d7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants