Advertisement
Guest User

Pytorch slowdown test 2

a guest
Oct 26th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import torch
  2. import time
  3. from torch.autograd import Variable
  4. import numpy as np
  5.  
  6. """
  7. This program gets slower and slower by time. No noticeable change in memory
  8. consumption seen on nvidia-smi.
  9. """
  10.  
  11. usecuda = 1
  12.  
  13. dtype = torch.FloatTensor
  14. N=1
  15. if usecuda:
  16.   N = 100*N
  17.   dtype = torch.cuda.FloatTensor
  18.  
  19. xdim = (100,50,2)
  20.  
  21. x0 = torch.randn(*xdim).type(dtype)
  22. z0 = torch.randn(N, xdim[0]).type(dtype)
  23.  
  24. x = Variable(x0, requires_grad=True)
  25. z = Variable(z0, requires_grad=False)
  26.  
  27.  
  28. t0 = time.clock()
  29. tsum = np.zeros(5)
  30. tsump = np.zeros(5)
  31. i = 0
  32. M=1000
  33. while True:
  34.   t1 = time.clock()
  35.   b = x.repeat(N,1,1,1).view(N, -1, 2).sum(2).view(N, xdim[0], xdim[1])
  36.   c = z.view(N, xdim[0], 1).expand(N, xdim[0], xdim[1])/b
  37.   loss = c.sum()
  38.   t2 = time.clock()
  39.   loss.backward()
  40.   t3 = time.clock()
  41.   z -= 0.1*b.view(N, -1 , xdim[1]).lt(0.1).sum(2).type(dtype)
  42.   t4 = time.clock()
  43.   x.data -= 0.0000000001*x.grad.data
  44.   x.grad.data.zero_()
  45.   t5 = time.clock()
  46.   tsum += np.array((t1 - t0, t2 - t1, t3 - t2, t4 - t3, t5 - t4))
  47.   t0 = t5
  48.   if i%M == M-1:
  49.     print(tsum, tsum - tsump)
  50.     #print("%.2fs +%.1f%%" % (tsum, tsum*100/tsump - 100))
  51.     tsump = tsum
  52.     tsum = np.zeros(5)
  53.   i = i + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement