unit_test.py 2.44 KB
Newer Older
Hang Zhang's avatar
test  
Hang Zhang committed
1
2
3
4
5
6
7
8
9
10
11
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: Hang Zhang
## ECE Department, Rutgers University
## Email: zhang.hang@rutgers.edu
## Copyright (c) 2017
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree 
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import encoding
Hang Zhang's avatar
sync BN  
Hang Zhang committed
12
13
import unittest

Hang Zhang's avatar
test  
Hang Zhang committed
14
15
16
17
import torch
import torch.nn.functional as F
from torch.autograd import Variable, gradcheck

Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
18
19
20
21
import torchvision.models as models

EPS = 1e-6

Hang Zhang's avatar
Hang Zhang committed
22
def test_aggregate():
Hang Zhang's avatar
test  
Hang Zhang committed
23
24
25
26
27
28
29
30
    B,N,K,D = 2,3,4,5
    A = Variable(torch.cuda.DoubleTensor(B,N,K).uniform_(-0.5,0.5), 
        requires_grad=True)
    X = Variable(torch.cuda.DoubleTensor(B,N,D).uniform_(-0.5,0.5), 
        requires_grad=True)
    C = Variable(torch.cuda.DoubleTensor(K,D).uniform_(-0.5,0.5), 
        requires_grad=True)
    input = (A, X, C)
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
31
    test = gradcheck(encoding.functions.aggregate, input, eps=1e-6, atol=1e-4)
Hang Zhang's avatar
Hang Zhang committed
32
    print('Testing aggregate(): {}'.format(test))
Hang Zhang's avatar
test  
Hang Zhang committed
33
34


Hang Zhang's avatar
Hang Zhang committed
35
def test_scaledL2():
Hang Zhang's avatar
test  
Hang Zhang committed
36
37
38
39
40
41
42
43
    B,N,K,D = 2,3,4,5
    X = Variable(torch.cuda.DoubleTensor(B,N,D).uniform_(-0.5,0.5), 
        requires_grad=True)
    C = Variable(torch.cuda.DoubleTensor(K,D).uniform_(-0.5,0.5), 
        requires_grad=True)
    S = Variable(torch.cuda.DoubleTensor(K).uniform_(-0.5,0.5), 
        requires_grad=True)
    input = (X, C, S)
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
44
    test = gradcheck(encoding.functions.scaledL2, input, eps=1e-6, atol=1e-4)
Hang Zhang's avatar
Hang Zhang committed
45
    print('Testing scaledL2(): {}'.format(test))
Hang Zhang's avatar
test  
Hang Zhang committed
46
47
48
49
50
51
52


def test_encoding():
    B,C,H,W,K = 2,3,4,5,6
    X = Variable(torch.cuda.DoubleTensor(B,C,H,W).uniform_(-0.5,0.5), 
        requires_grad=True)
    input = (X,)
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
53
    layer = encoding.nn.Encoding(C,K).double().cuda()
Hang Zhang's avatar
test  
Hang Zhang committed
54
55
    test = gradcheck(layer, input, eps=1e-6, atol=1e-4)
    print('Testing encoding(): {}'.format(test))
Hang Zhang's avatar
sync BN  
Hang Zhang committed
56

Hang Zhang's avatar
test  
Hang Zhang committed
57
58
59
60
61
62

def test_sum_square():
    B,C,H,W = 2,3,4,5
    X = Variable(torch.cuda.DoubleTensor(B,C,H,W).uniform_(-0.5,0.5), 
        requires_grad=True)
    input = (X,)
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
63
    test = gradcheck(encoding.functions.sum_square, input, eps=1e-6, atol=1e-4)
Hang Zhang's avatar
test  
Hang Zhang committed
64
65
66
    print('Testing sum_square(): {}'.format(test))


Hang Zhang's avatar
sync BN  
Hang Zhang committed
67
68
69
70
71
def test_all_reduce():
    ngpu = torch.cuda.device_count()
    X = [torch.DoubleTensor(2,4,4).uniform_(-0.5,0.5).cuda(i) for i in range(ngpu)]
    for x in X:
        x.requires_grad = True
Hang Zhang's avatar
Hang Zhang committed
72
    Y = encoding.parallel.allreduce(1, *X)
Hang Zhang's avatar
sync BN  
Hang Zhang committed
73
    assert (len(X) == len(Y))
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
74
75


Hang Zhang's avatar
test  
Hang Zhang committed
76
if __name__ == '__main__':
Hang Zhang's avatar
sync BN  
Hang Zhang committed
77
78
    import nose
    nose.runmodule()