@@ -46,8 +46,10 @@ class BilinearInterpKernel : public framework::OpKernel<T> {
46
46
int in_chw = channels * in_hw;
47
47
int out_chw = channels * out_hw;
48
48
49
- T ratio_h = (out_h > 1 ) ? static_cast <T>(in_h - 1 ) / (out_h - 1 ) : 0 .f ;
50
- T ratio_w = (out_w > 1 ) ? static_cast <T>(in_w - 1 ) / (out_w - 1 ) : 0 .f ;
49
+ float ratio_h =
50
+ (out_h > 1 ) ? static_cast <float >(in_h - 1 ) / (out_h - 1 ) : 0 .f ;
51
+ float ratio_w =
52
+ (out_w > 1 ) ? static_cast <float >(in_w - 1 ) / (out_w - 1 ) : 0 .f ;
51
53
52
54
if (in_h == out_h && in_w == out_w) {
53
55
memcpy (output, input, input_t ->numel () * sizeof (T));
@@ -56,24 +58,24 @@ class BilinearInterpKernel : public framework::OpKernel<T> {
56
58
for (int i = 0 ; i < out_h; ++i) { // loop for images
57
59
int h = ratio_h * i;
58
60
int hid = (h < in_h - 1 ) ? 1 : 0 ;
59
- T h1lambda = ratio_h * i - h;
60
- T h2lambda = 1 - h1lambda;
61
+ float h1lambda = ratio_h * i - h;
62
+ float h2lambda = 1 . f - h1lambda;
61
63
62
64
for (int j = 0 ; j < out_w; ++j) {
63
65
int w = ratio_w * j;
64
66
int wid = (w < in_w - 1 ) ? 1 : 0 ;
65
- T w1lambda = ratio_w * j - w;
66
- T w2lambda = 1 - w1lambda;
67
+ float w1lambda = ratio_w * j - w;
68
+ float w2lambda = 1 . f - w1lambda;
67
69
// calculate four position for bilinear interpolation
68
70
const T* in_pos = &input[k * in_chw + h * in_w + w];
69
71
T* out_pos = &output[k * out_chw + i * out_w + j];
70
72
71
73
for (int c = 0 ; c < channels; ++c) { // loop for channels
72
74
// bilinear interpolation
73
- out_pos[0 ] =
75
+ out_pos[0 ] = static_cast <T>(
74
76
h2lambda * (w2lambda * in_pos[0 ] + w1lambda * in_pos[wid]) +
75
77
h1lambda * (w2lambda * in_pos[hid * in_w] +
76
- w1lambda * in_pos[hid * in_w + wid]);
78
+ w1lambda * in_pos[hid * in_w + wid])) ;
77
79
in_pos += in_hw;
78
80
out_pos += out_hw;
79
81
}
@@ -117,8 +119,10 @@ class BilinearInterpGradKernel : public framework::OpKernel<T> {
117
119
int in_chw = channels * in_hw;
118
120
int out_chw = channels * out_hw;
119
121
120
- T ratio_h = (out_h > 1 ) ? static_cast <T>(in_h - 1 ) / (out_h - 1 ) : 0 .f ;
121
- T ratio_w = (out_w > 1 ) ? static_cast <T>(in_w - 1 ) / (out_w - 1 ) : 0 .f ;
122
+ float ratio_h =
123
+ (out_h > 1 ) ? static_cast <float >(in_h - 1 ) / (out_h - 1 ) : 0 .f ;
124
+ float ratio_w =
125
+ (out_w > 1 ) ? static_cast <float >(in_w - 1 ) / (out_w - 1 ) : 0 .f ;
122
126
123
127
if (in_h == out_h && in_w == out_w) {
124
128
memcpy (d_input, d_output, d_input_t ->numel () * sizeof (T));
@@ -127,22 +131,24 @@ class BilinearInterpGradKernel : public framework::OpKernel<T> {
127
131
for (int i = 0 ; i < out_h; ++i) { // loop for images
128
132
int h = ratio_h * i;
129
133
int hid = (h < in_h - 1 ) ? 1 : 0 ;
130
- T h1lambda = ratio_h * i - h;
131
- T h2lambda = 1 - h1lambda;
134
+ float h1lambda = ratio_h * i - h;
135
+ float h2lambda = 1 - h1lambda;
132
136
133
137
for (int j = 0 ; j < out_w; ++j) {
134
138
int w = ratio_w * j;
135
139
int wid = (w < in_w - 1 ) ? 1 : 0 ;
136
- T w1lambda = ratio_w * j - w;
137
- T w2lambda = 1 - w1lambda;
140
+ float w1lambda = ratio_w * j - w;
141
+ float w2lambda = 1 - w1lambda;
138
142
T* in_pos = &d_input[k * in_chw + h * in_w + w];
139
143
const T* out_pos = &d_output[k * out_chw + i * out_w + j];
140
144
141
145
for (int c = 0 ; c < channels; ++c) { // loop for channels
142
- in_pos[0 ] += h2lambda * w2lambda * out_pos[0 ];
143
- in_pos[wid] += h2lambda * w1lambda * out_pos[0 ];
144
- in_pos[hid * in_w] += h1lambda * w2lambda * out_pos[0 ];
145
- in_pos[hid * in_w + wid] += h1lambda * w1lambda * out_pos[0 ];
146
+ in_pos[0 ] += static_cast <T>(h2lambda * w2lambda * out_pos[0 ]);
147
+ in_pos[wid] += static_cast <T>(h2lambda * w1lambda * out_pos[0 ]);
148
+ in_pos[hid * in_w] +=
149
+ static_cast <T>(h1lambda * w2lambda * out_pos[0 ]);
150
+ in_pos[hid * in_w + wid] +=
151
+ static_cast <T>(h1lambda * w1lambda * out_pos[0 ]);
146
152
in_pos += in_hw;
147
153
out_pos += out_hw;
148
154
}
0 commit comments