Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.cuda.memory_allocated function

PyTorch torch.cuda.memory_allocated function


PyTorch torch reference manual PyTorch torch reference manual

torch.cuda.memory_allocated is a PyTorch function for getting allocated GPU memory. It returns the memory size currently allocated on the CUDA device, in bytes.

Function definition

torch.cuda.memory_allocated(device=None)


Usage examples

Example

import

torch

Check allocated GPU memory

if

torch.

cuda

.

is_available

(

)

:

   

print

(

f

"Initial memory: {torch.cuda.memory_allocated()} bytes"

)

   

Creating tensors

    x

=

torch.

randn

(

1000

,

1000

)

.

cuda

(

)

   

print

(

f

"After allocation: {torch.cuda.memory_allocated()} bytes"

)

   

Delete the tensor

   

del

x

   

print

(

f

"After deletion: {torch.cuda.memory_allocated()} bytes"

)

else

:

   

print

(

"CUDA not available"

)


PyTorch torch reference manual PyTorch torch reference manual