1 from __future__ 
import absolute_import
     2 from __future__ 
import division
     3 from __future__ 
import print_function
     4 from __future__ 
import unicode_literals
     6 from functools 
import wraps
    18 from debug_embed_params 
import run_embed_params
    22 from torchvision.models.alexnet 
import alexnet
    23 from torchvision.models.inception 
import inception_v3
    24 from torchvision.models.densenet 
import densenet121
    25 from torchvision.models.resnet 
import resnet50
    26 from torchvision.models.vgg 
import vgg16, vgg16_bn, vgg19, vgg19_bn
    40 from test_pytorch_common 
import skipIfTravis, skipIfNoLapack, skipIfNoCuda
    46 def skipIfEmbed(func):
    49             raise unittest.SkipTest(
"Skip embed_params verify test")
    75 def do_export(model, inputs, *args, **kwargs):
    78     return f.getvalue(), out
    85     print(
'Cannot import torch, hence caffe2-torch test will not run.')
    92 RNN_SEQUENCE_LENGTH = 11
    97     'alexnet': 
'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
    98     'dcgan_b': 
'https://s3.amazonaws.com/pytorch/test_data/export/netG_bedroom_epoch_1-0649e76b.pth',
    99     'dcgan_f': 
'https://s3.amazonaws.com/pytorch/test_data/export/netG_faces_epoch_49-d86035a6.pth',
   100     'densenet121': 
'https://download.pytorch.org/models/densenet121-d66d3027.pth',
   101     'inception_v3_google': 
'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
   102     'resnet50': 
'https://download.pytorch.org/models/resnet50-19c8e357.pth',
   103     'srresNet': 
'https://s3.amazonaws.com/pytorch/demos/srresnet-e10b2039.pth',
   104     'super_resolution': 
'https://s3.amazonaws.com/pytorch/test_data/export/superres_epoch100-44c6958e.pth',
   105     'squeezenet1_0': 
'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth',
   106     'squeezenet1_1': 
'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth',
   107     'vgg16': 
'https://download.pytorch.org/models/vgg16-397923af.pth',
   108     'vgg19': 
'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
   118             torch.cuda.manual_seed_all(0)
   119         np.random.seed(seed=0)
   121     def convert_cuda(self, model, input):
   122         cuda_model = model.cuda()
   124         cuda_input = function._nested_map(
   126             lambda o: o.cuda())(input)
   127         return cuda_model, cuda_input
   129     def run_debug_test(self, model, train, batch_size, state_dict=None,
   130                        input=
None, use_gpu=
True, example_outputs=
None):
   132         # TODO: remove this from the final release version   133         This test is for our debugging only for the case where   138         if state_dict 
is not None:
   139             model.load_state_dict(state_dict)
   143             input = torch.randn(batch_size, 3, 224, 224, requires_grad=
True)
   147         onnxir, torch_out = do_export(model, input, export_params=self.
embed_params, verbose=
False,
   148                                       example_outputs=example_outputs)
   149         if isinstance(torch_out, torch.autograd.Variable):
   150             torch_out = (torch_out,)
   152         caffe2_out = run_embed_params(onnxir, model, input, state_dict, use_gpu)
   153         for _, (x, y) 
in enumerate(zip(torch_out, caffe2_out)):
   154             np.testing.assert_almost_equal(x.data.cpu().numpy(), y, decimal=3)
   157                         input=
None, use_gpu=
True, rtol=0.001, atol=1e-7,
   158                         example_outputs=
None):
   160         This is what the user facing version will look like   166         if state_dict 
is not None:
   167             model.load_state_dict(state_dict)
   171             input = torch.randn(batch_size, 3, 224, 224, requires_grad=
True)
   179     def run_model_test(self, model, train, batch_size, state_dict=None,
   180                        input=
None, use_gpu=
True, rtol=0.001, atol=1e-7,
   181                        example_outputs=
None):
   185                                  use_gpu=use_gpu_, rtol=rtol, atol=atol,
   186                                  example_outputs=example_outputs)
   189                                 use_gpu=use_gpu_, example_outputs=example_outputs)
   191     def test_linear(self):
   192         class MyModel(torch.nn.Module):
   194                 super(MyModel, self).__init__()
   196                     nn.Linear(4, 5, bias=
True),
   197                     nn.ReLU(inplace=
True),
   198                     nn.Linear(5, 6, bias=
True),
   199                     nn.ReLU(inplace=
True),
   200                     nn.Linear(6, 7, bias=
True),
   203             def forward(self, input):
   207         input = torch.randn(3, 4, requires_grad=
True)
   208         self.
run_model_test(model, train=
False, batch_size=0, input=input)
   210     def test_lstm_cell(self):
   211         model = nn.LSTMCell(RNN_INPUT_SIZE, RNN_HIDDEN_SIZE)
   212         input = torch.randn(BATCH_SIZE, RNN_INPUT_SIZE)
   213         h0 = torch.randn(BATCH_SIZE, RNN_HIDDEN_SIZE)
   214         c0 = torch.randn(BATCH_SIZE, RNN_HIDDEN_SIZE)
   215         self.
run_model_test(model, train=
False, batch_size=BATCH_SIZE, input=(input, (h0, c0)), use_gpu=
False)
   217     def test_gru_cell(self):
   218         model = nn.GRUCell(RNN_INPUT_SIZE, RNN_HIDDEN_SIZE)
   219         input = torch.randn(BATCH_SIZE, RNN_INPUT_SIZE)
   220         h0 = torch.randn(BATCH_SIZE, RNN_HIDDEN_SIZE)
   221         self.
