Empty_cpu return TensorBase as Tensor

I’m a fresher on PyTorch and I found the following code in aten/src/ATen/native/TensorFactories.cpp:254

Tensor empty_cpu(IntArrayRef size, c10::optional<ScalarType> dtype_opt, c10::optional<Layout> layout_opt,
                 c10::optional<Device> device_opt, c10::optional<bool> pin_memory_opt, c10::optional<c10::MemoryFormat> memory_format_opt) {
  return at::detail::empty_cpu(size, dtype_opt, layout_opt, device_opt, pin_memory_opt, memory_format_opt);
}

My question is that: This function returns a Tensor object and it seems that the return value of “at::detail::empty_cpu()” is TensorBase which is base class of Tensor. (e.g. at::detail::empty_cpu() returns a base-class object and pass it to a subclass variable)

aten/src/ATen/EmptyTensor.cpp:239
TensorBase empty_cpu(
    IntArrayRef size,
    c10::optional<ScalarType> dtype_opt,
    c10::optional<Layout> layout_opt,
    c10::optional<Device> device_opt,
    c10::optional<bool> pin_memory_opt,
    c10::optional<c10::MemoryFormat> memory_format_opt) {
  TORCH_INTERNAL_ASSERT_DEBUG_ONLY(device_or_default(device_opt).type() == DeviceType::CPU);
  TORCH_INTERNAL_ASSERT_DEBUG_ONLY(layout_or_default(layout_opt) == Layout::Strided);

  auto pin_memory = pinned_memory_or_default(pin_memory_opt);
  auto dtype = dtype_or_default(dtype_opt);
  return empty_cpu(size, dtype, pin_memory, memory_format_opt);
}

Why that works? Any suggestions would be appreciated :sweat_smile:

Hey!

The Tensor and TensorBase objects are pretty much the same thing. TensorBase is just a trick we use to speed up compilation by removing the dependency of the whole library (that depends on Tensor) to all the methods on Tensor.
So TensorBase is the Tensor object with all the methods defined in regular cpp files while Tensor is the same with all the methods from native_functions.yaml (and our codegen in general) added to it.

To make this smooth, you can trivially convert from one object to the other.

1 Like

I get it! Thank you very much!!! :grin: