Check out the tutorials at the pytorch web site.

In [7]:
%%writefile nptorch.py

import numpy as np
import torch
import time

def cuda_is_available():
    return torch.cuda.is_available()
    
class NpTorch(object):

    def __init__(self, useTorch=False, useCuda=False):

        self.useCuda = useCuda
        self.useTorch = useTorch or useCuda

        if useTorch or useCuda:
            # Functions that are the same for torch on CPU and torch on GPU
            self.tanh = torch.tanh
            self.exp = torch.exp
            self.trans = torch.t
            self.clipmin = lambda x, minx: torch.clamp(x, min=minx)
            self.reshape = lambda x, v: x.view(v)
            self.pack = lambda xs: torch.cat([x.view(-1) for x in xs])
            self.argmax = lambda x, ax: torch.max(x, ax)[1]
        else:
            # Numpy versions
            self.tanh = np.tanh
            self.exp = np.exp
            self.trans = np.transpose
            self.clipmin = lambda x, minx: np.clip(x, minx, None)
            self.reshape = np.reshape
            self.pack = lambda xs: np.hstack([x.flat for x in xs])
            self.argmax = np.argmax

        if useCuda:
            # Unique to torch on GPU
            self.convertFromNumpy = lambda x: torch.from_numpy(x).type(torch.cuda.FloatTensor)
            self.convertToNumpy = lambda x: x.cpu().numpy()
        elif useTorch:
            # Unique to torch on CPU
            self.convertFromNumpy = lambda x: torch.from_numpy(x).type(torch.FloatTensor)
            self.convertToNumpy = lambda x: x.numpy()
        else:
            # Numpy
            self.convertFromNumpy = lambda x: x.astype(np.float32)
            self.convertToNumpy = lambda x: x

    def __repr__(self):
        return 'NpTorch(useTorch={}, useCuda={})'.format(self.useTorch, self.useCuda)
    
if __name__ == '__main__':

    if torch.cuda.is_available():
        testThese = ((False, False), (True, False), (False, True), (True, True))
    else:
        testThese = ((False, False), (True, False))
        print('cuda is not available so will not be tested')
        
    for useTorch, useCuda in testThese:
        
        nptorch = NpTorch(useTorch, useCuda)

        A = np.arange(6).reshape((2,3))
        print('----------------------------------------------')
        print('A =\n', A)
        print('----------------------------------------------')
        
        print('----------------------------------------------')
        print('useTorch={} useCuda={}'.format(useTorch, useCuda))
        print('----------------------------------------------')
        AA = nptorch.convertFromNumpy(A)
        print('----------------------------------------------')
        print('  nptorch.convertFromNumpy(A)\n', AA)

        print('----------------------------------------------')
        print('  nptorch.tanh(AA) = \n{}'.format(nptorch.tanh(AA)))
        print('----------------------------------------------')
        print('  nptorch.trans(AA) = \n{}'.format(nptorch.trans(AA)))
        print('----------------------------------------------')
        print('  nptorch.reshape(AA, (1, 6)) = \n{}'.format(nptorch.reshape(AA, (1, 6))))

        print('----------------------------------------------')
        print('  nptorch.convertToNumpy(AA)\n', nptorch.convertToNumpy(AA))

        print('----------------------------------------------')
        print('  nptorch.pack((AA,AA))\n', nptorch.pack((AA,AA)))

        print('----------------------------------------------')
        print('  nptorch.argmax((AA,1))\n', nptorch.argmax(AA, 1))

    print('----------------------------------------------')
    print('Big matrix multiply. A @ A for A 10000x10000')
    # A = 1.e-6 * np.arange(10000*10000).reshape((10000, 10000))
    A = np.random.uniform(-1.e-4, 1.e-4, size=(10000, 10000))
    nptorch = NpTorch(False, False)
    startTime = time.time()
    A = nptorch.convertFromNumpy(A)  # make it single precision
    B = A @ A
    print('Numpy took {:.6f} seconds [0,0]={}'.format(time.time() - startTime, B[0,0]))
    
    nptorch = NpTorch(True, False)
    startTime = time.time()
    At = nptorch.convertFromNumpy(A)
    B = At @ At
    print('Torch on CPU took {:.6f} seconds [0,0]={}'.format(time.time() - startTime, B[0,0]))
    
    nptorch = NpTorch(False, True)
    At = nptorch.convertFromNumpy(A)  # make it single precision
    startTime = time.time()
    Bt = At @ At
    print('Torch on GPU took {:.6f} seconds [0,0]={}'.format(time.time() - startTime, Bt[0,0]))

    nptorch = NpTorch(False, True)
    startTime = time.time()
    At = nptorch.convertFromNumpy(A)  # make it single precision
    Bt = At @ At
    B = Bt.cpu()
    print('Torch on GPU with communication took {:.6f} seconds [0,0]={}'.format(time.time() - startTime, B[0,0]))
