Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. # Import PyTorch
  2. import torch # import main library
  3. import torch.nn as nn # import modules like nn.ReLU()
  4. import torch.nn.functional as F # import torch functions like F.relu() and F.relu_()
  5.  
  6. def get_memory_allocated(device, inplace = False):
  7. '''
  8. Function measures allocated memory before and after the ReLU function call.
  9. INPUT:
  10. - device: gpu device to run the operation
  11. - inplace: True - to run ReLU in-place, False - for normal ReLU call
  12. '''
  13.  
  14. # Create a large tensor
  15. t = torch.randn(10000, 10000, device=device)
  16.  
  17. # Measure allocated memory
  18. torch.cuda.synchronize()
  19. start_max_memory = torch.cuda.max_memory_allocated() / 1024**2
  20. start_memory = torch.cuda.memory_allocated() / 1024**2
  21.  
  22. # Call in-place or normal ReLU
  23. if inplace:
  24. F.relu_(t)
  25. else:
  26. output = F.relu(t)
  27.  
  28. # Measure allocated memory after the call
  29. torch.cuda.synchronize()
  30. end_max_memory = torch.cuda.max_memory_allocated() / 1024**2
  31. end_memory = torch.cuda.memory_allocated() / 1024**2
  32.  
  33. # Return amount of memory allocated for ReLU call
  34. return end_memory - start_memory, end_max_memory - start_max_memory
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement