torch.masked_fill
function masked_fill(input: unknown, mask: unknown, value: unknown): TensorFills elements of input tensor with value where mask is True.
Creates a new tensor where positions indicated by the boolean mask are replaced with the specified value, while other positions retain original values. Essential for:
- Attention masking: Setting padding tokens to -inf before softmax
- Conditional updates: Selective tensor modification based on conditions
- Loss masking: Zeroing out gradients for ignored positions
- NaN/Inf handling: Replacing invalid values with valid ones
- Gradient: Gradient is 0 where mask is True, unchanged elsewhere
- Broadcasting: Mask is broadcast to input shape if smaller
- Memory: Creates new tensor; use masked_fill_ for in-place
Parameters
inputunknown- The input tensor (any shape, any dtype)
maskunknown- Boolean mask tensor (broadcastable to input shape)
valueunknown- The value to fill at masked positions
Returns
Tensor– New tensor with masked positions filled with valueExamples
// Basic masking
const x = torch.tensor([1, 2, 3, 4, 5]);
const mask = torch.tensor([true, false, true, false, true]);
torch.masked_fill(x, mask, 0); // [0, 2, 0, 4, 0]
// Attention masking (set padding to -inf)
const scores = torch.randn(4, 4);
const padding_mask = torch.tensor([[false, false, true, true],
[false, false, false, true],
[false, false, false, false],
[false, false, false, false]]);
const masked_scores = torch.masked_fill(scores, padding_mask, -Infinity);
const attention = torch.softmax(masked_scores, -1); // Padding positions become 0
// Replace NaN values
const data = torch.tensor([1, NaN, 3, NaN, 5]);
const nan_mask = torch.isnan(data);
torch.masked_fill(data, nan_mask, 0); // [1, 0, 3, 0, 5]See Also
- PyTorch torch.masked_fill()
- where - Conditional element selection between two tensors
- masked_scatter - Scatter values from source at masked positions
- masked_select - Select elements where mask is True