Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions toynlp/bert/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def forward(
attention_weight = q @ k.transpose(-2, -1) / (head_dim**0.5)
# pad mask
if mask is not None:
# TODO: -inf?
attention_weight = attention_weight.masked_fill(mask == 0, float("-10000"))
attention_weight = attention_weight.masked_fill(mask == 0, float("-inf"))

attention_score = torch.nn.functional.softmax(attention_weight, dim=-1)
attention_score = self.dropout(attention_score)
Expand Down
6 changes: 3 additions & 3 deletions toynlp/transformer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def forward(
# pad mask
if mask is not None:
# TODO: -inf?
attention_weight = attention_weight.masked_fill(mask == 0, float("-10000"))
attention_weight = attention_weight.masked_fill(mask == 0, float("-inf"))

attention_score = torch.nn.functional.softmax(attention_weight, dim=-1)

Expand Down Expand Up @@ -253,8 +253,8 @@ def _get_source_mask(self, source_token_ids: torch.Tensor) -> torch.Tensor:
return (source_token_ids != self.padding_idx).unsqueeze(1).unsqueeze(2)

def _get_target_mask(self, target_token_ids: torch.Tensor) -> torch.Tensor:
# shape: (batch_size, 1, target_seq_length, 1)
pad_mask = (target_token_ids != self.padding_idx).unsqueeze(1).unsqueeze(3)
# shape: (batch_size, 1, 1, target_seq_length)
pad_mask = (target_token_ids != self.padding_idx).unsqueeze(1).unsqueeze(2)
target_seq_length = target_token_ids.size(1)
# shape: (batch_size, 1, target_seq_length, target_seq_length)
causal_mask = torch.tril(torch.ones((target_seq_length, target_seq_length), device=self.device)).bool()
Expand Down
Loading