mindspore 2.3.0__cp39-cp39-win_amd64.whl → 2.4.1__cp39-cp39-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mindspore might be problematic. Click here for more details.

Files changed (287) hide show
  1. mindspore/.commit_id +1 -1
  2. mindspore/__init__.py +3 -1
  3. mindspore/_c_dataengine.cp39-win_amd64.pyd +0 -0
  4. mindspore/_c_expression.cp39-win_amd64.pyd +0 -0
  5. mindspore/_c_mindrecord.cp39-win_amd64.pyd +0 -0
  6. mindspore/_checkparam.py +50 -9
  7. mindspore/_extends/parse/compile_config.py +41 -0
  8. mindspore/_extends/parse/parser.py +9 -7
  9. mindspore/_extends/parse/standard_method.py +52 -14
  10. mindspore/_extends/pijit/pijit_func_white_list.py +350 -24
  11. mindspore/amp.py +24 -10
  12. mindspore/avcodec-59.dll +0 -0
  13. mindspore/avdevice-59.dll +0 -0
  14. mindspore/avfilter-8.dll +0 -0
  15. mindspore/avformat-59.dll +0 -0
  16. mindspore/avutil-57.dll +0 -0
  17. mindspore/common/__init__.py +6 -4
  18. mindspore/common/_pijit_context.py +190 -0
  19. mindspore/common/_register_for_tensor.py +2 -1
  20. mindspore/common/_tensor_overload.py +139 -0
  21. mindspore/common/api.py +102 -87
  22. mindspore/common/dump.py +5 -6
  23. mindspore/common/generator.py +1 -7
  24. mindspore/common/hook_handle.py +14 -26
  25. mindspore/common/initializer.py +51 -15
  26. mindspore/common/mindir_util.py +2 -2
  27. mindspore/common/parameter.py +62 -15
  28. mindspore/common/recompute.py +39 -9
  29. mindspore/common/sparse_tensor.py +7 -3
  30. mindspore/common/tensor.py +183 -37
  31. mindspore/communication/__init__.py +1 -1
  32. mindspore/communication/_comm_helper.py +38 -3
  33. mindspore/communication/comm_func.py +315 -60
  34. mindspore/communication/management.py +14 -14
  35. mindspore/context.py +132 -22
  36. mindspore/dataset/__init__.py +1 -1
  37. mindspore/dataset/audio/__init__.py +1 -1
  38. mindspore/dataset/core/config.py +7 -0
  39. mindspore/dataset/core/validator_helpers.py +7 -0
  40. mindspore/dataset/engine/cache_client.py +1 -1
  41. mindspore/dataset/engine/datasets.py +72 -44
  42. mindspore/dataset/engine/datasets_audio.py +7 -7
  43. mindspore/dataset/engine/datasets_standard_format.py +53 -3
  44. mindspore/dataset/engine/datasets_text.py +20 -20
  45. mindspore/dataset/engine/datasets_user_defined.py +174 -104
  46. mindspore/dataset/engine/datasets_vision.py +33 -33
  47. mindspore/dataset/engine/iterators.py +29 -0
  48. mindspore/dataset/engine/obs/util.py +7 -0
  49. mindspore/dataset/engine/queue.py +114 -60
  50. mindspore/dataset/engine/serializer_deserializer.py +2 -2
  51. mindspore/dataset/engine/validators.py +34 -14
  52. mindspore/dataset/text/__init__.py +1 -4
  53. mindspore/dataset/transforms/__init__.py +0 -3
  54. mindspore/dataset/utils/line_reader.py +2 -0
  55. mindspore/dataset/vision/__init__.py +1 -4
  56. mindspore/dataset/vision/utils.py +1 -1
  57. mindspore/dataset/vision/validators.py +2 -1
  58. mindspore/dnnl.dll +0 -0
  59. mindspore/{nn/extend → experimental/es}/__init__.py +4 -11
  60. mindspore/experimental/es/embedding_service.py +883 -0
  61. mindspore/{nn/layer → experimental/es}/embedding_service_layer.py +218 -30
  62. mindspore/experimental/llm_boost/__init__.py +21 -0
  63. mindspore/{nn/extend/layer → experimental/llm_boost/atb}/__init__.py +4 -8
  64. mindspore/experimental/llm_boost/atb/boost_base.py +211 -0
  65. mindspore/experimental/llm_boost/atb/llama_boost.py +115 -0
  66. mindspore/experimental/llm_boost/atb/qwen_boost.py +101 -0
  67. mindspore/experimental/llm_boost/register.py +129 -0
  68. mindspore/experimental/llm_boost/utils.py +31 -0
  69. mindspore/experimental/optim/adamw.py +85 -0
  70. mindspore/experimental/optim/optimizer.py +3 -0
  71. mindspore/hal/__init__.py +3 -3
  72. mindspore/hal/contiguous_tensors_handle.py +175 -0
  73. mindspore/hal/stream.py +18 -0
  74. mindspore/include/api/model_group.h +13 -1
  75. mindspore/include/api/types.h +10 -10
  76. mindspore/include/dataset/config.h +2 -2
  77. mindspore/include/dataset/constants.h +2 -2
  78. mindspore/include/dataset/execute.h +2 -2
  79. mindspore/include/dataset/vision.h +4 -0
  80. mindspore/jpeg62.dll +0 -0
  81. mindspore/log.py +1 -1
  82. mindspore/mindrecord/filewriter.py +68 -51
  83. mindspore/mindspore_backend.dll +0 -0
  84. mindspore/mindspore_common.dll +0 -0
  85. mindspore/mindspore_core.dll +0 -0
  86. mindspore/mindspore_glog.dll +0 -0
  87. mindspore/mindspore_np_dtype.dll +0 -0
  88. mindspore/mindspore_ops.dll +0 -0
  89. mindspore/mint/__init__.py +983 -46
  90. mindspore/mint/distributed/__init__.py +31 -0
  91. mindspore/mint/distributed/distributed.py +254 -0
  92. mindspore/mint/nn/__init__.py +268 -23
  93. mindspore/mint/nn/functional.py +125 -19
  94. mindspore/mint/nn/layer/__init__.py +39 -0
  95. mindspore/mint/nn/layer/activation.py +133 -0
  96. mindspore/mint/nn/layer/normalization.py +477 -0
  97. mindspore/mint/nn/layer/pooling.py +110 -0
  98. mindspore/mint/optim/adamw.py +26 -13
  99. mindspore/mint/special/__init__.py +63 -0
  100. mindspore/multiprocessing/__init__.py +2 -1
  101. mindspore/nn/__init__.py +0 -1
  102. mindspore/nn/cell.py +276 -96
  103. mindspore/nn/layer/activation.py +211 -44
  104. mindspore/nn/layer/basic.py +137 -10
  105. mindspore/nn/layer/embedding.py +137 -2
  106. mindspore/nn/layer/normalization.py +101 -5
  107. mindspore/nn/layer/padding.py +34 -48
  108. mindspore/nn/layer/pooling.py +161 -7
  109. mindspore/nn/layer/transformer.py +3 -3
  110. mindspore/nn/loss/__init__.py +2 -2
  111. mindspore/nn/loss/loss.py +84 -6
  112. mindspore/nn/optim/__init__.py +2 -1
  113. mindspore/nn/optim/adadelta.py +1 -1
  114. mindspore/nn/optim/adam.py +1 -1
  115. mindspore/nn/optim/lamb.py +1 -1
  116. mindspore/nn/optim/tft_wrapper.py +124 -0
  117. mindspore/nn/wrap/cell_wrapper.py +12 -23
  118. mindspore/nn/wrap/grad_reducer.py +5 -5
  119. mindspore/nn/wrap/loss_scale.py +17 -3
  120. mindspore/numpy/__init__.py +1 -1
  121. mindspore/numpy/array_creations.py +65 -68
  122. mindspore/numpy/array_ops.py +64 -60
  123. mindspore/numpy/fft.py +610 -75
  124. mindspore/numpy/logic_ops.py +11 -10
  125. mindspore/numpy/math_ops.py +85 -84
  126. mindspore/numpy/utils_const.py +4 -4
  127. mindspore/opencv_core452.dll +0 -0
  128. mindspore/opencv_imgcodecs452.dll +0 -0
  129. mindspore/opencv_imgproc452.dll +0 -0
  130. mindspore/ops/__init__.py +6 -4
  131. mindspore/ops/_grad_experimental/grad_array_ops.py +0 -11
  132. mindspore/ops/_grad_experimental/grad_comm_ops.py +67 -4
  133. mindspore/ops/_grad_experimental/grad_math_ops.py +0 -22
  134. mindspore/ops/_vmap/vmap_array_ops.py +2 -4
  135. mindspore/ops/_vmap/vmap_math_ops.py +17 -1
  136. mindspore/ops/_vmap/vmap_nn_ops.py +43 -2
  137. mindspore/ops/auto_generate/cpp_create_prim_instance_helper.py +91 -7
  138. mindspore/ops/auto_generate/gen_arg_dtype_cast.py +2 -0
  139. mindspore/ops/auto_generate/gen_extend_func.py +767 -13
  140. mindspore/ops/auto_generate/gen_ops_def.py +2452 -364
  141. mindspore/ops/auto_generate/gen_ops_prim.py +5442 -1756
  142. mindspore/ops/auto_generate/pyboost_inner_prim.py +176 -56
  143. mindspore/ops/composite/base.py +85 -48
  144. mindspore/ops/composite/multitype_ops/_compile_utils.py +1 -0
  145. mindspore/ops/composite/multitype_ops/not_in_impl.py +2 -2
  146. mindspore/ops/function/__init__.py +22 -0
  147. mindspore/ops/function/array_func.py +492 -153
  148. mindspore/ops/function/debug_func.py +113 -1
  149. mindspore/ops/function/fft_func.py +15 -2
  150. mindspore/ops/function/grad/grad_func.py +3 -2
  151. mindspore/ops/function/math_func.py +564 -207
  152. mindspore/ops/function/nn_func.py +817 -383
  153. mindspore/ops/function/other_func.py +3 -2
  154. mindspore/ops/function/random_func.py +402 -12
  155. mindspore/ops/function/reshard_func.py +13 -11
  156. mindspore/ops/function/sparse_unary_func.py +1 -1
  157. mindspore/ops/function/vmap_func.py +3 -2
  158. mindspore/ops/functional.py +24 -14
  159. mindspore/ops/op_info_register.py +3 -3
  160. mindspore/ops/operations/__init__.py +7 -2
  161. mindspore/ops/operations/_grad_ops.py +2 -76
  162. mindspore/ops/operations/_infer_ops.py +1 -1
  163. mindspore/ops/operations/_inner_ops.py +71 -94
  164. mindspore/ops/operations/array_ops.py +14 -146
  165. mindspore/ops/operations/comm_ops.py +63 -53
  166. mindspore/ops/operations/custom_ops.py +83 -19
  167. mindspore/ops/operations/debug_ops.py +42 -10
  168. mindspore/ops/operations/manually_defined/_inner.py +12 -0
  169. mindspore/ops/operations/manually_defined/ops_def.py +273 -20
  170. mindspore/ops/operations/math_ops.py +12 -223
  171. mindspore/ops/operations/nn_ops.py +20 -114
  172. mindspore/ops/operations/other_ops.py +7 -4
  173. mindspore/ops/operations/random_ops.py +46 -1
  174. mindspore/ops/primitive.py +18 -6
  175. mindspore/ops_generate/arg_dtype_cast.py +2 -0
  176. mindspore/ops_generate/gen_aclnn_implement.py +11 -11
  177. mindspore/ops_generate/gen_constants.py +36 -0
  178. mindspore/ops_generate/gen_ops.py +67 -52
  179. mindspore/ops_generate/gen_ops_inner_prim.py +1 -1
  180. mindspore/ops_generate/gen_pyboost_func.py +131 -47
  181. mindspore/ops_generate/op_proto.py +10 -3
  182. mindspore/ops_generate/pyboost_utils.py +14 -1
  183. mindspore/ops_generate/template.py +43 -21
  184. mindspore/parallel/__init__.py +3 -1
  185. mindspore/parallel/_auto_parallel_context.py +31 -9
  186. mindspore/parallel/_cell_wrapper.py +85 -0
  187. mindspore/parallel/_parallel_serialization.py +47 -19
  188. mindspore/parallel/_tensor.py +127 -13
  189. mindspore/parallel/_utils.py +53 -22
  190. mindspore/parallel/algo_parameter_config.py +5 -5
  191. mindspore/parallel/checkpoint_transform.py +46 -39
  192. mindspore/parallel/cluster/process_entity/__init__.py +1 -1
  193. mindspore/parallel/cluster/process_entity/_api.py +31 -23
  194. mindspore/parallel/cluster/process_entity/_utils.py +2 -27
  195. mindspore/parallel/parameter_broadcast.py +3 -4
  196. mindspore/parallel/shard.py +162 -31
  197. mindspore/parallel/transform_safetensors.py +1146 -0
  198. mindspore/profiler/__init__.py +2 -1
  199. mindspore/profiler/common/constant.py +29 -0
  200. mindspore/profiler/common/registry.py +47 -0
  201. mindspore/profiler/common/util.py +28 -0
  202. mindspore/profiler/dynamic_profiler.py +694 -0
  203. mindspore/profiler/envprofiling.py +17 -19
  204. mindspore/profiler/parser/ascend_analysis/constant.py +18 -0
  205. mindspore/profiler/parser/ascend_analysis/file_manager.py +25 -4
  206. mindspore/profiler/parser/ascend_analysis/function_event.py +43 -19
  207. mindspore/profiler/parser/ascend_analysis/fwk_cann_parser.py +31 -26
  208. mindspore/profiler/parser/ascend_analysis/fwk_file_parser.py +56 -10
  209. mindspore/profiler/parser/ascend_analysis/msprof_timeline_parser.py +55 -8
  210. mindspore/profiler/parser/ascend_analysis/path_manager.py +313 -0
  211. mindspore/profiler/parser/ascend_analysis/profiler_info_parser.py +27 -20
  212. mindspore/profiler/parser/ascend_analysis/trace_event_manager.py +9 -2
  213. mindspore/profiler/parser/ascend_msprof_exporter.py +5 -4
  214. mindspore/profiler/parser/ascend_timeline_generator.py +27 -25
  215. mindspore/profiler/parser/base_timeline_generator.py +19 -25
  216. mindspore/profiler/parser/cpu_gpu_timeline_generator.py +25 -12
  217. mindspore/profiler/parser/framework_parser.py +1 -391
  218. mindspore/profiler/parser/gpu_analysis/__init__.py +14 -0
  219. mindspore/profiler/parser/gpu_analysis/function_event.py +44 -0
  220. mindspore/profiler/parser/gpu_analysis/fwk_file_parser.py +89 -0
  221. mindspore/profiler/parser/gpu_analysis/profiler_info_parser.py +72 -0
  222. mindspore/profiler/parser/memory_usage_parser.py +0 -154
  223. mindspore/profiler/parser/profiler_info.py +78 -6
  224. mindspore/profiler/profiler.py +153 -0
  225. mindspore/profiler/profiling.py +285 -413
  226. mindspore/rewrite/__init__.py +1 -2
  227. mindspore/rewrite/common/namespace.py +4 -4
  228. mindspore/rewrite/symbol_tree/symbol_tree.py +3 -3
  229. mindspore/run_check/_check_version.py +39 -104
  230. mindspore/safeguard/rewrite_obfuscation.py +591 -247
  231. mindspore/swresample-4.dll +0 -0
  232. mindspore/swscale-6.dll +0 -0
  233. mindspore/tinyxml2.dll +0 -0
  234. mindspore/train/__init__.py +4 -3
  235. mindspore/train/_utils.py +105 -19
  236. mindspore/train/amp.py +171 -53
  237. mindspore/train/callback/__init__.py +2 -2
  238. mindspore/train/callback/_callback.py +4 -4
  239. mindspore/train/callback/_checkpoint.py +97 -31
  240. mindspore/train/callback/_cluster_monitor.py +1 -1
  241. mindspore/train/callback/_flops_collector.py +1 -0
  242. mindspore/train/callback/_loss_monitor.py +3 -3
  243. mindspore/train/callback/_on_request_exit.py +145 -31
  244. mindspore/train/callback/_summary_collector.py +5 -5
  245. mindspore/train/callback/_tft_register.py +375 -0
  246. mindspore/train/dataset_helper.py +15 -3
  247. mindspore/train/metrics/metric.py +3 -3
  248. mindspore/train/metrics/roc.py +4 -4
  249. mindspore/train/mind_ir_pb2.py +44 -39
  250. mindspore/train/model.py +154 -58
  251. mindspore/train/serialization.py +342 -128
  252. mindspore/turbojpeg.dll +0 -0
  253. mindspore/utils/__init__.py +21 -0
  254. mindspore/utils/utils.py +60 -0
  255. mindspore/version.py +1 -1
  256. {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/METADATA +13 -7
  257. {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/RECORD +260 -254
  258. {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/WHEEL +1 -1
  259. mindspore/include/c_api/ms/abstract.h +0 -67
  260. mindspore/include/c_api/ms/attribute.h +0 -197
  261. mindspore/include/c_api/ms/base/handle_types.h +0 -43
  262. mindspore/include/c_api/ms/base/macros.h +0 -32
  263. mindspore/include/c_api/ms/base/status.h +0 -33
  264. mindspore/include/c_api/ms/base/types.h +0 -283
  265. mindspore/include/c_api/ms/context.h +0 -102
  266. mindspore/include/c_api/ms/graph.h +0 -160
  267. mindspore/include/c_api/ms/node.h +0 -606
  268. mindspore/include/c_api/ms/tensor.h +0 -161
  269. mindspore/include/c_api/ms/value.h +0 -84
  270. mindspore/mindspore_shared_lib.dll +0 -0
  271. mindspore/nn/extend/basic.py +0 -140
  272. mindspore/nn/extend/embedding.py +0 -143
  273. mindspore/nn/extend/layer/normalization.py +0 -109
  274. mindspore/nn/extend/pooling.py +0 -117
  275. mindspore/nn/layer/embedding_service.py +0 -531
  276. mindspore/ops/_op_impl/aicpu/strided_slice_v2.py +0 -93
  277. mindspore/ops/_op_impl/aicpu/strided_slice_v2_grad.py +0 -66
  278. mindspore/ops/extend/__init__.py +0 -53
  279. mindspore/ops/extend/array_func.py +0 -218
  280. mindspore/ops/extend/math_func.py +0 -76
  281. mindspore/ops/extend/nn_func.py +0 -308
  282. mindspore/ops/silent_check.py +0 -162
  283. mindspore/profiler/parser/msadvisor_analyzer.py +0 -82
  284. mindspore/profiler/parser/msadvisor_parser.py +0 -240
  285. mindspore/train/callback/_mindio_ttp.py +0 -443
  286. {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/entry_points.txt +0 -0
  287. {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/top_level.txt +0 -0
@@ -29,7 +29,6 @@ import mindspore.common.dtype as mstype
29
29
  from mindspore.ops.function.math_func import logsumexp
30
30
  from mindspore.ops.function.random_func import _get_seed, _set_prim_op_user_data
31
31
  from mindspore.common.tensor import Tensor
32
- from mindspore.common.parameter import Parameter
33
32
  from mindspore._c_expression import Tensor as Tensor_
34
33
  from mindspore.ops._primitive_cache import _get_cache_prim
35
34
  from mindspore import _checkparam as validator
@@ -41,13 +40,22 @@ from mindspore.ops.operations.nn_ops import ChannelShuffle
41
40
  from mindspore.ops.operations.nn_ops import TripletMarginLoss
42
41
  from mindspore.ops.operations._sequence_ops import TupleToTensor, TensorToTuple, ListToTensor
43
42
  from mindspore.common.api import _function_forbid_reuse
44
- from mindspore.ops.auto_generate import log_softmax, dense, prelu, celu, relu, fast_gelu, silu, elu, sigmoid, relu6
45
- from mindspore.ops.auto_generate import group_norm_op, rms_norm, layer_norm_ext_op, batch_norm_ext_op
46
- from mindspore.ops.auto_generate import (reflection_pad_1d_op, reflection_pad_2d_op, reflection_pad_3d_op, # pylint: disable=W0611
43
+ from mindspore.ops.auto_generate import log_softmax, dense, prelu, celu, relu, fast_gelu, silu, elu, sigmoid, relu6, \
44
+ softmax_impl, swiglu, logsigmoid_op
45
+ from mindspore.ops.auto_generate import group_norm_op, rms_norm, layer_norm_ext_op, batch_norm_ext_op, mse_loss_ext
46
+ from mindspore.ops.auto_generate import (reflection_pad_1d_op, reflection_pad_2d_op, add_layernorm_v2_op,
47
+ reflection_pad_3d_op, # pylint: disable=W0611
47
48
  replication_pad_1d_op, replication_pad_2d_op, replication_pad_3d_op,
48
- constant_pad_nd_op, dropout_ext_op, reverse_v2_impl)
49
- from mindspore.ops.auto_generate.gen_ops_prim import embedding_op, Convolution
49
+ constant_pad_nd_op, dropout_ext_op, reverse_v2_impl, avg_pool2d_op,
50
+ upsample_nearest1d_op, upsample_nearest2d_op, upsample_nearest3d_op,
51
+ upsample_linear1d_op, upsample_bilinear2d_op, upsample_bicubic2d_op,
52
+ upsample_trilinear3d_impl, fill_scalar_op, floor_op)
53
+ from mindspore.ops.auto_generate.gen_ops_prim import embedding_op, Convolution, ConstantPadND, MaxPoolWithIndices, \
54
+ MaxPoolWithMask
50
55
  from mindspore.common.generator import default_generator
56
+ from mindspore.ops.auto_generate import hardshrink, hardsigmoid, hardswish
57
+ from mindspore.ops.auto_generate import softshrink
58
+ from mindspore.ops.auto_generate import adaptive_avg_pool2d_ext_op
51
59
 
52
60
  abs_ = P.Abs()
53
61
  add_ = P.Add()
@@ -64,7 +72,6 @@ gather_ = P.Gather()
64
72
  gather_d_ = P.GatherD()
65
73
  gelu_ = P.GeLU()
66
74
  greater_ = P.Greater()
67
- hardswish_ = P.HSwish()
68
75
  less_ = P.Less()
69
76
  list_to_tensor_ = ListToTensor()
70
77
  log_ = P.Log()
@@ -106,6 +113,7 @@ check_string_const = constexpr(validator.check_string)
106
113
 
107
114
  generator_step_ = Tensor(1, mstype.int64)
108
115
 
116
+
109
117
  def adaptive_avg_pool2d(input, output_size):
110
118
  r"""
111
119
  Performs 2D adaptive average pooling on a multi-plane input signal.
@@ -200,6 +208,92 @@ def adaptive_avg_pool2d(input, output_size):
200
208
  return adaptive_avgpool2d_(input)
201
209
 
202
210
 
211
+ def adaptive_avg_pool2d_ext(input, output_size):
212
+ r"""
213
+ Performs 2D adaptive average pooling on a multi-plane input signal.
214
+ That is, for any input size, the size of the specified output is H x W.
215
+ The number of output features is equal to the number of input features.
216
+
217
+ The input and output data format can be "NCHW" and "CHW". N is the batch size, C is the number of channels,
218
+ H is the feature height, and W is the feature width.
219
+
220
+ For adaptive average pooling for 2D:
221
+
222
+ .. math::
223
+ \begin{align}
224
+ h_{start} &= floor(i * H_{in} / H_{out})\\
225
+ h_{end} &= ceil((i + 1) * H_{in} / H_{out})\\
226
+ w_{start} &= floor(j * W_{in} / W_{out})\\
227
+ w_{end} &= ceil((j + 1) * W_{in} / W_{out})\\
228
+ Output(i,j) &= \frac{\sum Input[h_{start}:h_{end}, w_{start}:w_{end}]}{(h_{end}- h_{start})
229
+ * (w_{end}- w_{start})}
230
+ \end{align}
231
+
232
+ .. warning::
233
+ This is an experimental API that is subject to change or deletion.
234
+
235
+ Args:
236
+ input (Tensor): The input of adaptive_avg_pool2d, which is a 3D or 4D tensor,
237
+ with float16 or float32 data type.
238
+ output_size (Union[int, tuple]): The target output size. `output_size` can be a tuple :math:`(H, W)`,
239
+ or an int H for :math:`(H, H)`. :math:`H` and :math:`W` can be int or None.
240
+ If it is None, it means the output size is the same as the input size.
241
+
242
+ Returns:
243
+ Tensor, with the same type as the `input`.
244
+
245
+ Shape of the output is `input_shape[:len(input_shape) - len(out_shape)] + out_shape`.
246
+
247
+ .. math::
248
+
249
+ out\_shape = \begin{cases}
250
+ input\_shape[-2] + output\_size[1], & \text{if } output\_size text{ is (None, w);}\\
251
+ output\_size[0] + input\_shape[-1], & \text{if } output\_size text{ is (h, None);}\\
252
+ input\_shape[-2:], & \text{if } output\_size text{ is (None, None);}\\
253
+ (h, h), & \text{if } output\_size text{ is h;}\\
254
+ (h, w), & \text{if } output\_size text{ is (h, w)}
255
+ \end{cases}
256
+
257
+ Raises:
258
+ ValueError: If `output_size` is a tuple and the length of `output_size` is not 2.
259
+ TypeError: If `input` is not a Tensor.
260
+ TypeError: If dtype of `input` is not float16, float32 or float64.
261
+ ValueError: If the dimension of `input` is less than or equal to the dimension of `output_size`.
262
+
263
+ Supported Platforms:
264
+ ``Ascend``
265
+
266
+ Examples:
267
+ >>> import mindspore
268
+ >>> import numpy as np
269
+ >>> from mindspore import Tensor, mint
270
+ >>> # case 1: output_size=(3, 2)
271
+ >>> input = Tensor(np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
272
+ ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
273
+ ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]), mindspore.float32)
274
+ >>> output = mint.nn.functional.adaptive_avg_pool2d(input, (3, 2))
275
+ >>> print(output)
276
+ [[[1.5 2.5]
277
+ [4.5 5.5]
278
+ [7.5 8.5]]
279
+ [[1.5 2.5]
280
+ [4.5 5.5]
281
+ [7.5 8.5]]
282
+ [[1.5 2.5]
283
+ [4.5 5.5]
284
+ [7.5 8.5]]]
285
+ """
286
+ output_size_ = None
287
+ if isinstance(output_size, int):
288
+ output_size_ = output_size
289
+ else:
290
+ origin_shape = shape_(input)
291
+ w_ = origin_shape[-2] if output_size[-2] is None else output_size[-2]
292
+ h_ = origin_shape[-1] if output_size[-1] is None else output_size[-1]
293
+ output_size_ = (w_, h_)
294
+ return adaptive_avg_pool2d_ext_op(input, output_size_)
295
+
296
+
203
297
  def adaptive_avg_pool3d(input, output_size):
204
298
  r"""
205
299
  Performs 3D adaptive average pooling on a multi-plane input signal.
@@ -446,6 +540,9 @@ def avg_pool2d(input_x, kernel_size=1, stride=1, padding=0, ceil_mode=False, cou
446
540
  .. warning::
447
541
  `kernel_size` is in the range `[1, 255]`. `stride` is in the range `[1, 63]`.
448
542
 
543
+ Note:
544
+ This interface currently does not support Atlas A2 training series products.
545
+
449
546
  Args:
450
547
  input_x (Tensor): Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
451
548
  kernel_size (Union[int, tuple[int]]): The size of kernel used to take the average value. It is an int number
@@ -518,37 +615,41 @@ def avg_pool2d(input_x, kernel_size=1, stride=1, padding=0, ceil_mode=False, cou
518
615
  def avg_pool2d_ext(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True,
519
616
  divisor_override=None):
520
617
  r"""
521
- Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes.
522
- Typically the input is of shape :math:`(N, C, H_{in}, W_{in})`, outputs regional average in the
523
- :math:`(H_{in}, W_{in})`-dimension. Given kernel size :math:`(k_{H}, k_{W})` and `stride` , the operation
524
- is as follows.
618
+ Applies a 2D average pooling over an input Tensor which can be regarded as a composition of
619
+ 2D input planes. Typically the input is of shape :math:`(N, C, H_{in}, W_{in})` ,
620
+ outputs regional average in the :math:`(H_{in}, W_{in})` -dimension.
621
+ Given kernel size :math:`(kH, kW)` and `stride` , the operation is as follows.
622
+
623
+ .. note::
624
+ On the Atlas platform, when calculating the input, the precision is degraded from float32 to float16.
525
625
 
526
626
  .. math::
527
- \text{output}(N_i, C_j, h, w) = \frac{1}{k_{H} * k_{W}} \sum_{m=0}^{k_{H}-1} \sum_{n=0}^{k_{W}-1}
627
+ \text{output}(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1}
528
628
  \text{input}(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)
529
629
 
530
630
  Args:
531
- input (Tensor): Tensor of shape :math:`(N, C, H_{in}, W_{in})`.
631
+ input (Tensor): Tensor of shape :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
532
632
  kernel_size (Union[int, tuple[int], list[int]]): The size of kernel used to take the average value.
533
- Can be a single number or a tuple (kH, kW).
534
- stride (Union[int, tuple[int], list[int]]): The distance of kernel moving. Can be a single number or
535
- a tuple (sH, sW). Default value is `kernel_size` .
536
- padding (Union(int, tuple[int], list[int])): Implicit zero padding to be added on both sides.
537
- Can be a single number or a tuple (padH, padW). Default: 0.
538
- ceil_mode (bool): If True, apply ceil instead of floor to compute the output shape.
633
+ Can be a single number or a tuple :math:`(kH, kW)` .
634
+ stride (Union[int, tuple[int], list[int]], optional): The distance of kernel moving.
635
+ Can be a single number or a tuple :math:`(sH, sW)` . Default: ``None``,
636
+ where its value is equal to `kernel_size`.
637
+ padding (Union[int, tuple[int], list[int]], optional): Implicit zero padding to be added on both sides.
638
+ Can be a single number or a tuple :math:`(padH, padW)` . Default: ``0``.
639
+ ceil_mode (bool, optional): If True, apply ceil instead of floor to compute the output shape.
539
640
  Default: ``False``.
540
- count_include_pad (bool): If True, include the zero-padding in the averaging calculation.
641
+ count_include_pad (bool, optional): If True, include the zero-padding in the averaging calculation.
541
642
  Default: ``True`` .
542
- divisor_override (int): If specified, it will be used as divisor in the averaging calculation,
643
+ divisor_override (int, optional): If specified, it will be used as divisor in the averaging calculation,
543
644
  otherwise size of pooling region will be used. Default: ``None``.
544
645
 
545
646
  Returns:
546
- Tensor, with shape :math:`(N, C, H_{out}, W_{out})`.
647
+ Tensor, with shape :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`.
547
648
 
548
649
  .. math::
549
650
  \begin{array}{ll} \\
550
- H_{out} = \frac{H_{in} + 2 \times padding[0] - kernel_size[0]}{stride[0]} + 1 \\
551
- W_{out} = \frac{W_{in} + 2 \times padding[1] - kernel_size[1]}{stride[1]} + 1
651
+ H_{out} = \frac{H_{in} + 2 \times padding[0] - kernel\_size[0]}{stride[0]} + 1 \\
652
+ W_{out} = \frac{W_{in} + 2 \times padding[1] - kernel\_size[1]}{stride[1]} + 1
552
653
  \end{array}
553
654
 
554
655
  Raises:
@@ -556,11 +657,10 @@ def avg_pool2d_ext(input, kernel_size, stride=None, padding=0, ceil_mode=False,
556
657
  TypeError: If `kernel_size` or `stride` is neither int nor tuple.
557
658
  TypeError: If `ceil_mode` or `count_include_pad` is not a bool.
558
659
  TypeError: If `divisor_override` is not an int or None.
559
- ValueError: If the dimension of `input` is not equal to `4` or `3`.
660
+ ValueError: If the dimension of `input` is not equal to `3` or `4`.
560
661
  ValueError: If `kernel_size` or `stride` is less than 1.
561
- ValueError: If `kernel_size` or `stride` is a tuple whose length is not equal to `2` or `1`.
562
- ValueError: If `padding` is neither a int nor a tuple whose length is equal to `2` or `1`.
563
662
  ValueError: If value of `padding` is less than `0`.
663
+ ValueError: If `kernel_size`, `padding` or `stride` is a tuple whose length is not equal to `1` or `2`.
564
664
 
565
665
  Supported Platforms:
566
666
  ``Ascend``
@@ -573,16 +673,15 @@ def avg_pool2d_ext(input, kernel_size, stride=None, padding=0, ceil_mode=False,
573
673
  >>> output = ops.function.nn_func.avg_pool2d_ext(x, kernel_size=2, stride=1)
574
674
  >>> print(output)
575
675
  [[[[ 2.5 3.5 4.5]
576
- [ 6.5 7.5 8.5]]
577
- [[14.5 15.5 16.5]
578
- [18.5 19.5 20.5]]
579
- [[26.5 27.5 28.5]
580
- [30.5 31.5 32.5]]]]
676
+ [ 6.5 7.5 8.5]]
677
+ [[14.5 15.5 16.5]
678
+ [18.5 19.5 20.5]]
679
+ [[26.5 27.5 28.5]
680
+ [30.5 31.5 32.5]]]]
581
681
  """
582
682
  if stride is None:
583
683
  stride = kernel_size
584
- return _get_cache_prim(ops.auto_generate.AvgPool2D)()(input, kernel_size, stride, padding,
585
- ceil_mode, count_include_pad, divisor_override)
684
+ return avg_pool2d_op(input, kernel_size, stride, padding, ceil_mode, count_include_pad, divisor_override)
586
685
 
587
686
 
588
687
  def _check_avg_pool3d_padding(padding):
@@ -615,6 +714,9 @@ def avg_pool3d(input_x, kernel_size=1, stride=1, padding=0, ceil_mode=False, cou
615
714
  .. warning::
616
715
  `kernel_size` is in the range `[1, 255]`. `stride` is in the range `[1, 63]`.
617
716
 
717
+ Note:
718
+ This interface currently does not support Atlas A2 training series products.
719
+
618
720
  Args:
619
721
  input_x (Tensor): Tensor of shape :math:`(N, C, D_{in}, H_{in}, W_{in})`. Currently support float16 and
620
722
  float32 data type.
@@ -875,7 +977,7 @@ def adaptive_max_pool3d(input, output_size, return_indices=False):
875
977
  width respectively. The value must be a positive integer. If it is None, the output size and
876
978
  input size of the corresponding dimension are the same.
877
979
  return_indices (bool, optional): If `return_indices` is `True`, the indices of max value would be output,
878
- Otherwise, it will not be output. Default: `False`.
980
+ Otherwise, it will not be output. Default: ``False``.
879
981
 
880
982
  Returns:
881
983
  - **y** (Tensor) - Tensor, with the same number of dims and data type as the `input`.
@@ -1227,7 +1329,8 @@ def binary_cross_entropy_with_logits(input, target, weight=None, pos_weight=None
1227
1329
  Adds sigmoid activation function to input `input` as logits, and uses the given logits to compute binary cross
1228
1330
  entropy between the `input` and the `target`.
1229
1331
 
1230
- Sets input `input` as :math:`X`, input target as :math:`Y`, input weight as :math:`W`, output as :math:`L`. Then,
1332
+ Sets input `input` as :math:`X`, input `target` as :math:`Y`, input `weight` as :math:`W`, output as :math:`L`.
1333
+ Then,
1231
1334
 
1232
1335
  .. math::
1233
1336
 
@@ -1268,15 +1371,19 @@ def binary_cross_entropy_with_logits(input, target, weight=None, pos_weight=None
1268
1371
  :math:`P_c>1` increases the recall, :math:`P_c<1` increases the precision.
1269
1372
 
1270
1373
  Args:
1271
- input (Tensor): Input `input`. Data type must be float16 or float32.
1374
+ input (Tensor): Input `input`. Data type must be float16, float32 or bfloat16(only Atlas A2 series products
1375
+ are supported).
1272
1376
  target (Tensor): Ground truth label, has the same shape as `input`.
1273
- Data type must be float16 or float32.
1377
+ Data type must be float16, float32 or bfloat16(only Atlas A2 series products
1378
+ are supported).
1274
1379
  weight (Tensor, optional): A rescaling weight applied to the loss of each batch element. It can be
1275
- broadcast to a tensor with shape of `input`. Data type must be float16 or float32.
1276
- Default: ``None``, `weight` is a Tensor whose value is ``1``.
1380
+ broadcast to a tensor with shape of `input`. Data type must be float16, float32 or bfloat16(only Atlas A2
1381
+ series products are supported).
1382
+ Default: ``None``, it equals to `weight` is a Tensor whose value is ``1``.
1277
1383
  pos_weight (Tensor, optional): A weight of positive examples. Must be a vector with length equal to the
1278
1384
  number of classes. It can be broadcast to a tensor with shape of `input`.
1279
- Data type must be float16 or float32. Default: ``None``, `pos_weight` is a Tensor whose value is ``1``.
1385
+ Data type must be float16, float32 or bfloat16(only Atlas A2 series products are supported).
1386
+ Default: ``None``, it equals to `pos_weight` is a Tensor whose value is ``1``.
1280
1387
  reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1281
1388
  ``'sum'`` . Default: ``'mean'`` .
1282
1389
 
@@ -1290,7 +1397,7 @@ def binary_cross_entropy_with_logits(input, target, weight=None, pos_weight=None
1290
1397
 
1291
1398
  Raises:
1292
1399
  TypeError: If input `input`, `target`, `weight`, `pos_weight` is not Tensor.
1293
- TypeError: If data type of input `input`, `target`, `weight`, `pos_weight` is neither float16 nor float32.
1400
+ TypeError: If data type of input `input`, `target`, `weight`, `pos_weight` is not float16, float32 or bfloat16.
1294
1401
  TypeError: If data type of input `reduction` is not string.
1295
1402
  ValueError: If `weight` or `pos_weight` can not be broadcast to a tensor with shape of `input`.
1296
1403
  ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` or ``'sum'``.
@@ -1323,6 +1430,10 @@ def dropout(input, p=0.5, training=True, seed=None):
1323
1430
  avoid overfitting. And the return will be multiplied by :math:`\frac{1}{1-p}` during training.
1324
1431
  During the reasoning, this operation returns the same Tensor as the `x`.
1325
1432
 
1433
+ .. warning::
1434
+ The Ascend backend does not support the reproducibility of random numbers, so
1435
+ the `seed` parameter has no effect.
1436
+
1326
1437
  Args:
1327
1438
  input (Tensor): The input Tensor of shape :math:`(*, N)`, with data type of float16, float32 or float64.
1328
1439
  p (float, optional): The dropping rate, between 0 and 1, e.g. p = 0.1,
@@ -1911,55 +2022,6 @@ def kl_div(logits, labels, reduction='mean'):
1911
2022
  return _get_cache_prim(P.KLDivLoss)(reduction=reduction)(logits, labels)
1912
2023
 
1913
2024
 
1914
- def hardshrink(x, lambd=0.5):
1915
- r"""
1916
- Hard Shrink activation function. Calculates the output according to the input elements.
1917
-
1918
- The formula is defined as follows:
1919
-
1920
- .. math::
1921
- \text{HardShrink}(x) =
1922
- \begin{cases}
1923
- x, & \text{ if } x > \lambda \\
1924
- x, & \text{ if } x < -\lambda \\
1925
- 0, & \text{ otherwise }
1926
- \end{cases}
1927
-
1928
- HShrink Activation Function Graph:
1929
-
1930
- .. image:: ../images/HShrink.png
1931
- :align: center
1932
-
1933
- Args:
1934
- x (Tensor): The input of Hard Shrink with data type of float16 or float32.
1935
- lambd (float, optional): The threshold :math:`\lambda` defined by the Hard Shrink formula.
1936
- Default: ``0.5`` .
1937
-
1938
- Returns:
1939
- Tensor, has the same data type and shape as the input `x`.
1940
-
1941
- Raises:
1942
- TypeError: If `lambd` is not a float.
1943
- TypeError: If `x` is not a tensor.
1944
- TypeError: If dtype of `x` is neither float16 nor float32.
1945
-
1946
- Supported Platforms:
1947
- ``Ascend`` ``GPU`` ``CPU``
1948
-
1949
- Examples:
1950
- >>> import mindspore
1951
- >>> import numpy as np
1952
- >>> from mindspore import Tensor, ops
1953
- >>> x = Tensor(np.array([[ 0.5, 1, 2.0], [0.0533,0.0776,-2.1233]]), mindspore.float32)
1954
- >>> output = ops.hardshrink(x)
1955
- >>> print(output)
1956
- [[ 0. 1. 2. ]
1957
- [ 0. 0. -2.1233]]
1958
- """
1959
- hshrink_op = _get_cache_prim(P.HShrink)(lambd)
1960
- return hshrink_op(x)
1961
-
1962
-
1963
2025
  @constexpr
1964
2026
  def _check_axis_in_range(axis, ndim):
1965
2027
  """Checks axes are with the bounds of ndim"""
@@ -2148,48 +2210,6 @@ def is_floating_point(input):
2148
2210
  return input.dtype in [mstype.float32, mstype.bfloat16, mstype.float16, mstype.float64]
2149
2211
 
2150
2212
 
2151
- def hardswish(x):
2152
- r"""
2153
- Applies hswish-type activation element-wise. The input is a Tensor with any valid shape.
2154
-
2155
- Hard swish is defined as:
2156
-
2157
- .. math::
2158
-
2159
- \text{hswish}(x_{i}) = x_{i} * \frac{ReLU6(x_{i} + 3)}{6}
2160
-
2161
- where :math:`x_i` is an element of the input Tensor.
2162
-
2163
- HSwish Activation Function Graph:
2164
-
2165
- .. image:: ../images/HSwish.png
2166
- :align: center
2167
-
2168
- Args:
2169
- x (Tensor): The input to compute the Hard Swish.
2170
-
2171
- Returns:
2172
- Tensor, has the same data type and shape as the input.
2173
-
2174
- Raises:
2175
- TypeError: If `x` is not a Tensor.
2176
- TypeError: If dtype of `x` is not int or float.
2177
-
2178
- Supported Platforms:
2179
- ``Ascend`` ``GPU`` ``CPU``
2180
-
2181
- Examples:
2182
- >>> import mindspore
2183
- >>> import numpy as np
2184
- >>> from mindspore import Tensor, ops
2185
- >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
2186
- >>> output = ops.hardswish(x)
2187
- >>> print(output)
2188
- [-0.3333 -0.3333 0 1.666 0.6665]
2189
- """
2190
- return hardswish_(x)
2191
-
2192
-
2193
2213
  def _is_dim_unknown(shape):
2194
2214
  return isinstance(shape, tuple) and -2 in shape
2195
2215
 
@@ -2616,14 +2636,7 @@ def _interploate_ext_make_tuple(input, value):
2616
2636
  if F.isconstant(value) and F.isconstant(rank):
2617
2637
  out = tuple([value for _ in range(rank)])
2618
2638
  else:
2619
- s = tuple_to_tensor_((rank,), mstype.int32)
2620
- v = None
2621
- if isinstance(value, int):
2622
- v = F.scalar_to_tensor(value, mstype.int64)
2623
- else:
2624
- v = F.scalar_to_tensor(value, mstype.float32)
2625
- t = fillv2_(s, v)
2626
- out = tensor_to_tuple_(t)
2639
+ out = tensor_to_tuple_(fill_scalar_op((rank,), value, None))
2627
2640
  return out
2628
2641
 
2629
2642
 
@@ -2638,11 +2651,9 @@ def _interpolate_ext_scale_factor_convert_size(input, scale_factor):
2638
2651
  size = tuple([floor(shape[i + 2] * scale_factor[i])
2639
2652
  for i in range(tuple_len)])
2640
2653
  else:
2641
- x = tuple_to_tensor_(shape[2:], mstype.int64)
2642
- y = tuple_to_tensor_(scale_factor, mstype.float32)
2643
- t = x * y
2644
- t = ops.TruncateDiv()(t, Tensor(1))
2645
- t = ops.cast(t, mstype.int64)
2654
+ x = tuple_to_tensor_(shape[2:], mstype.float32)
2655
+ y = tuple_to_tensor_(tuple(scale_factor), mstype.float32)
2656
+ t = ops.cast(floor_op(x * y), mstype.int64)
2646
2657
  size = tensor_to_tuple_(t)
2647
2658
  return size
2648
2659
 
@@ -2656,9 +2667,15 @@ def interpolate_ext(input,
2656
2667
  r"""
2657
2668
  Samples the input Tensor to the given size or scale_factor by using one of the interpolate algorithms.
2658
2669
 
2670
+ .. warnings:
2671
+ This is an experimental API that is subject to change or deletion.
2672
+
2659
2673
  .. note::
2660
- - In 'linear' mode, backpropagation does not support scenarios where `scale_factor` is not None
2661
- and `align_corners` is False.
2674
+ - In 'linear' mode, the scenarios, where `scale_factor` is not None and `align_corners` is False,
2675
+ is not supported.
2676
+ - In 'nearest' mode, there may exist precision problem in the scenarios, where input is 3-D/4-D Tensor
2677
+ and the image is scaled by scale_factor.
2678
+ - `mode` and `scale_factor` should be constants.
2662
2679
 
2663
2680
  Args:
2664
2681
  input (Tensor): Tensor to be resized.
@@ -2673,10 +2690,8 @@ def interpolate_ext(input,
2673
2690
  after removing the first two dimensions N, C.
2674
2691
  One and only one of size and scale_factor can be set to None. Default: ``None`` .
2675
2692
  mode (str): The sampling algorithm.
2676
- One of 'nearest', 'linear' (3D only), 'bilinear' (4D only), 'trilinear' (5D only), 'bicubic' (4D only),
2677
- 'area', 'nearest-exact'(matches Scikit-Image and PIL nearest neighbours interpolation algorithms and fixes
2678
- knows issues with `nearest`, 3D and 4D). Default: ``"nearest"`` .
2679
-
2693
+ One of 'nearest', 'linear' (3D only), 'bilinear' (4D only), 'trilinear' (5D only), and 'bicubic' (4D only).
2694
+ Default: ``"nearest"`` .
2680
2695
  align_corners (bool): Whether to use corner alignment for coordinate mapping. Assuming a transformation is
2681
2696
  applied to the input Tensor along the x-axis, the specific calculation formula is as follows:
2682
2697
 
@@ -2698,39 +2713,24 @@ def interpolate_ext(input,
2698
2713
  and finally scaled using the value of `size`.
2699
2714
  If False, the value of `size` or `scale_factor` will be used for direct interpolation. Default: ``None`` .
2700
2715
 
2701
- .. note::
2702
- The 'nearest-exact' mode is the same as the nearest-neighbor interpolation algorithm used in
2703
- scikit-image and PIL. The 'nearest' mode produces the same results as the INTER_NEAREST interpolation
2704
- algorithm used in OpenCV.
2705
-
2706
2716
  Args Support List and Supported Platforms:
2707
2717
 
2708
2718
  +---------------+-----------+---------------+--------------+----------------+
2709
2719
  | mode | input.dim | align_corners | scale_factor | device |
2710
2720
  +===============+===========+===============+==============+================+
2711
- | nearest | 3 | \- | √ | Ascend,GPU,CPU |
2712
- +---------------+-----------+---------------+--------------+----------------+
2713
- | | 4 | \- | √ | Ascend,GPU,CPU |
2714
- +---------------+-----------+---------------+--------------+----------------+
2715
- | | 5 | \- | √ | Ascend,GPU,CPU |
2716
- +---------------+-----------+---------------+--------------+----------------+
2717
- | linear | 3 | √ | √ | Ascend,GPU,CPU |
2718
- +---------------+-----------+---------------+--------------+----------------+
2719
- | bilinear | 4 | √ | × | Ascend,GPU,CPU |
2721
+ | nearest | 3 | \- | √ | Ascend |
2720
2722
  +---------------+-----------+---------------+--------------+----------------+
2721
- | bicubic | 4 | | × | Ascend,GPU,CPU |
2722
- +---------------+-----------+---------------+--------------+----------------+
2723
- | area | 3 | \- | √ | Ascend,GPU,CPU |
2723
+ | | 4 | \- | | Ascend |
2724
2724
  +---------------+-----------+---------------+--------------+----------------+
2725
- | | 4 | \- | √ | Ascend,GPU,CPU |
2725
+ | | 5 | \- | √ | Ascend |
2726
2726
  +---------------+-----------+---------------+--------------+----------------+
2727
- | | 5 | \- | √ | Ascend,GPU,CPU |
2727
+ | linear | 3 | | √ | Ascend |
2728
2728
  +---------------+-----------+---------------+--------------+----------------+
2729
- | nearest-exact | 3 | \- | × | Ascend,CPU |
2729
+ | bilinear | 4 | | × | Ascend |
2730
2730
  +---------------+-----------+---------------+--------------+----------------+
2731
- | | 4 | \- | × | Ascend,CPU |
2731
+ | bicubic | 4 | | × | Ascend |
2732
2732
  +---------------+-----------+---------------+--------------+----------------+
2733
- | trilinear | 5 | √ | √ | Ascend,GPU,CPU |
2733
+ | trilinear | 5 | √ | √ | Ascend |
2734
2734
  +---------------+-----------+---------------+--------------+----------------+
2735
2735
 
2736
2736
  - `-` indicates that there is no such parameter.
@@ -2738,7 +2738,21 @@ def interpolate_ext(input,
2738
2738
  - `√` indicates that this parameter is supported.
2739
2739
 
2740
2740
  Returns:
2741
- Tensor, resized, whose dimensions and dtype are the same as `input`.
2741
+ Tensor, sampled, whose dimensions and dtype are the same as `input`.
2742
+
2743
+ Shape:
2744
+ - Input: :math:`(N, C, W_{in})`, :math:`(N, C, H_{in}, W_{in})` or :math:`(N, C, D_{in}, H_{in}, W_{in})`
2745
+ - Output: :math:`(N, C, W_{out})`, :math:`(N, C, H_{out}, W_{out})`
2746
+ or :math:`(N, C, D_{out}, H_{out}, W_{out})`, where
2747
+
2748
+ .. math::
2749
+ D_{out} = \left\lfloor D_{in} \times \text{scale\_factor} \right\rfloor
2750
+
2751
+ .. math::
2752
+ H_{out} = \left\lfloor H_{in} \times \text{scale\_factor} \right\rfloor
2753
+
2754
+ .. math::
2755
+ W_{out} = \left\lfloor W_{in} \times \text{scale\_factor} \right\rfloor
2742
2756
 
2743
2757
  Raises:
2744
2758
  TypeError: `input` is not a Tensor.
@@ -2753,7 +2767,7 @@ def interpolate_ext(input,
2753
2767
  ValueError: `align_corners` is not in the corresponding list of supported values.
2754
2768
 
2755
2769
  Supported Platforms:
2756
- ``Ascend`` ``GPU`` ``CPU``
2770
+ ``Ascend``
2757
2771
 
2758
2772
  Examples:
2759
2773
  >>> import mindspore
@@ -2764,66 +2778,32 @@ def interpolate_ext(input,
2764
2778
  [[[1. 1. 2. 2. 3. 3.]
2765
2779
  [4. 4. 5. 5. 6. 6.]]]
2766
2780
  """
2781
+
2767
2782
  def run_nearest(x, size, align_corners=None, scale_factor=None):
2768
2783
  x_rank = F.rank(x)
2769
2784
  if x_rank == 3:
2770
- x = _get_cache_prim(ops.auto_generate.UpsampleNearest1D)()(
2771
- x, size, scale_factor)
2785
+ out = upsample_nearest1d_op(x, size, scale_factor)
2772
2786
  elif x_rank == 4:
2773
- x = _get_cache_prim(ops.auto_generate.UpsampleNearest2D)()(
2774
- x, size, scale_factor)
2787
+ out = upsample_nearest2d_op(x, size, scale_factor)
2775
2788
  else:
2776
- x = _get_cache_prim(P.UpsampleNearest3D)()(x, size, scale_factor)
2777
- return x
2789
+ out = upsample_nearest3d_op(x, size, scale_factor)
2790
+ return out
2778
2791
 
2779
2792
  def run_linear(x, size, align_corners=None, scale_factor=None):
2780
- out = _get_cache_prim(
2781
- ops.auto_generate.UpsampleLinear1D)()(x, size, scale_factor, align_corners)
2793
+ out = upsample_linear1d_op(x, size, scale_factor, align_corners)
2782
2794
  return out
2783
2795
 
2784
2796
  def run_bilinear(x, size, align_corners=None, scale_factor=None):
2785
- out = _get_cache_prim(
2786
- ops.auto_generate.UpsampleBilinear2D)()(x, size, scale_factor, align_corners)
2797
+ out = upsample_bilinear2d_op(x, size, scale_factor, align_corners)
2787
2798
  return out
2788
2799
 
2789
2800
  def run_trilinear(x, size, align_corners=None, scale_factor=None):
2790
- resize = _get_cache_prim(P.nn_ops.UpsampleTrilinear3D)(align_corners)
2791
- return resize(x, size, scale_factor)
2801
+ out = upsample_trilinear3d_impl(x, size, scale_factor, align_corners)
2802
+ return out
2792
2803
 
2793
2804
  def run_bicubic(x, size, align_corners=None, scale_factor=None):
2794
- resize = _get_cache_prim(P.image_ops.ResizeBicubic)(
2795
- align_corners=align_corners, half_pixel_centers=not align_corners)
2796
- x = resize(x, size)
2797
- return x
2798
-
2799
- def run_area(x, size, align_corners=None, scale_factor=None):
2800
- x_rank = F.rank(x)
2801
- if x_rank == 3:
2802
- x = F.adaptive_avg_pool1d(x, size[0])
2803
- elif x_rank == 4:
2804
- x = F.adaptive_avg_pool2d(x, tuple(size))
2805
- else:
2806
- x = F.adaptive_avg_pool3d(x, tuple(size))
2807
- return x
2808
-
2809
- def run_nearest_exact(x, size, align_corners=None, scale_factor=None):
2810
- x_rank = F.rank(x)
2811
- if x_rank == 3:
2812
- size = size[:1] + (1,)
2813
- # For impl of nearest 3D use 4D.
2814
- x = x.unsqueeze(-1)
2815
- resize = _get_cache_prim(P.ResizeNearestNeighborV2)(
2816
- align_corners=False,
2817
- half_pixel_centers=True)
2818
- x = resize(x, size)
2819
- x = _get_cache_prim(P.Squeeze)(-1)(x)
2820
- if x_rank == 4:
2821
- resize = _get_cache_prim(P.ResizeNearestNeighborV2)(
2822
- align_corners=False,
2823
- half_pixel_centers=True)
2824
- x = resize(x, size)
2825
- return x
2826
-
2805
+ out = upsample_bicubic2d_op(x, size, scale_factor, align_corners)
2806
+ return out
2827
2807
 
2828
2808
  resize_funcs = {
2829
2809
  "nearest": run_nearest,
@@ -2831,8 +2811,6 @@ def interpolate_ext(input,
2831
2811
  "bilinear": run_bilinear,
2832
2812
  "bicubic": run_bicubic,
2833
2813
  "trilinear": run_trilinear,
2834
- "area": run_area,
2835
- "nearest-exact": run_nearest_exact,
2836
2814
  }
2837
2815
 
2838
2816
  # mode check
@@ -3034,8 +3012,7 @@ def softmax(input, axis=-1, *, dtype=None):
3034
3012
  raise TypeError(f" the type of 'axis' must be 'int', but got '{axis}' with type '{type_axis}'.")
3035
3013
  if dtype is not None:
3036
3014
  input = ops.cast(input, dtype)
3037
- softmax_ = _get_cache_prim(P.Softmax)(axis)
3038
- return softmax_(input)
3015
+ return softmax_impl(input, axis)
3039
3016
 
3040
3017
 
3041
3018
  def softmax_ext(input, dim=None, dtype=None):
@@ -3082,8 +3059,50 @@ def softmax_ext(input, dim=None, dtype=None):
3082
3059
  raise TypeError(f" the type of 'dim' must be 'int', but got '{dim}' with type '{type_dim}'.")
3083
3060
  if dtype is not None:
3084
3061
  input = ops.cast(input, dtype)
3085
- softmax_ = _get_cache_prim(P.Softmax)(dim)
3086
- return softmax_(input)
3062
+ return softmax_impl(input, dim)
3063
+
3064
+
3065
+ def log_softmax_ext(input, dim=None, *, dtype=None):
3066
+ r"""
3067
+ Applies the Log Softmax function to the input tensor on the specified axis.
3068
+ Supposes a slice in the given axis, :math:`x` for each element :math:`x_i`,
3069
+ the Log Softmax function is shown as follows:
3070
+
3071
+ .. math::
3072
+ \text{output}(x_i) = \log \left(\frac{\exp(x_i)} {\sum_{j = 0}^{N-1}\exp(x_j)}\right),
3073
+
3074
+ where :math:`N` is the length of the Tensor.
3075
+
3076
+ Args:
3077
+ input (Tensor): The input Tensor.
3078
+ dim (int, optional): The axis to perform the Log softmax operation. Default: ``None`` .
3079
+
3080
+ Keyword Args:
3081
+ dtype (:class:`mindspore.dtype`, optional): The desired dtype of returned Tensor. If not set to None, the input
3082
+ Tensor will be cast to `dtype` before the operation is performed. This is useful for preventing overflows.
3083
+ If set to None, stay the same as original Tensor. Default: ``None`` .
3084
+
3085
+ Returns:
3086
+ Tensor, with the same shape as the input.
3087
+
3088
+ Raises:
3089
+ TypeError: If `dim` is not an int.
3090
+ ValueError: If `dim` is not in range [-len(input.shape), len(input.shape)).
3091
+
3092
+ Supported Platforms:
3093
+ ``Ascend``
3094
+
3095
+ Examples:
3096
+ >>> import mindspore
3097
+ >>> import numpy as np
3098
+ >>> from mindspore import Tensor, ops
3099
+ >>> logits = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32)
3100
+ >>> output = ops.function.nn_func.log_softmax_ext(logits, dim=-1)
3101
+ >>> print(output)
3102
+ [-4.4519143 -3.4519143 -2.4519143 -1.4519144 -0.4519144]
3103
+ """
3104
+ log_softmax_op = _get_cache_prim(P.LogSoftmaxExt)()
3105
+ return log_softmax_op(input, dim, dtype)
3087
3106
 
3088
3107
 
3089
3108
  def softmin(x, axis=-1, *, dtype=None):
@@ -3131,82 +3150,34 @@ def softmin(x, axis=-1, *, dtype=None):
3131
3150
  if dtype is not None:
3132
3151
  x = ops.cast(x, dtype)
3133
3152
  softmax_ = _get_cache_prim(P.Softmax)(axis)
3134
- return softmax_(-1*x)
3153
+ return softmax_(-1 * x)
3154
+
3155
+
3156
+ def soft_shrink(input, lambd=0.5):
3157
+ r"""
3158
+ `soft_shrink` is deprecated, please use `softshrink` instead.
3159
+ """
3160
+ logger.warning("`soft_shrink` is deprecated, please use `softshrink` instead.")
3161
+ soft_shrink_op = _get_cache_prim(P.SoftShrink)(lambd)
3162
+ return soft_shrink_op(input)
3135
3163
 
3136
3164
 
3137
- def softshrink(x, lambd=0.5):
3165
+ def softplus(input, beta=1, threshold=20): # pylint:disable=redefined-outer-name
3138
3166
  r"""
3139
- Applies the Softshrink function element-wise.
3167
+ Applies softplus function to `input` element-wise.
3168
+
3169
+ The softplus function is shown as follows, x is the element of `input` :
3140
3170
 
3141
3171
  .. math::
3142
- \text{SoftShrink}(x) =
3143
- \begin{cases}
3144
- x - \lambda, & \text{ if } x > \lambda \\
3145
- x + \lambda, & \text{ if } x < -\lambda \\
3146
- 0, & \text{ otherwise }
3147
- \end{cases}
3148
3172
 
3149
- SoftShrink Activation Function Graph:
3173
+ \text{output} = \frac{1}{beta}\log(1 + \exp(\text{beta * x}))
3150
3174
 
3151
- .. image:: ../images/Softshrink.png
3152
- :align: center
3175
+ When :math:`input * beta > threshold`, the implementation converts to the linear function
3176
+ to ensure numerical stability.
3153
3177
 
3154
3178
  Args:
3155
- x (Tensor): The input of soft shrink with data type of float16 or float32.
3156
- lambd (float): The :math:`\lambda` must be no less than zero. Default: ``0.5`` .
3157
-
3158
- Returns:
3159
- Tensor, has the same shape and data type as `x`.
3160
-
3161
- Raises:
3162
- TypeError: If `lambd` is not a float.
3163
- TypeError: If `x` is not a Tensor.
3164
- TypeError: If dtype of `x` is neither float16 nor float32.
3165
- ValueError: If `lambd` is less than 0.
3166
-
3167
- Supported Platforms:
3168
- ``Ascend`` ``GPU`` ``CPU``
3169
-
3170
- Examples:
3171
- >>> import mindspore
3172
- >>> from mindspore import Tensor
3173
- >>> from mindspore import ops
3174
- >>> import numpy as np
3175
- >>> x = Tensor(np.array([[ 0.5297, 0.7871, 1.1754], [ 0.7836, 0.6218, -1.1542]]), mindspore.float32)
3176
- >>> output = ops.softshrink(x)
3177
- >>> print(output)
3178
- [[ 0.02979 0.287 0.676 ]
3179
- [ 0.2837 0.1216 -0.6543 ]]
3180
- """
3181
- soft_shrink_op = _get_cache_prim(P.SoftShrink)(lambd)
3182
- return soft_shrink_op(x)
3183
-
3184
-
3185
- def soft_shrink(input, lambd=0.5):
3186
- r"""
3187
- `soft_shrink` is deprecated, please use `softshrink` instead.
3188
- """
3189
- logger.warning("`soft_shrink` is deprecated, please use `softshrink` instead.")
3190
- soft_shrink_op = _get_cache_prim(P.SoftShrink)(lambd)
3191
- return soft_shrink_op(input)
3192
-
3193
-
3194
- def softplus(input, beta=1, threshold=20): # pylint:disable=redefined-outer-name
3195
- r"""
3196
- Applies softplus function to `input` element-wise.
3197
-
3198
- The softplus function is shown as follows, x is the element of `input` :
3199
-
3200
- .. math::
3201
-
3202
- \text{output} = \frac{1}{beta}\log(1 + \exp(\text{beta * x}))
3203
-
3204
- When :math:`input * beta > threshold`, the implementation converts to the linear function
3205
- to ensure numerical stability.
3206
-
3207
- Args:
3208
- input (Tensor) - Tensor of any dimension.
3209
- Supported dtypes:
3179
+ input (Tensor) - Tensor of any dimension.
3180
+ Supported dtypes:
3210
3181
 
3211
3182
  - GPU/CPU: float16, float32, float64.
3212
3183
  - Ascend: float16, float32.
@@ -3234,6 +3205,8 @@ def softplus(input, beta=1, threshold=20): # pylint:disable=redefined-outer-name
3234
3205
  >>> print(output)
3235
3206
  [0.7443967 0.79813886 30. 25.]
3236
3207
  """
3208
+ if beta == 0:
3209
+ raise ValueError("For 'softplus', the value of 'beta' cannot be 0.")
3237
3210
  scaling_input = beta * input
3238
3211
  op_output = (1 / beta) * softplus_(scaling_input)
3239
3212
  return ops.select(input * beta > threshold, input, op_output)
@@ -3332,6 +3305,50 @@ def logsigmoid(x):
3332
3305
  return ret
3333
3306
 
3334
3307
 
3308
+ def logsigmoid_ext(input):
3309
+ r"""
3310
+ Applies logsigmoid activation element-wise. The input is a Tensor with any valid shape.
3311
+
3312
+ Logsigmoid is defined as:
3313
+
3314
+ .. math::
3315
+ \text{logsigmoid}(x_{i}) = \log(\frac{1}{1 + \exp(-x_i)}),
3316
+
3317
+ where :math:`x_{i}` is the element of the input.
3318
+
3319
+ LogSigmoid Activation Function Graph:
3320
+
3321
+ .. image:: ../images/LogSigmoid.png
3322
+ :align: center
3323
+
3324
+ .. warning::
3325
+ This is an experimental API that is subject to change or deletion.
3326
+
3327
+ Args:
3328
+ input (Tensor): The input of LogSigmoid with data type of bfloat16, float16 or float32.
3329
+ The shape is :math:`(*)` where :math:`*` means, any number of additional dimensions.
3330
+
3331
+ Returns:
3332
+ Tensor, with the same type and shape as the `input`.
3333
+
3334
+ Raises:
3335
+ TypeError: If dtype of `input` is not bfloat16, float16 and float32.
3336
+ TypeError: If `input` is not a Tensor.
3337
+
3338
+ Supported Platforms:
3339
+ ``Ascend``
3340
+
3341
+ Examples:
3342
+ >>> import mindspore
3343
+ >>> from mindspore import Tensor, ops
3344
+ >>> input = Tensor([1.0, 2.0, 3.0], mindspore.float32)
3345
+ >>> output = ops.function.nn_func.logsigmoid_ext(input)
3346
+ >>> print(output)
3347
+ [-0.31326166 -0.12692806 -0.04858734]
3348
+ """
3349
+ return logsigmoid_op(input)[0]
3350
+
3351
+
3335
3352
  def _check_dense_add_bias_shape(input_shape, output_shape, bias_shape):
3336
3353
  """Check that the output has the correct shape after adding bias."""
3337
3354
  if input_shape != output_shape:
@@ -3678,6 +3695,7 @@ def pad_ext(input, pad, mode='constant', value=0.0):
3678
3695
  ``Ascend``
3679
3696
 
3680
3697
  Examples:
3698
+ >>> import mindspore as ms
3681
3699
  >>> from mindspore import ops
3682
3700
  >>> import numpy as np
3683
3701
  >>> x = ms.Tensor(np.arange(1 * 2 * 2 * 2).reshape((1, 2, 2, 2)), dtype=ms.float64)
@@ -4526,6 +4544,7 @@ def intopk(x1, x2, k):
4526
4544
  _in_topk = _get_cache_prim(P.InTopK)(k)
4527
4545
  return _in_topk(x1, x2)
4528
4546
 
4547
+
4529
4548
  def lrn(x, depth_radius=5, bias=1.0, alpha=1.0, beta=0.5, norm_region="ACROSS_CHANNELS"):
4530
4549
  r"""
4531
4550
  Local Response Normalization.
@@ -5629,7 +5648,7 @@ def conv1d(input, weight, bias=None, stride=1, pad_mode="valid", padding=0, dila
5629
5648
  raise TypeError(f"For 'conv1d', the 'bias' must be a Tensor, but got {type(bias)}.")
5630
5649
  if bias.shape[0] != out_channel:
5631
5650
  raise ValueError(f"For 'conv1d', given weight of size {weight_shape}, expected bias to be 1-dimensional with " \
5632
- f"{out_channel} elements, but got bias of size {bias.shape[0]} instead.")
5651
+ f"{out_channel} elements, but got bias of size {bias.shape[0]} instead.")
5633
5652
  output = bias_add(squeezed_conv_res, bias)
5634
5653
  return output
5635
5654
 
@@ -5767,12 +5786,207 @@ def conv2d(input, weight, bias=None, stride=1, pad_mode="valid", padding=0, dila
5767
5786
  raise TypeError(f"For 'conv2d', the 'bias' must be a Tensor, but got {type(bias)}.")
5768
5787
  if bias.shape[0] != out_channel:
5769
5788
  raise ValueError(f"For 'conv2d', Given weight of size {weight_shape}, expected bias to be 1-dimensional with " \
5770
- f"{out_channel} elements, but got bias of size {bias.shape[0]} instead.")
5789
+ f"{out_channel} elements, but got bias of size {bias.shape[0]} instead.")
5771
5790
  conv_result = conv(input, weight)
5772
5791
  output = bias_add(conv_result, bias)
5773
5792
  return output
5774
5793
 
5775
5794
 
5795
+ def _check_stride_when_same_mode(stride):
5796
+ """ stride must be 1 when pad mode is same """
5797
+ if isinstance(stride, int):
5798
+ if stride != 1:
5799
+ raise ValueError(f"For conv2d, 'padding=same' is not supported for stride convolution, " \
5800
+ f"but got {stride}")
5801
+ elif isinstance(stride, tuple):
5802
+ validator.check_int(len(stride), 2, validator.EQ, "stride", 'conv2d')
5803
+ if not all(s == 1 for s in stride):
5804
+ raise ValueError(f"For conv2d, 'padding=same' is not supported for stride convolution, " \
5805
+ f"but got {stride}")
5806
+ else:
5807
+ raise TypeError(f"For conv2d, the parameter 'stride' must be a int/tuple, but got {type(stride)}")
5808
+
5809
+
5810
+ def _get_pad_info(dilation, weight):
5811
+ """ Get pad list by dilation and weight shape """
5812
+ need_pad_nd = False
5813
+ pad_l = ()
5814
+ pad_r = ()
5815
+ for i in range(2):
5816
+ d = dilation[i]
5817
+ weight_size = weight.shape[i + 2]
5818
+ pad = d * (weight_size - 1)
5819
+ pad_l += (int(pad / 2),)
5820
+ pad_r += (int(pad - pad_l[i]),)
5821
+ if pad_l[i] != pad_r[i]:
5822
+ need_pad_nd = True
5823
+ return need_pad_nd, pad_l, pad_r
5824
+
5825
+
5826
+ def _get_pad_nd_info(pad_l, pad_r):
5827
+ """ Get pad_nd list if input need to exec pad_nd """
5828
+ pad_nd = ()
5829
+ new_pad_l = ()
5830
+ for i in range(2):
5831
+ delta_pad = pad_r[i] - pad_l[i]
5832
+ if delta_pad > 0:
5833
+ pad_nd = (0, delta_pad,) + pad_nd
5834
+ new_pad_l += (pad_l[i],)
5835
+ else:
5836
+ pad_nd = (delta_pad, 0,) + pad_nd
5837
+ new_pad_l += (pad_r[i],)
5838
+ return pad_nd, new_pad_l
5839
+
5840
+
5841
+ def conv2d_ext(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1):
5842
+ r"""
5843
+ Applies a 2D convolution over an input tensor. The input tenor is typically of
5844
+ shape :math:`(N, C_{in}, H_{in}, W_{in})`, where :math:`N` is batch size, :math:`C` is
5845
+ channel number, :math:`H` is feature height, :math:`W` is feature width.
5846
+
5847
+ The output is calculated based on formula:
5848
+
5849
+ .. math::
5850
+
5851
+ \text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) +
5852
+ \sum_{k = 0}^{C_{in} - 1} \text{ccor}({\text{weight}(C_{\text{out}_j}, k), \text{X}(N_i, k)})
5853
+
5854
+ where :math:`bias` is the output channel bias, :math:`ccor` is
5855
+ the `cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_,
5856
+ , :math:`weight` is the convolution kernel value and :math:`X` represents the input feature map.
5857
+
5858
+ Here are the indices' meanings:
5859
+
5860
+ - :math:`i` corresponds to the batch number, the range is :math:`[0, N-1]`,
5861
+ where :math:`N` is the batch size of the input.
5862
+
5863
+ - :math:`j` corresponds to the output channel, the range is :math:`[0, C_{out}-1]`,
5864
+ where :math:`C_{out}` is the number of output channels, which is also equal to the number of kernels.
5865
+
5866
+ - :math:`k` corresponds to the input channel, the range is :math:`[0, C_{in}-1]`,
5867
+ where :math:`C_{in}` is the number of
5868
+ input channels, which is also equal to the number of channels in the convolutional kernels.
5869
+
5870
+ Therefore, in the above formula, :math:`{bias}(C_{out_j})` represents the bias of the :math:`j`-th
5871
+ output channel, :math:`{weight}(C_{out_j}, k)` represents the slice of the :math:`j`-th convolutional
5872
+ kernel in the :math:`k`-th channel, and :math:`{X}(N_i, k)` represents the slice of the :math:`k`-th input
5873
+ channel in the :math:`i`-th batch of the input feature map.
5874
+
5875
+ The shape of the convolutional kernel is given by :math:`(\text{kernel_size[0]}, \text{kernel_size[1]})`,
5876
+ where :math:`\text{kernel_size[0]}` and :math:`\text{kernel_size[1]}` are the height and width of the kernel,
5877
+ respectively.
5878
+ If we consider the input and output channels as well as the `group` parameter, the complete kernel shape
5879
+ will be :math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]})`,
5880
+ where `group` is the number of groups dividing `x`'s input channel when applying group convolution.
5881
+
5882
+ For more details about convolution layer, please refer to `Gradient Based Learning Applied to Document Recognition
5883
+ <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_ and
5884
+ `ConvNets <http://cs231n.github.io/convolutional-networks/>`_.
5885
+
5886
+ Note:
5887
+ On Ascend platform, only group convolution in depthwise convolution scenarios is supported.
5888
+ That is, when `groups>1`, condition :math:`C_{in}` = :math:`C_{out}` = `groups` must be satisfied.
5889
+
5890
+ Args:
5891
+ input (Tensor): Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
5892
+ weight (Tensor): Tensor of shape
5893
+ :math:`(N, C_{in} / \text{groups}, \text{kernel_size[0]}, \text{kernel_size[1]})`, then the size of kernel
5894
+ is :math:`(\text{kernel_size[0]}, \text{kernel_size[1]})`.
5895
+ bias (Tensor, optional): Bias Tensor with shape :math:`(C_{out})`.
5896
+ When bias is ``None`` , zeros will be used. Default: ``None`` .
5897
+ stride (Union(int, tuple[int]), optional): The distance of kernel moving, an int number that represents
5898
+ the height and width of movement are both strides, or a tuple of two int numbers that
5899
+ represent height and width of movement respectively. Default: ``1`` .
5900
+ padding (Union(int, tuple[int], list[int], str), optional): Implicit paddings on both sides of the input `x`.
5901
+ Can be a string, one integer or a tuple/list with 2 integers.
5902
+ If `padding` is a string, the optional values are ``"same"`` , ``"valid"``.
5903
+
5904
+ - same: Adopts the way of completion. The height and width of the output will be equal to
5905
+ the input `x` divided by stride. The padding will be evenly calculated in top and bottom,
5906
+ left and right possiblily. Otherwise, the last extra padding will be calculated from the bottom
5907
+ and the right side. If this mode is set, `padding` must be 0.
5908
+
5909
+ - valid: Adopts the way of discarding. The possible largest height and width of output will be returned
5910
+ without padding. Extra pixels will be discarded. If this mode is set, `padding` must be 0.
5911
+
5912
+ If `padding` is one integer, the paddings of top, bottom, left and right are the same, equal to padding.
5913
+ If `padding` is a tuple/list with 2 integers, the padding of top adn bottom is padding[0],
5914
+ and the padding of left and right is padding[1]. Default: ``0`` .
5915
+ dilation (Union(int, tuple[int]), optional): Gaps between kernel elements.The data type is int or a tuple of
5916
+ 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be :math:`k > 1`,
5917
+ there will be :math:`k - 1` pixels skipped for each sampling location. Its value must
5918
+ be greater than or equal to 1 and bounded by the height and width of the input `x`. Default: ``1`` .
5919
+ groups (int, optional): Splits `input` into groups. Default: ``1`` .
5920
+
5921
+ Returns:
5922
+ Tensor, the value that applied 2D convolution. The shape is :math:`(N, C_{out}, H_{out}, W_{out})`.
5923
+ To see how different pad modes affect the output shape, please refer to
5924
+ :class:`mindspore.nn.Conv2d` for more details.
5925
+
5926
+
5927
+ Raises:
5928
+ TypeError: If `stride`, `padding` or `dilation` is neither an int nor a tuple.
5929
+ TypeError: `groups` is not an int.
5930
+ TypeError: If `bias` is not a Tensor.
5931
+ ValueError: If the shape of `bias` is not :math:`(C_{out})` .
5932
+ ValueError: If `stride` or `dilation` is less than 1.
5933
+ ValueError: If `pad_mode` is not one of 'same', 'valid' or 'pad'.
5934
+ ValueError: If `padding` is a tuple/list whose length is not equal to 2.
5935
+
5936
+ Supported Platforms:
5937
+ ``Ascend``
5938
+
5939
+ Examples:
5940
+ >>> import mindspore
5941
+ >>> import numpy as np
5942
+ >>> from mindspore import Tensor, ops
5943
+ >>> from mindspore.ops.function.nn_func import conv2d_ext
5944
+ >>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32)
5945
+ >>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32)
5946
+ >>> output = conv2d_ext(x, weight)
5947
+ >>> print(output.shape)
5948
+ (10, 32, 30, 30)
5949
+ """
5950
+
5951
+ def _convolution_same(input, weight, bias, dilation, groups):
5952
+ """ convolution when mode is 'same' """
5953
+ if isinstance(dilation, int):
5954
+ dilation = (dilation,) * 2
5955
+ validator.check_int(len(weight.shape), 4, validator.EQ, "weight.shape", 'conv2d')
5956
+ validator.check_int(len(dilation), 2, validator.EQ, "dilation", 'conv2d')
5957
+
5958
+ # Calc padding info
5959
+ need_pad_nd, pad_l, pad_r = _get_pad_info(dilation, weight)
5960
+ if not need_pad_nd:
5961
+ conv = _get_cache_prim(Convolution)(stride, pad_l, dilation, False, (0, 0), groups)
5962
+ return conv(input, weight, bias)
5963
+
5964
+ # Calc pad nd info
5965
+ pad_nd, pad_l = _get_pad_nd_info(pad_l, pad_r)
5966
+ pad_nd_op = _get_cache_prim(ConstantPadND)()
5967
+ padded_input = pad_nd_op(input, pad_nd, 0)
5968
+ conv = _get_cache_prim(Convolution)(stride, pad_l, dilation, False, (0, 0), groups)
5969
+ return conv(padded_input, weight, bias)
5970
+
5971
+ if isinstance(padding, int):
5972
+ padding = (padding,) * 2
5973
+
5974
+ if isinstance(padding, (tuple, list)):
5975
+ conv = _get_cache_prim(Convolution)(stride, padding, dilation, False, (0, 0), groups)
5976
+ return conv(input, weight, bias)
5977
+ if isinstance(padding, str):
5978
+ if padding == 'valid':
5979
+ conv = _get_cache_prim(Convolution)(stride, (0, 0), dilation, False, (0, 0), groups)
5980
+ return conv(input, weight, bias)
5981
+ if padding == 'same':
5982
+ _check_stride_when_same_mode(stride)
5983
+ return _convolution_same(input, weight, bias, dilation, groups)
5984
+ raise ValueError(f"For conv2d, the parameter 'padding' must be 'same' or 'valid' when " \
5985
+ f"the type of 'padding' is string.")
5986
+ raise TypeError(f"For conv2d, the parameter 'padding' must be a tuple/list " \
5987
+ f"or a string, but got {type(padding)}")
5988
+
5989
+
5776
5990
  def conv_transpose2d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1):
5777
5991
  r"""
5778
5992
  Calculates a 2D transposed convolution, which can be regarded as Conv2d for the gradient of the input,
@@ -5842,51 +6056,6 @@ def conv_transpose2d(input, weight, bias=None, stride=1, padding=0, output_paddi
5842
6056
  return conv(input, weight, bias)
5843
6057
 
5844
6058
 
5845
- def hardsigmoid(input):
5846
- r"""
5847
- Hard sigmoid activation function.
5848
-
5849
- Applies hard sigmoid activation element-wise. The input is a Tensor with any valid shape.
5850
-
5851
- Hard sigmoid is defined as:
5852
-
5853
- .. math::
5854
-
5855
- \text{hsigmoid}(x_{i}) = \max(0, \min(1, \frac{x_{i} + 3}{6}))
5856
-
5857
- where :math:`x_i` is an element of the input Tensor.
5858
-
5859
- HSigmoid Activation Function Graph:
5860
-
5861
- .. image:: ../images/HSigmoid.png
5862
- :align: center
5863
-
5864
- Args:
5865
- input (Tensor): The input Tensor.
5866
-
5867
- Returns:
5868
- A Tensor whose dtype and shape are the same as `input`.
5869
-
5870
- Raises:
5871
- TypeError: If `input` is not a Tensor.
5872
- TypeError: If dtype of `input` is not int or float.
5873
-
5874
- Supported Platforms:
5875
- ``Ascend`` ``GPU`` ``CPU``
5876
-
5877
- Examples:
5878
- >>> import mindspore
5879
- >>> import numpy as np
5880
- >>> from mindspore import Tensor, ops
5881
- >>> x = Tensor(np.array([ -3.5, 0, 4.3]), mindspore.float32)
5882
- >>> output = ops.hardsigmoid(x)
5883
- >>> print(output)
5884
- [0. 0.5 1. ]
5885
- """
5886
- hardsigmoid_ = NN_OPS.HSigmoid()
5887
- return hardsigmoid_(input)
5888
-
5889
-
5890
6059
  def hardtanh(input, min_val=-1.0, max_val=1.0):
5891
6060
  r"""
5892
6061
  Applies the hardtanh activation function element-wise. The activation function is defined as:
@@ -6066,6 +6235,7 @@ def adaptive_avg_pool1d(input, output_size):
6066
6235
  >>> print(output.shape)
6067
6236
  (1, 3, 2)
6068
6237
  """
6238
+
6069
6239
  def _check(x, output_size):
6070
6240
  x_in_shape = x.shape
6071
6241
  x_dtype = dtype_(x)
@@ -6161,6 +6331,74 @@ def layer_norm(input, normalized_shape, weight=None, bias=None, eps=1e-5):
6161
6331
  return layer_norm_ext_op(input, normalized_shape, weight, bias, eps)[0]
6162
6332
 
6163
6333
 
6334
+ def add_layer_norm(x1, x2, gamma, beta, epsilon=1e-5, additional_output=False):
6335
+ r"""
6336
+ Implements the add_layer_norm algorithm.
6337
+
6338
+ .. math::
6339
+ \begin{aligned}
6340
+ x = x1 + x2 \\
6341
+ y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta \\
6342
+ \end{aligned}
6343
+
6344
+ .. warning::
6345
+ This is an experimental API that is subject to change or deletion.
6346
+
6347
+ Args:
6348
+ x1 (Tensor): Input of Addition Calculation in AddLayerNorm. `x1 + x2` will be calculated in the operator
6349
+ and the calculation result is normalized. Data type is float16, bfloat16 or float32 .
6350
+ x2 (Tensor): Input of Addition Calculation in AddLayerNorm. `x1 + x2` will be calculated in the operator
6351
+ and the calculation result is normalized. Has the same dtype and shape as the `x1`.
6352
+ gamma (Tensor): Learnable parameter :math:`\gamma` . Tensor of shape is 1D, keep same with last
6353
+ dimension `x1` .
6354
+ beta (Tensor): Learnable parameter :math:`\beta` . Tensor of shape is 1D, keep same with last dimension `x1` .
6355
+ epsilon (float, optional): A value added to the denominator for numerical stability(:math:`\epsilon`).
6356
+ Default: ``1e-5`` .
6357
+ additional_output (bool, optional): Indicates whether to enable the output of `x=x1+x2`.
6358
+ Default: ``False`` .
6359
+
6360
+ Returns:
6361
+ tuple [Tensor], tuple of 4 Tensors. the output of normalized input and the updated parameters.
6362
+
6363
+ - **y** (Tensor) - Output of normalization, has the same type as the `x1`.
6364
+ - **mean** (Tensor) - The mean of input, has the same type as the `x1`.
6365
+ - **rstd** (Tensor) - The reciprocal of the input standard deviation. Shape is the same as `mean` .
6366
+ - **x** (Tensor) - output of `x1 + x2`.
6367
+
6368
+ Raises:
6369
+ TypeError: If `x1` is not a Tensor.
6370
+ TypeError: If `x2` is not a Tensor.
6371
+ TypeError: If `gamma` is not a Tensor.
6372
+ TypeError: If `beta` is not a Tensor.
6373
+ TypeError: If `epsilon` is not a float.
6374
+ TypeError: If `additional_output` is not a bool.
6375
+
6376
+ Supported Platforms:
6377
+ ``Ascend``
6378
+
6379
+ Examples:
6380
+ >>> import mindspore
6381
+ >>> import numpy as np
6382
+ >>> from mindspore import Tensor, ops
6383
+ >>> x1 = Tensor(np.array([[1, 2, 3], [1, 2, 3]]), mindspore.float32)
6384
+ >>> x2 = Tensor(np.array([[1, 2, 3], [1, 2, 3]]), mindspore.float32)
6385
+ >>> gamma = Tensor(np.ones([3]), mindspore.float32)
6386
+ >>> beta = Tensor(np.zeros([3]), mindspore.float32)
6387
+ >>> epsilon = 1e-7
6388
+ >>> output = ops.add_layer_norm(x1, x2, gamma, beta, epsilon)
6389
+ >>> print(output[0])
6390
+ [[-1.2247448 0 1.2247448]
6391
+ [-1.2247448 0 1.2247448]]
6392
+ >>> print(output[1])
6393
+ [[4]
6394
+ [4]]
6395
+ >>> print(output[2])
6396
+ [[0.6123724]
6397
+ [0.6123724]]
6398
+ """
6399
+ return add_layernorm_v2_op(x1, x2, gamma, beta, epsilon, additional_output)
6400
+
6401
+
6164
6402
  def group_norm(input, num_groups, weight=None, bias=None, eps=1e-5):
6165
6403
  r"""Group Normalization over a mini-batch of inputs.
6166
6404
 
@@ -6285,6 +6523,7 @@ def batch_norm_ext(input, running_mean, running_var, weight=None, bias=None, tra
6285
6523
  output = batch_norm_ext_op(input, weight, bias, running_mean, running_var, training, momentum, eps)
6286
6524
  return output[0]
6287
6525
 
6526
+
6288
6527
  def batch_norm(input_x, running_mean, running_var, weight, bias, training=False, momentum=0.1, eps=1e-5):
6289
6528
  r"""
6290
6529
  Batch Normalization for input data and updated parameters.
@@ -6437,7 +6676,7 @@ def binary_cross_entropy(logits, labels, weight=None, reduction='mean'):
6437
6676
  - ``'sum'``: the output elements will be summed.
6438
6677
 
6439
6678
  Returns:
6440
- Tensor or Scalar. Returns Tensor that has the same dtype and shape as `logits` if `reduction` is 'none'.
6679
+ Tensor or Scalar. Returns Tensor that has the same dtype and shape as `logits` if `reduction` is ``'none'``.
6441
6680
  Otherwise, returns a scalar Tensor.
6442
6681
 
6443
6682
  Raises:
@@ -6630,7 +6869,7 @@ def conv3d(input, weight, bias=None, stride=1, pad_mode="valid", padding=0, dila
6630
6869
  in_channel = input_shape[1]
6631
6870
  if not (in_channel % groups == 0 and out_channel % groups == 0):
6632
6871
  raise ValueError("The argument 'groups' should be divisible by 'in_channel' " \
6633
- "and 'out_channel'")
6872
+ "and 'out_channel'")
6634
6873
  if isinstance(padding, (list, tuple)):
6635
6874
  padding = _manipulate_padding(padding, dim=3)
6636
6875
  conv = _get_cache_prim(P.Conv3D)(out_channel, kernel_size, 1, pad_mode, padding, stride, dilation, groups, "NCDHW")
@@ -7152,6 +7391,9 @@ def lp_pool1d(x, norm_type, kernel_size, stride=None, ceil_mode=False):
7152
7391
  .. math::
7153
7392
  f(X) = \sqrt[p]{\sum_{x \in X} x^{p}}
7154
7393
 
7394
+ Note:
7395
+ This interface currently does not support Atlas A2 training series products.
7396
+
7155
7397
  Args:
7156
7398
  x (Tensor): Tensor of shape :math:`(N, C, L_{in})` or :math:`(C, L_{in})`.
7157
7399
  norm_type (Union[int, float]): Type of normalization, represents p in the formula, can not be 0,
@@ -7229,6 +7471,9 @@ def lp_pool2d(x, norm_type, kernel_size, stride=None, ceil_mode=False):
7229
7471
  .. math::
7230
7472
  f(X) = \sqrt[p]{\sum_{x \in X} x^{p}}
7231
7473
 
7474
+ Note:
7475
+ This interface currently does not support Atlas A2 training series products.
7476
+
7232
7477
  Args:
7233
7478
  x (Tensor): Tensor of shape :math:`(N, C, H_{in}, W_{in})`.
7234
7479
  norm_type (Union[int, float]): Type of normalization, represents p in the formula, can not be 0,
@@ -7382,6 +7627,9 @@ def msort(input):
7382
7627
 
7383
7628
  ops.msort(t) is equivalent to ops.Sort(axis=0)(t)[0]. See also :class:`mindspore.ops.Sort()`.
7384
7629
 
7630
+ .. Note::
7631
+ The Ascend backend only supports sorting the 1D input.
7632
+
7385
7633
  Args:
7386
7634
  input (Tensor): The input to sort, with float16 or float32 data type.
7387
7635
 
@@ -7883,6 +8131,94 @@ def max_pool2d(x, kernel_size, stride=None, padding=0, dilation=1, return_indice
7883
8131
  return out
7884
8132
 
7885
8133
 
8134
+ def max_pool2d_ext(input, kernel_size, stride=None, padding=0, dilation=1, *, ceil_mode=False, return_indices=False):
8135
+ r"""
8136
+ Performs a 2D max pooling on the input Tensor.
8137
+
8138
+ Typically, the input is a Tensor with shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})`, outputs
8139
+ regional maximum in the :math:`(H_{in}, W_{in})`-dimension. Given `kernel_size`
8140
+ :math:`ks = (h_{ker}, w_{ker})` and `stride` :math:`s = (s_0, s_1)`, the operation is as follows:
8141
+
8142
+ .. math::
8143
+ \text{output}(N_i, C_j, h, w) =
8144
+ \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1}
8145
+ \text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)
8146
+
8147
+ .. warning::
8148
+ Only support on Atlas A2 training series.
8149
+
8150
+ Args:
8151
+ input (Tensor): Tensor of shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})` with data type of float32
8152
+ in Ascend.
8153
+ kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value and arg
8154
+ value, is an int number that represents height and width of the kernel, or a tuple of
8155
+ two int numbers that represent height and width respectively.
8156
+ stride (Union[int, tuple[int], None]): The distance of kernel moving, an int number that represents
8157
+ the height and width of movement are both stride, or a tuple of two int numbers that
8158
+ represent height and width of movement respectively.
8159
+ Default: ``None`` , which indicates the moving step is `kernel_size` .
8160
+ padding (Union[int, tuple[int]]): An int number that represents the height and width of movement are both
8161
+ strides, or a tuple of two int numbers that represent height and width of movement respectively.
8162
+ Default: ``0`` .
8163
+ dilation (Union[int, tuple[int]]): Control the stride of elements in the kernel. Default: ``1`` .
8164
+ ceil_mode (bool): Whether to use ceil instead of floor to calculate output shape. Default: ``False`` .
8165
+ return_indices (bool): Whether to output the indices of max value. Default: ``False`` .
8166
+
8167
+ Returns:
8168
+ If `return_indices` is ``False`` , return a Tensor `output`, else return a tuple (`output`, `argmax`).
8169
+
8170
+ - **output** (Tensor) - Maxpooling result, with shape :math:`(N_{out}, C_{out}, H_{out}, W_{out})`.
8171
+ It has the same data type as `input`.
8172
+
8173
+ .. math::
8174
+ H_{out} = \left\lfloor\frac{H_{in} + 2 * \text{padding[0]} - \text{dilation[0]}
8175
+ \times (\text{kernel_size[0]} - 1) - 1}{\text{stride[0]}} + 1\right\rfloor
8176
+
8177
+ .. math::
8178
+ W_{out} = \left\lfloor\frac{W_{in} + 2 * \text{padding[1]} - \text{dilation[1]}
8179
+ \times (\text{kernel_size[1]} - 1) - 1}{\text{stride[1]}} + 1\right\rfloor
8180
+
8181
+ - **argmax** (Tensor) - Index corresponding to the maximum value. In Ascend, data type is int32.
8182
+ It will be return only when `return_indices` is True.
8183
+
8184
+ Raises:
8185
+ TypeError: If `input` is not a Tensor.
8186
+ ValueError: If length of shape of `input` is not equal to 4.
8187
+ TypeError: If `kernel_size` , `stride` , `padding` or `dilation` is not int or tuple.
8188
+ ValueError: If `kernel_size`, `stride` or `dilation` is less than 1.
8189
+ ValueError: If `dilation` is not all 1.
8190
+ ValueError: If `padding` is less than 0.
8191
+ ValueError: If `padding` is more than half of `kernel_size`.
8192
+ TypeError: If `ceil_mode` is not bool.
8193
+
8194
+ Supported Platforms:
8195
+ ``Ascend``
8196
+
8197
+ Examples:
8198
+ >>> import mindspore
8199
+ >>> import numpy as np
8200
+ >>> from mindspore import Tensor, ops
8201
+ >>> from mindspore.ops.function.nn_func import max_pool2d_ext
8202
+ >>> input = Tensor(np.arange(20 * 16 * 50 * 32).reshape((20, 16, 50, 32)), mindspore.float32)
8203
+ >>> output_tensor, argmax = max_pool2d_ext(input, kernel_size=(3, 2), stride=(2, 1),
8204
+ ceil_mode=False, return_indices=True)
8205
+ >>> print(output_tensor.shape)
8206
+ (20, 16, 24, 31)
8207
+ >>> print(argmax.shape)
8208
+ (20, 16, 24, 31)
8209
+ """
8210
+ strides = stride if (stride is not None) else kernel_size
8211
+ if return_indices:
8212
+ max_pool_func_ = _get_cache_prim(MaxPoolWithIndices)(kernel_size, strides, padding, dilation, ceil_mode)
8213
+ out, indices = max_pool_func_(input)
8214
+ else:
8215
+ max_pool_func_ = _get_cache_prim(MaxPoolWithMask)(kernel_size, strides, padding, dilation, ceil_mode)
8216
+ out, indices = max_pool_func_(input)
8217
+ if return_indices:
8218
+ return out, indices
8219
+ return out
8220
+
8221
+
7886
8222
  def prompt_flash_attention(query, key, value, attn_mask, actual_seq_lengths, actual_seq_lengths_kv, pse_shift,
7887
8223
  deq_scale1, quant_scale1, deq_scale2, quant_scale2, quant_offset2, num_heads,
7888
8224
  scale_value=1.0, pre_tokens=2147483547, next_tokens=0, input_layout='BSH',
@@ -7959,62 +8295,157 @@ def prompt_flash_attention(query, key, value, attn_mask, actual_seq_lengths, act
7959
8295
  quant_scale1, deq_scale2, quant_scale2, quant_offset2)
7960
8296
 
7961
8297
 
7962
- def incre_flash_attention(query, key, value, attn_mask, actual_seq_lengths, pse_shift, dequant_scale1, quant_scale1,
7963
- dequant_scale2, quant_scale2, quant_offset2, antiquant_scale, antiquant_offset, block_table,
7964
- num_heads, input_layout="BSH", scale_value=1.0, num_key_value_heads=0, block_size=0,
7965
- inner_precise=1):
8298
+ def incre_flash_attention(query, key, value, attn_mask=None, actual_seq_lengths=None, pse_shift=None,
8299
+ dequant_scale1=None, quant_scale1=None, dequant_scale2=None, quant_scale2=None,
8300
+ quant_offset2=None, antiquant_scale=None, antiquant_offset=None, block_table=None,
8301
+ num_heads=1, input_layout='BSH', scale_value=1.0, num_key_value_heads=0,
8302
+ block_size=0, inner_precise=1, kv_padding_size=None):
7966
8303
  r"""
7967
- The interface for fully inference.
7968
-
7969
8304
  B -- Batch size
7970
8305
 
8306
+ N -- Num heads
8307
+
8308
+ kvN -- Num key value heads
8309
+
7971
8310
  S -- Sequence length
7972
8311
 
8312
+ D -- Head dim
8313
+
7973
8314
  H -- Hidden size
7974
8315
 
8316
+ kvH -- Hidden size of key value
8317
+
8318
+ where :math:`H=N\times D`, :math:`kvH=kvN\times D`
8319
+
8320
+ Self attention constructs an attention model based on the relationship between input samples themselves. The
8321
+ principle is to assume that there is a length of the input sample sequence :math:`x` of :math:`n`, and each
8322
+ element of :math:`x` is a :math:`d` dimensional vector, which can be viewed as a token embedding. This sequence
8323
+ can be transformed through 3 weight matrices to obtain 3 matrices with dimensions of :math:`n\times d`. The self
8324
+ attention calculation formula is defined as:
8325
+
8326
+ .. math::
8327
+ Attention(Q,K,V)=Softmax(\frac{QK^{T} }{\sqrt{d} } )V
8328
+
8329
+ where the product of :math:`Q` and :math:`K^{T}` represents the attention of input :math:`x`. To avoid the value
8330
+ becoming too large, it is usually scaled by dividing it by the square root of :math:`d` and perform softmax
8331
+ normalization on each row, yields a matrix of :math:`n\times d` after multiplying :math:`V`.
8332
+
7975
8333
  .. warning::
7976
8334
  This is an experimental API that is subject to change or deletion.
7977
- If there is no input parameter and no default value, None needs to be passed.
7978
8335
 
7979
- Inputs:
7980
- - **query** (Tensor) - The query tensor with data type of float16 or bfloat16.
7981
- Input tensor of shape :math:`(B, 1, H)` / :math:`(B, N, 1, D)`.
7982
- - **key** (TensorList) - The key tensor with data type of float16 or bfloat16.
7983
- Input tensor of shape :math:`(B, S, H)` / :math:`(B, N, S, D)`.
7984
- - **value** (TensorList) - The value tensor with data type of float16 or bfloat16.
7985
- Input tensor of shape :math:`(B, S, H)` / :math:`(B, N, S, D)`.
7986
- - **attn_mask** (Tensor) - The attention mask tensor with data type of float16 or bool.
7987
- Input tensor of shape :math:`(B, S)` / :math:`(B, 1, S)` / :math:`(B, 1, 1, S)`.
7988
- - **actual_seq_lengths** (Tensor) - Describe actual sequence length of each input with data type of int.
7989
- - **pse_shift** (Tensor) - The position encoding tensor with data type of float16 or float32.
7990
- - **dequant_scale1** (Tensor) - Quantitative parametor, the tensor with data type of uint64.
7991
- - **quant_scale1** (Tensor) - Quantitative parametor, the tensor with data type of float.
7992
- - **dequant_scale2** (Tensor) - Quantitative parametor, the tensor with data type of uint64.
7993
- - **quant_scale2** (Tensor) - Quantitative parametor, the tensor with data type of float.
7994
- - **quant_offset2** (Tensor) - Quantitative parametor, the tensor with data type of float.
7995
- - **antiquant_scale** (Tensor) - Quantitative parametor, the tensor with data type of float.
7996
- - **antiquant_offset** (Tensor) - Quantitative parametor, the tensor with data type of float.
7997
- - **block_table** (Tensor) - The tensor with data type of float.
7998
- - **num_heads** (int) - The number of heads.
7999
- - **input_layout** (str) - the data layout of the input qkv, support `(BSH)` and `(BNSD)`. Default `BSH`.
8000
- - **scale_value** (double) - The scale value indicating the scale coefficient, which is used as the scalar of
8001
- Muls in the calculation. Default: 1.0.
8002
- - **num_key_value_heads** (int) - head numbers of key/value which are used in GQA algorithm.
8003
- The value o indicates if the key and value have the same head nums, use numHeads. Default: 0.
8004
- - **block_size** (int) - Default: 0.
8005
- - **inner_precise** (int) - Default: 1.
8336
+ Note:
8337
+ - If there is no input parameter and no default value, None needs to be passed.
8338
+ - The shape of the tensor corresponding to the key and value parameters needs to be completely consistent.
8339
+ - :math:`N` of parameter query is equal with num_heads. :math:`N` of parameter key and parameter value is equal
8340
+ with num_key_value_heads. num_heads is a multiple of num_key_value_heads.
8341
+ - Quantization
8342
+
8343
+ - When the data type of query, key, and value is float16 and the data type of output is int8, the input
8344
+ parameter quant_scale2 is required and quant_offset2 is optional.
8345
+ - When antiquant_scale exists, key and value need to be passed by int8. antiquant_offset is optional.
8346
+ - The data type of antiquant_scale and antiquant_offset should be consistency with that of query.
8347
+ - pse_shift
8348
+
8349
+ - The pse_shift data type needs to be consistent with the query data type, and only supports D-axis alignment,
8350
+ which means that the D-axis can be divided by 16.
8351
+ - Page attention:
8352
+
8353
+ - The necessary condition for enabling page attention is that the block_table exists, and the key
8354
+ and value are arranged in a contiguous memory according to the index in the block_table. The support for
8355
+ key and value dtypes is float16/bfloat16/int8.
8356
+ - In the enabling scenario of page attention, 16 alignment is required when input types of key and value are
8357
+ float16/bfloat16, and 32 alignment is required when input types of key and value are int8. It is
8358
+ recommended to use 128.
8359
+ - The maximum max_block_num_per_seq currently supported by blocktable is 16k, and exceeding 16k will result
8360
+ in interception and error messages; If you encounter :math:`S` being too large and causing
8361
+ max_block_num_per_seq to exceed 16k, you can increase the block_size to solve the problem.
8362
+ - The multiplication of all dimensions of the shape of the parameters key and value in the page attention
8363
+ scenario cannot exceed the representation range of int32.
8364
+ - When performing per-channel post quantization, page attention cannot be enabled simultaneously.
8365
+ - kv_padding_size:
8366
+
8367
+ - The calculation formula for the starting point of KV cache transfer is
8368
+ :math:`S-kv\_padding\_size-actual\_seq\_lengths`. The calculation formula for the transfer endpoint of KV
8369
+ cache is :math:`S-kv\_padding\_size`. When the starting or ending point of the KV cache transfer is less
8370
+ than 0, the returned data result is all 0.
8371
+ - When kv_padding_size is less than 0, it will be set to 0.
8372
+ - kv_padding_size needs to be enabled together with the actual_seq_lengths parameter, otherwise it is
8373
+ considered as the KV right padding scene.
8374
+ - It needs to be enabled together with the atten_mask parameter and ensure that the meaning of atten_mask is
8375
+ correct, that is, it can correctly hide invalid data. Otherwise, it will introduce accuracy issues.
8376
+ - kv_padding_size does not support page attention scenarios
8006
8377
 
8007
- Outputs:
8008
- - **attention_out** (Tensor) - Input tensor of shape :math:`(B, 1, H)` / :math:`(B, N, 1, D)`.
8378
+ Args:
8379
+ query (Tensor): The query tensor with data type of float16 or bfloat16.
8380
+ The shape is :math:`(B, 1, H)` / :math:`(B, N, 1, D)`.
8381
+ key (TensorList): The key tensor with data type of float16 or bfloat16 or int8.
8382
+ The shape is :math:`(B, S, kvH)` / :math:`(B, kvN, S, D)`.
8383
+ value (TensorList): The value tensor with data type of float16 or bfloat16 or int8.
8384
+ The shape is :math:`(B, S, kvH)` / :math:`(B, kvN, S, D)`.
8385
+ attn_mask (Tensor, optional): The attention mask tensor with data type of bool or int8 or uint8.
8386
+ The shape is :math:`(B, S)` / :math:`(B, 1, S)` / :math:`(B, 1, 1, S)`. Default: ``None``.
8387
+ actual_seq_lengths (Union[Tensor, tuple[int], list[int]], optional): Describe actual sequence length of each
8388
+ input with data type of int32 or int64. The shape is :math:`(B, )`. Default: ``None``.
8389
+ pse_shift (Tensor, optional): The position encoding tensor with data type of float16 or bfloat16. Input tensor
8390
+ of shape :math:`(1, N, 1, S)` / :math:`(B, N, 1, S)`. Default: ``None``.
8391
+ dequant_scale1 (Tensor, optional): Quantitative parametor, the tensor with data type of uint64 or float32. It
8392
+ is disable now. Default: ``None``.
8393
+ quant_scale1 (Tensor, optional): Quantitative parametor, the tensor with data type of float32. It is disable
8394
+ now. Default: ``None``.
8395
+ dequant_scale2 (Tensor, optional): Quantitative parametor, the tensor with data type of uint64 or float32. It
8396
+ is disable now. Default: ``None``.
8397
+ quant_scale2 (Tensor, optional): Post Quantitative parametor, the tensor with data type of float32.
8398
+ The shape is :math:`(1,)`. Default: ``None``.
8399
+ quant_offset2 (Tensor, optional): Post Quantitative parametor, the tensor with data type of float32.
8400
+ The shape is :math:`(1,)`. Default: ``None``.
8401
+ antiquant_scale (Tensor, optional): Pseudo Quantitative parametor, the tensor with data type of float16 or
8402
+ bfloat16. The shape is :math:`(2, kvN, 1, D)` when input_layout is 'BNSD' or :math:`(2, kvH)` when
8403
+ input_layout is 'BSH'. Default: ``None``.
8404
+ antiquant_offset (Tensor, optional): Pseudo Quantitative parametor, the tensor with data type of float16 or
8405
+ bfloat16. The shape is :math:`(2, kvN, 1, D)` when input_layout is 'BNSD' or :math:`(2, kvH)` when
8406
+ input_layout is 'BSH'. Default: ``None``.
8407
+ block_table (Tensor, optional): The tensor with data type of int32. The shape is
8408
+ :math:`(B, max\_block\_num\_per\_seq)`,
8409
+ where :math:`max\_block\_num\_per\_seq = ceil(\frac{max(actual\_seq\_length)}{block\_size} )`.
8410
+ Default: ``None``.
8411
+ num_heads (int): The number of heads.
8412
+ input_layout (str): The data layout of the input qkv, support 'BSH' and 'BNSD'. Default ``'BSH'``.
8413
+ scale_value (double): The scale value indicating the scale coefficient, which is used as the scalar of
8414
+ Muls in the calculation. Default: ``1.0``.
8415
+ num_key_value_heads (int): Head numbers of key/value which are used in GQA algorithm.
8416
+ The value 0 indicates if the key and value have the same head nums, use numHeads. Default: ``0``.
8417
+ block_size (int): The maximum number of tokens stored in each block of KV in page attention. Default: ``0``.
8418
+ inner_precise (int): Default: ``1``.
8419
+ kv_padding_size (Tensor, optional): The tensor with data type of int64. The range of values is
8420
+ :math:`0\le kv\_padding\_size \le S-max(actual\_seq\_length)`. The shape is :math:`()` or :math:`(1,)`.
8421
+ Default: ``None``.
8422
+
8423
+ Returns:
8424
+ attention_out (Tensor), the shape is :math:`(B, 1, H)` / :math:`(B, N, 1, D)`.
8009
8425
 
8010
8426
  Supported Platforms:
8011
8427
  ``Ascend``
8012
- """
8013
8428
 
8429
+ Examples:
8430
+ >>> from mindspore import ops
8431
+ >>> from mindspore.common import Tensor
8432
+ >>> from mindspore.common import dtype as mstype
8433
+ >>> import numpy as np
8434
+ >>> B, N, S, D, kvN = 1, 4, 10, 128, 1
8435
+ >>> query = Tensor(np.random.randn(B, 1, N * D), mstype.float16)
8436
+ >>> key = [Tensor(np.random.randn(B, S, kvN * D), mstype.float16)]
8437
+ >>> value = [Tensor(np.random.randn(B, S, kvN * D), mstype.float16)]
8438
+ >>> ifa_ms = ops.functional.incre_flash_attention
8439
+ >>> attn_out = ifa_ms(query, key, value, num_heads=N, num_key_value_heads=kvN)
8440
+ >>> attn_out
8441
+ Tensor(shape=[1, 1, 512], dtype=Float16, value=
8442
+ [[[ 1.6104e+00, 7.3438e-01, 1.0684e+00 ... -8.7891e-01, 1.7695e+00, 1.0264e+00]]])
8443
+ """
8014
8444
  _ifa = _get_cache_prim(NN_OPS.IncreFlashAttention)(num_heads, input_layout, scale_value, num_key_value_heads,
8015
8445
  block_size, inner_precise)
8016
8446
  return _ifa(query, key, value, attn_mask, actual_seq_lengths, pse_shift, dequant_scale1, quant_scale1,
8017
- dequant_scale2, quant_scale2, quant_offset2, antiquant_scale, antiquant_offset, block_table)
8447
+ dequant_scale2, quant_scale2, quant_offset2, antiquant_scale, antiquant_offset, block_table,
8448
+ kv_padding_size)
8018
8449
 
8019
8450
 
8020
8451
  def embedding(input, weight, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False):
@@ -8066,14 +8497,13 @@ def embedding(input, weight, padding_idx=None, max_norm=None, norm_type=2.0, sca
8066
8497
  [ 5.49015924e-02, 3.47811311e-01, -1.89771220e-01],
8067
8498
  [ 2.09307984e-01, -2.24846993e-02, 3.40124398e-01]]]
8068
8499
  """
8069
- if not isinstance(weight, Parameter):
8070
- raise TypeError(f"For Embedding, the weight must be a mindspore.Parameter, but got {type(weight)}.")
8071
8500
  return embedding_op(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq)
8072
8501
 
8073
8502
 
8074
8503
  __all__ = [
8075
8504
  'adaptive_avg_pool1d',
8076
8505
  'adaptive_avg_pool2d',
8506
+ 'adaptive_avg_pool2d_ext',
8077
8507
  'adaptive_avg_pool3d',
8078
8508
  'adaptive_max_pool1d',
8079
8509
  'adaptive_max_pool2d',
@@ -8104,6 +8534,7 @@ __all__ = [
8104
8534
  'pixel_unshuffle',
8105
8535
  'hardshrink',
8106
8536
  'is_floating_point',
8537
+ 'incre_flash_attention',
8107
8538
  'flip',
8108
8539
  'fliplr',
8109
8540
  'flipud',
@@ -8111,6 +8542,7 @@ __all__ = [
8111
8542
  'interpolate',
8112
8543
  'upsample',
8113
8544
  'layer_norm',
8545
+ 'mse_loss_ext',
8114
8546
  'log_softmax',
8115
8547
  'mish',
8116
8548
  'lrn',
@@ -8149,6 +8581,7 @@ __all__ = [
8149
8581
  'relu',
8150
8582
  'relu6',
8151
8583
  'rrelu',
8584
+ 'swiglu',
8152
8585
  'conv3d',
8153
8586
  'glu',
8154
8587
  'margin_ranking_loss',
@@ -8169,6 +8602,7 @@ __all__ = [
8169
8602
  'triplet_margin_loss',
8170
8603
  'channel_shuffle',
8171
8604
  'hardsigmoid',
8605
+ 'add_layer_norm',
8172
8606
  'group_norm',
8173
8607
  'rms_norm',
8174
8608
  ]