Overwriting nptorch.py
In [8]:
!nvidia-smi
Wed Mar 21 12:52:29 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.25                 Driver Version: 390.25                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  TITAN Xp            Off  | 00000000:02:00.0 Off |                  N/A |
| 23%   34C    P8     9W / 250W |   2299MiB / 12196MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  NVS 315             Off  | 00000000:03:00.0 N/A |                  N/A |
| 32%   49C    P0    N/A /  N/A |    399MiB /   961MiB |     N/A      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0     31802      C   ...ons/e/fac/anderson/anaconda3/bin/python  2287MiB |
|    1                    Not Supported                                       |
+-----------------------------------------------------------------------------+
In [9]:
%run nptorch.py
----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=False useCuda=False
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.tanh(AA) = 
[[0.        0.7615942 0.9640276]
 [0.9950548 0.9993293 0.9999092]]
----------------------------------------------
  nptorch.trans(AA) = 
[[0. 3.]
 [1. 4.]
 [2. 5.]]
----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 
[[0. 1. 2. 3. 4. 5.]]
----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 [0. 1. 2. 3. 4. 5. 0. 1. 2. 3. 4. 5.]
----------------------------------------------
  nptorch.argmax((AA,1))
 [2 2]
----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=True useCuda=False
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.FloatTensor of size 2x3]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.FloatTensor of size 2x3]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.FloatTensor of size 3x2]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.FloatTensor of size 1x6]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.FloatTensor of size 12]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.LongTensor of size 2]

----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=False useCuda=True
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.cuda.FloatTensor of size 3x2 (GPU 0)]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.cuda.FloatTensor of size 1x6 (GPU 0)]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.cuda.FloatTensor of size 12 (GPU 0)]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.cuda.LongTensor of size 2 (GPU 0)]

----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=True useCuda=True
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.cuda.FloatTensor of size 3x2 (GPU 0)]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.cuda.FloatTensor of size 1x6 (GPU 0)]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.cuda.FloatTensor of size 12 (GPU 0)]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.cuda.LongTensor of size 2 (GPU 0)]

----------------------------------------------
Big matrix multiply. A @ A for A 10000x10000
Numpy took 4.168096 seconds [0,0]=-6.550509397129645e-07
Torch on CPU took 3.649840 seconds [0,0]=-6.550509397129645e-07
Torch on GPU took 0.000080 seconds [0,0]=-6.550495186274929e-07
Torch on GPU with communication took 0.464708 seconds [0,0]=-6.550495186274929e-07
In [10]:
%run nptorch.py
----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=False useCuda=False
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.tanh(AA) = 
[[0.        0.7615942 0.9640276]
 [0.9950548 0.9993293 0.9999092]]
----------------------------------------------
  nptorch.trans(AA) = 
[[0. 3.]
 [1. 4.]
 [2. 5.]]
----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 
[[0. 1. 2. 3. 4. 5.]]
----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 [0. 1. 2. 3. 4. 5. 0. 1. 2. 3. 4. 5.]
----------------------------------------------
  nptorch.argmax((AA,1))
 [2 2]
----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=True useCuda=False
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.FloatTensor of size 2x3]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.FloatTensor of size 2x3]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.FloatTensor of size 3x2]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.FloatTensor of size 1x6]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.FloatTensor of size 12]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.LongTensor of size 2]