run_model_test(model, train=
False, batch_size=BATCH_SIZE, input=(input, h0), use_gpu=
False)
   223     def _dispatch_rnn_test(self, name, *args, **kwargs):
   231     def _elman_rnn_test(self, layers, nonlinearity, bidirectional,
   232                         initial_state, packed_sequence, dropout):
   233         model = nn.RNN(RNN_INPUT_SIZE, RNN_HIDDEN_SIZE,
   235                        nonlinearity=nonlinearity,
   236                        bidirectional=bidirectional,
   239         if packed_sequence == 1:
   241         if packed_sequence == 2:
   244         def make_input(batch_size):
   245             seq_lengths = np.random.randint(1, RNN_SEQUENCE_LENGTH + 1, size=batch_size)
   246             seq_lengths = list(reversed(sorted(map(int, seq_lengths))))
   247             inputs = [torch.randn(l, RNN_INPUT_SIZE) 
for l 
in seq_lengths]
   248             inputs = rnn_utils.pad_sequence(inputs)
   249             if packed_sequence == 2:
   250                 inputs = inputs.transpose(0, 1)
   253             directions = 2 
if bidirectional 
else 1
   256                 h0 = torch.randn(directions * layers, batch_size, RNN_HIDDEN_SIZE)
   258             if packed_sequence != 0:
   259                 inputs.append(torch.IntTensor(seq_lengths))
   263                 input = tuple(inputs)
   266         input = make_input(RNN_BATCH_SIZE)
   267         self.
run_model_test(model, train=
False, batch_size=RNN_BATCH_SIZE, input=input, use_gpu=
False, atol=1e-7)
   270         onnxir, _ = do_export(model, input)
   271         other_input = make_input(RNN_BATCH_SIZE + 1)
   272         _ = run_embed_params(onnxir, model, other_input, use_gpu=
False)
   274     def _lstm_test(self, layers, bidirectional, initial_state,
   275                    packed_sequence, dropout):
   277             RNN_INPUT_SIZE, RNN_HIDDEN_SIZE, layers,
   278             bidirectional=bidirectional, dropout=dropout)
   279         if packed_sequence == 1:
   281         if packed_sequence == 2:
   284         def make_input(batch_size):
   285             seq_lengths = np.random.randint(1, RNN_SEQUENCE_LENGTH + 1, size=batch_size)
   286             seq_lengths = list(reversed(sorted(map(int, seq_lengths))))
   287             inputs = [torch.randn(l, RNN_INPUT_SIZE) 
for l 
in seq_lengths]
   288             inputs = rnn_utils.pad_sequence(inputs)
   289             if packed_sequence == 2:
   290                 inputs = inputs.transpose(0, 1)
   293             directions = 2 
if bidirectional 
else 1
   296                 h0 = torch.randn(directions * layers, batch_size, RNN_HIDDEN_SIZE)
   297                 c0 = torch.randn(directions * layers, batch_size, RNN_HIDDEN_SIZE)
   298                 inputs.append((h0, c0))
   299             if packed_sequence != 0:
   300                 inputs.append(torch.IntTensor(seq_lengths))
   304                 input = tuple(inputs)
   307         input = make_input(RNN_BATCH_SIZE)
   308         self.
run_model_test(model, train=
False, batch_size=RNN_BATCH_SIZE, input=input, use_gpu=
False)
   311         onnxir, _ = do_export(model, input)
   312         other_input = make_input(RNN_BATCH_SIZE + 1)
   313         _ = run_embed_params(onnxir, model, other_input, use_gpu=
False)
   315     def _gru_test(self, layers, bidirectional, initial_state,
   316                   packed_sequence, dropout):
   317         model = nn.GRU(RNN_INPUT_SIZE, RNN_HIDDEN_SIZE, layers,
   318                        bidirectional=bidirectional, dropout=dropout)
   319         if packed_sequence == 1:
   321         if packed_sequence == 2:
   324         def make_input(batch_size):
   325             seq_lengths = np.random.randint(1, RNN_SEQUENCE_LENGTH + 1, size=batch_size)
   326             seq_lengths = list(reversed(sorted(map(int, seq_lengths))))
   327             inputs = [torch.randn(l, RNN_INPUT_SIZE) 
for l 
in seq_lengths]
   328             inputs = rnn_utils.pad_sequence(inputs)
   329             if packed_sequence == 2:
   330                 inputs = inputs.transpose(0, 1)
   333             directions = 2 
if bidirectional 
else 1
   336                 h0 = torch.randn(directions * layers, batch_size, RNN_HIDDEN_SIZE)
   338             if packed_sequence != 0:
   339                 inputs.append(torch.IntTensor(seq_lengths))
   343                 input = tuple(inputs)
   346         input = make_input(RNN_BATCH_SIZE)
   347         self.
