Search code examples
c++libtorch

LibTorch sizeof tensor


I would like to know how I can get the size in bytes of the data type of a torch::Tensor.
I saw somewhere online that sizeof(tensor.dtype()) should work, but for my float32 tensor it prints out 1.


Solution

  • I found it myself finally.
    torch::elementSize() returns the size of the given ScalarType.

    To convert from tensor.dtype(), which has type caffe2::MetaType, to a Scalar, I had to use this converter torch::typeMetaToScalarType(tensor.dtype())

    Therefore, the size of a tensor in memory can be calculated like this:
    tensor.numel() * torch::elementSize(torch::typeMetaToScalarType(tensor.dtype()))

    The docs mention none of those functions, making it very hard to find them.