PyTorch – How to check if a tensor is contiguous or not?


A contiguous tensor is a tensor whose elements are stored in a contiguous order without leaving any empty space between them. A tensor created originally is always a contiguous tensor. A tensor can be viewed with different dimensions in contiguous manner.

A transpose of a tensor creates a view of the original tensor which follows non-contiguous order. The transpose of a tensor is non-contiguous.

Syntax

Tensor.is_contiguous()

It returns True if the Tensor is contiguous; False otherwise.

Let's take a couple of example to demonstrate how to use this function to check if a tensor is contiguous or non-contiguous.

Example 1

# import torch library
import torch

# define a torch tensor
A = torch.tensor([1. ,2. ,3. ,4. ,5. ,6.])
print(A)

# find a view of the above tensor
B = A.view(-1,3)
print(B)

print("id(A):", id(A))
print("id(A.view):", id(A.view(-1,3)))
# check if A or A.view() are contiguous or not
print(A.is_contiguous()) # True
print(A.view(-1,3).is_contiguous()) # True
print(B.is_contiguous()) # True

Output

tensor([1., 2., 3., 4., 5., 6.])
tensor([[1., 2., 3.],
   [4., 5., 6.]])
id(A): 80673600
id(A.view): 63219712
True
True
True

Example 2

# import torch library
import torch

# create a torch tensor
A = torch.tensor([[1.,2.],[3.,4.],[5.,6.]])
print(A)

# take transpose of the above tensor
B = A.transpose(0,1)
print(B)
print("id(A):", id(A))
print("id(A.transpose):", id(A.transpose(0,1)))

# check if A or A transpose are contiguous or not
print(A.is_contiguous()) # True
print(A.transpose(0,1).is_contiguous()) # False
print(B.is_contiguous()) # False

Output

tensor([[1., 2.],
   [3., 4.],
   [5., 6.]])
tensor([[1., 3., 5.],
   [2., 4., 6.]])
id(A): 63218368
id(A.transpose): 99215808
True
False
False

Updated on: 06-Dec-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements