Skip to content

Commit c249518

Browse files
committed
fix negative indices
1 parent b84bc49 commit c249518

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

aten/src/THCUNN/linear_upsampling.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ template<typename Acctype>
1717
__device__ __forceinline__
1818
static Acctype linear_upsampling_compute_source_index(
1919
Acctype scale, int dst_index, bool align_corners) {
20-
if (dst_index == 0) {
21-
return Acctype(0);
22-
} else if (align_corners) {
23-
return scale * dst_index;
24-
} else {
25-
return scale * (dst_index + Acctype(0.5)) - Acctype(0.5);
26-
}
20+
if (align_corners) {
21+
return scale * dst_index;
22+
} else {
23+
Acctype src_idx = scale * (dst_index + Acctype(0.5)) - Acctype(0.5);
24+
return src_idx < Acctype(0) ? Acctype(0) : src_idx;
25+
}
2726
}
2827

2928

aten/src/THNN/generic/linear_upsampling.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ static inline float THNN_(linear_upsampling_compute_scale)(
1414

1515
static inline float THNN_(linear_upsampling_compute_source_index)(
1616
float scale, int dst_index, bool align_corners) {
17-
if (dst_index == 0) {
18-
return 0.f;
19-
} else if (align_corners) {
17+
if (align_corners) {
2018
return scale * dst_index;
2119
} else {
22-
return scale * (dst_index + 0.5) - 0.5;
20+
float src_idx = scale * (dst_index + 0.5) - 0.5;
21+
return src_idx < 0 ? 0.f : src_idx;
2322
}
2423
}
2524

0 commit comments

Comments
 (0)