[Pytorch] Contiguous
pytorch tensor에 contiguous라는 method를 처음 접하게 되어서, 찾아본 후에 정리해본다.
Pytorch 공식 document
https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html
torch.Tensor.contiguous — PyTorch 2.4 documentation
Shortcuts
pytorch.org
공식 문서에 따르면,
'Returns a contiguous in memory tensor containing the same data as self tensor. If self tensor is already in the specified memory format, this function returns the self tensor.'
라고 설명하고 있는데, 간단히 예시로 설명을 하자면
tensor = torch.randn([2,3])
a,b = tensor.size()
for i in range(a):
for j in range(b):
print(tensor[i][j].data_ptr())
print("\n")
'''
46351727104
46351727108
46351727112
46351727116
46351727120
46351727124
'''
tensor_t = tensor.T
a,b = tensor_t.size()
for i in range(a):
for j in range(b):
print(tensor_t[i][j].data_ptr())
print("\n")
'''
46351727104
46351727116
46351727108
46351727120
46351727112
46351727124
'''
tensor_t_cont = tensor_t.contiguous()
for i in range(a):
for j in range(b):
print(tensor_t_cont[i][j].data_ptr())
'''
46363534144
46363534148
46363534152
46363534156
46363534160
46363534164
'''
2x3의 tensor의 경우에 텐서 내의 각 원소값은 메모리 주소를 할당받게 되고
float32 타입의 tensor이므로 각각 4바이트의 메모리를 사용하게 된다.
그렇기 때문에 각 원소별 memory pointer 값을 출력해보면 4씩 메모리 주소값이 차이가 나는 것을 확인 가능하다.
이를 transpose 시킨 tensor_t의 경우에는 이와 달리 4씩 주소값이 차이가 나지 않는다. 이는 transpose를 하게 되면서
최초에 'tensor'에 할당된 메모리 주소가 그대로 유지된 채로 reshape이 일어났기 때문에 주소값이 섞이게 된 것이다.
하지만 여기서
tensor_t.contiguous()
를 해준다면 'tensor'의 경우처럼 메모리 값이 순서대로 정렬되어 나타나게 되는 것이다.
이는 pytorch의 'view' 함수를 사용할 때 적용가능한데, view함수는 contiguous하지 않은 경우에는 error가 발생하며 실행되지 않는다. 그렇기 때문에 view함수를 사용하기 전에 contiguous하게 만들어 주고 실행하는 것이 옳다.
참고로, reshape 함수의 경우에는 contiguous 여부에 영향을 받지 않는다.