Pytorch, tensor存储机制

tensor在内存的储存,分为两个部分(也就是说一个tensor占用了两个内存位置),一个内存储存了这个tensor的形状size、步长stride、数据的索引等信息,我们把这一部分称之为头信息区(Tensor);另一个内存储的就是真正的数据,我们称为存储区 (Storage)。

类似TCP IP,地址头,及存储空间。C语言的结构体也可以实现这种。

1)storage().data_ptr()

(Pdb) xx= batch_T.storage().data_ptr()
(Pdb) p xx
2506820080832
(Pdb)  batch_T[0,0].storage().data_ptr()
2506820080832
(Pdb)  batch_T[0,1].storage().data_ptr()
2506820080832
(Pdb)  batch_T[0,2].storage().data_ptr()
2506820080832

可见不同元素的头位置是一样的,

2)storage_offset()可以得到偏移

(Pdb)  batch_T[0,0,0].storage_offset()
0
(Pdb)  batch_T[0,2,0].storage_offset()
4
(Pdb)  batch_T[0,4,0].storage_offset()
8
(Pdb)  batch_T[0,4,0].dtype
torch.float64

(Pdb)  batch_T[0,4,0]
tensor(-0.0550, dtype=torch.float64)

这个可以得到偏移,但是实际内存地址还是不知道。每个都会带个类型?

猜你喜欢

转载自blog.csdn.net/anlongstar/article/details/130561843