qfeval_functions.functions.mulsum

mulsum(x, y, dim=(), keepdim=False, mean=False, *, _ddof=0)[source]

Compute sum or mean of element-wise product in a memory-efficient way.

This function calculates the sum (or mean) of the element-wise product of two tensors (x * y).sum(dim) or (x * y).mean(dim) without creating the intermediate product tensor in memory. This is particularly crucial when working with large tensors or when broadcasting would result in a very large intermediate tensor that could exceed available memory.

Parameters:
  • x (Tensor) – The first input tensor.

  • y (Tensor) – The second input tensor. Must be broadcastable with x.

  • dim (Union[int, Tuple[int, ...]]) – The dimension(s) along which to compute the sum or mean. If not specified (default is empty tuple), the operation is computed over all dimensions.

  • keepdim (bool) – Whether the output tensor has dim retained or not. Default is False.

  • mean (bool) – If True, computes the mean instead of sum. Default is False (computes sum).

  • _ddof (int) – Delta degrees of freedom for mean calculations. The divisor used is N - _ddof, where N is the number of elements. Default is 0. This is an internal parameter.

Returns:

The sum or mean of the element-wise product. The shape depends on the input dimensions, dim, and keepdim parameters.

Return type:

Tensor

Example

>>> # Simple element-wise product sum
>>> x = torch.tensor([1.0, 2.0, 3.0])
>>> y = torch.tensor([2.0, 3.0, 4.0])
>>> QF.mulsum(x, y)
tensor(20.)
>>> # Equivalent to (x * y).sum()
>>> torch.allclose(QF.mulsum(x, y), (x * y).sum())
True
>>> # Mean instead of sum
>>> QF.mulsum(x, y, mean=True)
tensor(6.6667)
>>> # 2D tensors with specific dimension
>>> x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> y = torch.tensor([[2.0, 1.0], [1.0, 2.0]])
>>> QF.mulsum(x, y, dim=1)
tensor([ 4., 11.])
>>> # Memory-efficient broadcasting
>>> x = torch.randn(1000, 1)
>>> y = torch.randn(1, 1000)
>>> # Efficiently computes without creating 1000x1000 intermediate tensor
>>> result = QF.mulsum(x, y)
>>> # With keepdim
>>> x = torch.tensor([[[1.0, 2.0]], [[3.0, 4.0]]])
>>> y = torch.tensor([[[2.0, 1.0]], [[1.0, 2.0]]])
>>> QF.mulsum(x, y, dim=(1, 2), keepdim=True)
tensor([[[ 4.]],

        [[11.]]])

See also

mulmean(): Convenience function for computing means. einsum(): The underlying Einstein summation function. covar(): Uses this function for efficient covariance calculations.