mindspore 2.1.0__cp39-none-any.whl → 2.2.11__cp39-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 (578) hide show
  1. mindspore/.commit_id +1 -1
  2. mindspore/__init__.py +4 -1
  3. mindspore/_akg/akg/build_module.py +5 -6
  4. mindspore/_akg/akg/composite/build_module.py +139 -22
  5. mindspore/_akg/akg/composite/split_stitch.py +10 -11
  6. mindspore/_akg/akg/ms/info_version_adapt.py +67 -1
  7. mindspore/_akg/akg/tvm/api.py +4 -3
  8. mindspore/_akg/akg/tvm/autotvm/__init__.py +1 -2
  9. mindspore/_akg/akg/tvm/autotvm/graph_tuner/base_graph_tuner.py +1 -5
  10. mindspore/_akg/akg/tvm/autotvm/measure/__init__.py +1 -1
  11. mindspore/_akg/akg/tvm/autotvm/measure/measure.py +1 -10
  12. mindspore/_akg/akg/tvm/autotvm/measure/measure_methods.py +1 -372
  13. mindspore/_akg/akg/tvm/build_module.py +16 -1
  14. mindspore/_akg/akg/tvm/contrib/graph_runtime.py +0 -53
  15. mindspore/_akg/akg/tvm/hybrid/parser.py +7 -6
  16. mindspore/_akg/akg/tvm/ir_builder.py +1 -1
  17. mindspore/_akg/akg/tvm/module.py +1 -2
  18. mindspore/_akg/akg/tvm/stmt.py +2 -2
  19. mindspore/_akg/akg/utils/ascend_profilier/cann_file_parser.py +76 -0
  20. mindspore/_akg/akg/utils/ascend_profilier/file_manager.py +56 -0
  21. mindspore/_akg/akg/utils/ascend_profilier/op_summary_bean.py +23 -0
  22. mindspore/_akg/akg/utils/ascend_profilier/op_summary_headers.py +8 -0
  23. mindspore/_akg/akg/utils/ascend_profilier/op_summary_parser.py +42 -0
  24. mindspore/_akg/akg/utils/ascend_profilier/path_manager.py +65 -0
  25. mindspore/_akg/akg/utils/composite_op_helper.py +16 -12
  26. mindspore/_akg/akg/utils/dump_ascend_meta.py +22 -3
  27. mindspore/_akg/akg/utils/kernel_exec.py +98 -274
  28. mindspore/_akg/akg/utils/result_analysis.py +4 -24
  29. mindspore/_akg/akg/utils/tbe_codegen_utils.py +219 -0
  30. mindspore/_akg/akg/utils/util.py +56 -1
  31. mindspore/_c_dataengine.cpython-39-aarch64-linux-gnu.so +0 -0
  32. mindspore/_c_expression.cpython-39-aarch64-linux-gnu.so +0 -0
  33. mindspore/_c_mindrecord.cpython-39-aarch64-linux-gnu.so +0 -0
  34. mindspore/_check_jit_forbidden_api.py +3 -1
  35. mindspore/_checkparam.py +23 -29
  36. mindspore/_extends/graph_kernel/__init__.py +0 -1
  37. mindspore/_extends/graph_kernel/model/graph_split.py +84 -76
  38. mindspore/_extends/graph_kernel/model/model_builder.py +9 -50
  39. mindspore/_extends/graph_kernel/splitter.py +4 -11
  40. mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +122 -15
  41. mindspore/_extends/parallel_compile/akg_compiler/build_tbe_kernel.py +84 -67
  42. mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +4 -2
  43. mindspore/_extends/parallel_compile/akg_compiler/util.py +10 -7
  44. mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py +2 -2
  45. mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py +6 -5
  46. mindspore/_extends/parallel_compile/tbe_compiler/tbe_job.py +1 -1
  47. mindspore/_extends/parallel_compile/tbe_compiler/tbe_job_manager.py +1 -1
  48. mindspore/_extends/parse/__init__.py +13 -15
  49. mindspore/_extends/parse/namespace.py +7 -33
  50. mindspore/_extends/parse/parser.py +67 -72
  51. mindspore/_extends/parse/resources.py +1 -1
  52. mindspore/_extends/parse/standard_method.py +86 -106
  53. mindspore/_extends/parse/trope.py +1 -1
  54. mindspore/_extends/remote/kernel_build_server.py +25 -7
  55. mindspore/_extends/remote/kernel_build_server_akg_v2.py +55 -0
  56. mindspore/_install_custom.py +43 -0
  57. mindspore/_mindspore_offline_debug.cpython-39-aarch64-linux-gnu.so +0 -0
  58. mindspore/amp.py +47 -11
  59. mindspore/bin/cache_admin +0 -0
  60. mindspore/bin/cache_server +0 -0
  61. mindspore/boost/boost.py +1 -8
  62. mindspore/boost/boost_cell_wrapper.py +3 -2
  63. mindspore/boost/grad_accumulation.py +1 -1
  64. mindspore/boost/group_loss_scale_manager.py +8 -7
  65. mindspore/common/__init__.py +5 -3
  66. mindspore/common/_jit_fallback_utils.py +6 -0
  67. mindspore/common/_register_for_adapter.py +2 -0
  68. mindspore/common/_register_for_tensor.py +2 -2
  69. mindspore/common/_stub_tensor.py +13 -0
  70. mindspore/common/_utils.py +29 -0
  71. mindspore/common/api.py +174 -259
  72. mindspore/common/auto_dynamic_shape.py +494 -0
  73. mindspore/common/dtype.py +18 -11
  74. mindspore/common/dump.py +6 -4
  75. mindspore/common/initializer.py +14 -14
  76. mindspore/common/jit_config.py +33 -15
  77. mindspore/common/lazy_inline.py +126 -7
  78. mindspore/common/mindir_util.py +101 -0
  79. mindspore/common/parameter.py +51 -41
  80. mindspore/common/seed.py +4 -4
  81. mindspore/common/sparse_tensor.py +13 -14
  82. mindspore/common/tensor.py +243 -165
  83. mindspore/communication/__init__.py +7 -4
  84. mindspore/communication/_comm_helper.py +83 -4
  85. mindspore/communication/management.py +152 -84
  86. mindspore/config/op_info.config +14 -3
  87. mindspore/config/super_bar_config.json +4 -2
  88. mindspore/context.py +152 -61
  89. mindspore/dataset/__init__.py +5 -5
  90. mindspore/dataset/audio/__init__.py +2 -2
  91. mindspore/dataset/audio/transforms.py +52 -52
  92. mindspore/dataset/callback/ds_callback.py +16 -2
  93. mindspore/dataset/core/config.py +68 -51
  94. mindspore/dataset/engine/cache_client.py +33 -7
  95. mindspore/dataset/engine/datasets.py +250 -112
  96. mindspore/dataset/engine/datasets_audio.py +43 -211
  97. mindspore/dataset/engine/datasets_standard_format.py +16 -35
  98. mindspore/dataset/engine/datasets_text.py +43 -67
  99. mindspore/dataset/engine/datasets_user_defined.py +86 -100
  100. mindspore/dataset/engine/datasets_vision.py +219 -1029
  101. mindspore/dataset/engine/iterators.py +11 -4
  102. mindspore/dataset/engine/obs/obs_mindrecord_dataset.py +4 -0
  103. mindspore/dataset/engine/obs/util.py +3 -0
  104. mindspore/dataset/engine/samplers.py +1 -1
  105. mindspore/dataset/engine/validators.py +19 -5
  106. mindspore/dataset/text/__init__.py +3 -3
  107. mindspore/dataset/text/transforms.py +101 -127
  108. mindspore/dataset/text/utils.py +205 -138
  109. mindspore/dataset/transforms/__init__.py +1 -1
  110. mindspore/dataset/transforms/py_transforms_util.py +40 -12
  111. mindspore/dataset/transforms/transforms.py +95 -40
  112. mindspore/dataset/utils/browse_dataset.py +8 -2
  113. mindspore/dataset/utils/line_reader.py +17 -19
  114. mindspore/dataset/vision/__init__.py +3 -3
  115. mindspore/dataset/vision/c_transforms.py +6 -3
  116. mindspore/dataset/vision/transforms.py +409 -287
  117. mindspore/dataset/vision/utils.py +13 -14
  118. mindspore/dataset/vision/validators.py +11 -1
  119. mindspore/experimental/map_parameter.py +14 -0
  120. mindspore/{nn/optim_ex → experimental/optim}/__init__.py +30 -29
  121. mindspore/{nn/optim_ex → experimental/optim}/adam.py +60 -67
  122. mindspore/{nn/optim_ex → experimental/optim}/adamw.py +181 -203
  123. mindspore/experimental/optim/lr_scheduler.py +1427 -0
  124. mindspore/{nn/optim_ex → experimental/optim}/optimizer.py +252 -259
  125. mindspore/{nn/optim_ex → experimental/optim}/sgd.py +147 -152
  126. mindspore/gen_ops.py +273 -0
  127. mindspore/include/OWNERS +0 -1
  128. mindspore/include/api/data_type.h +2 -1
  129. mindspore/include/api/graph.h +0 -15
  130. mindspore/include/api/kernel.h +2 -0
  131. mindspore/include/api/kernel_api.h +37 -12
  132. mindspore/include/api/model.h +17 -14
  133. mindspore/include/api/status.h +8 -3
  134. mindspore/include/api/types.h +37 -4
  135. mindspore/include/c_api/ms/abstract.h +67 -0
  136. mindspore/include/c_api/ms/attribute.h +197 -0
  137. mindspore/include/c_api/ms/base/handle_types.h +43 -0
  138. mindspore/include/c_api/ms/base/macros.h +32 -0
  139. mindspore/include/c_api/ms/base/status.h +33 -0
  140. mindspore/include/c_api/ms/base/types.h +282 -0
  141. mindspore/include/c_api/ms/context.h +102 -0
  142. mindspore/include/c_api/ms/graph.h +160 -0
  143. mindspore/include/c_api/ms/node.h +606 -0
  144. mindspore/include/c_api/ms/tensor.h +161 -0
  145. mindspore/include/c_api/ms/value.h +84 -0
  146. mindspore/include/dataset/constants.h +6 -5
  147. mindspore/include/dataset/execute.h +23 -13
  148. mindspore/include/dataset/text.h +26 -26
  149. mindspore/include/dataset/transforms.h +13 -13
  150. mindspore/include/dataset/vision.h +60 -60
  151. mindspore/include/dataset/vision_ascend.h +5 -6
  152. mindspore/include/dataset/vision_lite.h +17 -17
  153. mindspore/include/mindapi/base/type_id.h +1 -0
  154. mindspore/include/mindapi/base/types.h +1 -0
  155. mindspore/lib/libdnnl.so.2 +0 -0
  156. mindspore/lib/libjemalloc.so.2 +0 -0
  157. mindspore/lib/libmindspore.so +0 -0
  158. mindspore/lib/libmindspore_backend.so +0 -0
  159. mindspore/lib/libmindspore_common.so +0 -0
  160. mindspore/lib/libmindspore_core.so +0 -0
  161. mindspore/lib/libmindspore_glog.so.0 +0 -0
  162. mindspore/lib/libmindspore_gpr.so.15 +0 -0
  163. mindspore/lib/libmindspore_grpc++.so.1 +0 -0
  164. mindspore/lib/libmindspore_grpc.so.15 +0 -0
  165. mindspore/lib/libmindspore_shared_lib.so +0 -0
  166. mindspore/lib/libnnacl.so +0 -0
  167. mindspore/lib/libopencv_core.so.4.5 +0 -0
  168. mindspore/lib/libopencv_imgcodecs.so.4.5 +0 -0
  169. mindspore/lib/libopencv_imgproc.so.4.5 +0 -0
  170. mindspore/lib/libps_cache.so +0 -0
  171. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/config/ascend310/aic-ascend310-ops-info.json +123 -0
  172. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/config/ascend310p/aic-ascend310p-ops-info.json +123 -0
  173. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/config/ascend910/aic-ascend910-ops-info.json +158 -0
  174. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/config/ascend910b/aic-ascend910b-ops-info.json +37 -0
  175. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/custom_aicore_ops_impl/add_dsl.py +46 -0
  176. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/custom_aicore_ops_impl/add_tik.py +51 -0
  177. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/custom_aicore_ops_impl/kv_cache_mgr.py +241 -0
  178. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/ai_core/tbe/custom_aicore_ops_impl/matmul_tik.py +212 -0
  179. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/vector_core/tbe/custom_aicore_ops_impl/add_dsl.py +46 -0
  180. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/vector_core/tbe/custom_aicore_ops_impl/add_tik.py +51 -0
  181. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/vector_core/tbe/custom_aicore_ops_impl/kv_cache_mgr.py +241 -0
  182. mindspore/lib/plugin/ascend/custom_aicore_ops/op_impl/vector_core/tbe/custom_aicore_ops_impl/matmul_tik.py +212 -0
  183. mindspore/lib/plugin/ascend/custom_aicore_ops/op_proto/libop_proto.so +0 -0
  184. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_impl/cpu/aicpu_kernel/impl/libcust_aicpu_kernels.so +0 -0
  185. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_impl/cpu/aicpu_kernel/impl/libcust_cpu_kernels.so +0 -0
  186. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_impl/cpu/config/cust_aicpu_kernel.json +8998 -0
  187. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_proto/libcust_op_proto.so +0 -0
  188. mindspore/lib/plugin/ascend/libakg.so +0 -0
  189. mindspore/lib/plugin/ascend/libascend_collective.so +0 -0
  190. mindspore/lib/plugin/ascend/libdvpp_utils.so +0 -0
  191. mindspore/lib/plugin/ascend/libhccl_plugin.so +0 -0
  192. mindspore/lib/plugin/ascend/libmindspore_aicpu_kernels.so +0 -0
  193. mindspore/lib/plugin/ascend/libmindspore_cpu_kernels.so +0 -0
  194. mindspore/lib/plugin/cpu/libakg.so +0 -0
  195. mindspore/lib/plugin/libmindspore_ascend.so.1 +0 -0
  196. mindspore/lib/plugin/libmindspore_ascend.so.2 +0 -0
  197. mindspore/mindrecord/tools/imagenet_to_mr.py +1 -1
  198. mindspore/mindrecord/tools/mnist_to_mr.py +2 -2
  199. mindspore/nn/__init__.py +0 -2
  200. mindspore/nn/cell.py +313 -74
  201. mindspore/nn/dynamic_lr.py +21 -21
  202. mindspore/nn/layer/activation.py +22 -30
  203. mindspore/nn/layer/basic.py +15 -13
  204. mindspore/nn/layer/channel_shuffle.py +1 -1
  205. mindspore/nn/layer/container.py +271 -9
  206. mindspore/nn/layer/conv.py +323 -204
  207. mindspore/nn/layer/dense.py +8 -5
  208. mindspore/nn/layer/embedding.py +33 -27
  209. mindspore/nn/layer/flash_attention.py +61 -95
  210. mindspore/nn/layer/image.py +8 -6
  211. mindspore/nn/layer/math.py +16 -25
  212. mindspore/nn/layer/normalization.py +107 -66
  213. mindspore/nn/layer/padding.py +1 -1
  214. mindspore/nn/layer/pooling.py +131 -109
  215. mindspore/nn/layer/rnn_cells.py +27 -22
  216. mindspore/nn/layer/rnns.py +13 -16
  217. mindspore/nn/layer/thor_layer.py +1 -1
  218. mindspore/nn/layer/transformer.py +221 -154
  219. mindspore/nn/learning_rate_schedule.py +9 -1
  220. mindspore/nn/loss/loss.py +235 -174
  221. mindspore/nn/optim/ada_grad.py +2 -1
  222. mindspore/nn/optim/adadelta.py +1 -0
  223. mindspore/nn/optim/adafactor.py +2 -1
  224. mindspore/nn/optim/adam.py +7 -4
  225. mindspore/nn/optim/adamax.py +3 -2
  226. mindspore/nn/optim/adasum.py +2 -2
  227. mindspore/nn/optim/asgd.py +2 -3
  228. mindspore/nn/optim/ftrl.py +6 -5
  229. mindspore/nn/optim/lamb.py +7 -4
  230. mindspore/nn/optim/lars.py +1 -1
  231. mindspore/nn/optim/lazyadam.py +5 -3
  232. mindspore/nn/optim/momentum.py +2 -1
  233. mindspore/nn/optim/optimizer.py +53 -4
  234. mindspore/nn/optim/proximal_ada_grad.py +3 -4
  235. mindspore/nn/optim/rmsprop.py +4 -3
  236. mindspore/nn/optim/rprop.py +23 -12
  237. mindspore/nn/optim/sgd.py +26 -11
  238. mindspore/nn/optim/thor.py +9 -7
  239. mindspore/nn/probability/bijector/bijector.py +5 -5
  240. mindspore/nn/probability/bijector/power_transform.py +27 -27
  241. mindspore/nn/probability/bijector/softplus.py +3 -3
  242. mindspore/nn/probability/distribution/_utils/custom_ops.py +3 -3
  243. mindspore/nn/probability/distribution/bernoulli.py +5 -5
  244. mindspore/nn/probability/distribution/beta.py +3 -3
  245. mindspore/nn/probability/distribution/categorical.py +7 -7
  246. mindspore/nn/probability/distribution/cauchy.py +0 -1
  247. mindspore/nn/probability/distribution/distribution.py +3 -3
  248. mindspore/nn/probability/distribution/gamma.py +3 -3
  249. mindspore/nn/probability/distribution/geometric.py +4 -4
  250. mindspore/nn/probability/distribution/gumbel.py +4 -4
  251. mindspore/nn/probability/distribution/log_normal.py +2 -2
  252. mindspore/nn/probability/distribution/logistic.py +2 -2
  253. mindspore/nn/probability/distribution/poisson.py +4 -4
  254. mindspore/nn/probability/distribution/transformed_distribution.py +3 -3
  255. mindspore/nn/probability/distribution/uniform.py +6 -6
  256. mindspore/nn/wrap/__init__.py +4 -2
  257. mindspore/nn/wrap/cell_wrapper.py +87 -34
  258. mindspore/nn/wrap/grad_reducer.py +8 -5
  259. mindspore/nn/wrap/loss_scale.py +105 -42
  260. mindspore/numpy/array_creations.py +1 -2
  261. mindspore/numpy/array_ops.py +3 -2
  262. mindspore/numpy/utils_const.py +5 -5
  263. mindspore/offline_debug/convert_async.py +2 -2
  264. mindspore/ops/_grad_experimental/__init__.py +0 -5
  265. mindspore/ops/_grad_experimental/grad_array_ops.py +2 -3
  266. mindspore/ops/_grad_experimental/grad_comm_ops.py +15 -2
  267. mindspore/ops/_grad_experimental/grad_debug_ops.py +0 -37
  268. mindspore/ops/_grad_experimental/grad_implementations.py +11 -1
  269. mindspore/ops/_grad_experimental/grad_inner_ops.py +2 -216
  270. mindspore/ops/_grad_experimental/grad_math_ops.py +19 -199
  271. mindspore/ops/_grad_experimental/grad_sparse.py +15 -0
  272. mindspore/ops/_grad_experimental/grad_sparse_ops.py +3 -3
  273. mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +1 -1
  274. mindspore/ops/_op_impl/aicpu/__init__.py +14 -2
  275. mindspore/ops/_op_impl/aicpu/add.py +3 -3
  276. mindspore/ops/_op_impl/aicpu/bias_add_grad.py +0 -1
  277. mindspore/ops/_op_impl/aicpu/count_nonzero.py +43 -0
  278. mindspore/ops/_op_impl/{_custom_op/flash_attention/constants.py → aicpu/eps.py} +18 -27
  279. mindspore/ops/_op_impl/aicpu/gamma.py +2 -2
  280. mindspore/ops/_op_impl/aicpu/linear_sum_assignment.py +21 -2
  281. mindspore/ops/_op_impl/aicpu/log_uniform_candidate_sampler.py +6 -3
  282. mindspore/ops/_op_impl/aicpu/lu_unpack_grad.py +0 -1
  283. mindspore/ops/_op_impl/aicpu/multinomial.py +3 -3
  284. mindspore/ops/_op_impl/aicpu/parameterized_truncated_normal.py +15 -7
  285. mindspore/ops/_op_impl/aicpu/random_categorical.py +39 -19
  286. mindspore/ops/_op_impl/aicpu/random_choice_with_mask.py +5 -2
  287. mindspore/ops/_op_impl/aicpu/random_poisson.py +103 -52
  288. mindspore/ops/_op_impl/aicpu/random_shuffle.py +17 -15
  289. mindspore/ops/_op_impl/aicpu/{sparseaddmm.py → sparse_addmm.py} +2 -2
  290. mindspore/ops/_op_impl/aicpu/{sparsesparsemaximum.py → sparse_sparse_maximum.py} +4 -4
  291. mindspore/ops/_op_impl/aicpu/standard_laplace.py +5 -5
  292. mindspore/ops/_op_impl/aicpu/standard_normal.py +5 -5
  293. mindspore/ops/_op_impl/aicpu/truncated_normal.py +9 -7
  294. mindspore/ops/_op_impl/aicpu/uniform.py +5 -3
  295. mindspore/ops/_op_impl/aicpu/uniform_candidate_sampler.py +8 -4
  296. mindspore/ops/_op_impl/aicpu/uniform_int.py +5 -5
  297. mindspore/ops/_op_impl/aicpu/uniform_real.py +4 -4
  298. mindspore/ops/_op_impl/tbe/__init__.py +4 -4
  299. mindspore/ops/_op_impl/tbe/inplace_index_add.py +7 -3
  300. mindspore/ops/_op_impl/tbe/trans_data_ds.py +2 -0
  301. mindspore/ops/_primitive_cache.py +1 -1
  302. mindspore/ops/_tracefunc.py +45 -13
  303. mindspore/ops/_utils/utils.py +6 -1
  304. mindspore/ops/_vmap/vmap_array_ops.py +3 -3
  305. mindspore/ops/_vmap/vmap_base.py +3 -3
  306. mindspore/ops/_vmap/vmap_convolution_ops.py +1 -1
  307. mindspore/ops/_vmap/vmap_grad_math_ops.py +6 -4
  308. mindspore/ops/_vmap/vmap_math_ops.py +5 -2
  309. mindspore/ops/_vmap/vmap_nn_ops.py +61 -7
  310. mindspore/ops/arg_dtype_cast.py +54 -0
  311. mindspore/ops/composite/base.py +37 -10
  312. mindspore/ops/composite/math_ops.py +5 -4
  313. mindspore/ops/composite/multitype_ops/_compile_utils.py +275 -73
  314. mindspore/ops/composite/multitype_ops/_constexpr_utils.py +16 -9
  315. mindspore/ops/composite/multitype_ops/add_impl.py +43 -4
  316. mindspore/ops/composite/multitype_ops/getitem_impl.py +42 -4
  317. mindspore/ops/composite/multitype_ops/ones_like_impl.py +6 -0
  318. mindspore/ops/composite/multitype_ops/setitem_impl.py +2 -1
  319. mindspore/ops/composite/multitype_ops/zeros_like_impl.py +9 -0
  320. mindspore/ops/deprecated.py +304 -0
  321. mindspore/ops/function/__init__.py +4 -1
  322. mindspore/ops/function/array_func.py +174 -193
  323. mindspore/ops/function/clip_func.py +81 -13
  324. mindspore/ops/function/debug_func.py +1 -1
  325. mindspore/ops/function/grad/grad_func.py +18 -9
  326. mindspore/ops/function/image_func.py +10 -4
  327. mindspore/ops/function/linalg_func.py +5 -5
  328. mindspore/ops/function/math_func.py +575 -386
  329. mindspore/ops/function/nn_func.py +568 -260
  330. mindspore/ops/function/random_func.py +88 -57
  331. mindspore/ops/function/sparse_func.py +1 -1
  332. mindspore/ops/function/sparse_unary_func.py +14 -12
  333. mindspore/ops/function/vmap_func.py +6 -5
  334. mindspore/ops/functional.py +15 -10
  335. mindspore/ops/op_info_register.py +244 -25
  336. mindspore/ops/operations/__init__.py +31 -19
  337. mindspore/ops/operations/_grad_ops.py +71 -7
  338. mindspore/ops/operations/_inner_ops.py +350 -17
  339. mindspore/ops/operations/_quant_ops.py +4 -8
  340. mindspore/ops/operations/_sequence_ops.py +42 -0
  341. mindspore/ops/operations/array_ops.py +68 -282
  342. mindspore/ops/operations/comm_ops.py +107 -59
  343. mindspore/ops/operations/custom_ops.py +94 -70
  344. mindspore/ops/operations/debug_ops.py +8 -4
  345. mindspore/ops/operations/image_ops.py +18 -12
  346. mindspore/ops/operations/inner_ops.py +26 -3
  347. mindspore/ops/operations/math_ops.py +192 -144
  348. mindspore/ops/operations/nn_ops.py +857 -489
  349. mindspore/ops/operations/other_ops.py +0 -22
  350. mindspore/ops/operations/random_ops.py +53 -111
  351. mindspore/ops/operations/sparse_ops.py +3 -1
  352. mindspore/ops/primitive.py +24 -18
  353. mindspore/parallel/_auto_parallel_context.py +68 -8
  354. mindspore/parallel/_cost_model_context.py +2 -2
  355. mindspore/parallel/_offload_context.py +17 -3
  356. mindspore/parallel/_parallel_serialization.py +12 -5
  357. mindspore/parallel/_ps_context.py +12 -0
  358. mindspore/parallel/_tensor.py +18 -13
  359. mindspore/parallel/_transformer/layers.py +5 -3
  360. mindspore/parallel/_transformer/loss.py +1 -0
  361. mindspore/parallel/_transformer/moe.py +2 -2
  362. mindspore/parallel/_transformer/op_parallel_config.py +12 -1
  363. mindspore/parallel/_transformer/transformer.py +23 -3
  364. mindspore/parallel/_utils.py +11 -7
  365. mindspore/parallel/algo_parameter_config.py +85 -5
  366. mindspore/parallel/checkpoint_transform.py +19 -12
  367. mindspore/parallel/shard.py +21 -14
  368. mindspore/profiler/common/struct_type.py +3 -3
  369. mindspore/profiler/common/util.py +4 -2
  370. mindspore/profiler/envprofiling.py +1 -1
  371. mindspore/profiler/parser/aicpu_data_parser.py +5 -3
  372. mindspore/profiler/parser/ascend_flops_generator.py +2 -2
  373. mindspore/profiler/parser/ascend_fpbp_generator.py +1 -1
  374. mindspore/profiler/parser/ascend_hccl_generator.py +249 -12
  375. mindspore/profiler/parser/ascend_msprof_exporter.py +150 -255
  376. mindspore/profiler/parser/ascend_msprof_generator.py +204 -17
  377. mindspore/profiler/parser/ascend_op_generator.py +6 -6
  378. mindspore/profiler/parser/ascend_steptrace_generator.py +6 -4
  379. mindspore/profiler/parser/ascend_timeline_generator.py +14 -187
  380. mindspore/profiler/parser/base_timeline_generator.py +10 -8
  381. mindspore/profiler/parser/cpu_gpu_timeline_generator.py +16 -12
  382. mindspore/profiler/parser/flops_parser.py +15 -11
  383. mindspore/profiler/parser/framework_parser.py +38 -22
  384. mindspore/profiler/parser/hccl_parser.py +16 -12
  385. mindspore/profiler/parser/integrator.py +22 -11
  386. mindspore/profiler/parser/memory_usage_parser.py +2 -2
  387. mindspore/profiler/parser/minddata_analyzer.py +12 -14
  388. mindspore/profiler/parser/minddata_pipeline_parser.py +1 -1
  389. mindspore/profiler/parser/msadvisor_parser.py +8 -4
  390. mindspore/profiler/parser/op_intermediate_parser.py +5 -2
  391. mindspore/profiler/parser/optime_parser.py +1 -1
  392. mindspore/profiler/parser/profiler_info.py +21 -2
  393. mindspore/profiler/parser/step_trace_parser.py +11 -14
  394. mindspore/profiler/profiling.py +179 -89
  395. mindspore/rewrite/api/node.py +102 -19
  396. mindspore/rewrite/api/node_type.py +5 -1
  397. mindspore/rewrite/api/pattern_engine.py +1 -1
  398. mindspore/rewrite/api/scoped_value.py +9 -17
  399. mindspore/rewrite/api/symbol_tree.py +131 -47
  400. mindspore/rewrite/ast_helpers/__init__.py +2 -1
  401. mindspore/rewrite/ast_helpers/ast_finder.py +129 -0
  402. mindspore/rewrite/ast_helpers/ast_modifier.py +116 -104
  403. mindspore/rewrite/ast_transformers/flatten_recursive_stmt.py +93 -46
  404. mindspore/rewrite/common/rewrite_elog.py +5 -1
  405. mindspore/rewrite/namer.py +33 -24
  406. mindspore/rewrite/namespace.py +14 -5
  407. mindspore/{_extends/graph_kernel/expanders/complex → rewrite/node}/__init__.py +9 -9
  408. mindspore/rewrite/node/call_function.py +79 -0
  409. mindspore/rewrite/node/cell_container.py +135 -0
  410. mindspore/rewrite/node/control_flow.py +88 -0
  411. mindspore/rewrite/{node.py → node/node.py} +273 -234
  412. mindspore/rewrite/node/node_manager.py +254 -0
  413. mindspore/rewrite/{topological_manager.py → node/node_topological_manager.py} +13 -46
  414. mindspore/rewrite/parsers/arguments_parser.py +22 -21
  415. mindspore/rewrite/parsers/assign_parser.py +216 -221
  416. mindspore/rewrite/parsers/attribute_parser.py +9 -7
  417. mindspore/rewrite/parsers/class_def_parser.py +174 -113
  418. mindspore/rewrite/parsers/constant_parser.py +9 -6
  419. mindspore/rewrite/parsers/container_parser.py +9 -7
  420. mindspore/rewrite/parsers/for_parser.py +42 -21
  421. mindspore/rewrite/parsers/function_def_parser.py +24 -16
  422. mindspore/rewrite/parsers/if_parser.py +28 -24
  423. mindspore/rewrite/parsers/module_parser.py +196 -25
  424. mindspore/rewrite/{parser.py → parsers/parser.py} +4 -2
  425. mindspore/rewrite/{parser_register.py → parsers/parser_register.py} +1 -1
  426. mindspore/rewrite/parsers/return_parser.py +6 -6
  427. mindspore/rewrite/sparsify/sparse_transformer.py +12 -3
  428. mindspore/rewrite/sparsify/utils.py +1 -1
  429. mindspore/rewrite/symbol_tree.py +523 -578
  430. mindspore/rewrite/symbol_tree_builder.py +9 -193
  431. mindspore/rewrite/symbol_tree_dumper.py +2 -2
  432. mindspore/run_check/_check_version.py +6 -4
  433. mindspore/{ops/bprop_mindir → safeguard}/__init__.py +4 -3
  434. mindspore/safeguard/rewrite_obfuscation.py +541 -0
  435. mindspore/scipy/linalg.py +1 -1
  436. mindspore/scipy/ops.py +55 -5
  437. mindspore/scipy/optimize/__init__.py +3 -2
  438. mindspore/scipy/optimize/linear_sum_assignment.py +38 -33
  439. mindspore/scipy/optimize/minimize.py +7 -3
  440. mindspore/train/_utils.py +7 -3
  441. mindspore/train/amp.py +323 -123
  442. mindspore/train/anf_ir_pb2.py +14 -2
  443. mindspore/train/callback/_backup_and_restore.py +2 -12
  444. mindspore/train/callback/_callback.py +29 -4
  445. mindspore/train/callback/_checkpoint.py +23 -8
  446. mindspore/train/callback/_early_stop.py +2 -2
  447. mindspore/train/callback/_landscape.py +4 -4
  448. mindspore/train/callback/_loss_monitor.py +2 -2
  449. mindspore/train/callback/_on_request_exit.py +2 -2
  450. mindspore/train/callback/_reduce_lr_on_plateau.py +3 -4
  451. mindspore/train/callback/_summary_collector.py +15 -8
  452. mindspore/train/callback/_time_monitor.py +58 -5
  453. mindspore/train/data_sink.py +5 -11
  454. mindspore/train/dataset_helper.py +84 -57
  455. mindspore/train/loss_scale_manager.py +2 -2
  456. mindspore/train/metrics/__init__.py +3 -3
  457. mindspore/train/metrics/cosine_similarity.py +1 -1
  458. mindspore/train/metrics/hausdorff_distance.py +3 -2
  459. mindspore/train/metrics/mean_surface_distance.py +3 -2
  460. mindspore/train/metrics/metric.py +39 -19
  461. mindspore/train/metrics/roc.py +2 -2
  462. mindspore/train/metrics/root_mean_square_surface_distance.py +4 -3
  463. mindspore/train/mind_ir_pb2.py +85 -36
  464. mindspore/train/model.py +187 -47
  465. mindspore/train/serialization.py +487 -161
  466. mindspore/train/summary/_summary_adapter.py +1 -1
  467. mindspore/train/summary/_writer_pool.py +3 -2
  468. mindspore/train/summary/summary_record.py +37 -17
  469. mindspore/train/train_thor/convert_utils.py +3 -3
  470. mindspore/train/train_thor/dataset_helper.py +1 -1
  471. mindspore/version.py +1 -1
  472. {mindspore-2.1.0.dist-info → mindspore-2.2.11.dist-info}/METADATA +8 -8
  473. {mindspore-2.1.0.dist-info → mindspore-2.2.11.dist-info}/RECORD +477 -528
  474. {mindspore-2.1.0.dist-info → mindspore-2.2.11.dist-info}/entry_points.txt +0 -1
  475. mindspore/_akg/akg/tvm/contrib/debugger/__init__.py +0 -16
  476. mindspore/_akg/akg/tvm/contrib/debugger/debug_result.py +0 -274
  477. mindspore/_akg/akg/tvm/contrib/debugger/debug_runtime.py +0 -259
  478. mindspore/_akg/akg/tvm/contrib/peak.py +0 -341
  479. mindspore/_akg/akg/tvm/contrib/rpc.py +0 -25
  480. mindspore/_akg/akg/tvm/contrib/xcode.py +0 -257
  481. mindspore/_akg/akg/tvm/exec/__init__.py +0 -17
  482. mindspore/_akg/akg/tvm/exec/autotvm_log_editor.py +0 -60
  483. mindspore/_akg/akg/tvm/exec/measure_peak.py +0 -48
  484. mindspore/_akg/akg/tvm/exec/query_rpc_tracker.py +0 -48
  485. mindspore/_akg/akg/tvm/exec/rpc_proxy.py +0 -98
  486. mindspore/_akg/akg/tvm/exec/rpc_server.py +0 -88
  487. mindspore/_akg/akg/tvm/exec/rpc_tracker.py +0 -62
  488. mindspore/_akg/akg/tvm/rpc/__init__.py +0 -29
  489. mindspore/_akg/akg/tvm/rpc/base.py +0 -182
  490. mindspore/_akg/akg/tvm/rpc/client.py +0 -436
  491. mindspore/_akg/akg/tvm/rpc/proxy.py +0 -595
  492. mindspore/_akg/akg/tvm/rpc/server.py +0 -413
  493. mindspore/_akg/akg/tvm/rpc/tornado_util.py +0 -121
  494. mindspore/_akg/akg/tvm/rpc/tracker.py +0 -431
  495. mindspore/_extends/graph_kernel/expander.py +0 -80
  496. mindspore/_extends/graph_kernel/expanders/__init__.py +0 -54
  497. mindspore/_extends/graph_kernel/expanders/_utils.py +0 -269
  498. mindspore/_extends/graph_kernel/expanders/addn.py +0 -33
  499. mindspore/_extends/graph_kernel/expanders/batchnorm.py +0 -152
  500. mindspore/_extends/graph_kernel/expanders/batchnorm_grad.py +0 -105
  501. mindspore/_extends/graph_kernel/expanders/clip_by_norm_no_div_sum.py +0 -33
  502. mindspore/_extends/graph_kernel/expanders/complex/abs.py +0 -30
  503. mindspore/_extends/graph_kernel/expanders/complex/add.py +0 -44
  504. mindspore/_extends/graph_kernel/expanders/complex/div.py +0 -62
  505. mindspore/_extends/graph_kernel/expanders/complex/mul.py +0 -52
  506. mindspore/_extends/graph_kernel/expanders/complex/real_div.py +0 -62
  507. mindspore/_extends/graph_kernel/expanders/complex/sub.py +0 -45
  508. mindspore/_extends/graph_kernel/expanders/conv2d.py +0 -200
  509. mindspore/_extends/graph_kernel/expanders/dropout_grad.py +0 -30
  510. mindspore/_extends/graph_kernel/expanders/equal_count.py +0 -50
  511. mindspore/_extends/graph_kernel/expanders/erfc.py +0 -35
  512. mindspore/_extends/graph_kernel/expanders/expand_dims.py +0 -50
  513. mindspore/_extends/graph_kernel/expanders/fused_adam.py +0 -44
  514. mindspore/_extends/graph_kernel/expanders/fused_adam_weight_decay.py +0 -47
  515. mindspore/_extends/graph_kernel/expanders/fused_mul_add.py +0 -28
  516. mindspore/_extends/graph_kernel/expanders/gelu_grad.py +0 -70
  517. mindspore/_extends/graph_kernel/expanders/gkdropout.py +0 -40
  518. mindspore/_extends/graph_kernel/expanders/identity.py +0 -25
  519. mindspore/_extends/graph_kernel/expanders/layernorm.py +0 -93
  520. mindspore/_extends/graph_kernel/expanders/layernorm_grad.py +0 -113
  521. mindspore/_extends/graph_kernel/expanders/logsoftmax.py +0 -46
  522. mindspore/_extends/graph_kernel/expanders/logsoftmax_grad.py +0 -36
  523. mindspore/_extends/graph_kernel/expanders/matmul.py +0 -80
  524. mindspore/_extends/graph_kernel/expanders/maximum_grad.py +0 -59
  525. mindspore/_extends/graph_kernel/expanders/minimum_grad.py +0 -80
  526. mindspore/_extends/graph_kernel/expanders/oneslike.py +0 -26
  527. mindspore/_extends/graph_kernel/expanders/reduce_mean.py +0 -43
  528. mindspore/_extends/graph_kernel/expanders/relu_grad.py +0 -32
  529. mindspore/_extends/graph_kernel/expanders/sigmoid_cross_entropy_with_logits.py +0 -41
  530. mindspore/_extends/graph_kernel/expanders/sigmoid_cross_entropy_with_logits_grad.py +0 -35
  531. mindspore/_extends/graph_kernel/expanders/sigmoid_grad.py +0 -31
  532. mindspore/_extends/graph_kernel/expanders/slice.py +0 -35
  533. mindspore/_extends/graph_kernel/expanders/softmax_cross_entropy_with_logits.py +0 -42
  534. mindspore/_extends/graph_kernel/expanders/softmax_grad_ext.py +0 -41
  535. mindspore/_extends/graph_kernel/expanders/softsign.py +0 -28
  536. mindspore/_extends/graph_kernel/expanders/sqrt_grad.py +0 -29
  537. mindspore/_extends/graph_kernel/expanders/square_sum_all.py +0 -44
  538. mindspore/_extends/graph_kernel/expanders/square_sum_v1.py +0 -37
  539. mindspore/_extends/graph_kernel/expanders/squared_difference.py +0 -43
  540. mindspore/_extends/graph_kernel/expanders/tanh_grad.py +0 -31
  541. mindspore/_extends/graph_kernel/model/op_infer.py +0 -506
  542. mindspore/dataset/datapreprocess/__init__.py +0 -20
  543. mindspore/dataset/datapreprocess/preprocess_imagenet_validate_dataset.py +0 -54
  544. mindspore/include/api/net.h +0 -142
  545. mindspore/nn/lr_scheduler.py +0 -262
  546. mindspore/ops/_grad_experimental/grad_image_ops.py +0 -248
  547. mindspore/ops/_grad_experimental/grad_linalg_ops.py +0 -181
  548. mindspore/ops/_grad_experimental/grad_other_ops.py +0 -72
  549. mindspore/ops/_grad_experimental/grad_scalar_ops.py +0 -112
  550. mindspore/ops/_grad_experimental/grad_sequence_ops.py +0 -351
  551. mindspore/ops/_op_impl/_custom_op/flash_attention/attention.py +0 -350
  552. mindspore/ops/_op_impl/_custom_op/flash_attention/flash_attention_bwd.py +0 -409
  553. mindspore/ops/_op_impl/_custom_op/flash_attention/flash_attention_fwd.py +0 -578
  554. mindspore/ops/_op_impl/_custom_op/flash_attention/flash_attention_impl.py +0 -199
  555. mindspore/ops/_op_impl/_custom_op/flash_attention/tik_ops_utils.py +0 -446
  556. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/__init__.py +0 -0
  557. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/sparse_tiling.py +0 -45
  558. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/strategy.py +0 -67
  559. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/wukong_tiling.py +0 -62
  560. mindspore/ops/bprop_mindir/BNTrainingReduce_bprop.mindir +0 -0
  561. mindspore/ops/bprop_mindir/Broadcast_bprop.mindir +0 -0
  562. mindspore/ops/bprop_mindir/Depend_bprop.mindir +0 -0
  563. mindspore/ops/bprop_mindir/DepthwiseConv2dNative_bprop.mindir +0 -138
  564. mindspore/ops/bprop_mindir/EmbeddingLookup_bprop.mindir +0 -0
  565. mindspore/ops/bprop_mindir/Load_bprop.mindir +0 -0
  566. mindspore/ops/bprop_mindir/ScatterNonAliasingAdd_bprop.mindir +0 -0
  567. mindspore/ops/bprop_mindir/SparseGatherV2_bprop.mindir +0 -0
  568. mindspore/ops/bprop_mindir/SparseSoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
  569. mindspore/ops/bprop_mindir/Switch_bprop.mindir +0 -0
  570. mindspore/ops/bprop_mindir/TransShape_bprop.mindir +0 -0
  571. mindspore/ops/bprop_mindir/TupleGetItem_bprop.mindir +0 -0
  572. mindspore/ops/bprop_mindir/Unique_bprop.mindir +0 -0
  573. mindspore/ops/bprop_mindir/Unstack_bprop.mindir +0 -0
  574. mindspore/ops/bprop_mindir/generate_mindir.py +0 -114
  575. mindspore/rewrite/node_visitor.py +0 -44
  576. /mindspore/{ops/_op_impl/_custom_op/flash_attention → _akg/akg/utils/ascend_profilier}/__init__.py +0 -0
  577. {mindspore-2.1.0.dist-info → mindspore-2.2.11.dist-info}/WHEEL +0 -0
  578. {mindspore-2.1.0.dist-info → mindspore-2.2.11.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1427 @@
1
+ # Copyright 2023 Huawei Technologies Co., Ltd
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ============================================================================
15
+ """LRScheduler."""
16
+ from collections import Counter
17
+ from bisect import bisect_right
18
+ import math
19
+ from mindspore import ops, Tensor, Parameter
20
+ from mindspore.experimental.optim.optimizer import Optimizer
21
+ from mindspore.common.api import jit_class
22
+ import mindspore.common.dtype as mstype
23
+ from mindspore.ops import functional as F
24
+ from mindspore.ops import operations as P
25
+ from mindspore import _checkparam as Validator
26
+
27
+
28
+ __all__ = ['StepLR', 'LinearLR', 'LRScheduler', 'ExponentialLR', 'PolynomialLR', 'ChainedScheduler',
29
+ 'MultiplicativeLR', 'ConstantLR', 'MultiStepLR', 'LambdaLR', 'SequentialLR', 'ReduceLROnPlateau',
30
+ 'CyclicLR', 'CosineAnnealingWarmRestarts', 'CosineAnnealingLR']
31
+
32
+
33
+ @jit_class
34
+ class LRScheduler:
35
+ r"""
36
+ Basic class of learning rate schedule.
37
+
38
+ .. warning::
39
+ This is an experimental lr scheduler module that is subject to change.
40
+ This module must be used with optimizers in `Experimental Optimizer
41
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
42
+
43
+ Args:
44
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): The optimizer instance.
45
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
46
+
47
+ Raises:
48
+ TypeError: If `optimizer` is not an Optimizer.
49
+ KeyError: If `last_epoch` != -1 and ``'initial_lr'`` not in param groups.
50
+ ValueError: if `last_epoch` is not int.
51
+ ValueError: If `last_epoch` is not greater than -1.
52
+
53
+ Supported Platforms:
54
+ ``Ascend`` ``GPU`` ``CPU``
55
+
56
+ Examples:
57
+ >>> from mindspore import nn
58
+ >>> from mindspore.experimental import optim
59
+ >>>
60
+ >>> class ConstantLR(optim.lr_scheduler.LRScheduler):
61
+ ... def __init__(self, optimizer, factor=0.5, total_iters=3, last_epoch=-1):
62
+ ... self.factor = factor
63
+ ... self.total_iters = total_iters
64
+ ... super(ConstantLR, self).__init__(optimizer, last_epoch)
65
+ ...
66
+ ... def get_lr(self):
67
+ ... lrs = [lr.value() for lr in self._last_lr]
68
+ ... if self.last_epoch == 0:
69
+ ... return [lr * self.factor for lr in lrs]
70
+ ... if self.last_epoch != self.total_iters:
71
+ ... return lrs
72
+ ... return sreturn [lr / self.factor for lr in lrs]
73
+ >>>
74
+ >>> net = nn.Dense(8, 2)
75
+ >>> optimizer = optim.SGD(net.trainable_params(), 0.01)
76
+ >>> scheduler = ConstantLR(optimizer)
77
+ >>> for i in range(4):
78
+ ... scheduler.step()
79
+ ... current_lr = scheduler.get_last_lr()
80
+ ... print(current_lr)
81
+ [Tensor(shape=[], dtype=Float32, value= 0.005)]
82
+ [Tensor(shape=[], dtype=Float32, value= 0.005)]
83
+ [Tensor(shape=[], dtype=Float32, value= 0.01)]
84
+ [Tensor(shape=[], dtype=Float32, value= 0.01)]
85
+ """
86
+ def __init__(self, optimizer, last_epoch=-1):
87
+ if not isinstance(optimizer, Optimizer):
88
+ raise TypeError('{} is not an Optimizer'.format(
89
+ type(optimizer).__name__))
90
+ Validator.check_value_type("last_epoch", last_epoch, [int])
91
+ if last_epoch < -1:
92
+ raise ValueError("Invalid last_epoch: {}".format(last_epoch))
93
+ if last_epoch == -1:
94
+ for group in optimizer.param_groups:
95
+ group.setdefault('initial_lr', group['lr'].value())
96
+ else:
97
+ for i, group in enumerate(optimizer.param_groups):
98
+ if 'initial_lr' not in group:
99
+ raise KeyError(f"param 'initial_lr' is not specified "
100
+ f"in param_groups[{i}] when resuming an optimizer")
101
+ self.base_lrs = [group['initial_lr'] for group in optimizer.param_groups]
102
+ self.optimizer = optimizer
103
+ self._last_lr = [group['lr'] for group in optimizer.param_groups]
104
+ self.groups_num = len(optimizer.param_groups)
105
+ self.last_epoch = Parameter(Tensor(last_epoch, dtype=mstype.float32),
106
+ name='last_epoch_' + self.__class__.__name__)
107
+ self.increase_tensor = Tensor(1, mstype.int32)
108
+ self.step()
109
+
110
+ @staticmethod
111
+ def get_lr():
112
+ raise NotImplementedError
113
+
114
+ def get_last_lr(self):
115
+ """
116
+ Return last computed learning rate by current scheduler.
117
+ """
118
+ return [lr.value() for lr in self._last_lr]
119
+
120
+ def step(self, epoch=None):
121
+ """
122
+ Get the current learning rate and change the learning rate.
123
+
124
+ Args:
125
+ epoch (int, optional): The index of the last epoch. Default: ``None``.
126
+ """
127
+ if epoch is None:
128
+ ops.assign_add(self.last_epoch, self.increase_tensor)
129
+ values = self.get_lr()
130
+ else:
131
+ ops.assign(self.last_epoch, epoch)
132
+ if hasattr(self, "_get_closed_form_lr"):
133
+ values = self._get_closed_form_lr()
134
+ else:
135
+ values = self.get_lr()
136
+
137
+ for i in range(self.groups_num):
138
+ lr = values[i]
139
+ ops.assign(self.optimizer.param_groups[i]["lr"], lr)
140
+
141
+ return True
142
+
143
+
144
+ @jit_class
145
+ class StepLR(LRScheduler):
146
+ """Decays the learning rate of each parameter group by gamma every
147
+ step_size epochs. Notice that such decay can happen simultaneously with
148
+ other changes to the learning rate from outside this scheduler.
149
+
150
+ .. warning::
151
+ This is an experimental lr scheduler module that is subject to change.
152
+ This module must be used with optimizers in `Experimental Optimizer
153
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
154
+
155
+ Args:
156
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
157
+ step_size (int): Period of learning rate decay.
158
+ gamma (float, optional): Multiplicative factor of learning rate decay.
159
+ Default: ``0.5``.
160
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
161
+
162
+ Supported Platforms:
163
+ ``Ascend`` ``GPU`` ``CPU``
164
+
165
+ Examples:
166
+ >>> import mindspore
167
+ >>> from mindspore import nn
168
+ >>> from mindspore.experimental import optim
169
+ >>> # Define the network structure of LeNet5. Refer to
170
+ >>> # https://gitee.com/mindspore/docs/blob/r2.2/docs/mindspore/code/lenet.py
171
+ >>> net = LeNet5()
172
+ >>> loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
173
+ >>> optimizer = optim.Adam(net.trainable_params(), lr=0.05)
174
+ >>> # Assuming optimizer uses lr = 0.05 for all groups
175
+ >>> # lr = 0.05 if epoch < 2
176
+ >>> # lr = 0.005 if 2 <= epoch < 4
177
+ >>> # lr = 0.0005 if 4 <= epoch < 6
178
+ >>> scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.1)
179
+ >>> def forward_fn(data, label):
180
+ ... logits = net(data)
181
+ ... loss = loss_fn(logits, label)
182
+ ... return loss, logits
183
+ >>> grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)
184
+ >>> def train_step(data, label):
185
+ ... (loss, _), grads = grad_fn(data, label)
186
+ ... optimizer(grads)
187
+ ... return loss
188
+ >>> for epoch in range(6):
189
+ ... # Create the dataset taking MNIST as an example. Refer to
190
+ ... # https://gitee.com/mindspore/docs/blob/r2.2/docs/mindspore/code/mnist.py
191
+ ... for data, label in create_dataset():
192
+ ... train_step(data, label)
193
+ ... scheduler.step()
194
+ ... current_lr = scheduler.get_last_lr()
195
+ """
196
+ def __init__(self, optimizer, step_size, gamma=0.1, last_epoch=-1):
197
+ self.step_size = step_size
198
+ self.gamma = gamma
199
+ super(StepLR, self).__init__(optimizer, last_epoch)
200
+
201
+ def get_lr(self):
202
+ lrs = [lr.value() for lr in self._last_lr]
203
+ if self.last_epoch == 0 or self.last_epoch % self.step_size != 0:
204
+ return lrs
205
+ return [lr * self.gamma for lr in lrs]
206
+
207
+ def _get_closed_form_lr(self):
208
+ return [base_lr * self.gamma ** (self.last_epoch // self.step_size)
209
+ for base_lr in self.base_lrs]
210
+
211
+
212
+ @jit_class
213
+ class LinearLR(LRScheduler):
214
+ """Decays the learning rate of each parameter group by linearly changing small
215
+ multiplicative factor until the number of epoch reaches a pre-defined milestone: total_iters.
216
+ Notice that such decay can happen simultaneously with other changes to the learning rate
217
+ from outside this scheduler.
218
+
219
+ .. warning::
220
+ This is an experimental lr scheduler module that is subject to change.
221
+ This module must be used with optimizers in `Experimental Optimizer
222
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
223
+
224
+ Args:
225
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
226
+ start_factor (float, optional): The number we multiply learning rate in the first epoch.
227
+ The multiplication factor changes towards `end_factor` in the following epochs.
228
+ Default: ``1.0 /3``.
229
+ end_factor (float, optional): The number we multiply learning rate at the end of linear changing
230
+ process. Default: ``1.0``.
231
+ total_iters (int, optional): The number of iterations that multiplicative factor reaches to 1.
232
+ Default: ``5``.
233
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
234
+
235
+ Raises:
236
+ ValueError: If `start_factor` is not in the range of (0, 1].
237
+ ValueError: If `end_factor` is not in the range of [0, 1].
238
+
239
+ Supported Platforms:
240
+ ``Ascend`` ``GPU`` ``CPU``
241
+
242
+ Examples:
243
+ >>> import mindspore
244
+ >>> from mindspore import nn
245
+ >>> from mindspore.experimental import optim
246
+ >>> # Define the network structure of LeNet5. Refer to
247
+ >>> # https://gitee.com/mindspore/docs/blob/r2.2/docs/mindspore/code/lenet.py
248
+ >>> net = LeNet5()
249
+ >>> loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
250
+ >>> optimizer = optim.Adam(net.trainable_params(), lr=0.05)
251
+ >>> # Assuming optimizer uses lr = 0.05 for all groups
252
+ >>> # lr = 0.025 if epoch == 0
253
+ >>> # lr = 0.03125 if epoch == 1
254
+ >>> # lr = 0.0375 if epoch == 2
255
+ >>> # lr = 0.04375 if epoch == 3
256
+ >>> # lr = 0.05 if epoch >= 4
257
+ >>> scheduler = optim.lr_scheduler.LinearLR(optimizer, start_factor=0.5, total_iters=4)
258
+ >>> def forward_fn(data, label):
259
+ ... logits = net(data)
260
+ ... loss = loss_fn(logits, label)
261
+ ... return loss, logits
262
+ >>> grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)
263
+ >>> def train_step(data, label):
264
+ ... (loss, _), grads = grad_fn(data, label)
265
+ ... optimizer(grads)
266
+ ... return loss
267
+ >>> for epoch in range(5):
268
+ ... # Create the dataset taking MNIST as an example. Refer to
269
+ ... # https://gitee.com/mindspore/docs/blob/r2.2/docs/mindspore/code/mnist.py
270
+ ... for data, label in create_dataset():
271
+ ... train_step(data, label)
272
+ ... scheduler.step()
273
+ ... current_lr = scheduler.get_last_lr()
274
+ """
275
+
276
+ def __init__(self, optimizer, start_factor=1.0 / 3, end_factor=1.0, total_iters=5, last_epoch=-1):
277
+ if start_factor > 1.0 or start_factor <= 0:
278
+ raise ValueError('Starting multiplicative factor expected to be greater than 0 and '
279
+ 'less than or equal to 1.')
280
+
281
+ if end_factor > 1.0 or end_factor < 0:
282
+ raise ValueError('Ending multiplicative factor expected to be between 0 and 1.')
283
+
284
+ self.start_factor = start_factor
285
+ self.end_factor = end_factor
286
+ self.total_iters = total_iters
287
+ super(LinearLR, self).__init__(optimizer, last_epoch)
288
+
289
+ def get_lr(self):
290
+ lrs = [lr.value() for lr in self._last_lr]
291
+
292
+ if self.last_epoch == 0:
293
+ return [lr * self.start_factor for lr in lrs]
294
+
295
+ if self.last_epoch > self.total_iters:
296
+ return lrs
297
+
298
+ factor = 1. + (self.end_factor - self.start_factor) / (
299
+ self.total_iters * self.start_factor + (self.last_epoch - 1) * (self.end_factor - self.start_factor))
300
+ return [lr * factor for lr in lrs]
301
+
302
+ def _get_closed_form_lr(self):
303
+ return [base_lr * (self.start_factor +
304
+ (self.end_factor - self.start_factor) * min(self.total_iters, self.last_epoch)
305
+ / self.total_iters) for base_lr in self.base_lrs]
306
+
307
+
308
+ @jit_class
309
+ class ExponentialLR(LRScheduler):
310
+ r"""
311
+ For each epoch, the learning rate decays exponentially, multiplied by gamma.
312
+ Notice that such decay can happen simultaneously with other changes to the learning rate
313
+ from outside this scheduler.
314
+
315
+ .. warning::
316
+ This is an experimental lr scheduler module that is subject to change.
317
+ This module must be used with optimizers in `Experimental Optimizer
318
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
319
+
320
+ Args:
321
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
322
+ gamma (float): Learning rate scaling factor.
323
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
324
+
325
+ Supported Platforms:
326
+ ``Ascend`` ``GPU`` ``CPU``
327
+
328
+ Examples:
329
+ >>> from mindspore import nn
330
+ >>> from mindspore.experimental import optim
331
+ >>> class Net(nn.Cell):
332
+ ... def __init__(self):
333
+ ... super(Net, self).__init__()
334
+ ... self.fc = nn.Dense(16 * 5 * 5, 120)
335
+ ... def construct(self, x):
336
+ ... return self.fc(x)
337
+ >>> net = Net()
338
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.01)
339
+ >>> scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.5)
340
+ >>> for i in range(3):
341
+ ... scheduler.step()
342
+ ... current_lr = scheduler.get_last_lr()
343
+ ... print(current_lr)
344
+ [Tensor(shape=[], dtype=Float32, value= 0.005)]
345
+ [Tensor(shape=[], dtype=Float32, value= 0.0025)]
346
+ [Tensor(shape=[], dtype=Float32, value= 0.00125)]
347
+ """
348
+
349
+ def __init__(self, optimizer, gamma, last_epoch=-1):
350
+ self.gamma = gamma
351
+ super(ExponentialLR, self).__init__(optimizer, last_epoch)
352
+
353
+ def get_lr(self):
354
+ lrs = [lr.value() for lr in self._last_lr]
355
+ if self.last_epoch == 0:
356
+ return lrs
357
+ return [lr * self.gamma for lr in lrs]
358
+
359
+ def _get_closed_form_lr(self):
360
+ return [base_lr * self.gamma ** self.last_epoch
361
+ for base_lr in self.base_lrs]
362
+
363
+
364
+ @jit_class
365
+ class PolynomialLR(LRScheduler):
366
+ r"""
367
+ For each epoch, the learning rate is adjusted by polynomial fitting.
368
+ When the epoch is greater than or equal to `total_iters` , the learning rate is ``0`` .
369
+ Notice that such decay can happen simultaneously with other changes to the learning rate
370
+ from outside this scheduler.
371
+
372
+ The polynomial formula for learning rate calculation is as follows:
373
+
374
+ .. math::
375
+ \begin{split}
376
+ &factor = (\frac{1.0 - \frac{last\_epoch}{total\_iters}}{1.0 - \frac{last\_epoch - 1.0}{total\_iters}})
377
+ ^{power}\\
378
+ &lr = lr \times factor
379
+ \end{split}
380
+
381
+ .. warning::
382
+ This is an experimental lr scheduler module that is subject to change.
383
+ This module must be used with optimizers in `Experimental Optimizer
384
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
385
+
386
+ Args:
387
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
388
+ total_iters (int, optional): The number of iterations adjusting learning rate by polynomial fitting.
389
+ Default: ``5``.
390
+ power (float, optional): Power of polynomial. Default: ``1.0``.
391
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
392
+
393
+ Supported Platforms:
394
+ ``Ascend`` ``GPU`` ``CPU``
395
+
396
+ Examples:
397
+ >>> from mindspore import nn
398
+ >>> from mindspore.experimental import optim
399
+ >>> class Net(nn.Cell):
400
+ ... def __init__(self):
401
+ ... super(Net, self).__init__()
402
+ ... self.fc = nn.Dense(16 * 5 * 5, 120)
403
+ ... def construct(self, x):
404
+ ... return self.fc(x)
405
+ >>> net = Net()
406
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.01)
407
+ >>> scheduler = optim.lr_scheduler.PolynomialLR(optimizer)
408
+ >>> for i in range(6):
409
+ ... scheduler.step()
410
+ ... current_lr = scheduler.get_last_lr()
411
+ ... print(current_lr)
412
+ [Tensor(shape=[], dtype=Float32, value= 0.008)]
413
+ [Tensor(shape=[], dtype=Float32, value= 0.006)]
414
+ [Tensor(shape=[], dtype=Float32, value= 0.004)]
415
+ [Tensor(shape=[], dtype=Float32, value= 0.002)]
416
+ [Tensor(shape=[], dtype=Float32, value= 0)]
417
+ [Tensor(shape=[], dtype=Float32, value= 0)]
418
+ """
419
+ def __init__(self, optimizer, total_iters=5, power=1.0, last_epoch=-1):
420
+ self.total_iters = total_iters
421
+ self.power = power
422
+ self.min = P.Minimum()
423
+ self.cast = P.Cast()
424
+ super(PolynomialLR, self).__init__(optimizer, last_epoch)
425
+
426
+ def get_lr(self):
427
+ lrs = [lr.value() for lr in self._last_lr]
428
+
429
+ if self.last_epoch == 0 or self.last_epoch > self.total_iters:
430
+ return lrs
431
+ factor = ((1.0 - self.last_epoch / self.total_iters) / (
432
+ 1.0 - (self.last_epoch - 1) / self.total_iters)) ** self.power
433
+ return [lr * factor for lr in lrs]
434
+
435
+ def _get_closed_form_lr(self):
436
+ return [
437
+ (base_lr * (1.0 - self.min(self.total_iters, self.last_epoch) / self.total_iters) ** self.power)
438
+ for base_lr in self.base_lrs]
439
+
440
+
441
+ @jit_class
442
+ class ChainedScheduler:
443
+ r"""
444
+ Save the learning rate scheduler chain list of multiple learning rate schedulers,
445
+ and call the step() function to execute the step() function of each learning rate scheduler.
446
+
447
+ .. warning::
448
+ This is an experimental lr scheduler module that is subject to change.
449
+ This module must be used with optimizers in `Experimental Optimizer
450
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
451
+
452
+ Args:
453
+ schedulers (list[:class:`mindspore.experimental.optim.lr_scheduler.LRScheduler`]):
454
+ List of learning rate schedulers.
455
+
456
+ Supported Platforms:
457
+ ``Ascend`` ``GPU`` ``CPU``
458
+
459
+ Examples:
460
+ >>> from mindspore import nn
461
+ >>> from mindspore.experimental import optim
462
+ >>> class Net(nn.Cell):
463
+ ... def __init__(self):
464
+ ... super(Net, self).__init__()
465
+ ... self.fc = nn.Dense(16 * 5 * 5, 120)
466
+ ... def construct(self, x):
467
+ ... return self.fc(x)
468
+ >>> net = Net()
469
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.01)
470
+ >>> scheduler1 = optim.lr_scheduler.PolynomialLR(optimizer)
471
+ >>> scheduler2 = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.5)
472
+ >>> scheduler = optim.lr_scheduler.ChainedScheduler([scheduler1, scheduler2])
473
+ >>> for i in range(6):
474
+ ... scheduler.step()
475
+ ... current_lr = scheduler.get_last_lr()
476
+ ... print(current_lr)
477
+ [Tensor(shape=[], dtype=Float32, value= 0.004)]
478
+ [Tensor(shape=[], dtype=Float32, value= 0.0015)]
479
+ [Tensor(shape=[], dtype=Float32, value= 0.0005)]
480
+ [Tensor(shape=[], dtype=Float32, value= 0.000125)]
481
+ [Tensor(shape=[], dtype=Float32, value= 0)]
482
+ [Tensor(shape=[], dtype=Float32, value= 0)]
483
+ """
484
+ def __init__(self, schedulers):
485
+ self._schedulers = list(schedulers)
486
+ self.optimizer = schedulers[0].optimizer
487
+ self._last_lr = [lr for lr in self._schedulers[-1]._last_lr] # pylint: disable=W0212
488
+
489
+ def step(self):
490
+ """
491
+ Sequential execution of the saved learning rate scheduler's step() function.
492
+ """
493
+ for scheduler in self._schedulers:
494
+ scheduler.step()
495
+
496
+ def get_last_lr(self):
497
+ """
498
+ Return last computed learning rate by current scheduler.
499
+ """
500
+ return [lr.value() for lr in self._last_lr]
501
+
502
+
503
+ @jit_class
504
+ class LambdaLR(LRScheduler):
505
+ """Sets the learning rate of each parameter group to the initial lr
506
+ times a given function. When last_epoch=-1, sets initial lr as lr.
507
+
508
+ .. warning::
509
+ This is an experimental lr scheduler module that is subject to change.
510
+ This module must be used with optimizers in `Experimental Optimizer
511
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
512
+
513
+ Args:
514
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
515
+ lr_lambda (Union(function, list)): A function which computes a multiplicative
516
+ factor given a parameter `last_epoch`, or a list of such
517
+ functions, one for each group in `optimizer.param_groups`.
518
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
519
+
520
+ Raises:
521
+ ValueError: If the length of `lr_lambda` is not equal to the number of param groups.
522
+
523
+ Supported Platforms:
524
+ ``Ascend`` ``GPU`` ``CPU``
525
+
526
+ Examples:
527
+ >>> from mindspore import nn
528
+ >>> from mindspore.experimental import optim
529
+ >>> net = nn.Dense(2, 3)
530
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.01)
531
+ >>> lmbda = lambda epoch: 0.9 ** epoch
532
+ >>> scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=[lmbda])
533
+ >>> for i in range(3):
534
+ ... scheduler.step()
535
+ ... current_lr = scheduler.get_last_lr()
536
+ ... print(current_lr)
537
+ [Tensor(shape=[], dtype=Float32, value= 0.009)]
538
+ [Tensor(shape=[], dtype=Float32, value= 0.0081)]
539
+ [Tensor(shape=[], dtype=Float32, value= 0.00729)]
540
+ """
541
+ def __init__(self, optimizer, lr_lambda, last_epoch=-1):
542
+ if not isinstance(lr_lambda, list) and not isinstance(lr_lambda, tuple):
543
+ self.lr_lambdas = [lr_lambda] * len(optimizer.param_groups)
544
+ else:
545
+ if len(lr_lambda) != len(optimizer.param_groups):
546
+ raise ValueError("Expected {} lr_lambdas, but got {}".format(
547
+ len(optimizer.param_groups), len(lr_lambda)))
548
+ self.lr_lambdas = list(lr_lambda)
549
+ super(LambdaLR, self).__init__(optimizer, last_epoch)
550
+
551
+ def get_lr(self):
552
+ return [base_lr * lmbda(self.last_epoch)
553
+ for lmbda, base_lr in zip(self.lr_lambdas, self.base_lrs)]
554
+
555
+
556
+ @jit_class
557
+ class MultiplicativeLR(LRScheduler):
558
+ """Multiply the learning rate of each parameter group by the factor given
559
+ in the specified function. When last_epoch=-1, sets initial lr as lr.
560
+
561
+ .. warning::
562
+ This is an experimental lr scheduler module that is subject to change.
563
+ This module must be used with optimizers in `Experimental Optimizer
564
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
565
+
566
+ Args:
567
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
568
+ lr_lambda (Union(function, list)): A function which computes a multiplicative
569
+ factor given an integer parameter epoch, or a list of such
570
+ functions, one for each group in optimizer.param_groups.
571
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
572
+
573
+ Supported Platforms:
574
+ ``Ascend`` ``GPU`` ``CPU``
575
+
576
+ Examples:
577
+ >>> from mindspore import nn
578
+ >>> from mindspore.experimental import optim
579
+ >>> net = nn.Dense(2, 3)
580
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.01)
581
+ >>> lmbda = lambda epoch: 0.95
582
+ >>> scheduler = optim.lr_scheduler.MultiplicativeLR(optimizer, lr_lambda=lmbda)
583
+ >>> for i in range(3):
584
+ ... scheduler.step()
585
+ ... current_lr = scheduler.get_last_lr()
586
+ ... print(current_lr)
587
+ [Tensor(shape=[], dtype=Float32, value= 0.0095)]
588
+ [Tensor(shape=[], dtype=Float32, value= 0.009025)]
589
+ [Tensor(shape=[], dtype=Float32, value= 0.00857375)]
590
+ """
591
+ def __init__(self, optimizer, lr_lambda, last_epoch=-1):
592
+ if not isinstance(lr_lambda, list) and not isinstance(lr_lambda, tuple):
593
+ self.lr_lambdas = [lr_lambda] * len(optimizer.param_groups)
594
+ else:
595
+ if len(lr_lambda) != len(optimizer.param_groups):
596
+ raise ValueError("Expected {} lr_lambdas, but got {}".format(
597
+ len(optimizer.param_groups), len(lr_lambda)))
598
+ self.lr_lambdas = list(lr_lambda)
599
+ super(MultiplicativeLR, self).__init__(optimizer, last_epoch)
600
+
601
+ def get_lr(self):
602
+ lrs = [lr.value() for lr in self._last_lr]
603
+ if self.last_epoch > 0:
604
+ return [lr * lmbda(self.last_epoch)
605
+ for lmbda, lr in zip(self.lr_lambdas, lrs)]
606
+ return lrs
607
+
608
+
609
+ @jit_class
610
+ class MultiStepLR(LRScheduler):
611
+ """Multiply the learning rate of each parameter group by gamma once the
612
+ number of epoch reaches one of the milestones. Notice that such change can
613
+ happen simultaneously with other changes to the learning rate from outside
614
+ this scheduler. When last_epoch=-1, sets initial lr as lr.
615
+
616
+ .. warning::
617
+ This is an experimental lr scheduler module that is subject to change.
618
+ This module must be used with optimizers in `Experimental Optimizer
619
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
620
+
621
+ Args:
622
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
623
+ milestones (list): List of epoch indices. When `last_epoch` reach the milestone,
624
+ multiply the learning rate of each parameter group by `gamma`.
625
+ gamma (float, optional): Multiplicative factor of learning rate decay.
626
+ Default: ``0.1``.
627
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
628
+
629
+ Raises:
630
+ TypeError: If the `milestones` is not list.
631
+ TypeError: If elements of the `milestones` are not int.
632
+ TypeError: If the `gamma` is not float.
633
+
634
+ Supported Platforms:
635
+ ``Ascend`` ``GPU`` ``CPU``
636
+
637
+ Examples:
638
+ >>> from mindspore import nn
639
+ >>> from mindspore.experimental import optim
640
+ >>> net = nn.Dense(2, 3)
641
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.05)
642
+ >>> # Assuming optimizer uses lr = 0.05 for all groups
643
+ >>> # lr = 0.05 if epoch < 2
644
+ >>> # lr = 0.005 if 2 <= epoch < 4
645
+ >>> # lr = 0.0005 if epoch >= 4
646
+ >>> scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[2,4], gamma=0.1)
647
+ >>> for i in range(6):
648
+ ... scheduler.step()
649
+ ... current_lr = scheduler.get_last_lr()
650
+ ... print(current_lr)
651
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
652
+ [Tensor(shape=[], dtype=Float32, value= 0.005)]
653
+ [Tensor(shape=[], dtype=Float32, value= 0.005)]
654
+ [Tensor(shape=[], dtype=Float32, value= 0.0005)]
655
+ [Tensor(shape=[], dtype=Float32, value= 0.0005)]
656
+ [Tensor(shape=[], dtype=Float32, value= 0.0005)]
657
+ """
658
+
659
+ def __init__(self, optimizer, milestones, gamma=0.1, last_epoch=-1):
660
+ Validator.check_value_type('milestones', milestones, [list])
661
+ for milestone in milestones:
662
+ if not isinstance(milestone, int):
663
+ raise TypeError(f"For 'MultiStepLR', elements of the 'milestones' must be type of int, "
664
+ f"but got one element of 'milestones' type: {type(milestone)}.")
665
+ Validator.check_value_type('gamma', gamma, [float, int])
666
+ self.milestones = Counter(milestones)
667
+ self.milestones_keys = list(self.milestones.keys())
668
+ self.milestones_values = list(self.milestones.values())
669
+ self.gamma = gamma
670
+ super(MultiStepLR, self).__init__(optimizer, last_epoch)
671
+
672
+ def get_lr(self):
673
+ lrs = [lr.value() for lr in self._last_lr]
674
+ tmp_epoch = int(self.last_epoch.value())
675
+
676
+ for i in range(len(self.milestones_keys)):
677
+ if tmp_epoch == self.milestones_keys[i]:
678
+ value = self.milestones_values[i]
679
+ return [lr * self.gamma ** value for lr in lrs]
680
+ return lrs
681
+
682
+ def _get_closed_form_lr(self):
683
+ return [base_lr * self.gamma ** (self.last_epoch // self.step_size)
684
+ for base_lr in self.base_lrs]
685
+
686
+
687
+ @jit_class
688
+ class ConstantLR(LRScheduler):
689
+ """Decays the learning rate of each parameter group by a small constant factor until the
690
+ number of epoch reaches a pre-defined milestone: total_iters. Notice that such decay can
691
+ happen simultaneously with other changes to the learning rate from outside this scheduler.
692
+
693
+ .. warning::
694
+ This is an experimental lr scheduler module that is subject to change.
695
+ This module must be used with optimizers in `Experimental Optimizer
696
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
697
+
698
+ Args:
699
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
700
+ factor (float, optional): The factor number multiplied learning rate. Default: ``1./3``.
701
+ total_iters (int, optional): The number of steps that the scheduler decays the learning rate,
702
+ when the `last_epoch` reach `total_iters`, restore the learning rate. Default: ``5``.
703
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
704
+
705
+ Supported Platforms:
706
+ ``Ascend`` ``GPU`` ``CPU``
707
+
708
+ Examples:
709
+ >>> from mindspore import nn
710
+ >>> from mindspore.experimental import optim
711
+ >>> net = nn.Dense(2, 3)
712
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.05)
713
+ >>> # Assuming optimizer uses lr = 0.05 for all groups
714
+ >>> # lr = 0.025 if epoch <4
715
+ >>> # lr = 0.05 if epoch >= 4
716
+ >>> scheduler = optim.lr_scheduler.ConstantLR(optimizer, factor=0.5, total_iters=4)
717
+ >>> for i in range(6):
718
+ ... scheduler.step()
719
+ ... current_lr = scheduler.get_last_lr()
720
+ ... print(current_lr)
721
+ [Tensor(shape=[], dtype=Float32, value= 0.025)]
722
+ [Tensor(shape=[], dtype=Float32, value= 0.025)]
723
+ [Tensor(shape=[], dtype=Float32, value= 0.025)]
724
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
725
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
726
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
727
+ """
728
+ def __init__(self, optimizer, factor=1.0 / 3, total_iters=5, last_epoch=-1):
729
+ if factor > 1.0 or factor < 0:
730
+ raise ValueError('Constant multiplicative factor expected to be between 0 and 1.')
731
+
732
+ self.factor = factor
733
+ self.total_iters = total_iters
734
+ super(ConstantLR, self).__init__(optimizer, last_epoch)
735
+
736
+ def get_lr(self):
737
+ lrs = [lr.value() for lr in self._last_lr]
738
+ if self.last_epoch == 0:
739
+ return [lr * self.factor for lr in lrs]
740
+ if self.last_epoch != self.total_iters:
741
+ return lrs
742
+ return [lr / self.factor for lr in lrs]
743
+
744
+ def _get_closed_form_lr(self):
745
+ return [base_lr * (self.factor + (self.last_epoch >= self.total_iters) * (1 - self.factor))
746
+ for base_lr in self.base_lrs]
747
+
748
+
749
+ @jit_class
750
+ class SequentialLR:
751
+ r"""
752
+ Receives the list of schedulers that is expected to be called sequentially during
753
+ optimization process and milestone points that provides exact intervals to reflect
754
+ which scheduler is supposed to be called at a given epoch.
755
+
756
+ .. warning::
757
+ This is an experimental lr scheduler module that is subject to change.
758
+ This module must be used with optimizers in `Experimental Optimizer
759
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
760
+
761
+ Args:
762
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
763
+ schedulers (list[:class:`mindspore.experimental.optim.lr_scheduler.LRScheduler`]):
764
+ List of learning rate schedulers.
765
+ milestones (list): List of integers that reflects milestone points.
766
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
767
+
768
+ Raises:
769
+ ValueError: The optimizer in `schedulers` is different from the `optimizer` passed in.
770
+ ValueError: The optimizer in `schedulers` is different from the optimizer of `schedulers[0]`.
771
+ ValueError: Length of `milestones` is not equal to length of `schedulers` minus 1.
772
+
773
+ Supported Platforms:
774
+ ``Ascend`` ``GPU`` ``CPU``
775
+
776
+ Examples:
777
+ >>> from mindspore.experimental import optim
778
+ >>> from mindspore import nn
779
+ >>> net = nn.Dense(3, 2)
780
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.1)
781
+ >>> scheduler1 = optim.lr_scheduler.ConstantLR(optimizer, factor=0.1, total_iters=2)
782
+ >>> scheduler2 = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.9)
783
+ >>> scheduler = optim.lr_scheduler.SequentialLR(optimizer, schedulers=[scheduler1, scheduler2], milestones=[2])
784
+ >>> for i in range(6):
785
+ ... scheduler.step()
786
+ ... current_lr = scheduler.get_last_lr()
787
+ ... print(current_lr)
788
+ [Tensor(shape=[], dtype=Float32, value= 0.01)]
789
+ [Tensor(shape=[], dtype=Float32, value= 0.1)]
790
+ [Tensor(shape=[], dtype=Float32, value= 0.09)]
791
+ [Tensor(shape=[], dtype=Float32, value= 0.081)]
792
+ [Tensor(shape=[], dtype=Float32, value= 0.0729)]
793
+ [Tensor(shape=[], dtype=Float32, value= 0.06561)]
794
+ """
795
+ def __init__(self, optimizer, schedulers, milestones, last_epoch=-1):
796
+ for sched_idx in range(len(schedulers)):
797
+ if schedulers[sched_idx].optimizer != optimizer:
798
+ raise ValueError(
799
+ "Sequential Schedulers expects all schedulers to belong to the same optimizer, but "
800
+ f"got scheduler at index {sched_idx} is different from the optimizer passed in.")
801
+
802
+ if schedulers[sched_idx].optimizer != schedulers[0].optimizer:
803
+ raise ValueError(
804
+ "Sequential Schedulers expects all schedulers to belong to the same optimizer, but "
805
+ f"got schedulers at index {0} and {sched_idx} are different.")
806
+
807
+ if len(milestones) != len(schedulers) - 1:
808
+ raise ValueError(
809
+ "Sequential Schedulers expects number of schedulers provided to be one more "
810
+ "than the number of milestone points, but got number of schedulers {} and the "
811
+ "number of milestones {}".format(len(schedulers), len(milestones)))
812
+
813
+ self._schedulers = schedulers
814
+ self.milestones = milestones
815
+ self.milestones_len = len(milestones)
816
+ self.last_epoch = Parameter(Tensor(last_epoch+1, dtype=mstype.float32),
817
+ name='last_epoch_' + self.__class__.__name__)
818
+ self.increase_tensor = Tensor(1, mstype.int32)
819
+
820
+ self.optimizer = optimizer
821
+ for group in self.optimizer.param_groups:
822
+ ops.assign(group["lr"], group["initial_lr"])
823
+
824
+ for scheduler in self._schedulers:
825
+ ops.assign_sub(scheduler.last_epoch, self.increase_tensor)
826
+
827
+ self._schedulers[0].step()
828
+ self._last_lr = schedulers[0]._last_lr # pylint: disable=W0212
829
+
830
+
831
+ def step(self):
832
+ """
833
+ Get the current learning rate and change the learning rate.
834
+ """
835
+ ops.assign_add(self.last_epoch, self.increase_tensor)
836
+ tmp_epoch = int(self.last_epoch)
837
+
838
+ cur_idx = bisect_right(self.milestones, tmp_epoch)
839
+ scheduler = self._schedulers[cur_idx]
840
+ if cur_idx > 0 and self.milestones[cur_idx - 1] == tmp_epoch:
841
+ scheduler.step(0)
842
+ else:
843
+ scheduler.step()
844
+
845
+ def get_last_lr(self):
846
+ """
847
+ Return last computed learning rate by current scheduler.
848
+ """
849
+ return [lr.value() for lr in self._last_lr]
850
+
851
+
852
+ @jit_class
853
+ class ReduceLROnPlateau:
854
+ """
855
+ Reduce learning rate when a metric has stopped improving.
856
+ Models often benefit from reducing the learning rate by a factor
857
+ of 2-10 once learning stagnates. This scheduler reads a metrics
858
+ quantity and if no improvement is seen for a 'patience' number
859
+ of epochs, the learning rate is reduced.
860
+
861
+ .. warning::
862
+ This is an experimental lr scheduler module that is subject to change.
863
+ This module must be used with optimizers in `Experimental Optimizer
864
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
865
+
866
+ Args:
867
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
868
+ mode (str, optional): One of `min`, `max`. In `min` mode, lr will
869
+ be reduced when the quantity monitored has stopped
870
+ decreasing; in `max` mode it will be reduced when the
871
+ quantity monitored has stopped increasing. Default: ``'min'``.
872
+ factor (float, optional): Factor by which the learning rate will be
873
+ reduced. Default: ``0.1``.
874
+ patience (int, optional): Number of epochs with no improvement after
875
+ which learning rate will be reduced. For example, if
876
+ `patience = 2`, then we will ignore the first 2 epochs
877
+ with no improvement, and will only decrease the LR after the
878
+ 3rd epoch if the loss still hasn't improved then.
879
+ Default: ``10``.
880
+ threshold (float, optional): Threshold for measuring the new optimum,
881
+ to only focus on significant changes. Default: ``1e-4``.
882
+ threshold_mode (str, optional): One of `rel`, `abs`. Given dynamic_threshold is the benchmark to
883
+ define whether the current metric is improvement,
884
+ in ``'rel'`` mode, dynamic_threshold = best * ( 1 + threshold ) in ``'max'`` mode
885
+ or best * ( 1 - threshold ) in ``'min'`` mode.
886
+ In ``'abs'`` mode, dynamic_threshold = best + threshold in ``'max'`` mode or
887
+ best - threshold in ``'min'`` mode. Default: ``'rel'``.
888
+ cooldown (int, optional): Number of epochs to wait before resuming
889
+ normal operation after lr has been reduced. Default: ``0``.
890
+ min_lr (Union(float, list), optional): A scalar or a list of scalars. A
891
+ lower bound on the learning rate of all param groups
892
+ or each group respectively. Default: ``0``.
893
+ eps (float, optional): Minimal decay applied to lr. If the difference
894
+ between new and old lr is smaller than eps, the update is
895
+ ignored. Default: ``1e-8``.
896
+
897
+ Raises:
898
+ ValueError: `factor` is greater than or equal to 1.0.
899
+ TypeError: `optimizer` is not an `Optimizer`.
900
+ ValueError: When `min_lr` is a list or tuple, the length of it is not equal to the number of param groups.
901
+ ValueError: `mode` is neither ``'min'`` nor ``'max'``.
902
+ ValueError: `threshold_mode` is neither ``'rel'`` nor ``'abs'``.
903
+
904
+ Supported Platforms:
905
+ ``Ascend`` ``GPU`` ``CPU``
906
+
907
+ Examples:
908
+ >>> from mindspore.experimental import optim
909
+ >>> from mindspore import nn
910
+ >>> net = nn.Dense(3, 2)
911
+ >>> optimizer = optim.Adam(net.trainable_params(), 0.1)
912
+ >>> scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=0)
913
+ >>> metrics = [1, 1.5, 1.8, 0.4, 0.5]
914
+ >>> for i in range(5):
915
+ ... scheduler.step(metrics[i])
916
+ ... current_lr = scheduler._last_lr
917
+ ... print(current_lr)
918
+ [Tensor(shape=[], dtype=Float32, value= 0.1)]
919
+ [Tensor(shape=[], dtype=Float32, value= 0.01)]
920
+ [Tensor(shape=[], dtype=Float32, value= 0.001)]
921
+ [Tensor(shape=[], dtype=Float32, value= 0.001)]
922
+ [Tensor(shape=[], dtype=Float32, value= 0.0001)]
923
+ """
924
+ def __init__(self, optimizer, mode='min', factor=0.1, patience=10,
925
+ threshold=1e-4, threshold_mode='rel', cooldown=0,
926
+ min_lr=0, eps=1e-8):
927
+
928
+ if factor >= 1.0:
929
+ raise ValueError("The lr factor should be less than 1.0.")
930
+ self.factor = factor
931
+
932
+ if not isinstance(optimizer, Optimizer):
933
+ raise TypeError("Expected an `Optimizer`, but got type {}".format(type(optimizer).__name__))
934
+ self.optimizer = optimizer
935
+
936
+ if isinstance(min_lr, (list, tuple)):
937
+ if len(min_lr) != len(optimizer.param_groups):
938
+ raise ValueError("Expected {} min_lrs, got {}".format(len(optimizer.param_groups), len(min_lr)))
939
+ self.min_lrs = [Tensor(lr, mstype.float32) for lr in min_lr]
940
+ else:
941
+ self.min_lrs = [Tensor(min_lr, mstype.float32)] * len(optimizer.param_groups)
942
+
943
+ self.mode = mode
944
+ self.patience = patience
945
+ self.threshold = threshold
946
+ self.threshold_mode = threshold_mode
947
+ self.cooldown = cooldown
948
+ self.cooldown_counter = 0
949
+ self.eps = eps
950
+ self.mode_worse = None
951
+ self.assign = P.Assign()
952
+ self.cast = P.Cast()
953
+ self.last_epoch = Parameter(Tensor(0, dtype=mstype.int32),
954
+ name='last_epoch_' + self.__class__.__name__)
955
+
956
+ if self.mode not in {'min', 'max'}:
957
+ raise ValueError(f"`mode` should be 'min' or 'max', but got {self.mode}.")
958
+ if self.threshold_mode not in {'rel', 'abs'}:
959
+ raise ValueError(f"`threshold mode` should be 'rel' or 'abs', but got {self.threshold_mode}.")
960
+
961
+ if self.mode == 'min':
962
+ self.mode_worse = float("inf")
963
+ else:
964
+ self.mode_worse = float("-inf")
965
+
966
+ self.best = Parameter(Tensor(self.mode_worse, dtype=mstype.float32), name='best')
967
+
968
+ self.cooldown_counter = Parameter(Tensor(0, dtype=mstype.float32), name='cooldown_counter')
969
+ self.wait = Parameter(Tensor(0, dtype=mstype.float32), name='wait')
970
+ self.increase_tensor = Tensor(1, mstype.int32)
971
+ self._last_lr = [group['lr'] for group in self.optimizer.param_groups]
972
+
973
+ def step(self, metrics):
974
+ """
975
+ Get the current learning rate and change the learning rate.
976
+
977
+ Args:
978
+ metrics(Union(int, float)): the evaluation metrics.
979
+ """
980
+ epoch = self.last_epoch + 1
981
+ current = self.cast(metrics, mstype.float32)
982
+ self.assign(self.last_epoch, epoch)
983
+
984
+ if self._is_improvement(current, self.best):
985
+ ops.assign(self.best, current)
986
+ ops.assign(self.wait, 0)
987
+ else:
988
+ ops.assign_add(self.wait, self.increase_tensor)
989
+
990
+ if self.in_cooldown:
991
+ ops.assign_sub(self.cooldown_counter, self.increase_tensor)
992
+ ops.assign(self.wait, 0)
993
+
994
+ if self.wait > self.patience:
995
+ self._reduce_lr(epoch)
996
+ ops.assign(self.cooldown_counter, self.cooldown)
997
+ ops.assign(self.wait, 0)
998
+
999
+ return True
1000
+
1001
+ def _reduce_lr(self, epoch):
1002
+ for i, lr in enumerate(self._last_lr):
1003
+ old_lr = lr.value()
1004
+ new_lr = ops.maximum(old_lr * self.factor, self.min_lrs[i])
1005
+ if old_lr > new_lr + self.eps:
1006
+ ops.assign(lr, new_lr)
1007
+ return True
1008
+
1009
+ @property
1010
+ def in_cooldown(self):
1011
+ """ Whether in cooldown period. """
1012
+ return self.cooldown_counter > 0
1013
+
1014
+ def _is_improvement(self, current, best):
1015
+ """ Whether current metric value is better than best. """
1016
+ if self.mode == 'min' and self.threshold_mode == 'rel':
1017
+ rel_epsilon = 1. - self.threshold
1018
+ benchmark = best * rel_epsilon
1019
+ return current < benchmark
1020
+
1021
+ if self.mode == 'min' and self.threshold_mode == 'abs':
1022
+ benchmark = best - self.threshold
1023
+ return current < benchmark
1024
+
1025
+ if self.mode == 'max' and self.threshold_mode == 'rel':
1026
+ rel_epsilon = self.threshold + 1.
1027
+ benchmark = best * rel_epsilon
1028
+ return current > benchmark
1029
+
1030
+ benchmark = best + self.threshold
1031
+ return current > benchmark
1032
+
1033
+ def get_last_lr(self):
1034
+ """
1035
+ Return last computed learning rate by current scheduler.
1036
+ """
1037
+ return [lr.value() for lr in self._last_lr]
1038
+
1039
+
1040
+ @jit_class
1041
+ class CyclicLR(LRScheduler):
1042
+ r"""
1043
+ Sets the learning rate of each parameter group according to
1044
+ cyclical learning rate policy (CLR). The policy cycles the learning
1045
+ rate between two boundaries with a constant frequency, as detailed in
1046
+ the paper `Cyclical Learning Rates for Training Neural Networks <https://arxiv.org/abs/1506.01186>`_.
1047
+ The distance between the two boundaries can be scaled on a per-iteration
1048
+ or per-cycle basis.
1049
+
1050
+ This class has three built-in policies, as put forth in the paper:
1051
+
1052
+ - "triangular": A basic triangular cycle without amplitude scaling.
1053
+ - "triangular2": A basic triangular cycle that scales initial amplitude by half each cycle.
1054
+ - "exp_range": A cycle that scales initial amplitude by :math:`\text{gamma}^{\text{cycle iterations}}`
1055
+ at each cycle iteration.
1056
+
1057
+ .. warning::
1058
+ This is an experimental lr scheduler module that is subject to change.
1059
+ This module must be used with optimizers in `Experimental Optimizer
1060
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
1061
+
1062
+ Args:
1063
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
1064
+ base_lr (Union(float, list)): Initial learning rate which is the
1065
+ lower boundary in the cycle for each parameter group.
1066
+ max_lr (Union(float, list)): Upper learning rate boundaries in the cycle
1067
+ for each parameter group. Functionally, it defines the cycle amplitude (max_lr - base_lr).
1068
+ The lr at any cycle is the sum of base_lr and some scaling of the amplitude.
1069
+ step_size_up (int, optional): Number of training iterations in the
1070
+ increasing half of a cycle. Default: ``2000``.
1071
+ step_size_down (int, optional): Number of training iterations in the
1072
+ decreasing half of a cycle. If step_size_down is None,
1073
+ it is set to step_size_up. Default: ``None``.
1074
+ mode (str, optional): One of {triangular, triangular2, exp_range}.
1075
+ Values correspond to policies detailed above.
1076
+ If scale_fn is not None, this argument is ignored.
1077
+ Default: ``'triangular'``.
1078
+ gamma (float, optional): Constant in 'exp_range' scaling function:
1079
+ gamma**(cycle iterations). Default: ``1.0``.
1080
+ scale_fn (function, optional): Custom scaling policy defined by a single
1081
+ argument lambda function, where 0 <= scale_fn(x) <= 1 for all x >= 0.
1082
+ If specified, then 'mode' is ignored. Default: ``None``.
1083
+ scale_mode (str, optional): {'cycle', 'iterations'}.
1084
+ Defines whether scale_fn is evaluated on cycle number or cycle iterations (training
1085
+ iterations since start of cycle). Illegal inputs will use ``'iterations'`` by defaults.
1086
+ Default: ``'cycle'``.
1087
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
1088
+
1089
+ Raises:
1090
+ ValueError: When `base_lr` is list or tuple, the length of it is not equal to the number of param groups.
1091
+ ValueError: When `max_lr` is list or tuple, the length of it is not equal to the number of param groups.
1092
+ ValueError: `mode` is not in [``'triangular'``, ``'triangular2'``, ``'exp_range'``] and `scale_fn` is None.
1093
+
1094
+ Supported Platforms:
1095
+ ``Ascend`` ``GPU`` ``CPU``
1096
+
1097
+ Examples:
1098
+ >>> from mindspore.experimental import optim
1099
+ >>> from mindspore import nn
1100
+ >>> net = nn.Dense(3, 2)
1101
+ >>> optimizer = optim.SGD(net.trainable_params(), lr=0.1, momentum=0.9)
1102
+ >>> scheduler = optim.lr_scheduler.CyclicLR(optimizer, base_lr=0.01, max_lr=0.1)
1103
+ >>> expect_list = [[0.010045], [0.01009], [0.010135], [0.01018], [0.010225]]
1104
+ >>>
1105
+ >>> for i in range(5):
1106
+ ... scheduler.step()
1107
+ ... current_lr = scheduler.get_last_lr()
1108
+ ... print(current_lr)
1109
+ [Tensor(shape=[], dtype=Float32, value= 0.010045)]
1110
+ [Tensor(shape=[], dtype=Float32, value= 0.01009)]
1111
+ [Tensor(shape=[], dtype=Float32, value= 0.010135)]
1112
+ [Tensor(shape=[], dtype=Float32, value= 0.01018)]
1113
+ [Tensor(shape=[], dtype=Float32, value= 0.010225)]
1114
+ """
1115
+ def __init__(self,
1116
+ optimizer,
1117
+ base_lr,
1118
+ max_lr,
1119
+ step_size_up=2000,
1120
+ step_size_down=None,
1121
+ mode='triangular',
1122
+ gamma=1.,
1123
+ scale_fn=None,
1124
+ scale_mode='cycle',
1125
+ last_epoch=-1):
1126
+
1127
+ base_lrs = self._preprocess_input_param(optimizer, base_lr, 'base_lr')
1128
+
1129
+ if last_epoch == -1:
1130
+ for lr, group in zip(base_lrs, optimizer.param_groups):
1131
+ group['lr'] = Parameter(lr)
1132
+
1133
+ self.max_lrs = self._preprocess_input_param(optimizer, max_lr, 'max_lr')
1134
+ self.max_lrs = [Tensor(lr) for lr in self.max_lrs]
1135
+
1136
+ step_size_up = float(step_size_up)
1137
+ step_size_down = step_size_up if step_size_down is None else float(step_size_down)
1138
+
1139
+ self.total_step_size = step_size_up + step_size_down
1140
+ self.step_up_ratio = step_size_up / self.total_step_size
1141
+
1142
+ if mode not in ['triangular', 'triangular2', 'exp_range'] \
1143
+ and scale_fn is None:
1144
+ raise ValueError('mode is invalid and scale_fn is None')
1145
+
1146
+ self.mode = mode
1147
+ self.gamma = gamma
1148
+
1149
+ self._scale_fn_ref = None
1150
+ self._scale_fn_custom = scale_fn
1151
+ self.scale_mode = scale_mode
1152
+ self._init_scale_fn()
1153
+ self.floor = P.Floor()
1154
+
1155
+ super(CyclicLR, self).__init__(optimizer, last_epoch)
1156
+ self.base_lrs = [Tensor(lr) for lr in base_lrs]
1157
+
1158
+ def _init_scale_fn(self):
1159
+ """ Define the scale function. """
1160
+ if self._scale_fn_custom is not None:
1161
+ return
1162
+ if self.mode == 'triangular':
1163
+ self._scale_fn_ref = self._triangular_scale_fn
1164
+ self.scale_mode = 'cycle'
1165
+ elif self.mode == 'triangular2':
1166
+ self._scale_fn_ref = self._triangular2_scale_fn
1167
+ self.scale_mode = 'cycle'
1168
+ elif self.mode == 'exp_range':
1169
+ self._scale_fn_ref = self._exp_range_scale_fn
1170
+ self.scale_mode = 'iterations'
1171
+
1172
+ def _preprocess_input_param(self, optimizer, param, name):
1173
+ """Return correctly formatted lr/momentum for each param group."""
1174
+ if isinstance(param, (list, tuple)):
1175
+ if len(param) != len(optimizer.param_groups):
1176
+ raise ValueError("Expected {} values for {}, got {}".format(
1177
+ len(optimizer.param_groups), name, len(param)))
1178
+ return param
1179
+ return [param] * len(optimizer.param_groups)
1180
+
1181
+ def scale_fn(self, x):
1182
+ if self._scale_fn_custom is None:
1183
+ return self._scale_fn_ref(x)
1184
+ return self._scale_fn_custom(x)
1185
+
1186
+ def _triangular_scale_fn(self, x):
1187
+ return 1.
1188
+
1189
+ def _triangular2_scale_fn(self, x):
1190
+ return 1 / (2. ** (x - 1))
1191
+
1192
+ def _exp_range_scale_fn(self, x):
1193
+ return self.gamma ** (x)
1194
+
1195
+ def get_lr(self):
1196
+ cycle = self.floor(1 + self.last_epoch / self.total_step_size)
1197
+ x = 1. + self.last_epoch / self.total_step_size - cycle
1198
+ if x <= self.step_up_ratio:
1199
+ scale_factor = x / self.step_up_ratio
1200
+ else:
1201
+ scale_factor = (x - 1) / (self.step_up_ratio - 1)
1202
+ lrs = []
1203
+ for base_lr, max_lr in zip(self.base_lrs, self.max_lrs):
1204
+ base_height = (max_lr - base_lr) * scale_factor
1205
+
1206
+ if self.scale_mode == 'cycle':
1207
+ lr = base_lr + base_height * self.scale_fn(cycle)
1208
+ else:
1209
+ lr = base_lr + base_height * self.scale_fn(self.last_epoch)
1210
+ lrs.append(lr)
1211
+
1212
+ return lrs
1213
+
1214
+
1215
+ @jit_class
1216
+ class CosineAnnealingWarmRestarts(LRScheduler):
1217
+ r"""
1218
+ Set the learning rate of each parameter group using a cosine annealing warm restarts
1219
+ schedule. Where :math:`\eta_{max}` is set to the initial lr, :math:`\eta_{min}` is the minimum value
1220
+ for learning rate, :math:`\eta_{t}` is the current learning rate, :math:`T_{0}` is the number of iterations for the
1221
+ first restar, :math:`T_{i}` is the current number of iterations between two warm restarts in SGDR,
1222
+ :math:`T_{cur}` is the number of epochs since the last restart in SGDR.
1223
+
1224
+ .. math::
1225
+ \eta_t = \eta_{min} + \frac{1}{2}(\eta_{max} - \eta_{min})\left(1 +
1226
+ \cos\left(\frac{T_{cur}}{T_{i}}\pi\right)\right)
1227
+
1228
+ When :math:`T_{cur}=T_{i}`, set :math:`\eta_t = \eta_{min}`.
1229
+ When :math:`T_{cur}=0` after restart, set :math:`\eta_t=\eta_{max}`.
1230
+
1231
+ For more details, please refer to: `SGDR: Stochastic Gradient Descent with Warm Restarts
1232
+ <https://arxiv.org/abs/1608.03983>`_.
1233
+
1234
+ .. warning::
1235
+ This is an experimental lr scheduler module that is subject to change.
1236
+ This module must be used with optimizers in `Experimental Optimizer
1237
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
1238
+
1239
+ Args:
1240
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
1241
+ T_0 (int): Number of iterations for the first restart.
1242
+ T_mult (int, optional): A factor increases :math:`T_{i}` after a restart. Default: ``1``.
1243
+ eta_min (Union(float, int), optional): Minimum learning rate. Default: ``0``.
1244
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
1245
+
1246
+ Raises:
1247
+ ValueError: `T_0` is less than or equal than 0 or not an int.
1248
+ ValueError: `T_mult` is less than or equal than 1 or not an int.
1249
+ ValueError: `eta_min` is not int or float.
1250
+
1251
+ Supported Platforms:
1252
+ ``Ascend`` ``GPU`` ``CPU``
1253
+
1254
+ Examples:
1255
+ >>> from mindspore.experimental import optim
1256
+ >>> from mindspore import nn
1257
+ >>> net = nn.Dense(3, 2)
1258
+ >>> optimizer = optim.SGD(net.trainable_params(), lr=0.1, momentum=0.9)
1259
+ >>> scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, 2)
1260
+ >>> iters = 3
1261
+ >>> for epoch in range(4):
1262
+ ... for i in range(iters):
1263
+ ... scheduler.step(epoch + i / iters)
1264
+ ... current_lr = scheduler.get_last_lr()
1265
+ ... print(current_lr)
1266
+ [Tensor(shape=[], dtype=Float32, value= 0.1)]
1267
+ [Tensor(shape=[], dtype=Float32, value= 0.0933013)]
1268
+ [Tensor(shape=[], dtype=Float32, value= 0.075)]
1269
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
1270
+ [Tensor(shape=[], dtype=Float32, value= 0.025)]
1271
+ [Tensor(shape=[], dtype=Float32, value= 0.00669873)]
1272
+ """
1273
+ def __init__(self, optimizer, T_0, T_mult=1, eta_min=0, last_epoch=-1):
1274
+ if T_0 <= 0 or not isinstance(T_0, int):
1275
+ raise ValueError("T_0 should be an integer and equal or greater than 0, but got {}".format(T_0))
1276
+ if T_mult < 1 or not isinstance(T_mult, int):
1277
+ raise ValueError("T_mult should be an integer and equal or greater than 1, but got {}".format(T_mult))
1278
+ self.T_0 = Parameter(Tensor(T_0, dtype=mstype.float32), name='T_0')
1279
+ self.T_i = Parameter(Tensor(T_0, dtype=mstype.float32), name='T_i')
1280
+ self.T_mult = T_mult
1281
+ Validator.check_value_type('eta_min', eta_min, [float, int])
1282
+ self.eta_min = Tensor(eta_min)
1283
+ self.T_cur = Parameter(Tensor(last_epoch, dtype=mstype.float32), name='T_cur')
1284
+ self.increase_tensor = Tensor(1, mstype.int32)
1285
+ self.zero_tensor = Tensor(0, mstype.int32)
1286
+
1287
+ self.math_pi = math.pi
1288
+ self.cos = P.Cos()
1289
+ self.cast = P.Cast()
1290
+ self.log = P.Log()
1291
+ self.cast = P.Cast()
1292
+ self.assign = P.Assign()
1293
+ self.floor = P.Floor()
1294
+ self._last_lr = [group["lr"] for group in optimizer.param_groups]
1295
+ super(CosineAnnealingWarmRestarts, self).__init__(optimizer, last_epoch)
1296
+
1297
+ def get_lr(self):
1298
+ pct = self.cast(self.math_pi * self.T_cur / self.T_i, mstype.float32)
1299
+ return [self.eta_min + (base_lr - self.eta_min) * (1 + self.cos(pct)) / 2
1300
+ for base_lr in self.base_lrs]
1301
+
1302
+ def step(self, epoch=None):
1303
+ """
1304
+ Get the current learning rate and change the learning rate.
1305
+
1306
+ Args:
1307
+ epoch (int, optional): The index of the last epoch. Default: ``None``.
1308
+ """
1309
+ if epoch is None and self.last_epoch < 0:
1310
+ epoch = self.zero_tensor
1311
+
1312
+ if epoch is None:
1313
+ epoch = self.last_epoch + 1
1314
+ ops.assign_add(self.T_cur, self.increase_tensor)
1315
+ if self.T_cur >= self.T_i:
1316
+ ops.assign(self.T_cur, self.T_cur - self.T_i)
1317
+ ops.assign(self.T_i, self.T_i * self.T_mult)
1318
+
1319
+ else:
1320
+ if epoch < 0:
1321
+ raise ValueError("epoch should be a non-negative integer, but got {}".format(epoch))
1322
+ epoch = self.cast(epoch, mstype.float32)
1323
+
1324
+ if epoch >= self.T_0:
1325
+ if self.T_mult == 1:
1326
+ ops.assign(self.T_cur, epoch % self.T_0)
1327
+
1328
+ else:
1329
+ exp = int(self.log((epoch / self.T_0 * (self.T_mult - 1) + 1), self.T_mult))
1330
+ value = epoch - self.T_0 * (self.T_mult ** exp - 1) / (self.T_mult - 1)
1331
+ ops.assign(self.T_cur, value)
1332
+ ops.assign(self.T_i, self.T_0 * self.T_mult ** exp)
1333
+
1334
+ else:
1335
+ ops.assign(self.T_i, self.T_0.value())
1336
+ ops.assign(self.T_cur, self.cast(epoch, mstype.float32))
1337
+
1338
+ self.assign(self.last_epoch, self.floor(epoch))
1339
+
1340
+ for i, data in enumerate(zip(self.optimizer.param_groups, self.get_lr())):
1341
+ _, lr = data
1342
+ F.assign(self.optimizer.param_groups[i]["lr"], lr)
1343
+
1344
+
1345
+ @jit_class
1346
+ class CosineAnnealingLR(LRScheduler):
1347
+ r"""
1348
+ Set the learning rate of each parameter group using a cosine annealing lr
1349
+ schedule. Where :math:`\eta_{max}` is set to the initial lr, :math:`\eta_{min}` is the minimum value
1350
+ for learning rate, :math:`\eta_{t}` is the current learning rate, :math:`\T_{max}` is iteration number of cosine
1351
+ function, and :math:`T_{cur}` is the number of epochs since the last restart in SGDR.
1352
+
1353
+ .. math::
1354
+ \begin{aligned}
1355
+ \eta_t & = \eta_{min} + \frac{1}{2}(\eta_{max} - \eta_{min})\left(1
1356
+ + \cos\left(\frac{T_{cur}}{T_{max}}\pi\right)\right),
1357
+ & T_{cur} \neq (2k+1)T_{max}; \\
1358
+ \eta_{t+1} & = \eta_{t} + \frac{1}{2}(\eta_{max} - \eta_{min})
1359
+ \left(1 - \cos\left(\frac{1}{T_{max}}\pi\right)\right),
1360
+ & T_{cur} = (2k+1)T_{max}.
1361
+ \end{aligned}
1362
+
1363
+ For more details, please refer to: `SGDR: Stochastic Gradient Descent with Warm Restarts
1364
+ <https://arxiv.org/abs/1608.03983>`_
1365
+
1366
+ .. warning::
1367
+ This is an experimental lr scheduler module that is subject to change.
1368
+ This module must be used with optimizers in `Experimental Optimizer
1369
+ <https://www.mindspore.cn/docs/en/r2.2/api_python/mindspore.experimental.html#experimental-optimizer>`_ .
1370
+
1371
+ Args:
1372
+ optimizer (:class:`mindspore.experimental.optim.Optimizer`): Wrapped optimizer.
1373
+ T_max (int): Maximum number of iterations.
1374
+ eta_min (float, optional): Minimum learning rate. Default: ``0``.
1375
+ last_epoch (int, optional): The index of the last epoch. Default: ``-1``.
1376
+
1377
+ Supported Platforms:
1378
+ ``Ascend`` ``GPU`` ``CPU``
1379
+
1380
+ Examples:
1381
+ >>> from mindspore.experimental import optim
1382
+ >>> from mindspore import nn
1383
+ >>> net = nn.Dense(3, 2)
1384
+ >>> optimizer = optim.SGD(net.trainable_params(), lr=0.1, momentum=0.9)
1385
+ >>> scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=2)
1386
+ >>>
1387
+ >>> for i in range(6):
1388
+ ... scheduler.step()
1389
+ ... current_lr = scheduler.get_last_lr()
1390
+ ... print(current_lr)
1391
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
1392
+ [Tensor(shape=[], dtype=Float32, value= 0)]
1393
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
1394
+ [Tensor(shape=[], dtype=Float32, value= 0.1)]
1395
+ [Tensor(shape=[], dtype=Float32, value= 0.05)]
1396
+ [Tensor(shape=[], dtype=Float32, value= 0)]
1397
+ """
1398
+ def __init__(self, optimizer, T_max, eta_min=0, last_epoch=-1):
1399
+ self.T_max = T_max
1400
+ self.eta_min = eta_min
1401
+ self.math_pi = math.pi
1402
+ self.cos = P.Cos()
1403
+ self.cast = P.Cast()
1404
+ super(CosineAnnealingLR, self).__init__(optimizer, last_epoch)
1405
+
1406
+ def get_lr(self):
1407
+ lrs = [lr.value() for lr in self._last_lr]
1408
+
1409
+ if self.last_epoch == 0:
1410
+ return lrs
1411
+
1412
+ if (self.last_epoch - 1 - self.T_max) % (2 * self.T_max) == 0:
1413
+ pct_pi = self.cast(self.math_pi / self.T_max, mstype.float32)
1414
+ return [lr + (base_lr - self.eta_min) *
1415
+ (1 - self.cos(pct_pi)) / 2
1416
+ for base_lr, lr in
1417
+ zip(self.base_lrs, lrs)]
1418
+
1419
+ return [(1 + self.cos(self.math_pi * self.last_epoch / self.T_max)) /
1420
+ (1 + self.cos(self.math_pi * (self.last_epoch - 1) / self.T_max)) *
1421
+ (lr - self.eta_min) + self.eta_min
1422
+ for lr in lrs]
1423
+
1424
+ def _get_closed_form_lr(self):
1425
+ return [self.eta_min + (base_lr - self.eta_min) *
1426
+ (1 + self.cos(self.math_pi * self.last_epoch / self.T_max)) / 2
1427
+ for base_lr in self.base_lrs]