run_model_test(model, train=
False, batch_size=RNN_BATCH_SIZE, input=input, use_gpu=
False)
   350         onnxir, _ = do_export(model, input)
   351         other_input = make_input(RNN_BATCH_SIZE + 1)
   352         _ = run_embed_params(onnxir, model, other_input, use_gpu=
False)
   354     def test_rnn_init_predict_split(self):
   355         model = nn.LSTM(RNN_INPUT_SIZE, RNN_HIDDEN_SIZE, 3, bidirectional=
True)
   356         seq_lengths = np.random.randint(1, RNN_SEQUENCE_LENGTH + 1, size=7)
   357         seq_lengths = list(reversed(sorted(map(int, seq_lengths))))
   358         input = [torch.randn(l, RNN_INPUT_SIZE) 
for l 
in seq_lengths]
   359         input = rnn_utils.pad_sequence(input)
   364         mp = onnx.ModelProto.FromString(do_export(model, input, export_params=self.
embed_params)[0])
   365         prepared = c2.prepare(mp, device=
'CPU')
   367             assert len(prepared.init_net.op) == 875
   368             assert len(prepared.predict_net.op) == 130
   370             assert len(prepared.init_net.op) == 8
   371             assert len(prepared.predict_net.op) == 997
   373     def test_alexnet(self):
   374         state_dict = model_zoo.load_url(model_urls[
'alexnet'], progress=
False)
   375         self.
run_model_test(alexnet(), train=
False, batch_size=BATCH_SIZE,
   376                             state_dict=state_dict, atol=1e-3)
   379     def test_dcgan(self):
   384             torch.cuda.manual_seed_all(1)
   386         netD = dcgan._netD(1)
   387         netD.apply(dcgan.weights_init)
   388         input = torch.randn(BATCH_SIZE, 3, dcgan.imgsz, dcgan.imgsz)
   392         netG = dcgan._netG(1)
   393         netG.apply(dcgan.weights_init)
   394         state_dict = model_zoo.load_url(model_urls[
'dcgan_b'], progress=
False)
   396         noise = torch.randn(BATCH_SIZE, dcgan.nz, 1, 1).normal_(0, 1)
   398                             input=noise, state_dict=state_dict, rtol=1e-2, atol=1e-6)
   401                      "model on net has cuda in it, awaiting fix")
   402     def test_densenet(self):
   403         state_dict = model_zoo.load_url(model_urls[
'densenet121'], progress=
False)
   404         self.
run_model_test(densenet121(), train=
False, batch_size=BATCH_SIZE,
   405                             state_dict=state_dict, atol=1e-7)
   407     @skip(
"doesn't match exactly...")
   409     def test_inception(self):
   410         x = torch.randn(BATCH_SIZE, 3, 299, 299, requires_grad=
True)
   413         self.
run_model_test(inception_v3(), train=
False, batch_size=BATCH_SIZE,
   414                             state_dict=state_dict, input=x)
   416     def test_resnet(self):
   417         state_dict = model_zoo.load_url(model_urls[
'resnet50'], progress=
False)
   418         self.
run_model_test(resnet50(), train=
False, batch_size=BATCH_SIZE,
   419                             state_dict=state_dict, atol=1e-6)
   421     def test_squeezenet(self):
   423         state_dict = model_zoo.load_url(model_urls[
'squeezenet1_1'], progress=
False)
   425         self.
run_model_test(sqnet_v1_1, train=
False, batch_size=BATCH_SIZE,
   426                             state_dict=state_dict)
   430     @unittest.skip(
"This model takes too much memory")
   431     def test_srresnet(self):
   433             rescale_factor=4, n_filters=64, n_blocks=8)
   434         state_dict = model_zoo.load_url(model_urls[
'srresNet'], progress=
False)
   435         x = torch.randn(1, 3, 224, 224, requires_grad=
True)
   437                             batch_size=1, state_dict=state_dict,
   438                             input=x, use_gpu=
False)
   443     def test_super_resolution(self):
   445         state_dict = model_zoo.load_url(model_urls[
'super_resolution'], progress=
False)
   446         x = torch.randn(1, 1, 224, 224, requires_grad=
True)
   448                             batch_size=BATCH_SIZE, state_dict=state_dict,
   449                             input=x, use_gpu=
False, atol=1e-6)
   451     @unittest.skip(
"This model takes too much memory")
   452     def test_vgg16(self):
   453         state_dict = model_zoo.load_url(model_urls[
'vgg16'], progress=
False)
   455                             state_dict=state_dict)
   457     @skip(
"disable to run tests faster...")
   458     def test_vgg16_bn(self):
   460                             batch_size=BATCH_SIZE)
   462     @skip(
"disable to run tests faster...")
   463     def test_vgg19(self):
   464         state_dict = model_zoo.load_url(model_urls[
'vgg19'], progress=
False)
   466                             state_dict=state_dict)
   468     @skip(
"disable to run tests faster...")
   469     def test_vgg19_bn(self):
   471                             batch_size=BATCH_SIZE)
   473     def run_word_language_model(self, model_name):
   481         model = word_language_model.RNNModel(model_name, ntokens, emsize,
   482                                              nhid, nlayers, dropout, tied,
   484         x = torch.arange(0, ntokens).long().view(-1, batchsize)
   487                             batch_size=batchsize, use_gpu=
False)
   489     def test_word_language_model_RNN_TANH(self):
   492     def test_word_language_model_RNN_RELU(self):
   495     def test_word_language_model_LSTM(self):
   498     def test_word_language_model_GRU(self):
   501     def test_batchnorm1d_special(self):
   502         c = torch.randn(BATCH_SIZE, 224)
   503         model = nn.BatchNorm1d(224)
   504         self.
run_model_test(model, train=
True, input=c, batch_size=BATCH_SIZE)
   506     def test_batchnorm2d_noaffine(self):
   507         c = torch.randn(128, 128, 1, 1)
   508         model = nn.BatchNorm2d(128, affine=
False)
   509         self.
