@@ -1500,7 +1500,7 @@ function rnntest.SequencerCriterion()
1500
1500
local gradInputTable = sc :backward (split :forward (input ), split :forward (target ))
1501
1501
mytester :assertTensorEq (gradInputTensor , torch .cat (gradInputTable , 1 ):view (gradInputTensor :size ()), 0 , " SequencerCriterion backward type err " )
1502
1502
1503
- if pcall ( function () require ' cunn ' end ) then
1503
+ if rnn . cuda then
1504
1504
-- test cuda()
1505
1505
sc .gradInput = {}
1506
1506
sc :cuda ()
@@ -2107,7 +2107,7 @@ function rnntest.MaskZeroCriterion()
2107
2107
mytester :assert (math.abs (err3 - err ) < 0.0000001 , " MaskZeroCriterion cast fwd err" )
2108
2108
mytester :assertTensorEq (gradInput3 , gradInput :float (), 0.0000001 , " MaskZeroCriterion cast bwd err" )
2109
2109
2110
- if pcall ( function () require ' cunn ' end ) then
2110
+ if rnn . cuda then
2111
2111
-- test cuda
2112
2112
mznll :cuda ()
2113
2113
if v2 then
@@ -2828,6 +2828,49 @@ function rnntest.SeqLSTM_Lua_vs_C()
2828
2828
end
2829
2829
end
2830
2830
2831
+ function rnntest .SeqLSTM_cuda ()
2832
+ if not rnn .cuda then
2833
+ return
2834
+ end
2835
+
2836
+ local ty = torch .getdefaulttensortype ()
2837
+ torch .setdefaulttensortype (' torch.FloatTensor' )
2838
+
2839
+ local seqlen , batchsize = 3 , 4
2840
+ local inputsize , outputsize = 2 , 5
2841
+
2842
+ local input = torch .randn (seqlen , batchsize , inputsize )
2843
+
2844
+ local seqlstm = nn .SeqLSTM (inputsize , outputsize )
2845
+ local seqlstmCuda = nn .SeqLSTM (inputsize , outputsize )
2846
+ seqlstmCuda .weight :copy (seqlstm .weight )
2847
+ seqlstmCuda .bias :copy (seqlstm .bias )
2848
+ seqlstmCuda :cuda ()
2849
+
2850
+ local output = seqlstm :forward (input )
2851
+ local outputCuda = seqlstmCuda :forward (input :cuda ())
2852
+ mytester :assertTensorEq (output , outputCuda :float (), precision )
2853
+
2854
+ seqlstm :zeroGradParameters ()
2855
+ seqlstmCuda :zeroGradParameters ()
2856
+
2857
+ local gradOutput = torch .randn (seqlen , batchsize , outputsize )
2858
+
2859
+ local gradInput = seqlstm :backward (input , gradOutput )
2860
+ local gradInputCuda = seqlstmCuda :backward (input :cuda (), gradOutput :cuda ())
2861
+
2862
+ mytester :assertTensorEq (gradInput , gradInputCuda :float (), precision )
2863
+
2864
+ local params , gradParams = seqlstm :parameters ()
2865
+ local paramsCuda , gradParamsCuda = seqlstmCuda :parameters ()
2866
+
2867
+ for i = 1 ,# paramsCuda do
2868
+ mytester :assertTensorEq (gradParams [i ], gradParamsCuda [i ]:float (), precision )
2869
+ end
2870
+
2871
+ torch .setdefaulttensortype (ty )
2872
+ end
2873
+
2831
2874
function rnntest .SeqLSTM_maskzero ()
2832
2875
-- tests that it works with non-masked inputs regardless of maskzero's value.
2833
2876
-- Note that more maskzero = true tests with masked inputs are in SeqLSTM unit test.
@@ -2858,7 +2901,7 @@ function rnntest.SeqLSTM_maskzero()
2858
2901
mytester :assertTensorEq (gradParams , gradParams2 , 0.000001 )
2859
2902
if benchmark then
2860
2903
local T , N , D , H = 20 , 20 , 50 , 50
2861
- if pcall ( function () require ' cunn ' end ) then
2904
+ if rnn . cuda then
2862
2905
T , N , D , H = 100 , 128 , 250 , 250
2863
2906
end
2864
2907
@@ -3603,7 +3646,7 @@ function rnntest.SeqGRU_maskzero()
3603
3646
mytester :assertTensorEq (gradParams , gradParams2 , 0.000001 )
3604
3647
if benchmark then
3605
3648
local T , N , D , H = 20 , 20 , 50 , 50
3606
- if pcall ( function () require ' cunn ' end ) then
3649
+ if rnn . cuda then
3607
3650
T , N , D , H = 100 , 128 , 250 , 250
3608
3651
end
3609
3652
@@ -3682,6 +3725,49 @@ function rnntest.SeqGRU_Lua_vs_C()
3682
3725
end
3683
3726
end
3684
3727
3728
+ function rnntest .SeqGRU_cuda ()
3729
+ if not rnn .cuda then
3730
+ return
3731
+ end
3732
+
3733
+ local ty = torch .getdefaulttensortype ()
3734
+ torch .setdefaulttensortype (' torch.FloatTensor' )
3735
+
3736
+ local seqlen , batchsize = 3 , 4
3737
+ local inputsize , outputsize = 2 , 5
3738
+
3739
+ local input = torch .randn (seqlen , batchsize , inputsize )
3740
+
3741
+ local seqgru = nn .SeqGRU (inputsize , outputsize )
3742
+ local seqgruCuda = nn .SeqGRU (inputsize , outputsize )
3743
+ seqgruCuda .weight :copy (seqgru .weight )
3744
+ seqgruCuda .bias :copy (seqgru .bias )
3745
+ seqgruCuda :cuda ()
3746
+
3747
+ local output = seqgru :forward (input )
3748
+ local outputCuda = seqgruCuda :forward (input :cuda ())
3749
+ mytester :assertTensorEq (output , outputCuda :float (), precision )
3750
+
3751
+ seqgru :zeroGradParameters ()
3752
+ seqgruCuda :zeroGradParameters ()
3753
+
3754
+ local gradOutput = torch .randn (seqlen , batchsize , outputsize )
3755
+
3756
+ local gradInput = seqgru :backward (input , gradOutput )
3757
+ local gradInputCuda = seqgruCuda :backward (input :cuda (), gradOutput :cuda ())
3758
+
3759
+ mytester :assertTensorEq (gradInput , gradInputCuda :float (), precision )
3760
+
3761
+ local params , gradParams = seqgru :parameters ()
3762
+ local paramsCuda , gradParamsCuda = seqgruCuda :parameters ()
3763
+
3764
+ for i = 1 ,# paramsCuda do
3765
+ mytester :assertTensorEq (gradParams [i ], gradParamsCuda [i ]:float (), precision )
3766
+ end
3767
+
3768
+ torch .setdefaulttensortype (ty )
3769
+ end
3770
+
3685
3771
function checkgrad (opfunc , x , eps )
3686
3772
-- compute true gradient:
3687
3773
local _ ,dC = opfunc (x )
@@ -3865,6 +3951,51 @@ function rnntest.VariableLength_FromSamples()
3865
3951
end
3866
3952
end
3867
3953
3954
+ function rnntest .VariableLength_FromSamples_cuda ()
3955
+ if not rnn .cuda then
3956
+ return
3957
+ end
3958
+
3959
+ torch .manualSeed (0 )
3960
+ local nSamples = 10
3961
+ local maxLength = 20
3962
+ for run = 1 ,10 do
3963
+ local lengths = torch .LongTensor (nSamples )
3964
+ lengths :random (maxLength )
3965
+ local samples = {}
3966
+ for i = 1 ,nSamples do
3967
+ local t = torch .rand (lengths [i ], 5 )
3968
+ samples [i ] = t :cuda ()
3969
+ end
3970
+ local output = torch .CudaTensor ()
3971
+ local mask = torch .CudaByteTensor ()
3972
+ local indexes , mappedLengths = output .nn .VariableLength_FromSamples (samples , output , mask )
3973
+
3974
+ output = output :float ()
3975
+ mask = mask :byte ()
3976
+ for i , ids in ipairs (indexes ) do
3977
+ local m = mask :select (2 , i )
3978
+ local t = output :select (2 , i )
3979
+ for j , sampleId in ipairs (ids ) do
3980
+ local l = lengths [sampleId ]
3981
+ -- check that the length was mapped correctly
3982
+ mytester :assert (l == mappedLengths [i ][j ])
3983
+ -- checks that the mask is 0 for valid entries
3984
+ mytester :assert (math.abs (m :narrow (1 , 1 , l ):sum ()) < 0.000001 )
3985
+ -- checks that the valid entries are equal
3986
+ mytester :assertTensorEq (t :narrow (1 , 1 , l ), samples [sampleId ]:float ())
3987
+ if l < m :size (1 ) then
3988
+ mytester :assert (m [l + 1 ] == 1 )
3989
+ end
3990
+ if l + 1 < m :size (1 ) then
3991
+ m = m :narrow (1 , l + 2 , m :size (1 )- l - 1 )
3992
+ t = t :narrow (1 , l + 2 , t :size (1 )- l - 1 )
3993
+ end
3994
+ end
3995
+ end
3996
+ end
3997
+ end
3998
+
3868
3999
function rnntest .VariableLength_ToSamples ()
3869
4000
local nSamples = 10
3870
4001
local maxLength = 20
@@ -3886,6 +4017,30 @@ function rnntest.VariableLength_ToSamples()
3886
4017
end
3887
4018
end
3888
4019
4020
+ function rnntest .VariableLength_ToSamples_cuda ()
4021
+ if not rnn .cuda then
4022
+ return
4023
+ end
4024
+ local nSamples = 10
4025
+ local maxLength = 20
4026
+ for run = 1 ,10 do
4027
+ local lengths = torch .LongTensor (nSamples )
4028
+ lengths :random (maxLength )
4029
+ local samples = {}
4030
+ for i = 1 ,nSamples do
4031
+ samples [i ] = torch .rand (lengths [i ], 5 ):cuda ()
4032
+ end
4033
+ local output = torch .CudaTensor ()
4034
+ local mask = torch .CudaByteTensor ()
4035
+ local indexes , mappedLengths = output .nn .VariableLength_FromSamples (samples , output , mask )
4036
+ local new_samples = output .nn .VariableLength_ToSamples (indexes , mappedLengths , output )
4037
+ mytester :assert (# samples == # new_samples )
4038
+ for i = 1 ,nSamples do
4039
+ mytester :assertTensorEq (samples [i ]:float (), new_samples [i ]:float ())
4040
+ end
4041
+ end
4042
+ end
4043
+
3889
4044
function rnntest .VariableLength_ToFinal ()
3890
4045
local nSamples = 10
3891
4046
local maxLength = 20
@@ -3910,6 +4065,30 @@ function rnntest.VariableLength_ToFinal()
3910
4065
end
3911
4066
end
3912
4067
4068
+ function rnntest .VariableLength_ToFinal_cuda ()
4069
+ local nSamples = 10
4070
+ local maxLength = 20
4071
+ for run = 1 ,10 do
4072
+ local lengths = torch .LongTensor (nSamples )
4073
+ lengths :random (maxLength )
4074
+ local samples = {}
4075
+ for i = 1 ,nSamples do
4076
+ local t = torch .rand (lengths [i ], 5 ):cuda ()
4077
+ samples [i ] = t
4078
+ end
4079
+ local output = torch .CudaTensor ()
4080
+ local mask = torch .CudaByteTensor ()
4081
+ local indexes , mappedLengths = output .nn .VariableLength_FromSamples (samples , output , mask )
4082
+
4083
+ local final = torch .CudaTensor ()
4084
+ output .nn .VariableLength_ToFinal (indexes , mappedLengths , output , final )
4085
+
4086
+ for i = 1 ,nSamples do
4087
+ mytester :assertTensorEq (samples [i ]:select (1 , lengths [i ]):float (), final :select (1 , i ):float ())
4088
+ end
4089
+ end
4090
+ end
4091
+
3913
4092
function rnntest .VariableLength_FromFinal ()
3914
4093
torch .manualSeed (2 )
3915
4094
local nSamples = 10
@@ -3943,6 +4122,39 @@ function rnntest.VariableLength_FromFinal()
3943
4122
end
3944
4123
end
3945
4124
4125
+ function rnntest .VariableLength_FromFinal_cuda ()
4126
+ torch .manualSeed (2 )
4127
+ local nSamples = 10
4128
+ local maxLength = 20
4129
+ for run = 1 ,1 do
4130
+ local lengths = torch .LongTensor (nSamples )
4131
+ lengths :random (maxLength )
4132
+ local samples = {}
4133
+ for i = 1 ,nSamples do
4134
+ local t = torch .rand (lengths [i ], 5 ):cuda ()
4135
+ samples [i ] = t
4136
+ end
4137
+ local output = torch .CudaTensor ()
4138
+ local mask = torch .CudaByteTensor ()
4139
+ local indexes , mappedLengths = output .nn .VariableLength_FromSamples (samples , output , mask )
4140
+
4141
+ local final = torch .CudaTensor ()
4142
+ output .nn .VariableLength_ToFinal (indexes , mappedLengths , output , final )
4143
+
4144
+ local re_output = torch .CudaTensor ()
4145
+ output .nn .VariableLength_FromFinal (indexes , mappedLengths , final , re_output )
4146
+
4147
+ local new_samples = output .nn .VariableLength_ToSamples (indexes , mappedLengths , re_output )
4148
+
4149
+ for i = 1 ,nSamples do
4150
+ if lengths [i ] > 1 then
4151
+ mytester :assert (new_samples [i ]:narrow (1 , 1 , lengths [i ]- 1 ):abs ():sum () < 0.000001 )
4152
+ end
4153
+ mytester :assertTensorEq (samples [i ]:select (1 , lengths [i ]):float (), new_samples [i ]:select (1 , lengths [i ]):float ())
4154
+ end
4155
+ end
4156
+ end
4157
+
3946
4158
function rnntest .VariableLength_lstm ()
3947
4159
-- test seqlen x batchsize x hiddensize
3948
4160
local maxLength = 8
@@ -4987,7 +5199,7 @@ function rnntest.VRClassReward()
4987
5199
mytester :assertTensorEq (gradInput [2 ], gradInput2 , 0.000001 , " VRClassReward backward baseline err" )
4988
5200
mytester :assert (math.abs (gradInput [1 ]:sum ()) < 0.000001 , " VRClassReward backward class err" )
4989
5201
4990
- if pcall ( function () require ' cunn ' end ) then
5202
+ if rnn . cuda then
4991
5203
local gradInput = {gradInput [1 ], gradInput [2 ]}
4992
5204
input [1 ], input [2 ] = input [1 ]:cuda (), input [2 ]:cuda ()
4993
5205
target = target :cuda ()
@@ -5859,7 +6071,7 @@ function rnntest.NCE_main()
5859
6071
end
5860
6072
5861
6073
5862
- if pcall ( function () require ' cunn ' end ) then
6074
+ if rnn . cuda then
5863
6075
-- test training with cuda
5864
6076
5865
6077
ncem :cuda ()
@@ -6082,7 +6294,7 @@ function rnntest.NCE_batchnoise()
6082
6294
end
6083
6295
6084
6296
6085
- if pcall ( function () require ' cunn ' end ) then
6297
+ if rnn . cuda then
6086
6298
-- test training with cuda
6087
6299
6088
6300
ncem :cuda ()
@@ -6168,7 +6380,7 @@ function rnntest.NCE_multicuda()
6168
6380
if not pcall (function () require ' torchx' end ) then
6169
6381
return
6170
6382
end
6171
- if not pcall ( function () require ' cunn ' end ) then
6383
+ if not rnn . cuda then
6172
6384
return
6173
6385
end
6174
6386
if cutorch .getDeviceCount () < 2 then
0 commit comments