Skip to content

Commit

Permalink
Merge pull request #50899 from geetachavan1/cherrypicks_SP2AJ
Browse files Browse the repository at this point in the history
Disallow division by zero FPE in tf.raw_ops.SparseDenseCwiseDiv
  • Loading branch information
mihaimaruseac committed Jul 26, 2021
2 parents ecee625 + 6984d06 commit c8fb7fc
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tensorflow/core/kernels/sparse_dense_binary_op_shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ class SparseDenseBinaryOpShared : public OpKernel {
OP_REQUIRES_OK(
ctx, ctx->allocate_temp(DataTypeToEnum<T>::value, TensorShape({nnz}),
&dense_gathered));

bool op_is_div = false;
if (absl::StrContains(ctx->op_kernel().type_string_view(), "Div")) {
op_is_div = true;
}
// Pulls relevant entries from the dense side, with reshape and broadcasting
// *of the dense side* taken into account. Use a TensorRef to avoid blowing
// up memory.
Expand Down Expand Up @@ -143,6 +146,12 @@ class SparseDenseBinaryOpShared : public OpKernel {
errors::InvalidArgument("Provided indices are out-of-bounds w.r.t. " \
"dense side with broadcasted shape")); \
dense_gathered_flat(i) = rhs_ref.coeff(idx); \
if (op_is_div) { \
OP_REQUIRES(ctx, dense_gathered_flat(i) != 0, \
errors::InvalidArgument( \
"SparseDenseCwiseDiv cannot divide by zero," \
"but input dense tensor contains zero ")); \
} \
} \
break; \
}
Expand Down

0 comments on commit c8fb7fc

Please sign in to comment.