run_model_test(model, train=
False, input=c, batch_size=BATCH_SIZE)
   511     def test_constant(self):
   512         c = torch.randn(BATCH_SIZE, 3, 224, 224)
   514         class MyModel(torch.nn.Module):
   516                 super(MyModel, self).__init__()
   518             def forward(self, input):
   519                 return input + c.type_as(input)
   521         self.
run_model_test(MyModel(), train=
False, batch_size=BATCH_SIZE)
   523     def test_consumed_bn(self):
   524         underlying = nn.BatchNorm2d(3)
   525         self.
run_model_test(underlying, train=
True, batch_size=BATCH_SIZE)
   527     def _test_index_generic(self, fn):
   528         class MyModel(torch.nn.Module):
   530                 super(MyModel, self).__init__()
   532             def forward(self, input):
   535         m1 = torch.randn(3, 4)
   536         self.
run_model_test(MyModel(), input=m1, train=
False, batch_size=BATCH_SIZE)
   538     def test_index_1d(self):
   541     def test_index_2d_1dimslice(self):
   544     def test_index_2d_sliceint(self):
   547     def test_index_2d_neg_slice(self):
   553     def test_index_2d_2dimslice(self):   554         self._test_index_generic(lambda input: input[0:1, 0:1])   557     def test_index_2d_neg_slice2dim(self):   558         self._test_index_generic(lambda input: input[0:-1, 0:-1])   561     def test_chunk(self):
   562         class MyModel(torch.nn.Module):
   564                 super(MyModel, self).__init__()
   566             def forward(self, input):
   569                 return input.chunk(8, dim=2)[-1]
   570         self.
run_model_test(MyModel(), train=
False, batch_size=BATCH_SIZE)
   573         class MyModel(torch.nn.Module):
   575                 super(MyModel, self).__init__()
   577             def forward(self, input):
   579         input = torch.empty(BATCH_SIZE, 10, 10).uniform_(4, 9)
   580         self.
run_model_test(MyModel(), train=
False, input=input, batch_size=BATCH_SIZE)
   583         class MyModel(torch.nn.Module):
   585                 super(MyModel, self).__init__()
   587             def forward(self, input):
   589         input = torch.empty(BATCH_SIZE, 10, 10).uniform_(4, 9)
   590         self.
run_model_test(MyModel(), train=
False, input=input, batch_size=BATCH_SIZE)
   593         class MyModel(torch.nn.Module):
   595                 super(MyModel, self).__init__()
   597             def forward(self, input):
   599         input = torch.empty(BATCH_SIZE, 10, 10).uniform_(4, 9)
   600         self.
run_model_test(MyModel(), train=
False, input=input, batch_size=BATCH_SIZE)
   602     def test_trigonometry(self):
   604             class MyModel(torch.nn.Module):
   606                     super(MyModel, self).__init__()
   608                 def forward(self, input):
   609                     return getattr(input, name)()
   610             input = torch.empty(BATCH_SIZE, 10, 10).uniform_()
   611             self.
run_model_test(MyModel(), train=
False, input=input, batch_size=BATCH_SIZE)
   620     def test_addconstant(self):
   621         class MyModel(torch.nn.Module):
   623                 super(MyModel, self).__init__()
   625             def forward(self, input):
   629         self.
run_model_test(MyModel(), train=
False, batch_size=BATCH_SIZE)
   631     def test_subconstant(self):
   632         class MyModel(torch.nn.Module):
   634                 super(MyModel, self).__init__()
   636             def forward(self, input):
   640         self.
run_model_test(MyModel(), train=
False, batch_size=BATCH_SIZE)
   642     def test_embedding(self):
   643         model = nn.Embedding(10, 3, padding_idx=-1)
   644         input = torch.LongTensor(list(range(10))[::-1])
   645         self.
run_model_test(model, train=
False, input=input, batch_size=BATCH_SIZE)
   647     def test_constantpad2d(self):
   648         model = nn.ConstantPad2d((1, 2, 3, 4), 3.5)
   651     def test_reflectionpad2d(self):
   652         model = nn.ReflectionPad2d((1, 2, 3, 4))
   655     def test_replicationpad2d(self):
   656         model = nn.ReplicationPad2d((1, 2, 3, 4))
   659     def test_maxpool2d(self):
   660         model = nn.MaxPool2d(5, padding=(1, 2))
   663     def test_maxpool2d_single_padding(self):
   664         model = nn.MaxPool2d(5, padding=2)
   667     def test_maxpool1d_ceil(self):
   668         model = nn.MaxPool1d(3, 2, ceil_mode=
True)
   669         x = torch.randn(20, 16, 50, requires_grad=
True)
   670         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   672     def test_maxpool2d_ceil(self):
   673         model = nn.MaxPool2d(3, 2, ceil_mode=
True)
   674         x = torch.randn(20, 16, 50, 32, requires_grad=
True)
   675         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   677     def test_maxpool3d_ceil(self):
   678         model = nn.MaxPool3d(3, 2, ceil_mode=
True)
   679         x = torch.randn(20, 16, 50, 44, 31, requires_grad=
True)
   680         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   682     @unittest.skip(
"C2 and PyTorch have small difference in padding implementation")
   683     def test_avgpool2d(self):
   684         model = nn.AvgPool2d(5, padding=(2))
   687     def test_avgpool2d_with_count_include_pad_set_false(self):
   688         model = nn.AvgPool2d(7, padding=(2), count_include_pad=
False)
   691     def test_avgpool2d_with_count_include_pad_set_true(self):
   692         model = nn.AvgPool2d(7, padding=(2), count_include_pad=
True)
   695     def test_avgpool2d_no_padding(self):
   696         model = nn.AvgPool2d(5)
   699     def test_avg_pool1D_ceil(self):
   700         model = torch.nn.AvgPool1d(3, 2, ceil_mode=
True)
   701         x = torch.randn(1, 1, 7, requires_grad=
True)
   702         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   704     def test_avg_pool2D_ceil(self):
   705         model = torch.nn.AvgPool2d(3, 2, ceil_mode=
True)
   706         x = torch.randn(20, 16, 50, 32, requires_grad=
True)
   707         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   709     def test_avg_pool3D_ceil(self):
   710         model = torch.nn.AvgPool3d(3, 2, ceil_mode=
True)
   711         x = torch.randn(20, 16, 50, 44, 31, requires_grad=
True)
   712         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   714     def test_adaptive_avg_pool1D(self):
   715         model = torch.nn.AdaptiveAvgPool1d((5))
   716         x = torch.randn(20, 16, 50, requires_grad=
True)
   717         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   719     def test_adaptive_avg_pool2D(self):
   720         model = torch.nn.AdaptiveAvgPool2d((5, 4))
   721         x = torch.randn(20, 16, 50, 32, requires_grad=
True)
   722         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   724     def test_adaptive_avg_pool3D(self):
   725         model = torch.nn.AdaptiveAvgPool3d((5, 4, 3))
   726         x = torch.randn(20, 16, 50, 44, 30, requires_grad=
True)
   727         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   729     def test_adaptive_max_pool1D(self):
   730         model = torch.nn.AdaptiveMaxPool1d((5))
   731         x = torch.randn(20, 16, 50, requires_grad=
True)
   732         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   734     def test_adaptive_max_pool2D(self):
   735         model = torch.nn.AdaptiveMaxPool2d((5, 4))
   736         x = torch.randn(20, 16, 50, 32, requires_grad=
True)
   737         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   739     def test_adaptive_max_pool3D(self):
   740         model = torch.nn.AdaptiveMaxPool3d((5, 4, 3))
   741         x = torch.randn(20, 16, 50, 44, 30, requires_grad=
True)
   742         self.
run_model_test(model, train=
False, input=x, batch_size=BATCH_SIZE)
   744     def test_weight_norm(self):
   745         model = nn.utils.weight_norm(nn.Conv1d(1, 1, 3))
   746         input = torch.randn(1, 1, 5, requires_grad=
True)
   748             model, train=
True, batch_size=0, input=input, use_gpu=
False   751     def test_mnist(self):
   753         input = torch.randn(BATCH_SIZE, 1, 28, 28)
   756         self.
run_model_test(model, train=
False, input=input, batch_size=BATCH_SIZE,
   757                             state_dict=state_dict)
   760         class MyModel(torch.nn.Module):
   762                 super(MyModel, self).__init__()
   764             def forward(self, m1, m2):
   765                 return torch.mm(m1, m2)
   766         m1 = torch.randn(3, 4)
   767         m2 = torch.randn(4, 5)
   768         self.
run_model_test(MyModel(), train=
False, input=(m1, m2), batch_size=BATCH_SIZE, use_gpu=
False)
   770     def test_addmm(self):
   771         class MyModel(torch.nn.Module):
   773                 super(MyModel, self).__init__()
   775             def forward(self, ma, m1, m2):
   776                 return torch.addmm(ma, m1, m2)
   778         m1 = torch.randn(3, 4)
   779         m2 = torch.randn(4, 5)
   780         self.
run_model_test(MyModel(), train=
False, input=(ma, m1, m2), batch_size=BATCH_SIZE, use_gpu=
False)
   783     def test_consecutive_transposes(self):
   784         class MyModel(torch.nn.Module):
   786                 super(MyModel, self).__init__()
   788             def forward(self, x):
   789                 return x.transpose(1, 2).transpose(2, 3)
   790         x = torch.randn(5, 6, 7, 8)
   791         self.
run_model_test(MyModel(), train=
False, input=x, batch_size=BATCH_SIZE, use_gpu=
False)
   795         for params 
in [{}] + [{
'dim': i} 
for i 
in range(len(shape))]:
   796             class MyModel(torch.nn.Module):
   798                     super(MyModel, self).__init__()
   800                 def forward(self, x):
   801                     return torch.sum(x, **params)
   802             x = torch.randn(*shape)
   803             self.
run_model_test(MyModel(), train=
False, input=(x), batch_size=BATCH_SIZE, use_gpu=
False)
   805     def test_cumsum(self):
   807         for params 
in [{
'dim': i} 
for i 
in range(len(shape))]:
   808             class MyModel(torch.nn.Module):
   810                     super(MyModel, self).__init__()
   812                 def forward(self, x):
   813                     return torch.cumsum(x, **params)
   814             x = torch.randn(*shape)
   815             self.
run_model_test(MyModel(), train=
False, input=(x), batch_size=BATCH_SIZE, use_gpu=
False)
   817     def test_layer_norm(self):
   818         shape = (20, 5, 10, 10)
   820         class MyModel(torch.nn.Module):
   822                 super(MyModel, self).__init__()
   823                 self.
