Reducing tensor size

Hi all
If I create GPU tensor like
A=torch.tensor((100,100),device=cuda)
Then reduce it’s size as
A=A.narrow(0,0,50)
then empty_cache is called but no memory freed.
Is it normal behaviour?

Hi,

Yes this is expected.
The underlying storage did not changed, only the part of it you’re looking at.
If you really want the full storage to go away, you will need to get a new storage for the small tensor by calling .clone() on it. Then the large storage will be freed.

Hi, I thought about clone or copy, but I have no additional memory for copy or clone - tensors are too big.
Is it possible to this in-place?
I don’t really need any clone - I only want to free unused memory for another task

Unfortunately it’s not possible. When you allocate memory, you ask for a “block” of it of a given size. Then you can free this “block” but you cannot free only part of it. So it’s not possible to do this :confused:
If this is not performance critical, you can use the cpu to do this: send the slice to the cpu, delete the big gpu tensor and then send back the slice into the gpu.