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

Update knn_datastore.py #9

Merged
merged 1 commit into from
Dec 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fairseq/modules/knn_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def calculate_select_knn_prob(self,
weight_sum_knn_weight = torch.matmul(knn_select_prob.unsqueeze(-2), knn_weight).squeeze(-2).unsqueeze(-1) # [B, S, K, 1]
# print('calculate multiple k prob sum, took {} s'.format(time.time() - start))

knn_tgt_prob = torch.zeros(B, S, K, self.vocab_size).to(queries.device) # [B, S, K, Vocab Size]
knn_tgt_prob = torch.zeros(B, S, K, self.vocab_size, device=queries.device) # [B, S, K, Vocab Size]
tgt_index = tgt_index.unsqueeze_(-1) # [B, S, K, 1]

# start = time.time()
Expand Down Expand Up @@ -366,7 +366,7 @@ def calculate_knn_prob(self,
knn_weight = torch.softmax(scaled_dists, dim=-1).unsqueeze(-1) # [B, S, K, 1]

# set the target index for each neighbor
knn_tgt_prob = torch.zeros(bsz, seq_len, self.k, self.vocab_size).to(queries.device) # [B, S, K, Vocab Size]
knn_tgt_prob = torch.zeros(bsz, seq_len, self.k, self.vocab_size, device=queries.device) # [B, S, K, Vocab Size]
tgt_index = tgt_index.unsqueeze_(-1) # [B, S, K, 1]

# implemented with pytorch_scatter
Expand Down