ln = torch.nn.LayerNorm([5, 10, 10])
   825             def forward(self, x):
   828         x = torch.randn(*shape)
   829         self.
run_model_test(MyModel(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
   831     def test_repeat(self):
   832         class MyModel(torch.nn.Module):
   834                 super(MyModel, self).__init__()
   836             def forward(self, x):
   837                 return x.repeat(1, 2, 3, 4)
   839         x = torch.randn(4, 3, 2, 1, requires_grad=
True)
   840         self.
run_model_test(MyModel(), train=
False, input=(x), batch_size=BATCH_SIZE, use_gpu=
False)
   842     @unittest.skip(
"Temporary - waiting for https://github.com/onnx/onnx/pull/1773.")
   843     def test_upsample(self):
   844         x = torch.randn(1, 2, 3, 4, requires_grad=
True)
   845         model = nn.Upsample(scale_factor=2, mode=
'nearest')
   847                             batch_size=BATCH_SIZE, use_gpu=
False)
   849     def test_repeat_dim_overflow(self):
   850         class MyModel(torch.nn.Module):
   852                 super(MyModel, self).__init__()
   854             def forward(self, x):
   855                 return x.repeat(1, 2, 3, 4)
   857         x = torch.randn(1, 2, requires_grad=
True)
   858         self.
run_model_test(MyModel(), train=
False, input=(x), batch_size=BATCH_SIZE, use_gpu=
False)
   860     def test_repeat_dynamic(self):
   861         class MyModel(torch.nn.Module):
   863                 super(MyModel, self).__init__()
   865             def forward(self, x, y):
   866                 return x.repeat(y.size()[0] / 2, y.size()[1] * 2)
   868         x = torch.randn(1, 2, requires_grad=
True)
   869         y = torch.randn(2, 4, requires_grad=
True)
   870         self.
run_model_test(MyModel(), train=
False, input=(x, y), batch_size=BATCH_SIZE, use_gpu=
False)
   874         for params 
in [{}] + [{
'dim': i} 
for i 
in range(len(shape))]:
   875             class MyModel(torch.nn.Module):
   877                     super(MyModel, self).__init__()
   879                 def forward(self, x):
   880                     return torch.mean(x, **params)
   881             x = torch.randn(*shape)
   882             self.
run_model_test(MyModel(), train=
False, input=(x), batch_size=BATCH_SIZE, use_gpu=
False)
   885     def test_softmax(self):
   886         for i 
in range(7)[2:]:
   887             model = nn.Softmax(dim=i - 1)
   888             dims = [2] * (i - 2) + [3, 4]
   889             input = torch.ones(*dims, requires_grad=
True)
   890             self.
run_model_test(model, train=
False, batch_size=BATCH_SIZE, input=input)
   892     def test_logsoftmax(self):
   893         for i 
in range(7)[2:]:
   894             model = nn.LogSoftmax(dim=i - 1)
   895             dims = [2] * (i - 2) + [3, 4]
   896             input = torch.ones(*dims, requires_grad=
True)
   897             self.
run_model_test(model, train=
False, batch_size=BATCH_SIZE, input=input)
   899     def test_randn(self):
   900         x = torch.randn(1, 2, 3, 4)
   902         class MyModule(torch.nn.Module):
   903             def forward(self, x):
   904                 return (torch.randn(1, 2, 3, 4) + x).shape
   906                             batch_size=BATCH_SIZE, use_gpu=
False)
   908     def test_convtranspose(self):
   909         model = nn.ConvTranspose2d(3, 3, 3, stride=3, bias=
False, padding=1, output_padding=2)
   910         self.
run_model_test(model, train=
False, batch_size=BATCH_SIZE, atol=1e-7)
   912     def test_unsqueeze(self):
   914         for dim 
in range(len(shape) + 1):
   915             class MyModel(torch.nn.Module):
   917                     super(MyModel, self).__init__()
   919                 def forward(self, x):
   920                     return x.unsqueeze(dim)
   921             x = torch.randn(*shape)
   922             self.
run_model_test(MyModel(), train=
False, input=(x), batch_size=BATCH_SIZE, atol=1e-7)
   927     def test_instance_norm(self):
   928         underlying = nn.InstanceNorm2d(3)
   929         self.
run_model_test(underlying, train=
False, batch_size=BATCH_SIZE)
   931     def test_pixel_shuffle(self):
   932         underlying = nn.PixelShuffle(4)
   933         shape = (1, 64, 5, 5)
   934         input = Variable(torch.randn(*shape),
   937                             batch_size=BATCH_SIZE)
   939     def test_dynamic_sizes(self):
   940         class MyModel(torch.nn.Module):
   942                 super(MyModel, self).__init__()
   944             def forward(self, x):
   946                 new_shape = torch.cat((torch.LongTensor([-1]), shape[0].view(1)))
   948         x = torch.randn(3, 5, 7)
   949         self.
run_model_test(MyModel(), train=
False, input=x, batch_size=BATCH_SIZE, use_gpu=
False)
   951     def test_advanced_broadcast(self):
   952         class MyModel(torch.nn.Module):
   954                 super(MyModel, self).__init__()
   956             def forward(self, x, y):
   957                 return torch.mul(x, y)
   958         x = torch.randn(1, 5, 10)
   959         y = torch.randn(1, 5, 1)
   960         self.