----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=False useCuda=True
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.cuda.FloatTensor of size 3x2 (GPU 0)]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.cuda.FloatTensor of size 1x6 (GPU 0)]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.cuda.FloatTensor of size 12 (GPU 0)]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.cuda.LongTensor of size 2 (GPU 0)]

----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=True useCuda=True
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.cuda.FloatTensor of size 3x2 (GPU 0)]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.cuda.FloatTensor of size 1x6 (GPU 0)]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.cuda.FloatTensor of size 12 (GPU 0)]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.cuda.LongTensor of size 2 (GPU 0)]

----------------------------------------------
Big matrix multiply. A @ A for A 10000x10000
Numpy took 4.093786 seconds [0,0]=-2.67882683147036e-07
Torch on CPU took 3.776025 seconds [0,0]=-2.67882683147036e-07
Torch on GPU took 0.000069 seconds [0,0]=-2.678823989299417e-07
Torch on GPU with communication took 0.463620 seconds [0,0]=-2.678823989299417e-07
In [11]:
%run nptorch.py
----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=False useCuda=False
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.tanh(AA) = 
[[0.        0.7615942 0.9640276]
 [0.9950548 0.9993293 0.9999092]]
----------------------------------------------
  nptorch.trans(AA) = 
[[0. 3.]
 [1. 4.]
 [2. 5.]]
----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 
[[0. 1. 2. 3. 4. 5.]]
----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 [0. 1. 2. 3. 4. 5. 0. 1. 2. 3. 4. 5.]
----------------------------------------------
  nptorch.argmax((AA,1))
 [2 2]
----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=True useCuda=False
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.FloatTensor of size 2x3]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.FloatTensor of size 2x3]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.FloatTensor of size 3x2]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.FloatTensor of size 1x6]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.FloatTensor of size 12]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.LongTensor of size 2]

----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=False useCuda=True
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.cuda.FloatTensor of size 3x2 (GPU 0)]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.cuda.FloatTensor of size 1x6 (GPU 0)]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.cuda.FloatTensor of size 12 (GPU 0)]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.cuda.LongTensor of size 2 (GPU 0)]

----------------------------------------------
A =
 [[0 1 2]
 [3 4 5]]
----------------------------------------------
----------------------------------------------
useTorch=True useCuda=True
----------------------------------------------
----------------------------------------------
  nptorch.convertFromNumpy(A)
 
 0  1  2
 3  4  5
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.tanh(AA) = 

 0.0000  0.7616  0.9640
 0.9951  0.9993  0.9999
[torch.cuda.FloatTensor of size 2x3 (GPU 0)]

----------------------------------------------
  nptorch.trans(AA) = 

 0  3
 1  4
 2  5
[torch.cuda.FloatTensor of size 3x2 (GPU 0)]

----------------------------------------------
  nptorch.reshape(AA, (1, 6)) = 

 0  1  2  3  4  5
[torch.cuda.FloatTensor of size 1x6 (GPU 0)]

----------------------------------------------
  nptorch.convertToNumpy(AA)
 [[0. 1. 2.]
 [3. 4. 5.]]
----------------------------------------------
  nptorch.pack((AA,AA))
 
 0
 1
 2
 3
 4
 5
 0
 1
 2
 3
 4
 5
[torch.cuda.FloatTensor of size 12 (GPU 0)]

----------------------------------------------
  nptorch.argmax((AA,1))
 
 2
 2
[torch.cuda.LongTensor of size 2 (GPU 0)]

----------------------------------------------
Big matrix multiply. A @ A for A 10000x10000
Numpy took 4.206856 seconds [0,0]=-2.629718096613942e-07
Torch on CPU took 3.461764 seconds [0,0]=-2.629718096613942e-07
Torch on GPU took 0.000071 seconds [0,0]=-2.629715822877188e-07
Torch on GPU with communication took 0.464186 seconds [0,0]=-2.629715822877188e-07
In [ ]:
 
In [ ]: