def _OverrideBinaryOperatorHelper(func, op_name, clazz_object=ops.Tensor):
    'Register operators with different tensor and scalar versions.\n\n  If `clazz_object` is `SparseTensor`, assumes `func` takes `(sp_indices,\n  sp_values, sp_shape, dense)` and outputs `(new_sp_values)`.\n\n  Args:\n    func: the operator\n    op_name: name of the operator being overridden\n    clazz_object: class to override for.  Either `Tensor` or `SparseTensor`.\n  '

    def binary_op_wrapper(x, y):
        with ops.name_scope(None, op_name, [x, y]) as name:
            if (isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor)):
                return func(x, y, name=name)
            elif (not isinstance(y, sparse_tensor.SparseTensor)):
                try:
                    y = ops.convert_to_tensor_v2(y, dtype_hint=x.dtype.base_dtype, name='y')
                except TypeError:
                    if hasattr(type(y), ('__r%s__' % op_name)):
                        return NotImplemented
                    else:
                        raise
            return func(x, y, name=name)

    def binary_op_wrapper_sparse(sp_x, y):
        with ops.name_scope(None, op_name, [sp_x, y]) as name:
            y = ops.convert_to_tensor(y, dtype=sp_x.dtype.base_dtype, name='y')
            return sparse_tensor.SparseTensor(sp_x.indices, func(sp_x.indices, sp_x.values, sp_x.dense_shape, y, name=name), sp_x.dense_shape)

    def r_binary_op_wrapper(y, x):
        with ops.name_scope(None, op_name, [x, y]) as name:
            x = ops.convert_to_tensor(x, dtype=y.dtype.base_dtype, name='x')
            return func(x, y, name=name)
    try:
        doc = func.__doc__
    except AttributeError:
        doc = None
    binary_op_wrapper.__doc__ = doc
    r_binary_op_wrapper.__doc__ = doc
    binary_op_wrapper_sparse.__doc__ = doc
    if (clazz_object is ops.Tensor):
        clazz_object._override_operator(('__%s__' % op_name), binary_op_wrapper)
        del binary_op_wrapper
        clazz_object._override_operator(('__r%s__' % op_name), r_binary_op_wrapper)
        del r_binary_op_wrapper
    else:
        clazz_object._override_operator(('__%s__' % op_name), binary_op_wrapper_sparse)
        del binary_op_wrapper_sparse
Register operators with different tensor and scalar versions.

If `clazz_object` is `SparseTensor`, assumes `func` takes `(sp_indices,
sp_values, sp_shape, dense)` and outputs `(new_sp_values)`.

Args:
  func: the operator
  op_name: name of the operator being overridden
  clazz_object: class to override for.  Either `Tensor` or `SparseTensor`.
def _OverrideBinaryOperatorHelper(func, op_name, clazz_object=ops.Tensor):
    'Register operators with different tensor and scalar versions.\n\n  If `clazz_object` is `SparseTensor`, assumes `func` takes `(sp_indices,\n  sp_values, sp_shape, dense)` and outputs `(new_sp_values)`.\n\n  Args:\n    func: the operator\n    op_name: name of the operator being overridden\n    clazz_object: class to override for.  Either `Tensor` or `SparseTensor`.\n  '

    def binary_op_wrapper(x, y):
        with ops.name_scope(None, op_name, [x, y]) as name:
            if (isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor)):
                return func(x, y, name=name)
            elif (not isinstance(y, sparse_tensor.SparseTensor)):
                try:
                    y = ops.convert_to_tensor_v2(y, dtype_hint=x.dtype.base_dtype, name='y')
                except TypeError:
                    if hasattr(type(y), ('__r%s__' % op_name)):
                        return NotImplemented
                    else:
                        raise
            return func(x, y, name=name)

    def binary_op_wrapper_sparse(sp_x, y):
        with ops.name_scope(None, op_name, [sp_x, y]) as name:
            y = ops.convert_to_tensor(y, dtype=sp_x.dtype.base_dtype, name='y')
            return sparse_tensor.SparseTensor(sp_x.indices, func(sp_x.indices, sp_x.values, sp_x.dense_shape, y, name=name), sp_x.dense_shape)

    def r_binary_op_wrapper(y, x):
        with ops.name_scope(None, op_name, [x, y]) as name:
            x = ops.convert_to_tensor(x, dtype=y.dtype.base_dtype, name='x')
            return func(x, y, name=name)
    try:
        doc = func.__doc__
    except AttributeError:
        doc = None
    binary_op_wrapper.__doc__ = doc
    r_binary_op_wrapper.__doc__ = doc
    binary_op_wrapper_sparse.__doc__ = doc
    if (clazz_object is ops.Tensor):
        clazz_object._override_operator(('__%s__' % op_name), binary_op_wrapper)
        del binary_op_wrapper
        clazz_object._override_operator(('__r%s__' % op_name), r_binary_op_wrapper)
        del r_binary_op_wrapper
    else:
        clazz_object._override_operator(('__%s__' % op_name), binary_op_wrapper_sparse)
        del binary_op_wrapper_sparse