Optional tensors in PyTorch c++ extension
I'm writing  a C++ extension for pytorch, and using the c++ api to do so. To my forward function, I need to pass an optional tensor. Inside the function, I want to do different things based on whether this optional parameter was passed or not. In general, we use NULL for optional pointer arguments in C++ and check inside the function if the pointer is NULL or not. I don't know how to do this for the at::Tensor type of Torch's c++ api. 

void xyz_forward(
    const at::Tensor xyz1, 
    const at::Tensor xyz2, 
    const at::Tensor optional_constraints = something)
{
     if(optional_constraints){
        //do something
     }else{
        //do something else
     }
}


Note that, I can't do const at::Tensor optional_constraints = at::ones or something, because that parameter can take any real value and can be of varying size/shape. I can't assign it a numerical value as an optional argument. Is there a NULL equivalent for this?

One possibility could be to use std::optional as std::optional&lt;at::Tensor&gt; optional_constraints = std::nullopt. It is contextually convertible to bool, so you can check it with if (optional_constraints). Use the .value() method to get the tensor if you pass one, otherwise the default value will be std::nullopt.