run_model_test(MyModel(), train=
False, input=(x, y), batch_size=BATCH_SIZE, use_gpu=
False)
   962     def test_int8_export(self):
   963         class MyModel(torch.nn.Module):
   965                 super(MyModel, self).__init__()
   966                 self.
param = torch.ByteTensor(3, 4).random_()
   968             def forward(self, x):
   969                 return x * self.param.float()
   974         torch.onnx._export(MyModel(), (torch.rand(3, 4),), f, verbose=
True, export_type=ExportTypes.ZIP_ARCHIVE)
   976         X = np.random.rand(3, 4).astype(np.float32)
   980         model = c2.prepare_zip_archive(f)
   983     def test_neg_slice(self):
   984         class NegSlice(torch.nn.Module):
   985             def forward(self, x):
   988         x = torch.randn(3, 4, 5)
   989         self.
run_model_test(NegSlice(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
   991     def test_neg_slice_large(self):
   992         class NegSlice(torch.nn.Module):
   993             def forward(self, x):
   994                 return x[:, :, :, :, -3]
   996         x = torch.randn(3, 4, 5, 6, 7)
   997         self.
run_model_test(NegSlice(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
   999     @unittest.skip(
'https://github.com/pytorch/pytorch/issues/10984')
  1000     def test_neg_slice_large_negone(self):
  1001         class NegSlice(torch.nn.Module):
  1002             def forward(self, x):
  1003                 return x[:, :, :, :, -1]
  1005         x = torch.randn(3, 4, 5, 6, 7)
  1006         self.
run_model_test(NegSlice(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1008     def test_dynamic_slice(self):
  1009         class DynamicSliceExportMod(torch.nn.Module):
  1010             def forward(self, x):
  1013                     results.append(x[:x.size(0) - i, i:x.size(2), i:3])
  1014                 return tuple(results)
  1016         x = torch.rand(5, 5, 5)
  1017         self.
run_model_test(DynamicSliceExportMod(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1019     def test_dynamic_slice_to_the_end(self):
  1020         class DynamicSliceExportMod(torch.nn.Module):
  1021             def forward(self, x):
  1024                     results.append(x[:, i:, x.size(2) - 5])
  1025                 return tuple(results)
  1027         x = torch.rand(5, 5, 5)
  1028         self.
run_model_test(DynamicSliceExportMod(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1030     def test_tensor_factories(self):
  1031         class TensorFactory(torch.nn.Module):
  1032             def forward(self, x):
  1033                 return torch.zeros(x.size()) + torch.ones(x.size())
  1035         x = torch.randn(2, 3, 4)
  1036         self.
run_model_test(TensorFactory(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1038     def test_where_functional(self):
  1039         class WhereFunctional(torch.nn.Module):
  1040             def forward(self, x):
  1041                 return torch.where(x > 2.0, x, torch.neg(x))
  1043         x = torch.randn(3, 4)
  1044         self.
run_model_test(WhereFunctional(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1046     def test_where_method(self):
  1047         class WhereMethod(torch.nn.Module):
  1048             def forward(self, x):
  1049                 return x.where(x > 2.0, torch.neg(x))
  1051         x = torch.randn(3, 4)
  1052         self.
run_model_test(WhereMethod(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1054     def test_data_dependent_zeros_factory(self):
  1055         class ZerosFactory(torch.nn.Module):
  1056             def forward(self, input):
  1057                 return torch.cat([input, torch.zeros(input.size(0), 1).type_as(input)], dim=1)
  1059         x = torch.zeros(3, 4)
  1060         self.
run_model_test(ZerosFactory(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1062     def test_implicit_expand(self):
  1063         class ImplicitExpandExportMod(torch.nn.Module):
  1064             def forward(self, x):
  1067         x = torch.randn(3, 4)
  1068         self.
run_model_test(ImplicitExpandExportMod(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1070     def test_reduce_sum(self):
  1071         class ReduceSumNegativeIndices(torch.nn.Module):
  1072             def forward(self, x):
  1075         x = torch.randn(2, 3, 4)
  1076         self.
run_model_test(ReduceSumNegativeIndices(), train=
False, input=(x,), batch_size=BATCH_SIZE, use_gpu=
False)
  1078     def test_group_norm(self):
  1079         c = torch.randn(BATCH_SIZE, 6, 224)
  1080         model = nn.GroupNorm(3, 6)
  1081         self.
run_model_test(model, train=
True, input=c, batch_size=BATCH_SIZE)
  1083     def test_rsub(self):
  1084         class RsubModel(torch.nn.Module):
  1085             def forward(self, x):
  1088         x = torch.randn(1, 2)
  1090                             batch_size=BATCH_SIZE, use_gpu=
False)
  1092     def test_isnan(self):
  1093         class IsNaNModel(torch.nn.Module):
  1094             def forward(self, input):
  1095                 return torch.isnan(input)
  1098         self.
run_model_test(IsNaNModel(), train=
False, input=x, batch_size=BATCH_SIZE, use_gpu=
False)
  1100     def test_flatten(self):
  1101         class FlattenModel(torch.nn.Module):
  1102             def forward(self, input):
  1103                 return torch.flatten(input)
  1105         x = torch.randn(1, 2, 3, 4, requires_grad=
True)
  1106         self.
run_model_test(FlattenModel(), train=
False, input=x, batch_size=BATCH_SIZE)
  1108     def test_flatten2D(self):
  1109         class FlattenModel(torch.nn.Module):
  1110             def forward(self, input):
  1111                 return torch.flatten(input, 1)
  1113         x = torch.randn(1, 2, 3, 4, requires_grad=
True)
  1114         self.
run_model_test(FlattenModel(), train=
False, input=x, batch_size=BATCH_SIZE)
  1116     def test_argmax(self):
  1117         class ArgmaxModel(torch.nn.Module):
  1118             def forward(self, input):
  1119                 return torch.argmax(input, dim=1)
  1121         x = torch.randn(4, 4, requires_grad=
True)
  1122         self.
run_model_test(ArgmaxModel(), train=
False, input=x, batch_size=BATCH_SIZE)
  1124     def test_argmin(self):
  1125         class ArgminModel(torch.nn.Module):
  1126             def forward(self, input):
  1127                 return torch.argmin(input, dim=1)
  1129         x = torch.randn(4, 4, requires_grad=
True)
  1130         self.
run_model_test(ArgminModel(), train=
False, input=x, batch_size=BATCH_SIZE)
  1132     def test_reshape(self):
  1133         class ReshapeModel(torch.nn.Module):
  1134             def forward(self, input):
  1135                 return input.reshape(1, 1)
  1137         x = torch.randn(1, requires_grad=
True)
  1138         self.
run_model_test(ReshapeModel(), train=
False, input=x, batch_size=BATCH_SIZE)
  1140     def test_reshape_as(self):
  1141         class ReshapeAsModel(torch.nn.Module):
  1142             def forward(self, input):
  1143                 y = torch.randn(3, 1, 2, 1, requires_grad=
False)
  1144                 return input.reshape_as(y)
  1146         x = torch.randn(2, 3, requires_grad=
True)
  1147         self.
run_model_test(ReshapeAsModel(), train=
False, input=x, batch_size=BATCH_SIZE)
  1149     def test_narrow(self):
  1150         class NarrowModel(torch.nn.Module):
  1151             def forward(self, input):
  1152                 return torch.narrow(input, 0, 0, 2)
  1154         x = torch.randn(3, 3, requires_grad=
True)
  1155         self.
run_model_test(NarrowModel(), train=
False, input=x, batch_size=BATCH_SIZE)
  1160 def make_test(name, base, layer, bidirectional, initial_state,
  1161               variable_length, dropout,
  1163     test_name = str(
'_'.join([
  1164         'test', name, layer[1],
  1165         bidirectional[1], initial_state[1],
  1166         variable_length[1], dropout[1]
  1173             bidirectional=bidirectional[0],
  1174             initial_state=initial_state[0],
  1175             packed_sequence=variable_length[0],
  1179     f.__name__ = test_name
  1180     setattr(TestCaffe2Backend, f.__name__, f)
  1183 def setup_rnn_tests():
  1188     bidirectional_opts = [
  1190         (
True, 
'bidirectional')
  1192     initial_state_opts = [
  1193         (
True, 
'with_initial_state'),
  1194         (
False, 
'no_initial_state')
  1196     variable_length_opts = [
  1197         (0, 
'without_sequence_lengths'),
  1198         (1, 
'with_variable_length_sequences'),
  1199         (2, 
'with_batch_first_sequence_lengths')
  1202         (0.2, 
'with_dropout'),
  1203         (0.0, 
'without_dropout')
  1206     for (layer, bidirectional, initial_state, variable_length, dropout) 
in \
  1211             variable_length_opts,
  1215         for base, name, extra_kwargs 
in (
  1216                 (
'elman', 
'elman_relu', {
'nonlinearity': 
u'relu'}),
  1217                 (
'elman', 
'elman_tanh', {
'nonlinearity': 
u'tanh'}),
  1218                 (
'lstm', 
'lstm', {}),
  1221             make_test(name, base, layer, bidirectional, initial_state,
  1222                       variable_length, dropout,
  1227     TestCaffe2Backend.test_gru_trilayer_forward_with_initial_state_without_sequence_lengths_with_dropout
  1231     assert test_count == 192, test_count
  1236 TestCaffe2BackendEmbed = type(str(
"TestCaffe2BackendEmbed"),
  1237                               (unittest.TestCase,),
  1238                               dict(TestCaffe2Backend.__dict__, embed_params=
True))
  1241 if __name__ == 
'__main__':
 
def reshape_from_tensor_shape(x, shape)
 
def _lstm_test(self, layers, bidirectional, initial_state, packed_sequence, dropout)
 
def _elman_rnn_test(self, layers, nonlinearity, bidirectional, initial_state, packed_sequence, dropout)
 
def _test_index_generic(self, fn)
 
def verify(model, args, backend, verbose=False, training=False, rtol=1e-3, atol=1e-7, test_args=2)
 
def _dispatch_rnn_test(self, name, args, kwargs)
 
def _export(args, kwargs)
 
def run_word_language_model(self, model_name)
 
def run_debug_test(self, model, train, batch_size, state_dict=None, input=None, use_gpu=True, example_outputs=None)
 
def run_actual_test(self, model, train, batch_size, state_dict=None, input=None, use_gpu=True, rtol=0.001, atol=1e-7, example_outputs=None)
 
def convert_cuda(self, model, input)
 
def run_model_test(self, model, train, batch_size, state_dict=None, input=None, use_gpu=True, rtol=0.001, atol=1e-7, example_outputs=None)
 
def set_default_tensor_type(t)
 
def _gru_test(self, layers, bidirectional, initial_state, packed_sequence, dropout)