However, if I do not use the torch.cuda.synchronize(), after 5 ms, my code is end and return the result, this should prove that I have got what I want.
Here is my code:
import time
import torch

def do_it(X,kh,kw):
  if not X.is_cuda:
    raise Exception,"X is not in GPU!"
  if not len(X.shape) == 3:
    raise Exception,"The shape of X is error!"
  C, H, W = X.shape
  X_unfold = X.unfold(1,kh,1).unfold(2,kw,1)
  X_unfold = X_unfold.permute(1,2,0,3,4)
  shape_ = X_unfold.shape
  sample_num = shape_[0]*shape_[1]
  sample_dim = shape_[2]*shape_[3]*shape_[4]
  Y = X_unfold.reshape(sample_num, sample_dim)
  K = torch.mm(Y,Y.t())
  return Y, K

if __name__ == '__main__':
  X = torch.rand(512,60,60).cuda()
  Y, K = do_it(X,15,15)
  torch.cuda.synchronize()
  start = time.time()
  for i in range(10):
    _, K = do_it(X,15,15)
  torch.cuda.synchronize()
  end = time.time()
  print(end-start)