mindspore 1.10.0__cp37-none-any.whl → 2.0.0rc1__cp37-none-any.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 (944) hide show
  1. mindspore/.commit_id +1 -1
  2. mindspore/Third_Party_Open_Source_Software_Notice +9064 -0
  3. mindspore/__init__.py +9 -4
  4. mindspore/_akg/akg/composite/build_module.py +11 -0
  5. mindspore/_akg/akg/config/repository_cuda.json +11 -0
  6. mindspore/_akg/akg/tvm/contrib/nvcc.py +4 -3
  7. mindspore/_c_dataengine.cpython-37m-aarch64-linux-gnu.so +0 -0
  8. mindspore/_c_expression.cpython-37m-aarch64-linux-gnu.so +0 -0
  9. mindspore/_c_mindrecord.cpython-37m-aarch64-linux-gnu.so +0 -0
  10. mindspore/_check_jit_forbidden_api.py +102 -0
  11. mindspore/_checkparam.py +1066 -1001
  12. mindspore/_extends/builtin_operations.py +32 -4
  13. mindspore/_extends/graph_kernel/model/graph_split.py +66 -222
  14. mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +12 -9
  15. mindspore/_extends/parallel_compile/akg_compiler/build_tbe_kernel.py +119 -26
  16. mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +50 -50
  17. mindspore/_extends/parallel_compile/akg_compiler/util.py +9 -6
  18. mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py +4 -25
  19. mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py +9 -4
  20. mindspore/_extends/parallel_compile/tbe_compiler/tbe_job_manager.py +1 -27
  21. mindspore/_extends/parse/__init__.py +5 -3
  22. mindspore/_extends/parse/namespace.py +17 -2
  23. mindspore/_extends/parse/parser.py +193 -34
  24. mindspore/_extends/parse/resources.py +7 -8
  25. mindspore/_extends/parse/standard_method.py +1780 -435
  26. mindspore/_extends/parse/trope.py +3 -1
  27. mindspore/_mindspore_offline_debug.cpython-37m-aarch64-linux-gnu.so +0 -0
  28. mindspore/amp.py +53 -58
  29. mindspore/bin/cache_admin +0 -0
  30. mindspore/bin/cache_server +0 -0
  31. mindspore/boost/adasum.py +3 -2
  32. mindspore/boost/boost.py +2 -2
  33. mindspore/boost/boost_cell_wrapper.py +46 -26
  34. mindspore/boost/dim_reduce.py +6 -5
  35. mindspore/boost/grad_accumulation.py +2 -1
  36. mindspore/boost/group_loss_scale_manager.py +1 -1
  37. mindspore/common/__init__.py +11 -10
  38. mindspore/common/_decorator.py +2 -0
  39. mindspore/common/_register_for_adapter.py +55 -0
  40. mindspore/common/_stub_tensor.py +201 -0
  41. mindspore/common/_utils.py +57 -0
  42. mindspore/common/api.py +582 -297
  43. mindspore/common/dtype.py +66 -18
  44. mindspore/common/dump.py +2 -2
  45. mindspore/common/initializer.py +38 -1
  46. mindspore/common/jit_config.py +25 -13
  47. mindspore/common/mutable.py +53 -24
  48. mindspore/common/parameter.py +60 -37
  49. mindspore/common/seed.py +8 -24
  50. mindspore/common/sparse_tensor.py +927 -0
  51. mindspore/common/tensor.py +1627 -3900
  52. mindspore/communication/__init__.py +10 -5
  53. mindspore/communication/_comm_helper.py +78 -214
  54. mindspore/communication/_hccl_management.py +2 -1
  55. mindspore/communication/management.py +136 -47
  56. mindspore/config/op_info.config +501 -1008
  57. mindspore/config/super_bar_config.json +512 -0
  58. mindspore/context.py +291 -56
  59. mindspore/dataset/__init__.py +12 -8
  60. mindspore/dataset/audio/__init__.py +9 -9
  61. mindspore/dataset/audio/transforms.py +1090 -228
  62. mindspore/dataset/audio/utils.py +87 -39
  63. mindspore/dataset/audio/validators.py +223 -1
  64. mindspore/dataset/callback/ds_callback.py +17 -15
  65. mindspore/dataset/core/config.py +246 -17
  66. mindspore/dataset/core/py_util_helpers.py +4 -3
  67. mindspore/dataset/core/validator_helpers.py +10 -10
  68. mindspore/{parallel/nn/layers.py → dataset/debug/__init__.py} +7 -8
  69. mindspore/dataset/debug/debug_hook.py +65 -0
  70. mindspore/dataset/debug/pre_defined_hook.py +67 -0
  71. mindspore/dataset/engine/__init__.py +7 -3
  72. mindspore/dataset/engine/cache_client.py +9 -9
  73. mindspore/dataset/engine/datasets.py +648 -477
  74. mindspore/dataset/engine/datasets_audio.py +165 -167
  75. mindspore/dataset/engine/datasets_standard_format.py +93 -67
  76. mindspore/dataset/engine/datasets_text.py +492 -342
  77. mindspore/dataset/engine/datasets_user_defined.py +85 -50
  78. mindspore/dataset/engine/datasets_vision.py +1224 -699
  79. mindspore/dataset/engine/graphdata.py +134 -69
  80. mindspore/dataset/engine/iterators.py +50 -9
  81. mindspore/dataset/engine/offload.py +52 -31
  82. mindspore/dataset/engine/samplers.py +27 -24
  83. mindspore/dataset/engine/serializer_deserializer.py +14 -15
  84. mindspore/dataset/engine/validators.py +213 -52
  85. mindspore/dataset/text/__init__.py +10 -8
  86. mindspore/dataset/text/transforms.py +152 -57
  87. mindspore/dataset/text/utils.py +98 -49
  88. mindspore/dataset/text/validators.py +25 -0
  89. mindspore/dataset/transforms/__init__.py +4 -2
  90. mindspore/dataset/transforms/c_transforms.py +11 -13
  91. mindspore/dataset/transforms/py_transforms.py +2 -2
  92. mindspore/dataset/transforms/py_transforms_util.py +10 -0
  93. mindspore/dataset/transforms/transforms.py +13 -15
  94. mindspore/dataset/transforms/validators.py +7 -7
  95. mindspore/dataset/utils/__init__.py +2 -1
  96. mindspore/dataset/utils/browse_dataset.py +13 -13
  97. mindspore/dataset/utils/line_reader.py +121 -0
  98. mindspore/dataset/vision/__init__.py +8 -7
  99. mindspore/dataset/vision/c_transforms.py +125 -126
  100. mindspore/dataset/vision/py_transforms.py +37 -37
  101. mindspore/dataset/vision/py_transforms_util.py +23 -20
  102. mindspore/dataset/vision/transforms.py +316 -315
  103. mindspore/dataset/vision/utils.py +313 -17
  104. mindspore/dataset/vision/validators.py +6 -6
  105. mindspore/default_config.py +0 -1
  106. mindspore/{compression → experimental}/__init__.py +6 -5
  107. mindspore/experimental/map_parameter.py +275 -0
  108. mindspore/include/OWNERS +0 -1
  109. mindspore/include/api/callback/callback.h +9 -13
  110. mindspore/include/api/callback/ckpt_saver.h +2 -2
  111. mindspore/include/api/callback/loss_monitor.h +2 -2
  112. mindspore/include/api/callback/lr_scheduler.h +5 -5
  113. mindspore/include/api/callback/time_monitor.h +2 -2
  114. mindspore/include/api/callback/train_accuracy.h +4 -6
  115. mindspore/include/api/cfg.h +19 -6
  116. mindspore/include/api/context.h +70 -9
  117. mindspore/include/api/delegate.h +8 -1
  118. mindspore/include/api/dual_abi_helper.h +8 -24
  119. mindspore/include/api/metrics/accuracy.h +2 -2
  120. mindspore/include/api/metrics/metrics.h +4 -3
  121. mindspore/include/api/model.h +9 -4
  122. mindspore/include/api/model_group.h +68 -0
  123. mindspore/include/api/model_parallel_runner.h +17 -17
  124. mindspore/include/api/net.h +12 -11
  125. mindspore/include/api/serialization.h +20 -4
  126. mindspore/include/api/status.h +7 -1
  127. mindspore/include/api/types.h +25 -21
  128. mindspore/include/api/visible.h +4 -0
  129. mindspore/include/c_api/model_c.h +5 -0
  130. mindspore/include/c_api/status_c.h +1 -1
  131. mindspore/include/dataset/config.h +1 -1
  132. mindspore/include/dataset/constants.h +14 -0
  133. mindspore/include/dataset/text.h +59 -0
  134. mindspore/include/dataset/vision.h +56 -117
  135. mindspore/include/dataset/vision_lite.h +102 -0
  136. mindspore/include/mindapi/base/type_id.h +42 -3
  137. mindspore/lib/libdnnl.so.2 +0 -0
  138. mindspore/lib/libicudata.so.69 +0 -0
  139. mindspore/lib/libicui18n.so.69 +0 -0
  140. mindspore/lib/libicuuc.so.69 +0 -0
  141. mindspore/lib/libmindspore.so +0 -0
  142. mindspore/lib/libmindspore_backend.so +0 -0
  143. mindspore/lib/libmindspore_common.so +0 -0
  144. mindspore/lib/libmindspore_core.so +0 -0
  145. mindspore/lib/libmindspore_glog.so.0 +0 -0
  146. mindspore/lib/libmindspore_gpr.so.15 +0 -0
  147. mindspore/lib/libmindspore_grpc++.so.1 +0 -0
  148. mindspore/lib/libmindspore_grpc.so.15 +0 -0
  149. mindspore/lib/libmindspore_shared_lib.so +0 -0
  150. mindspore/lib/libmpi_adapter.so +0 -0
  151. mindspore/lib/libmpi_collective.so +0 -0
  152. mindspore/lib/libnnacl.so +0 -0
  153. mindspore/lib/libopencv_core.so.4.5 +0 -0
  154. mindspore/lib/libopencv_imgcodecs.so.4.5 +0 -0
  155. mindspore/lib/libopencv_imgproc.so.4.5 +0 -0
  156. mindspore/lib/libps_cache.so +0 -0
  157. mindspore/lib/plugin/ascend/libakg.so +0 -0
  158. mindspore/lib/plugin/ascend/libascend_collective.so +0 -0
  159. mindspore/lib/plugin/ascend/libdvpp_utils.so +0 -0
  160. mindspore/lib/plugin/ascend/libhccl_plugin.so +0 -0
  161. mindspore/lib/plugin/ascend/libmindspore_aicpu_kernels.so +0 -0
  162. mindspore/lib/plugin/ascend/libmindspore_cpu_kernels.so +0 -0
  163. mindspore/lib/{libakg.so → plugin/cpu/libakg.so} +0 -0
  164. mindspore/lib/plugin/libmindspore_ascend.so.1 +0 -0
  165. mindspore/lib/plugin/libmindspore_ascend.so.2 +0 -0
  166. mindspore/log.py +28 -28
  167. mindspore/mindrecord/common/exceptions.py +2 -4
  168. mindspore/mindrecord/filereader.py +19 -1
  169. mindspore/mindrecord/filewriter.py +250 -88
  170. mindspore/mindrecord/mindpage.py +13 -13
  171. mindspore/mindrecord/shardheader.py +15 -15
  172. mindspore/mindrecord/shardreader.py +9 -0
  173. mindspore/mindrecord/shardwriter.py +29 -29
  174. mindspore/mindrecord/tools/cifar100_to_mr.py +9 -9
  175. mindspore/mindrecord/tools/cifar10_to_mr.py +9 -9
  176. mindspore/mindrecord/tools/csv_to_mr.py +4 -4
  177. mindspore/mindrecord/tools/imagenet_to_mr.py +70 -65
  178. mindspore/mindrecord/tools/mnist_to_mr.py +41 -41
  179. mindspore/mindrecord/tools/tfrecord_to_mr.py +6 -6
  180. mindspore/nn/__init__.py +1 -5
  181. mindspore/nn/cell.py +297 -234
  182. mindspore/nn/dynamic_lr.py +1 -1
  183. mindspore/nn/grad/cell_grad.py +17 -42
  184. mindspore/nn/layer/__init__.py +7 -4
  185. mindspore/nn/layer/activation.py +131 -88
  186. mindspore/nn/layer/basic.py +313 -613
  187. mindspore/nn/layer/channel_shuffle.py +103 -0
  188. mindspore/nn/layer/combined.py +1 -1
  189. mindspore/nn/layer/container.py +52 -6
  190. mindspore/nn/layer/conv.py +112 -43
  191. mindspore/nn/layer/dense.py +10 -9
  192. mindspore/nn/layer/embedding.py +36 -34
  193. mindspore/nn/layer/image.py +123 -27
  194. mindspore/nn/layer/math.py +108 -107
  195. mindspore/nn/layer/normalization.py +212 -366
  196. mindspore/nn/layer/padding.py +370 -42
  197. mindspore/nn/layer/pooling.py +1443 -219
  198. mindspore/nn/layer/rnn_cells.py +11 -16
  199. mindspore/nn/layer/rnns.py +38 -39
  200. mindspore/nn/layer/thor_layer.py +24 -25
  201. mindspore/nn/layer/timedistributed.py +5 -5
  202. mindspore/nn/layer/transformer.py +701 -0
  203. mindspore/nn/learning_rate_schedule.py +8 -8
  204. mindspore/nn/loss/__init__.py +9 -6
  205. mindspore/nn/loss/loss.py +678 -142
  206. mindspore/nn/metrics.py +53 -0
  207. mindspore/nn/optim/_dist_optimizer_registry.py +2 -2
  208. mindspore/nn/optim/ada_grad.py +8 -8
  209. mindspore/nn/optim/adadelta.py +2 -3
  210. mindspore/nn/optim/adafactor.py +18 -14
  211. mindspore/nn/optim/adam.py +429 -87
  212. mindspore/nn/optim/adamax.py +5 -6
  213. mindspore/nn/optim/adasum.py +10 -8
  214. mindspore/nn/optim/asgd.py +7 -7
  215. mindspore/nn/optim/ftrl.py +81 -11
  216. mindspore/nn/optim/lamb.py +7 -8
  217. mindspore/nn/optim/lars.py +4 -4
  218. mindspore/nn/optim/lazyadam.py +82 -7
  219. mindspore/nn/optim/momentum.py +8 -7
  220. mindspore/nn/optim/optimizer.py +19 -10
  221. mindspore/nn/optim/proximal_ada_grad.py +6 -5
  222. mindspore/nn/optim/rmsprop.py +3 -3
  223. mindspore/nn/optim/rprop.py +20 -16
  224. mindspore/nn/optim/sgd.py +21 -15
  225. mindspore/nn/optim/thor.py +23 -21
  226. mindspore/nn/probability/__init__.py +0 -2
  227. mindspore/nn/probability/bijector/bijector.py +7 -6
  228. mindspore/nn/probability/bijector/invert.py +4 -2
  229. mindspore/nn/probability/bijector/softplus.py +2 -2
  230. mindspore/nn/probability/bnn_layers/dense_variational.py +1 -1
  231. mindspore/nn/probability/bnn_layers/layer_distribution.py +2 -2
  232. mindspore/nn/probability/distribution/__init__.py +6 -0
  233. mindspore/nn/probability/distribution/_utils/custom_ops.py +3 -2
  234. mindspore/nn/probability/distribution/_utils/utils.py +11 -17
  235. mindspore/nn/probability/distribution/bernoulli.py +6 -6
  236. mindspore/nn/probability/distribution/beta.py +1 -1
  237. mindspore/nn/probability/distribution/categorical.py +9 -9
  238. mindspore/nn/probability/distribution/cauchy.py +8 -8
  239. mindspore/nn/probability/distribution/distribution.py +12 -6
  240. mindspore/nn/probability/distribution/exponential.py +5 -5
  241. mindspore/nn/probability/distribution/gamma.py +3 -3
  242. mindspore/nn/probability/distribution/geometric.py +6 -5
  243. mindspore/nn/probability/distribution/gumbel.py +5 -5
  244. mindspore/nn/probability/distribution/half_normal.py +133 -0
  245. mindspore/nn/probability/distribution/laplace.py +128 -0
  246. mindspore/nn/probability/distribution/log_normal.py +0 -1
  247. mindspore/nn/probability/distribution/logistic.py +4 -5
  248. mindspore/nn/probability/distribution/normal.py +11 -15
  249. mindspore/nn/probability/distribution/poisson.py +6 -2
  250. mindspore/nn/probability/distribution/student_t.py +150 -0
  251. mindspore/nn/probability/distribution/transformed_distribution.py +4 -4
  252. mindspore/nn/probability/distribution/uniform.py +5 -5
  253. mindspore/nn/reinforcement/_tensors_queue.py +3 -3
  254. mindspore/nn/reinforcement/tensor_array.py +2 -2
  255. mindspore/nn/sparse/sparse.py +8 -1
  256. mindspore/nn/wrap/cell_wrapper.py +55 -27
  257. mindspore/nn/wrap/grad_reducer.py +20 -11
  258. mindspore/nn/wrap/loss_scale.py +47 -30
  259. mindspore/numpy/array_creations.py +33 -22
  260. mindspore/numpy/array_ops.py +46 -42
  261. mindspore/numpy/logic_ops.py +6 -27
  262. mindspore/numpy/math_ops.py +26 -19
  263. mindspore/numpy/utils.py +1 -8
  264. mindspore/numpy/utils_const.py +112 -62
  265. mindspore/ops/__init__.py +6 -3
  266. mindspore/ops/_constants.py +0 -6
  267. mindspore/ops/_grad/__init__.py +2 -1
  268. mindspore/ops/_grad/grad_array_ops.py +209 -152
  269. mindspore/ops/_grad/grad_base.py +55 -17
  270. mindspore/ops/_grad/grad_clip_ops.py +11 -3
  271. mindspore/ops/_grad/grad_comm_ops.py +58 -47
  272. mindspore/ops/_grad/grad_implementations.py +21 -61
  273. mindspore/ops/_grad/grad_inner_ops.py +48 -6
  274. mindspore/ops/_grad/grad_math_ops.py +306 -161
  275. mindspore/ops/_grad/grad_nn_ops.py +192 -181
  276. mindspore/ops/_grad/grad_other_ops.py +1 -1
  277. mindspore/ops/_grad/grad_quant_ops.py +5 -5
  278. mindspore/ops/_grad/grad_sequence_ops.py +296 -0
  279. mindspore/ops/_grad/grad_sparse.py +15 -9
  280. mindspore/ops/_grad_experimental/__init__.py +1 -0
  281. mindspore/ops/_grad_experimental/grad_array_ops.py +441 -55
  282. mindspore/ops/_grad_experimental/grad_image_ops.py +25 -7
  283. mindspore/ops/_grad_experimental/grad_inner_ops.py +3 -44
  284. mindspore/ops/_grad_experimental/grad_linalg_ops.py +16 -21
  285. mindspore/ops/_grad_experimental/grad_math_ops.py +979 -49
  286. mindspore/ops/_grad_experimental/grad_nn_ops.py +78 -8
  287. mindspore/ops/_grad_experimental/grad_scalar_ops.py +112 -0
  288. mindspore/ops/_grad_experimental/grad_sparse_ops.py +197 -13
  289. mindspore/ops/_op_impl/__init__.py +3 -3
  290. mindspore/ops/_op_impl/_custom_op/__init__.py +0 -1
  291. mindspore/ops/_op_impl/_custom_op/_basic.py +0 -1
  292. mindspore/ops/_op_impl/_custom_op/batch_matmul_impl.py +1 -1
  293. mindspore/ops/_op_impl/_custom_op/batchnorm_fold.py +4 -2
  294. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2.py +2 -2
  295. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad.py +2 -2
  296. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad_reduce.py +5 -5
  297. mindspore/ops/_op_impl/_custom_op/batchnorm_fold_grad.py +3 -3
  298. mindspore/ops/_op_impl/_custom_op/cholesky_trsm_impl.py +1 -1
  299. mindspore/ops/_op_impl/_custom_op/correction_mul.py +3 -3
  300. mindspore/ops/_op_impl/_custom_op/correction_mul_grad.py +2 -2
  301. mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +4 -8
  302. mindspore/ops/_op_impl/_custom_op/dsd_impl.py +1 -1
  303. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel.py +2 -2
  304. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad.py +2 -2
  305. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad_reduce.py +2 -2
  306. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer.py +2 -2
  307. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad.py +2 -2
  308. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad_reduce.py +2 -2
  309. mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel.py +2 -2
  310. mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel_grad.py +2 -2
  311. mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer.py +2 -2
  312. mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer_grad.py +2 -2
  313. mindspore/ops/_op_impl/_custom_op/fused_abs_max1_impl.py +1 -1
  314. mindspore/ops/_op_impl/_custom_op/img2col_impl.py +1 -1
  315. mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_left_impl.py +2 -2
  316. mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_right_impl.py +1 -1
  317. mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_left_cast_impl.py +1 -1
  318. mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_right_mul_impl.py +1 -1
  319. mindspore/ops/_op_impl/_custom_op/matmul_cube_impl.py +2 -2
  320. mindspore/ops/_op_impl/_custom_op/matmul_dds_grad_impl.py +0 -1
  321. mindspore/ops/_op_impl/_custom_op/matmul_dds_impl.py +0 -1
  322. mindspore/ops/_op_impl/_custom_op/matrix_combine_impl.py +1 -1
  323. mindspore/ops/_op_impl/_custom_op/minmax_update_perchannel.py +2 -2
  324. mindspore/ops/_op_impl/_custom_op/minmax_update_perlayer.py +2 -2
  325. mindspore/ops/_op_impl/_custom_op/transpose02314_impl.py +1 -1
  326. mindspore/ops/_op_impl/aicpu/__init__.py +238 -3
  327. mindspore/ops/_op_impl/aicpu/abs.py +36 -0
  328. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d.py +34 -0
  329. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d_grad.py +34 -0
  330. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d.py +39 -0
  331. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d_grad.py +39 -0
  332. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d_grad.py +37 -0
  333. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d.py +42 -0
  334. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d_grad.py +152 -0
  335. mindspore/ops/_op_impl/aicpu/add.py +43 -0
  336. mindspore/ops/_op_impl/aicpu/addcdiv.py +0 -32
  337. mindspore/ops/_op_impl/aicpu/addcmul.py +0 -84
  338. mindspore/ops/_op_impl/aicpu/affine_grid_grad.py +35 -0
  339. mindspore/ops/_op_impl/aicpu/arg_max.py +75 -0
  340. mindspore/ops/_op_impl/aicpu/arg_min.py +75 -0
  341. mindspore/ops/_op_impl/aicpu/argmin_with_value.py +43 -0
  342. mindspore/ops/_op_impl/aicpu/batch_matmul.py +43 -0
  343. mindspore/ops/_op_impl/aicpu/batch_norm_grad_grad.py +49 -0
  344. mindspore/ops/_op_impl/aicpu/bernoulli.py +48 -0
  345. mindspore/ops/_op_impl/aicpu/bessel_i0.py +31 -0
  346. mindspore/ops/_op_impl/aicpu/bias_add.py +44 -0
  347. mindspore/ops/_op_impl/aicpu/bias_add_grad.py +43 -0
  348. mindspore/ops/_op_impl/aicpu/bincount.py +33 -0
  349. mindspore/{nn/probability/infer/variational/__init__.py → ops/_op_impl/aicpu/cauchy.py} +17 -10
  350. mindspore/ops/_op_impl/aicpu/channel_shuffle.py +40 -0
  351. mindspore/ops/_op_impl/aicpu/cholesky.py +1 -1
  352. mindspore/ops/_op_impl/{cpu/bias_add.py → aicpu/choleskygrad.py} +9 -7
  353. mindspore/ops/_op_impl/aicpu/combined_non_max_suppression.py +42 -0
  354. mindspore/ops/_op_impl/aicpu/concat_offset.py +42 -0
  355. mindspore/ops/_op_impl/aicpu/concat_offset_v1.py +31 -0
  356. mindspore/ops/_op_impl/aicpu/conj.py +11 -0
  357. mindspore/ops/_op_impl/aicpu/crop_and_resize_grad_image.py +38 -0
  358. mindspore/ops/_op_impl/aicpu/cumulative_logsumexp.py +36 -0
  359. mindspore/ops/_op_impl/aicpu/deformable_offsets.py +38 -0
  360. mindspore/ops/_op_impl/aicpu/deformable_offsets_grad.py +2 -2
  361. mindspore/ops/_op_impl/aicpu/dense_to_sparse_set_operation.py +48 -0
  362. mindspore/ops/_op_impl/aicpu/diag.py +36 -0
  363. mindspore/ops/_op_impl/aicpu/diag_part.py +36 -0
  364. mindspore/ops/_op_impl/aicpu/diagonal.py +35 -0
  365. mindspore/ops/_op_impl/{cpu/bias_add_grad.py → aicpu/digamma.py} +9 -7
  366. mindspore/ops/_op_impl/aicpu/eig.py +35 -0
  367. mindspore/ops/_op_impl/aicpu/fft_with_size.py +41 -0
  368. mindspore/ops/_op_impl/aicpu/flatten.py +1 -0
  369. mindspore/ops/_op_impl/aicpu/fmax.py +36 -0
  370. mindspore/ops/_op_impl/aicpu/fmin.py +37 -0
  371. mindspore/ops/_op_impl/aicpu/fractional_max_pool3d_with_fixed_ksize.py +1 -1
  372. mindspore/ops/_op_impl/aicpu/fse_decode.py +43 -0
  373. mindspore/ops/_op_impl/aicpu/glu.py +33 -0
  374. mindspore/ops/_op_impl/aicpu/glu_grad.py +34 -0
  375. mindspore/ops/_op_impl/aicpu/greater.py +41 -0
  376. mindspore/ops/_op_impl/aicpu/greater_equal.py +41 -0
  377. mindspore/ops/_op_impl/aicpu/index_put.py +50 -0
  378. mindspore/ops/_op_impl/{tbe/scatter_add_ds.py → aicpu/inplace_index_add.py} +17 -21
  379. mindspore/ops/_op_impl/aicpu/instance_norm_v2.py +41 -0
  380. mindspore/ops/_op_impl/aicpu/instance_norm_v2_grad.py +44 -0
  381. mindspore/ops/_op_impl/aicpu/layer_norm_grad_grad.py +47 -0
  382. mindspore/ops/_op_impl/aicpu/less.py +41 -0
  383. mindspore/ops/_op_impl/aicpu/less_equal.py +41 -0
  384. mindspore/ops/_op_impl/aicpu/lgamma.py +32 -0
  385. mindspore/ops/_op_impl/aicpu/log_normal_reverse.py +33 -0
  386. mindspore/ops/_op_impl/aicpu/logit.py +33 -0
  387. mindspore/ops/_op_impl/aicpu/logit_grad.py +34 -0
  388. mindspore/ops/_op_impl/aicpu/masked_fill.py +42 -0
  389. mindspore/ops/_op_impl/aicpu/masked_scatter.py +39 -0
  390. mindspore/ops/_op_impl/aicpu/matmul.py +39 -0
  391. mindspore/ops/_op_impl/aicpu/matrix_logarithm.py +31 -0
  392. mindspore/ops/_op_impl/aicpu/matrix_power.py +32 -0
  393. mindspore/ops/_op_impl/aicpu/matrix_solve_ls.py +36 -0
  394. mindspore/ops/_op_impl/aicpu/matrix_triangular_solve.py +36 -0
  395. mindspore/ops/_op_impl/aicpu/mirror_pad.py +2 -0
  396. mindspore/ops/_op_impl/aicpu/mirror_pad_grad.py +0 -4
  397. mindspore/ops/_op_impl/aicpu/mul.py +3 -1
  398. mindspore/ops/_op_impl/aicpu/multinomial.py +14 -6
  399. mindspore/ops/_op_impl/aicpu/multinomial_with_replacement.py +35 -0
  400. mindspore/ops/_op_impl/aicpu/nan_to_num.py +34 -0
  401. mindspore/ops/_op_impl/aicpu/nllloss.py +38 -0
  402. mindspore/ops/_op_impl/aicpu/nllloss_grad.py +39 -0
  403. mindspore/ops/_op_impl/aicpu/ones_like.py +0 -2
  404. mindspore/ops/_op_impl/aicpu/polar.py +32 -0
  405. mindspore/ops/_op_impl/aicpu/polygamma.py +34 -0
  406. mindspore/ops/_op_impl/aicpu/qr.py +36 -0
  407. mindspore/ops/_op_impl/aicpu/quant_dtype_cast.py +40 -0
  408. mindspore/ops/_op_impl/aicpu/quantile.py +35 -0
  409. mindspore/ops/_op_impl/aicpu/ragged_tensor_to_sparse.py +73 -0
  410. mindspore/ops/_op_impl/aicpu/ragged_tensor_to_tensor.py +74 -0
  411. mindspore/ops/_op_impl/aicpu/random_shuffle.py +3 -0
  412. mindspore/ops/_op_impl/aicpu/randperm_v2.py +41 -0
  413. mindspore/ops/_op_impl/aicpu/range.py +36 -0
  414. mindspore/ops/_op_impl/aicpu/reciprocal.py +34 -0
  415. mindspore/ops/_op_impl/aicpu/reciprocal_grad.py +35 -0
  416. mindspore/ops/_op_impl/aicpu/reduce_sum.py +57 -0
  417. mindspore/ops/_op_impl/aicpu/resize_bicubic.py +2 -8
  418. mindspore/ops/_op_impl/aicpu/resize_bicubic_grad.py +1 -1
  419. mindspore/ops/_op_impl/aicpu/resize_v2.py +68 -0
  420. mindspore/ops/_op_impl/aicpu/resize_v2_grad.py +68 -0
  421. mindspore/ops/_op_impl/aicpu/scatter_elements.py +4 -0
  422. mindspore/ops/_op_impl/aicpu/scatter_nd_update.py +2 -0
  423. mindspore/ops/_op_impl/aicpu/search_sorted.py +12 -6
  424. mindspore/ops/_op_impl/aicpu/self_adjoint_eig.py +34 -0
  425. mindspore/ops/_op_impl/aicpu/sequence_add.py +34 -0
  426. mindspore/ops/_op_impl/aicpu/sequence_add_offset.py +34 -0
  427. mindspore/ops/_op_impl/aicpu/sequence_addn.py +38 -0
  428. mindspore/ops/_op_impl/aicpu/slice_grad.py +76 -0
  429. mindspore/ops/_op_impl/aicpu/smooth_l1_loss.py +35 -0
  430. mindspore/ops/_op_impl/aicpu/smooth_l1_loss_grad.py +37 -0
  431. mindspore/ops/_op_impl/aicpu/sort.py +39 -0
  432. mindspore/ops/_op_impl/aicpu/sparse_apply_adagrad_da.py +0 -24
  433. mindspore/ops/_op_impl/aicpu/sparse_cross.py +42 -0
  434. mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows.py +63 -0
  435. mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows_grad.py +45 -0
  436. mindspore/ops/_op_impl/aicpu/sparse_matrix_mat_mul.py +56 -0
  437. mindspore/ops/_op_impl/{tbe/slice_ds.py → aicpu/sparse_segment_sum.py} +16 -24
  438. mindspore/ops/_op_impl/aicpu/sparse_segment_sum_with_num_segments.py +68 -0
  439. mindspore/ops/_op_impl/aicpu/sparse_slice.py +63 -0
  440. mindspore/ops/_op_impl/aicpu/sparse_slice_grad.py +61 -0
  441. mindspore/ops/_op_impl/aicpu/squared_difference.py +2 -0
  442. mindspore/ops/_op_impl/aicpu/strided_slice_v2.py +93 -0
  443. mindspore/ops/_op_impl/aicpu/strided_slice_v2_grad.py +66 -0
  444. mindspore/ops/_op_impl/aicpu/tensor_scatter_update.py +59 -0
  445. mindspore/ops/_op_impl/{tbe/gather_v2.py → aicpu/tile.py} +24 -24
  446. mindspore/ops/_op_impl/aicpu/tridiagonal_solve.py +35 -0
  447. mindspore/ops/_op_impl/aicpu/tril_indices.py +34 -0
  448. mindspore/ops/_op_impl/aicpu/triu_indices.py +34 -0
  449. mindspore/ops/_op_impl/aicpu/uniform.py +34 -0
  450. mindspore/ops/_op_impl/aicpu/uniform_candidate_sampler.py +1 -0
  451. mindspore/ops/_op_impl/aicpu/unique_consecutive.py +10 -2
  452. mindspore/ops/_op_impl/cpu/__init__.py +1 -2
  453. mindspore/ops/_op_impl/cpu/dynamic_shape.py +5 -1
  454. mindspore/ops/_op_impl/cpu/maximum_grad.py +2 -0
  455. mindspore/{compression/common/__init__.py → ops/_op_impl/cpu/pyexecute.py} +13 -8
  456. mindspore/ops/_op_impl/cpu/reduce_sum.py +8 -0
  457. mindspore/ops/_op_impl/cpu/sparse_slice.py +62 -0
  458. mindspore/ops/_op_impl/cpu/sparse_slice_grad.py +60 -0
  459. mindspore/ops/_op_impl/cpu/tensor_shape.py +5 -1
  460. mindspore/ops/_op_impl/tbe/__init__.py +27 -608
  461. mindspore/ops/_op_impl/tbe/addcdiv_ds.py +42 -0
  462. mindspore/ops/_op_impl/tbe/addcmul_ds.py +44 -0
  463. mindspore/ops/_op_impl/tbe/assign_add_ds.py +1 -0
  464. mindspore/ops/_op_impl/tbe/atomic_addr_clean.py +1 -1
  465. mindspore/ops/_op_impl/tbe/avg_pool_3d_grad.py +1 -1
  466. mindspore/ops/_op_impl/tbe/basic_lstm_cell_c_state_grad_v2.py +0 -1
  467. mindspore/ops/_op_impl/tbe/batch_to_space.py +1 -1
  468. mindspore/ops/_op_impl/tbe/batch_to_space_nd.py +1 -1
  469. mindspore/ops/_op_impl/tbe/batch_to_space_nd_v2.py +41 -0
  470. mindspore/ops/_op_impl/tbe/bce_with_logits_loss.py +1 -0
  471. mindspore/ops/_op_impl/tbe/bias_add_grad.py +2 -0
  472. mindspore/ops/_op_impl/tbe/bn_infer_grad.py +4 -2
  473. mindspore/ops/_op_impl/tbe/bn_infer_grad_ds.py +40 -0
  474. mindspore/ops/_op_impl/tbe/bn_training_update.py +0 -1
  475. mindspore/ops/_op_impl/tbe/bn_training_update_ds.py +0 -1
  476. mindspore/ops/_op_impl/tbe/broadcast_to_ds.py +6 -4
  477. mindspore/ops/_op_impl/tbe/cast.py +0 -2
  478. mindspore/ops/_op_impl/tbe/cast_ds.py +3 -3
  479. mindspore/ops/_op_impl/tbe/ctc_loss_v2.py +0 -2
  480. mindspore/ops/_op_impl/tbe/ctc_loss_v2_grad.py +0 -2
  481. mindspore/ops/_op_impl/tbe/data_format_dim_map_ds.py +1 -0
  482. mindspore/ops/_op_impl/tbe/deformable_offsets.py +1 -0
  483. mindspore/ops/_op_impl/tbe/depthwise_conv2d.py +1 -1
  484. mindspore/ops/_op_impl/tbe/dynamic_atomic_addr_clean.py +1 -1
  485. mindspore/ops/_op_impl/tbe/gather_nd.py +1 -0
  486. mindspore/ops/_op_impl/tbe/greater.py +2 -0
  487. mindspore/ops/_op_impl/tbe/{index_add.py → inplace_index_add.py} +3 -6
  488. mindspore/ops/_op_impl/tbe/layer_norm_beta_gamma_backprop_v2.py +0 -1
  489. mindspore/ops/_op_impl/tbe/npu_clear_float_status_v2.py +35 -0
  490. mindspore/ops/_op_impl/tbe/npu_get_float_status_v2.py +35 -0
  491. mindspore/ops/_op_impl/tbe/one_hot_ds.py +0 -6
  492. mindspore/ops/_op_impl/tbe/{greater_ds.py → reduce_all_ds.py} +13 -16
  493. mindspore/ops/_op_impl/tbe/reduce_any_ds.py +39 -0
  494. mindspore/ops/_op_impl/tbe/roi_align_ds.py +44 -0
  495. mindspore/ops/_op_impl/tbe/roi_align_grad_ds.py +44 -0
  496. mindspore/ops/_op_impl/tbe/scatter_add.py +2 -0
  497. mindspore/ops/_op_impl/tbe/scatter_nd_add.py +2 -2
  498. mindspore/ops/_op_impl/tbe/slice.py +26 -15
  499. mindspore/ops/_op_impl/tbe/space_to_batch.py +1 -1
  500. mindspore/ops/_op_impl/tbe/space_to_batch_nd.py +1 -1
  501. mindspore/ops/_op_impl/tbe/strided_slice_grad_d.py +1 -0
  502. mindspore/ops/_op_impl/tbe/trans_data_ds.py +15 -5
  503. mindspore/ops/_op_impl/tbe/unsorted_segment_sum.py +1 -1
  504. mindspore/ops/_op_impl/tbe/unsorted_segment_sum_ds.py +2 -0
  505. mindspore/ops/_primitive_cache.py +3 -2
  506. mindspore/ops/_register_for_op.py +11 -0
  507. mindspore/ops/_utils/__init__.py +1 -1
  508. mindspore/ops/_utils/utils.py +20 -41
  509. mindspore/ops/_vmap/__init__.py +2 -2
  510. mindspore/ops/_vmap/vmap_array_ops.py +170 -78
  511. mindspore/ops/_vmap/vmap_base.py +24 -10
  512. mindspore/ops/_vmap/vmap_convolution_ops.py +7 -10
  513. mindspore/ops/_vmap/vmap_grad_math_ops.py +4 -4
  514. mindspore/ops/_vmap/vmap_grad_nn_ops.py +41 -9
  515. mindspore/ops/_vmap/vmap_image_ops.py +52 -0
  516. mindspore/ops/_vmap/vmap_math_ops.py +77 -6
  517. mindspore/ops/_vmap/vmap_nn_ops.py +78 -29
  518. mindspore/ops/_vmap/vmap_other_ops.py +3 -1
  519. mindspore/ops/_vmap/vmap_random_ops.py +55 -3
  520. mindspore/ops/_vmap/vmap_sparse_ops.py +1 -0
  521. mindspore/ops/bprop_mindir/AdaptiveAvgPool2D_bprop.mindir +0 -0
  522. mindspore/ops/bprop_mindir/AdaptiveMaxPool2D_bprop.mindir +0 -0
  523. mindspore/ops/bprop_mindir/ApproximateEqual_bprop.mindir +18 -19
  524. mindspore/ops/bprop_mindir/Argmax_bprop.mindir +13 -12
  525. mindspore/ops/bprop_mindir/Argmin_bprop.mindir +14 -13
  526. mindspore/ops/bprop_mindir/AssignSub_bprop.mindir +17 -18
  527. mindspore/ops/bprop_mindir/Assign_bprop.mindir +16 -16
  528. mindspore/ops/bprop_mindir/AvgPool3D_bprop.mindir +150 -0
  529. mindspore/ops/bprop_mindir/AvgPool_bprop.mindir +66 -0
  530. mindspore/ops/bprop_mindir/BCEWithLogitsLoss_bprop.mindir +0 -0
  531. mindspore/ops/bprop_mindir/BNTrainingReduce_bprop.mindir +13 -12
  532. mindspore/ops/bprop_mindir/BatchNormGrad_bprop.mindir +0 -0
  533. mindspore/ops/bprop_mindir/BatchToSpaceND_bprop.mindir +28 -0
  534. mindspore/ops/bprop_mindir/BiasAddGrad_bprop.mindir +0 -0
  535. mindspore/ops/bprop_mindir/BinaryCrossEntropy_bprop.mindir +33 -0
  536. mindspore/ops/bprop_mindir/BroadcastTo_bprop.mindir +306 -0
  537. mindspore/ops/bprop_mindir/Broadcast_bprop.mindir +12 -8
  538. mindspore/ops/bprop_mindir/CTCLoss_bprop.mindir +0 -0
  539. mindspore/ops/bprop_mindir/Concat_bprop.mindir +0 -0
  540. mindspore/ops/bprop_mindir/Conv2DBackpropFilter_bprop.mindir +240 -0
  541. mindspore/ops/bprop_mindir/Conv2DBackpropInput_bprop.mindir +247 -0
  542. mindspore/ops/bprop_mindir/Conv2DTranspose_bprop.mindir +247 -0
  543. mindspore/ops/bprop_mindir/Conv3DTranspose_bprop.mindir +315 -0
  544. mindspore/ops/bprop_mindir/Conv3D_bprop.mindir +278 -0
  545. mindspore/ops/bprop_mindir/DType_bprop.mindir +12 -12
  546. mindspore/ops/bprop_mindir/DeformableOffsets_bprop.mindir +58 -0
  547. mindspore/ops/bprop_mindir/Depend_bprop.mindir +12 -13
  548. mindspore/ops/bprop_mindir/DepthToSpace_bprop.mindir +23 -0
  549. mindspore/ops/bprop_mindir/DepthwiseConv2dNative_bprop.mindir +138 -0
  550. mindspore/ops/bprop_mindir/DiagPart_bprop.mindir +15 -0
  551. mindspore/ops/bprop_mindir/Dropout2D_bprop.mindir +0 -0
  552. mindspore/ops/bprop_mindir/Dropout3D_bprop.mindir +0 -0
  553. mindspore/ops/bprop_mindir/DropoutDoMask_bprop.mindir +22 -24
  554. mindspore/ops/bprop_mindir/DropoutGenMask_bprop.mindir +16 -14
  555. mindspore/ops/bprop_mindir/DropoutGrad_bprop.mindir +27 -0
  556. mindspore/ops/bprop_mindir/Dropout_bprop.mindir +0 -0
  557. mindspore/ops/bprop_mindir/DynamicGRUV2_bprop.mindir +0 -0
  558. mindspore/ops/bprop_mindir/DynamicRNN_bprop.mindir +0 -0
  559. mindspore/ops/bprop_mindir/DynamicShape_bprop.mindir +12 -12
  560. mindspore/ops/bprop_mindir/Elu_bprop.mindir +16 -0
  561. mindspore/ops/bprop_mindir/EmbeddingLookup_bprop.mindir +0 -0
  562. mindspore/ops/bprop_mindir/Equal_bprop.mindir +18 -19
  563. mindspore/ops/bprop_mindir/ExpandDims_bprop.mindir +58 -0
  564. mindspore/ops/bprop_mindir/FastGeLU_bprop.mindir +16 -0
  565. mindspore/ops/bprop_mindir/Flatten_bprop.mindir +54 -0
  566. mindspore/ops/bprop_mindir/FloorDiv_bprop.mindir +18 -15
  567. mindspore/ops/bprop_mindir/GatherD_bprop.mindir +26 -0
  568. mindspore/ops/bprop_mindir/GatherNd_bprop.mindir +57 -0
  569. mindspore/ops/bprop_mindir/Gather_bprop.mindir +0 -0
  570. mindspore/ops/bprop_mindir/GreaterEqual_bprop.mindir +17 -18
  571. mindspore/ops/bprop_mindir/Greater_bprop.mindir +18 -19
  572. mindspore/ops/bprop_mindir/HSigmoid_bprop.mindir +16 -0
  573. mindspore/ops/bprop_mindir/HSwish_bprop.mindir +16 -0
  574. mindspore/ops/bprop_mindir/IOU_bprop.mindir +18 -19
  575. mindspore/ops/bprop_mindir/InstanceNorm_bprop.mindir +0 -0
  576. mindspore/ops/bprop_mindir/IsFinite_bprop.mindir +13 -12
  577. mindspore/ops/bprop_mindir/IsInf_bprop.mindir +13 -10
  578. mindspore/ops/bprop_mindir/IsNan_bprop.mindir +14 -11
  579. mindspore/ops/bprop_mindir/KLDivLoss_bprop.mindir +126 -0
  580. mindspore/ops/bprop_mindir/L2Loss_bprop.mindir +15 -0
  581. mindspore/ops/bprop_mindir/L2Normalize_bprop.mindir +30 -0
  582. mindspore/ops/bprop_mindir/LRN_bprop.mindir +43 -0
  583. mindspore/ops/bprop_mindir/LayerNormGrad_bprop.mindir +0 -0
  584. mindspore/ops/bprop_mindir/LessEqual_bprop.mindir +18 -19
  585. mindspore/ops/bprop_mindir/Less_bprop.mindir +17 -18
  586. mindspore/ops/bprop_mindir/LinSpace_bprop.mindir +22 -19
  587. mindspore/ops/bprop_mindir/Load_bprop.mindir +12 -13
  588. mindspore/ops/bprop_mindir/LogSoftmax_bprop.mindir +23 -0
  589. mindspore/ops/bprop_mindir/LogicalAnd_bprop.mindir +17 -18
  590. mindspore/ops/bprop_mindir/LogicalNot_bprop.mindir +14 -13
  591. mindspore/ops/bprop_mindir/MaskedSelect_bprop.mindir +21 -0
  592. mindspore/ops/bprop_mindir/MaxPool3DGradGrad_bprop.mindir +74 -0
  593. mindspore/ops/bprop_mindir/MaxPool3DGrad_bprop.mindir +74 -0
  594. mindspore/ops/bprop_mindir/MaxPool3D_bprop.mindir +75 -0
  595. mindspore/ops/bprop_mindir/MaxPoolGradGrad_bprop.mindir +65 -0
  596. mindspore/ops/bprop_mindir/MaxPoolWithArgmax_bprop.mindir +0 -0
  597. mindspore/ops/bprop_mindir/Maximum_bprop.mindir +0 -0
  598. mindspore/ops/bprop_mindir/Minimum_bprop.mindir +0 -0
  599. mindspore/ops/bprop_mindir/MirrorPad_bprop.mindir +27 -0
  600. mindspore/ops/bprop_mindir/Mish_bprop.mindir +35 -0
  601. mindspore/ops/bprop_mindir/MulNoNan_bprop.mindir +0 -0
  602. mindspore/ops/bprop_mindir/NLLLoss_bprop.mindir +0 -0
  603. mindspore/ops/bprop_mindir/NonZero_bprop.mindir +14 -0
  604. mindspore/ops/bprop_mindir/NotEqual_bprop.mindir +18 -19
  605. mindspore/ops/bprop_mindir/OneHot_bprop.mindir +25 -23
  606. mindspore/ops/bprop_mindir/OnesLike_bprop.mindir +13 -13
  607. mindspore/ops/bprop_mindir/PReLU_bprop.mindir +0 -0
  608. mindspore/ops/bprop_mindir/Pad_bprop.mindir +0 -0
  609. mindspore/ops/bprop_mindir/Padding_bprop.mindir +0 -0
  610. mindspore/ops/bprop_mindir/RNNTLoss_bprop.mindir +29 -0
  611. mindspore/ops/bprop_mindir/ROIAlign_bprop.mindir +82 -0
  612. mindspore/ops/bprop_mindir/Range_bprop.mindir +21 -19
  613. mindspore/ops/bprop_mindir/Rank_bprop.mindir +11 -11
  614. mindspore/ops/bprop_mindir/ReLU6_bprop.mindir +16 -0
  615. mindspore/ops/bprop_mindir/ReLUV2_bprop.mindir +0 -0
  616. mindspore/ops/bprop_mindir/ReduceAll_bprop.mindir +18 -17
  617. mindspore/ops/bprop_mindir/ReduceAny_bprop.mindir +18 -17
  618. mindspore/ops/bprop_mindir/ReluGrad_bprop.mindir +19 -23
  619. mindspore/ops/bprop_mindir/Reshape_bprop.mindir +60 -0
  620. mindspore/ops/bprop_mindir/ResizeBilinear_bprop.mindir +29 -0
  621. mindspore/ops/bprop_mindir/ResizeNearestNeighbor_bprop.mindir +89 -0
  622. mindspore/ops/bprop_mindir/ReverseSequence_bprop.mindir +52 -0
  623. mindspore/ops/bprop_mindir/ReverseV2_bprop.mindir +22 -0
  624. mindspore/ops/bprop_mindir/Round_bprop.mindir +14 -13
  625. mindspore/ops/bprop_mindir/ScatterMax_bprop.mindir +0 -0
  626. mindspore/ops/bprop_mindir/ScatterMin_bprop.mindir +0 -0
  627. mindspore/ops/bprop_mindir/ScatterNdUpdate_bprop.mindir +22 -0
  628. mindspore/ops/bprop_mindir/ScatterNd_bprop.mindir +24 -0
  629. mindspore/ops/bprop_mindir/ScatterNonAliasingAdd_bprop.mindir +22 -0
  630. mindspore/ops/bprop_mindir/ScatterUpdate_bprop.mindir +0 -0
  631. mindspore/ops/bprop_mindir/SeLU_bprop.mindir +21 -0
  632. mindspore/ops/bprop_mindir/Select_bprop.mindir +30 -34
  633. mindspore/ops/bprop_mindir/Shape_bprop.mindir +12 -12
  634. mindspore/ops/bprop_mindir/SigmoidCrossEntropyWithLogits_bprop.mindir +21 -0
  635. mindspore/ops/bprop_mindir/SigmoidGrad_bprop.mindir +0 -0
  636. mindspore/ops/bprop_mindir/Sigmoid_bprop.mindir +16 -0
  637. mindspore/ops/bprop_mindir/Sign_bprop.mindir +13 -12
  638. mindspore/ops/bprop_mindir/Slice_bprop.mindir +26 -0
  639. mindspore/ops/bprop_mindir/SmoothL1Loss_bprop.mindir +36 -0
  640. mindspore/ops/bprop_mindir/SoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
  641. mindspore/ops/bprop_mindir/Softplus_bprop.mindir +16 -0
  642. mindspore/ops/bprop_mindir/Softsign_bprop.mindir +33 -0
  643. mindspore/ops/bprop_mindir/Sort_bprop.mindir +0 -0
  644. mindspore/ops/bprop_mindir/SpaceToBatchND_bprop.mindir +28 -0
  645. mindspore/ops/bprop_mindir/SpaceToDepth_bprop.mindir +23 -0
  646. mindspore/ops/bprop_mindir/SparseGatherV2_bprop.mindir +0 -0
  647. mindspore/ops/bprop_mindir/SparseSoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
  648. mindspore/ops/bprop_mindir/Split_bprop.mindir +22 -0
  649. mindspore/ops/bprop_mindir/Squeeze_bprop.mindir +54 -0
  650. mindspore/ops/bprop_mindir/StridedSliceGrad_bprop.mindir +95 -0
  651. mindspore/ops/bprop_mindir/StridedSlice_bprop.mindir +98 -0
  652. mindspore/ops/bprop_mindir/Switch_bprop.mindir +28 -32
  653. mindspore/ops/bprop_mindir/TanhGrad_bprop.mindir +0 -0
  654. mindspore/ops/bprop_mindir/Tanh_bprop.mindir +66 -0
  655. mindspore/ops/bprop_mindir/TensorScatterAdd_bprop.mindir +22 -0
  656. mindspore/ops/bprop_mindir/TensorScatterUpdate_bprop.mindir +29 -0
  657. mindspore/ops/bprop_mindir/TensorShape_bprop.mindir +14 -0
  658. mindspore/ops/bprop_mindir/Tile_bprop.mindir +0 -0
  659. mindspore/ops/bprop_mindir/TopK_bprop.mindir +0 -0
  660. mindspore/ops/bprop_mindir/TransShape_bprop.mindir +23 -0
  661. mindspore/ops/bprop_mindir/TruncateDiv_bprop.mindir +18 -15
  662. mindspore/ops/bprop_mindir/TupleGetItem_bprop.mindir +11 -13
  663. mindspore/ops/bprop_mindir/Unique_bprop.mindir +16 -0
  664. mindspore/ops/bprop_mindir/Unstack_bprop.mindir +22 -0
  665. mindspore/ops/bprop_mindir/UpsampleNearest3D_bprop.mindir +32 -0
  666. mindspore/ops/bprop_mindir/UpsampleTrilinear3D_bprop.mindir +38 -0
  667. mindspore/ops/bprop_mindir/ZerosLike_bprop.mindir +13 -12
  668. mindspore/ops/bprop_mindir/__init__.py +1 -4
  669. mindspore/ops/bprop_mindir/generate_mindir.py +32 -20
  670. mindspore/ops/composite/__init__.py +12 -13
  671. mindspore/ops/composite/base.py +261 -254
  672. mindspore/ops/composite/env_ops.py +41 -0
  673. mindspore/ops/composite/math_ops.py +197 -156
  674. mindspore/ops/composite/multitype_ops/_compile_utils.py +428 -176
  675. mindspore/ops/composite/multitype_ops/_constexpr_utils.py +188 -87
  676. mindspore/ops/composite/multitype_ops/add_impl.py +23 -1
  677. mindspore/ops/composite/multitype_ops/div_impl.py +3 -3
  678. mindspore/ops/composite/multitype_ops/equal_impl.py +1 -0
  679. mindspore/ops/composite/multitype_ops/floordiv_impl.py +1 -1
  680. mindspore/ops/composite/multitype_ops/getitem_impl.py +52 -5
  681. mindspore/ops/composite/multitype_ops/greater_equal_impl.py +31 -0
  682. mindspore/ops/composite/multitype_ops/greater_impl.py +31 -0
  683. mindspore/ops/composite/multitype_ops/in_impl.py +15 -3
  684. mindspore/ops/composite/multitype_ops/less_equal_impl.py +33 -2
  685. mindspore/ops/composite/multitype_ops/less_impl.py +33 -0
  686. mindspore/ops/composite/multitype_ops/logical_and_impl.py +2 -2
  687. mindspore/ops/composite/multitype_ops/logical_or_impl.py +2 -1
  688. mindspore/ops/composite/multitype_ops/mod_impl.py +1 -1
  689. mindspore/ops/composite/multitype_ops/mul_impl.py +21 -7
  690. mindspore/ops/composite/multitype_ops/not_in_impl.py +15 -3
  691. mindspore/ops/composite/multitype_ops/ones_like_impl.py +2 -4
  692. mindspore/ops/composite/multitype_ops/pow_impl.py +1 -0
  693. mindspore/ops/composite/multitype_ops/setitem_impl.py +62 -70
  694. mindspore/ops/composite/multitype_ops/sub_impl.py +3 -3
  695. mindspore/ops/composite/multitype_ops/zeros_like_impl.py +41 -4
  696. mindspore/ops/function/__init__.py +323 -8
  697. mindspore/ops/function/array_func.py +3511 -780
  698. mindspore/ops/function/clip_func.py +329 -0
  699. mindspore/ops/function/debug_func.py +6 -6
  700. mindspore/ops/function/grad/__init__.py +5 -1
  701. mindspore/ops/function/grad/grad_func.py +736 -65
  702. mindspore/ops/function/image_func.py +270 -0
  703. mindspore/ops/function/linalg_func.py +268 -8
  704. mindspore/ops/function/math_func.py +8032 -3164
  705. mindspore/ops/function/nn_func.py +5619 -1855
  706. mindspore/ops/function/other_func.py +115 -0
  707. mindspore/ops/function/parameter_func.py +11 -10
  708. mindspore/ops/function/random_func.py +939 -77
  709. mindspore/ops/function/sparse_func.py +249 -84
  710. mindspore/ops/function/sparse_unary_func.py +2303 -0
  711. mindspore/ops/function/spectral_func.py +146 -0
  712. mindspore/ops/function/vmap_func.py +114 -0
  713. mindspore/ops/functional.py +182 -254
  714. mindspore/ops/op_info_register.py +79 -34
  715. mindspore/ops/operations/__init__.py +210 -118
  716. mindspore/ops/operations/_csr_ops.py +7 -7
  717. mindspore/ops/operations/_embedding_cache_ops.py +25 -15
  718. mindspore/ops/operations/_grad_ops.py +447 -322
  719. mindspore/ops/operations/_inner_ops.py +547 -176
  720. mindspore/ops/operations/_map_tensor_ops.py +112 -0
  721. mindspore/ops/operations/_ms_kernel.py +29 -27
  722. mindspore/ops/operations/_ocr_ops.py +11 -11
  723. mindspore/ops/operations/_opaque_predicate_registry.py +41 -0
  724. mindspore/ops/operations/_quant_ops.py +186 -101
  725. mindspore/ops/operations/_rl_inner_ops.py +122 -61
  726. mindspore/ops/operations/_scalar_ops.py +466 -0
  727. mindspore/ops/operations/_sequence_ops.py +1047 -0
  728. mindspore/ops/operations/_tensor_array.py +10 -11
  729. mindspore/ops/operations/_thor_ops.py +4 -4
  730. mindspore/ops/operations/array_ops.py +1428 -1226
  731. mindspore/ops/operations/comm_ops.py +180 -117
  732. mindspore/ops/operations/control_ops.py +4 -2
  733. mindspore/ops/operations/custom_ops.py +185 -98
  734. mindspore/ops/operations/debug_ops.py +92 -54
  735. mindspore/ops/operations/image_ops.py +406 -211
  736. mindspore/ops/operations/inner_ops.py +42 -53
  737. mindspore/ops/operations/linalg_ops.py +32 -29
  738. mindspore/ops/operations/math_ops.py +2076 -897
  739. mindspore/ops/operations/nn_ops.py +1282 -1252
  740. mindspore/ops/operations/other_ops.py +124 -278
  741. mindspore/ops/operations/random_ops.py +345 -178
  742. mindspore/ops/operations/rl_ops.py +8 -9
  743. mindspore/ops/operations/sparse_ops.py +502 -157
  744. mindspore/ops/operations/spectral_ops.py +107 -0
  745. mindspore/ops/primitive.py +192 -15
  746. mindspore/ops/vm_impl_registry.py +23 -2
  747. mindspore/parallel/__init__.py +6 -1
  748. mindspore/parallel/_auto_parallel_context.py +199 -92
  749. mindspore/parallel/_cell_wrapper.py +4 -2
  750. mindspore/parallel/_cost_model_context.py +3 -0
  751. mindspore/parallel/_dp_allreduce_fusion.py +2 -1
  752. mindspore/parallel/_offload_context.py +185 -0
  753. mindspore/parallel/_parallel_serialization.py +167 -28
  754. mindspore/parallel/_ps_context.py +9 -5
  755. mindspore/parallel/_recovery_context.py +1 -1
  756. mindspore/parallel/_tensor.py +9 -1
  757. mindspore/{nn/transformer → parallel/_transformer}/__init__.py +6 -6
  758. mindspore/{nn/transformer → parallel/_transformer}/layers.py +59 -37
  759. mindspore/{nn/transformer → parallel/_transformer}/loss.py +4 -7
  760. mindspore/{nn/transformer → parallel/_transformer}/moe.py +160 -35
  761. mindspore/{nn/transformer → parallel/_transformer}/op_parallel_config.py +3 -3
  762. mindspore/{nn/transformer → parallel/_transformer}/transformer.py +235 -196
  763. mindspore/parallel/_utils.py +47 -7
  764. mindspore/parallel/algo_parameter_config.py +5 -1
  765. mindspore/parallel/checkpoint_transform.py +329 -0
  766. mindspore/parallel/shard.py +229 -0
  767. mindspore/profiler/__init__.py +2 -1
  768. mindspore/profiler/common/util.py +4 -3
  769. mindspore/profiler/common/validator/validate_path.py +2 -2
  770. mindspore/profiler/envprofiling.py +249 -0
  771. mindspore/profiler/parser/aicpu_data_parser.py +38 -39
  772. mindspore/profiler/parser/ascend_timeline_generator.py +497 -0
  773. mindspore/profiler/parser/base_timeline_generator.py +471 -0
  774. mindspore/profiler/parser/cpu_gpu_timeline_generator.py +684 -0
  775. mindspore/profiler/parser/framework_parser.py +42 -16
  776. mindspore/profiler/parser/hccl_parser.py +158 -158
  777. mindspore/profiler/parser/hwts_log_parser.py +7 -6
  778. mindspore/profiler/parser/integrator.py +18 -1579
  779. mindspore/profiler/parser/minddata_analyzer.py +8 -8
  780. mindspore/profiler/parser/msadvisor_analyzer.py +14 -27
  781. mindspore/profiler/parser/msadvisor_parser.py +2 -4
  782. mindspore/profiler/parser/optime_parser.py +17 -18
  783. mindspore/profiler/parser/profiler_info.py +108 -0
  784. mindspore/profiler/parser/step_trace_parser.py +1 -1
  785. mindspore/profiler/profiling.py +396 -194
  786. mindspore/rewrite/__init__.py +6 -2
  787. mindspore/rewrite/api/node.py +51 -110
  788. mindspore/rewrite/api/node_type.py +10 -6
  789. mindspore/rewrite/api/pattern_engine.py +51 -7
  790. mindspore/rewrite/api/scoped_value.py +64 -53
  791. mindspore/rewrite/api/symbol_tree.py +108 -61
  792. mindspore/rewrite/api/tree_node_helper.py +2 -3
  793. mindspore/{compression/quant/__init__.py → rewrite/ast_creator_register.py} +20 -11
  794. mindspore/rewrite/ast_helpers/__init__.py +6 -3
  795. mindspore/rewrite/ast_helpers/ast_creator.py +115 -0
  796. mindspore/rewrite/ast_helpers/ast_finder.py +99 -1
  797. mindspore/rewrite/ast_helpers/ast_modifier.py +17 -4
  798. mindspore/rewrite/ast_helpers/ast_replacer.py +1 -1
  799. mindspore/rewrite/ast_transformers/__init__.py +0 -1
  800. mindspore/rewrite/ast_transformers/flatten_recursive_stmt.py +46 -5
  801. mindspore/rewrite/ast_transformers/remove_return_out_of_if.py +6 -3
  802. mindspore/rewrite/common/__init__.py +2 -0
  803. mindspore/rewrite/common/event.py +1 -1
  804. mindspore/rewrite/common/observable.py +1 -1
  805. mindspore/rewrite/common/observer.py +1 -1
  806. mindspore/rewrite/common/rewrite_elog.py +35 -0
  807. mindspore/rewrite/namer.py +2 -2
  808. mindspore/rewrite/namespace.py +14 -4
  809. mindspore/rewrite/node.py +161 -13
  810. mindspore/rewrite/parser.py +0 -1
  811. mindspore/rewrite/parser_register.py +0 -1
  812. mindspore/rewrite/parsers/arguments_parser.py +3 -2
  813. mindspore/rewrite/parsers/assign_parser.py +267 -67
  814. mindspore/rewrite/parsers/attribute_parser.py +56 -0
  815. mindspore/rewrite/parsers/class_def_parser.py +191 -108
  816. mindspore/rewrite/parsers/constant_parser.py +101 -0
  817. mindspore/rewrite/parsers/container_parser.py +88 -0
  818. mindspore/rewrite/parsers/for_parser.py +28 -15
  819. mindspore/rewrite/parsers/function_def_parser.py +21 -5
  820. mindspore/rewrite/parsers/if_parser.py +11 -28
  821. mindspore/rewrite/parsers/module_parser.py +9 -6
  822. mindspore/rewrite/parsers/return_parser.py +3 -2
  823. mindspore/rewrite/sparsify/__init__.py +0 -0
  824. mindspore/rewrite/sparsify/sparse_transformer.py +448 -0
  825. mindspore/rewrite/sparsify/sparsify.py +109 -0
  826. mindspore/rewrite/sparsify/utils.py +173 -0
  827. mindspore/rewrite/symbol_tree.py +322 -109
  828. mindspore/rewrite/symbol_tree_builder.py +45 -8
  829. mindspore/rewrite/symbol_tree_dumper.py +0 -1
  830. mindspore/rewrite/topological_manager.py +1 -2
  831. mindspore/run_check/_check_version.py +209 -112
  832. mindspore/run_check/run_check.py +2 -1
  833. mindspore/scipy/linalg.py +13 -117
  834. mindspore/scipy/ops.py +5 -71
  835. mindspore/scipy/ops_grad.py +1 -25
  836. mindspore/scipy/ops_wrapper.py +1 -1
  837. mindspore/scipy/optimize/_bfgs.py +1 -1
  838. mindspore/scipy/optimize/_lagrange.py +200 -0
  839. mindspore/scipy/optimize/line_search.py +3 -2
  840. mindspore/scipy/optimize/minimize.py +43 -6
  841. mindspore/scipy/sparse/__init__.py +2 -2
  842. mindspore/scipy/sparse/linalg.py +5 -465
  843. mindspore/scipy/utils.py +2 -1
  844. mindspore/scipy/utils_const.py +7 -1
  845. mindspore/train/__init__.py +6 -4
  846. mindspore/train/_utils.py +28 -5
  847. mindspore/train/amp.py +321 -50
  848. mindspore/train/callback/__init__.py +3 -1
  849. mindspore/train/callback/_backup_and_restore.py +120 -0
  850. mindspore/train/callback/_callback.py +8 -8
  851. mindspore/train/callback/_checkpoint.py +12 -9
  852. mindspore/train/callback/_early_stop.py +13 -7
  853. mindspore/train/callback/_history.py +8 -8
  854. mindspore/train/callback/_lambda_callback.py +6 -6
  855. mindspore/train/callback/_landscape.py +36 -38
  856. mindspore/train/callback/_loss_monitor.py +12 -6
  857. mindspore/train/callback/_lr_scheduler_callback.py +2 -4
  858. mindspore/train/callback/_on_request_exit.py +212 -0
  859. mindspore/train/callback/_reduce_lr_on_plateau.py +13 -7
  860. mindspore/train/callback/_summary_collector.py +27 -19
  861. mindspore/train/callback/_time_monitor.py +13 -7
  862. mindspore/train/checkpoint_pb2.py +68 -8
  863. mindspore/train/data_sink.py +122 -33
  864. mindspore/train/dataset_helper.py +28 -87
  865. mindspore/train/loss_scale_manager.py +4 -7
  866. mindspore/{nn → train}/metrics/__init__.py +20 -20
  867. mindspore/{nn → train}/metrics/accuracy.py +12 -10
  868. mindspore/{nn → train}/metrics/auc.py +4 -4
  869. mindspore/{nn → train}/metrics/bleu_score.py +4 -4
  870. mindspore/{nn → train}/metrics/confusion_matrix.py +10 -8
  871. mindspore/{nn → train}/metrics/cosine_similarity.py +4 -4
  872. mindspore/{nn → train}/metrics/dice.py +6 -5
  873. mindspore/{nn → train}/metrics/error.py +7 -5
  874. mindspore/{nn → train}/metrics/fbeta.py +9 -7
  875. mindspore/{nn → train}/metrics/hausdorff_distance.py +8 -6
  876. mindspore/{nn → train}/metrics/loss.py +4 -3
  877. mindspore/{nn → train}/metrics/mean_surface_distance.py +6 -5
  878. mindspore/{nn → train}/metrics/metric.py +6 -5
  879. mindspore/{nn → train}/metrics/occlusion_sensitivity.py +4 -3
  880. mindspore/{nn → train}/metrics/perplexity.py +5 -4
  881. mindspore/{nn → train}/metrics/precision.py +5 -4
  882. mindspore/{nn → train}/metrics/recall.py +5 -4
  883. mindspore/{nn → train}/metrics/roc.py +7 -6
  884. mindspore/{nn → train}/metrics/root_mean_square_surface_distance.py +6 -5
  885. mindspore/{nn → train}/metrics/topk.py +7 -5
  886. mindspore/train/mind_ir_pb2.py +339 -32
  887. mindspore/train/model.py +113 -84
  888. mindspore/train/serialization.py +547 -167
  889. mindspore/train/summary/_summary_adapter.py +1 -1
  890. mindspore/train/summary/summary_record.py +43 -12
  891. mindspore/train/train_thor/convert_utils.py +7 -1
  892. mindspore/train/train_thor/dataset_helper.py +3 -3
  893. mindspore/train/train_thor/model_thor.py +0 -4
  894. mindspore/version.py +1 -1
  895. {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/METADATA +4 -3
  896. {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/RECORD +899 -675
  897. mindspore/compression/common/constant.py +0 -124
  898. mindspore/compression/export/__init__.py +0 -19
  899. mindspore/compression/export/quant_export.py +0 -514
  900. mindspore/compression/quant/qat.py +0 -636
  901. mindspore/compression/quant/quant_utils.py +0 -462
  902. mindspore/compression/quant/quantizer.py +0 -68
  903. mindspore/nn/layer/quant.py +0 -1868
  904. mindspore/nn/layer/rnn_utils.py +0 -90
  905. mindspore/nn/probability/dpn/__init__.py +0 -22
  906. mindspore/nn/probability/dpn/vae/__init__.py +0 -25
  907. mindspore/nn/probability/dpn/vae/cvae.py +0 -138
  908. mindspore/nn/probability/dpn/vae/vae.py +0 -122
  909. mindspore/nn/probability/infer/__init__.py +0 -22
  910. mindspore/nn/probability/infer/variational/elbo.py +0 -70
  911. mindspore/nn/probability/infer/variational/svi.py +0 -84
  912. mindspore/nn/probability/toolbox/__init__.py +0 -22
  913. mindspore/nn/probability/toolbox/anomaly_detection.py +0 -99
  914. mindspore/nn/probability/toolbox/uncertainty_evaluation.py +0 -363
  915. mindspore/nn/probability/transforms/__init__.py +0 -22
  916. mindspore/nn/probability/transforms/transform_bnn.py +0 -262
  917. mindspore/nn/probability/zhusuan/__init__.py +0 -18
  918. mindspore/nn/probability/zhusuan/framework/__init__.py +0 -18
  919. mindspore/nn/probability/zhusuan/framework/bn.py +0 -95
  920. mindspore/nn/probability/zhusuan/variational/__init__.py +0 -18
  921. mindspore/nn/probability/zhusuan/variational/elbo.py +0 -46
  922. mindspore/ops/_op_impl/tbe/bias_add_grad_ds.py +0 -52
  923. mindspore/ops/_op_impl/tbe/scatter_nd_add_ds.py +0 -43
  924. mindspore/ops/bprop_mindir/AssignAdd_bprop.mindir +0 -20
  925. mindspore/ops/bprop_mindir/Identity_bprop.mindir +0 -9
  926. mindspore/ops/bprop_mindir/LogicalOr_bprop.mindir +0 -20
  927. mindspore/ops/bprop_mindir/ReLU_bprop.mindir +0 -16
  928. mindspore/ops/bprop_mindir/UpdateState_bprop.mindir +0 -17
  929. mindspore/ops/bprop_mindir/stop_gradient_bprop.mindir +0 -12
  930. mindspore/ops/composite/array_ops.py +0 -210
  931. mindspore/ops/composite/clip_ops.py +0 -238
  932. mindspore/ops/composite/random_ops.py +0 -426
  933. mindspore/ops/composite/vmap_ops.py +0 -38
  934. mindspore/ops/operations/sponge_ops.py +0 -3531
  935. mindspore/ops/operations/sponge_update_ops.py +0 -2546
  936. mindspore/parallel/nn/__init__.py +0 -42
  937. mindspore/parallel/nn/loss.py +0 -22
  938. mindspore/parallel/nn/moe.py +0 -21
  939. mindspore/parallel/nn/op_parallel_config.py +0 -22
  940. mindspore/parallel/nn/transformer.py +0 -31
  941. mindspore/run_check/_check_deps_version.py +0 -84
  942. {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/WHEEL +0 -0
  943. {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/entry_points.txt +0 -0
  944. {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/top_level.txt +0 -0
mindspore/nn/loss/loss.py CHANGED
@@ -13,24 +13,25 @@
13
13
  # limitations under the License.
14
14
  # ============================================================================
15
15
  """loss"""
16
- from __future__ import absolute_import
16
+ from __future__ import absolute_import, division
17
+ import math
17
18
 
18
19
  import mindspore
19
20
  import mindspore.common.dtype as mstype
21
+ import mindspore.ops as ops
20
22
  from mindspore import log
21
23
  from mindspore.common.tensor import Tensor
22
24
  from mindspore.common.parameter import Parameter
23
25
  from mindspore.ops import operations as P
26
+ from mindspore.ops.operations import _inner_ops as inner
24
27
  from mindspore.ops.operations.nn_ops import MultiMarginLoss as MultiMarginLossOp
25
28
  from mindspore.ops.operations.nn_ops import MultilabelMarginLoss as MultilabelMarginLossOp
26
- from mindspore.ops.operations.nn_ops import TripletMarginLoss as TripletMarginLossOp
27
29
  from mindspore.ops import functional as F
28
30
  from mindspore import nn
29
- from mindspore.ops.primitive import constexpr
31
+ from mindspore.ops.primitive import constexpr, _primexpr
30
32
  from mindspore.nn.cell import Cell
31
33
  from mindspore.nn.layer.activation import get_activation
32
- from mindspore._checkparam import Validator as validator
33
- from mindspore._checkparam import Rel
34
+ from mindspore import _checkparam as validator
34
35
  from mindspore import context
35
36
 
36
37
 
@@ -245,13 +246,10 @@ class L1Loss(LossBase):
245
246
  def __init__(self, reduction='mean'):
246
247
  """Initialize L1Loss."""
247
248
  super(L1Loss, self).__init__(reduction)
248
- self.abs = P.Abs()
249
+ self.reduction = reduction
249
250
 
250
251
  def construct(self, logits, labels):
251
- _check_is_tensor('logits', logits, self.cls_name)
252
- _check_is_tensor('labels', labels, self.cls_name)
253
- x = self.abs(logits - labels)
254
- return self.get_loss(x)
252
+ return F.l1_loss(logits, labels, self.reduction)
255
253
 
256
254
 
257
255
  class MSELoss(LossBase):
@@ -457,6 +455,79 @@ class MAELoss(LossBase):
457
455
  return self.get_loss(x)
458
456
 
459
457
 
458
+ class MarginRankingLoss(LossBase):
459
+ r"""
460
+ MarginRankingLoss creates a criterion that measures the loss.
461
+
462
+ Given two tensors :math:`input1`, :math:`input2` and a Tensor label :math:`target` with values 1 or -1,
463
+ the operation is as follows:
464
+
465
+ .. math::
466
+ \text{loss}(input1, input2, target) = \max(0, -target * (input1 - input2) + \text{margin})
467
+
468
+ Args:
469
+ margin (float, optional): Specify the adjustment factor of the operation. Default 0.0.
470
+ reduction (str, optional): Specifies which reduction to be applied to the output. It must be one of "none",
471
+ "mean", and "sum", meaning no reduction, reduce mean and sum on output, respectively. Default: "mean".
472
+
473
+ Inputs:
474
+ - **input1** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number
475
+ of additional dimensions.
476
+ - **input2** (Tensor) - Tensor of shape :math:`(N, *)`, same shape and dtype as `input1`.
477
+ - **target** (Tensor) - Contains value 1 or -1. Suppose the shape of `input1` is
478
+ :math:`(x_1, x_2, x_3, ..., x_R)`, then the shape of `target` must be :math:`(x_1, x_2, x_3, ..., x_R)`.
479
+
480
+ Outputs:
481
+ Tensor or Scalar. if `reduction` is "none", its shape is the same as `labels`.
482
+ Otherwise, a scalar value will be returned.
483
+
484
+ Raises:
485
+ TypeError: If `margin` is not a float.
486
+ TypeError: If `input1`, `input2` or `target` is not a Tensor.
487
+ TypeError: If the types of `input1` and `input2` are inconsistent.
488
+ TypeError: If the types of `input1` and `target` are inconsistent.
489
+ ValueError: If the shape of `input1` and `input2` are inconsistent.
490
+ ValueError: If the shape of `input1` and `target` are inconsistent.
491
+ ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
492
+
493
+ Supported Platforms:
494
+ ``Ascend`` ``GPU`` ``CPU``
495
+
496
+ Examples:
497
+ >>> import mindspore as ms
498
+ >>> import mindspore.nn as nn
499
+ >>> import mindspore.ops as ops
500
+ >>> from mindspore.ops import Tensor
501
+ >>> import numpy as np
502
+ >>> loss1 = nn.MarginRankingLoss(reduction='none')
503
+ >>> loss2 = nn.MarginRankingLoss(reduction='mean')
504
+ >>> loss3 = nn.MarginRankingLoss(reduction='sum')
505
+ >>> sign = ops.Sign()
506
+ >>> input1 = Tensor(np.array([0.3864, -2.4093, -1.4076]), ms.float32)
507
+ >>> input2 = Tensor(np.array([-0.6012, -1.6681, 1.2928]), ms.float32)
508
+ >>> target = sign(Tensor(np.array([-2, -2, 3]), ms.float32))
509
+ >>> output1 = loss1(input1, input2, target)
510
+ >>> print(output1)
511
+ [0.98759997 0. 2.7003999 ]
512
+ >>> output2 = loss2(input1, input2, target)
513
+ >>> print(output2)
514
+ 1.2293333
515
+ >>> output3 = loss3(input1, input2, target)
516
+ >>> print(output3)
517
+ 3.6879997
518
+ """
519
+
520
+ def __init__(self, margin=0.0, reduction='mean'):
521
+ """Initialize MarginRankingLoss."""
522
+ super(MarginRankingLoss, self).__init__(reduction)
523
+ self.reduction = reduction
524
+ self.margin = margin
525
+
526
+ def construct(self, input1, input2, target):
527
+ x = ops.margin_ranking_loss(input1, input2, target, self.margin, self.reduction)
528
+ return x
529
+
530
+
460
531
  class SmoothL1Loss(LossBase):
461
532
  r"""
462
533
  SmoothL1 loss function, if the absolute error element-wise between the predicted value and the target value
@@ -502,7 +573,7 @@ class SmoothL1Loss(LossBase):
502
573
 
503
574
  Outputs:
504
575
  Tensor, if `reduction` is 'none', then output is a tensor with the same shape as `logits`.
505
- Otherwise the shape of output tensor is `(1,)`.
576
+ Otherwise the shape of output tensor is `()`.
506
577
 
507
578
  Raises:
508
579
  TypeError: If `beta` is not a float.
@@ -568,7 +639,7 @@ class SoftMarginLoss(LossBase):
568
639
  ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
569
640
 
570
641
  Supported Platforms:
571
- ``Ascend``
642
+ ``Ascend`` ``GPU``
572
643
 
573
644
  Examples:
574
645
  >>> loss = nn.SoftMarginLoss()
@@ -594,11 +665,11 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
594
665
  Measures the distribution error between the probabilities of the input (computed with softmax function) and the
595
666
  labels where the classes are mutually exclusive (only one class is positive) using cross entropy loss.
596
667
 
597
- Typical input into this function is unnormalized scores denoted as x whose shape is (N, C),
668
+ Typical input into this function is unnormalized scores denoted as x whose shape is :math:`(N, C)` ,
598
669
  and the corresponding targets.
599
670
 
600
671
  Typically, the input to this function is the fractional value of each category and the corresponding target value,
601
- and the input format is (N, C).
672
+ and the input format is :math:`(N, C)` .
602
673
 
603
674
  For each instance :math:`x_i`, i ranges from 0 to N-1, the loss is given as:
604
675
 
@@ -619,8 +690,8 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
619
690
  If "none", do not perform reduction. Default: "none".
620
691
 
621
692
  Inputs:
622
- - **logits** (Tensor) - Tensor of shape (N, C). Data type must be float16 or float32.
623
- - **labels** (Tensor) - Tensor of shape (N, ). If `sparse` is True, The type of
693
+ - **logits** (Tensor) - Tensor of shape :math:`(N, C)` . Data type must be float16 or float32.
694
+ - **labels** (Tensor) - Tensor of shape :math:`(N, )` . If `sparse` is True, The type of
624
695
  `labels` is int32 or int64. Otherwise, the type of `labels` is the same as the type of `logits`.
625
696
 
626
697
  Outputs:
@@ -703,9 +774,8 @@ class DiceLoss(LossBase):
703
774
  Default: 1e-5.
704
775
 
705
776
  Inputs:
706
- - **logits** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number of
707
- additional dimensions. The data type must be float16 or float32.
708
- - **labels** (Tensor) - Tensor of shape :math:`(N, *)`, same shape as the `logits`.
777
+ - **logits** (Tensor) - Input predicted value. The data type must be float16 or float32.
778
+ - **labels** (Tensor) - Input target value. Same shape as the `logits`.
709
779
  The data type must be float16 or float32.
710
780
 
711
781
  Outputs:
@@ -751,13 +821,13 @@ class DiceLoss(LossBase):
751
821
  return dice_loss
752
822
 
753
823
 
754
- @constexpr
824
+ @_primexpr
755
825
  def _check_shape(logits_shape, label_shape, prim_name=None):
756
826
  """Internal function, used to check whether the shape of logits and labels meets the requirements."""
757
827
  validator.check('logits_shape', logits_shape, 'label_shape', label_shape, prim_name=prim_name)
758
828
 
759
829
 
760
- @constexpr
830
+ @_primexpr
761
831
  def _check_ndim_multi(logits_dim, label_dim, prim_name=None):
762
832
  """Internal function, used to check whether the dimension of logits and label meets the requirements."""
763
833
  msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
@@ -767,7 +837,7 @@ def _check_ndim_multi(logits_dim, label_dim, prim_name=None):
767
837
  raise ValueError(f"{msg_prefix} 'labels' dimension must be greater than 1, but got {label_dim}.")
768
838
 
769
839
 
770
- @constexpr
840
+ @_primexpr
771
841
  def _check_weights(weight_shape, label_shape, prim_name=None):
772
842
  """Internal function, used to check whether the reduced shape meets the requirements."""
773
843
  msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
@@ -994,7 +1064,7 @@ class SampledSoftmaxLoss(LossBase):
994
1064
  def _softmax_cross_entropy(self, logits, targets):
995
1065
  stable_exp_logits = self.exp(logits - self.reduce_max_true(logits, 1))
996
1066
  pred = stable_exp_logits / self.reduce_sum_true(stable_exp_logits, 1)
997
- return -self.reduce_sum(targets * self.log(pred + 1.0e-20), 1)
1067
+ return -1 * self.reduce_sum(targets * self.log(pred + 1.0e-20), 1)
998
1068
 
999
1069
  def _compute_sampled_logits(self, weights,
1000
1070
  biases,
@@ -1089,6 +1159,156 @@ class SampledSoftmaxLoss(LossBase):
1089
1159
  return out_logits, out_labels
1090
1160
 
1091
1161
 
1162
+ class PoissonNLLLoss(LossBase):
1163
+ r"""
1164
+ Poisson negative log likelihood loss.
1165
+
1166
+ The loss is:
1167
+
1168
+ .. math::
1169
+ \mathcal{L}_{D} = \sum_{i = 0}^{|D|}\left( x_{i} - y_{i}\ln x_{i} + \ln{y_{i}!} \right)
1170
+
1171
+ where :math:`\mathcal{L}_{D}` is the loss, :math:`y_{i}` is the `target`,
1172
+ :math:`x_{i}` is the `input`.
1173
+
1174
+ If `log_input` is True, use :math:`e^{x_{i}} - y_{i} x_{i}` instead of :math:`x_{i} - y_{i}\ln x_{i}`.
1175
+ When calculating logarithms, the lower bound of `input` is set to `eps` to avoid numerical errors.
1176
+
1177
+ If `full` is False, the last term :math:`\ln{y_{i}!}` will be omitted,
1178
+ otherwise the last term will be approximated using Stirling formula:
1179
+
1180
+ .. math::
1181
+ n! \approx \sqrt{2\pi n}\left( \frac{n}{e} \right)^{n}
1182
+
1183
+ Note:
1184
+ Calculating the logarithm of a negative number or the exponent of a large positive number under Ascend
1185
+ will have a different range of return values and results different from those under GPU and CPU.
1186
+
1187
+ Args:
1188
+ log_input (bool, optional): Whether use log input. Default: True.
1189
+ full (bool, optional): Whether include the Stirling approximation term in the loss calculation. Default: False.
1190
+ eps (float, optional): Lower bound of `input` when calculating logarithms. Default: 1e-08.
1191
+ reduction (str, optional): Apply specific reduction method to the output:
1192
+ 'none', 'mean', 'sum'. Default: 'mean'.
1193
+
1194
+ Inputs:
1195
+ - **input** (Tensor) - The input Tensor. The shape can be any number of dimensions.
1196
+ - **target** (Tensor) - The label Tensor which has the same shape as `input`.
1197
+
1198
+ Outputs:
1199
+ Tensor or Scalar, if `reduction` is 'none', then output is a tensor and has the same shape as `input`.
1200
+ Otherwise it is a scalar.
1201
+
1202
+ Raises:
1203
+ TypeError: If `reduction` is not a str.
1204
+ TypeError: If neither `input` nor `target` is a tensor.
1205
+ TypeError: If dtype of `input` or `target` is not currently supported.
1206
+
1207
+ Supported Platforms:
1208
+ ``Ascend`` ``GPU`` ``CPU``
1209
+
1210
+ Examples:
1211
+ >>> x = Tensor([[0.3, 0.7], [0.5, 0.5]])
1212
+ >>> target = Tensor([[1.0, 2.0], [3.0, 4.0]])
1213
+ >>> loss = nn.PoissonNLLLoss()
1214
+ >>> output = loss(x, target)
1215
+ >>> print(output.asnumpy())
1216
+ 0.3652635
1217
+ """
1218
+
1219
+ def __init__(self, log_input=True, full=False, eps=1e-08, reduction="mean"):
1220
+ """Initialize PoissonNLLLoss."""
1221
+ super(PoissonNLLLoss, self).__init__(reduction=reduction)
1222
+ self.log_input = log_input
1223
+ self.full = full
1224
+ self.eps = eps
1225
+ self.maximum = P.Maximum()
1226
+ self.cast = P.Cast()
1227
+
1228
+ def construct(self, input, target):
1229
+ _check_is_tensor('input', input, self.cls_name)
1230
+ _check_is_tensor('target', target, self.cls_name)
1231
+ if input.ndim == 0 or target.ndim == 0:
1232
+ raise ValueError(
1233
+ "For 'PoissonNLLLoss', the inputs must be non-scalar, but got shapes: "
1234
+ f"input: {input.shape}, target: {target.shape}"
1235
+ )
1236
+ target = self.cast(target, input.dtype)
1237
+ if self.log_input:
1238
+ loss = input.exp() - target * input
1239
+ else:
1240
+ loss = input - target * ((input + self.eps).log())
1241
+ if self.full:
1242
+ target = self.maximum(target, self.eps)
1243
+ stirling_term = (target > 1) * ((target + 0.5) * target.log() - target + get_half_ln_2_pi())
1244
+ loss += F.masked_fill(stirling_term, target <= 1, 0)
1245
+ out = self.get_loss(loss)
1246
+ return out
1247
+
1248
+
1249
+ @constexpr
1250
+ def get_half_ln_2_pi():
1251
+ return 0.5 * math.log(2 * math.pi)
1252
+
1253
+
1254
+ class MultiLabelSoftMarginLoss(LossBase):
1255
+ r"""
1256
+ Calculates the MultiLabelSoftMarginLoss.
1257
+ The multi-label soft margin loss is a commonly used loss function in multi-label classification tasks
1258
+ where an input sample can belong to multiple classes.
1259
+ Given an input :math:`x` and binary labels :math:`y` of size :math:`(N,C)`, where :math:`N` denotes
1260
+ the number of samples and :math:`C` denotes the number of classes.
1261
+
1262
+ .. math::
1263
+ \mathcal{loss\left( x , y \right)} = - \frac{1}{N}\frac{1}{C}\sum_{i = 1}^{N}
1264
+ \sum_{j = 1}^{C}\left(y_{ij}\log\frac{1}{1 + e^{- x_{ij}}} + \left( 1 - y_{ij}
1265
+ \right)\log\frac{e^{-x_{ij}}}{1 + e^{-x_{ij}}} \right)
1266
+
1267
+ where :math:`x{ij}` represents the predicted score of sample :math:`i` for class :math:`j`. :math:`y{ij}`
1268
+ represents the binary label of sample :math:`i` for class :math:`j`, where sample :math:`i` belongs to
1269
+ class :math:`j` if :math:`y{ij}=1` , and sample :math:`i` does not belong to class :math:`j` if :math:`y{ij}=0`.
1270
+ For a multi-label classification task, each sample may have multiple labels with a value of 1 in the binary
1271
+ label :math:`y`. `weight` will multiply to the loss of each class if given.
1272
+
1273
+ Args:
1274
+ weight (Union[Tensor, int, float]): The manual rescaling weight given to each class. Default: None.
1275
+ reduction (str): Specifies which reduction to be applied to the output. It must be one of
1276
+ 'none', 'mean', and 'sum', meaning no reduction, reduce mean and sum on output, respectively.
1277
+ Default: 'mean'.
1278
+
1279
+ Inputs:
1280
+ - **x** (Tensor) - A tensor of shape (N, C), where N is batch size and C is number
1281
+ of classes.
1282
+ - **target** (Tensor) - The label target Tensor which has the same shape as `x`.
1283
+
1284
+ Outputs:
1285
+ Tensor, the data type is the same as x, if the reduction is 'none', its shape is (N), otherwise it is zero.
1286
+
1287
+ Raises:
1288
+ ValueError: If the rank of `x` or `target` is not 2.
1289
+
1290
+ Supported Platforms:
1291
+ ``Ascend`` ``GPU`` ``CPU``
1292
+
1293
+ Examples:
1294
+ >>> x = Tensor([[0.3, 0.6, 0.6], [0.9, 0.4, 0.2]])
1295
+ >>> target = Tensor([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]])
1296
+ >>> loss = nn.MultiLabelSoftMarginLoss(reduction='mean')
1297
+ >>> out = loss(x, target)
1298
+ >>> print(out.asnumpy())
1299
+ 0.84693956
1300
+ """
1301
+
1302
+ def __init__(self, weight=None, reduction="mean"):
1303
+ """Initialize MultiLabelSoftMarginLoss."""
1304
+ super(MultiLabelSoftMarginLoss, self).__init__(reduction)
1305
+ self.weight = weight
1306
+ self.reduction = reduction
1307
+
1308
+ def construct(self, x, target):
1309
+ return F.multilabel_soft_margin_loss(x, target, self.weight, self.reduction)
1310
+
1311
+
1092
1312
  class MultiMarginLoss(LossBase):
1093
1313
  r"""
1094
1314
  Creates a criterion that optimizes a multi-class classification hinge
@@ -1100,25 +1320,28 @@ class MultiMarginLoss(LossBase):
1100
1320
  output :math:`y` is:
1101
1321
 
1102
1322
  .. math::
1103
- \text{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\text{margin} - x[y] + x[i]))^p)}{\text{x.size}(0)}
1323
+ \text{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\text{margin} - x[y] + x[i]))^p}{\text{x.size}(0)}
1104
1324
 
