Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols are ignored.
(tensor, padding_idx)
| 4 | |
| 5 | |
| 6 | def make_positions(tensor, padding_idx): |
| 7 | """Replace non-padding symbols with their position numbers. |
| 8 | Position numbers begin at padding_idx+1. Padding symbols are ignored. |
| 9 | """ |
| 10 | # The series of casts and type-conversions here are carefully |
| 11 | # balanced to both work with ONNX export and XLA. In particular XLA |
| 12 | # prefers ints, cumsum defaults to output longs, and ONNX doesn't know |
| 13 | # how to handle the dtype kwarg in cumsum. |
| 14 | mask = tensor.ne(padding_idx).int() |
| 15 | return ( |
| 16 | torch.cumsum(mask, dim=1).type_as(mask) * mask |
| 17 | ).long() + padding_idx |
| 18 | |
| 19 | |
| 20 | def softmax(x, dim): |
nothing calls this directly
no outgoing calls
no test coverage detected