candle-ext 0.1.7

An extension library to Candle that provides PyTorch functions not currently available in Candle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use crate::{
    candle::{Result, Tensor, WithDType},
    TensorExt, F,
};

impl F {
    /// Fills elements of xs tensor with value where mask is True.
    ///
    /// The shape of mask must be broadcastable with the shape of the underlying tensor.
    pub fn masked_fill<D: WithDType>(xs: &Tensor, mask: &Tensor, value: D) -> Result<Tensor> {
        let on_true = xs.full_like(value)?;
        let on_false = xs;
        mask.broadcast_as(xs.shape())?
            .where_cond(&on_true, on_false)
    }
}