Exception when adding tensors

I’m converting a python code to C++ and I have to add 2 tensors, one of them (z_vals) shaped [64] and another one (rand_mult) shaped [100, 100, 64]. In python the broadcasting is made automatically, but in C++ I did it manually, converting the first tensor to [100, 100, 64]:

torch::Tensor z_vals_broad = torch::broadcast_to(z_vals, {100, 100, 64});

This way, I should be able to add them up, but every time I try, an exception is called, without much information about what the problem is. It only says Error at memory location.

I’ve tried:

z_vals = z_vals_broad.add(rand_mult);

and

torch::Tensor z_vals_b = torch::add(z_vals_broad, rand_mult);

but none of it works. Am I missing something in the broadcasting part? Or is the problem elsewhere?

I cannot reproduce the issue and am able to directly add these tensors:

#include <torch/script.h> // One-stop header.

#include <iostream>
#include <memory>

int main(int argc, const char* argv[]) {
  auto a = torch::randn({64});
  auto b = torch::randn({100, 100, 64});

  auto c = a + b;
  std::cout << c.sizes() << std::endl;

  return 0;
}

Output:

./example-app 
[100, 100, 64]

I managed to solve it here, thanks! The problem was one of the tensors was allocated at CUDA.