|
| 1 | +from __future__ import division |
| 2 | +from __future__ import print_function |
| 3 | + |
| 4 | +''' |
| 5 | +From PyTorch: |
| 6 | +
|
| 7 | +Copyright (c) 2016- Facebook, Inc (Adam Paszke) |
| 8 | +Copyright (c) 2014- Facebook, Inc (Soumith Chintala) |
| 9 | +Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) |
| 10 | +Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu) |
| 11 | +Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu) |
| 12 | +Copyright (c) 2011-2013 NYU (Clement Farabet) |
| 13 | +Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston) |
| 14 | +Copyright (c) 2006 Idiap Research Institute (Samy Bengio) |
| 15 | +Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz) |
| 16 | +
|
| 17 | +From Caffe2: |
| 18 | +
|
| 19 | +Copyright (c) 2016-present, Facebook Inc. All rights reserved. |
| 20 | +
|
| 21 | +All contributions by Facebook: |
| 22 | +Copyright (c) 2016 Facebook Inc. |
| 23 | +
|
| 24 | +All contributions by Google: |
| 25 | +Copyright (c) 2015 Google Inc. |
| 26 | +All rights reserved. |
| 27 | +
|
| 28 | +All contributions by Yangqing Jia: |
| 29 | +Copyright (c) 2015 Yangqing Jia |
| 30 | +All rights reserved. |
| 31 | +
|
| 32 | +All contributions from Caffe: |
| 33 | +Copyright(c) 2013, 2014, 2015, the respective contributors |
| 34 | +All rights reserved. |
| 35 | +
|
| 36 | +All other contributions: |
| 37 | +Copyright(c) 2015, 2016 the respective contributors |
| 38 | +All rights reserved. |
| 39 | +
|
| 40 | +Caffe2 uses a copyright model similar to Caffe: each contributor holds |
| 41 | +copyright over their contributions to Caffe2. The project versioning records |
| 42 | +all such contribution and copyright details. If a contributor wants to further |
| 43 | +mark their specific copyright on a particular contribution, they should |
| 44 | +indicate their copyright solely in the commit message of the change when it is |
| 45 | +committed. |
| 46 | +
|
| 47 | +All rights reserved. |
| 48 | +''' |
| 49 | + |
| 50 | +"""Tests for rn50.""" |
| 51 | + |
| 52 | +import math |
| 53 | +import random |
| 54 | +import unittest |
| 55 | +from functools import reduce |
| 56 | + |
| 57 | +import torch |
| 58 | +import torch.nn as nn |
| 59 | +from torch.jit._recursive import wrap_cpp_module |
| 60 | +import copy |
| 61 | + |
| 62 | +import intel_pytorch_extension |
| 63 | +from intel_pytorch_extension import core |
| 64 | + |
| 65 | +import torch.nn as nn |
| 66 | +import torch.backends.cudnn as cudnn |
| 67 | +from torch.nn import Parameter |
| 68 | +import torch.nn.functional as F |
| 69 | +from torch.autograd import gradcheck |
| 70 | +from torch.autograd.gradcheck import gradgradcheck |
| 71 | +from torch._six import inf, nan |
| 72 | + |
| 73 | +from common_utils import TestCase, iter_indices, TEST_NUMPY, TEST_SCIPY, TEST_MKL, \ |
| 74 | + TEST_LIBROSA, run_tests, download_file, skipIfNoLapack, suppress_warnings, \ |
| 75 | + IS_WINDOWS, PY3, NO_MULTIPROCESSING_SPAWN, do_test_dtypes, do_test_empty_full, \ |
| 76 | + IS_SANDCASTLE, load_tests, brute_pdist, brute_cdist, slowTest, \ |
| 77 | + skipCUDANonDefaultStreamIf, skipCUDAMemoryLeakCheckIf |
| 78 | + |
| 79 | +device = 'dpcpp:0' |
| 80 | +#device = 'cpu:0' |
| 81 | +SIZE = 100 |
| 82 | + |
| 83 | +torch._C._jit_set_profiling_mode(False) |
| 84 | +torch._C._jit_set_profiling_executor(False) |
| 85 | + |
| 86 | +def test_output(model, x): |
| 87 | + modelName = model.__class__.__name__ |
| 88 | + core.disable_jit() |
| 89 | + |
| 90 | + model = model.to('dpcpp').eval() |
| 91 | + x = x.to('dpcpp') |
| 92 | + with torch.no_grad(): |
| 93 | + result = model(x) |
| 94 | + |
| 95 | + smodel = torch.jit.script(model) |
| 96 | + smodel.eval() |
| 97 | + with torch.no_grad(): |
| 98 | + sresult = smodel(x) |
| 99 | + |
| 100 | + print(f'\nAre {modelName} and Scripted{modelName} outputs the same: ', |
| 101 | + torch.allclose( |
| 102 | + sresult, result, rtol=1e-05, atol=1e-06, equal_nan=False)) |
| 103 | + |
| 104 | + core.enable_jit() |
| 105 | + pmodel = torch.jit.script(model) |
| 106 | + # bn folding |
| 107 | + pmodel = wrap_cpp_module(torch._C._jit_pass_fold_convbn(pmodel._c)) |
| 108 | + with torch.no_grad(): |
| 109 | + # conv relu fusion, conv sum fusion or conv sum relu fusion |
| 110 | + print(pmodel.graph_for(x)) |
| 111 | + presult = pmodel(x) |
| 112 | + |
| 113 | + # print(result) |
| 114 | + # print(sresult) |
| 115 | + # print(presult) |
| 116 | + |
| 117 | + print(f'\nWith or without pyrys, are Scripted{modelName} outputs the same: ', |
| 118 | + torch.allclose( |
| 119 | + sresult, presult, rtol=1e-05, atol=1e-06, equal_nan=False)) |
| 120 | + |
| 121 | +class Conv2dRelu_Fixed(nn.Module): |
| 122 | + def __init__(self, in_channels, out_channels, **kwargs): |
| 123 | + super(Conv2dRelu_Fixed, self).__init__() |
| 124 | + seed = 2018 |
| 125 | + torch.manual_seed(seed) |
| 126 | + self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) |
| 127 | + |
| 128 | + def forward(self, x): |
| 129 | + return F.relu(self.conv(x), inplace=True) |
| 130 | + |
| 131 | +class CascadedConv2dBnSumRelu(nn.Module): |
| 132 | + def __init__(self, in_channels, mid_channels, out_channels, **kwargs): |
| 133 | + super(CascadedConv2dBnSumRelu, self).__init__() |
| 134 | + torch.manual_seed(2018) |
| 135 | + self.conv = nn.Conv2d(in_channels, mid_channels, bias=False, **kwargs) |
| 136 | + self.conv1 = nn.Conv2d( |
| 137 | + mid_channels, out_channels, bias=False, padding=1, **kwargs) |
| 138 | + self.conv2 = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) |
| 139 | + self.bn = nn.BatchNorm2d(mid_channels, eps=0.001) |
| 140 | + self.bn1 = nn.BatchNorm2d(out_channels, eps=0.001) |
| 141 | + self.bn2 = nn.BatchNorm2d(out_channels, eps=0.001) |
| 142 | + |
| 143 | + def forward(self, x): |
| 144 | + a = self.conv(x) |
| 145 | + a = self.bn(a) |
| 146 | + a = F.relu(a, inplace=True) |
| 147 | + a = self.conv1(a) |
| 148 | + a = self.bn1(a) |
| 149 | + b = self.conv2(x) |
| 150 | + b = self.bn2(b) |
| 151 | + return F.relu(a.add_(b), inplace=True) |
| 152 | + |
| 153 | +class Tester(TestCase): |
| 154 | + n = 32 |
| 155 | + c = 3 |
| 156 | + h = 224 |
| 157 | + w = 224 |
| 158 | + print('input size: (%d, %d, %d, %d)' % (n, c, h, w)) |
| 159 | + |
| 160 | + def test_output_conv_relu(self): |
| 161 | + test_output( |
| 162 | + Conv2dRelu_Fixed(self.c, 32, kernel_size=3, stride=1), |
| 163 | + torch.rand(self.n, self.c, self.h, self.w)) |
| 164 | + |
| 165 | + def test_output_cascaded_conv2d_bn_sum_relu(self): |
| 166 | + test_output( |
| 167 | + CascadedConv2dBnSumRelu(self.c, 64, 32, kernel_size=3, stride=1), |
| 168 | + torch.rand(self.n, self.c, self.h, self.w)) |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + core.enable_auto_dnnl() |
| 172 | + test = unittest.main() |
0 commit comments