1105
1325
  where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`
1106
1326
  and :math:`i \neq y`.
1107
1327
 
1108
- Optionally, you can give non-equal weighting on the classes by passing
1109
- a 1D input `weight` tensor w into the constructor.
1110
-
1111
1328
  Args:
1112
- p (int): Optional. The norm degree for pairwise distance. Should be 1 or 2. Default: 1.
1113
- margin (float): Optional. A parameter to change pairwise distance. Default: 1.0.
1114
- reduction (str): Apply specific reduction method to the output: 'none', 'mean', 'sum'. Default: "mean".
1329
+ p (int, optional): The norm degree for pairwise distance. Should be 1 or 2. Default: 1.
1330
+ margin (float, optional): A parameter to change pairwise distance. Default: 1.0.
1331
+ reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
1332
+ Default: 'mean'.
1333
+
1334
+ - 'none': no reduction will be applied.
1335
+ - 'mean': the sum of the output will be divided by the number of elements in the output.
1336
+ - 'sum': the output will be summed.
1337
+
1338
+ weight (Tensor, optional): The rescaling weight to each class with shape :math:`(C,)`. Data type only
1339
+ support float32, float16 or float64. Default: None, all classes are weighted equally.
1115
1340
 
1116
1341
  Inputs:
1117
1342
  - **x** (Tensor) - Input x, with shape :math:`(N, C)`. Data type only support float32, float16 or float64.
1118
1343
  - **target** (Tensor) - Ground truth labels, with shape :math:`(N,)`. Data type only support int64. The
1119
1344
  value of target should be non-negative, less than C.
1120
- - **weight** (Tensor, optional) - The rescaling weight to each class with shape :math:`(C,)`. Data type only
1121
- support float32, float16 or float64. Default: None.
1122
1345
 
1123
1346
  Outputs:
1124
1347
  Tensor, When `reduction` is 'none', the shape is :math:`(N,)`.
@@ -1138,7 +1361,7 @@ class MultiMarginLoss(LossBase):
1138
1361
  ValueError: If rank of `x` is not 2 or rank of 'target' is not 1.
1139
1362
 
1140
1363
  Supported Platforms:
1141
- ``Ascend`` ``CPU``
1364
+ ``Ascend`` ``GPU`` ``CPU``
1142
1365
 
1143
1366
  Examples:
1144
1367
  >>> x = Tensor(np.ones(shape=[3, 3]), mindspore.float32)
@@ -1149,20 +1372,23 @@ class MultiMarginLoss(LossBase):
1149
1372
  0.6666667
1150
1373
  """
1151
1374
 
1152
- def __init__(self, p=1, margin=1.0, reduction='mean'):
1375
+ def __init__(self, p=1, margin=1.0, reduction='mean', weight=None):
1153
1376
  """Initialize MultiMarginLoss."""
1154
1377
  super(MultiMarginLoss, self).__init__()
1155
1378
  self.multi_margin_loss = MultiMarginLossOp(p=p, margin=margin, reduction=reduction)
1156
- self.ones = P.Ones()
1379
+ self.generate_ones = ops.Fill()
1380
+ self.weight = weight
1157
1381
 
1158
1382
  def construct(self, x, target, weight=None):
1159
1383
  _check_is_tensor('x', x, self.cls_name)
1160
1384
  _check_is_tensor('target', target, self.cls_name)
1385
+ if self.weight is not None:
1386
+ weight = self.weight
1161
1387
  weight_one = weight is None
1162
1388
  if not weight_one:
1163
1389
  _check_is_tensor('weight', weight, self.cls_name)
1164
1390
  else:
1165
- weight = self.ones(x.shape[1], x.dtype)
1391
+ weight = self.generate_ones(x.dtype, x.astype('float32')[0].shape, 1)
1166
1392
  loss = self.multi_margin_loss(x, target, weight)
1167
1393
  return loss
1168
1394
 
@@ -1197,12 +1423,13 @@ class BCELoss(LossBase):
1197
1423
  weight (Tensor, optional): A rescaling weight applied to the loss of each batch element.
1198
1424
  And it must have the same shape and data type as `inputs`. Default: None
1199
1425
  reduction (str): Specifies the reduction to be applied to the output.
1200
- Its value must be one of 'none', 'mean', 'sum'. Default: 'none'.
1426
+ Its value must be one of 'none', 'mean', 'sum'. Default: 'mean'.
1201
1427
 
1202
1428
  Inputs:
1203
1429
  - **logits** (Tensor) - The input tensor with shape :math:`(N, *)` where :math:`*` means, any number
1204
1430
  of additional dimensions. The data type must be float16 or float32.
1205
- - **labels** (Tensor) - The label tensor with shape :math:`(N, *)`, the same shape and data type as `logits`.
1431
+ - **labels** (Tensor) - The label tensor with shape :math:`(N, *)` where :math:`*` means, any number
1432
+ of additional dimensions. The same shape and data type as `logits`.
1206
1433
 
1207
1434
  Outputs:
1208
1435
  Tensor, has the same dtype as `logits`. if `reduction` is 'none', then it has the same shape as `logits`.
@@ -1226,9 +1453,9 @@ class BCELoss(LossBase):
1226
1453
  1.8952923
1227
1454
  """
1228
1455
 
1229
- def __init__(self, weight=None, reduction='none'):
1456
+ def __init__(self, weight=None, reduction='mean'):
1230
1457
  """Initialize BCELoss."""
1231
- super(BCELoss, self).__init__()
1458
+ super(BCELoss, self).__init__(reduction)
1232
1459
  self.binary_cross_entropy = P.BinaryCrossEntropy(reduction=reduction)
1233
1460
  self.weight_one = weight is None
1234
1461
  if not self.weight_one:
@@ -1247,7 +1474,7 @@ class BCELoss(LossBase):
1247
1474
  return loss
1248
1475
 
1249
1476
 
1250
- @constexpr
1477
+ @_primexpr
1251
1478
  def _check_reduced_shape_valid(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2):
1252
1479
  """Internal function, used to check whether the reduced shape meets the requirements."""
1253
1480
  validator.check_reduce_shape(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2)
@@ -1305,13 +1532,13 @@ class CosineEmbeddingLoss(LossBase):
1305
1532
  self.reduce_sum = P.ReduceSum()
1306
1533
  self.maximum = P.Maximum()
1307
1534
  validator.check_value_type("margin", margin, [float], self.cls_name)
1308
- self.margin = validator.check_float_range(margin, -1.0, 1.0, Rel.INC_BOTH, "margin", self.cls_name)
1535
+ self.margin = validator.check_float_range(margin, -1.0, 1.0, validator.INC_BOTH, "margin", self.cls_name)
1309
1536
 
1310
1537
  def construct(self, logits_x1, logits_x2, labels):
1311
1538
  _check_is_tensor('logits_x1', logits_x1, self.cls_name)
1312
1539
  _check_is_tensor('logits_x2', logits_x2, self.cls_name)
1313
1540
  _check_is_tensor('labels', labels, self.cls_name)
1314
- F.same_type_shape(logits_x1, logits_x2)
1541
+ inner.same_type_shape_(logits_x1, logits_x2)
1315
1542
  _check_reduced_shape_valid(F.shape(logits_x1), F.shape(labels), (1,), self.cls_name, "logits_x1", "labels")
1316
1543
  # if labels > 0, 1-cosine(logits_x1, logits_x2)
1317
1544
  # else, max(0, cosine(logits_x1, logits_x2)-margin)
@@ -1333,12 +1560,12 @@ class CosineEmbeddingLoss(LossBase):
1333
1560
 
1334
1561
  class MultilabelMarginLoss(LossBase):
1335
1562
  r"""
1336
- MultilabelMarginLoss operation.
1563
+ Creates a loss criterion that minimizes the hinge loss for multi-class
1564
+ classification tasks.
1565
+ It takes a 2D mini-batch Tensor :math:`x` as input and a 2D
1566
+ Tensor :math:`y` containing target class indices as output.
1337
1567
 
1338
- Creates a criterion that optimizes a multi-class multi-classification
1339
- hinge loss (margin-based loss) between input :math:`x` (a 2D mini-batch `Tensor`)
1340
- and output :math:`y` (which is a 2D `Tensor` of target class indices).
1341
- For each sample in the mini-batch:
1568
+ Each sample in the mini-batch, the loss is computed as follows:
1342
1569
 
1343
1570
  .. math::
1344
1571
  \text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)}
@@ -1346,17 +1573,18 @@ class MultilabelMarginLoss(LossBase):
1346
1573
  where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`, \
1347
1574
  :math:`y \in \left\{0, \; \cdots , \; \text{y.size}(0) - 1\right\}`, \
1348
1575
  :math:`0 \leq y[j] \leq \text{x.size}(0)-1`, \
1349
- and :math:`i \neq y[j]` for all :math:`i` and :math:`j`.
1576
+ and for all :math:`i` and :math:`j`, :math:`i` does not equal to :math:`y[j]`.
1350
1577
 
1351
- :math:`y` and :math:`x` must have the same size.
1578
+ Furthermore, both :math:`y` and :math:`x` should have identical sizes.
1352
1579
 
1353
- The criterion only considers a contiguous block of non-negative targets that
1354
- starts at the front.
1355
-
1356
- This allows for different samples to have variable amounts of target classes.
1580
+ Note:
1581
+ For this operator, only a contiguous sequence of non-negative targets that starts at
1582
+ the beginning is taken into consideration, which means that different samples can have different
1583
+ number of target classes.
1357
1584
 
1358
1585
  Args:
1359
- reduction (str): Apply specific reduction method to the output: 'none', 'mean', 'sum'. Default: "mean".
1586
+ reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
1587
+ Default: "mean".
1360
1588
 
1361
1589
  Inputs:
1362
1590
  - **x** (Tensor) - Predict data. Tensor of shape :math:`(C)` or :math:`(N, C)`, where :math:`N`
