<fim-prefix>dicator_out = self.evaluate(
          [sp_output, empty_row_indicator])

      self.assertAllEqual(output.indices, [[0, 0], [1, 0]])
      self.assertAllEqual(output.values, [-1, -1])
      self.assertAllEqual(output.dense_shape, [2, 5])
      self.assertAllEqual(empty_row_indicator_out, np.ones(2).astype(np.bool_))

  def testEmptyOutput(self):
    with test_util.use_gpu():
      sp_input = sparse_tensor.SparseTensor(
          indices=np.ones([0, 2]),
          values=np.ones([0]),
          dense_shape=np.array([0, 3]))
      sp_output, empty_row_indicator = (
          sparse_ops.sparse_fill_empty_rows(sp_input, -1))

      output, empty_row_indicator_out = self.evaluate(
          [sp_output, empty_row_indicator])

      self.assertAllEqual(output.indices, np.ones([0, 2]))
      self.assertAllEqual(output.values, np.ones([0]))
      self.assertAllEqual(output.dense_shape, [0, 3])
      self.assertAllEqual(empty_row_indicator_out, [])

  def testInvalidIndices(self):
    with test_util.use_gpu():
      sp_input = sparse_tensor.SparseTensor(
          indices=np.array([[1, 2], [1, 3], [99, 1], [99, 3]]),
          values=np.array([1, 3, 2, 4]),
          dense_shape=np.array([2, 5]))

      with self.assertRaisesRegex(errors.InvalidArgumentError,
                                  r"indices\(2, 0\) is invalid"):
        self.evaluate(sparse_ops.sparse_fill_empty_rows(sp_input, -1))


class SparseAddTest(test_util.TensorFlowTestCase):

  def testValuesInVariable(self):
    # BUG: CWE-20 Improper Input Validation
    # indices = constant_op.constant([[1]], dtype=dtypes.int64)
    # FIXED: 
 <fim-middle>   indices = constant_op.constant([[0]], dtype=dtypes.int64)
    values = variables.Variable([1], trainable=False, dtype=dtypes.float32)
    shape = constant_op.constant([1], dtype=dtypes.int64)

    s<fix-suffix>p_input = sparse_tensor.SparseTensor(indices, values, shape)
    sp_output = sparse_ops.sparse_add(sp_input, sp_input)

    with test_util.force_cpu():
      self.evaluate(variables.global_variables_initializer())
      output = self.evaluate(sp_output)
      self.assertAllEqual(output.values, [2])


class SparseReduceTest(test_util.TensorFlowTestCase):

  # [[1, ?, 2]
  #  [?, 3, ?]]
  # where ? is implicitly-zero.
  ind = np.array([[0, 0], [0, 2], [1, 1]]).astype(np.int64)
  vals = np.array([1, 1, 1]).astype(np.int32)
  dense_shape = np.array([2, 3]).astype(np.int64)

  def _compare(self, sp_t, reduction_axes, ndims, keep_dims, do_sum):
    densified = self.evaluate(sparse_ops.sparse_tensor_to_dense(sp_t))

    np_ans = densified
    if reduction_axes is None:
      if do_sum:
        np_ans = np.sum(np_ans, keepdims=keep_dims)
      else:
        np_ans = np.max(np_ans, keepdims=keep_dims)
    else:
      if not isinstance(reduction_axes, list):  # Single scalar.
        reduction_a<|endoftext|>