multiary.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from paddle import _C_ops
  15. from paddle.base.framework import dygraph_only
  16. __all__ = []
  17. @dygraph_only
  18. def addmm(input, x, y, beta=1.0, alpha=1.0, name=None):
  19. """
  20. Note:
  21. This API is only supported from ``CUDA 11.0`` .
  22. Applies matrix multiplication for `x` and `y` , `input` is added to
  23. the final result. The equation is:
  24. .. math::
  25. out = alpha * x * y + beta * input
  26. The supported input/output Tensor layout are as follows:
  27. Note:
  28. input[SparseCsrTensor] + x[SparseCsrTensor] @ y[SparseCsrTensor] -> out[SparseCsrTensor]
  29. input[DenseTensor] + x[SparseCsrTensor] @ y[DenseTensor] -> out[DenseTensor]
  30. input[SparseCooTensor] + x[SparseCooTensor] @ y[SparseCooTensor] -> out[SparseCooTensor]
  31. input[DenseTensor] + x[SparseCooTensor] @ y[DenseTensor] -> out[DenseTensor]
  32. It supports backward propagation.
  33. Dimensions `input` , `x` , `y` must be same and >= 2D. Automatic broadcasting of Tensor is not supported.
  34. Args:
  35. input (SparseTensor|DenseTensor): The input tensor. Shape is [*, M, N]. The data type can be float32 or float64.
  36. x (SparseTensor): The input SparseTensor. Shape is [*, M, K]. The data type can be float32 or float64.
  37. y (SparseTensor|DenseTensor): The input tensor. Shape is [*, K, N]. The data type can be float32 or float64.
  38. beta (float, optional): Coefficient of `input` . Default: 1.0
  39. alpha (float, optional): Coefficient of `x * y` . Default: 1.0
  40. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  41. Returns:
  42. SparseTensor|DenseTensor: Tensor type, date type and shape is the same with `input` .
  43. Examples:
  44. .. code-block:: python
  45. >>> # doctest: +REQUIRES(env:GPU)
  46. >>> import paddle
  47. >>> paddle.device.set_device('gpu')
  48. >>> # dense + csr @ dense -> dense
  49. >>> input = paddle.rand([3, 2])
  50. >>> crows = [0, 1, 2, 3]
  51. >>> cols = [1, 2, 0]
  52. >>> values = [1., 2., 3.]
  53. >>> x = paddle.sparse.sparse_csr_tensor(crows, cols, values, [3, 3])
  54. >>> y = paddle.rand([3, 2])
  55. >>> out = paddle.sparse.addmm(input, x, y, 3.0, 2.0)
  56. >>> # dense + coo @ dense -> dense
  57. >>> input = paddle.rand([3, 2])
  58. >>> indices = [[0, 1, 2], [1, 2, 0]]
  59. >>> values = [1., 2., 3.]
  60. >>> x = paddle.sparse.sparse_coo_tensor(indices, values, [3, 3])
  61. >>> y = paddle.rand([3, 2])
  62. >>> out = paddle.sparse.addmm(input, x, y, 3.0, 2.0)
  63. """
  64. return _C_ops.sparse_addmm(input, x, y, beta, alpha)