RuntimeError: Expected tensor's dynamic type to be Variable, not Tensor

I am writing an cpp extension, where I need to create a new variable within the cpp function.

This is my code here:

#include <torch/torch.h>
#include <vector>
using namespace at;
Tensor foo()
{
    Tensor a = ones(CPU(kFloat), {3, 4});
    return a;
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
{
    m.def("foo", &foo, "foo");
}

However it does not work as I expected, but raise a Runtime error:

RuntimeError: Expected tensor's dynamic type to be Variable, not Tensor

I guess the problem is I with my code, rather than the framework. Could anyone help me with this? Thanks in advance.

Solved. Solution link: