Skip to content

Commit 29417d1

Browse files
ricklentzfacebook-github-bot
authored andcommitted
NaN (divide by zero) fix for issue #561 and #790 (#891)
Summary: #561 #790 Divide by zero fix (NaN fix). When perspective_correct=True, BarycentricPerspectiveCorrectionForward and BarycentricPerspectiveCorrectionBackward in ../csrc/utils/geometry_utils.cuh are called. The denominator (denom) values should not be allowed to go to zero. I'm able to resolve this issue locally with this PR and submit it for the team's review. Pull Request resolved: #891 Reviewed By: patricklabatut Differential Revision: D31829695 Pulled By: bottler fbshipit-source-id: a3517b8362f6e60d48c35731258d8ce261b1d912
1 parent 57b9c72 commit 29417d1

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

pytorch3d/csrc/utils/geometry_utils.cuh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ __device__ inline float3 BarycentricPerspectiveCorrectionForward(
177177
const float w0_top = bary.x * z1 * z2;
178178
const float w1_top = z0 * bary.y * z2;
179179
const float w2_top = z0 * z1 * bary.z;
180-
const float denom = w0_top + w1_top + w2_top;
180+
const float denom = fmaxf(w0_top + w1_top + w2_top, kEpsilon);
181181
const float w0 = w0_top / denom;
182182
const float w1 = w1_top / denom;
183183
const float w2 = w2_top / denom;
@@ -208,7 +208,7 @@ BarycentricPerspectiveCorrectionBackward(
208208
const float w0_top = bary.x * z1 * z2;
209209
const float w1_top = z0 * bary.y * z2;
210210
const float w2_top = z0 * z1 * bary.z;
211-
const float denom = w0_top + w1_top + w2_top;
211+
const float denom = fmaxf(w0_top + w1_top + w2_top, kEpsilon);
212212

213213
// Now do backward pass
214214
const float grad_denom_top =

pytorch3d/csrc/utils/geometry_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ inline vec3<T> BarycentricPerspectiveCorrectionForward(
198198
const T w0_top = bary.x * z1 * z2;
199199
const T w1_top = bary.y * z0 * z2;
200200
const T w2_top = bary.z * z0 * z1;
201-
const T denom = w0_top + w1_top + w2_top;
201+
const T denom = std::max<T>(w0_top + w1_top + w2_top, kEpsilon);
202202
const T w0 = w0_top / denom;
203203
const T w1 = w1_top / denom;
204204
const T w2 = w2_top / denom;
@@ -229,7 +229,7 @@ inline std::tuple<vec3<T>, T, T, T> BarycentricPerspectiveCorrectionBackward(
229229
const T w0_top = bary.x * z1 * z2;
230230
const T w1_top = bary.y * z0 * z2;
231231
const T w2_top = bary.z * z0 * z1;
232-
const T denom = w0_top + w1_top + w2_top;
232+
const T denom = std::max<T>(w0_top + w1_top + w2_top, kEpsilon);
233233

234234
// Now do backward pass
235235
const T grad_denom_top =

0 commit comments

Comments
 (0)