@@ -1367,8 +1595,6 @@ class MultilabelMarginLoss(LossBase):
1367
1595
  Outputs:
1368
1596
  - **y** (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. If `reduction` is "none", its shape
1369
1597
  is :math:`(N)`. Otherwise, a scalar value will be returned.
1370
- - **is_target** (Tensor) - Output tensor for backward input, with the same shape as `target`,
1371
- data type must be int32.
1372
1598
 
1373
1599
  Raises:
1374
1600
  TypeError: If `x` or `target` is not a Tensor.
@@ -1379,7 +1605,7 @@ class MultilabelMarginLoss(LossBase):
1379
1605
  ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1380
1606
 
1381
1607
  Supported Platforms:
1382
- ``Ascend``
1608
+ ``Ascend`` ``GPU``
1383
1609
 
1384
1610
  Examples:
1385
1611
  >>> loss = nn.MultilabelMarginLoss()
@@ -1387,8 +1613,7 @@ class MultilabelMarginLoss(LossBase):
1387
1613
  >>> target = Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), mindspore.int32)
1388
1614
  >>> output = loss(x, target)
1389
1615
  >>> print(output)
1390
- (Tensor(shape=[], dtype=Float32, value= 0.325), Tensor(shape=[2, 4], dtype=Int32, value=
1391
- [[1, 1, 1, 1], [0, 0, 1, 1]]))
1616
+ Tensor(shape=[], dtype=Float32, value=0.325)
1392
1617
  """
1393
1618
 
1394
1619
  def __init__(self, reduction='mean'):
@@ -1396,7 +1621,8 @@ class MultilabelMarginLoss(LossBase):
1396
1621
  self.multilabel_margin_loss = MultilabelMarginLossOp(reduction=reduction)
1397
1622
 
1398
1623
  def construct(self, x, target):
1399
- return self.multilabel_margin_loss(x, target)
1624
+ loss, _ = self.multilabel_margin_loss(x, target)
1625
+ return loss
1400
1626
 
1401
1627
 
1402
1628
  class BCEWithLogitsLoss(LossBase):
@@ -1434,7 +1660,8 @@ class BCEWithLogitsLoss(LossBase):
1434
1660
  Inputs:
1435
1661
  - **logits** (Tensor) - Input logits with shape :math:`(N, *)` where :math:`*` means, any number
1436
1662
  of additional dimensions. The data type must be float16 or float32.
1437
- - **labels** (Tensor) - Ground truth label with shape :math:`(N, *)`, same shape and dtype as `logits`.
1663
+ - **labels** (Tensor) - Ground truth label with shape :math:`(N, *)` where :math:`*` means, any number
1664
+ of additional dimensions. The same shape and data type as `logits`.
1438
1665
 
1439
1666
  Outputs:
1440
1667
  Tensor or Scalar, if `reduction` is 'none', its shape is the same as `logits`.
@@ -1490,7 +1717,7 @@ class BCEWithLogitsLoss(LossBase):
1490
1717
  return loss
1491
1718
 
1492
1719
 
1493
- @constexpr
1720
+ @_primexpr
1494
1721
  def _check_ndim(logits_nidm, labels_ndim, prime_name=None):
1495
1722
  '''Internal function, used to check whether the dimension of logits and labels meets the requirements.'''
1496
1723
  msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
@@ -1505,7 +1732,7 @@ def _check_ndim(logits_nidm, labels_ndim, prime_name=None):
1505
1732
  f"dimension of 'logits' {logits_nidm} and dimension of 'labels' {labels_ndim}.")
1506
1733
 
1507
1734
 
1508
- @constexpr
1735
+ @_primexpr
1509
1736
  def _check_channel_and_shape(logits, labels, prime_name=None):
1510
1737
  '''Internal function, used to check whether the channels or shape of logits and labels meets the requirements.'''
1511
1738
  msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
@@ -1635,18 +1862,6 @@ class FocalLoss(LossBase):
1635
1862
  return self.get_loss(loss)
1636
1863
 
1637
1864
 
1638
- @constexpr
1639
- def _dtype_check(logits_dtype, labels_dtype, prim_name):
1640
- """Check dtype."""
1641
- if logits_dtype not in [mstype.float32, mstype.float16]:
1642
- raise TypeError("For {}, the logits_dtype must be float32 or float16, but got {}.".format(prim_name,
1643
- logits_dtype))
1644
- if logits_dtype != labels_dtype:
1645
- raise TypeError("For {}, the labels_dtype must equal to logits_dtype {}, but got {}".format(prim_name,
1646
- logits_dtype,
1647
- labels_dtype))
1648
-
1649
-
1650
1865
  class HuberLoss(LossBase):
1651
1866
  r"""
