MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / multiply

Function multiply

python/paddle/sparse/binary.py:357–405  ·  view source on GitHub ↗

Multiply two sparse tensors element-wise. Input x and y's shape should be identical and have same sparse type(SparseCooTensor or SparseCsrTensor).If input is SparseCooTensor, x and y's sparse_dim should be identical. The equation is: .. math:: out = x * y Args:

(x: Tensor, y: Tensor, name: str | None = None)

Source from the content-addressed store, hash-verified

355
356
357def multiply(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
358 """
359 Multiply two sparse tensors element-wise. Input x and y's shape should be identical and have same sparse
360 type(SparseCooTensor or SparseCsrTensor).If input is SparseCooTensor, x and y's sparse_dim should be identical.
361 The equation is:
362
363 .. math::
364 out = x * y
365
366 Args:
367 x (Tensor): the input tensor, it's data type should be float32, float64, int32, int64, complex64, complex128.
368 y (Tensor): the input tensor, it's data type should be float32, float64, int32, int64, complex64, complex128.
369 name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
370
371 Returns:
372 Tensor: the result tensor.
373
374 Examples:
375
376 .. code-block:: pycon
377
378 >>> import paddle
379
380 >>> paddle.device.set_device("cpu")
381
382 >>> x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
383 >>> y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
384 >>> sparse_x = x.to_sparse_csr()
385 >>> sparse_y = y.to_sparse_csr()
386 >>> sparse_z = paddle.sparse.multiply(sparse_x, sparse_y)
387 >>> print(sparse_z.to_dense())
388 Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
389 [[ 0., -0., 0., -4.],
390 [ 0., 0., 9., 0.],
391 [ 8., 15., 0., 0.]])
392
393 """
394
395 if isinstance(y, (int, float)):
396 return _C_ops.sparse_scale(x, float(y), 0.0, True)
397 else:
398 if in_dygraph_mode():
399 return _C_ops.sparse_multiply(x, y)
400 elif in_pir_mode():
401 return _C_ops.sparse_multiply(x, y)
402 else:
403 raise RuntimeError(
404 "We currently only support dynamic graph mode or the new IR mode."
405 )
406
407
408def divide(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:

Callers 7

std_implFunction · 0.50
var_implMethod · 0.50
multiply_ad_funcFunction · 0.50
relu_custom_backwardFunction · 0.50

Calls 4

in_dygraph_modeFunction · 0.90
in_pir_modeFunction · 0.90
RuntimeErrorClass · 0.85
floatFunction · 0.50

Tested by

no test coverage detected