You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sparse.py 5.6 kB

5 years ago
optimize the comment and log description 修改: ops/operations/_inner_ops.py 修改: ops/operations/_quant_ops.py 修改: ops/operations/array_ops.py 修改: ops/operations/comm_ops.py 修改: ops/operations/math_ops.py 修改: ops/operations/quantum_ops.py 修改: ops/operations/rl_ops.py 修改: ops/operations/sponge_ops.py 修改: ops/operations/sponge_update_ops.py 修改: train/__init__.py 修改: common/tensor.py 修改: train/serialization.py 修改: ccsrc/pipeline/jit/parse/parse.h 修改: explainer/benchmark/_attribution/metric.py 修改: ops/composite/multitype_ops/_constexpr_utils.py 修改: ops/operations/comm_ops.py 修改: RELEASE.md 修改: mindspore/_extends/parse/standard_method.py 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/concat_offset_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/dynamic_shape_cpu_kernel.cc 修改: mindspore/ccsrc/frontend/parallel/ops_info/reshape_info.cc 修改: mindspore/ccsrc/frontend/parallel/ops_info/tile_info.cc 修改: mindspore/ccsrc/frontend/parallel/ops_info/transpose_info.cc 修改: mindspore/ccsrc/frontend/parallel/strategy.h 修改: mindspore/common/tensor.py 修改: mindspore/core/abstract/prim_arrays.cc 修改: mindspore/core/abstract/prim_nn.cc 修改: mindspore/core/ops/conv2d.cc 修改: mindspore/core/ops/logical_and.h 修改: mindspore/core/ops/logical_not.h 修改: mindspore/core/ops/logical_or.h 修改: mindspore/core/ops/reduce_all.h 修改: mindspore/core/ops/reduce_any.h 修改: mindspore/lite/src/runtime/kernel/arm/fp32_grad/sgd.cc 修改: mindspore/nn/layer/quant.py 修改: mindspore/nn/optim/sgd.py 修改: mindspore/nn/sparse/sparse.py 修改: mindspore/numpy/array_creations.py 修改: mindspore/numpy/array_ops.py 修改: mindspore/numpy/logic_ops.py 修改: mindspore/numpy/math_ops.py 修改: mindspore/ops/operations/_inner_ops.py 修改: mindspore/ops/operations/array_ops.py 修改: mindspore/ops/operations/rl_ops.py 修改: mindspore/train/_utils.py 修改: tests/ut/python/model/test_lenet_core_after_exception.py 修改: mindspore/_extends/parse/standard_method.py 修改: mindspore/ops/operations/rl_ops.py 修改: mindspore/core/abstract/prim_nn.cc 修改: mindspore/core/ops/conv2d.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/ctcloss_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/fl/fused_pull_weight_kernel.h 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/fl/fused_push_weight_kernel.h 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/mkldnn/conv2d_grad_filter_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/mkldnn/conv2d_grad_input_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/ps/sparse_apply_ftrl_ps_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/ps/sparse_apply_lazy_adam_ps_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/scatter_arithmetic_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/split_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/update_cache_cpu_kernel.cc 修改: mindspore/ccsrc/backend/kernel_compiler/gpu/arrays/split_gpu_kernel.h 修改: mindspore/ccsrc/backend/kernel_compiler/gpu/math/broadcast_gpu_kernel.h 修改: mindspore/ccsrc/backend/kernel_compiler/gpu/nn/conv2d_grad_input_gpu_kernel.h 修改: mindspore/ccsrc/fl/server/server.cc 修改: mindspore/ccsrc/frontend/optimizer/ad/kpynative.cc 修改: mindspore/ccsrc/frontend/optimizer/irpass/incorporate_getitem.h 修改: mindspore/ccsrc/frontend/optimizer/irpass/inline.h 修改: mindspore/ccsrc/minddata/dataset/core/device_tensor.cc 修改: mindspore/ccsrc/minddata/dataset/core/tensor.cc 修改: mindspore/ccsrc/minddata/dataset/engine/datasetops/source/emnist_op.cc 修改: mindspore/ccsrc/minddata/dataset/engine/datasetops/source/mnist_op.cc 修改: mindspore/ccsrc/minddata/dataset/engine/datasetops/source/qmnist_op.cc 修改: mindspore/ccsrc/minddata/dataset/engine/ir/datasetops/dataset_node.cc 修改: mindspore/ccsrc/minddata/dataset/engine/opt/pre/epoch_ctrl_pass.cc 修改: mindspore/ccsrc/minddata/dataset/kernels/image/lite_image_utils.cc 修改: mindspore/ccsrc/pipeline/jit/action.cc 修改: mindspore/ccsrc/pipeline/jit/static_analysis/evaluator.cc 修改: mindspore/ccsrc/runtime/device/ascend/executor/tiling/op_tiling_adapter.cc 修改: mindspore/compression/quant/quant_utils.py 修改: mindspore/core/abstract/prim_nn.cc 修改: mindspore/dataset/engine/validators.py 修改: mindspore/lite/micro/coder/opcoders/nnacl/fp32/affine_fp32_coder.cc 修改: mindspore/lite/micro/coder/opcoders/nnacl/int8/affine_int8_coder.cc 修改: mindspore/lite/src/runtime/kernel/ascend310/src/custom_kernel.cc 修改: mindspore/lite/src/runtime/kernel/opencl/kernel/matmul.cc 修改: mindspore/lite/src/runtime/kernel/opencl/kernel/strassen.cc 修改: mindspore/lite/tools/common/graph_util.h 修改: mindspore/lite/tools/optimizer/fisson/fisson_util.cc 修改: mindspore/ops/composite/math_ops.py 修改: mindspore/ops/operations/_inner_ops.py 修改: mindspore/ops/operations/array_ops.py 修改: mindspore/ops/operations/math_ops.py 修改: mindspore/ops/operations/other_ops.py 修改: mindspore/boost/boost_cell_wrapper.py 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/update_cache_cpu_kernel.cc 修改: mindspore/ccsrc/common/trans.cc 修改: mindspore/ccsrc/frontend/parallel/cache_embedding/cache_embedding.cc 修改: mindspore/ccsrc/frontend/parallel/ops_info/gather_info.cc 修改: mindspore/lite/src/common/log_util.h 修改: mindspore/nn/wrap/loss_scale.py 修改: mindspore/parallel/nn/moe.py 修改: tests/mindspore_test_framework/mindspore_test.py 修改: mindspore/ccsrc/backend/kernel_compiler/cpu/split_cpu_kernel.cc 修改: mindspore/lite/tools/common/graph_util.h 修改: mindspore/ccsrc/frontend/parallel/ops_info/gather_info.cc 修改: mindspore/core/ops/conv2d.cc 修改: tests/ut/python/model/test_lenet_core_after_exception.py
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright 2020-2021 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """Sparse related tools."""
  16. from mindspore.ops import operations as P
  17. from ..cell import Cell
  18. class SparseToDense(Cell):
  19. """
  20. Converts a sparse tensor into dense.
  21. In Python, for the ease of use, three tensors are collected into a SparseTensor class.
  22. MindSpore uses three independent dense tensors: indices, value and dense shape to represent the sparse tensor.
  23. Separate indexes, values and dense shape tensors can be wrapped in a Sparse Tensor object
  24. before being passed to the OPS below.
  25. Inputs:
  26. - **sparse_tensor** (:class:`mindspore.SparseTensor`): the sparse tensor to convert.
  27. Outputs:
  28. Tensor, converted from sparse tensor.
  29. Raises:
  30. TypeError: If `sparse_tensor.indices` is not a Tensor.
  31. TypeError: If 'sparse_tensor.values' is not a Tensor.
  32. TypeError: If 'sparse_tensor.dense_shape' is not a tuple.
  33. Supported Platforms:
  34. ``CPU``
  35. Examples:
  36. >>> import mindspore as ms
  37. >>> from mindspore import Tensor, SparseTensor
  38. >>> import mindspore.nn as nn
  39. >>> indices = Tensor([[0, 1], [1, 2]])
  40. >>> values = Tensor([1, 2], dtype=ms.int32)
  41. >>> dense_shape = (3, 4)
  42. >>> sparse_tensor = SparseTensor(indices, values, dense_shape)
  43. >>> sparse_to_dense = nn.SparseToDense()
  44. >>> result = sparse_to_dense(sparse_tensor)
  45. >>> print(result)
  46. [[0 1 0 0]
  47. [0 0 2 0]
  48. [0 0 0 0]]
  49. """
  50. def __init__(self):
  51. """Initialize SparseToDense."""
  52. super(SparseToDense, self).__init__()
  53. self.sparse_to_dense = P.SparseToDense()
  54. def construct(self, sparse_tensor):
  55. return self.sparse_to_dense(sparse_tensor.indices,
  56. sparse_tensor.values,
  57. sparse_tensor.dense_shape)
  58. class SparseTensorDenseMatmul(Cell):
  59. """
  60. Multiplies sparse matrix `a` and dense matrix `b`.
  61. The rank of sparse matrix and dense matrix must be equal to `2`.
  62. Args:
  63. adjoint_st (bool): If true, sparse tensor is transposed before multiplication. Default: False.
  64. adjoint_dt (bool): If true, dense tensor is transposed before multiplication. Default: False.
  65. Inputs:
  66. - **indices** (Tensor) - A 2-D Tensor, represents the position of the element in the sparse tensor.
  67. Support int32, int64, each element value should be non-negative. The shape is :math:`(n, 2)`.
  68. - **values** (Tensor) - A 1-D Tensor, represents the value corresponding to the position in the `indices`.
  69. Support float16, float32, float64, int32, int64. The shape should be :math:`(n,)`.
  70. - **sparse_shape** (tuple) - A positive int tuple which specifies the shape of sparse tensor,
  71. should have 2 elements, represent sparse tensor shape is :math:`(N, C)`.
  72. - **dense** (Tensor) - A 2-D Tensor, the dtype is same as `values`.
  73. If `adjoint_st` is False and `adjoint_dt` is False, the shape must be :math:`(C, M)`.
  74. If `adjoint_st` is False and `adjoint_dt` is True, the shape must be :math:`(M, C)`.
  75. If `adjoint_st` is True and `adjoint_dt` is False, the shape must be :math:`(N, M)`.
  76. If `adjoint_st` is True and `adjoint_dt` is True, the shape must be :math:`(M, N)`.
  77. Outputs:
  78. Tensor, the dtype is the same as `values`.
  79. If `adjoint_st` is False, the shape is :math:`(N, M)`.
  80. If `adjoint_st` is True, the shape is :math:`(C, M)`.
  81. Raises:
  82. TypeError: If the type of `adjoint_st` or `adjoint_dt` is not bool, or the dtype of `indices`,
  83. dtype of `values` and dtype of `dense` don't meet the parameter description.
  84. ValueError: If `sparse_shape`, shape of `indices`, shape of `values`,
  85. and shape of `dense` don't meet the parameter description.
  86. Supported Platforms:
  87. ``CPU``
  88. Examples:
  89. >>> import mindspore as ms
  90. >>> from mindspore import Tensor
  91. >>> from mindspore import nn
  92. >>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
  93. >>> values = Tensor([1, 2], dtype=ms.float32)
  94. >>> sparse_shape = (3, 4)
  95. >>> dense = Tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype=ms.float32)
  96. >>> sparse_dense_matmul = nn.SparseTensorDenseMatmul()
  97. >>> out = sparse_dense_matmul(indices, values, sparse_shape, dense)
  98. >>> print(out)
  99. [[2 2]
  100. [6 6]
  101. [0 0]]
  102. """
  103. def __init__(self, adjoint_st=False, adjoint_dt=False):
  104. """Initialize SparseTensorDenseMatmul"""
  105. super(SparseTensorDenseMatmul, self).__init__()
  106. self.adj_st = adjoint_st
  107. self.adj_dt = adjoint_dt
  108. self.sparse_dense_matmul = P.SparseTensorDenseMatmul(adjoint_st=self.adj_st, adjoint_dt=self.adj_dt)
  109. def construct(self, indices, values, sparse_shape, dense):
  110. return self.sparse_dense_matmul(indices, values, sparse_shape, dense)