1652
1867
  HuberLoss calculate the error between the predicted value and the target value.
@@ -1727,47 +1942,28 @@ class HuberLoss(LossBase):
1727
1942
  def __init__(self, reduction='mean', delta=1.0):
1728
1943
  """Initialize HuberLoss."""
1729
1944
  super(HuberLoss, self).__init__(reduction=reduction)
1730
- validator.check_value_type('delta', delta, [float, int], self.cls_name)
1731
- validator.check_number("delta", delta, 0.0, Rel.GT, self.cls_name)
1732
- self.sub = P.Sub()
1733
- self.mul = P.Mul()
1734
- self.abs = P.Abs()
1735
- self.less = P.Less()
1736
- self.square = P.Square()
1737
- self.select = P.Select()
1738
- self.dtype = P.DType()
1945
+ self.reduction = reduction
1739
1946
  self.delta = delta
1740
- self.delta_half = 0.5 * self.delta
1741
1947
 
1742
1948
  def construct(self, logits, labels):
1743
- _check_is_tensor('logits', logits, self.cls_name)
1744
- _check_is_tensor('labels', labels, self.cls_name)
1745
- logits_dtype = self.dtype(logits)
1746
- labels_dtype = self.dtype(labels)
1747
- _dtype_check(logits_dtype, labels_dtype, self.cls_name)
1748
- z = self.abs(self.sub(logits, labels))
1749
- condition = self.less(z, self.delta)
1750
- l1 = self.mul(0.5, self.square(z))
1751
- l2 = self.mul(self.delta, self.sub(z, self.delta_half))
1752
- loss = self.select(condition, l1, l2)
1753
-
1754
- return self.get_loss(loss)
1949
+ return F.huber_loss(logits, labels, self.reduction, self.delta)
1755
1950
 
