List initialization of IntArrayRef. Why does it fail

I have a function computing fft2 containing this code:

  void DoSomeThing(Tensor *x)
  {
    c10::optional<IntArrayRef> n_opt = c10::nullopt;
    c10::optional<IntArrayRef> norm= c10::nullopt;
    IntArrayRef dim_arr = {-2, 1};
    Tensor z = torch::fft::fft2(*x, n_opt, dim_arr, norm);
  }

it will fail in release mode but not in debug mode. The issue seem to be that fft2 find huge values for dim_arr. E.g. values like 10032412311. (Note that if I print the content of dim_arr I got -2 and -1.) If I change it to

  void DoSomeThing(Tensor *x)
  {
    c10::optional<IntArrayRef> n_opt = c10::nullopt;
    c10::optional<IntArrayRef> norm= c10::nullopt;
    int64_t arr[2] = {-2,-1};
    IntArrayRef dim_arr = IntArrayRef(arr, 2) ;
    Tensor z = torch::fft::fft2(*x, n_opt, dim_arr, norm);
  }

it works fine. Note that this also works fine:

  void DoSomeThing(Tensor *x)
  {
    c10::optional<IntArrayRef> n_opt = c10::nullopt;
    c10::optional<IntArrayRef> norm= c10::nullopt;
    Tensor z = torch::fft::fft2(*x, n_opt,  {-2,-1}, norm);
  }

I probably missed some essential c++ here. That one cannot initialize a IntArrayRef using a list other than in a function argument list.

Someone who can explain what is happening?

1 Like

@Anders_G
IntArrayRef dim_arr = {-2, 1}; will most likely failed. Since {-2, 1} is temporary, ArrayRef won’t copy the value, it just reference the value. So the behavior of dim_arr is undefined if you do this. You might be able to print out the correct value, but the address is already released, so anything can happen.

2 Likes

Thanks, I suspected something along those line, but I was not sure.