I met two questions when reading source code.
First is about tensor_ctor function. As we all known, tensor_ctor run internal_new_from_data to create a new tensor. Like following source code, typeIdWithDefault and deviceOptional function will all set device_idx=2, so why here set device=2? If I want create a tensor and transfer to device 0 here, where will the code run?
auto new_tensor = internal_new_from_data(
typeIdWithDefault(r, 2, dispatch_key),
r.scalartypeWithDefault(1, scalar_type),
r.deviceOptional(2),
data,
/*copy_variables=*/true,
/*copy_numpy=*/true,
/*type_inference=*/type_inference,
pin_memory);
Second question is also about to function. when I run tensor.to(0), function THPVariable_to will run in c++ level. And the function call chain here is THPVariable_to->dispatch_to. But here I can not find the next function after dispatch_to function.
tatic Tensor dispatch_to(const Tensor & self, Device device, bool non_blocking, bool copy, c10::optional<c10::MemoryFormat> optional_memory_format) {
pybind11::gil_scoped_release no_gil;
return self.to(self.options().device(device).memory_format(optional_memory_format), non_blocking, copy);
}
Because to function is a cudaMemcpyHostToDevice function, so I want to search the to fucntion from the ending to beginning. But only can find copy_ function(THPVariable_copy_). So what’s function after dispatch_to.
I just started learning the source code. Thanks for your reply.