1756
1951
 
1757
1952
  class TripletMarginLoss(LossBase):
1758
1953
  r"""
1759
1954
  TripletMarginLoss operation.
1760
1955
 
1761
- Creates a criterion that measures the triplet loss given an input
1762
- tensors :math:`x1`, :math:`x2`, :math:`x3` and a margin with a value greater than :math:`0`.
1763
- This is used for measuring a relative similarity between samples. A triplet
1764
- is composed by `a`, `p` and `n` (i.e., `anchor`, `positive examples` and `negative
1765
- examples` respectively). The shapes of all input tensors should be
1766
- :math:`(N, D)`.
1956
+ Triple loss is used to measure the relative similarity between samples,
1957
+ which is measured by a triplet and a :math:`margin` with a value greater than :math:`0` .
1958
+ The triplet is composed by :math:`a`, :math:`p`, :math:`n` in the following formula.
1959
+
1960
+ The shapes of all input tensors should be :math:`(N, *)` , where :math:`N` is batch size
1961
+ and :math:`*` means any number of additional dimensions.
1767
1962
 
1768
- The distance swap is described in detail in the paper `Learning shallow
1769
- convolutional feature descriptors with triplet losses` by
1770
- V. Balntas, E. Riba et al.
1963
+ The distance swap is described in detail in the paper
1964
+ `Learning local feature descriptors with triplets and shallow convolutional neural
1965
+ networks <http://158.109.8.37/files/BRP2016.pdf>`_
1966
+ by V. Balntas, E. Riba et al.
1771
1967
 
1772
1968
  The loss function for each sample in the mini-batch is:
1773
1969
 
@@ -1780,28 +1976,30 @@ class TripletMarginLoss(LossBase):
1780
1976
  d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p
1781
1977
 
1782
1978
  Args:
1783
- p (int): The norm degree for pairwise distance. Default: 2.
1784
- eps (float): Default: 1e-06.
1785
- swap (bool): The distance swap is described in detail in the paper
1786
- `Learning shallow convolutional feature descriptors with triplet losses` by
1787
- V. Balntas, E. Riba et al. Default: "False".
1788
- reduction (str): Apply specific reduction method to the output: 'none', 'mean', 'sum'. Default: "mean".
1979
+ p (int, optional): The degree of norm for pairwise distance. Default: 2.
1980
+ eps (float, optional): Add small value to avoid division by zero. Default: 1e-06.
1981
+ swap (bool, optional): The distance swap change the negative distance to the distance between positive
1982
+ sample and negative sample. Default: "False".
1983
+ reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
1984
+ Default: "mean".
1985
+ margin (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair. Default: 1.0.
1789
1986
 
1790
1987
  Inputs:
1791
1988
  - **x** (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.
1792
- - **positive** (Tensor) - A sample belonging to the same category as x, with the same type and shape as `x`.
1793
- - **negative** (Tensor) - A sample belonging to the different class from x, with the same type and shape as `x`.
1794
- - **margin** (Tensor) - Make a margin between the positive pair and the negative pair.
1989
+ :math:`a` in the above formula.
1990
+ - **positive** (Tensor) - A sample belonging to the same category as `x`, with the same type and
1991
+ shape as `x`. :math:`p` in the above formula.
1992
+ - **negative** (Tensor) - A sample belonging to the different class from `x`, with the same type and shape
1993
+ as `x`. :math:`n` in the above formula.
1994
+ - **margin** (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair.
1995
+ Default: 1.0.
1795
1996
 
1796
1997
  Outputs:
1797
- Union[Tensor, Scalar], if `reduction` is "none", its shape is :math:`(N)`.
1798
- Otherwise, a scalar value will be returned.
1998
+ Tensor. If `reduction` is "none", its shape is :math:`(N)`. Otherwise, a scalar value will be returned.
1799
1999
 
1800
2000
  Raises:
1801
- TypeError: If `x` or `positive` or 'negative' or 'margin' is not a Tensor.
1802
- TypeError: If dtype of `x` or `positive` or `negative` is not BasicType.
2001
+ TypeError: If `x` or `positive` or 'negative' is not a Tensor.
1803
2002
  TypeError: If dtype of `x`, `positive` and `negative` is not the same.
1804
- TypeError: If `margin` is not float32.
1805
2003
  TypeError: If `p` is not an int.
1806
2004
  TypeError: If `eps` is not a float.
1807
2005
  TypeError: If `swap` is not a bool.
@@ -1812,25 +2010,31 @@ class TripletMarginLoss(LossBase):
1812
2010
  ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1813
2011
 
1814
2012
  Supported Platforms:
1815
- ``Ascend`` ``CPU``
2013
+ ``GPU``
1816
2014
 
1817
2015
  Examples:
1818
2016
  >>> loss = nn.TripletMarginLoss()
1819
2017
  >>> x = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
1820
2018
  >>> positive = Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), mindspore.float32)
1821
2019
  >>> negative = Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), mindspore.float32)
1822
- >>> margin = Tensor(1.0, mindspore.float32)
1823
- >>> output = loss(x, positive, negative, margin)
2020
+ >>> output = loss(x, positive, negative)
1824
2021
  >>> print(output)
1825
2022
  0.8881968
1826
2023
  """
1827
2024
 
1828
- def __init__(self, p=2, swap=False, eps=1e-6, reduction='mean'):
2025
+ def __init__(self, p=2, swap=False, eps=1e-06, reduction='mean', margin=1.):
1829
2026
  super(TripletMarginLoss, self).__init__()
1830
- self.triplet_margin_loss = TripletMarginLossOp(p=p, swap=swap, eps=eps, reduction=reduction)
2027
+ self.p = p
2028
+ self.swap = swap
2029
+ self.eps = eps
2030
+ self.reduction = reduction
2031
+ self.margin = margin
1831
2032
 
1832
- def construct(self, x, positive, negative, margin):
1833
- return self.triplet_margin_loss(x, positive, negative, margin)
2033
+ def construct(self, x, positive, negative, margin=1.):
2034
+ if self.margin != 1.0:
2035
+ margin = self.margin
2036
+ return F.triplet_margin_loss(x, positive, negative, margin=margin, p=self.p,
2037
+ eps=self.eps, swap=self.swap, reduction=self.reduction)
1834
2038
 
1835
2039
 
1836
2040
  @constexpr
@@ -1851,7 +2055,7 @@ class NLLLoss(LossBase):
1851
2055
  r"""
1852
2056
  Gets the negative log likelihood loss between logits and labels.
1853
2057
 
1854
- The nll loss with reduction=none can be described as:
2058
+ The nll loss with :math:`reduction = none` can be described as:
1855
2059
 
1856
2060
  .. math::
1857
2061
 
@@ -1928,9 +2132,9 @@ class NLLLoss(LossBase):
1928
2132
 
1929
2133
 
1930
2134
  @constexpr
1931
- def _check_cross_entropy_inputs(logits_shape, label_shape, \
1932
- logits_rank, label_rank, \
1933
- logits_dtype, label_dtype, \
2135
+ def _check_cross_entropy_inputs(logits_shape, label_shape,
2136
+ logits_rank, label_rank,
2137
+ logits_dtype, label_dtype,
1934
2138
  prim_name=None):
1935
2139
  """Internal function, used to check whether the shape of logits and labels meets the requirements."""
1936
2140
  validator.check_type_name('logits', logits_dtype, [mstype.float16, mstype.float32], prim_name)
@@ -1944,11 +2148,12 @@ def _check_cross_entropy_inputs(logits_shape, label_shape, \
1944
2148
  f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
1945
2149
  elif label_rank == logits_rank - 1:
1946
2150
  validator.check_type_name('labels', label_dtype, [mstype.int32], prim_name)
1947
- logits_shape_new = (logits_shape[0], *logits_shape[2:])
1948
- if logits_shape_new != label_shape:
1949
- raise ValueError(f"{msg_prefix} shape of 'logits' should be (N, C, d_0, d_1, ...), "
1950
- f"and the shape of 'labels' should be (N, d_0, d_1, ...), "
1951
- f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
2151
+ if logits_rank != 1:
2152
+ logits_shape_new = (logits_shape[0], *logits_shape[2:])
2153
+ if logits_shape_new != label_shape:
2154
+ raise ValueError(f"{msg_prefix} shape of 'logits' should be (N, C, d_0, d_1, ...), "
2155
+ f"and the shape of 'labels' should be (N, d_0, d_1, ...), "
2156
+ f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
1952
2157
  else:
1953
2158
  raise ValueError(f"{msg_prefix} rank of 'logits' and 'labels' should be:\n"
1954
2159
  f"1. 'logits.ndim == labels.ndim' for probabilities, \n"
@@ -1958,7 +2163,7 @@ def _check_cross_entropy_inputs(logits_shape, label_shape, \
1958
2163
 
1959
2164
  @constexpr
1960
2165
  def _cross_entropy_ignore_index_warning(prim_name):
1961
- """Internal function, used to warrning when ignore_index > 0 for probabilities."""
2166
+ """Internal function, used to warning when ignore_index > 0 for probabilities."""
1962
2167
  log.warning(f"For \'{prim_name}\', 'ignore_index' does not work when 'labels' is Probability.")
1963
2168
 
1964
2169
 
@@ -2065,7 +2270,7 @@ class CrossEntropyLoss(LossBase):
2065
2270
  super().__init__(reduction)
2066
2271
  validator.check_value_type('ignore_index', ignore_index, int, self.cls_name)
2067
2272
  validator.check_value_type('label_smoothing', label_smoothing, float, self.cls_name)
2068
- validator.check_float_range(label_smoothing, 0.0, 1.0, Rel.INC_BOTH, 'label_smoothing', self.cls_name)
2273
+ validator.check_float_range(label_smoothing, 0.0, 1.0, validator.INC_BOTH, 'label_smoothing', self.cls_name)
2069
2274
 
2070
2275
  if weight is not None:
2071
2276
  validator.check_value_type("weight", weight, [Tensor], self.cls_name)
@@ -2079,10 +2284,341 @@ class CrossEntropyLoss(LossBase):
2079
2284
  def construct(self, logits, labels):
2080
2285
  _check_is_tensor('logits', logits, self.cls_name)
2081
2286
  _check_is_tensor('labels', labels, self.cls_name)
2082
- _check_cross_entropy_inputs(logits.shape, labels.shape, \
2083
- logits.ndim, labels.ndim, \
2084
- logits.dtype, labels.dtype, \
2287
+ _check_cross_entropy_inputs(logits.shape, labels.shape,
2288
+ logits.ndim, labels.ndim,
2289
+ logits.dtype, labels.dtype,
2085
2290
  self.cls_name)
2086
2291
  if logits.ndim == labels.ndim and self.ignore_index > 0:
2087
2292
  _cross_entropy_ignore_index_warning(self.cls_name)
2088
2293
  return F.cross_entropy(logits, labels, self.weight, self.ignore_index, self.reduction, self.label_smoothing)
2294
+
2295
+
2296
+ class KLDivLoss(LossBase):
2297
+ r"""
2298
+ Computes the Kullback-Leibler divergence between the `logits` and the `labels`.
2299
+
2300
+ For tensors of the same shape :math:`x` and :math:`target`,
2301
+ the updating formulas of KLDivLoss algorithm are as follows,
2302
+
2303
+ .. math::
2304
+ L(x, target) = target \cdot (\log target - x)
2305
+
2306
+ Then,
2307
+
2308
+ .. math::
2309
+ \ell(x, target) = \begin{cases}
2310
+ L(x, target), & \text{if reduction} = \text{'none';}\\
2311
+ \operatorname{mean}(L(x, target)), & \text{if reduction} = \text{'mean';}\\
2312
+ \operatorname{sum}(L(x, target)) / x.\operatorname{shape}[0], & \text{if reduction} = \text{'batchmean';}\\
2313
+ \operatorname{sum}(L(x, target)), & \text{if reduction} = \text{'sum'.}
2314
+ \end{cases}
2315
+
2316
+ where :math:`x` represents `logits`,
2317
+ :math:`target` represents `labels`, and
2318
+ :math:`\ell(x, target)` represents `output`.
2319
+
2320
+ Note:
2321
+ - Currently it does not support float64 input on `Ascend`.
2322
+ - The output aligns with the mathematical definition of Kullback-Leibler divergence
2323
+ only when `reduction` is set to 'batchmean'.
2324
+
2325
+ Args:
2326
+ reduction (str): Specifies the reduction to be applied to the output.
2327
+ Default: 'mean'.
2328
+
2329
+ - On Ascend, the value of `reduction` must be one of 'batchmean', 'none' or 'sum'.
2330
+ - On GPU, the value of `reduction` must be one of 'mean', 'none' or 'sum'.
2331
+ - On CPU, the value of `reduction` must be one of 'mean', 'batchmean', 'none' or 'sum'.
2332
+
2333
+ Inputs:
2334
+ - **logits** (Tensor) - The input Tensor. The data type must be float16, float32 or float64.
2335
+ - **labels** (Tensor) - The label Tensor which has the same shape and data type as `logits`.
2336
+
2337
+ Outputs:
2338
+ Tensor or Scalar, if `reduction` is 'none', then output is a tensor and has the same shape as `logits`.
2339
+ Otherwise, it is a scalar.
2340
+
2341
+ Raises:
2342
+ TypeError: If `reduction` is not a str.
2343
+ TypeError: If neither `logits` nor `labels` is a Tensor.
2344
+ TypeError: If dtype of `logits` or `labels` is not currently supported.
2345
+ ValueError: If shape of `logits` is not the same as `labels`.
2346
+ RuntimeError: If `logits` or `labels` is a scalar when `reduction` is 'batchmean'.
2347
+
2348
+ Supported Platforms:
2349
+ ``Ascend`` ``GPU`` ``CPU``
2350
+
2351
+ Examples:
2352
+ >>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
2353
+ >>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32)
2354
+ >>> loss = nn.KLDivLoss(reduction='mean')
2355
+ >>> output = loss(logits, labels)
2356
+ >>> print(output)
2357
+ -0.23333333
2358
+ """
2359
+
2360
+ def __init__(self, reduction='mean'):
2361
+ super().__init__()
2362
+ self.reduction = reduction
2363
+
2364
+ def construct(self, logits, labels):
2365
+ _check_is_tensor('logits', logits, self.cls_name)
2366
+ _check_is_tensor('labels', labels, self.cls_name)
2367
+ return F.kl_div(logits, labels, self.reduction)
2368
+
2369
+
2370
+ class CTCLoss(LossBase):
2371
+ """
2372
+ Calculates the CTC (Connectionist Temporal Classification) loss. It's mainly used to calculate the loss between
2373
+ the continuous, unsegemented time series and the target series.
2374
+
2375
+ For the CTC algorithm, refer to `Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with
2376
+ Recurrent Neural Networks <http://www.cs.toronto.edu/~graves/icml_2006.pdf>`_ .
2377
+
2378
+ Args:
2379
+ blank (int, optional): The blank label. Default: 0.
2380
+ reduction (str, optional): Implements the reduction method to the output with
2381
+ 'none', 'mean', or 'sum'. Default: 'mean'.
2382
+ zero_infinity (bool, optional): If loss is infinite, this parameter determines whether to set that loss
2383
+ and its correlated gradient to zero. Default: False.
2384
+
2385
+ Inputs:
2386
+ - **log_probs** (Tensor) - A tensor of shape :math:`(T, N, C)` or :math:`(T, C)`, where T is length of input,
2387
+ N is size of the batch and C is the number of classes. T, N and C are positive integers.
2388
+ - **targets** (Tensor) - A tensor of shape :math:`(N, S)` or (sum( `target_lengths` )),
2389
+ where S is max target length, means the target sequences.
2390
+ - **input_lengths** (Union[tuple, Tensor]) - A tuple or Tensor of shape(N). It means the lengths of the input.
2391
+ - **target_lengths** (Union[tuple, Tensor]) - A tuple or Tensor of shape(N). It means the lengths of the target.
2392
+
2393
+ Outputs:
2394
+ - **neg_log_likelihood** (Tensor) - A loss value which is differentiable with respect to each input node.
2395
+
2396
+ Raises:
2397
+ TypeError: If `log_probs` or `targets` is not a Tensor.
2398
+ TypeError: If `zero_infinity` is not a bool, `reduction` is not string.
2399
+ TypeError: If the dtype of `log_probs` is not float or double.
2400
+ TypeError: If the dtype of `targets`, `input_lengths` or `target_lengths` is not int32 or int64.
2401
+ ValueError: If `reduction` is not "none", "mean" or "sum".
2402
+ ValueError: If the types of `targets`, `input_lengths` or `target_lengths` are different.
2403
+ ValueError: If the value of `blank` is not in range [0, C). C is number of classes of `log_probs` .
2404
+ ValueError: If the dimension of `targets` is not one when the shape of `log_prob` is :math:`(T, C)`.
2405
+ RuntimeError: If any value of `input_lengths` is larger than T. T is length of `log_probs` .
2406
+ RuntimeError: If any target_lengths[i] is not in range [0, input_length[i]].
2407
+
2408
+ Supported Platforms:
2409
+ ``Ascend`` ``GPU`` ``CPU``
2410
+
2411
+ Examples:
2412
+ >>> import numpy as np
2413
+ >>> from mindspore import Tensor
2414
+ >>> from mindspore import dtype as mstype
2415
+ >>> from mindspore.nn.loss import CTCLoss
2416
+ >>> T = 5 # Input sequence length
2417
+ >>> C = 2 # Number of classes
2418
+ >>> N = 2 # Batch size
2419
+ >>> S = 3 # Target sequence length of longest target in batch (padding length)
2420
+ >>> S_min = 2 # Minimum target length, for demonstration purposes
2421
+ >>> arr = np.arange(T*N*C).reshape((T, N, C))
2422
+ >>> ms_input = Tensor(arr, dtype=mstype.float32)
2423
+ >>> input_lengths = np.full(shape=(N), fill_value=T)
2424
+ >>> input_lengths = Tensor(input_lengths, dtype=mstype.int32)
2425
+ >>> target_lengths = np.full(shape=(N), fill_value=S_min)
2426
+ >>> target_lengths = Tensor(target_lengths, dtype=mstype.int32)
2427
+ >>> target = np.random.randint(1, C, size=(N, S))
2428
+ >>> target = Tensor(target, dtype=mstype.int32)
2429
+ >>> ctc_loss = CTCLoss(blank=0, reduction='none', zero_infinity=False)
2430
+ >>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
2431
+ >>> print(loss)
2432
+ [-45.79497 -55.794968]
2433
+ >>> arr = np.arange(T*C).reshape((T, C))
2434
+ >>> ms_input = Tensor(arr, dtype=mstype.float32)
2435
+ >>> input_lengths = Tensor([T], dtype=mstype.int32)
2436
+ >>> target_lengths = Tensor([S_min], dtype=mstype.int32)
2437
+ >>> target = np.random.randint(1, C, size=(S_min,))
2438
+ >>> target = Tensor(target, dtype=mstype.int32)
2439
+ >>> ctc_loss = CTCLoss(blank=0, reduction='none', zero_infinity=False)
2440
+ >>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
2441
+ >>> print(loss)
2442
+ -25.794968
2443
+ """
2444
+
2445
+ def __init__(self, blank=0, reduction='mean', zero_infinity=False):
2446
+ super().__init__()
2447
+ self.blank = blank
2448
+ self.reduction = reduction
2449
+ self.zero_infinity = zero_infinity
2450
+
2451
+ def construct(self, log_probs, targets, input_lengths, target_lengths):
2452
+ _check_is_tensor('log_probs', log_probs, self.cls_name)
2453
+ _check_is_tensor('targets', targets, self.cls_name)
2454
+ if log_probs.ndim == 2:
2455
+ if targets.ndim > 1:
2456
+ raise ValueError("For CTCLoss, when the shape of log_probs is (T, C), the dimension of targets should"
2457
+ "be equal to one.")
2458
+ log_probs = log_probs.expand_dims(-2)
2459
+ targets = targets.expand_dims(0)
2460
+ neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
2461
+ self.zero_infinity)
2462
+ return neg_log_hood.squeeze()
2463
+ neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
2464
+ self.zero_infinity)
2465
+ return neg_log_hood
2466
+
2467
+
2468
+ class GaussianNLLLoss(LossBase):
2469
+ r"""
2470
+ Gaussian negative log likelihood loss.
2471
+
2472
+ The target values are considered to be samples from a Gaussian distribution, where the expectation and variance are
2473
+ predicted by a neural network. For `labels` modeled on a Gaussian distribution, `logits` to record expectations,
2474
+ and the variance `var` (elements are all positive), the calculated loss is:
2475
+
2476
+ .. math::
2477
+ \text{loss} = \frac{1}{2}\left(\log\left(\text{max}\left(\text{var},
2478
+ \ \text{eps}\right)\right) + \frac{\left(\text{logits} - \text{labels}\right)^2}
2479
+ {\text{max}\left(\text{var}, \ \text{eps}\right)}\right) + \text{const.}
2480
+
2481
+ where :math:`eps` is used for stability of :math:`log`. When :math:`full=True`, a constant will be added to
2482
+ the loss. If the shape of :math:`var` and :math:`logits` are not the same (due to a homoscedastic assumption),
2483
+ their shapes must allow correct broadcasting.
2484
+
2485
+ Keyword Args:
2486
+ full (bool, optional): Whether include the constant term in the loss calculation. When :math:`full=True`,
2487
+ the constant term `const.` will be :math:`0.5 * log(2\pi)`. Default: False.
2488
+ eps (float, optional): Used to improve the stability of log function. Default: 1e-6.
2489
+ reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', or 'sum'.
2490
+ Default: 'mean'.
2491
+
2492
+ Inputs:
2493
+ - **logits** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)` where :math:`*` means any number of
2494
+ additional dimensions.
2495
+ - **labels** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as the logits, or same shape
2496
+ as the logits but with one dimension equal to 1 (to allow for broadcasting).
2497
+ - **var** - Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as logits, or same shape as the logits
2498
+ but with one dimension equal to 1, or same shape as the logits but with one fewer dimension
2499
+ (to allow for broadcasting).
2500
+
2501
+ Returns:
2502
+ Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
2503
+
2504
+ Raises:
2505
+ TypeError: If `logits` is not a Tensor.
2506
+ TypeError: If `labels` is not a Tensor.
2507
+ TypeError: If `full` is not a bool.
2508
+ TypeError: If `eps` is not a float.
2509
+ ValueError: If `eps` is not a float within (0, inf).
2510
+ ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
2511
+
2512
+ Supported Platforms:
2513
+ ``Ascend`` ``GPU`` ``CPU``
2514
+
2515
+ Examples:
2516
+ >>> import numpy as np
2517
+ >>> from mindspore import Tensor
2518
+ >>> import mindspore.nn as nn
2519
+ >>> import mindspore.common.dtype as mstype
2520
+ >>> arr1 = np.arange(8).reshape((4, 2))
2521
+ >>> arr2 = np.array([2, 3, 1, 4, 6, 4, 4, 9]).reshape((4, 2))
2522
+ >>> logits = Tensor(arr1, mstype.float32)
2523
+ >>> labels = Tensor(arr2, mstype.float32)
2524
+ >>> loss = nn.GaussianNLLLoss(reduction='mean')
2525
+ >>> var = Tensor(np.ones((4, 1)), mstype.float32)
2526
+ >>> output = loss(logits, labels, var)
2527
+ >>> print(output)
2528
+ 1.4374993
2529
+
2530
+ Reference:
2531
+ Nix, D. A. and Weigend, A. S., "Estimating the mean and variance of the
2532
+ target probability distribution", Proceedings of 1994 IEEE International
2533
+ Conference on Neural Networks (ICNN'94), Orlando, FL, USA, 1994, pp. 55-60
2534
+ vol.1, doi: 10.1109/ICNN.1994.374138.
2535
+ """
2536
+
2537
+ def __init__(self, *, full=False, eps=1e-6, reduction='mean'):
2538
+ super(GaussianNLLLoss, self).__init__()
2539
+ validator.check_float_range(eps, 0, float('inf'), validator.INC_NEITHER, "eps", self.cls_name)
2540
+ validator.check_value_type('full', full, [bool], self.cls_name)
2541
+ validator.check_string(reduction, ['none', 'mean', 'sum'], 'reduction', 'gaussian_nll_loss')
2542
+ self.full = full
2543
+ self.eps = eps
2544
+ self.reduction = reduction
2545
+
2546
+ def construct(self, logits, labels, var):
2547
+ _check_is_tensor('logits', logits, self.cls_name)
2548
+ _check_is_tensor('labels', labels, self.cls_name)
2549
+ _check_is_tensor('var', var, self.cls_name)
2550
+ return ops.gaussian_nll_loss(logits, labels, var, self.full, self.eps, self.reduction)
2551
+
2552
+
2553
+ class HingeEmbeddingLoss(LossBase):
2554
+ r"""
2555
+ Calculate the Hinge Embedding Loss value based on the input 'logits' and' labels' (only including 1 or -1).
2556
+ Usually used to measure the similarity between two inputs.
2557
+
2558
+ The loss function for :math:`n`-th sample in the mini-batch is
2559
+
2560
+ .. math::
2561
+ l_n = \begin{cases}
2562
+ x_n, & \text{if}\; y_n = 1,\\
2563
+ \max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1,
2564
+ \end{cases}
2565
+
2566
+ and the total loss functions is
2567
+
2568
+ .. math::
2569
+ \ell(x, y) = \begin{cases}
2570
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
2571
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
2572
+ \end{cases}
2573
+
2574
+ where :math:`L = \{l_1,\dots,l_N\}^\top`.
2575
+
2576
+ Args:
2577
+ margin (float, int): Threshold defined by Hinge Embedding Loss :math:`margin`.
2578
+ Represented as :math:`\Delta` in the formula. Default: 1.0.
2579
+ reduction (str): Specify the computing method to be applied to the outputs: 'none', 'mean', or 'sum'.
2580
+ Default: 'mean'.
2581
+
2582
+ Inputs:
2583
+ - **logits** (Tensor) - The predicted value, expressed as :math:`x` in the equation.
2584
+ Tensor of shape :math:`(*)` where :math:`*` means any number of dimensions.
2585
+ - **labels** (Tensor) - Same shape as the logits, contains -1 or 1.
2586
+
2587
+ Returns:
2588
+ Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
2589
+
2590
+ Raises:
2591
+ TypeError: If `logits` is not a Tensor.
2592
+ TypeError: If `labels` is not a Tensor.
2593
+ TypeError: If `margin` is not a float or int.
2594
+ ValueError: If `labels` does not have the same shape as `logits` or they could not broadcast to each other.
2595
+ ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
2596
+
2597
+ Supported Platforms:
2598
+ ``Ascend`` ``GPU`` ``CPU``
2599
+
2600
+ Examples:
2601
+ >>> import numpy as np
2602
+ >>> from mindspore import Tensor
2603
+ >>> import mindspore.nn as nn
2604
+ >>> import mindspore.common.dtype as mstype
2605
+ >>> arr1 = np.array([0.9, -1.2, 2, 0.8, 3.9, 2, 1, 0, -1]).reshape((3, 3))
2606
+ >>> arr2 = np.array([1, 1, -1, 1, -1, 1, -1, 1, 1]).reshape((3, 3))
2607
+ >>> logits = Tensor(arr1, mstype.float32)
2608
+ >>> labels = Tensor(arr2, mstype.float32)
2609
+ >>> loss = nn.HingeEmbeddingLoss(reduction='mean')
2610
+ >>> output = loss(logits, labels)
2611
+ >>> print(output)
2612
+ 0.16666667
2613
+ """
2614
+
2615
+ def __init__(self, margin=1.0, reduction='mean'):
2616
+ super(HingeEmbeddingLoss, self).__init__()
2617
+ validator.check_value_type('margin', margin, [float, int], self.cls_name)
2618
+ validator.check_string(reduction, ['none', 'sum', 'mean'], 'reduction', self.cls_name)
2619
+ self.margin = margin
2620
+ self.reduction = reduction
2621
+
2622
+ def construct(self, logits, labels):
2623
+ loss = ops.hinge_embedding_loss(logits, labels, self.margin, self.reduction)
2624
+ return loss