mindspore 2.3.0__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1400) hide show
  1. mindspore/.commit_id +1 -0
  2. mindspore/ConcurrencyCheck.dll +0 -0
  3. mindspore/CppBuildInsights.dll +0 -0
  4. mindspore/CppCoreCheck.dll +0 -0
  5. mindspore/EnumIndex.dll +0 -0
  6. mindspore/EspXEngine.dll +0 -0
  7. mindspore/HResultCheck.dll +0 -0
  8. mindspore/KernelTraceControl.dll +0 -0
  9. mindspore/LocalESPC.dll +0 -0
  10. mindspore/Microsoft.Diagnostics.Tracing.EventSource.dll +0 -0
  11. mindspore/Microsoft.VisualStudio.RemoteControl.dll +0 -0
  12. mindspore/Microsoft.VisualStudio.Telemetry.dll +0 -0
  13. mindspore/Microsoft.VisualStudio.Utilities.Internal.dll +0 -0
  14. mindspore/Newtonsoft.Json.dll +0 -0
  15. mindspore/System.Runtime.CompilerServices.Unsafe.dll +0 -0
  16. mindspore/VariantClear.dll +0 -0
  17. mindspore/__init__.py +51 -0
  18. mindspore/_c_dataengine.cp310-win_amd64.pyd +0 -0
  19. mindspore/_c_expression.cp310-win_amd64.pyd +0 -0
  20. mindspore/_c_mindrecord.cp310-win_amd64.pyd +0 -0
  21. mindspore/_check_jit_forbidden_api.py +106 -0
  22. mindspore/_checkparam.py +1378 -0
  23. mindspore/_extends/__init__.py +23 -0
  24. mindspore/_extends/builtin_operations.py +224 -0
  25. mindspore/_extends/graph_kernel/__init__.py +17 -0
  26. mindspore/_extends/graph_kernel/model/__init__.py +19 -0
  27. mindspore/_extends/graph_kernel/model/graph_parallel.py +311 -0
  28. mindspore/_extends/graph_kernel/model/graph_split.py +1348 -0
  29. mindspore/_extends/graph_kernel/model/model.py +553 -0
  30. mindspore/_extends/graph_kernel/model/model_builder.py +216 -0
  31. mindspore/_extends/graph_kernel/parallel_estimate.py +60 -0
  32. mindspore/_extends/graph_kernel/splitter.py +140 -0
  33. mindspore/_extends/graph_kernel/utils.py +28 -0
  34. mindspore/_extends/parallel_compile/__init__.py +19 -0
  35. mindspore/_extends/parallel_compile/akg_compiler/__init__.py +19 -0
  36. mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +269 -0
  37. mindspore/_extends/parallel_compile/akg_compiler/build_tbe_kernel.py +529 -0
  38. mindspore/_extends/parallel_compile/akg_compiler/compiler.py +56 -0
  39. mindspore/_extends/parallel_compile/akg_compiler/gen_custom_op_files.py +96 -0
  40. mindspore/_extends/parallel_compile/akg_compiler/get_file_path.py +36 -0
  41. mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +556 -0
  42. mindspore/_extends/parallel_compile/akg_compiler/util.py +159 -0
  43. mindspore/_extends/parse/__init__.py +49 -0
  44. mindspore/_extends/parse/compile_config.py +258 -0
  45. mindspore/_extends/parse/namespace.py +136 -0
  46. mindspore/_extends/parse/parser.py +1446 -0
  47. mindspore/_extends/parse/resources.py +213 -0
  48. mindspore/_extends/parse/standard_method.py +4437 -0
  49. mindspore/_extends/parse/trope.py +97 -0
  50. mindspore/_extends/pijit/__init__.py +23 -0
  51. mindspore/_extends/pijit/pijit_func_white_list.py +343 -0
  52. mindspore/_extends/remote/__init__.py +19 -0
  53. mindspore/_extends/remote/kernel_build_server.py +199 -0
  54. mindspore/_extends/remote/kernel_build_server_akg.py +55 -0
  55. mindspore/_extends/remote/kernel_build_server_akg_v2.py +55 -0
  56. mindspore/_extends/remote/kernel_build_server_ascend.py +75 -0
  57. mindspore/_extends/utils.py +68 -0
  58. mindspore/_install_custom.py +43 -0
  59. mindspore/_profiler.py +30 -0
  60. mindspore/amp.py +419 -0
  61. mindspore/atlprov.dll +0 -0
  62. mindspore/avcodec-59.dll +0 -0
  63. mindspore/avdevice-59.dll +0 -0
  64. mindspore/avfilter-8.dll +0 -0
  65. mindspore/avformat-59.dll +0 -0
  66. mindspore/avutil-57.dll +0 -0
  67. mindspore/boost/__init__.py +42 -0
  68. mindspore/boost/adasum.py +319 -0
  69. mindspore/boost/base.py +535 -0
  70. mindspore/boost/boost.py +400 -0
  71. mindspore/boost/boost_cell_wrapper.py +790 -0
  72. mindspore/boost/dim_reduce.py +323 -0
  73. mindspore/boost/grad_accumulation.py +79 -0
  74. mindspore/boost/grad_freeze.py +382 -0
  75. mindspore/boost/group_loss_scale_manager.py +166 -0
  76. mindspore/boost/less_batch_normalization.py +174 -0
  77. mindspore/c1.dll +0 -0
  78. mindspore/c1xx.dll +0 -0
  79. mindspore/c2.dll +0 -0
  80. mindspore/cfgpersist.dll +0 -0
  81. mindspore/clang_rt.asan_dbg_dynamic-x86_64.dll +0 -0
  82. mindspore/clang_rt.asan_dynamic-x86_64.dll +0 -0
  83. mindspore/common/__init__.py +84 -0
  84. mindspore/common/_auto_dynamic.py +68 -0
  85. mindspore/common/_decorator.py +50 -0
  86. mindspore/common/_jit_fallback_utils.py +110 -0
  87. mindspore/common/_monad.py +25 -0
  88. mindspore/common/_register_for_adapter.py +74 -0
  89. mindspore/common/_register_for_recompute.py +48 -0
  90. mindspore/common/_register_for_tensor.py +45 -0
  91. mindspore/common/_stub_tensor.py +210 -0
  92. mindspore/common/_utils.py +122 -0
  93. mindspore/common/api.py +2049 -0
  94. mindspore/common/auto_dynamic_shape.py +507 -0
  95. mindspore/common/dtype.py +422 -0
  96. mindspore/common/dump.py +131 -0
  97. mindspore/common/file_system.py +48 -0
  98. mindspore/common/generator.py +260 -0
  99. mindspore/common/hook_handle.py +155 -0
  100. mindspore/common/initializer.py +880 -0
  101. mindspore/common/jit_config.py +98 -0
  102. mindspore/common/lazy_inline.py +240 -0
  103. mindspore/common/mindir_util.py +111 -0
  104. mindspore/common/mutable.py +234 -0
  105. mindspore/common/no_inline.py +54 -0
  106. mindspore/common/np_dtype.py +25 -0
  107. mindspore/common/parameter.py +1048 -0
  108. mindspore/common/recompute.py +262 -0
  109. mindspore/common/seed.py +260 -0
  110. mindspore/common/sparse_tensor.py +1171 -0
  111. mindspore/common/symbol.py +122 -0
  112. mindspore/common/tensor.py +4859 -0
  113. mindspore/communication/__init__.py +37 -0
  114. mindspore/communication/_comm_helper.py +466 -0
  115. mindspore/communication/_hccl_management.py +297 -0
  116. mindspore/communication/comm_func.py +1140 -0
  117. mindspore/communication/management.py +673 -0
  118. mindspore/config/op_info.config +533 -0
  119. mindspore/context.py +1976 -0
  120. mindspore/d3dcompiler_47.dll +0 -0
  121. mindspore/dataset/__init__.py +90 -0
  122. mindspore/dataset/audio/__init__.py +61 -0
  123. mindspore/dataset/audio/transforms.py +3690 -0
  124. mindspore/dataset/audio/utils.py +386 -0
  125. mindspore/dataset/audio/validators.py +1172 -0
  126. mindspore/dataset/callback/__init__.py +20 -0
  127. mindspore/dataset/callback/ds_callback.py +368 -0
  128. mindspore/dataset/callback/validators.py +32 -0
  129. mindspore/dataset/core/__init__.py +13 -0
  130. mindspore/dataset/core/config.py +1088 -0
  131. mindspore/dataset/core/datatypes.py +101 -0
  132. mindspore/dataset/core/py_util_helpers.py +65 -0
  133. mindspore/dataset/core/validator_helpers.py +774 -0
  134. mindspore/dataset/debug/__init__.py +21 -0
  135. mindspore/dataset/debug/debug_hook.py +97 -0
  136. mindspore/dataset/debug/pre_defined_hook.py +67 -0
  137. mindspore/dataset/engine/__init__.py +124 -0
  138. mindspore/dataset/engine/cache_admin.py +47 -0
  139. mindspore/dataset/engine/cache_client.py +129 -0
  140. mindspore/dataset/engine/datasets.py +4554 -0
  141. mindspore/dataset/engine/datasets_audio.py +911 -0
  142. mindspore/dataset/engine/datasets_standard_format.py +493 -0
  143. mindspore/dataset/engine/datasets_text.py +2161 -0
  144. mindspore/dataset/engine/datasets_user_defined.py +1114 -0
  145. mindspore/dataset/engine/datasets_vision.py +4816 -0
  146. mindspore/dataset/engine/iterators.py +342 -0
  147. mindspore/dataset/engine/obs/__init__.py +23 -0
  148. mindspore/dataset/engine/obs/config_loader.py +68 -0
  149. mindspore/dataset/engine/obs/obs_mindrecord_dataset.py +508 -0
  150. mindspore/dataset/engine/obs/util.py +475 -0
  151. mindspore/dataset/engine/offload.py +596 -0
  152. mindspore/dataset/engine/queue.py +250 -0
  153. mindspore/dataset/engine/samplers.py +895 -0
  154. mindspore/dataset/engine/serializer_deserializer.py +159 -0
  155. mindspore/dataset/engine/validators.py +2875 -0
  156. mindspore/dataset/text/__init__.py +54 -0
  157. mindspore/dataset/text/transforms.py +1703 -0
  158. mindspore/dataset/text/utils.py +715 -0
  159. mindspore/dataset/text/validators.py +642 -0
  160. mindspore/dataset/transforms/__init__.py +48 -0
  161. mindspore/dataset/transforms/c_transforms.py +638 -0
  162. mindspore/dataset/transforms/py_transforms.py +393 -0
  163. mindspore/dataset/transforms/py_transforms_util.py +255 -0
  164. mindspore/dataset/transforms/transforms.py +1260 -0
  165. mindspore/dataset/transforms/validators.py +410 -0
  166. mindspore/dataset/utils/__init__.py +19 -0
  167. mindspore/dataset/utils/browse_dataset.py +190 -0
  168. mindspore/dataset/utils/line_reader.py +124 -0
  169. mindspore/dataset/vision/__init__.py +68 -0
  170. mindspore/dataset/vision/c_transforms.py +2641 -0
  171. mindspore/dataset/vision/py_transforms.py +2120 -0
  172. mindspore/dataset/vision/py_transforms_util.py +1660 -0
  173. mindspore/dataset/vision/transforms.py +7295 -0
  174. mindspore/dataset/vision/utils.py +863 -0
  175. mindspore/dataset/vision/validators.py +1482 -0
  176. mindspore/default_config.py +2 -0
  177. mindspore/dnnl.dll +0 -0
  178. mindspore/dpcmi.dll +0 -0
  179. mindspore/experimental/__init__.py +20 -0
  180. mindspore/experimental/map_parameter.py +309 -0
  181. mindspore/experimental/optim/__init__.py +40 -0
  182. mindspore/experimental/optim/adadelta.py +161 -0
  183. mindspore/experimental/optim/adagrad.py +168 -0
  184. mindspore/experimental/optim/adam.py +193 -0
  185. mindspore/experimental/optim/adamax.py +170 -0
  186. mindspore/experimental/optim/adamw.py +205 -0
  187. mindspore/experimental/optim/asgd.py +153 -0
  188. mindspore/experimental/optim/lr_scheduler.py +1371 -0
  189. mindspore/experimental/optim/nadam.py +157 -0
  190. mindspore/experimental/optim/optimizer.py +259 -0
  191. mindspore/experimental/optim/radam.py +194 -0
  192. mindspore/experimental/optim/rmsprop.py +154 -0
  193. mindspore/experimental/optim/rprop.py +164 -0
  194. mindspore/experimental/optim/sgd.py +156 -0
  195. mindspore/hal/__init__.py +40 -0
  196. mindspore/hal/_ascend.py +57 -0
  197. mindspore/hal/_base.py +57 -0
  198. mindspore/hal/_cpu.py +56 -0
  199. mindspore/hal/_gpu.py +57 -0
  200. mindspore/hal/device.py +356 -0
  201. mindspore/hal/event.py +179 -0
  202. mindspore/hal/memory.py +326 -0
  203. mindspore/hal/stream.py +339 -0
  204. mindspore/include/OWNERS +7 -0
  205. mindspore/include/api/allocator.h +97 -0
  206. mindspore/include/api/callback/callback.h +93 -0
  207. mindspore/include/api/callback/ckpt_saver.h +41 -0
  208. mindspore/include/api/callback/loss_monitor.h +33 -0
  209. mindspore/include/api/callback/lr_scheduler.h +51 -0
  210. mindspore/include/api/callback/time_monitor.h +34 -0
  211. mindspore/include/api/callback/train_accuracy.h +37 -0
  212. mindspore/include/api/cell.h +90 -0
  213. mindspore/include/api/cfg.h +82 -0
  214. mindspore/include/api/context.h +602 -0
  215. mindspore/include/api/data_type.h +47 -0
  216. mindspore/include/api/delegate.h +178 -0
  217. mindspore/include/api/delegate_api.h +75 -0
  218. mindspore/include/api/dual_abi_helper.h +208 -0
  219. mindspore/include/api/format.h +28 -0
  220. mindspore/include/api/graph.h +46 -0
  221. mindspore/include/api/kernel.h +58 -0
  222. mindspore/include/api/kernel_api.h +168 -0
  223. mindspore/include/api/metrics/accuracy.h +36 -0
  224. mindspore/include/api/metrics/metrics.h +41 -0
  225. mindspore/include/api/model.h +438 -0
  226. mindspore/include/api/model_group.h +79 -0
  227. mindspore/include/api/model_parallel_runner.h +168 -0
  228. mindspore/include/api/serialization.h +185 -0
  229. mindspore/include/api/status.h +192 -0
  230. mindspore/include/api/types.h +431 -0
  231. mindspore/include/api/visible.h +41 -0
  232. mindspore/include/c_api/context_c.h +179 -0
  233. mindspore/include/c_api/data_type_c.h +52 -0
  234. mindspore/include/c_api/format_c.h +46 -0
  235. mindspore/include/c_api/model_c.h +347 -0
  236. mindspore/include/c_api/ms/abstract.h +67 -0
  237. mindspore/include/c_api/ms/attribute.h +197 -0
  238. mindspore/include/c_api/ms/base/handle_types.h +43 -0
  239. mindspore/include/c_api/ms/base/macros.h +32 -0
  240. mindspore/include/c_api/ms/base/status.h +33 -0
  241. mindspore/include/c_api/ms/base/types.h +283 -0
  242. mindspore/include/c_api/ms/context.h +102 -0
  243. mindspore/include/c_api/ms/graph.h +160 -0
  244. mindspore/include/c_api/ms/node.h +606 -0
  245. mindspore/include/c_api/ms/tensor.h +161 -0
  246. mindspore/include/c_api/ms/value.h +84 -0
  247. mindspore/include/c_api/status_c.h +79 -0
  248. mindspore/include/c_api/tensor_c.h +146 -0
  249. mindspore/include/c_api/types_c.h +67 -0
  250. mindspore/include/dataset/config.h +163 -0
  251. mindspore/include/dataset/constants.h +363 -0
  252. mindspore/include/dataset/execute.h +196 -0
  253. mindspore/include/dataset/text.h +1092 -0
  254. mindspore/include/dataset/transforms.h +638 -0
  255. mindspore/include/dataset/vision.h +2125 -0
  256. mindspore/include/dataset/vision_ascend.h +206 -0
  257. mindspore/include/dataset/vision_lite.h +625 -0
  258. mindspore/jpeg62.dll +0 -0
  259. mindspore/log.py +633 -0
  260. mindspore/mindrecord/__init__.py +43 -0
  261. mindspore/mindrecord/common/__init__.py +17 -0
  262. mindspore/mindrecord/common/constant.py +20 -0
  263. mindspore/mindrecord/common/enums.py +44 -0
  264. mindspore/mindrecord/common/exceptions.py +311 -0
  265. mindspore/mindrecord/config.py +809 -0
  266. mindspore/mindrecord/filereader.py +174 -0
  267. mindspore/mindrecord/filewriter.py +705 -0
  268. mindspore/mindrecord/mindpage.py +210 -0
  269. mindspore/mindrecord/shardheader.py +141 -0
  270. mindspore/mindrecord/shardindexgenerator.py +74 -0
  271. mindspore/mindrecord/shardreader.py +117 -0
  272. mindspore/mindrecord/shardsegment.py +128 -0
  273. mindspore/mindrecord/shardutils.py +185 -0
  274. mindspore/mindrecord/shardwriter.py +237 -0
  275. mindspore/mindrecord/tools/__init__.py +17 -0
  276. mindspore/mindrecord/tools/cifar10.py +140 -0
  277. mindspore/mindrecord/tools/cifar100.py +153 -0
  278. mindspore/mindrecord/tools/cifar100_to_mr.py +185 -0
  279. mindspore/mindrecord/tools/cifar10_to_mr.py +177 -0
  280. mindspore/mindrecord/tools/csv_to_mr.py +200 -0
  281. mindspore/mindrecord/tools/imagenet_to_mr.py +206 -0
  282. mindspore/mindrecord/tools/mnist_to_mr.py +259 -0
  283. mindspore/mindrecord/tools/tfrecord_to_mr.py +360 -0
  284. mindspore/mindspore_backend.dll +0 -0
  285. mindspore/mindspore_common.dll +0 -0
  286. mindspore/mindspore_core.dll +0 -0
  287. mindspore/mindspore_glog.dll +0 -0
  288. mindspore/mindspore_np_dtype.dll +0 -0
  289. mindspore/mindspore_shared_lib.dll +0 -0
  290. mindspore/mint/__init__.py +1137 -0
  291. mindspore/mint/linalg/__init__.py +22 -0
  292. mindspore/mint/nn/__init__.py +512 -0
  293. mindspore/mint/nn/functional.py +573 -0
  294. mindspore/mint/optim/__init__.py +24 -0
  295. mindspore/mint/optim/adamw.py +185 -0
  296. mindspore/msobj140.dll +0 -0
  297. mindspore/mspdb140.dll +0 -0
  298. mindspore/mspdbcore.dll +0 -0
  299. mindspore/mspdbst.dll +0 -0
  300. mindspore/mspft140.dll +0 -0
  301. mindspore/msvcdis140.dll +0 -0
  302. mindspore/msvcp140.dll +0 -0
  303. mindspore/msvcp140_1.dll +0 -0
  304. mindspore/msvcp140_2.dll +0 -0
  305. mindspore/msvcp140_atomic_wait.dll +0 -0
  306. mindspore/msvcp140_codecvt_ids.dll +0 -0
  307. mindspore/multiprocessing/__init__.py +72 -0
  308. mindspore/nn/__init__.py +48 -0
  309. mindspore/nn/cell.py +2605 -0
  310. mindspore/nn/dynamic_lr.py +482 -0
  311. mindspore/nn/extend/__init__.py +29 -0
  312. mindspore/nn/extend/basic.py +140 -0
  313. mindspore/nn/extend/embedding.py +143 -0
  314. mindspore/nn/extend/layer/__init__.py +27 -0
  315. mindspore/nn/extend/layer/normalization.py +109 -0
  316. mindspore/nn/extend/pooling.py +117 -0
  317. mindspore/nn/grad/__init__.py +21 -0
  318. mindspore/nn/grad/cell_grad.py +196 -0
  319. mindspore/nn/layer/__init__.py +63 -0
  320. mindspore/nn/layer/activation.py +1655 -0
  321. mindspore/nn/layer/basic.py +1519 -0
  322. mindspore/nn/layer/channel_shuffle.py +90 -0
  323. mindspore/nn/layer/combined.py +248 -0
  324. mindspore/nn/layer/container.py +734 -0
  325. mindspore/nn/layer/conv.py +1505 -0
  326. mindspore/nn/layer/dense.py +204 -0
  327. mindspore/nn/layer/embedding.py +751 -0
  328. mindspore/nn/layer/embedding_service.py +531 -0
  329. mindspore/nn/layer/embedding_service_layer.py +393 -0
  330. mindspore/nn/layer/image.py +661 -0
  331. mindspore/nn/layer/math.py +1069 -0
  332. mindspore/nn/layer/normalization.py +1177 -0
  333. mindspore/nn/layer/padding.py +894 -0
  334. mindspore/nn/layer/pooling.py +2148 -0
  335. mindspore/nn/layer/rnn_cells.py +388 -0
  336. mindspore/nn/layer/rnns.py +849 -0
  337. mindspore/nn/layer/thor_layer.py +963 -0
  338. mindspore/nn/layer/timedistributed.py +155 -0
  339. mindspore/nn/layer/transformer.py +823 -0
  340. mindspore/nn/learning_rate_schedule.py +512 -0
  341. mindspore/nn/loss/__init__.py +36 -0
  342. mindspore/nn/loss/loss.py +2846 -0
  343. mindspore/nn/metrics.py +53 -0
  344. mindspore/nn/optim/__init__.py +44 -0
  345. mindspore/nn/optim/_dist_optimizer_registry.py +111 -0
  346. mindspore/nn/optim/ada_grad.py +217 -0
  347. mindspore/nn/optim/adadelta.py +206 -0
  348. mindspore/nn/optim/adafactor.py +448 -0
  349. mindspore/nn/optim/adam.py +1297 -0
  350. mindspore/nn/optim/adamax.py +220 -0
  351. mindspore/nn/optim/adasum.py +548 -0
  352. mindspore/nn/optim/asgd.py +216 -0
  353. mindspore/nn/optim/ftrl.py +401 -0
  354. mindspore/nn/optim/lamb.py +296 -0
  355. mindspore/nn/optim/lars.py +202 -0
  356. mindspore/nn/optim/lazyadam.py +533 -0
  357. mindspore/nn/optim/momentum.py +239 -0
  358. mindspore/nn/optim/optimizer.py +1034 -0
  359. mindspore/nn/optim/proximal_ada_grad.py +242 -0
  360. mindspore/nn/optim/rmsprop.py +264 -0
  361. mindspore/nn/optim/rprop.py +251 -0
  362. mindspore/nn/optim/sgd.py +237 -0
  363. mindspore/nn/optim/thor.py +1310 -0
  364. mindspore/nn/probability/__init__.py +22 -0
  365. mindspore/nn/probability/bijector/__init__.py +35 -0
  366. mindspore/nn/probability/bijector/bijector.py +337 -0
  367. mindspore/nn/probability/bijector/exp.py +65 -0
  368. mindspore/nn/probability/bijector/gumbel_cdf.py +144 -0
  369. mindspore/nn/probability/bijector/invert.py +126 -0
  370. mindspore/nn/probability/bijector/power_transform.py +196 -0
  371. mindspore/nn/probability/bijector/scalar_affine.py +167 -0
  372. mindspore/nn/probability/bijector/softplus.py +189 -0
  373. mindspore/nn/probability/bnn_layers/__init__.py +29 -0
  374. mindspore/nn/probability/bnn_layers/_util.py +46 -0
  375. mindspore/nn/probability/bnn_layers/bnn_cell_wrapper.py +112 -0
  376. mindspore/nn/probability/bnn_layers/conv_variational.py +267 -0
  377. mindspore/nn/probability/bnn_layers/dense_variational.py +302 -0
  378. mindspore/nn/probability/bnn_layers/layer_distribution.py +123 -0
  379. mindspore/nn/probability/distribution/__init__.py +56 -0
  380. mindspore/nn/probability/distribution/_utils/__init__.py +34 -0
  381. mindspore/nn/probability/distribution/_utils/custom_ops.py +96 -0
  382. mindspore/nn/probability/distribution/_utils/utils.py +362 -0
  383. mindspore/nn/probability/distribution/bernoulli.py +334 -0
  384. mindspore/nn/probability/distribution/beta.py +391 -0
  385. mindspore/nn/probability/distribution/categorical.py +435 -0
  386. mindspore/nn/probability/distribution/cauchy.py +383 -0
  387. mindspore/nn/probability/distribution/distribution.py +827 -0
  388. mindspore/nn/probability/distribution/exponential.py +350 -0
  389. mindspore/nn/probability/distribution/gamma.py +391 -0
  390. mindspore/nn/probability/distribution/geometric.py +335 -0
  391. mindspore/nn/probability/distribution/gumbel.py +257 -0
  392. mindspore/nn/probability/distribution/half_normal.py +133 -0
  393. mindspore/nn/probability/distribution/laplace.py +128 -0
  394. mindspore/nn/probability/distribution/log_normal.py +272 -0
  395. mindspore/nn/probability/distribution/logistic.py +379 -0
  396. mindspore/nn/probability/distribution/normal.py +336 -0
  397. mindspore/nn/probability/distribution/poisson.py +288 -0
  398. mindspore/nn/probability/distribution/student_t.py +149 -0
  399. mindspore/nn/probability/distribution/transformed_distribution.py +235 -0
  400. mindspore/nn/probability/distribution/uniform.py +375 -0
  401. mindspore/nn/reinforcement/__init__.py +24 -0
  402. mindspore/nn/reinforcement/_batch_read_write.py +142 -0
  403. mindspore/nn/reinforcement/_tensors_queue.py +152 -0
  404. mindspore/nn/reinforcement/tensor_array.py +145 -0
  405. mindspore/nn/sparse/__init__.py +23 -0
  406. mindspore/nn/sparse/sparse.py +147 -0
  407. mindspore/nn/wrap/__init__.py +49 -0
  408. mindspore/nn/wrap/cell_wrapper.py +979 -0
  409. mindspore/nn/wrap/grad_reducer.py +608 -0
  410. mindspore/nn/wrap/loss_scale.py +680 -0
  411. mindspore/numpy/__init__.py +121 -0
  412. mindspore/numpy/array_creations.py +2734 -0
  413. mindspore/numpy/array_ops.py +2625 -0
  414. mindspore/numpy/dtypes.py +185 -0
  415. mindspore/numpy/fft.py +431 -0
  416. mindspore/numpy/logic_ops.py +935 -0
  417. mindspore/numpy/math_ops.py +5910 -0
  418. mindspore/numpy/utils.py +214 -0
  419. mindspore/numpy/utils_const.py +565 -0
  420. mindspore/opencv_core452.dll +0 -0
  421. mindspore/opencv_imgcodecs452.dll +0 -0
  422. mindspore/opencv_imgproc452.dll +0 -0
  423. mindspore/ops/__init__.py +54 -0
  424. mindspore/ops/_constants.py +30 -0
  425. mindspore/ops/_grad_experimental/__init__.py +31 -0
  426. mindspore/ops/_grad_experimental/grad_array_ops.py +830 -0
  427. mindspore/ops/_grad_experimental/grad_base.py +143 -0
  428. mindspore/ops/_grad_experimental/grad_comm_ops.py +670 -0
  429. mindspore/ops/_grad_experimental/grad_debug_ops.py +31 -0
  430. mindspore/ops/_grad_experimental/grad_implementations.py +203 -0
  431. mindspore/ops/_grad_experimental/grad_inner_ops.py +79 -0
  432. mindspore/ops/_grad_experimental/grad_math_ops.py +824 -0
  433. mindspore/ops/_grad_experimental/grad_nn_ops.py +231 -0
  434. mindspore/ops/_grad_experimental/grad_quant_ops.py +238 -0
  435. mindspore/ops/_grad_experimental/grad_sparse.py +342 -0
  436. mindspore/ops/_grad_experimental/grad_sparse_ops.py +399 -0
  437. mindspore/ops/_grad_experimental/taylor_rule.py +220 -0
  438. mindspore/ops/_op_impl/__init__.py +23 -0
  439. mindspore/ops/_op_impl/_custom_op/__init__.py +39 -0
  440. mindspore/ops/_op_impl/_custom_op/_basic.py +158 -0
  441. mindspore/ops/_op_impl/_custom_op/batch_matmul_impl.py +279 -0
  442. mindspore/ops/_op_impl/_custom_op/batchnorm_fold.py +156 -0
  443. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2.py +109 -0
  444. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad.py +125 -0
  445. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad_reduce.py +105 -0
  446. mindspore/ops/_op_impl/_custom_op/batchnorm_fold_grad.py +124 -0
  447. mindspore/ops/_op_impl/_custom_op/cholesky_trsm_impl.py +116 -0
  448. mindspore/ops/_op_impl/_custom_op/correction_mul.py +89 -0
  449. mindspore/ops/_op_impl/_custom_op/correction_mul_grad.py +196 -0
  450. mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +366 -0
  451. mindspore/ops/_op_impl/_custom_op/dsd_impl.py +162 -0
  452. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel.py +136 -0
  453. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad.py +206 -0
  454. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad_reduce.py +88 -0
  455. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer.py +128 -0
  456. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad.py +199 -0
  457. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad_reduce.py +88 -0
  458. mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel.py +156 -0
  459. mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel_grad.py +184 -0
  460. mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer.py +143 -0
  461. mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer_grad.py +169 -0
  462. mindspore/ops/_op_impl/_custom_op/fused_abs_max1_impl.py +548 -0
  463. mindspore/ops/_op_impl/_custom_op/img2col_impl.py +881 -0
  464. mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_left_impl.py +278 -0
  465. mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_right_impl.py +200 -0
  466. mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_left_cast_impl.py +334 -0
  467. mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_right_mul_impl.py +255 -0
  468. mindspore/ops/_op_impl/_custom_op/matmul_cube_impl.py +222 -0
  469. mindspore/ops/_op_impl/_custom_op/matmul_dds_grad_impl.py +644 -0
  470. mindspore/ops/_op_impl/_custom_op/matmul_dds_impl.py +488 -0
  471. mindspore/ops/_op_impl/_custom_op/matrix_combine_impl.py +87 -0
  472. mindspore/ops/_op_impl/_custom_op/minmax_update_perchannel.py +129 -0
  473. mindspore/ops/_op_impl/_custom_op/minmax_update_perlayer.py +121 -0
  474. mindspore/ops/_op_impl/_custom_op/transpose02314_impl.py +352 -0
  475. mindspore/ops/_op_impl/aicpu/__init__.py +441 -0
  476. mindspore/ops/_op_impl/aicpu/abs.py +36 -0
  477. mindspore/ops/_op_impl/aicpu/acos.py +32 -0
  478. mindspore/ops/_op_impl/aicpu/acos_grad.py +33 -0
  479. mindspore/ops/_op_impl/aicpu/acosh.py +34 -0
  480. mindspore/ops/_op_impl/aicpu/acosh_grad.py +35 -0
  481. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d.py +34 -0
  482. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d_grad.py +34 -0
  483. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d.py +39 -0
  484. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d_grad.py +39 -0
  485. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d.py +37 -0
  486. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d_grad.py +37 -0
  487. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d.py +42 -0
  488. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d_grad.py +152 -0
  489. mindspore/ops/_op_impl/aicpu/add.py +43 -0
  490. mindspore/ops/_op_impl/aicpu/add_n.py +41 -0
  491. mindspore/ops/_op_impl/aicpu/add_v2.py +40 -0
  492. mindspore/ops/_op_impl/aicpu/addcdiv.py +41 -0
  493. mindspore/ops/_op_impl/aicpu/addcmul.py +47 -0
  494. mindspore/ops/_op_impl/aicpu/adjust_contrastv2.py +32 -0
  495. mindspore/ops/_op_impl/aicpu/adjust_hue.py +31 -0
  496. mindspore/ops/_op_impl/aicpu/adjust_saturation.py +32 -0
  497. mindspore/ops/_op_impl/aicpu/affine_grid.py +33 -0
  498. mindspore/ops/_op_impl/aicpu/affine_grid_grad.py +35 -0
  499. mindspore/ops/_op_impl/aicpu/angle.py +31 -0
  500. mindspore/ops/_op_impl/aicpu/arg_max.py +75 -0
  501. mindspore/ops/_op_impl/aicpu/arg_min.py +75 -0
  502. mindspore/ops/_op_impl/aicpu/argmax_with_value.py +43 -0
  503. mindspore/ops/_op_impl/aicpu/argmin_with_value.py +43 -0
  504. mindspore/ops/_op_impl/aicpu/asin.py +32 -0
  505. mindspore/ops/_op_impl/aicpu/asin_grad.py +33 -0
  506. mindspore/ops/_op_impl/aicpu/asinh.py +34 -0
  507. mindspore/ops/_op_impl/aicpu/asinh_grad.py +35 -0
  508. mindspore/ops/_op_impl/aicpu/atanh.py +34 -0
  509. mindspore/ops/_op_impl/aicpu/avgpool_grad_v1.py +37 -0
  510. mindspore/ops/_op_impl/aicpu/avgpool_v1.py +36 -0
  511. mindspore/ops/_op_impl/aicpu/bartlett_window.py +36 -0
  512. mindspore/ops/_op_impl/aicpu/batch_matmul.py +43 -0
  513. mindspore/ops/_op_impl/aicpu/batch_norm_grad_grad.py +49 -0
  514. mindspore/ops/_op_impl/aicpu/bernoulli.py +48 -0
  515. mindspore/ops/_op_impl/aicpu/bessel_i0.py +31 -0
  516. mindspore/ops/_op_impl/aicpu/betainc.py +31 -0
  517. mindspore/ops/_op_impl/aicpu/bias_add.py +44 -0
  518. mindspore/ops/_op_impl/aicpu/bias_add_grad.py +42 -0
  519. mindspore/ops/_op_impl/aicpu/bincount.py +33 -0
  520. mindspore/ops/_op_impl/aicpu/blackman_window.py +36 -0
  521. mindspore/ops/_op_impl/aicpu/broadcast_to.py +58 -0
  522. mindspore/ops/_op_impl/aicpu/bucketize.py +34 -0
  523. mindspore/ops/_op_impl/aicpu/cache_swap_table.py +102 -0
  524. mindspore/ops/_op_impl/aicpu/cast.py +225 -0
  525. mindspore/ops/_op_impl/aicpu/cauchy.py +33 -0
  526. mindspore/ops/_op_impl/aicpu/channel_shuffle.py +40 -0
  527. mindspore/ops/_op_impl/aicpu/check_numerics.py +33 -0
  528. mindspore/ops/_op_impl/aicpu/cholesky.py +32 -0
  529. mindspore/ops/_op_impl/aicpu/cholesky_inverse.py +31 -0
  530. mindspore/ops/_op_impl/aicpu/cholesky_solve.py +33 -0
  531. mindspore/ops/_op_impl/aicpu/choleskygrad.py +32 -0
  532. mindspore/ops/_op_impl/aicpu/coalesce.py +37 -0
  533. mindspore/ops/_op_impl/aicpu/col2im.py +38 -0
  534. mindspore/ops/_op_impl/aicpu/combined_non_max_suppression.py +42 -0
  535. mindspore/ops/_op_impl/aicpu/compare_and_bitpack.py +37 -0
  536. mindspore/ops/_op_impl/aicpu/complex.py +32 -0
  537. mindspore/ops/_op_impl/aicpu/complex_abs.py +31 -0
  538. mindspore/ops/_op_impl/aicpu/compute_accidental_hits.py +44 -0
  539. mindspore/ops/_op_impl/aicpu/concat.py +57 -0
  540. mindspore/ops/_op_impl/aicpu/concat_offset.py +42 -0
  541. mindspore/ops/_op_impl/aicpu/concat_offset_v1.py +31 -0
  542. mindspore/ops/_op_impl/aicpu/conj.py +42 -0
  543. mindspore/ops/_op_impl/aicpu/conjugate_transpose.py +58 -0
  544. mindspore/ops/_op_impl/aicpu/cos.py +34 -0
  545. mindspore/ops/_op_impl/aicpu/cosh.py +34 -0
  546. mindspore/ops/_op_impl/aicpu/count_nonzero.py +43 -0
  547. mindspore/ops/_op_impl/aicpu/crop_and_resize.py +69 -0
  548. mindspore/ops/_op_impl/aicpu/crop_and_resize_grad_boxes.py +68 -0
  549. mindspore/ops/_op_impl/aicpu/crop_and_resize_grad_image.py +38 -0
  550. mindspore/ops/_op_impl/aicpu/cross.py +42 -0
  551. mindspore/ops/_op_impl/aicpu/csr_sparse_matrix_to_dense.py +48 -0
  552. mindspore/ops/_op_impl/aicpu/csr_sparse_matrix_to_sparse_tensor.py +51 -0
  553. mindspore/ops/_op_impl/aicpu/ctc_greedy_decoder.py +35 -0
  554. mindspore/ops/_op_impl/aicpu/ctc_loss_v2.py +43 -0
  555. mindspore/ops/_op_impl/aicpu/ctc_loss_v2_grad.py +45 -0
  556. mindspore/ops/_op_impl/aicpu/ctcloss.py +38 -0
  557. mindspore/ops/_op_impl/aicpu/cummax.py +41 -0
  558. mindspore/ops/_op_impl/aicpu/cumprod.py +58 -0
  559. mindspore/ops/_op_impl/aicpu/cumsum.py +58 -0
  560. mindspore/ops/_op_impl/aicpu/cumulative_logsumexp.py +36 -0
  561. mindspore/ops/_op_impl/aicpu/data_format_vec_permute.py +32 -0
  562. mindspore/ops/_op_impl/aicpu/deformable_offsets.py +38 -0
  563. mindspore/ops/_op_impl/aicpu/deformable_offsets_grad.py +43 -0
  564. mindspore/ops/_op_impl/aicpu/dense_to_csr_sparse_matrix.py +49 -0
  565. mindspore/ops/_op_impl/aicpu/dense_to_dense_set_operation.py +45 -0
  566. mindspore/ops/_op_impl/aicpu/dense_to_sparse_set_operation.py +48 -0
  567. mindspore/ops/_op_impl/aicpu/depth_to_space.py +44 -0
  568. mindspore/ops/_op_impl/aicpu/diag.py +36 -0
  569. mindspore/ops/_op_impl/aicpu/diag_part.py +36 -0
  570. mindspore/ops/_op_impl/aicpu/diagonal.py +35 -0
  571. mindspore/ops/_op_impl/aicpu/digamma.py +31 -0
  572. mindspore/ops/_op_impl/aicpu/div.py +41 -0
  573. mindspore/ops/_op_impl/aicpu/div_no_nan.py +35 -0
  574. mindspore/ops/_op_impl/aicpu/dropout2d.py +42 -0
  575. mindspore/ops/_op_impl/aicpu/dropout3d.py +42 -0
  576. mindspore/ops/_op_impl/aicpu/dropout_genmask.py +41 -0
  577. mindspore/ops/_op_impl/aicpu/dropout_genmask_v3.py +32 -0
  578. mindspore/ops/_op_impl/aicpu/dynamic_stitch.py +42 -0
  579. mindspore/ops/_op_impl/aicpu/edit_distance.py +56 -0
  580. mindspore/ops/_op_impl/aicpu/eig.py +35 -0
  581. mindspore/ops/_op_impl/aicpu/embedding_lookup.py +102 -0
  582. mindspore/ops/_op_impl/aicpu/end_of_sequence.py +30 -0
  583. mindspore/ops/_op_impl/aicpu/environ_create.py +28 -0
  584. mindspore/ops/_op_impl/aicpu/environ_destroy_all.py +28 -0
  585. mindspore/ops/_op_impl/aicpu/environ_get.py +41 -0
  586. mindspore/ops/_op_impl/aicpu/environ_set.py +40 -0
  587. mindspore/ops/_op_impl/aicpu/eps.py +32 -0
  588. mindspore/ops/_op_impl/aicpu/equal.py +41 -0
  589. mindspore/ops/_op_impl/aicpu/exp.py +37 -0
  590. mindspore/ops/_op_impl/aicpu/expand.py +45 -0
  591. mindspore/ops/_op_impl/aicpu/expand_dims.py +42 -0
  592. mindspore/ops/_op_impl/aicpu/expm1.py +34 -0
  593. mindspore/ops/_op_impl/aicpu/extract_glimpse.py +35 -0
  594. mindspore/ops/_op_impl/aicpu/eye.py +44 -0
  595. mindspore/ops/_op_impl/aicpu/fft_with_size.py +47 -0
  596. mindspore/ops/_op_impl/aicpu/fill_diagonal.py +39 -0
  597. mindspore/ops/_op_impl/aicpu/fill_v2.py +58 -0
  598. mindspore/ops/_op_impl/aicpu/flatten.py +43 -0
  599. mindspore/ops/_op_impl/aicpu/floor_div.py +38 -0
  600. mindspore/ops/_op_impl/aicpu/fmax.py +36 -0
  601. mindspore/ops/_op_impl/aicpu/fmin.py +37 -0
  602. mindspore/ops/_op_impl/aicpu/fractional_avg_pool.py +41 -0
  603. mindspore/ops/_op_impl/aicpu/fractional_avg_pool_grad.py +41 -0
  604. mindspore/ops/_op_impl/aicpu/fractional_max_pool.py +41 -0
  605. mindspore/ops/_op_impl/aicpu/fractional_max_pool3d_grad_with_fixed_ksize.py +43 -0
  606. mindspore/ops/_op_impl/aicpu/fractional_max_pool3d_with_fixed_ksize.py +65 -0
  607. mindspore/ops/_op_impl/aicpu/fractional_max_pool_grad.py +42 -0
  608. mindspore/ops/_op_impl/aicpu/fractional_max_pool_grad_with_fixed_ksize.py +42 -0
  609. mindspore/ops/_op_impl/aicpu/fractional_max_pool_with_fixed_ksize.py +49 -0
  610. mindspore/ops/_op_impl/aicpu/fse_decode.py +43 -0
  611. mindspore/ops/_op_impl/aicpu/fused_sparse_adam.py +46 -0
  612. mindspore/ops/_op_impl/aicpu/fused_sparse_ftrl.py +41 -0
  613. mindspore/ops/_op_impl/aicpu/fused_sparse_lazy_adam.py +46 -0
  614. mindspore/ops/_op_impl/aicpu/fused_sparse_proximal_adagrad.py +39 -0
  615. mindspore/ops/_op_impl/aicpu/gamma.py +38 -0
  616. mindspore/ops/_op_impl/aicpu/gather.py +46 -0
  617. mindspore/ops/_op_impl/aicpu/gather_d.py +79 -0
  618. mindspore/ops/_op_impl/aicpu/gather_d_grad_v2.py +79 -0
  619. mindspore/ops/_op_impl/aicpu/gather_grad.py +54 -0
  620. mindspore/ops/_op_impl/aicpu/gather_nd.py +56 -0
  621. mindspore/ops/_op_impl/aicpu/gcd.py +32 -0
  622. mindspore/ops/_op_impl/aicpu/generate_eod_mask.py +38 -0
  623. mindspore/ops/_op_impl/aicpu/geqrf.py +32 -0
  624. mindspore/ops/_op_impl/aicpu/get_next.py +39 -0
  625. mindspore/ops/_op_impl/aicpu/glu.py +33 -0
  626. mindspore/ops/_op_impl/aicpu/glu_grad.py +34 -0
  627. mindspore/ops/_op_impl/aicpu/greater.py +41 -0
  628. mindspore/ops/_op_impl/aicpu/greater_equal.py +41 -0
  629. mindspore/ops/_op_impl/aicpu/grid_sampler_2d.py +35 -0
  630. mindspore/ops/_op_impl/aicpu/grid_sampler_2d_grad.py +38 -0
  631. mindspore/ops/_op_impl/aicpu/grid_sampler_3d.py +34 -0
  632. mindspore/ops/_op_impl/aicpu/grid_sampler_3d_grad.py +38 -0
  633. mindspore/ops/_op_impl/aicpu/hamming_window.py +57 -0
  634. mindspore/ops/_op_impl/aicpu/hard_sigmoid.py +32 -0
  635. mindspore/ops/_op_impl/aicpu/hard_sigmoid_grad.py +33 -0
  636. mindspore/ops/_op_impl/aicpu/heaviside.py +40 -0
  637. mindspore/ops/_op_impl/aicpu/histogram.py +35 -0
  638. mindspore/ops/_op_impl/aicpu/hsv_to_rgb.py +32 -0
  639. mindspore/ops/_op_impl/aicpu/hypot.py +32 -0
  640. mindspore/ops/_op_impl/aicpu/identity.py +42 -0
  641. mindspore/ops/_op_impl/aicpu/identity_n.py +41 -0
  642. mindspore/ops/_op_impl/aicpu/igamma.py +30 -0
  643. mindspore/ops/_op_impl/aicpu/igammac.py +30 -0
  644. mindspore/ops/_op_impl/aicpu/igammagrada.py +30 -0
  645. mindspore/ops/_op_impl/aicpu/im2col.py +43 -0
  646. mindspore/ops/_op_impl/aicpu/imag.py +31 -0
  647. mindspore/ops/_op_impl/aicpu/index_fill.py +54 -0
  648. mindspore/ops/_op_impl/aicpu/index_put.py +50 -0
  649. mindspore/ops/_op_impl/aicpu/init_data_set_queue.py +27 -0
  650. mindspore/ops/_op_impl/aicpu/inplace_index_add.py +39 -0
  651. mindspore/ops/_op_impl/aicpu/instance_norm_v2.py +41 -0
  652. mindspore/ops/_op_impl/aicpu/instance_norm_v2_grad.py +44 -0
  653. mindspore/ops/_op_impl/aicpu/is_finite.py +40 -0
  654. mindspore/ops/_op_impl/aicpu/is_inf.py +31 -0
  655. mindspore/ops/_op_impl/aicpu/is_nan.py +31 -0
  656. mindspore/ops/_op_impl/aicpu/kldivloss.py +34 -0
  657. mindspore/ops/_op_impl/aicpu/kldivlossgrad.py +35 -0
  658. mindspore/ops/_op_impl/aicpu/layer_norm_grad_grad.py +47 -0
  659. mindspore/ops/_op_impl/aicpu/lcm.py +32 -0
  660. mindspore/ops/_op_impl/aicpu/left_shift.py +38 -0
  661. mindspore/ops/_op_impl/aicpu/less.py +41 -0
  662. mindspore/ops/_op_impl/aicpu/less_equal.py +41 -0
  663. mindspore/ops/_op_impl/aicpu/lgamma.py +33 -0
  664. mindspore/ops/_op_impl/aicpu/linear_sum_assignment.py +57 -0
  665. mindspore/ops/_op_impl/aicpu/linspace.py +33 -0
  666. mindspore/ops/_op_impl/aicpu/list_diff.py +50 -0
  667. mindspore/ops/_op_impl/aicpu/log.py +37 -0
  668. mindspore/ops/_op_impl/aicpu/log1p.py +34 -0
  669. mindspore/ops/_op_impl/aicpu/log_matrix_determinant.py +31 -0
  670. mindspore/ops/_op_impl/aicpu/log_normal_reverse.py +33 -0
  671. mindspore/ops/_op_impl/aicpu/log_uniform_candidate_sampler.py +37 -0
  672. mindspore/ops/_op_impl/aicpu/logical_xor.py +30 -0
  673. mindspore/ops/_op_impl/aicpu/logit.py +33 -0
  674. mindspore/ops/_op_impl/aicpu/logit_grad.py +34 -0
  675. mindspore/ops/_op_impl/aicpu/logspace.py +36 -0
  676. mindspore/ops/_op_impl/aicpu/lower_bound.py +47 -0
  677. mindspore/ops/_op_impl/aicpu/lstsq.py +34 -0
  678. mindspore/ops/_op_impl/aicpu/lu.py +39 -0
  679. mindspore/ops/_op_impl/aicpu/lu_solve.py +32 -0
  680. mindspore/ops/_op_impl/aicpu/lu_unpack.py +114 -0
  681. mindspore/ops/_op_impl/aicpu/lu_unpack_grad.py +49 -0
  682. mindspore/ops/_op_impl/aicpu/masked_fill.py +42 -0
  683. mindspore/ops/_op_impl/aicpu/masked_scatter.py +40 -0
  684. mindspore/ops/_op_impl/aicpu/masked_select.py +31 -0
  685. mindspore/ops/_op_impl/aicpu/masked_select_grad.py +35 -0
  686. mindspore/ops/_op_impl/aicpu/matmul.py +39 -0
  687. mindspore/ops/_op_impl/aicpu/matrix_band_part.py +59 -0
  688. mindspore/ops/_op_impl/aicpu/matrix_determinant.py +30 -0
  689. mindspore/ops/_op_impl/aicpu/matrix_diag_part_v3.py +54 -0
  690. mindspore/ops/_op_impl/aicpu/matrix_diag_v3.py +56 -0
  691. mindspore/ops/_op_impl/aicpu/matrix_exp.py +34 -0
  692. mindspore/ops/_op_impl/aicpu/matrix_inverse.py +31 -0
  693. mindspore/ops/_op_impl/aicpu/matrix_logarithm.py +31 -0
  694. mindspore/ops/_op_impl/aicpu/matrix_power.py +37 -0
  695. mindspore/ops/_op_impl/aicpu/matrix_set_diag_v3.py +54 -0
  696. mindspore/ops/_op_impl/aicpu/matrix_solve.py +35 -0
  697. mindspore/ops/_op_impl/aicpu/matrix_solve_ls.py +36 -0
  698. mindspore/ops/_op_impl/aicpu/matrix_triangular_solve.py +36 -0
  699. mindspore/ops/_op_impl/aicpu/max_pool3d_grad_with_argmax.py +60 -0
  700. mindspore/ops/_op_impl/aicpu/max_pool3d_with_argmax.py +59 -0
  701. mindspore/ops/_op_impl/aicpu/max_unpool2d.py +57 -0
  702. mindspore/ops/_op_impl/aicpu/max_unpool2d_grad.py +58 -0
  703. mindspore/ops/_op_impl/aicpu/max_unpool3d.py +57 -0
  704. mindspore/ops/_op_impl/aicpu/max_unpool3d_grad.py +58 -0
  705. mindspore/ops/_op_impl/aicpu/maximum_grad_grad.py +40 -0
  706. mindspore/ops/_op_impl/aicpu/maxpool_grad_v1.py +46 -0
  707. mindspore/ops/_op_impl/aicpu/maxpool_v1.py +42 -0
  708. mindspore/ops/_op_impl/aicpu/median.py +39 -0
  709. mindspore/ops/_op_impl/aicpu/median_grad.py +45 -0
  710. mindspore/ops/_op_impl/aicpu/meshgrid.py +41 -0
  711. mindspore/ops/_op_impl/aicpu/minimum_grad_grad.py +40 -0
  712. mindspore/ops/_op_impl/aicpu/mirror_pad.py +50 -0
  713. mindspore/ops/_op_impl/aicpu/mirror_pad_grad.py +48 -0
  714. mindspore/ops/_op_impl/aicpu/mul.py +43 -0
  715. mindspore/ops/_op_impl/aicpu/mul_no_nan.py +42 -0
  716. mindspore/ops/_op_impl/aicpu/multi_margin_loss.py +37 -0
  717. mindspore/ops/_op_impl/aicpu/multi_margin_loss_grad.py +41 -0
  718. mindspore/ops/_op_impl/aicpu/multilabel_margin_loss_grad.py +37 -0
  719. mindspore/ops/_op_impl/aicpu/multinomial.py +47 -0
  720. mindspore/ops/_op_impl/aicpu/multinomial_with_replacement.py +35 -0
  721. mindspore/ops/_op_impl/aicpu/mvlgamma.py +32 -0
  722. mindspore/ops/_op_impl/aicpu/mvlgamma_grad.py +33 -0
  723. mindspore/ops/_op_impl/aicpu/nan_to_num.py +34 -0
  724. mindspore/ops/_op_impl/aicpu/neg.py +36 -0
  725. mindspore/ops/_op_impl/aicpu/nextafter.py +32 -0
  726. mindspore/ops/_op_impl/aicpu/nllloss.py +38 -0
  727. mindspore/ops/_op_impl/aicpu/nllloss_grad.py +39 -0
  728. mindspore/ops/_op_impl/aicpu/no_repeat_ngram.py +34 -0
  729. mindspore/ops/_op_impl/aicpu/non_deterministic_ints.py +33 -0
  730. mindspore/ops/_op_impl/aicpu/non_max_suppression.py +36 -0
  731. mindspore/ops/_op_impl/aicpu/non_max_suppression_with_overlaps.py +35 -0
  732. mindspore/ops/_op_impl/aicpu/non_zero.py +43 -0
  733. mindspore/ops/_op_impl/aicpu/not_equal.py +39 -0
  734. mindspore/ops/_op_impl/aicpu/nth_element.py +39 -0
  735. mindspore/ops/_op_impl/aicpu/nuclear_norm.py +33 -0
  736. mindspore/ops/_op_impl/aicpu/one_hot.py +116 -0
  737. mindspore/ops/_op_impl/aicpu/ones_like.py +39 -0
  738. mindspore/ops/_op_impl/aicpu/orgqr.py +34 -0
  739. mindspore/ops/_op_impl/aicpu/pad_and_shift.py +33 -0
  740. mindspore/ops/_op_impl/aicpu/pad_v3.py +61 -0
  741. mindspore/ops/_op_impl/aicpu/pad_v3_grad.py +59 -0
  742. mindspore/ops/_op_impl/aicpu/padding.py +41 -0
  743. mindspore/ops/_op_impl/aicpu/parameterized_truncated_normal.py +54 -0
  744. mindspore/ops/_op_impl/aicpu/pdist_grad.py +33 -0
  745. mindspore/ops/_op_impl/aicpu/poisson.py +37 -0
  746. mindspore/ops/_op_impl/aicpu/polar.py +32 -0
  747. mindspore/ops/_op_impl/aicpu/polygamma.py +34 -0
  748. mindspore/ops/_op_impl/aicpu/pow.py +39 -0
  749. mindspore/ops/_op_impl/aicpu/print_tensor.py +39 -0
  750. mindspore/ops/_op_impl/aicpu/priority_replay_buffer.py +113 -0
  751. mindspore/ops/_op_impl/aicpu/qr.py +36 -0
  752. mindspore/ops/_op_impl/aicpu/quant_dtype_cast.py +40 -0
  753. mindspore/ops/_op_impl/aicpu/quantile.py +35 -0
  754. mindspore/ops/_op_impl/aicpu/ragged_range.py +49 -0
  755. mindspore/ops/_op_impl/aicpu/ragged_tensor_to_sparse.py +73 -0
  756. mindspore/ops/_op_impl/aicpu/ragged_tensor_to_tensor.py +74 -0
  757. mindspore/ops/_op_impl/aicpu/random_categorical.py +68 -0
  758. mindspore/ops/_op_impl/aicpu/random_choice_with_mask.py +36 -0
  759. mindspore/ops/_op_impl/aicpu/random_gamma.py +38 -0
  760. mindspore/ops/_op_impl/aicpu/random_poisson.py +134 -0
  761. mindspore/ops/_op_impl/aicpu/random_shuffle.py +47 -0
  762. mindspore/ops/_op_impl/aicpu/randperm.py +38 -0
  763. mindspore/ops/_op_impl/aicpu/randperm_v2.py +41 -0
  764. mindspore/ops/_op_impl/aicpu/range.py +36 -0
  765. mindspore/ops/_op_impl/aicpu/range_v2.py +35 -0
  766. mindspore/ops/_op_impl/aicpu/real.py +31 -0
  767. mindspore/ops/_op_impl/aicpu/real_div.py +40 -0
  768. mindspore/ops/_op_impl/aicpu/reciprocal.py +34 -0
  769. mindspore/ops/_op_impl/aicpu/reciprocal_grad.py +35 -0
  770. mindspore/ops/_op_impl/aicpu/reduce_mean.py +57 -0
  771. mindspore/ops/_op_impl/aicpu/reduce_prod.py +57 -0
  772. mindspore/ops/_op_impl/aicpu/reduce_sum.py +57 -0
  773. mindspore/ops/_op_impl/aicpu/relu_grad_v3.py +41 -0
  774. mindspore/ops/_op_impl/aicpu/relu_v3.py +38 -0
  775. mindspore/ops/_op_impl/aicpu/reservoir_replay_buffer.py +96 -0
  776. mindspore/ops/_op_impl/aicpu/reshape.py +42 -0
  777. mindspore/ops/_op_impl/aicpu/resize_area.py +40 -0
  778. mindspore/ops/_op_impl/aicpu/resize_bicubic.py +20 -0
  779. mindspore/ops/_op_impl/aicpu/resize_bicubic_grad.py +19 -0
  780. mindspore/ops/_op_impl/aicpu/resize_bilinear.py +32 -0
  781. mindspore/ops/_op_impl/aicpu/resize_bilinear_grad.py +32 -0
  782. mindspore/ops/_op_impl/aicpu/resize_nearest_neighbor_v2.py +36 -0
  783. mindspore/ops/_op_impl/aicpu/resize_nearest_neighbor_v2_grad.py +35 -0
  784. mindspore/ops/_op_impl/aicpu/resize_v2.py +68 -0
  785. mindspore/ops/_op_impl/aicpu/resize_v2_grad.py +68 -0
  786. mindspore/ops/_op_impl/aicpu/reverse_sequence.py +55 -0
  787. mindspore/ops/_op_impl/aicpu/reversev2.py +54 -0
  788. mindspore/ops/_op_impl/aicpu/rgb_to_hsv.py +32 -0
  789. mindspore/ops/_op_impl/aicpu/right_shift.py +38 -0
  790. mindspore/ops/_op_impl/aicpu/rnnt_loss.py +35 -0
  791. mindspore/ops/_op_impl/aicpu/round.py +34 -0
  792. mindspore/ops/_op_impl/aicpu/rsqrt.py +33 -0
  793. mindspore/ops/_op_impl/aicpu/rsqrt_grad.py +36 -0
  794. mindspore/ops/_op_impl/aicpu/sample_distorted_bounding_box_v2.py +49 -0
  795. mindspore/ops/_op_impl/aicpu/scale_and_translate.py +52 -0
  796. mindspore/ops/_op_impl/aicpu/scale_and_translate_grad.py +36 -0
  797. mindspore/ops/_op_impl/aicpu/scatter.py +79 -0
  798. mindspore/ops/_op_impl/aicpu/scatter_add_with_axis.py +53 -0
  799. mindspore/ops/_op_impl/aicpu/scatter_elements.py +39 -0
  800. mindspore/ops/_op_impl/aicpu/scatter_nd.py +59 -0
  801. mindspore/ops/_op_impl/aicpu/scatter_nd_max.py +54 -0
  802. mindspore/ops/_op_impl/aicpu/scatter_nd_min.py +54 -0
  803. mindspore/ops/_op_impl/aicpu/scatter_nd_update.py +59 -0
  804. mindspore/ops/_op_impl/aicpu/search_sorted.py +44 -0
  805. mindspore/ops/_op_impl/aicpu/segment_max.py +52 -0
  806. mindspore/ops/_op_impl/aicpu/segment_mean.py +56 -0
  807. mindspore/ops/_op_impl/aicpu/segment_min.py +52 -0
  808. mindspore/ops/_op_impl/aicpu/segment_prod.py +56 -0
  809. mindspore/ops/_op_impl/aicpu/segment_sum.py +56 -0
  810. mindspore/ops/_op_impl/aicpu/select.py +45 -0
  811. mindspore/ops/_op_impl/aicpu/self_adjoint_eig.py +34 -0
  812. mindspore/ops/_op_impl/aicpu/sequence_add.py +34 -0
  813. mindspore/ops/_op_impl/aicpu/sequence_add_offset.py +34 -0
  814. mindspore/ops/_op_impl/aicpu/sequence_addn.py +38 -0
  815. mindspore/ops/_op_impl/aicpu/sequence_concat.py +40 -0
  816. mindspore/ops/_op_impl/aicpu/sequence_stack.py +40 -0
  817. mindspore/ops/_op_impl/aicpu/set_size.py +38 -0
  818. mindspore/ops/_op_impl/aicpu/sign.py +36 -0
  819. mindspore/ops/_op_impl/aicpu/sin.py +34 -0
  820. mindspore/ops/_op_impl/aicpu/sinc.py +43 -0
  821. mindspore/ops/_op_impl/aicpu/sinh.py +34 -0
  822. mindspore/ops/_op_impl/aicpu/slice.py +59 -0
  823. mindspore/ops/_op_impl/aicpu/slice_grad.py +76 -0
  824. mindspore/ops/_op_impl/aicpu/smooth_l1_loss.py +35 -0
  825. mindspore/ops/_op_impl/aicpu/smooth_l1_loss_grad.py +37 -0
  826. mindspore/ops/_op_impl/aicpu/sort.py +39 -0
  827. mindspore/ops/_op_impl/aicpu/space_to_depth.py +44 -0
  828. mindspore/ops/_op_impl/aicpu/sparse_addmm.py +87 -0
  829. mindspore/ops/_op_impl/aicpu/sparse_apply_adagrad_da.py +80 -0
  830. mindspore/ops/_op_impl/aicpu/sparse_apply_centered_rms_prop.py +105 -0
  831. mindspore/ops/_op_impl/aicpu/sparse_apply_momentum.py +80 -0
  832. mindspore/ops/_op_impl/aicpu/sparse_apply_proximal_gradient_descent.py +79 -0
  833. mindspore/ops/_op_impl/aicpu/sparse_concat.py +59 -0
  834. mindspore/ops/_op_impl/aicpu/sparse_cross.py +42 -0
  835. mindspore/ops/_op_impl/aicpu/sparse_dense_cwise_add.py +58 -0
  836. mindspore/ops/_op_impl/aicpu/sparse_dense_cwise_div.py +58 -0
  837. mindspore/ops/_op_impl/aicpu/sparse_dense_cwise_mul.py +58 -0
  838. mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows.py +63 -0
  839. mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows_grad.py +45 -0
  840. mindspore/ops/_op_impl/aicpu/sparse_matrix_mat_mul.py +56 -0
  841. mindspore/ops/_op_impl/aicpu/sparse_matrix_nnz.py +81 -0
  842. mindspore/ops/_op_impl/aicpu/sparse_matrix_transpose.py +116 -0
  843. mindspore/ops/_op_impl/aicpu/sparse_reorder.py +56 -0
  844. mindspore/ops/_op_impl/aicpu/sparse_reshape.py +34 -0
  845. mindspore/ops/_op_impl/aicpu/sparse_segment_mean_grad.py +36 -0
  846. mindspore/ops/_op_impl/aicpu/sparse_segment_mean_with_num_segments.py +44 -0
  847. mindspore/ops/_op_impl/aicpu/sparse_segment_sqrt_n.py +43 -0
  848. mindspore/ops/_op_impl/aicpu/sparse_segment_sqrt_n_grad.py +38 -0
  849. mindspore/ops/_op_impl/aicpu/sparse_segment_sqrt_n_with_num_segments.py +44 -0
  850. mindspore/ops/_op_impl/aicpu/sparse_segment_sum.py +49 -0
  851. mindspore/ops/_op_impl/aicpu/sparse_segment_sum_with_num_segments.py +68 -0
  852. mindspore/ops/_op_impl/aicpu/sparse_slice.py +63 -0
  853. mindspore/ops/_op_impl/aicpu/sparse_slice_grad.py +61 -0
  854. mindspore/ops/_op_impl/aicpu/sparse_softmax.py +33 -0
  855. mindspore/ops/_op_impl/aicpu/sparse_softmax_cross_entropy_with_logits_v2.py +35 -0
  856. mindspore/ops/_op_impl/aicpu/sparse_sparse_maximum.py +53 -0
  857. mindspore/ops/_op_impl/aicpu/sparse_sparse_minimum.py +53 -0
  858. mindspore/ops/_op_impl/aicpu/sparse_tensor_dense_add.py +84 -0
  859. mindspore/ops/_op_impl/aicpu/sparse_tensor_dense_mat_mul.py +190 -0
  860. mindspore/ops/_op_impl/aicpu/sparse_tensor_to_csr_sparse_matrix.py +51 -0
  861. mindspore/ops/_op_impl/aicpu/sparse_to_dense_v2.py +73 -0
  862. mindspore/ops/_op_impl/aicpu/split.py +45 -0
  863. mindspore/ops/_op_impl/aicpu/sqrt.py +34 -0
  864. mindspore/ops/_op_impl/aicpu/sqrt_grad.py +35 -0
  865. mindspore/ops/_op_impl/aicpu/square.py +35 -0
  866. mindspore/ops/_op_impl/aicpu/squared_difference.py +37 -0
  867. mindspore/ops/_op_impl/aicpu/squeeze.py +42 -0
  868. mindspore/ops/_op_impl/aicpu/sspaddmm.py +97 -0
  869. mindspore/ops/_op_impl/aicpu/stack.py +45 -0
  870. mindspore/ops/_op_impl/aicpu/stack_push_pop.py +87 -0
  871. mindspore/ops/_op_impl/aicpu/standard_laplace.py +34 -0
  872. mindspore/ops/_op_impl/aicpu/standard_normal.py +34 -0
  873. mindspore/ops/_op_impl/aicpu/stateless_dropout_genmask.py +37 -0
  874. mindspore/ops/_op_impl/aicpu/stft.py +70 -0
  875. mindspore/ops/_op_impl/aicpu/strided_slice.py +43 -0
  876. mindspore/ops/_op_impl/aicpu/strided_slice_grad.py +50 -0
  877. mindspore/ops/_op_impl/aicpu/strided_slice_v2.py +93 -0
  878. mindspore/ops/_op_impl/aicpu/strided_slice_v2_grad.py +66 -0
  879. mindspore/ops/_op_impl/aicpu/sub.py +41 -0
  880. mindspore/ops/_op_impl/aicpu/sub_and_filter.py +36 -0
  881. mindspore/ops/_op_impl/aicpu/tan.py +34 -0
  882. mindspore/ops/_op_impl/aicpu/tanh.py +34 -0
  883. mindspore/ops/_op_impl/aicpu/tanh_grad.py +35 -0
  884. mindspore/ops/_op_impl/aicpu/tensor_scatter_update.py +59 -0
  885. mindspore/ops/_op_impl/aicpu/tile.py +56 -0
  886. mindspore/ops/_op_impl/aicpu/topk.py +34 -0
  887. mindspore/ops/_op_impl/aicpu/trace.py +40 -0
  888. mindspore/ops/_op_impl/aicpu/tracegrad.py +41 -0
  889. mindspore/ops/_op_impl/aicpu/trans_data.py +35 -0
  890. mindspore/ops/_op_impl/aicpu/transpose.py +58 -0
  891. mindspore/ops/_op_impl/aicpu/tridiagonal_matmul.py +42 -0
  892. mindspore/ops/_op_impl/aicpu/tridiagonal_solve.py +35 -0
  893. mindspore/ops/_op_impl/aicpu/tril.py +42 -0
  894. mindspore/ops/_op_impl/aicpu/tril_indices.py +34 -0
  895. mindspore/ops/_op_impl/aicpu/triplet_margin_loss.py +62 -0
  896. mindspore/ops/_op_impl/aicpu/triu.py +43 -0
  897. mindspore/ops/_op_impl/aicpu/triu_indices.py +34 -0
  898. mindspore/ops/_op_impl/aicpu/truncated_normal.py +39 -0
  899. mindspore/ops/_op_impl/aicpu/uniform.py +36 -0
  900. mindspore/ops/_op_impl/aicpu/uniform_candidate_sampler.py +41 -0
  901. mindspore/ops/_op_impl/aicpu/uniform_int.py +36 -0
  902. mindspore/ops/_op_impl/aicpu/uniform_real.py +33 -0
  903. mindspore/ops/_op_impl/aicpu/unique.py +31 -0
  904. mindspore/ops/_op_impl/aicpu/unique_consecutive.py +47 -0
  905. mindspore/ops/_op_impl/aicpu/unique_with_pad.py +32 -0
  906. mindspore/ops/_op_impl/aicpu/unravel_index.py +32 -0
  907. mindspore/ops/_op_impl/aicpu/unsorted_segment_prod.py +53 -0
  908. mindspore/ops/_op_impl/aicpu/unsorted_segment_sum.py +57 -0
  909. mindspore/ops/_op_impl/aicpu/unstack.py +45 -0
  910. mindspore/ops/_op_impl/aicpu/update_cache.py +44 -0
  911. mindspore/ops/_op_impl/aicpu/upper_bound.py +47 -0
  912. mindspore/ops/_op_impl/aicpu/upsample_nearest_3d.py +42 -0
  913. mindspore/ops/_op_impl/aicpu/upsample_nearest_3d_grad.py +49 -0
  914. mindspore/ops/_op_impl/aicpu/upsample_trilinear_3d.py +40 -0
  915. mindspore/ops/_op_impl/aicpu/upsample_trilinear_3d_grad.py +50 -0
  916. mindspore/ops/_op_impl/aicpu/xdivy.py +35 -0
  917. mindspore/ops/_op_impl/aicpu/xlogy.py +33 -0
  918. mindspore/ops/_op_impl/aicpu/zeros_like.py +42 -0
  919. mindspore/ops/_op_impl/aicpu/zeta.py +31 -0
  920. mindspore/ops/_op_impl/akg/__init__.py +19 -0
  921. mindspore/ops/_op_impl/akg/ascend/__init__.py +48 -0
  922. mindspore/ops/_op_impl/akg/ascend/abs.py +35 -0
  923. mindspore/ops/_op_impl/akg/ascend/add.py +42 -0
  924. mindspore/ops/_op_impl/akg/ascend/add_n.py +37 -0
  925. mindspore/ops/_op_impl/akg/ascend/batchmatmul.py +33 -0
  926. mindspore/ops/_op_impl/akg/ascend/cast.py +46 -0
  927. mindspore/ops/_op_impl/akg/ascend/equal.py +35 -0
  928. mindspore/ops/_op_impl/akg/ascend/exp.py +35 -0
  929. mindspore/ops/_op_impl/akg/ascend/expand_dims.py +33 -0
  930. mindspore/ops/_op_impl/akg/ascend/greater.py +34 -0
  931. mindspore/ops/_op_impl/akg/ascend/greater_equal.py +35 -0
  932. mindspore/ops/_op_impl/akg/ascend/less.py +31 -0
  933. mindspore/ops/_op_impl/akg/ascend/less_equal.py +35 -0
  934. mindspore/ops/_op_impl/akg/ascend/load_im2col.py +33 -0
  935. mindspore/ops/_op_impl/akg/ascend/log.py +34 -0
  936. mindspore/ops/_op_impl/akg/ascend/maximum.py +36 -0
  937. mindspore/ops/_op_impl/akg/ascend/minimum.py +39 -0
  938. mindspore/ops/_op_impl/akg/ascend/mul.py +41 -0
  939. mindspore/ops/_op_impl/akg/ascend/neg.py +37 -0
  940. mindspore/ops/_op_impl/akg/ascend/pow.py +35 -0
  941. mindspore/ops/_op_impl/akg/ascend/prod_force_se_a.py +33 -0
  942. mindspore/ops/_op_impl/akg/ascend/real_div.py +36 -0
  943. mindspore/ops/_op_impl/akg/ascend/reciprocal.py +32 -0
  944. mindspore/ops/_op_impl/akg/ascend/reduce_max.py +32 -0
  945. mindspore/ops/_op_impl/akg/ascend/reduce_min.py +32 -0
  946. mindspore/ops/_op_impl/akg/ascend/reduce_sum.py +37 -0
  947. mindspore/ops/_op_impl/akg/ascend/rsqrt.py +35 -0
  948. mindspore/ops/_op_impl/akg/ascend/select.py +37 -0
  949. mindspore/ops/_op_impl/akg/ascend/sqrt.py +35 -0
  950. mindspore/ops/_op_impl/akg/ascend/square.py +35 -0
  951. mindspore/ops/_op_impl/akg/ascend/sub.py +42 -0
  952. mindspore/ops/_op_impl/akg/cpu/__init__.py +23 -0
  953. mindspore/ops/_op_impl/akg/cpu/coo2csr.py +29 -0
  954. mindspore/ops/_op_impl/akg/cpu/csr2coo.py +29 -0
  955. mindspore/ops/_op_impl/akg/cpu/csr_gather.py +33 -0
  956. mindspore/ops/_op_impl/akg/cpu/csr_mm.py +34 -0
  957. mindspore/ops/_op_impl/akg/cpu/csr_mul.py +33 -0
  958. mindspore/ops/_op_impl/akg/cpu/csr_mv.py +33 -0
  959. mindspore/ops/_op_impl/akg/cpu/csr_reduce_sum.py +31 -0
  960. mindspore/ops/_op_impl/akg/gpu/__init__.py +24 -0
  961. mindspore/ops/_op_impl/akg/gpu/coo2csr.py +29 -0
  962. mindspore/ops/_op_impl/akg/gpu/csr2coo.py +29 -0
  963. mindspore/ops/_op_impl/akg/gpu/csr_div.py +36 -0
  964. mindspore/ops/_op_impl/akg/gpu/csr_gather.py +33 -0
  965. mindspore/ops/_op_impl/akg/gpu/csr_mm.py +37 -0
  966. mindspore/ops/_op_impl/akg/gpu/csr_mul.py +36 -0
  967. mindspore/ops/_op_impl/akg/gpu/csr_mv.py +36 -0
  968. mindspore/ops/_op_impl/akg/gpu/csr_reduce_sum.py +33 -0
  969. mindspore/ops/_op_impl/cpu/__init__.py +78 -0
  970. mindspore/ops/_op_impl/cpu/adam.py +49 -0
  971. mindspore/ops/_op_impl/cpu/adam_weight_decay.py +47 -0
  972. mindspore/ops/_op_impl/cpu/arg_max.py +30 -0
  973. mindspore/ops/_op_impl/cpu/arg_max_with_value.py +31 -0
  974. mindspore/ops/_op_impl/cpu/arg_min_with_value.py +31 -0
  975. mindspore/ops/_op_impl/cpu/buffer_append.py +28 -0
  976. mindspore/ops/_op_impl/cpu/buffer_get.py +28 -0
  977. mindspore/ops/_op_impl/cpu/buffer_sample.py +28 -0
  978. mindspore/ops/_op_impl/cpu/cast.py +171 -0
  979. mindspore/ops/_op_impl/cpu/concat_offset.py +38 -0
  980. mindspore/ops/_op_impl/cpu/conv2d.py +30 -0
  981. mindspore/ops/_op_impl/cpu/conv3d.py +30 -0
  982. mindspore/ops/_op_impl/cpu/div.py +32 -0
  983. mindspore/ops/_op_impl/cpu/dropout.py +31 -0
  984. mindspore/ops/_op_impl/cpu/dropout_grad.py +30 -0
  985. mindspore/ops/_op_impl/cpu/dynamic_shape.py +42 -0
  986. mindspore/ops/_op_impl/cpu/dynamic_stitch.py +41 -0
  987. mindspore/ops/_op_impl/cpu/equal_count.py +30 -0
  988. mindspore/ops/_op_impl/cpu/gather_d.py +49 -0
  989. mindspore/ops/_op_impl/cpu/gather_d_grad.py +38 -0
  990. mindspore/ops/_op_impl/cpu/gather_d_grad_v2.py +40 -0
  991. mindspore/ops/_op_impl/cpu/gather_v2.py +40 -0
  992. mindspore/ops/_op_impl/cpu/hsigmoid.py +33 -0
  993. mindspore/ops/_op_impl/cpu/hsigmoid_grad.py +34 -0
  994. mindspore/ops/_op_impl/cpu/hswish.py +32 -0
  995. mindspore/ops/_op_impl/cpu/hswish_grad.py +33 -0
  996. mindspore/ops/_op_impl/cpu/identity_n.py +40 -0
  997. mindspore/ops/_op_impl/cpu/is_finite.py +39 -0
  998. mindspore/ops/_op_impl/cpu/l2loss.py +30 -0
  999. mindspore/ops/_op_impl/cpu/layer_norm.py +36 -0
  1000. mindspore/ops/_op_impl/cpu/layer_norm_grad.py +38 -0
  1001. mindspore/ops/_op_impl/cpu/maximum.py +35 -0
  1002. mindspore/ops/_op_impl/cpu/maximum_grad.py +47 -0
  1003. mindspore/ops/_op_impl/cpu/minimum.py +40 -0
  1004. mindspore/ops/_op_impl/cpu/minimum_grad.py +51 -0
  1005. mindspore/ops/_op_impl/cpu/mirror_pad.py +36 -0
  1006. mindspore/ops/_op_impl/cpu/mirror_pad_grad.py +36 -0
  1007. mindspore/ops/_op_impl/cpu/mul.py +32 -0
  1008. mindspore/ops/_op_impl/cpu/one_hot.py +31 -0
  1009. mindspore/ops/_op_impl/cpu/pad.py +32 -0
  1010. mindspore/ops/_op_impl/cpu/pow.py +32 -0
  1011. mindspore/ops/_op_impl/cpu/priority_replay_buffer.py +42 -0
  1012. mindspore/ops/_op_impl/cpu/pyexecute.py +29 -0
  1013. mindspore/ops/_op_impl/cpu/pyfunc.py +29 -0
  1014. mindspore/ops/_op_impl/cpu/range.py +34 -0
  1015. mindspore/ops/_op_impl/cpu/real_div.py +33 -0
  1016. mindspore/ops/_op_impl/cpu/reduce_all.py +29 -0
  1017. mindspore/ops/_op_impl/cpu/reduce_any.py +29 -0
  1018. mindspore/ops/_op_impl/cpu/reduce_max.py +32 -0
  1019. mindspore/ops/_op_impl/cpu/reduce_mean.py +40 -0
  1020. mindspore/ops/_op_impl/cpu/reduce_min.py +32 -0
  1021. mindspore/ops/_op_impl/cpu/reduce_prod.py +40 -0
  1022. mindspore/ops/_op_impl/cpu/reduce_std.py +31 -0
  1023. mindspore/ops/_op_impl/cpu/reduce_sum.py +41 -0
  1024. mindspore/ops/_op_impl/cpu/space_to_batch_nd.py +38 -0
  1025. mindspore/ops/_op_impl/cpu/sparse_slice.py +62 -0
  1026. mindspore/ops/_op_impl/cpu/sparse_slice_grad.py +60 -0
  1027. mindspore/ops/_op_impl/cpu/split.py +34 -0
  1028. mindspore/ops/_op_impl/cpu/sspaddmm.py +95 -0
  1029. mindspore/ops/_op_impl/cpu/stack.py +38 -0
  1030. mindspore/ops/_op_impl/cpu/sub.py +32 -0
  1031. mindspore/ops/_op_impl/cpu/tensor_copy_slices.py +41 -0
  1032. mindspore/ops/_op_impl/cpu/tile.py +37 -0
  1033. mindspore/ops/_op_impl/cpu/top_k.py +31 -0
  1034. mindspore/ops/_op_impl/cpu/transpose.py +39 -0
  1035. mindspore/ops/_primitive_cache.py +90 -0
  1036. mindspore/ops/_register_for_op.py +73 -0
  1037. mindspore/ops/_utils/__init__.py +20 -0
  1038. mindspore/ops/_utils/utils.py +147 -0
  1039. mindspore/ops/_vmap/__init__.py +25 -0
  1040. mindspore/ops/_vmap/vmap_array_ops.py +2151 -0
  1041. mindspore/ops/_vmap/vmap_base.py +533 -0
  1042. mindspore/ops/_vmap/vmap_convolution_ops.py +441 -0
  1043. mindspore/ops/_vmap/vmap_debug_ops.py +50 -0
  1044. mindspore/ops/_vmap/vmap_grad_math_ops.py +274 -0
  1045. mindspore/ops/_vmap/vmap_grad_nn_ops.py +806 -0
  1046. mindspore/ops/_vmap/vmap_image_ops.py +194 -0
  1047. mindspore/ops/_vmap/vmap_math_ops.py +977 -0
  1048. mindspore/ops/_vmap/vmap_nn_ops.py +2209 -0
  1049. mindspore/ops/_vmap/vmap_other_ops.py +105 -0
  1050. mindspore/ops/_vmap/vmap_random_ops.py +122 -0
  1051. mindspore/ops/_vmap/vmap_sparse_ops.py +89 -0
  1052. mindspore/ops/auto_generate/__init__.py +31 -0
  1053. mindspore/ops/auto_generate/cpp_create_prim_instance_helper.py +231 -0
  1054. mindspore/ops/auto_generate/gen_arg_dtype_cast.py +250 -0
  1055. mindspore/ops/auto_generate/gen_arg_handler.py +197 -0
  1056. mindspore/ops/auto_generate/gen_extend_func.py +980 -0
  1057. mindspore/ops/auto_generate/gen_ops_def.py +6443 -0
  1058. mindspore/ops/auto_generate/gen_ops_prim.py +13167 -0
  1059. mindspore/ops/auto_generate/pyboost_inner_prim.py +429 -0
  1060. mindspore/ops/composite/__init__.py +71 -0
  1061. mindspore/ops/composite/base.py +1281 -0
  1062. mindspore/ops/composite/env_ops.py +41 -0
  1063. mindspore/ops/composite/math_ops.py +125 -0
  1064. mindspore/ops/composite/multitype_ops/__init__.py +77 -0
  1065. mindspore/ops/composite/multitype_ops/_compile_utils.py +1458 -0
  1066. mindspore/ops/composite/multitype_ops/_constexpr_utils.py +897 -0
  1067. mindspore/ops/composite/multitype_ops/add_impl.py +606 -0
  1068. mindspore/ops/composite/multitype_ops/bitwise_and_impl.py +56 -0
  1069. mindspore/ops/composite/multitype_ops/bitwise_or_impl.py +56 -0
  1070. mindspore/ops/composite/multitype_ops/bitwise_xor_impl.py +56 -0
  1071. mindspore/ops/composite/multitype_ops/div_impl.py +189 -0
  1072. mindspore/ops/composite/multitype_ops/equal_impl.py +335 -0
  1073. mindspore/ops/composite/multitype_ops/floordiv_impl.py +88 -0
  1074. mindspore/ops/composite/multitype_ops/getitem_impl.py +400 -0
  1075. mindspore/ops/composite/multitype_ops/greater_equal_impl.py +109 -0
  1076. mindspore/ops/composite/multitype_ops/greater_impl.py +110 -0
  1077. mindspore/ops/composite/multitype_ops/in_impl.py +196 -0
  1078. mindspore/ops/composite/multitype_ops/left_shift_impl.py +37 -0
  1079. mindspore/ops/composite/multitype_ops/less_equal_impl.py +111 -0
  1080. mindspore/ops/composite/multitype_ops/less_impl.py +112 -0
  1081. mindspore/ops/composite/multitype_ops/logic_not_impl.py +113 -0
  1082. mindspore/ops/composite/multitype_ops/logical_and_impl.py +60 -0
  1083. mindspore/ops/composite/multitype_ops/logical_or_impl.py +61 -0
  1084. mindspore/ops/composite/multitype_ops/mod_impl.py +86 -0
  1085. mindspore/ops/composite/multitype_ops/mul_impl.py +294 -0
  1086. mindspore/ops/composite/multitype_ops/negative_impl.py +79 -0
  1087. mindspore/ops/composite/multitype_ops/not_equal_impl.py +290 -0
  1088. mindspore/ops/composite/multitype_ops/not_in_impl.py +196 -0
  1089. mindspore/ops/composite/multitype_ops/ones_like_impl.py +96 -0
  1090. mindspore/ops/composite/multitype_ops/pow_impl.py +87 -0
  1091. mindspore/ops/composite/multitype_ops/right_shift_impl.py +37 -0
  1092. mindspore/ops/composite/multitype_ops/setitem_impl.py +884 -0
  1093. mindspore/ops/composite/multitype_ops/sub_impl.py +116 -0
  1094. mindspore/ops/composite/multitype_ops/uadd_impl.py +29 -0
  1095. mindspore/ops/composite/multitype_ops/zeros_like_impl.py +228 -0
  1096. mindspore/ops/deprecated.py +315 -0
  1097. mindspore/ops/extend/__init__.py +53 -0
  1098. mindspore/ops/extend/array_func.py +218 -0
  1099. mindspore/ops/extend/math_func.py +76 -0
  1100. mindspore/ops/extend/nn_func.py +308 -0
  1101. mindspore/ops/function/__init__.py +760 -0
  1102. mindspore/ops/function/array_func.py +6889 -0
  1103. mindspore/ops/function/clip_func.py +384 -0
  1104. mindspore/ops/function/debug_func.py +69 -0
  1105. mindspore/ops/function/fft_func.py +31 -0
  1106. mindspore/ops/function/grad/__init__.py +34 -0
  1107. mindspore/ops/function/grad/grad_func.py +1424 -0
  1108. mindspore/ops/function/image_func.py +292 -0
  1109. mindspore/ops/function/linalg_func.py +416 -0
  1110. mindspore/ops/function/math_func.py +11877 -0
  1111. mindspore/ops/function/nn_func.py +8175 -0
  1112. mindspore/ops/function/other_func.py +114 -0
  1113. mindspore/ops/function/parameter_func.py +134 -0
  1114. mindspore/ops/function/random_func.py +1539 -0
  1115. mindspore/ops/function/reshard_func.py +102 -0
  1116. mindspore/ops/function/sparse_func.py +884 -0
  1117. mindspore/ops/function/sparse_unary_func.py +2422 -0
  1118. mindspore/ops/function/spectral_func.py +150 -0
  1119. mindspore/ops/function/vmap_func.py +116 -0
  1120. mindspore/ops/functional.py +454 -0
  1121. mindspore/ops/op_info_register.py +1572 -0
  1122. mindspore/ops/operations/__init__.py +717 -0
  1123. mindspore/ops/operations/_csr_ops.py +403 -0
  1124. mindspore/ops/operations/_custom_grad.py +181 -0
  1125. mindspore/ops/operations/_embedding_cache_ops.py +307 -0
  1126. mindspore/ops/operations/_grad_ops.py +3052 -0
  1127. mindspore/ops/operations/_infer_ops.py +19 -0
  1128. mindspore/ops/operations/_inner_ops.py +2567 -0
  1129. mindspore/ops/operations/_map_tensor_ops.py +112 -0
  1130. mindspore/ops/operations/_ms_kernel.py +601 -0
  1131. mindspore/ops/operations/_ocr_ops.py +379 -0
  1132. mindspore/ops/operations/_opaque_predicate_registry.py +41 -0
  1133. mindspore/ops/operations/_pyfunc_registry.py +58 -0
  1134. mindspore/ops/operations/_quant_ops.py +1844 -0
  1135. mindspore/ops/operations/_rl_inner_ops.py +1231 -0
  1136. mindspore/ops/operations/_scalar_ops.py +106 -0
  1137. mindspore/ops/operations/_sequence_ops.py +1155 -0
  1138. mindspore/ops/operations/_sparse_grad_ops.py +56 -0
  1139. mindspore/ops/operations/_tensor_array.py +359 -0
  1140. mindspore/ops/operations/_thor_ops.py +807 -0
  1141. mindspore/ops/operations/array_ops.py +6258 -0
  1142. mindspore/ops/operations/comm_ops.py +1996 -0
  1143. mindspore/ops/operations/control_ops.py +127 -0
  1144. mindspore/ops/operations/custom_ops.py +1065 -0
  1145. mindspore/ops/operations/debug_ops.py +646 -0
  1146. mindspore/ops/operations/image_ops.py +1041 -0
  1147. mindspore/ops/operations/inner_ops.py +697 -0
  1148. mindspore/ops/operations/linalg_ops.py +95 -0
  1149. mindspore/ops/operations/manually_defined/__init__.py +24 -0
  1150. mindspore/ops/operations/manually_defined/_inner.py +61 -0
  1151. mindspore/ops/operations/manually_defined/ops_def.py +2016 -0
  1152. mindspore/ops/operations/math_ops.py +5306 -0
  1153. mindspore/ops/operations/nn_ops.py +9669 -0
  1154. mindspore/ops/operations/other_ops.py +871 -0
  1155. mindspore/ops/operations/random_ops.py +1243 -0
  1156. mindspore/ops/operations/reshard_ops.py +53 -0
  1157. mindspore/ops/operations/rl_ops.py +288 -0
  1158. mindspore/ops/operations/sparse_ops.py +2753 -0
  1159. mindspore/ops/operations/spectral_ops.py +111 -0
  1160. mindspore/ops/primitive.py +1034 -0
  1161. mindspore/ops/signature.py +54 -0
  1162. mindspore/ops/silent_check.py +162 -0
  1163. mindspore/ops/vm_impl_registry.py +91 -0
  1164. mindspore/ops_generate/__init__.py +27 -0
  1165. mindspore/ops_generate/arg_dtype_cast.py +250 -0
  1166. mindspore/ops_generate/arg_handler.py +197 -0
  1167. mindspore/ops_generate/gen_aclnn_implement.py +263 -0
  1168. mindspore/ops_generate/gen_ops.py +1084 -0
  1169. mindspore/ops_generate/gen_ops_inner_prim.py +131 -0
  1170. mindspore/ops_generate/gen_pyboost_func.py +968 -0
  1171. mindspore/ops_generate/gen_utils.py +209 -0
  1172. mindspore/ops_generate/op_proto.py +138 -0
  1173. mindspore/ops_generate/pyboost_utils.py +354 -0
  1174. mindspore/ops_generate/template.py +239 -0
  1175. mindspore/parallel/__init__.py +28 -0
  1176. mindspore/parallel/_auto_parallel_context.py +1466 -0
  1177. mindspore/parallel/_cell_wrapper.py +91 -0
  1178. mindspore/parallel/_cost_model_context.py +700 -0
  1179. mindspore/parallel/_dp_allreduce_fusion.py +159 -0
  1180. mindspore/parallel/_offload_context.py +275 -0
  1181. mindspore/parallel/_parallel_serialization.py +533 -0
  1182. mindspore/parallel/_ps_context.py +242 -0
  1183. mindspore/parallel/_recovery_context.py +110 -0
  1184. mindspore/parallel/_tensor.py +660 -0
  1185. mindspore/parallel/_transformer/__init__.py +35 -0
  1186. mindspore/parallel/_transformer/layers.py +765 -0
  1187. mindspore/parallel/_transformer/loss.py +251 -0
  1188. mindspore/parallel/_transformer/moe.py +693 -0
  1189. mindspore/parallel/_transformer/op_parallel_config.py +222 -0
  1190. mindspore/parallel/_transformer/transformer.py +3119 -0
  1191. mindspore/parallel/_utils.py +600 -0
  1192. mindspore/parallel/algo_parameter_config.py +400 -0
  1193. mindspore/parallel/checkpoint_transform.py +643 -0
  1194. mindspore/parallel/cluster/__init__.py +15 -0
  1195. mindspore/parallel/cluster/process_entity/__init__.py +18 -0
  1196. mindspore/parallel/cluster/process_entity/_api.py +344 -0
  1197. mindspore/parallel/cluster/process_entity/_utils.py +126 -0
  1198. mindspore/parallel/cluster/run.py +136 -0
  1199. mindspore/parallel/mpi/__init__.py +14 -0
  1200. mindspore/parallel/mpi/_mpi_config.py +116 -0
  1201. mindspore/parallel/parameter_broadcast.py +152 -0
  1202. mindspore/parallel/shard.py +350 -0
  1203. mindspore/perf_msvcbuildinsights.dll +0 -0
  1204. mindspore/pgodb140.dll +0 -0
  1205. mindspore/pgort140.dll +0 -0
  1206. mindspore/profiler/__init__.py +27 -0
  1207. mindspore/profiler/common/__init__.py +14 -0
  1208. mindspore/profiler/common/exceptions/__init__.py +14 -0
  1209. mindspore/profiler/common/exceptions/error_code.py +83 -0
  1210. mindspore/profiler/common/exceptions/exceptions.py +286 -0
  1211. mindspore/profiler/common/process_pool.py +41 -0
  1212. mindspore/profiler/common/singleton.py +28 -0
  1213. mindspore/profiler/common/struct_type.py +118 -0
  1214. mindspore/profiler/common/util.py +444 -0
  1215. mindspore/profiler/common/validator/__init__.py +14 -0
  1216. mindspore/profiler/common/validator/validate_path.py +84 -0
  1217. mindspore/profiler/envprofiling.py +256 -0
  1218. mindspore/profiler/parser/__init__.py +14 -0
  1219. mindspore/profiler/parser/aicpu_data_parser.py +272 -0
  1220. mindspore/profiler/parser/ascend_analysis/__init__.py +14 -0
  1221. mindspore/profiler/parser/ascend_analysis/constant.py +53 -0
  1222. mindspore/profiler/parser/ascend_analysis/file_manager.py +159 -0
  1223. mindspore/profiler/parser/ascend_analysis/function_event.py +161 -0
  1224. mindspore/profiler/parser/ascend_analysis/fwk_cann_parser.py +131 -0
  1225. mindspore/profiler/parser/ascend_analysis/fwk_file_parser.py +85 -0
  1226. mindspore/profiler/parser/ascend_analysis/msprof_timeline_parser.py +57 -0
  1227. mindspore/profiler/parser/ascend_analysis/profiler_info_parser.py +116 -0
  1228. mindspore/profiler/parser/ascend_analysis/tlv_decoder.py +86 -0
  1229. mindspore/profiler/parser/ascend_analysis/trace_event_manager.py +68 -0
  1230. mindspore/profiler/parser/ascend_cluster_generator.py +116 -0
  1231. mindspore/profiler/parser/ascend_communicate_generator.py +314 -0
  1232. mindspore/profiler/parser/ascend_flops_generator.py +116 -0
  1233. mindspore/profiler/parser/ascend_fpbp_generator.py +82 -0
  1234. mindspore/profiler/parser/ascend_hccl_generator.py +271 -0
  1235. mindspore/profiler/parser/ascend_integrate_generator.py +42 -0
  1236. mindspore/profiler/parser/ascend_memory_generator.py +185 -0
  1237. mindspore/profiler/parser/ascend_msprof_exporter.py +281 -0
  1238. mindspore/profiler/parser/ascend_msprof_generator.py +187 -0
  1239. mindspore/profiler/parser/ascend_op_generator.py +334 -0
  1240. mindspore/profiler/parser/ascend_steptrace_generator.py +94 -0
  1241. mindspore/profiler/parser/ascend_timeline_generator.py +543 -0
  1242. mindspore/profiler/parser/base_timeline_generator.py +489 -0
  1243. mindspore/profiler/parser/container.py +229 -0
  1244. mindspore/profiler/parser/cpu_gpu_timeline_generator.py +684 -0
  1245. mindspore/profiler/parser/flops_parser.py +531 -0
  1246. mindspore/profiler/parser/framework_enum.py +111 -0
  1247. mindspore/profiler/parser/framework_parser.py +854 -0
  1248. mindspore/profiler/parser/framework_struct.py +61 -0
  1249. mindspore/profiler/parser/hccl_parser.py +573 -0
  1250. mindspore/profiler/parser/hwts_log_parser.py +122 -0
  1251. mindspore/profiler/parser/integrator.py +526 -0
  1252. mindspore/profiler/parser/memory_usage_parser.py +431 -0
  1253. mindspore/profiler/parser/minddata_analyzer.py +800 -0
  1254. mindspore/profiler/parser/minddata_parser.py +186 -0
  1255. mindspore/profiler/parser/minddata_pipeline_parser.py +299 -0
  1256. mindspore/profiler/parser/msadvisor_analyzer.py +82 -0
  1257. mindspore/profiler/parser/msadvisor_parser.py +240 -0
  1258. mindspore/profiler/parser/op_intermediate_parser.py +149 -0
  1259. mindspore/profiler/parser/optime_parser.py +250 -0
  1260. mindspore/profiler/parser/profiler_info.py +141 -0
  1261. mindspore/profiler/parser/step_trace_parser.py +666 -0
  1262. mindspore/profiler/profiling.py +2054 -0
  1263. mindspore/rewrite/__init__.py +29 -0
  1264. mindspore/rewrite/api/__init__.py +17 -0
  1265. mindspore/rewrite/api/node.py +519 -0
  1266. mindspore/rewrite/api/node_type.py +53 -0
  1267. mindspore/rewrite/api/pattern_engine.py +490 -0
  1268. mindspore/rewrite/api/scoped_value.py +181 -0
  1269. mindspore/rewrite/api/symbol_tree.py +497 -0
  1270. mindspore/rewrite/ast_helpers/__init__.py +25 -0
  1271. mindspore/rewrite/ast_helpers/ast_converter.py +143 -0
  1272. mindspore/rewrite/ast_helpers/ast_finder.py +404 -0
  1273. mindspore/rewrite/ast_helpers/ast_flattener.py +268 -0
  1274. mindspore/rewrite/ast_helpers/ast_modifier.py +605 -0
  1275. mindspore/rewrite/ast_helpers/ast_replacer.py +79 -0
  1276. mindspore/rewrite/common/__init__.py +19 -0
  1277. mindspore/rewrite/common/config.py +24 -0
  1278. mindspore/rewrite/common/error_log.py +39 -0
  1279. mindspore/rewrite/common/event.py +28 -0
  1280. mindspore/rewrite/common/namer.py +271 -0
  1281. mindspore/rewrite/common/namespace.py +118 -0
  1282. mindspore/rewrite/common/observable.py +44 -0
  1283. mindspore/rewrite/common/observer.py +54 -0
  1284. mindspore/rewrite/node/__init__.py +22 -0
  1285. mindspore/rewrite/node/call_function.py +95 -0
  1286. mindspore/rewrite/node/cell_container.py +139 -0
  1287. mindspore/rewrite/node/control_flow.py +113 -0
  1288. mindspore/rewrite/node/node.py +1428 -0
  1289. mindspore/rewrite/node/node_manager.py +283 -0
  1290. mindspore/rewrite/node/node_topological_manager.py +223 -0
  1291. mindspore/rewrite/parsers/__init__.py +29 -0
  1292. mindspore/rewrite/parsers/arguments_parser.py +63 -0
  1293. mindspore/rewrite/parsers/assign_parser.py +852 -0
  1294. mindspore/rewrite/parsers/attribute_parser.py +57 -0
  1295. mindspore/rewrite/parsers/class_def_parser.py +289 -0
  1296. mindspore/rewrite/parsers/constant_parser.py +104 -0
  1297. mindspore/rewrite/parsers/container_parser.py +88 -0
  1298. mindspore/rewrite/parsers/expr_parser.py +55 -0
  1299. mindspore/rewrite/parsers/for_parser.py +61 -0
  1300. mindspore/rewrite/parsers/function_def_parser.py +84 -0
  1301. mindspore/rewrite/parsers/if_parser.py +85 -0
  1302. mindspore/rewrite/parsers/module_parser.py +117 -0
  1303. mindspore/rewrite/parsers/parser.py +43 -0
  1304. mindspore/rewrite/parsers/parser_register.py +86 -0
  1305. mindspore/rewrite/parsers/return_parser.py +37 -0
  1306. mindspore/rewrite/parsers/while_parser.py +59 -0
  1307. mindspore/rewrite/sparsify/__init__.py +0 -0
  1308. mindspore/rewrite/sparsify/sparse_transformer.py +457 -0
  1309. mindspore/rewrite/sparsify/sparsify.py +112 -0
  1310. mindspore/rewrite/sparsify/utils.py +179 -0
  1311. mindspore/rewrite/symbol_tree/__init__.py +20 -0
  1312. mindspore/rewrite/symbol_tree/symbol_tree.py +1819 -0
  1313. mindspore/rewrite/symbol_tree/symbol_tree_builder.py +76 -0
  1314. mindspore/rewrite/symbol_tree/symbol_tree_dumper.py +142 -0
  1315. mindspore/run_check/__init__.py +20 -0
  1316. mindspore/run_check/_check_version.py +574 -0
  1317. mindspore/run_check/run_check.py +66 -0
  1318. mindspore/safeguard/__init__.py +18 -0
  1319. mindspore/safeguard/rewrite_obfuscation.py +531 -0
  1320. mindspore/swresample-4.dll +0 -0
  1321. mindspore/swscale-6.dll +0 -0
  1322. mindspore/tbbmalloc.dll +0 -0
  1323. mindspore/tinyxml2.dll +0 -0
  1324. mindspore/train/__init__.py +47 -0
  1325. mindspore/train/_utils.py +439 -0
  1326. mindspore/train/amp.py +817 -0
  1327. mindspore/train/anf_ir_pb2.py +1517 -0
  1328. mindspore/train/callback/__init__.py +44 -0
  1329. mindspore/train/callback/_backup_and_restore.py +117 -0
  1330. mindspore/train/callback/_callback.py +613 -0
  1331. mindspore/train/callback/_checkpoint.py +751 -0
  1332. mindspore/train/callback/_cluster_monitor.py +201 -0
  1333. mindspore/train/callback/_dataset_graph.py +150 -0
  1334. mindspore/train/callback/_early_stop.py +239 -0
  1335. mindspore/train/callback/_flops_collector.py +238 -0
  1336. mindspore/train/callback/_history.py +92 -0
  1337. mindspore/train/callback/_lambda_callback.py +80 -0
  1338. mindspore/train/callback/_landscape.py +1049 -0
  1339. mindspore/train/callback/_loss_monitor.py +107 -0
  1340. mindspore/train/callback/_lr_scheduler_callback.py +76 -0
  1341. mindspore/train/callback/_mindio_ttp.py +443 -0
  1342. mindspore/train/callback/_on_request_exit.py +195 -0
  1343. mindspore/train/callback/_reduce_lr_on_plateau.py +226 -0
  1344. mindspore/train/callback/_summary_collector.py +1184 -0
  1345. mindspore/train/callback/_time_monitor.py +141 -0
  1346. mindspore/train/checkpoint_pb2.py +233 -0
  1347. mindspore/train/data_sink.py +219 -0
  1348. mindspore/train/dataset_helper.py +688 -0
  1349. mindspore/train/lineage_pb2.py +1260 -0
  1350. mindspore/train/loss_scale_manager.py +213 -0
  1351. mindspore/train/memory_profiling_pb2.py +298 -0
  1352. mindspore/train/metrics/__init__.py +175 -0
  1353. mindspore/train/metrics/accuracy.py +133 -0
  1354. mindspore/train/metrics/auc.py +129 -0
  1355. mindspore/train/metrics/bleu_score.py +170 -0
  1356. mindspore/train/metrics/confusion_matrix.py +700 -0
  1357. mindspore/train/metrics/cosine_similarity.py +109 -0
  1358. mindspore/train/metrics/dice.py +116 -0
  1359. mindspore/train/metrics/error.py +175 -0
  1360. mindspore/train/metrics/fbeta.py +167 -0
  1361. mindspore/train/metrics/hausdorff_distance.py +333 -0
  1362. mindspore/train/metrics/loss.py +97 -0
  1363. mindspore/train/metrics/mean_surface_distance.py +189 -0
  1364. mindspore/train/metrics/metric.py +373 -0
  1365. mindspore/train/metrics/occlusion_sensitivity.py +225 -0
  1366. mindspore/train/metrics/perplexity.py +133 -0
  1367. mindspore/train/metrics/precision.py +160 -0
  1368. mindspore/train/metrics/recall.py +159 -0
  1369. mindspore/train/metrics/roc.py +223 -0
  1370. mindspore/train/metrics/root_mean_square_surface_distance.py +191 -0
  1371. mindspore/train/metrics/topk.py +167 -0
  1372. mindspore/train/mind_ir_pb2.py +1903 -0
  1373. mindspore/train/model.py +2176 -0
  1374. mindspore/train/node_strategy_pb2.py +653 -0
  1375. mindspore/train/print_pb2.py +184 -0
  1376. mindspore/train/profiling_parallel_pb2.py +151 -0
  1377. mindspore/train/serialization.py +3101 -0
  1378. mindspore/train/summary/__init__.py +23 -0
  1379. mindspore/train/summary/_lineage_adapter.py +41 -0
  1380. mindspore/train/summary/_summary_adapter.py +496 -0
  1381. mindspore/train/summary/_writer_pool.py +207 -0
  1382. mindspore/train/summary/enums.py +56 -0
  1383. mindspore/train/summary/summary_record.py +581 -0
  1384. mindspore/train/summary/writer.py +167 -0
  1385. mindspore/train/summary_pb2.py +1165 -0
  1386. mindspore/train/train_thor/__init__.py +20 -0
  1387. mindspore/train/train_thor/convert_utils.py +268 -0
  1388. mindspore/train/train_thor/dataset_helper.py +192 -0
  1389. mindspore/train/train_thor/model_thor.py +257 -0
  1390. mindspore/turbojpeg.dll +0 -0
  1391. mindspore/vcmeta.dll +0 -0
  1392. mindspore/vcomp140.dll +0 -0
  1393. mindspore/vcruntime140.dll +0 -0
  1394. mindspore/vcruntime140_1.dll +0 -0
  1395. mindspore/version.py +1 -0
  1396. mindspore-2.3.0.dist-info/METADATA +351 -0
  1397. mindspore-2.3.0.dist-info/RECORD +1400 -0
  1398. mindspore-2.3.0.dist-info/WHEEL +5 -0
  1399. mindspore-2.3.0.dist-info/entry_points.txt +4 -0
  1400. mindspore-2.3.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,4554 @@
1
+ # Copyright 2022-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
+ """
16
+ 1. This file is an abstraction of the dataset loading class. It contains
17
+ some basic dataset operations(skip, filter, map, batch, ...).
18
+ 2. Specific dataset loading classes can be found in datasets_vision.py, datasets_text.py,
19
+ datasets_audio.py, datasets_standard_format.py and dataets_user_defined.py files.
20
+ datasets_vision.py: contains vision dataset loading classes.
21
+ datasets_text.py: contains text dataset loading classes.
22
+ datasets_audio.py: contains audio dataset loading classes.
23
+ datasets_standard_format.py: contains standard format loading classes which
24
+ any other kinds of datasets can be converted to.
25
+ dataets_user_defined.py: contains basic classes that help users to define
26
+ flexible ways to load dataset.
27
+ """
28
+ import atexit
29
+ import glob
30
+ import json
31
+ import os
32
+ import queue
33
+ import signal
34
+ import stat
35
+ import subprocess
36
+ import warnings
37
+
38
+ import gc
39
+ import time
40
+ import uuid
41
+ import multiprocessing
42
+ from enum import Enum
43
+ from importlib import import_module
44
+ import sys
45
+ import threading
46
+
47
+ import copy
48
+ import weakref
49
+ import platform
50
+ import psutil
51
+
52
+ import mindspore._c_dataengine as cde
53
+ from mindspore._c_expression import typing
54
+
55
+ from mindspore import log as logger
56
+ from mindspore.parallel._ps_context import _is_role_pserver, _is_role_sched, _get_ps_context,\
57
+ _enable_distributed_mindrt
58
+ from mindspore.dataset.engine.offload import GetOffloadModel
59
+
60
+ import mindspore.dataset.transforms.c_transforms as c_transforms
61
+ import mindspore.dataset.transforms.py_transforms as py_transforms
62
+ import mindspore.dataset.transforms as transforms
63
+ from mindspore.dataset.text.utils import SentencePieceModel, DE_C_INTER_SENTENCEPIECE_MODE
64
+ from mindspore.parallel._utils import _get_device_num
65
+ from mindspore.dataset.debug import DebugHook
66
+
67
+ from mindspore.dataset.engine import samplers
68
+ from .iterators import DictIterator, TupleIterator, DummyIterator, check_iterator_cleanup, _set_iterator_cleanup, \
69
+ ITERATORS_LIST, _unset_iterator_cleanup
70
+ from .queue import _SharedQueue, _Queue
71
+ from .validators import check_batch, check_shuffle, check_map, check_filter, check_repeat, check_skip, check_zip, \
72
+ check_rename, check_device_send, check_take, check_output_shape, check_project, \
73
+ check_sync_wait, check_zip_dataset, check_add_column, check_concat, check_split, check_bucket_batch_by_length, \
74
+ check_save, check_tuple_iterator, check_dict_iterator, check_schema, check_to_device_send, check_padded_batch, \
75
+ check_total_batch
76
+ from ..core.config import get_callback_timeout, _init_device_info, get_enable_shared_mem, get_num_parallel_workers, \
77
+ get_enable_watchdog, get_seed, set_seed, get_debug_mode, get_multiprocessing_timeout_interval, _get_debug_hook_list
78
+ from ..core.datatypes import mstype_to_detype
79
+ from ..core.validator_helpers import replace_none
80
+ from ..core.py_util_helpers import ExceptionHandler
81
+ from ..transforms.py_transforms_util import FuncWrapper, Implementation
82
+ from ..vision.transforms import ToNumpy
83
+ from ...mindrecord.config import _get_enc_key, _get_enc_mode, _get_hash_mode, encrypt, append_hash_to_file
84
+
85
+ try:
86
+ context = import_module("mindspore.context")
87
+ except ModuleNotFoundError:
88
+ context = None
89
+
90
+ if platform.system().lower() == "darwin" and multiprocessing.get_start_method() != "fork":
91
+ multiprocessing.set_start_method("fork", True)
92
+
93
+ OffloadToManualOffloadMode = {
94
+ None: cde.ManualOffloadMode.UNSPECIFIED,
95
+ False: cde.ManualOffloadMode.DISABLED,
96
+ True: cde.ManualOffloadMode.ENABLED
97
+ }
98
+
99
+ _train_dataset = None
100
+
101
+
102
+ def _set_training_dataset(dataset):
103
+ """
104
+ Set the dataset to be used when training recovery has occurred.
105
+
106
+ Args:
107
+ dataset: the training dataset or iterator
108
+ """
109
+ global _train_dataset
110
+ _train_dataset = dataset
111
+
112
+
113
+ def _get_training_dataset():
114
+ """
115
+ Get the dataset to be used when training recovery has occurred.
116
+
117
+ Returns:
118
+ training dataset/iterator
119
+ """
120
+ return _train_dataset
121
+
122
+
123
+ def _reset_training_dataset(global_step, dataset_size):
124
+ """
125
+ Reset the training dataset to the given global step.
126
+
127
+ Args:
128
+ global_step (int): Number of global steps that have completed training.
129
+ Dataset will provide data from its next step after reset.
130
+ dataset_size (int): Number of steps per epoch.
131
+ """
132
+ dataset = _get_training_dataset()
133
+ if dataset is not None:
134
+ dataset._reset(global_step, dataset_size) # pylint: disable=protected-access
135
+ else:
136
+ raise RuntimeError("Training dataset is not set.")
137
+
138
+
139
+ class Shuffle(str, Enum):
140
+ """Specify the shuffle mode.
141
+
142
+ - ``Shuffle.GLOBAL`` : Shuffle both the files and samples.
143
+ - ``Shuffle.FILES`` : Shuffle files only.
144
+ - ``Shuffle.INFILE`` : Shuffle data within each file.
145
+ """
146
+ GLOBAL: str = "global"
147
+ FILES: str = "files"
148
+ INFILE: str = "infile"
149
+
150
+
151
+ ShuffleToShuffleMode = {Shuffle.FILES: cde.ShuffleMode.FILES,
152
+ Shuffle.GLOBAL: cde.ShuffleMode.GLOBAL,
153
+ Shuffle.INFILE: cde.ShuffleMode.INFILE}
154
+
155
+
156
+ def shuffle_to_shuffle_mode(shuffle):
157
+ """
158
+ Shuffle Enum to Shuffle Mode
159
+
160
+ Args:
161
+ shuffle (Shuffle): shuffle flag to shuffle mode in C layer
162
+
163
+ Returns:
164
+ ShuffleMode, shuffle mode
165
+ """
166
+ shuffle_mode = cde.ShuffleMode.GLOBAL # Global shuffle
167
+ if not isinstance(shuffle, Shuffle):
168
+ if shuffle is None or shuffle:
169
+ shuffle_mode = cde.ShuffleMode.GLOBAL # Global shuffle
170
+ else:
171
+ shuffle_mode = cde.ShuffleMode.FALSE # No shuffle
172
+ else:
173
+ shuffle_mode = ShuffleToShuffleMode[shuffle]
174
+ return shuffle_mode
175
+
176
+
177
+ def shuffle_to_bool(shuffle):
178
+ """
179
+ Shuffle Enum to bool
180
+
181
+ Args:
182
+ shuffle (Shuffle): shuffle flag to bool
183
+
184
+ Returns:
185
+ bool, True / False
186
+ """
187
+ if shuffle is not None and not isinstance(shuffle, (bool, Shuffle)):
188
+ raise TypeError("shuffle must be of boolean or enum of 'Shuffle' values like 'Shuffle.GLOBAL' or "
189
+ "'Shuffle.FILES' or 'Shuffle.INFILE'.")
190
+
191
+ shuffle_bool = True
192
+ if not isinstance(shuffle, Shuffle):
193
+ if shuffle is None:
194
+ shuffle_bool = None
195
+ elif shuffle:
196
+ shuffle_bool = True
197
+ else:
198
+ shuffle_bool = False
199
+ else:
200
+ shuffle_bool = True
201
+ return shuffle_bool
202
+
203
+
204
+ @check_zip
205
+ def zip(datasets):
206
+ """
207
+ Zip the datasets in the input tuple of datasets.
208
+
209
+ Args:
210
+ datasets (tuple[Dataset]): A tuple of datasets to be zipped together.
211
+ The number of datasets must be more than 1.
212
+
213
+ Returns:
214
+ Dataset, a new dataset with the above operation applied.
215
+
216
+ Raises:
217
+ ValueError: If the number of datasets is 1.
218
+ TypeError: If datasets is not a tuple.
219
+
220
+ Examples:
221
+ >>> # Create a dataset which is the combination of dataset_1 and dataset_2
222
+ >>> import mindspore.dataset as ds
223
+ >>>
224
+ >>> dataset_1 = ds.GeneratorDataset([1], "column1")
225
+ >>> dataset_2 = ds.GeneratorDataset([2], "column2")
226
+ >>> dataset = ds.zip((dataset_1, dataset_2))
227
+ """
228
+ if len(datasets) <= 1:
229
+ raise ValueError(
230
+ "Can't zip empty or just one dataset!")
231
+ for dataset in datasets:
232
+ if not isinstance(dataset, Dataset):
233
+ raise TypeError("Invalid dataset, expected Dataset object, but got %s!" % type(dataset))
234
+ return ZipDataset(datasets)
235
+
236
+
237
+ def _get_operator_process():
238
+ """
239
+ Inner implemented method, mainly for passing sub-process id in C layer
240
+
241
+ Returns:
242
+ dict, mapping dict of operation id and corresponding process id.
243
+ """
244
+ global _OP_PROCESS
245
+ process_info = _OP_PROCESS
246
+ op_process = dict()
247
+ keys = process_info.keys()
248
+ fetched_all = True
249
+ for key in keys:
250
+ try:
251
+ op_process[key] = list(process_info[key][1])
252
+ item_full = (len(process_info[key][1]) == process_info[key][0])
253
+ except KeyError as err:
254
+ raise err
255
+ fetched_all = fetched_all and item_full
256
+ return op_process, fetched_all
257
+
258
+
259
+ def _set_dataset_permissions(file_name, num_files):
260
+ """
261
+ set saved dataset files' permissions to 600
262
+ the rule of dataset filenames should be the same as those in C++.
263
+ """
264
+ num_digits = len(str(num_files - 1))
265
+ if num_files == 1:
266
+ paths = [file_name]
267
+ else:
268
+ paths = ["{}{}".format(file_name, str(x).rjust(num_digits, '0')) for x in range(num_files)]
269
+
270
+ for item in paths:
271
+ if os.path.exists(item):
272
+ os.chmod(item, stat.S_IRUSR | stat.S_IWUSR)
273
+ index_file = item + ".db"
274
+ if os.path.exists(index_file):
275
+ os.chmod(index_file, stat.S_IRUSR | stat.S_IWUSR)
276
+
277
+
278
+ class Dataset:
279
+ """
280
+ Abstract class to represent a dataset in DataEngine's data pipeline.
281
+
282
+ This class is the base class of SourceDataset and Dataset, and represents
283
+ a node in the data flow graph.
284
+ Dataset
285
+ -----------------------------------------------------------
286
+ | | | |
287
+ VisionBaseDataset TextBaseDataset AudioBaseDataset |
288
+ - - - |
289
+ | | | |
290
+ ---------------------------------------- |
291
+ UnionBaseDataset |
292
+ |
293
+ SourceDataset
294
+ -
295
+ |
296
+ MappableDataset
297
+
298
+ DatasetOperation: MapDataset(UnionBaseDataset)
299
+ BatchDataset(UnionBaseDataset)
300
+ PaddedBatchDataset(UnionBaseDataset)
301
+ BucketBatchByLengthDataset(UnionBaseDataset)
302
+ ShuffleDataset(UnionBaseDataset)
303
+ FilterDataset(UnionBaseDataset)
304
+ RepeatDataset(UnionBaseDataset)
305
+ SkipDataset(UnionBaseDataset)
306
+ TakeDataset(UnionBaseDataset)
307
+ ZipDataset(UnionBaseDataset)
308
+ ConcatDataset(UnionBaseDataset)
309
+ RenameDataset(UnionBaseDataset)
310
+ ProjectDataset(UnionBaseDataset)
311
+ SyncWaitDataset(UnionBaseDataset)
312
+
313
+ Impl Dataset - vision: ImageFolderDataset(MappableDataset, VisionBaseDataset)
314
+ USPSDataset(SourceDataset, VisionBaseDataset)
315
+ Impl Dataset - text: TextFileDataset(SourceDataset, TextBaseDataset)
316
+ YahooAnswersDataset(SourceDataset, TextBaseDataset)
317
+ Impl Dataset - audio: LJSpeechDataset(MappableDataset, AudioBaseDataset)
318
+ TedliumDataset(MappableDataset, AudioBaseDataset)
319
+ Impl Dataset - standard: MindDataset(MappableDataset, UnionBaseDataset)
320
+ TFRecordDataset(SourceDataset, UnionBaseDataset)
321
+ Impl Dataset - user defined: GeneratorDataset(MappableDataset, UnionBaseDataset)
322
+ NumpySlicesDataset(GeneratorDataset)
323
+
324
+ Args:
325
+ num_parallel_workers (int, optional): Number of workers to process the dataset in parallel.
326
+ Default: ``None``.
327
+ """
328
+
329
+ def __init__(self, children=None, num_parallel_workers=None, cache=None):
330
+ # Note: children and parent are internal variables, not recommended for external using.
331
+ self.children = replace_none(children, [])
332
+ if isinstance(self.children, tuple):
333
+ self.children = list(self.children)
334
+ if not isinstance(self.children, list):
335
+ self.children = [self.children]
336
+
337
+ self.parent = []
338
+ for child in self.children:
339
+ child.parent.append(weakref.ref(self))
340
+ self.num_parallel_workers = num_parallel_workers
341
+ self.cache = cache
342
+
343
+ self._device_iter = 0
344
+ self._input_indexs = ()
345
+ self.saved_output_types = None
346
+ self.saved_output_shapes = None
347
+ self.estimated_output_shapes = None
348
+ self.runtime_context = None
349
+ self._col_names = None
350
+ self.dataset_size = None
351
+ self._batch_size = None
352
+ self._num_classes = None
353
+ self._repeat_count = None
354
+ self._class_indexing = None
355
+ self._sync = False
356
+ self._global_step = None
357
+
358
+ @staticmethod
359
+ def _get_operator_id(dataset):
360
+ """
361
+ Internal method to iterate the tree and obtain op_id of each operation.
362
+
363
+ Returns:
364
+ Dataset, the root dataset of the tree.
365
+ """
366
+ op_name = dict()
367
+ generator_process = dict()
368
+ op_name[str(dataset)] = 0
369
+ op_id = 1
370
+
371
+ def process_name(datasets, operator_id):
372
+ if not datasets:
373
+ return 0
374
+ temp = []
375
+ for item in datasets:
376
+ for d in item.children:
377
+ temp.append(d)
378
+ op_name[str(d)] = operator_id
379
+
380
+ from mindspore.dataset.engine.datasets_user_defined import GeneratorDataset
381
+ if isinstance(d, GeneratorDataset) and d.sample_fn and d.sample_fn.pids:
382
+ generator_process[operator_id] = [d.num_parallel_workers, set(d.sample_fn.pids)]
383
+
384
+ operator_id = operator_id + 1
385
+ return process_name(temp, operator_id)
386
+
387
+ process_name([dataset], op_id)
388
+ if generator_process:
389
+ global _OP_PROCESS
390
+ _OP_PROCESS.update(generator_process)
391
+ return op_name
392
+
393
+ def create_ir_tree(self, getter_mode=False):
394
+ """
395
+ Internal method to build an IR tree.
396
+
397
+ Args:
398
+ getter_mode (bool, optional): Whether to build IR tree in pull mode. Default: ``False``.
399
+
400
+ Returns:
401
+ Union[DatasetNode, Dataset], the root node of the IR tree and the root dataset of the IR tree.
402
+ """
403
+ parent = self.parent
404
+ self.parent = []
405
+ dataset = copy.deepcopy(self)
406
+ global _OP_NAME
407
+ _OP_NAME = Dataset._get_operator_id(dataset)
408
+ ir_tree = dataset.parse_tree(getter_mode)
409
+ self.parent = parent
410
+ _init_device_info()
411
+ return ir_tree, dataset
412
+
413
+ def parse_tree(self, getter_mode=False):
414
+ """
415
+ Internal method to parse the API tree into an IR tree.
416
+
417
+ Args:
418
+ getter_mode (bool, optional): Whether to build IR tree in pull mode. Default: ``False``.
419
+
420
+ Returns:
421
+ DatasetNode, the root node of the IR tree.
422
+ """
423
+ if len(self.parent) > 1:
424
+ raise ValueError("The data pipeline is not a tree (i.e., one node has 2 consumers)")
425
+ ir_children = [d.parse_tree(getter_mode) for d in self.children]
426
+ # Bootstrap can only be performed on a copy of the original dataset node.
427
+ # Bootstrap on original dataset node will make all iterators share the same process pool
428
+ self.pre_parse(getter_mode)
429
+ self.iterator_bootstrap()
430
+ ir_node = self.parse(ir_children)
431
+ ir_node = self.post_parse(ir_node)
432
+ return ir_node
433
+
434
+ def __safe_deepcopy__(self, memodict, exclude=()):
435
+ if id(self) in memodict:
436
+ return memodict[id(self)]
437
+ cls = self.__class__
438
+ new_op = cls.__new__(cls)
439
+ memodict[id(self)] = new_op
440
+ for arg, value in self.__dict__.items():
441
+ if arg in exclude:
442
+ setattr(new_op, arg, value)
443
+ else:
444
+ try:
445
+ setattr(new_op, arg, copy.deepcopy(value, memodict))
446
+ except TypeError:
447
+ setattr(new_op, arg, value)
448
+ return new_op
449
+
450
+ @staticmethod
451
+ def _noop_mode():
452
+ if _is_role_sched():
453
+ return True
454
+ return False
455
+
456
+ def iterator_bootstrap(self):
457
+ pass
458
+
459
+ def __add__(self, datasets):
460
+ return self.concat(datasets)
461
+
462
+ def to_json(self, filename=""):
463
+ """
464
+ Serialize a pipeline into JSON string and dump into file if filename is provided.
465
+
466
+ Args:
467
+ filename (str): filename of JSON file to be saved as. Default: ``""``.
468
+
469
+ Returns:
470
+ str, JSON string of the pipeline.
471
+
472
+ Examples:
473
+ >>> import mindspore.dataset as ds
474
+ >>> mnist_dataset_dir = "/path/to/mnist_dataset_directory"
475
+ >>> dataset = ds.MnistDataset(dataset_dir=mnist_dataset_dir)
476
+ >>> dataset_json = dataset.to_json("/path/to/mnist_dataset_pipeline.json")
477
+ """
478
+ ir_tree, _ = self.create_ir_tree()
479
+ return json.loads(ir_tree.to_json(filename))
480
+
481
+ @check_bucket_batch_by_length
482
+ def bucket_batch_by_length(self, column_names, bucket_boundaries, bucket_batch_sizes, element_length_function=None,
483
+ pad_info=None, pad_to_bucket_boundary=False, drop_remainder=False):
484
+ """
485
+ Bucket elements according to their lengths. Each bucket will be padded and batched when
486
+ they are full.
487
+
488
+ A length function is called on each row in the dataset. The row is then
489
+ bucketed based on its length and bucket boundaries. When a bucket reaches its
490
+ corresponding size specified in bucket_batch_sizes, the entire bucket will be
491
+ padded according to pad_info, and then form a batch.
492
+
493
+ Refer to the following figure for the execution process:
494
+
495
+ .. image:: bucket_batch_by_length_en.png
496
+
497
+ Args:
498
+ column_names (list[str]): Columns passed to element_length_function.
499
+ bucket_boundaries (list[int]): A list consisting of the upper boundaries
500
+ of the buckets. Must be strictly increasing. If there are n boundaries,
501
+ n+1 buckets are created: One bucket for [0, bucket_boundaries[0]), one
502
+ bucket for [bucket_boundaries[i], bucket_boundaries[i+1]) for each
503
+ 0<i<n-1, and the last bucket for [bucket_boundaries[n-1], inf).
504
+ bucket_batch_sizes (list[int]): A list consisting of the batch sizes for
505
+ each bucket. Must contain len(bucket_boundaries)+1 elements.
506
+ element_length_function (Callable, optional): A function that takes in
507
+ M arguments where M = len(column_names) and returns an integer. If no value
508
+ provided, parameter M the len(column_names) must be 1, and the size of the first
509
+ dimension of that column will be taken as the length. Default: ``None``.
510
+ pad_info (dict, optional): The information about how to batch each column. The key
511
+ corresponds to the column name, and the value must be a tuple of 2 elements.
512
+ The first element corresponds to the shape to pad to, and the second
513
+ element corresponds to the value to pad with. If a column is not
514
+ specified, then that column will be padded to the longest in the current
515
+ batch, and 0 will be used as the padding value. Any None dimensions will
516
+ be padded to the longest in the current batch, unless if
517
+ `pad_to_bucket_boundary` is ``True``. If no padding is wanted, set `pad_info`
518
+ to ``None``. Default: ``None``.
519
+ pad_to_bucket_boundary (bool, optional): If ``True``, will pad each None
520
+ dimension in `pad_info` to the bucket_boundary minus 1. If there are any
521
+ elements that fall into the last bucket, an error will occur.
522
+ Default: ``False``.
523
+ drop_remainder (bool, optional): If ``True``, will drop the last batch for each
524
+ bucket if it is not a full batch. Default: ``False``.
525
+
526
+ Returns:
527
+ Dataset, a new dataset with the above operation applied.
528
+
529
+ Examples:
530
+ >>> # Create a dataset where certain counts rows are combined into a batch
531
+ >>> # and drops the last incomplete batch if there is one.
532
+ >>> import mindspore.dataset as ds
533
+ >>> import numpy as np
534
+ >>> def generate_2_columns(n):
535
+ ... for i in range(n):
536
+ ... yield (np.array([i]), np.array([j for j in range(i + 1)]))
537
+ >>>
538
+ >>> column_names = ["col1", "col2"]
539
+ >>> dataset = ds.GeneratorDataset(generate_2_columns(8), column_names)
540
+ >>> bucket_boundaries = [5, 10]
541
+ >>> bucket_batch_sizes = [2, 1, 1]
542
+ >>> element_length_function = (lambda col1, col2: max(len(col1), len(col2)))
543
+ >>> # Will pad col2 to shape [bucket_boundaries[i]] where i is the
544
+ >>> # index of the bucket that is currently being batched.
545
+ >>> pad_info = {"col2": ([None], -1)}
546
+ >>> pad_to_bucket_boundary = True
547
+ >>> dataset = dataset.bucket_batch_by_length(column_names, bucket_boundaries,
548
+ ... bucket_batch_sizes,
549
+ ... element_length_function, pad_info,
550
+ ... pad_to_bucket_boundary)
551
+ """
552
+ return BucketBatchByLengthDataset(self, column_names, bucket_boundaries, bucket_batch_sizes,
553
+ element_length_function, pad_info, pad_to_bucket_boundary, drop_remainder)
554
+
555
+ @check_batch
556
+ def batch(self, batch_size, drop_remainder=False, num_parallel_workers=None, **kwargs):
557
+ """
558
+ Combine batch_size number of consecutive rows into batch which apply per_batch_map to the samples first.
559
+
560
+ For any column, all the elements within that column must have the same shape.
561
+
562
+ Refer to the following figure for the execution process:
563
+
564
+ .. image:: batch_en.png
565
+
566
+ Note:
567
+ The order of using repeat and batch reflects the number of batches and per_batch_map.
568
+ It is recommended that the repeat operation applied after the batch operation finished.
569
+
570
+ Args:
571
+ batch_size (Union[int, Callable]): The number of rows each batch is created with. An
572
+ int or callable object which takes exactly 1 parameter, BatchInfo.
573
+ drop_remainder (bool, optional): Determines whether or not to drop the last block
574
+ whose data row number is less than batch size. Default: ``False`` . If ``True`` ,
575
+ and if there are less than `batch_size` rows available to make the last batch,
576
+ then those rows will be dropped and not propagated to the child node.
577
+ num_parallel_workers (int, optional): Number of workers(threads) to process the dataset in parallel.
578
+ Default: ``None`` .
579
+ **kwargs:
580
+
581
+ - per_batch_map (Callable[[List[numpy.ndarray], ..., List[numpy.ndarray], BatchInfo], \
582
+ (List[numpy.ndarray], ..., List[numpy.ndarray])], optional): Per batch map callable.
583
+ Default: ``None``.
584
+ A callable which takes (List[numpy.ndarray], ..., List[numpy.ndarray], BatchInfo) as input parameters.
585
+ Each list[numpy.ndarray] represents a batch of numpy.ndarray on a given column. The number of lists
586
+ should match with the number of entries in input_columns. The last parameter of the callable should
587
+ always be a BatchInfo object. Per_batch_map should return
588
+ (list[numpy.ndarray], list[numpy.ndarray], ...). The length of each list in output should be the same
589
+ as the input. output_columns is required if the number of output lists is different from input.
590
+
591
+ - input_columns (Union[str, list[str]], optional): List of names of the input columns. The size of
592
+ the list should match with signature of `per_batch_map` callable. Default: ``None`` .
593
+
594
+ - output_columns (Union[str, list[str]], optional): List of names assigned to the columns
595
+ outputted by the last operation. This parameter is mandatory if len(input_columns) !=
596
+ len(output_columns). The size of this list must match the number of output
597
+ columns of the last operation. Default: ``None`` , output columns will have the same
598
+ name as the input columns, i.e., the columns will be replaced.
599
+
600
+ - python_multiprocessing (bool, optional): Parallelize Python function `per_batch_map` with
601
+ multi-processing or multi-threading mode, ``True`` means multi-processing,
602
+ ``False`` means multi-threading If `per_batch_map` is a I/O bound task, use
603
+ multi-threading mode. If `per_batch_map` is a CPU bound task, it is recommended to use
604
+ multi-processing mode. Default: ``False`` , use python multi-threading mode.
605
+
606
+ - max_rowsize(Union[int, list[int]], optional): Maximum size of row in MB that is used for shared memory
607
+ allocation to copy data between processes, the total occupied shared memory will increase as
608
+ ``num_parallel_workers`` and :func:`mindspore.dataset.config.set_prefetch_size` increase. If set
609
+ to -1, shared memory will be dynamically allocated with the actual size of data. This is only used if
610
+ ``python_multiprocessing`` is set to True. If it is an int value, it represents
611
+ ``input_columns`` and ``output_columns`` use this value as the unit to create shared memory.
612
+ If it is a list, the first element represents the ``input_columns`` use this value as the unit to
613
+ create shared memory, and the second element represents ``output_columns`` use this value as the unit
614
+ to create shared memory. Default: 16.
615
+
616
+ Returns:
617
+ Dataset, a new dataset with the above operation applied.
618
+
619
+ Examples:
620
+ >>> # 1) Create a dataset where every 5 rows are combined into a batch
621
+ >>> # and drops the last incomplete batch if there is one.
622
+ >>> import mindspore.dataset as ds
623
+ >>> from PIL import Image
624
+ >>>
625
+ >>> cifar10_dataset_dir = "/path/to/cifar10_dataset_directory"
626
+ >>> dataset = ds.Cifar10Dataset(dataset_dir=cifar10_dataset_dir, num_samples=10)
627
+ >>> dataset = dataset.batch(5, True)
628
+ >>>
629
+ >>> # 2) resize image according to its batch number, if it's 5-th batch, resize to (5^2, 5^2) = (25, 25)
630
+ >>> def np_resize(col, BatchInfo):
631
+ ... output = col.copy()
632
+ ... s = (BatchInfo.get_batch_num() + 1) ** 2
633
+ ... index = 0
634
+ ... for c in col:
635
+ ... img = Image.fromarray(c.astype('uint8')).convert('RGB')
636
+ ... img = img.resize((s, s))
637
+ ... output[index] = np.array(img)
638
+ ... index += 1
639
+ ... return (output,)
640
+ >>> dataset = dataset.batch(batch_size=8, input_columns=["image"], per_batch_map=np_resize)
641
+ >>>
642
+ >>> # 3) Create a dataset where its batch size is dynamic
643
+ >>> # Define a callable batch size function and let batch size increase 1 each time.
644
+ >>> def add_one(BatchInfo):
645
+ ... return BatchInfo.get_batch_num() + 1
646
+ >>> dataset = dataset.batch(batch_size=add_one, drop_remainder=True)
647
+ """
648
+ return BatchDataset(self, batch_size, drop_remainder, num_parallel_workers, **kwargs)
649
+
650
+ @check_padded_batch
651
+ def padded_batch(self, batch_size, drop_remainder=False, num_parallel_workers=None, pad_info=None):
652
+ """
653
+ Combine batch_size number of consecutive rows into batch which apply pad_info to the samples first.
654
+
655
+ Refer to the following figure for the execution process:
656
+
657
+ .. image:: padded_batch_en.png
658
+
659
+ Note:
660
+ The order of using repeat and padded_batch reflects the number of batches.
661
+ It is recommended that the repeat operation applied after the padded_batch operation finished.
662
+
663
+ Args:
664
+ batch_size (Union[int, Callable]): The number of rows each batch is created with. An
665
+ int or callable object which takes exactly 1 parameter, BatchInfo.
666
+ drop_remainder (bool, optional): Determines whether or not to drop the last block
667
+ whose data row number is less than batch size. Default: ``False``. If ``True``, and if there
668
+ are less than batch_size rows available to make the last batch, then those rows will
669
+ be dropped and not propagated to the child node.
670
+ num_parallel_workers (int, optional): Number of workers(threads) to process the dataset in parallel.
671
+ Default: ``None``.
672
+ pad_info (dict, optional): The pad information about how to batch each column. The key
673
+ corresponds to the column name, and the value must be a tuple of 2 elements.
674
+ The first element corresponds to the shape to pad to, and the second
675
+ element corresponds to the value to pad with. If a column is not
676
+ specified, then that column will be padded to the longest in the current
677
+ batch, and 0 will be used as the padding value. If ``pad_info={"col1": ([224, 224], 0)}``,
678
+ expand the data column named ``col1`` to shape (224, 224), and fill in the missing values with 0.
679
+ If ``pad_info={}``, all samples in the batch will be filled to the shape with the largest sample
680
+ in the current batch. If ``pad_info={"col1": (None, 100)}``, all samples in the batch will be filled
681
+ to the shape with the largest sample in the current batch, and fill in the missing values with 100.
682
+ If no padding is wanted, set `pad_info` to ``None``. Default: ``None``.
683
+
684
+ Returns:
685
+ Dataset, a new dataset with the above operation applied.
686
+
687
+ Examples:
688
+ >>> # 1) Pad every sample to the largest sample's shape and batch the samples
689
+ >>> import mindspore.dataset as ds
690
+ >>> dataset = ds.NumpySlicesDataset([[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]], "column1")
691
+ >>> dataset = dataset.padded_batch(2, True, pad_info={})
692
+ >>>
693
+ >>> # 2) Create a dataset where every 3 rows are combined into a batch
694
+ >>> # and drops the last incomplete batch if there is one.
695
+ >>> dataset = ds.NumpySlicesDataset([i for i in range(10)], "column1")
696
+ >>> dataset = dataset.padded_batch(3, True)
697
+ >>>
698
+ >>> # 3) Create a dataset where its batch size is dynamic
699
+ >>> # Define a callable batch size function and let batch size increase 1 each time.
700
+ >>> def add_one(BatchInfo):
701
+ ... return BatchInfo.get_batch_num() + 1
702
+ >>> dataset = dataset.padded_batch(batch_size=add_one, drop_remainder=True)
703
+ """
704
+ return PaddedBatchDataset(self, batch_size, drop_remainder, num_parallel_workers, pad_info)
705
+
706
+ @check_sync_wait
707
+ def sync_wait(self, condition_name, num_batch=1, callback=None):
708
+ """
709
+ Add a blocking condition to the input Dataset and a synchronize action will be applied.
710
+
711
+ Args:
712
+ condition_name (str): The condition name that is used to toggle sending next row.
713
+ num_batch (int): the number of batches without blocking at the start of each epoch.
714
+ Default: ``1``.
715
+ callback (function): The callback function that will be invoked when sync_update is called.
716
+ Default: ``None``.
717
+
718
+ Returns:
719
+ Dataset, a new dataset with the above operation applied.
720
+
721
+ Raises:
722
+ RuntimeError: If condition name already exists.
723
+
724
+ Examples:
725
+ >>> import mindspore.dataset as ds
726
+ >>> import numpy as np
727
+ >>> def gen():
728
+ ... for i in range(100):
729
+ ... yield (np.array(i),)
730
+ >>>
731
+ >>> class Augment:
732
+ ... def __init__(self, loss):
733
+ ... self.loss = loss
734
+ ...
735
+ ... def preprocess(self, input_):
736
+ ... return input_
737
+ ...
738
+ ... def update(self, data):
739
+ ... self.loss = data["loss"]
740
+ >>>
741
+ >>> batch_size = 4
742
+ >>> dataset = ds.GeneratorDataset(gen, column_names=["input"])
743
+ >>>
744
+ >>> aug = Augment(0)
745
+ >>> dataset = dataset.sync_wait(condition_name="policy", callback=aug.update)
746
+ >>> dataset = dataset.map(operations=[aug.preprocess], input_columns=["input"])
747
+ >>> dataset = dataset.batch(batch_size)
748
+ >>> count = 0
749
+ >>> for data in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
750
+ ... assert data["input"][0] == count
751
+ ... count += batch_size
752
+ ... data = {"loss": count}
753
+ ... dataset.sync_update(condition_name="policy", data=data)
754
+ """
755
+ return SyncWaitDataset(self, condition_name, num_batch, callback)
756
+
757
+ @check_shuffle
758
+ def shuffle(self, buffer_size):
759
+ """
760
+ Shuffle the dataset by creating a cache with the size of `buffer_size` .
761
+
762
+ 1. Make a shuffle buffer that contains the first `buffer_size` rows.
763
+ 2. Randomly select an element from the shuffle buffer to be the next row
764
+ propagated to the child node.
765
+ 3. Get the next row (if any) from the parent node and put it in the shuffle buffer.
766
+ 4. Repeat steps 2 and 3 until there are no more rows left in the shuffle buffer.
767
+
768
+ A random seed can be provided to be used on the first epoch via `dataset.config.set_seed` . In every subsequent
769
+ epoch, the seed is changed to a new one, randomly generated value.
770
+
771
+ Args:
772
+ buffer_size (int): The size of the buffer (must be larger than 1) for
773
+ shuffling. Setting `buffer_size` equal to the number of rows in the entire
774
+ dataset will result in a global shuffle.
775
+
776
+ Returns:
777
+ Dataset, a new dataset with the above operation applied.
778
+
779
+ Raises:
780
+ RuntimeError: If exist sync operations before shuffle.
781
+
782
+ Examples:
783
+ >>> import mindspore.dataset as ds
784
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
785
+ >>>
786
+ >>> # Optionally set the seed for fixed randomness
787
+ >>> ds.config.set_seed(58)
788
+ >>>
789
+ >>> # Create a shuffled dataset using a shuffle buffer of size 4
790
+ >>> dataset = dataset.shuffle(4)
791
+ """
792
+ return ShuffleDataset(self, buffer_size)
793
+
794
+ def flat_map(self, func):
795
+ """
796
+ Map `func` to each row in dataset and flatten the result.
797
+
798
+ Args:
799
+ func (function): A function that must take one `numpy.ndarray` as an argument and
800
+ return a `Dataset` .
801
+
802
+ Returns:
803
+ Dataset, a new dataset with the above operation applied.
804
+
805
+ Examples:
806
+ >>> import mindspore.dataset as ds
807
+ >>> # 1) flat_map on one column dataset
808
+ >>> dataset = ds.NumpySlicesDataset([[0, 1], [2, 3]], shuffle=False)
809
+ >>>
810
+ >>> def repeat(array):
811
+ ... # create a NumpySlicesDataset with the array
812
+ ... data = ds.NumpySlicesDataset(array, shuffle=False)
813
+ ... # repeat the dataset twice
814
+ ... data = data.repeat(2)
815
+ ... return data
816
+ >>>
817
+ >>> dataset = dataset.flat_map(repeat)
818
+ >>> # [0, 1, 0, 1, 2, 3, 2, 3]
819
+ >>>
820
+ >>> # 2) flat_map on multi column dataset
821
+ >>> dataset = ds.NumpySlicesDataset(([[0, 1], [2, 3]], [[0, -1], [-2, -3]]), shuffle=False)
822
+ >>>
823
+ >>> def plus_and_minus(col1, col2):
824
+ ... # apply different methods on columns
825
+ ... data = ds.NumpySlicesDataset((col1 + 1, col2 - 1), shuffle=False)
826
+ ... return data
827
+ >>>
828
+ >>> dataset = dataset.flat_map(plus_and_minus)
829
+ >>> # ([1, 2, 3, 4], [-1, -2, -3, -4])
830
+
831
+ Raises:
832
+ TypeError: If `func` is not a function.
833
+ TypeError: If `func` doesn't return a Dataset.
834
+ """
835
+ dataset = None
836
+ if not hasattr(func, '__call__'):
837
+ logger.critical("func must be a function.")
838
+ raise TypeError("func must be a function.")
839
+
840
+ for row_data in self.create_tuple_iterator(num_epochs=1, output_numpy=True):
841
+ if dataset is None:
842
+ dataset = func(*row_data)
843
+ else:
844
+ dataset += func(*row_data)
845
+
846
+ if not isinstance(dataset, Dataset):
847
+ logger.critical("flat_map must return a Dataset object.")
848
+ raise TypeError("flat_map must return a Dataset object.")
849
+ return dataset
850
+
851
+ @check_map
852
+ def map(self, operations, input_columns=None, output_columns=None, column_order=None,
853
+ num_parallel_workers=None, **kwargs):
854
+ """
855
+ Apply each operation in operations to this dataset.
856
+
857
+ Each operation will be passed one or more columns from the dataset as input, and one or
858
+ more columns will be outputted. The first operation will be passed the columns specified
859
+ in input_columns as input. If there is more than one operation in operations, the outputted
860
+ columns of the previous operation are used as the input columns for the next operation.
861
+
862
+ The columns outputted by the very last operation will be assigned names specified by
863
+ `output_columns` , and if not specified, the column name of output column is same as that of `input_columns` .
864
+
865
+ - If you use transformations (
866
+ `vision transform <https://mindspore.cn/docs/en/master/api_python/mindspore.\
867
+ dataset.transforms.html#module-mindspore.dataset.vision>`_ ,
868
+ `nlp transform <https://mindspore.cn/docs/en/master/api_python/mindspore.\
869
+ dataset.transforms.html#module-mindspore.dataset.text>`_ ,
870
+ `audio transform <https://mindspore.cn/docs/en/master/api_python/mindspore.\
871
+ dataset.transforms.html#module-mindspore.dataset.audio>`_ )
872
+ provided by mindspore dataset, please use the following parameters:
873
+
874
+ .. image:: map_parameter_en.png
875
+
876
+ - If you use user-defined transform as PyFunc (Python Func), please use the following parameters:
877
+
878
+ .. image:: map_parameter_pyfunc_en.png
879
+
880
+ Args:
881
+ operations (Union[list[TensorOperation], list[functions]]): List of operations to be
882
+ applied on the dataset. Operations are applied in the order they appear in this list.
883
+ input_columns (Union[str, list[str]], optional): List of the names of the columns that will be passed to
884
+ the first operation as input. The size of this list must match the number of
885
+ input columns expected by the first operation. Default: ``None``, the first
886
+ operation will be passed however many columns that are required, starting from
887
+ the first column.
888
+ output_columns (Union[str, list[str]], optional): List of names assigned to the columns outputted by
889
+ the last operation. This parameter is mandatory if len(input_columns) !=
890
+ len(output_columns). The size of this list must match the number of output
891
+ columns of the last operation. Default: ``None``, output columns will have the same
892
+ name as the input columns, i.e., the columns will be replaced.
893
+ num_parallel_workers (int, optional): Number of threads used to process the dataset in
894
+ parallel. Default: ``None``, the value from the configuration will be used.
895
+ **kwargs:
896
+
897
+ - python_multiprocessing (bool, optional): Parallelize Python operations with multiple worker processes.
898
+ This option could be beneficial if the Python operation is computational heavy. Default: ``False``.
899
+
900
+ - max_rowsize (Union[int, list[int]], optional): Maximum size of row in MB that is used for shared
901
+ memory allocation to copy data between processes, the total occupied shared memory will increase as
902
+ ``num_parallel_workers`` and :func:`mindspore.dataset.config.set_prefetch_size` increase. If set
903
+ to -1, shared memory will be dynamically allocated with the actual size of data. This is only used if
904
+ ``python_multiprocessing`` is set to True. If it is an int value, it represents
905
+ ``input_columns`` and ``output_columns`` use this value as the unit to create shared memory.
906
+ If it is a list, the first element represents the ``input_columns`` use this value as the unit to
907
+ create shared memory, and the second element represents ``output_columns`` use this value as the unit
908
+ to create shared memory. Default: 16.
909
+
910
+ - cache (DatasetCache, optional): Use tensor caching service to speed up dataset processing.
911
+ Default: ``None``, which means no cache is used.
912
+
913
+ - callbacks (DSCallback, list[DSCallback], optional): List of Dataset callbacks to be called.
914
+ Default: ``None``.
915
+
916
+ - offload (bool, optional): Flag to indicate whether offload is used. Default: ``None``.
917
+
918
+ Note:
919
+ - Input `operations` accepts TensorOperations defined in mindspore.dataset part, plus user-defined
920
+ Python functions (PyFuncs).
921
+ - Do not add network computing operators from mindspore.nn and mindspore.ops or others into this
922
+ `operations` .
923
+
924
+ Returns:
925
+ Dataset, a new dataset with the above operation applied.
926
+
927
+ Examples:
928
+ >>> import mindspore.dataset as ds
929
+ >>> import mindspore.dataset.vision as vision
930
+ >>> # dataset is an instance of Dataset which has 2 columns, "image" and "label".
931
+ >>> # image is of type bytes type which can be decoded to RGB
932
+ >>> # label is of type int32
933
+ >>> cifar10_dataset_dir = "/path/to/cifar10_dataset_directory"
934
+ >>> dataset = ds.Cifar10Dataset(dataset_dir=cifar10_dataset_dir)
935
+ >>>
936
+ >>> # Define two operations, where each operation accepts 1 input column and outputs 1 column.
937
+ >>> decode_op = vision.Decode(to_pil=False)
938
+ >>> random_jitter_op = vision.RandomColorAdjust(brightness=(0.8, 0.8), contrast=(1, 1),
939
+ ... saturation=(1, 1), hue=(0, 0))
940
+ >>>
941
+ >>> # 1) Simple map example.
942
+ >>>
943
+ >>> # Apply decode_op on column "image".
944
+ >>> dataset = dataset.map(operations=[decode_op], input_columns=["image"])
945
+ >>>
946
+ >>> # Decode and rename column "image" to "decoded_image".
947
+ >>> dataset = dataset.map(operations=[decode_op], input_columns=["image"], output_columns=["decoded_image"])
948
+ >>>
949
+ >>> # A simple example for user defined python function transform.
950
+ >>> dataset = ds.NumpySlicesDataset(data=[[0, 1, 2]], column_names=["data"])
951
+ >>> dataset = dataset.map(operations=[(lambda x: x - 1)], input_columns=["data"])
952
+ >>>
953
+ >>> # 2) Map example with more than one operation.
954
+ >>>
955
+ >>> # Create a dataset where the images are decoded, then randomly color jittered.
956
+ >>> # decode_op takes column "image" as input and outputs one column. The column
957
+ >>> # outputted by decode_op is passed as input to random_jitter_op.
958
+ >>> # random_jitter_op will output one column. Column "image" will be replaced by
959
+ >>> # the column outputted by random_jitter_op (the very last operation). All other
960
+ >>> # columns are unchanged.
961
+ >>> dataset = dataset.map(operations=[decode_op, random_jitter_op], input_columns=["image"])
962
+ >>>
963
+ >>> # Rename the column outputted by random_jitter_op to "image_mapped".
964
+ >>> dataset = dataset.map(operations=[decode_op, random_jitter_op], input_columns=["image"],
965
+ ... output_columns=["image_mapped"])
966
+ >>>
967
+ >>> # Map with multiple operations using pyfunc and rename column's name
968
+ >>> dataset = ds.NumpySlicesDataset(data=[[0, 1, 2]], column_names=["data"])
969
+ >>> dataset = dataset.map(operations=[(lambda x: x * x), (lambda x: x - 1)], input_columns=["data"],
970
+ ... output_columns=["data_mapped"])
971
+ >>>
972
+ >>> # 3) Example where number of input columns is not equal to number of output columns.
973
+ >>>
974
+ >>> # operations[0] is a lambda that takes 2 columns as input and outputs 3 columns.
975
+ >>> # operations[1] is a lambda that takes 3 columns as input and outputs 1 column.
976
+ >>> # operations[2] is a lambda that takes 1 column as input and outputs 4 columns.
977
+ >>> #
978
+ >>> # Note: The number of output columns of operation[i] must equal the number of
979
+ >>> # input columns of operation[i+1]. Otherwise, this map call will also result
980
+ >>> # in an error.
981
+ >>> operations = [(lambda x, y: (x, x + y, x + y + 1)),
982
+ ... (lambda x, y, z: x * y * z),
983
+ ... (lambda x: (x % 2, x % 3, x % 5, x % 7))]
984
+ >>> dataset = ds.NumpySlicesDataset(data=([[0, 1, 2]], [[3, 4, 5]]), column_names=["x", "y"])
985
+ >>> dataset = dataset.map(operations, input_columns=["x", "y"],
986
+ ... output_columns=["mod2", "mod3", "mod5", "mod7"])
987
+ """
988
+ if hasattr(self, 'operator_mixed') and getattr(self, 'operator_mixed') is True:
989
+ num_parallel_workers = 1
990
+ logger.warning(
991
+ "Input 'operations' of 'map' includes network computing operators like in mindspore.nn, mindspore.ops, "
992
+ "mindspore.numpy module and etc, which do not support multi-thread compiling, recommend to replace it "
993
+ "with python implemented operator like numpy etc. Here decrease 'num_parallel_workers' into 1.")
994
+
995
+ return MapDataset(self, operations, input_columns, output_columns, num_parallel_workers, **kwargs)
996
+
997
+ @check_filter
998
+ def filter(self, predicate, input_columns=None, num_parallel_workers=None):
999
+ """
1000
+ Filter dataset by prediction.
1001
+
1002
+ Args:
1003
+ predicate (callable): Python callable which returns a boolean value. If False then filter the element.
1004
+ input_columns (Union[str, list[str]], optional): List of names of the input columns. If not provided
1005
+ or provided with ``None``, the predicate will be applied on all columns in the dataset.
1006
+ Default: ``None``.
1007
+ num_parallel_workers (int, optional): Number of workers to process the dataset
1008
+ in parallel. Default: ``None``.
1009
+
1010
+ Returns:
1011
+ Dataset, a new dataset with the above operation applied.
1012
+
1013
+ Examples:
1014
+ >>> # generator data(0 ~ 19)
1015
+ >>> # filter the data that greater than or equal to 11
1016
+ >>> import mindspore.dataset as ds
1017
+ >>> dataset = ds.GeneratorDataset([i for i in range(20)], "data")
1018
+ >>> dataset = dataset.filter(predicate=lambda data: data < 11, input_columns = ["data"])
1019
+ """
1020
+ return FilterDataset(self, predicate, input_columns, num_parallel_workers)
1021
+
1022
+ @check_repeat
1023
+ def repeat(self, count=None):
1024
+ """
1025
+ Repeat this dataset `count` times. Repeat infinitely if the `count` is ``None`` or ``-1``.
1026
+
1027
+ Note:
1028
+ The order of using repeat and batch reflects the number of batches. It is recommended that
1029
+ the repeat operation is used after the batch operation.
1030
+
1031
+ Args:
1032
+ count (int): Number of times the dataset is going to be repeated. Default: ``None``.
1033
+
1034
+ Returns:
1035
+ Dataset, a new dataset with the above operation applied.
1036
+
1037
+ Examples:
1038
+ >>> import mindspore.dataset as ds
1039
+ >>>
1040
+ >>> # Create a dataset with 10 elements
1041
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1042
+ >>> ori_size = dataset.get_dataset_size()
1043
+ >>>
1044
+ >>> # Repeat the dataset 50 times.
1045
+ >>> dataset = dataset.repeat(50)
1046
+ >>> repeated_size = dataset.get_dataset_size()
1047
+ >>> print("ori_size", ori_size, ", repeated_size", repeated_size)
1048
+ ori_size 10 , repeated_size 500
1049
+ >>>
1050
+ >>> # Since the original dataset size is less than batch_size, thus no data is returned
1051
+ >>> dataset1 = ds.GeneratorDataset([i for i in range(10)], "column1")
1052
+ >>> dataset1 = dataset1.batch(batch_size=20, drop_remainder=True)
1053
+ >>> dataset1 = dataset1.repeat(6)
1054
+ >>>
1055
+ >>> # Repeat the original dataset to 60 elements, thus 3 batches are returned
1056
+ >>> dataset2 = ds.GeneratorDataset([i for i in range(10)], "column1")
1057
+ >>> dataset2 = dataset2.repeat(6)
1058
+ >>> dataset2 = dataset2.batch(batch_size=20, drop_remainder=True)
1059
+ >>> print("dataset1 size", dataset1.get_dataset_size(), ", dataset2 size", dataset2.get_dataset_size())
1060
+ dataset1 size 0 , dataset2 size 3
1061
+ """
1062
+ return RepeatDataset(self, count)
1063
+
1064
+ @check_skip
1065
+ def skip(self, count):
1066
+ """
1067
+ Skip the first N elements of this dataset.
1068
+
1069
+ Args:
1070
+ count (int): Number of elements in the dataset to be skipped.
1071
+
1072
+ Returns:
1073
+ Dataset, a new dataset with the above operation applied.
1074
+
1075
+ Examples:
1076
+ >>> import mindspore.dataset as ds
1077
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1078
+ >>> # Skip first 3 elements of dataset and retain 7 elements.
1079
+ >>> dataset = dataset.skip(3)
1080
+ """
1081
+ return SkipDataset(self, count)
1082
+
1083
+ @check_take
1084
+ def take(self, count=-1):
1085
+ """
1086
+ Take the first specified number of samples from the dataset.
1087
+
1088
+ Args:
1089
+ count (int, optional): The desired number of samples to take. If the value exceeds
1090
+ the total number of samples in the dataset, all data will be returned.
1091
+ Default: ``-1`` , will return all data.
1092
+
1093
+ Note:
1094
+ When there are operations that will change the number of samples of the dataset in
1095
+ the data pipeline, the location of the `take` operation can change its effect.
1096
+ For example, `batch` operation will combine the successive samples of the specified
1097
+ `batch_size` into 1 sample, so `.batch(batch_size).take(1)` will be equivalent to
1098
+ `.take(batch_size).batch(batch_size)`.
1099
+
1100
+ Returns:
1101
+ Dataset, a new dataset with the above operation applied.
1102
+
1103
+ Examples:
1104
+ >>> import mindspore.dataset as ds
1105
+ >>> mnist_dataset_dir = "/path/to/mnist_dataset_directory"
1106
+ >>> dataset = ds.MnistDataset(dataset_dir=mnist_dataset_dir)
1107
+ >>> # Take 50 samples from MNIST dataset.
1108
+ >>> dataset = dataset.take(50)
1109
+ """
1110
+ return TakeDataset(self, count)
1111
+
1112
+ def _get_absolute_split_sizes(self, sizes):
1113
+ """
1114
+ Internal method called by split to calculate absolute split sizes and to
1115
+ do some error checking after calculating absolute split sizes.
1116
+
1117
+ Returns:
1118
+ int, absolute split sizes of the dataset.
1119
+ """
1120
+ # Call get_dataset_size here and check input here because
1121
+ # don't want to call this once in check_split and another time in
1122
+ # here again
1123
+ dataset_size = self.get_dataset_size()
1124
+
1125
+ if dataset_size is None or dataset_size <= 0:
1126
+ raise RuntimeError("dataset_size is unknown, unable to split.")
1127
+
1128
+ if not isinstance(sizes, list):
1129
+ raise RuntimeError("sizes must be a list.")
1130
+
1131
+ all_int = all(isinstance(item, int) for item in sizes)
1132
+ if all_int:
1133
+ sizes_sum = sum(sizes)
1134
+ if sizes_sum != dataset_size:
1135
+ raise RuntimeError("Sum of split sizes {} is not equal to dataset size {}."
1136
+ .format(sizes_sum, dataset_size))
1137
+ return sizes
1138
+
1139
+ absolute_sizes = []
1140
+ for item in sizes:
1141
+ absolute_size = int(round(item * dataset_size))
1142
+ if absolute_size == 0:
1143
+ raise RuntimeError("Split percentage {} is too small.".format(item))
1144
+ absolute_sizes.append(absolute_size)
1145
+
1146
+ absolute_sizes_sum = sum(absolute_sizes)
1147
+
1148
+ # if we still need more rows, give them to the first split.
1149
+ # if we have too many rows, remove the extras from the first split that has
1150
+ # enough rows.
1151
+ size_difference = int(dataset_size - absolute_sizes_sum)
1152
+ if size_difference > 0:
1153
+ absolute_sizes[0] += size_difference
1154
+ else:
1155
+ for i, _ in enumerate(absolute_sizes):
1156
+ if absolute_sizes[i] + size_difference > 0:
1157
+ absolute_sizes[i] += size_difference
1158
+ break
1159
+
1160
+ if sum(absolute_sizes) != dataset_size:
1161
+ raise RuntimeError("Sum of calculated split sizes {} is not equal to dataset size {}."
1162
+ .format(absolute_sizes_sum, dataset_size))
1163
+
1164
+ return absolute_sizes
1165
+
1166
+ @check_split
1167
+ def split(self, sizes, randomize=True):
1168
+ """
1169
+ Split the dataset into smaller, non-overlapping datasets.
1170
+
1171
+ Args:
1172
+ sizes (Union[list[int], list[float]]): If a list of integers [s1, s2, …, sn] is
1173
+ provided, the dataset will be split into n datasets of size s1, size s2, …, size sn
1174
+ respectively. If the sum of all input sizes does not equal the original dataset size, an
1175
+ error will throw.
1176
+ If a list of floats [f1, f2, …, fn] is provided, all floats must be between 0 and 1
1177
+ and must sum to 1, otherwise an error will throw. The dataset will be split into n
1178
+ Datasets of size round(f1*K), round(f2*K), …, round(fn*K) where K is the size of the
1179
+ original dataset.
1180
+ If after rounding:
1181
+
1182
+ - Any size equals 0, an error will occur.
1183
+ - The sum of split sizes < K, the difference of K - sigma(round(fi * k)) will be added to the first
1184
+ split.
1185
+ - The sum of split sizes > K, the difference of sigma(round(fi * K)) - K will be removed from the first
1186
+ large enough split such that it will have at least 1 row after removing the difference.
1187
+
1188
+ randomize (bool, optional): Determines whether or not to split the data randomly. Default: ``True``.
1189
+ If True, the data will be randomly split. Otherwise, each split will be created with
1190
+ consecutive rows from the dataset.
1191
+
1192
+ Note:
1193
+ 1. Dataset cannot be sharded if split is going to be called.
1194
+ 2. It is strongly recommended to not shuffle the dataset, but use randomize=True instead.
1195
+ Shuffling the dataset may not be deterministic, which means the data in each split
1196
+ will be different in each epoch.
1197
+
1198
+ Returns:
1199
+ Tuple[Dataset], a tuple of new datasets split from the original one.
1200
+
1201
+ Raises:
1202
+ RuntimeError: If get_dataset_size returns None or is not supported for this dataset.
1203
+ RuntimeError: If `sizes` is list of integers and sum of all elements in sizes does not
1204
+ equal the dataset size.
1205
+ RuntimeError: If `sizes` is list of float and there is a split with size 0 after calculations.
1206
+ RuntimeError: If the dataset is sharded prior to calling split.
1207
+ ValueError: If `sizes` is list of float and not all floats are between 0 and 1, or if the
1208
+ floats don't sum to 1.
1209
+
1210
+ Examples:
1211
+ >>> # Split the data into train part and test part.
1212
+ >>> import mindspore.dataset as ds
1213
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1214
+ >>> train_dataset, test_dataset = dataset.split([0.9, 0.1])
1215
+ """
1216
+ if self.is_shuffled():
1217
+ logger.warning("Dataset is shuffled before split.")
1218
+
1219
+ if self.is_sharded():
1220
+ raise RuntimeError("Dataset should not be sharded before split.")
1221
+
1222
+ absolute_sizes = self._get_absolute_split_sizes(sizes)
1223
+ splits = []
1224
+ rows_to_skip = 0
1225
+ for size in absolute_sizes:
1226
+ ds = copy.deepcopy(self)
1227
+ if randomize:
1228
+ # want to shuffle the same way every epoch before split
1229
+ # in alter_tree, shuffle buffer is minimum 10000, so use 10000 here
1230
+ ds = ds.shuffle(10000)
1231
+ ds.reshuffle_each_epoch = False
1232
+
1233
+ if rows_to_skip > 0:
1234
+ ds = ds.skip(rows_to_skip)
1235
+
1236
+ ds = ds.take(size)
1237
+ splits.append(ds)
1238
+
1239
+ rows_to_skip += size
1240
+
1241
+ return tuple(splits)
1242
+
1243
+ @check_zip_dataset
1244
+ def zip(self, datasets):
1245
+ """
1246
+ Zip the datasets in the sense of input tuple of datasets. Columns in the input datasets must have different
1247
+ name.
1248
+
1249
+ Args:
1250
+ datasets (Union[Dataset, tuple[Dataset]]): A tuple of datasets or a single class Dataset
1251
+ to be zipped together with this dataset.
1252
+
1253
+ Returns:
1254
+ Dataset, a new dataset with the above operation applied.
1255
+
1256
+ Raises:
1257
+ TypeError: The parameter is not dataset object or tuple of dataset objects.
1258
+
1259
+ Examples:
1260
+ >>> # Create a dataset which is the combination of dataset_1 and dataset_2
1261
+ >>> import mindspore.dataset as ds
1262
+ >>> dataset_1 = ds.GeneratorDataset([1, 2, 3], "column1")
1263
+ >>> dataset_2 = ds.GeneratorDataset([1, 2, 3], "column2")
1264
+ >>> dataset = dataset_1.zip(dataset_2)
1265
+ """
1266
+ if isinstance(datasets, tuple):
1267
+ datasets = (self, *datasets)
1268
+ elif isinstance(datasets, Dataset):
1269
+ datasets = (self, datasets)
1270
+ else:
1271
+ raise TypeError("Invalid datasets, expected Dataset object or tuple of Dataset, but got %s!" % datasets)
1272
+ return ZipDataset(datasets)
1273
+
1274
+ @check_concat
1275
+ def concat(self, datasets):
1276
+ """
1277
+ Concatenate the dataset objects in the input list.
1278
+ Performing "+" operation on dataset objects can achieve the same effect.
1279
+
1280
+ For a dataset concatenated by many other dataset objects, it returns the data in the order of
1281
+ datasets passed in. If you want to change the data order(such as random selection from each dataset
1282
+ instead of in sequence), apply `use_sampler` method on the concatenated dataset object.
1283
+ Currently `use_sampler` supports `dataset.DistributedSampler` for sharding selection from each dataset
1284
+ or `dataset.RandomSampler` for random selection from each dataset, see examples below.
1285
+
1286
+ Note:
1287
+ The column name, and rank and type of the column data must be the same in the input datasets.
1288
+
1289
+ Args:
1290
+ datasets (Union[list, Dataset]): A list of datasets or a single class Dataset
1291
+ to be concatenated together with this dataset.
1292
+
1293
+ Returns:
1294
+ Dataset, a new dataset with the above operation applied.
1295
+
1296
+ Examples:
1297
+ >>> import mindspore.dataset as ds
1298
+ >>> dataset_1 = ds.GeneratorDataset([1, 2, 3], "column1", shuffle=False)
1299
+ >>> dataset_2 = ds.GeneratorDataset([4, 5, 6], "column1", shuffle=False)
1300
+ >>>
1301
+ >>> # Create a dataset by concatenating dataset_1 and dataset_2 with "+" operator
1302
+ >>> dataset = dataset_1 + dataset_2
1303
+ >>> # Create a dataset by concatenating dataset_1 and dataset_2 with concat operation
1304
+ >>> dataset = dataset_1.concat(dataset_2)
1305
+ >>>
1306
+ >>> # Check the data order of dataset
1307
+ >>> dataset_1 = ds.GeneratorDataset([1, 2, 3], "column1", shuffle=False)
1308
+ >>> dataset_2 = ds.GeneratorDataset([4, 5, 6], "column1", shuffle=False)
1309
+ >>> dataset = dataset_1 + dataset_2
1310
+ >>> result = list(dataset)
1311
+ >>> # [[Tensor(shape=[], dtype=Int64, value= 1)], [Tensor(shape=[], dtype=Int64, value= 2)],
1312
+ >>> # [Tensor(shape=[], dtype=Int64, value= 3)], [Tensor(shape=[], dtype=Int64, value= 4)],
1313
+ >>> # [Tensor(shape=[], dtype=Int64, value= 5)], [Tensor(shape=[], dtype=Int64, value= 6)]]
1314
+ >>>
1315
+ >>> # Change the data order of concatenated dataset with sharding selection
1316
+ >>> dataset_1 = ds.GeneratorDataset([1, 2, 3], "column1", shuffle=False)
1317
+ >>> dataset_2 = ds.GeneratorDataset([4, 5, 6], "column1", shuffle=False)
1318
+ >>> dataset = dataset_1.concat(dataset_2)
1319
+ >>> dataset.use_sampler(ds.DistributedSampler(num_shards=2, shard_id=1, shuffle=False))
1320
+ >>> result = list(dataset)
1321
+ >>> # [[Tensor(shape=[], dtype=Int64, value= 2)], [Tensor(shape=[], dtype=Int64, value= 4)],
1322
+ >>> # [Tensor(shape=[], dtype=Int64, value= 6)]]
1323
+ >>>
1324
+ >>> # Change the data order of concatenated dataset with random selection
1325
+ >>> dataset_1 = ds.GeneratorDataset([1, 2, 3], "column1", shuffle=False)
1326
+ >>> dataset_2 = ds.GeneratorDataset([4, 5, 6], "column1", shuffle=False)
1327
+ >>> dataset = dataset_1.concat(dataset_2)
1328
+ >>> dataset.use_sampler(ds.RandomSampler())
1329
+ >>> result = list(dataset)
1330
+ >>> # [[Tensor(shape=[], dtype=Int64, value= 1)], [Tensor(shape=[], dtype=Int64, value= 4)],
1331
+ >>> # [Tensor(shape=[], dtype=Int64, value= 2)], [Tensor(shape=[], dtype=Int64, value= 5)],
1332
+ >>> # [Tensor(shape=[], dtype=Int64, value= 6)], [Tensor(shape=[], dtype=Int64, value= 3)]]
1333
+ """
1334
+ if isinstance(datasets, Dataset):
1335
+ datasets = [self] + [datasets]
1336
+ elif isinstance(datasets, list):
1337
+ datasets = [self] + datasets
1338
+ else:
1339
+ raise TypeError("Invalid datasets, expected Dataset object or list of Dataset, but got %s!" % datasets)
1340
+ return ConcatDataset(datasets)
1341
+
1342
+ @check_rename
1343
+ def rename(self, input_columns, output_columns):
1344
+ """
1345
+ Rename the columns in input datasets.
1346
+
1347
+ Args:
1348
+ input_columns (Union[str, list[str]]): List of names of the input columns.
1349
+ output_columns (Union[str, list[str]]): List of names of the output columns.
1350
+
1351
+ Returns:
1352
+ Dataset, a new dataset with the above operation applied.
1353
+
1354
+ Examples:
1355
+ >>> import mindspore.dataset as ds
1356
+ >>> input_columns = ["input_col1", "input_col2", "input_col3"]
1357
+ >>> output_columns = ["output_col1", "output_col2", "output_col3"]
1358
+ >>>
1359
+ >>> # Create a dataset with 3 columns
1360
+ >>> dataset = ds.GeneratorDataset([(1, 2, 3), (3, 4, 5), (5, 6, 7)], column_names=input_columns)
1361
+ >>>
1362
+ >>> # Rename "input_col1" to "output_col1", "input_col2" to "output_col2", "input_col3" to "output_col3"
1363
+ >>> dataset = dataset.rename(input_columns=input_columns, output_columns=output_columns)
1364
+ """
1365
+
1366
+ return RenameDataset(self, input_columns, output_columns)
1367
+
1368
+ @check_project
1369
+ def project(self, columns):
1370
+ """
1371
+ The specified columns will be selected from the dataset and passed into
1372
+ the pipeline with the order specified. The other columns are discarded.
1373
+
1374
+ Args:
1375
+ columns(Union[str, list[str]]): List of names of the columns to project.
1376
+
1377
+ Returns:
1378
+ Dataset, a new dataset with the above operation applied.
1379
+
1380
+ Examples:
1381
+ >>> import mindspore.dataset as ds
1382
+ >>> # Create a dataset with 3 columns
1383
+ >>> input_columns = ["column1", "column2", "column3"]
1384
+ >>> dataset = ds.GeneratorDataset([(1, 2, 3), (3, 4, 5), (5, 6, 7)], column_names=input_columns)
1385
+ >>>
1386
+ >>> columns_to_project = ["column3", "column1", "column2"]
1387
+ >>> # in that order, regardless of the original order of columns.
1388
+ >>> dataset = dataset.project(columns=columns_to_project)
1389
+ """
1390
+
1391
+ return ProjectDataset(self, columns)
1392
+
1393
+ def apply(self, apply_func):
1394
+ """
1395
+ Apply a function in this dataset.
1396
+
1397
+ Args:
1398
+ apply_func (function): A function that must take one `Dataset` as an argument and
1399
+ return a preprocessed `Dataset` .
1400
+
1401
+ Returns:
1402
+ Dataset, a new dataset with the above operation applied.
1403
+
1404
+ Examples:
1405
+ >>> import mindspore.dataset as ds
1406
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1407
+ >>>
1408
+ >>> # Declare an apply_func function which returns a Dataset object
1409
+ >>> def apply_func(data):
1410
+ ... data = data.batch(2)
1411
+ ... return data
1412
+ >>>
1413
+ >>> # Use apply to call apply_func
1414
+ >>> dataset = dataset.apply(apply_func)
1415
+
1416
+ Raises:
1417
+ TypeError: If apply_func is not a function.
1418
+ TypeError: If apply_func doesn't return a Dataset.
1419
+ """
1420
+
1421
+ if not hasattr(apply_func, '__call__'):
1422
+ raise TypeError("apply_func must be a function.")
1423
+
1424
+ dataset = apply_func(self)
1425
+ if not isinstance(dataset, Dataset):
1426
+ raise TypeError("apply_func must return a dataset.")
1427
+ return dataset
1428
+
1429
+ @check_device_send
1430
+ def device_que(self, send_epoch_end=True, create_data_info_queue=False, queue_name=""):
1431
+ """
1432
+ Return a transferred Dataset that transfers data through a device.
1433
+
1434
+ Args:
1435
+ send_epoch_end (bool, optional): Whether to send end of sequence to device or not.
1436
+ Default: ``True``.
1437
+ create_data_info_queue (bool, optional): Whether to create queue which stores
1438
+ types and shapes of data or not. Default: ``False``.
1439
+ queue_name (str, optional): Name of queue which connects dataset processing and model
1440
+ computing. Default: ``""``.
1441
+
1442
+ Note:
1443
+ If device is Ascend, features of data will be transferred one by one. The limitation
1444
+ of data transmission per time is 256M.
1445
+
1446
+ Returns:
1447
+ Dataset, a new dataset with the above operation applied.
1448
+
1449
+ Examples:
1450
+ >>> import mindspore.dataset as ds
1451
+ >>> import time
1452
+ >>>
1453
+ >>> data = ds.TFRecordDataset('/path/to/TF_FILES', '/path/to/TF_SCHEMA_FILE', shuffle=ds.Shuffle.FILES)
1454
+ >>> data = data.device_que()
1455
+ >>> data.send()
1456
+ >>> time.sleep(0.1)
1457
+ >>> data.stop_send()
1458
+ """
1459
+ return TransferDataset(self, send_epoch_end, create_data_info_queue, queue_name)
1460
+
1461
+ @check_save
1462
+ def save(self, file_name, num_files=1, file_type='mindrecord'):
1463
+ """
1464
+ Save the dynamic data processed by the dataset pipeline in common dataset format.
1465
+ Supported dataset formats: ``'mindrecord'`` only. And you can use
1466
+ :class:`mindspore.dataset.MindDataset` API to read the saved file(s).
1467
+
1468
+ Implicit type casting exists when saving data as ``'mindrecord'`` . The transform table shows how to do
1469
+ type casting.
1470
+
1471
+ .. list-table:: Implicit Type Casting when Saving as `mindrecord`
1472
+ :widths: 25 25 50
1473
+ :header-rows: 1
1474
+
1475
+ * - Type in `dataset`
1476
+ - Type in `mindrecord`
1477
+ - Details
1478
+ * - bool
1479
+ - int32
1480
+ - transform to int32
1481
+ * - int8
1482
+ - int32
1483
+ -
1484
+ * - uint8
1485
+ - int32
1486
+ -
1487
+ * - int16
1488
+ - int32
1489
+ -
1490
+ * - uint16
1491
+ - int32
1492
+ -
1493
+ * - int32
1494
+ - int32
1495
+ -
1496
+ * - uint32
1497
+ - int64
1498
+ -
1499
+ * - int64
1500
+ - int64
1501
+ -
1502
+ * - uint64
1503
+ - int64
1504
+ - Maybe reverse
1505
+ * - float16
1506
+ - float32
1507
+ -
1508
+ * - float32
1509
+ - float32
1510
+ -
1511
+ * - float64
1512
+ - float64
1513
+ -
1514
+ * - string
1515
+ - string
1516
+ - Multi-dimensional string not supported
1517
+ * - bytes
1518
+ - bytes
1519
+ - Multi-dimensional bytes not supported
1520
+
1521
+ Note:
1522
+ 1. To save the samples in order, set dataset's `shuffle` to ``False`` and `num_files` to ``1``.
1523
+ 2. Before calling the function, do not use batch operation, repeat operation or data augmentation operations
1524
+ with random attribute in map operation.
1525
+ 3. When array dimension is variable, one-dimensional arrays or
1526
+ multi-dimensional arrays with variable dimension 0 are supported.
1527
+ 4. MindRecord does not support multi-dimensional string or multi-dimensional bytes.
1528
+
1529
+ Args:
1530
+ file_name (str): Path to dataset file.
1531
+ num_files (int, optional): Number of dataset files. Default: ``1`` .
1532
+ file_type (str, optional): Dataset format. Default: ``'mindrecord'`` .
1533
+
1534
+ Examples:
1535
+ >>> import mindspore.dataset as ds
1536
+ >>> import numpy as np
1537
+ >>>
1538
+ >>> def generator_1d():
1539
+ ... for i in range(10):
1540
+ ... yield (np.array([i]),)
1541
+ >>>
1542
+ >>> # apply dataset operations
1543
+ >>> d1 = ds.GeneratorDataset(generator_1d, ["data"], shuffle=False)
1544
+ >>> d1.save('/path/to/save_file')
1545
+ """
1546
+ if (_get_enc_key() is not None or _get_hash_mode() is not None) and num_files > 1:
1547
+ raise RuntimeError("When encode mode or hash check is enabled, " +
1548
+ "the automatic sharding function is unavailable.")
1549
+
1550
+ ir_tree, api_tree = self.create_ir_tree()
1551
+
1552
+ runtime_context = cde.PythonRuntimeContext()
1553
+ runtime_context.Init()
1554
+ consumer = cde.PythonSaveToDisk(file_name, num_files, file_type)
1555
+ consumer.Init(ir_tree)
1556
+ runtime_context.AssignConsumer(consumer)
1557
+
1558
+ consumer.Save()
1559
+
1560
+ if _get_hash_mode() is not None:
1561
+ append_hash_to_file(file_name)
1562
+ append_hash_to_file(file_name + ".db")
1563
+
1564
+ if _get_enc_key() is not None:
1565
+ encrypt(file_name, _get_enc_key(), _get_enc_mode())
1566
+ encrypt(file_name + ".db", _get_enc_key(), _get_enc_mode())
1567
+
1568
+ _set_dataset_permissions(file_name, num_files)
1569
+ del api_tree
1570
+
1571
+ @check_tuple_iterator
1572
+ def create_tuple_iterator(self, columns=None, num_epochs=-1, output_numpy=False, do_copy=True):
1573
+ """
1574
+ Create an iterator over the dataset that yields samples of type list, whose elements are
1575
+ the data for each column.
1576
+
1577
+ Args:
1578
+ columns (list[str], optional): Specify the output columns and the order.
1579
+ Default: ``None``, keep all the output columns and their original order.
1580
+ num_epochs (int, optional): The number of epochs to iterate over the entire dataset.
1581
+ Default: ``-1`` , the dataset can be iterated indefinitely.
1582
+ output_numpy (bool, optional): Whether to keep the output data as NumPy ndarray, or
1583
+ convert it to Tensor. Default: ``False`` .
1584
+ do_copy (bool, optional): Whether to copy the data when converting output to Tensor,
1585
+ or reuse the buffer for better performance, only works when `output_numpy` is ``False`` .
1586
+ Default: ``True`` .
1587
+
1588
+ Returns:
1589
+ Iterator, a dataset iterator that yields samples of type list.
1590
+
1591
+ Examples:
1592
+ >>> import mindspore.dataset as ds
1593
+ >>>
1594
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "data")
1595
+ >>> num_epochs = 3
1596
+ >>> iterator = dataset.create_tuple_iterator(num_epochs=num_epochs)
1597
+ >>> for epoch in range(num_epochs):
1598
+ ... for item in iterator:
1599
+ ... # output is of type tuple
1600
+ ... print(type(item))
1601
+ ... break
1602
+ ... break
1603
+ <class 'list'>
1604
+ """
1605
+ if output_numpy is None:
1606
+ output_numpy = False
1607
+
1608
+ if Dataset._noop_mode():
1609
+ return DummyIterator(self, 'tuple', output_numpy)
1610
+ return TupleIterator(self, columns, num_epochs, output_numpy, do_copy)
1611
+
1612
+ @check_dict_iterator
1613
+ def create_dict_iterator(self, num_epochs=-1, output_numpy=False, do_copy=True):
1614
+ """
1615
+ Create an iterator over the dataset that yields samples of type dict,
1616
+ while the key is the column name and the value is the data.
1617
+
1618
+ Args:
1619
+ num_epochs (int, optional): The number of epochs to iterate over the entire dataset.
1620
+ Default: ``-1`` , the dataset can be iterated indefinitely.
1621
+ output_numpy (bool, optional): Whether to keep the output data as NumPy ndarray, or
1622
+ convert it to Tensor. Default: ``False`` .
1623
+ do_copy (bool, optional): Whether to copy the data when converting output to Tensor,
1624
+ or reuse the buffer for better performance, only works when `output_numpy` is ``False`` .
1625
+ Default: ``True`` .
1626
+
1627
+ Returns:
1628
+ Iterator, a dataset iterator that yields samples of type dict.
1629
+
1630
+ Examples:
1631
+ >>> import mindspore.dataset as ds
1632
+ >>>
1633
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "data")
1634
+ >>> num_epochs = 3
1635
+ >>> iterator = dataset.create_dict_iterator(num_epochs=num_epochs)
1636
+ >>> for epoch in range(num_epochs):
1637
+ ... for item in iterator:
1638
+ ... # output is of type dict
1639
+ ... print(type(item))
1640
+ ... break
1641
+ ... break
1642
+ <class 'dict'>
1643
+ """
1644
+ if output_numpy is None:
1645
+ output_numpy = False
1646
+
1647
+ if Dataset._noop_mode():
1648
+ return DummyIterator(self, 'dict', output_numpy)
1649
+ return DictIterator(self, num_epochs, output_numpy, do_copy)
1650
+
1651
+ def __iter__(self):
1652
+ """Create an iterator over the dataset."""
1653
+ return self.create_tuple_iterator(num_epochs=1)
1654
+
1655
+ @property
1656
+ def input_indexs(self):
1657
+ """
1658
+ Get the column index, which represents the corresponding relationship between the data column order
1659
+ and the network when using the sink mode.
1660
+
1661
+ Returns:
1662
+ int, tuple of the input index information.
1663
+
1664
+ Examples:
1665
+ >>> import mindspore.dataset as ds
1666
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1667
+ >>> # set input_indexs
1668
+ >>> dataset.input_indexs = 10
1669
+ >>> print(dataset.input_indexs)
1670
+ 10
1671
+ """
1672
+ if self._input_indexs != ():
1673
+ return self._input_indexs
1674
+
1675
+ # find input_indexes of children
1676
+ children_input_index = [child.input_indexs for child in self.children]
1677
+
1678
+ # in case of more than one child, return the first input_indexes
1679
+ for cix in children_input_index:
1680
+ if cix != ():
1681
+ return cix
1682
+
1683
+ # if all children's input_indexes are () or the node is a leaf
1684
+ return self._input_indexs
1685
+
1686
+ @input_indexs.setter
1687
+ def input_indexs(self, value):
1688
+ self._input_indexs = value
1689
+
1690
+ def copy_batch_size(self, value):
1691
+ self._batch_size = value
1692
+
1693
+ def _init_tree_getters(self, getter_mode=True):
1694
+ """
1695
+ Get pipeline information.
1696
+
1697
+ Args:
1698
+ getter_mode (bool, optional): Whether to build IR tree in pull mode. Default: ``True``.
1699
+ """
1700
+ ir_tree, api_tree = self.create_ir_tree(getter_mode)
1701
+
1702
+ runtime_context = cde.PythonRuntimeContext()
1703
+ runtime_context.Init()
1704
+ getter = cde.TreeGetters()
1705
+ getter.Init(ir_tree)
1706
+ runtime_context.AssignConsumer(getter)
1707
+ return getter, runtime_context, api_tree
1708
+
1709
+ def __init_size_getter(self):
1710
+ """
1711
+ Get pipeline information.
1712
+ """
1713
+ ir_tree, api_tree = self.create_ir_tree()
1714
+
1715
+ runtime_context = cde.PythonRuntimeContext()
1716
+ runtime_context.Init()
1717
+ getter = cde.DatasetSizeGetters()
1718
+ getter.Init(ir_tree)
1719
+ runtime_context.AssignConsumer(getter)
1720
+ return getter, runtime_context, api_tree
1721
+
1722
+ def get_col_names(self):
1723
+ """
1724
+ Return the names of the columns in dataset.
1725
+
1726
+ Returns:
1727
+ list, list of column names in the dataset.
1728
+
1729
+ Examples:
1730
+ >>> import mindspore.dataset as ds
1731
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1732
+ >>> col_names = dataset.get_col_names()
1733
+ >>> print(col_names)
1734
+ ['column1']
1735
+
1736
+ """
1737
+ if self._col_names is None:
1738
+ runtime_getter = self._init_tree_getters()
1739
+ self._col_names = runtime_getter[0].GetColumnNames()
1740
+
1741
+ return self._col_names
1742
+
1743
+ @check_output_shape
1744
+ def output_shapes(self, estimate=False):
1745
+ """
1746
+ Get the shapes of output data.
1747
+
1748
+ Args:
1749
+ estimate (bool): If `estimate` is ``False`` , will return the shapes of first data row.
1750
+ Otherwise, will iterate the whole dataset and return the estimated shapes of data row,
1751
+ where dynamic shape is marked as None (used in dynamic data shapes scenario).
1752
+ Default: ``False`` .
1753
+
1754
+ Returns:
1755
+ list, list of shapes of each column.
1756
+
1757
+ Examples:
1758
+ >>> import mindspore.dataset as ds
1759
+ >>> import numpy as np
1760
+ >>>
1761
+ >>> def generator1():
1762
+ ... for i in range(1, 100):
1763
+ ... yield np.ones((16, 83, 83)), np.array([i])
1764
+ >>>
1765
+ >>> dataset = ds.GeneratorDataset(generator1, ["data1", "data2"])
1766
+ >>> output_shapes = dataset.output_shapes()
1767
+ >>> print(output_shapes)
1768
+ [[16, 83, 83], [1]]
1769
+ """
1770
+ # cache single shape
1771
+ if not estimate and self.saved_output_shapes is not None:
1772
+ return self.saved_output_shapes
1773
+ # cache estimate shape
1774
+ if estimate and self.estimated_output_shapes is not None:
1775
+ return self.estimated_output_shapes
1776
+
1777
+ # We have a hang problem when two-level pipeline with multiprocessing, we need to extend the life cycle
1778
+ # of runtime_context. We found this hang problem only occur on output_types and output_shapes.
1779
+ runtime_getter = self._init_tree_getters()
1780
+ self.runtime_context = runtime_getter[1]
1781
+ api_tree = runtime_getter[2]
1782
+ output_shapes = runtime_getter[0].GetOutputShapes(estimate)
1783
+ del api_tree
1784
+ # Need to terminate the runtime context to avoid the occasional hang problem for
1785
+ # Python (with multiprocessing enabled) in sink mode.
1786
+ self.runtime_context.Terminate()
1787
+ del self.runtime_context
1788
+
1789
+ if estimate:
1790
+ self.estimated_output_shapes = output_shapes
1791
+ else:
1792
+ self.saved_output_shapes = output_shapes
1793
+ return output_shapes
1794
+
1795
+ def output_types(self):
1796
+ """
1797
+ Get the types of output data.
1798
+
1799
+ Returns:
1800
+ list, list of data types.
1801
+
1802
+ Examples:
1803
+ >>> import mindspore.dataset as ds
1804
+ >>> import numpy as np
1805
+ >>>
1806
+ >>> def generator1():
1807
+ ... for i in range(1, 100):
1808
+ ... yield np.ones((16, 83, 83)).astype(np.float32), np.array([i]).astype(np.int32)
1809
+ >>>
1810
+ >>> dataset = ds.GeneratorDataset(generator1, ["data1", "data2"])
1811
+ >>> output_types = dataset.output_types()
1812
+ >>> print(output_types)
1813
+ [dtype('float32'), dtype('int32')]
1814
+ """
1815
+ if self.saved_output_types is None:
1816
+ runtime_getter = self._init_tree_getters()
1817
+ # We have a hang problem when two-level pipeline with multiprocessing, we need to extend the life cycle
1818
+ # of runtime_context. We found this hang problem only occur on output_types and output_shapes.
1819
+ self.runtime_context = runtime_getter[1]
1820
+ api_tree = runtime_getter[2]
1821
+ self.saved_output_types = runtime_getter[0].GetOutputTypes()
1822
+ del api_tree
1823
+ # Need to terminate the runtime context to avoid the occasional hang problem for
1824
+ # Python (with multiprocessing enabled) in sink mode.
1825
+ self.runtime_context.Terminate()
1826
+ del self.runtime_context
1827
+ return self.saved_output_types
1828
+
1829
+ def get_dataset_size(self):
1830
+ """
1831
+ Return the number of batches in an epoch.
1832
+
1833
+ Returns:
1834
+ int, number of batches.
1835
+
1836
+ Examples:
1837
+ >>> import mindspore.dataset as ds
1838
+ >>> import numpy as np
1839
+ >>>
1840
+ >>> # A generator return 66 samples
1841
+ >>> def generator1():
1842
+ ... for i in range(66):
1843
+ ... yield np.ones((16, 83, 83)), np.array([i])
1844
+ >>>
1845
+ >>> dataset = ds.GeneratorDataset(generator1, ["data1", "data2"])
1846
+ >>> dataset_size = dataset.get_dataset_size()
1847
+ >>> print(dataset_size)
1848
+ 66
1849
+ """
1850
+ if self.dataset_size is None:
1851
+ runtime_getter = self.__init_size_getter()
1852
+ self.dataset_size = runtime_getter[0].GetDatasetSize(False)
1853
+ if self.dataset_size == 0:
1854
+ logger.warning("Got 0 sample from dataset pipeline, check if drop all data or load dataset fail.")
1855
+
1856
+ return self.dataset_size
1857
+
1858
+ def num_classes(self):
1859
+ """
1860
+ Get the number of classes in a dataset.
1861
+
1862
+ Returns:
1863
+ int, number of classes.
1864
+
1865
+ Examples:
1866
+ >>> import mindspore.dataset as ds
1867
+ >>> # Read image files
1868
+ >>> image_folder_dataset_dir = "/path/to/image_folder_dataset_directory"
1869
+ >>> dataset = ds.ImageFolderDataset(dataset_dir=image_folder_dataset_dir)
1870
+ >>> # Check how many classes exist in image folder
1871
+ >>> num_classes = dataset.num_classes()
1872
+ """
1873
+ if self._num_classes is None:
1874
+ runtime_getter = self._init_tree_getters()
1875
+ self._num_classes = runtime_getter[0].GetNumClasses()
1876
+
1877
+ if self._num_classes == -1:
1878
+ return None
1879
+ return self._num_classes
1880
+
1881
+ def get_sync_notifiers(self):
1882
+ if self.children:
1883
+ return self.children[0].get_sync_notifiers()
1884
+ return {}
1885
+
1886
+ def disable_sync(self):
1887
+ if self.children:
1888
+ return self.children[0].disable_sync()
1889
+ return {}
1890
+
1891
+ def is_sync(self):
1892
+ if self.children:
1893
+ return self.children[0].is_sync()
1894
+ return False
1895
+
1896
+ def sync_update(self, condition_name, num_batch=None, data=None):
1897
+ """
1898
+ Release a blocking condition and trigger callback with given data.
1899
+
1900
+ Args:
1901
+ condition_name (str): The condition name that is used to toggle sending next row.
1902
+ num_batch (Union[int, None]): The number of batches (rows) that are released.
1903
+ When `num_batch` is ``None``, it will default to the number specified by the
1904
+ `sync_wait` operation. Default: ``None``.
1905
+ data (Any): The data passed to the callback, user defined. Default: ``None``.
1906
+
1907
+ Examples:
1908
+ >>> import numpy as np
1909
+ >>> import mindspore.dataset as ds
1910
+ >>>
1911
+ >>> def gen():
1912
+ ... for i in range(100):
1913
+ ... yield (np.array(i),)
1914
+ >>>
1915
+ >>> class Augment:
1916
+ ... def __init__(self, loss):
1917
+ ... self.loss = loss
1918
+ ...
1919
+ ... def preprocess(self, input_):
1920
+ ... return input_
1921
+ ...
1922
+ ... def update(self, data):
1923
+ ... self.loss = data["loss"]
1924
+ >>>
1925
+ >>> batch_size = 10
1926
+ >>> dataset = ds.GeneratorDataset(gen, column_names=["input"])
1927
+ >>> aug = Augment(0)
1928
+ >>> dataset = dataset.sync_wait(condition_name='', num_batch=1)
1929
+ >>> dataset = dataset.map(input_columns=["input"], operations=[aug.preprocess])
1930
+ >>> dataset = dataset.batch(batch_size)
1931
+ >>>
1932
+ >>> count = 0
1933
+ >>> for data in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
1934
+ ... count += 1
1935
+ ... data = {"loss": count}
1936
+ ... dataset.sync_update(condition_name="", data=data)
1937
+ """
1938
+ if (not isinstance(num_batch, int) and num_batch is not None) or \
1939
+ (isinstance(num_batch, int) and num_batch <= 0):
1940
+ # throwing exception, disable all sync_wait in pipeline
1941
+ self.disable_sync()
1942
+ raise RuntimeError("Sync_update batch size can only be positive integer, got : {}.".format(num_batch))
1943
+ notifiers_dict = self.get_sync_notifiers()
1944
+ if not isinstance(condition_name, str):
1945
+ raise TypeError("Argument condition_name with value {} is not of type str, but got {}."
1946
+ .format(condition_name, type(condition_name)))
1947
+ if condition_name not in notifiers_dict:
1948
+ # throwing exception, disable all sync_wait in pipeline
1949
+ self.disable_sync()
1950
+ raise RuntimeError("Condition name not found.")
1951
+ if num_batch is not None:
1952
+ num_batch *= self.get_batch_size()
1953
+ notifiers_dict[condition_name](num_batch, data)
1954
+
1955
+ def get_batch_size(self):
1956
+ """
1957
+ Return the size of batch.
1958
+
1959
+ Returns:
1960
+ int, the batch size of data.
1961
+
1962
+ Examples:
1963
+ >>> import mindspore.dataset as ds
1964
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1965
+ >>> dataset = dataset.batch(2)
1966
+ >>> batch_size = dataset.get_batch_size()
1967
+ >>> print(batch_size)
1968
+ 2
1969
+ """
1970
+ if self._batch_size is None:
1971
+ runtime_getter = self._init_tree_getters()
1972
+ self._batch_size = runtime_getter[0].GetBatchSize()
1973
+ if self._batch_size is None:
1974
+ self._batch_size = 1
1975
+ return self._batch_size
1976
+
1977
+ def get_repeat_count(self):
1978
+ """
1979
+ Get the replication times in RepeatDataset. Default: ``1`` .
1980
+
1981
+ Returns:
1982
+ int, the count of repeat.
1983
+
1984
+ Examples:
1985
+ >>> import mindspore.dataset as ds
1986
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
1987
+ >>> dataset = dataset.repeat(5)
1988
+ >>> repeat_count = dataset.get_repeat_count()
1989
+ >>> print(repeat_count)
1990
+ 5
1991
+ """
1992
+ if self._repeat_count is None:
1993
+ runtime_getter = self._init_tree_getters()
1994
+ self._repeat_count = runtime_getter[0].GetRepeatCount()
1995
+ if self._repeat_count is None:
1996
+ self._repeat_count = 1
1997
+ return self._repeat_count
1998
+
1999
+ def get_class_indexing(self):
2000
+ """
2001
+ Get the mapping dictionary from category names to category indexes.
2002
+
2003
+ This dictionary can be used to look up which category name corresponds to a particular category index.
2004
+
2005
+ Returns:
2006
+ Dict[str, int], the mappings from category names to category indexes.
2007
+
2008
+ Examples:
2009
+ >>> import mindspore.dataset as ds
2010
+ >>> # Read image files
2011
+ >>> image_folder_dataset_dir = "/path/to/image_folder_dataset_directory"
2012
+ >>> dataset = ds.ImageFolderDataset(dataset_dir=image_folder_dataset_dir)
2013
+ >>> # Check how many classes exist in image folder
2014
+ >>> class_indexing = dataset.get_class_indexing()
2015
+ """
2016
+ if self.children:
2017
+ return self.children[0].get_class_indexing()
2018
+ return {}
2019
+
2020
+ def reset(self):
2021
+ """
2022
+ Reset the dataset for next epoch.
2023
+
2024
+ Examples:
2025
+ >>> import mindspore.dataset as ds
2026
+ >>> mind_dataset_dir = ["/path/to/mind_dataset_file"]
2027
+ >>> dataset = ds.MindDataset(dataset_files=mind_dataset_dir)
2028
+ >>> for _ in range(5):
2029
+ ... num_iter = 0
2030
+ ... for data in dataset.create_tuple_iterator(num_epochs=1, output_numpy=True):
2031
+ ... num_iter += 1
2032
+ ... dataset.reset()
2033
+ """
2034
+
2035
+ def is_shuffled(self):
2036
+ """Returns True if the dataset or its children is shuffled."""
2037
+ for input_dataset in self.children:
2038
+ if input_dataset.is_shuffled():
2039
+ return True
2040
+
2041
+ return False
2042
+
2043
+ def is_sharded(self):
2044
+ """Returns True if the dataset or its children is sharded."""
2045
+ for input_dataset in self.children:
2046
+ if input_dataset.is_sharded():
2047
+ return True
2048
+
2049
+ return False
2050
+
2051
+ def parse(self, children=None):
2052
+ raise NotImplementedError("Dataset has to implement parse method.")
2053
+
2054
+ def __len__(self):
2055
+ """
2056
+ Get the length of dataset.
2057
+
2058
+ Returns:
2059
+ int, the length of dataset.
2060
+ """
2061
+ return self.get_dataset_size()
2062
+
2063
+ @staticmethod
2064
+ def _update_data_shard(num_shards, shard_id):
2065
+ """
2066
+ Update the shard number and shard id if necessary.
2067
+ This is normally used in distributed training mode like Parameter Server training.
2068
+ """
2069
+ # If this is in distributed execution mode,
2070
+ # the shard number and shard id might need to be updated according to the process's rank or role.
2071
+ worker_num = _get_ps_context("worker_num")
2072
+ server_num = _get_ps_context("server_num")
2073
+ if _is_role_pserver() and _enable_distributed_mindrt() and (worker_num != server_num):
2074
+ num_shards = worker_num
2075
+ shard_id = 0
2076
+ return num_shards, shard_id
2077
+
2078
+ def pre_parse(self, getter_mode):
2079
+ if getter_mode:
2080
+ if hasattr(self, "python_multiprocessing"):
2081
+ self.python_multiprocessing = False
2082
+ if hasattr(self, "num_parallel_workers"):
2083
+ self.num_parallel_workers = 1
2084
+
2085
+ def post_parse(self, ir_node):
2086
+ if self.cache:
2087
+ ir_node = ir_node.set_cache_client(self.cache.cache_client)
2088
+ if self.num_parallel_workers:
2089
+ ir_node = ir_node.set_num_workers(self.num_parallel_workers)
2090
+
2091
+ return ir_node
2092
+
2093
+ def set_init_step(self, init_step):
2094
+ self._global_step = init_step
2095
+
2096
+ def get_init_step(self):
2097
+ if self._global_step is not None:
2098
+ return self._global_step
2099
+ if len(self.children) == 1:
2100
+ return self.children[0].get_init_step()
2101
+ # When there are multiple children, we cannot tell from which child to get the initial step,
2102
+ # so we initialize from the beginning
2103
+ return 0
2104
+
2105
+
2106
+ class VisionBaseDataset(Dataset):
2107
+ """
2108
+ Abstract class to represent a vision source dataset which produces content to the data pipeline.
2109
+ """
2110
+
2111
+ def __init__(self, children=None, num_parallel_workers=None, cache=None):
2112
+ super().__init__(children=children, num_parallel_workers=num_parallel_workers, cache=cache)
2113
+
2114
+ def parse(self, children=None):
2115
+ raise NotImplementedError("Dataset has to implement parse method.")
2116
+
2117
+
2118
+ class TextBaseDataset(Dataset):
2119
+ """
2120
+ Abstract class to represent a text source dataset which produces content to the data pipeline.
2121
+ """
2122
+
2123
+ def __init__(self, children=None, num_parallel_workers=None, cache=None):
2124
+ super().__init__(children=children, num_parallel_workers=num_parallel_workers, cache=cache)
2125
+
2126
+ def parse(self, children=None):
2127
+ raise NotImplementedError("Dataset has to implement parse method.")
2128
+
2129
+ def build_vocab(self, columns, freq_range, top_k, special_tokens, special_first):
2130
+ """
2131
+ Function to create a Vocab from source dataset.
2132
+ Desired source dataset is a text type dataset.
2133
+
2134
+ Build a vocab from a dataset. This would collect all the unique words in a dataset and return a vocab
2135
+ which contains top_k most frequent words (if top_k is specified).
2136
+
2137
+ Note:
2138
+ mindspore.dataset.Dataset.build_vocab is deprecated from version 2.0
2139
+ and will be removed in a future version. Use mindspore.dataset.text.Vocab.from_dataset instead.
2140
+
2141
+ Args:
2142
+ columns(Union[str, list[str]]): Column names to get words from.
2143
+ freq_range(tuple[int]): A tuple of integers (min_frequency, max_frequency). Words within the frequency
2144
+ range will be stored.
2145
+ Naturally 0 <= min_frequency <= max_frequency <= total_words. min_frequency/max_frequency
2146
+ can be set to default, which corresponds to 0/total_words separately.
2147
+ top_k(int): Number of words to be built into vocab. top_k most frequent words are
2148
+ taken. The top_k is taken after freq_range. If not enough top_k, all words will be taken
2149
+ special_tokens(list[str]): A list of strings, each one is a special token.
2150
+ special_first(bool): Whether special_tokens will be prepended/appended to vocab, If special_tokens
2151
+ is specified and special_first is set to default, special_tokens will be prepended.
2152
+
2153
+ Returns:
2154
+ Vocab, vocab built from the dataset.
2155
+ """
2156
+ warnings.warn("mindspore.dataset.Dataset.build_vocab is deprecated from version 2.0 "
2157
+ "and will be removed in a future version. "
2158
+ "Use mindspore.dataset.text.Vocab.from_dataset instead.", DeprecationWarning)
2159
+
2160
+ def build_sentencepiece_vocab(self, columns, vocab_size, character_coverage, model_type, params):
2161
+ """
2162
+ Function to create a SentencePieceVocab from source dataset.
2163
+ Desired source dataset is a text type dataset.
2164
+
2165
+ Note:
2166
+ mindspore.dataset.Dataset.build_sentencepiece_vocab is deprecated from version 2.0
2167
+ and will be removed in a future version. Use mindspore.dataset.text.SentencePieceVocab.from_dataset instead.
2168
+
2169
+ Args:
2170
+ columns(list[str]): Column names to get words from.
2171
+ vocab_size(int): Vocabulary size.
2172
+ character_coverage(float): Percentage of characters covered by the model, must be between
2173
+ 0.98 and 1.0 Good defaults are: 0.9995 for languages with rich character sets like
2174
+ Japanese or Chinese character sets, and 1.0 for other languages with small character sets
2175
+ like English or Latin.
2176
+ model_type(SentencePieceModel): Model type. Choose from unigram (default), bpe, char, or word.
2177
+ The input sentence must be pretokenized when using word type.
2178
+ params(dict): Any extra optional parameters of sentencepiece library according to your raw data
2179
+
2180
+ Returns:
2181
+ SentencePieceVocab, vocab built from the dataset.
2182
+ """
2183
+ warnings.warn("mindspore.dataset.Dataset.build_sentencepiece_vocab is deprecated from version 2.0 "
2184
+ "and will be removed in a future version. "
2185
+ "Use mindspore.dataset.text.SentencePieceVocab.from_dataset instead.", DeprecationWarning)
2186
+
2187
+ def _build_vocab(self, columns, freq_range, top_k, special_tokens, special_first):
2188
+ """
2189
+ Function to create a Vocab from source dataset.
2190
+ Desired source dataset is a text type dataset.
2191
+
2192
+ Build a vocab from a dataset. This would collect all the unique words in a dataset and return a vocab
2193
+ which contains top_k most frequent words (if top_k is specified).
2194
+
2195
+ Args:
2196
+ columns(Union[str, list[str]]): Column names to get words from.
2197
+ freq_range(tuple[int]): A tuple of integers (min_frequency, max_frequency). Words within the frequency
2198
+ range will be stored.
2199
+ Naturally 0 <= min_frequency <= max_frequency <= total_words. min_frequency/max_frequency
2200
+ can be set to default, which corresponds to 0/total_words separately.
2201
+ top_k(int): Number of words to be built into vocab. top_k most frequent words are
2202
+ taken. The top_k is taken after freq_range. If not enough top_k, all words will be taken
2203
+ special_tokens(list[str]): A list of strings, each one is a special token.
2204
+ special_first(bool): Whether special_tokens will be prepended/appended to vocab, If special_tokens
2205
+ is specified and special_first is set to default, special_tokens will be prepended.
2206
+
2207
+ Returns:
2208
+ Vocab, vocab built from the dataset.
2209
+ """
2210
+ vocab = cde.Vocab()
2211
+ columns = replace_none(columns, [])
2212
+ if not isinstance(columns, list):
2213
+ columns = [columns]
2214
+
2215
+ freq_range = replace_none(freq_range, (0, 9223372036854775807))
2216
+ if freq_range[0] is None:
2217
+ freq_range = (0, freq_range[1])
2218
+ if freq_range[1] is None:
2219
+ freq_range = (freq_range[0], 9223372036854775807)
2220
+ special_tokens = replace_none(special_tokens, [])
2221
+ top_k = replace_none(top_k, 9223372036854775807)
2222
+
2223
+ ir_tree, api_tree = self.create_ir_tree()
2224
+
2225
+ # vocab node
2226
+ vocab_node = cde.BuildVocabNode(ir_tree, vocab, columns, freq_range, top_k, special_tokens, special_first)
2227
+
2228
+ runtime_context = cde.PythonRuntimeContext()
2229
+ runtime_context.Init()
2230
+
2231
+ # build vocab
2232
+ consumer = cde.PythonBuildVocabConsumer()
2233
+ consumer.Init(vocab_node)
2234
+ runtime_context.AssignConsumer(consumer)
2235
+
2236
+ consumer.Start()
2237
+ del api_tree
2238
+
2239
+ return vocab
2240
+
2241
+ def _build_sentencepiece_vocab(self, columns, vocab_size, character_coverage, model_type, params):
2242
+ """
2243
+ Function to create a SentencePieceVocab from source dataset.
2244
+ Desired source dataset is a text type dataset.
2245
+
2246
+ Args:
2247
+ columns(list[str]): Column names to get words from.
2248
+ vocab_size(int): Vocabulary size.
2249
+ character_coverage(float): Percentage of characters covered by the model, must be between
2250
+ 0.98 and 1.0 Good defaults are: 0.9995 for languages with rich character sets like
2251
+ Japanese or Chinese character sets, and 1.0 for other languages with small character sets
2252
+ like English or Latin.
2253
+ model_type(SentencePieceModel): Model type. Choose from unigram (default), bpe, char, or word.
2254
+ The input sentence must be pretokenized when using word type.
2255
+ params(dict): Any extra optional parameters of sentencepiece library according to your raw data
2256
+
2257
+ Returns:
2258
+ SentencePieceVocab, vocab built from the dataset.
2259
+ """
2260
+ if not isinstance(model_type, SentencePieceModel):
2261
+ raise TypeError("Argument model_type with value {0} is not of type SentencePieceModel, but got {1}." \
2262
+ .format(model_type, type(model_type)))
2263
+ model_type = DE_C_INTER_SENTENCEPIECE_MODE[model_type]
2264
+ vocab = cde.SentencePieceVocab()
2265
+
2266
+ ir_tree, api_tree = self.create_ir_tree()
2267
+
2268
+ # vocab node
2269
+ vocab_node = cde.BuildSentenceVocabNode(ir_tree, vocab, columns, vocab_size, character_coverage, model_type,
2270
+ params)
2271
+
2272
+ runtime_context = cde.PythonRuntimeContext()
2273
+ runtime_context.Init()
2274
+
2275
+ # build vocab
2276
+ consumer = cde.PythonBuildVocabConsumer()
2277
+ consumer.Init(vocab_node)
2278
+ runtime_context.AssignConsumer(consumer)
2279
+
2280
+ consumer.Start()
2281
+ del api_tree
2282
+
2283
+ return vocab
2284
+
2285
+
2286
+ class AudioBaseDataset(Dataset):
2287
+ """
2288
+ Abstract class to represent a audio source dataset which produces content to the data pipeline.
2289
+ """
2290
+
2291
+ def __init__(self, children=None, num_parallel_workers=None, cache=None):
2292
+ super().__init__(children=children, num_parallel_workers=num_parallel_workers, cache=cache)
2293
+
2294
+ def parse(self, children=None):
2295
+ raise NotImplementedError("Dataset has to implement parse method.")
2296
+
2297
+
2298
+ class UnionBaseDataset(VisionBaseDataset, TextBaseDataset, AudioBaseDataset):
2299
+ """
2300
+ Abstract class to represent a union source dataset which produces content to the data pipeline.
2301
+ """
2302
+
2303
+ def __init__(self, children=None, num_parallel_workers=None, cache=None):
2304
+ super().__init__(children=children, num_parallel_workers=num_parallel_workers, cache=cache)
2305
+
2306
+ def parse(self, children=None):
2307
+ raise NotImplementedError("Dataset has to implement parse method.")
2308
+
2309
+
2310
+ class SourceDataset(Dataset):
2311
+ """
2312
+ Abstract class to represent a source dataset which produces content to the data pipeline.
2313
+ """
2314
+
2315
+ def __init__(self, num_parallel_workers=None, num_samples=None, shuffle=True, num_shards=None, shard_id=None,
2316
+ cache=None):
2317
+ super().__init__(num_parallel_workers=num_parallel_workers, cache=cache)
2318
+ self.num_samples = replace_none(num_samples, 0)
2319
+ self.num_shards = replace_none(num_shards, 1)
2320
+ self.shard_id = replace_none(shard_id, 0)
2321
+
2322
+ if shuffle is not None and not isinstance(shuffle, (bool, Shuffle)):
2323
+ raise TypeError("shuffle must be of boolean or enum of 'Shuffle' values like 'Shuffle.GLOBAL' or "
2324
+ "'Shuffle.FILES' or 'Shuffle.INFILE'.")
2325
+
2326
+ self.shuffle_flag = 2 # Global shuffle
2327
+ if not isinstance(shuffle, Shuffle):
2328
+ if shuffle is None or shuffle:
2329
+ self.shuffle_flag = 2 # Global shuffle
2330
+ else:
2331
+ self.shuffle_flag = 0 # No shuffle
2332
+ else:
2333
+ if shuffle == Shuffle.GLOBAL:
2334
+ self.shuffle_flag = 2 # Global shuffle
2335
+ elif shuffle == Shuffle.FILES:
2336
+ self.shuffle_flag = 1 # Files shuffle
2337
+ elif shuffle == Shuffle.INFILE:
2338
+ self.shuffle_flag = 3 # Infile shuffle
2339
+
2340
+ def parse(self, children=None):
2341
+ raise NotImplementedError("Dataset has to implement parse method.")
2342
+
2343
+ @staticmethod
2344
+ def _find_files(patterns):
2345
+ """
2346
+ Utility function to search for files with the given glob patterns.
2347
+
2348
+ Args:
2349
+ patterns (Union[str, list[str]]): String or list of patterns to be searched.
2350
+
2351
+ Returns:
2352
+ list, list of files.
2353
+ """
2354
+
2355
+ if not isinstance(patterns, list):
2356
+ patterns = [patterns]
2357
+
2358
+ file_list = []
2359
+ unmatched_patterns = []
2360
+ for pattern in patterns:
2361
+ matches = [match for match in glob.glob(pattern, recursive=True) if os.path.isfile(match)]
2362
+
2363
+ if matches:
2364
+ file_list.extend(matches)
2365
+ else:
2366
+ unmatched_patterns.append(pattern)
2367
+
2368
+ if unmatched_patterns:
2369
+ raise ValueError("The following patterns did not match any files: {}.".format(unmatched_patterns))
2370
+
2371
+ if file_list: # not empty
2372
+ return file_list
2373
+ raise ValueError("The list of path names matching the patterns is empty.")
2374
+
2375
+ def is_shuffled(self):
2376
+ return self.shuffle_flag > 0
2377
+
2378
+ def is_sharded(self):
2379
+ if self.num_shards is not None:
2380
+ return self.num_shards > 1
2381
+ return False
2382
+
2383
+
2384
+ class MappableDataset(SourceDataset):
2385
+ """
2386
+ Abstract class to represent a source dataset which supports use of samplers.
2387
+ """
2388
+
2389
+ def parse(self, children=None):
2390
+ raise NotImplementedError("Dataset has to implement parse method.")
2391
+
2392
+ def __init__(self, num_parallel_workers=None, sampler=None, num_samples=None, shuffle=None, num_shards=None,
2393
+ shard_id=None, cache=None):
2394
+ num_shards, shard_id = self._update_data_shard(num_shards, shard_id)
2395
+ super().__init__(num_parallel_workers=num_parallel_workers, num_samples=num_samples, shuffle=shuffle,
2396
+ num_shards=num_shards, shard_id=shard_id, cache=cache)
2397
+ self.shuffle_flag = replace_none(shuffle, True)
2398
+ self.sampler = samplers.select_sampler(num_samples, sampler, shuffle, num_shards, shard_id)
2399
+
2400
+ def add_sampler(self, new_sampler):
2401
+ """
2402
+ Add a child sampler for the current dataset.
2403
+
2404
+ Args:
2405
+ new_sampler (Sampler): The child sampler to be added.
2406
+
2407
+ Examples:
2408
+ >>> import mindspore.dataset as ds
2409
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
2410
+ >>>
2411
+ >>> new_sampler = ds.DistributedSampler(10, 2)
2412
+ >>> dataset.add_sampler(new_sampler)
2413
+ """
2414
+ # Note: By adding a sampler, the sampled IDs will flow to the new_sampler
2415
+ # after first passing through the current samplers attached to this dataset.
2416
+ self.dataset_size = None
2417
+ new_sampler.add_child(self.sampler)
2418
+ self.sampler = new_sampler
2419
+
2420
+ def use_sampler(self, new_sampler):
2421
+ """
2422
+ Replace the last child sampler of the current dataset, remaining the parent sampler unchanged.
2423
+
2424
+ Args:
2425
+ new_sampler (Sampler): The new sampler to replace with.
2426
+
2427
+ Examples:
2428
+ >>> import mindspore.dataset as ds
2429
+ >>> dataset = ds.GeneratorDataset([i for i in range(10)], "column1")
2430
+ >>>
2431
+ >>> # use a DistributedSampler instead
2432
+ >>> new_sampler = ds.DistributedSampler(10, 2)
2433
+ >>> dataset.use_sampler(new_sampler)
2434
+ """
2435
+ if new_sampler is None:
2436
+ raise TypeError("Input sampler can not be None.")
2437
+ if not isinstance(new_sampler, (samplers.BuiltinSampler, samplers.Sampler)):
2438
+ raise TypeError("Input sampler is not an instance of a sampler.")
2439
+ self.dataset_size = None
2440
+
2441
+ self.sampler = self.sampler.child_sampler
2442
+ self.add_sampler(new_sampler)
2443
+
2444
+ def is_shuffled(self):
2445
+ return self.sampler.is_shuffled()
2446
+
2447
+ def is_sharded(self):
2448
+ return self.sampler.is_sharded()
2449
+
2450
+ @check_split
2451
+ def split(self, sizes, randomize=True):
2452
+ """
2453
+ Split the dataset into smaller, non-overlapping datasets.
2454
+
2455
+ Args:
2456
+ sizes (Union[list[int], list[float]]): If a list of integers [s1, s2, …, sn] is
2457
+ provided, the dataset will be split into n datasets of size s1, size s2, …, size sn
2458
+ respectively. If the sum of all sizes does not equal the original dataset size, an
2459
+ error will occur.
2460
+ If a list of floats [f1, f2, …, fn] is provided, all floats must be between 0 and 1
2461
+ and must sum to 1, otherwise an error will occur. The dataset will be split into n
2462
+ Datasets of size round(f1*K), round(f2*K), …, round(fn*K) where K is the size of the
2463
+ original dataset.
2464
+ If after rounding:
2465
+
2466
+ - Any size equals 0, an error will occur.
2467
+ - The sum of split sizes < K, the difference will be added to the first split.
2468
+ - The sum of split sizes > K, the difference will be removed from the first large
2469
+ enough split such that it will have at least 1 row after removing the difference.
2470
+
2471
+ randomize (bool, optional): Determines whether or not to split the data randomly. Default: ``True``.
2472
+ If ``True``, the data will be randomly split. Otherwise, each split will be created with
2473
+ consecutive rows from the dataset.
2474
+
2475
+ Note:
2476
+ 1. There is an optimized split function, which will be called automatically when the dataset
2477
+ that calls this function is a MappableDataset.
2478
+ 2. Dataset should not be sharded if split is going to be called. Instead, create a
2479
+ :class:`mindspore.dataset.DistributedSampler` and specify a split to shard after splitting.
2480
+ If the dataset is sharded after a split, it is strongly recommended setting the same
2481
+ seed in each instance of execution, otherwise each shard may not be part of the same
2482
+ split (see Examples).
2483
+ 3. It is strongly recommended to not shuffle the dataset, but set `randomize` to ``True`` instead.
2484
+ Shuffling the dataset may not be deterministic, which means the data in each split
2485
+ will be different in each epoch. Furthermore, if sharding occurs after split, each
2486
+ shard may not be part of the same split.
2487
+
2488
+ Returns:
2489
+ Tuple[Dataset], a tuple of new datasets split from the original one.
2490
+
2491
+ Raises:
2492
+ RuntimeError: If get_dataset_size returns None or is not supported for this dataset.
2493
+ RuntimeError: If `sizes` is list of integers and sum of all elements in sizes does not
2494
+ equal the dataset size.
2495
+ RuntimeError: If `sizes` is list of float and there is a split with size 0 after calculations.
2496
+ RuntimeError: If the dataset is sharded prior to calling split.
2497
+ ValueError: If `sizes` is list of float and not all floats are between 0 and 1, or if the
2498
+ floats don't sum to 1.
2499
+
2500
+ Examples:
2501
+ >>> import mindspore.dataset as ds
2502
+ >>> # Since many datasets have shuffle on by default, set shuffle to False if split will be called!
2503
+ >>> image_folder_dataset_dir = "/path/to/image_folder_dataset_directory"
2504
+ >>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir, shuffle=False)
2505
+ >>>
2506
+ >>> # Set the seed, and tell split to use this seed when randomizing.
2507
+ >>> # This is needed because sharding will be done later
2508
+ >>> ds.config.set_seed(58)
2509
+ >>> train_dataset, test_dataset = dataset.split([0.9, 0.1])
2510
+ >>>
2511
+ >>> # To shard the train dataset, use a DistributedSampler
2512
+ >>> train_sampler = ds.DistributedSampler(10, 2)
2513
+ >>> train_dataset.use_sampler(train_sampler)
2514
+ """
2515
+ if self.is_shuffled():
2516
+ logger.warning("Dataset is shuffled before split.")
2517
+
2518
+ if self.is_sharded():
2519
+ raise RuntimeError("Dataset should not be sharded before split.")
2520
+
2521
+ absolute_sizes = self._get_absolute_split_sizes(sizes)
2522
+ splits = []
2523
+ current_split_start_index = 0
2524
+ for size in absolute_sizes:
2525
+ ds = copy.deepcopy(self)
2526
+ ds.dataset_size = None
2527
+ if randomize:
2528
+ # want to shuffle the same way every epoch before split, we are assuming
2529
+ # that the user will call set_seed
2530
+ random_sampler = samplers.RandomSampler()
2531
+ random_sampler.reshuffle_each_epoch = False
2532
+ ds.add_sampler(random_sampler)
2533
+
2534
+ subset_sampler = samplers.SequentialSampler(current_split_start_index, size)
2535
+ ds.add_sampler(subset_sampler)
2536
+
2537
+ # add sequential sampler, so that if user calls use_sampler, we will
2538
+ # get rid of the sequential sampler instead of something we need
2539
+ ds.add_sampler(samplers.SequentialSampler())
2540
+
2541
+ splits.append(ds)
2542
+
2543
+ current_split_start_index += size
2544
+
2545
+ return tuple(splits)
2546
+
2547
+
2548
+ class BucketBatchByLengthDataset(UnionBaseDataset):
2549
+ """
2550
+ The result of applying BucketBatchByLength operation to the input dataset.
2551
+ """
2552
+
2553
+ def __init__(self, input_dataset, column_names, bucket_boundaries, bucket_batch_sizes, element_length_function,
2554
+ pad_info, pad_to_bucket_boundary, drop_remainder):
2555
+ super().__init__(children=input_dataset)
2556
+
2557
+ self.column_names = to_list(column_names)
2558
+ self.bucket_boundaries = replace_none(bucket_boundaries, [])
2559
+ self.bucket_batch_sizes = replace_none(bucket_batch_sizes, [])
2560
+ self.element_length_function = element_length_function
2561
+ self.pad_info = replace_none(pad_info, {})
2562
+ self.pad_to_bucket_boundary = replace_none(pad_to_bucket_boundary, False)
2563
+ self.drop_remainder = replace_none(drop_remainder, False)
2564
+
2565
+ def parse(self, children=None):
2566
+ return cde.BucketBatchByLengthNode(children[0], self.column_names, self.bucket_boundaries,
2567
+ self.bucket_batch_sizes, self.element_length_function, self.pad_info,
2568
+ self.pad_to_bucket_boundary, self.drop_remainder)
2569
+
2570
+
2571
+ def _check_shm_usage(num_worker, queue_size, in_rowsize, out_rowsize):
2572
+ """
2573
+ Check sufficient shared memory is available for shared memory queues
2574
+ when training in parallel mode.
2575
+ """
2576
+ threshold_ratio = 0.8
2577
+ # Verify available size only when using static shared memory on Linux
2578
+ if platform.system().lower() not in {"windows", "darwin"} and in_rowsize != -1 and out_rowsize != -1:
2579
+ device_num = _get_device_num()
2580
+ # In the cluster, _get_device_num indicates the number of the entire cluster. The maximum number of cards
2581
+ # on the ascend server is 8.
2582
+ if device_num > 1:
2583
+ device_num = min(device_num, 8)
2584
+ shm_estimate_usage = device_num * num_worker * \
2585
+ (queue_size + 2) * (in_rowsize + out_rowsize) * 1024 * 1024
2586
+ try:
2587
+ shm_available = psutil.disk_usage('/dev/shm').free
2588
+ if shm_estimate_usage >= threshold_ratio * shm_available:
2589
+ raise RuntimeError(
2590
+ "Insufficient shared memory available. Required: {}, Available: {}. "
2591
+ "The required memory can't exceed 80% of the available shared memory, "
2592
+ "it's recommended to reduce memory usage by following methods:\n"
2593
+ "1. reduce value of parameter max_rowsize or num_parallel_workers.\n"
2594
+ "2. reduce prefetch size by set_prefetch_size().\n"
2595
+ "3. disable shared memory by set_enable_shared_mem().".format(shm_estimate_usage, shm_available))
2596
+ except FileNotFoundError:
2597
+ raise RuntimeError("Expected /dev/shm to exist.")
2598
+
2599
+
2600
+ class BatchDataset(UnionBaseDataset):
2601
+ """
2602
+ The result of applying Batch operation to the input dataset.
2603
+
2604
+ Args:
2605
+ input_dataset (Dataset): Input Dataset to be batched.
2606
+ batch_size (Union[int, function]): The number of rows each batch is created with. An
2607
+ int or callable which takes exactly 1 parameter, BatchInfo.
2608
+ drop_remainder (bool, optional): Determines whether or not to drop the last
2609
+ possibly incomplete batch. Default: ``False``. If True, and if there are less
2610
+ than batch_size rows available to make the last batch, then those rows will
2611
+ be dropped and not propagated to the child node.
2612
+ num_parallel_workers (int, optional): Number of workers to process the dataset in parallel. Default: ``None``.
2613
+ per_batch_map (callable, optional): Per batch map callable. A callable which takes
2614
+ (list[Tensor], list[Tensor], ..., BatchInfo) as input parameters. Each list[Tensor] represents a batch of
2615
+ Tensors on a given column. The number of lists should match with number of entries in input_columns. The
2616
+ last parameter of the callable must always be a BatchInfo object.
2617
+ input_columns (Union[str, list[str]], optional): List of names of the input columns. The size of the list must
2618
+ match with signature of per_batch_map callable.
2619
+ output_columns (Union[str, list[str]], optional): List of names assigned to the columns outputted by
2620
+ the last operation. This parameter is mandatory if len(input_columns) !=
2621
+ len(output_columns). The size of this list must match the number of output
2622
+ columns of the last operation. Default: ``None``, output columns will have the same
2623
+ name as the input columns, i.e., the columns will be replaced.
2624
+ max_rowsize(Union[int, list[int]], optional): Maximum size of row in MB that is used for shared memory
2625
+ allocation to copy data between processes, the total occupied shared memory will increase as
2626
+ ``num_parallel_workers`` and :func:`mindspore.dataset.config.set_prefetch_size` increase. If set to -1,
2627
+ shared memory will be dynamically allocated with the actual size of data. This is only used if
2628
+ ``python_multiprocessing`` is set to True. If it is an int value, it represents
2629
+ ``input_columns`` and ``output_columns`` use this value as the unit to create shared memory.
2630
+ If it is a list, the first element represents the ``input_columns`` use this value as the unit to
2631
+ create shared memory, and the second element represents ``output_columns`` use this value as the unit
2632
+ to create shared memory. Default: 16.
2633
+
2634
+ """
2635
+
2636
+ def __init__(self, input_dataset, batch_size, drop_remainder=False, num_parallel_workers=None, per_batch_map=None,
2637
+ input_columns=None, output_columns=None, python_multiprocessing=False, max_rowsize=16):
2638
+ super().__init__(children=input_dataset, num_parallel_workers=num_parallel_workers)
2639
+
2640
+ if BatchDataset._is_ancestor_of_repeat(input_dataset):
2641
+ logger.warning("Repeat is located before batch, data from two epochs can be batched together.")
2642
+
2643
+ BatchDataset._update_batch_size_for_syncwait(input_dataset, batch_size)
2644
+
2645
+ # if batch_size is callable, set batch_size to 1 and batch_size_func to that callable function
2646
+ self.batch_size = batch_size if not callable(batch_size) else 1
2647
+ self.batch_size_func = None if not callable(batch_size) else batch_size
2648
+
2649
+ self.drop_remainder = replace_none(drop_remainder, False)
2650
+
2651
+ self.per_batch_map = per_batch_map
2652
+
2653
+ self.input_columns = to_list(input_columns)
2654
+ self.output_columns = to_list(output_columns)
2655
+
2656
+ self.python_multiprocessing = python_multiprocessing
2657
+ self.process_pool = None
2658
+ if isinstance(max_rowsize, int):
2659
+ self.max_rowsize = [max_rowsize * self.batch_size] * 2 if max_rowsize != -1 else [max_rowsize, max_rowsize]
2660
+ else:
2661
+ self.max_rowsize = [max_rowsize[0] * self.batch_size, max_rowsize[1] * self.batch_size]
2662
+
2663
+ def __del__(self):
2664
+ if hasattr(self, "process_pool") and self.process_pool is not None:
2665
+ self.process_pool.terminate()
2666
+ del self.process_pool
2667
+
2668
+ def parse(self, children=None):
2669
+ return cde.BatchNode(children[0], self.batch_size, self.drop_remainder, False, self.input_columns,
2670
+ self.output_columns, self.batch_size_func, self.per_batch_map, {},
2671
+ self.process_pool)
2672
+
2673
+ @staticmethod
2674
+ def _is_ancestor_of_repeat(dataset):
2675
+ """
2676
+ Utility function to find the case where repeat is used before batch.
2677
+
2678
+ Args:
2679
+ dataset (Dataset): Dataset to be checked.
2680
+
2681
+ Returns:
2682
+ bool, whether repeat is used before batch.
2683
+ """
2684
+ if isinstance(dataset, RepeatDataset):
2685
+ return True
2686
+ flag = False
2687
+ for input_dataset in dataset.children:
2688
+ flag = flag | BatchDataset._is_ancestor_of_repeat(input_dataset)
2689
+ return flag
2690
+
2691
+ @staticmethod
2692
+ def _update_batch_size_for_syncwait(dataset, batch_size):
2693
+ """
2694
+ Utility function to notify batch size to sync_wait.
2695
+
2696
+ Args:
2697
+ dataset (Dataset): Dataset to be checked.
2698
+ batch_size (int): batch size to notify.
2699
+ """
2700
+ if isinstance(dataset, SyncWaitDataset):
2701
+ dataset.update_sync_batch_size(batch_size)
2702
+ for input_dataset in dataset.children:
2703
+ BatchDataset._update_batch_size_for_syncwait(input_dataset, batch_size)
2704
+
2705
+ def __deepcopy__(self, memodict):
2706
+ return self.__safe_deepcopy__(memodict, exclude=("per_batch_map", "batch_size_func", "__transfer_dataset__"))
2707
+
2708
+ # Iterator bootstrap will be called on iterator construction.
2709
+ # A deep copy of Dataset object is created prior of iterator_bootstrap.
2710
+ # This method will create per iterator process pool and bind pyfunc execution to the pool.
2711
+ def iterator_bootstrap(self):
2712
+ """
2713
+ Per iterator bootstrap callback.
2714
+ """
2715
+ if self.python_multiprocessing and platform.system().lower() == 'windows':
2716
+ logger.warning("Python multiprocessing is not supported on Windows platform.")
2717
+ if self.python_multiprocessing and get_debug_mode():
2718
+ logger.warning("Python multiprocessing is not supported in debug mode."
2719
+ " Ignoring Python multiprocessing for batch operation.")
2720
+ self.python_multiprocessing = False
2721
+ if self.python_multiprocessing and platform.system().lower() != 'windows':
2722
+ if self.per_batch_map is None:
2723
+ logger.warning("per_batch_map is None so python_multiprocessing is ignored for batch.")
2724
+ return
2725
+
2726
+ # If user didn't specify num_parallel_workers, set it to default
2727
+ if self.num_parallel_workers is None:
2728
+ self.num_parallel_workers = get_num_parallel_workers()
2729
+
2730
+ self.process_pool = _PythonMultiprocessing(str(self), self.num_parallel_workers, [self.per_batch_map],
2731
+ self.max_rowsize)
2732
+ # Wrap per_batch_map into _PythonCallable
2733
+ self.per_batch_map = _PythonCallable(self.per_batch_map, 0, self.process_pool)
2734
+ else:
2735
+ if self.per_batch_map is not None:
2736
+ self.per_batch_map = FuncWrapper(self.per_batch_map)
2737
+
2738
+
2739
+ class BatchInfo(cde.CBatchInfo):
2740
+ """
2741
+ This class helps to get dataset information dynamically when the input of `batch_size` or `per_batch_map`
2742
+ in `batch` operation is a callable object.
2743
+ """
2744
+
2745
+ def get_batch_num(self):
2746
+ """
2747
+ Return the batch number being processed in current epoch, start from 0.
2748
+
2749
+ Examples:
2750
+ >>> # Create a dataset where its batch size is dynamic
2751
+ >>> # Define a callable batch size function and let batch size increase 1 each time.
2752
+ >>> import mindspore.dataset as ds
2753
+ >>> from mindspore.dataset import BatchInfo
2754
+ >>>
2755
+ >>> dataset = ds.GeneratorDataset([i for i in range(3)], "column1", shuffle=False)
2756
+ >>> def add_one(BatchInfo):
2757
+ ... return BatchInfo.get_batch_num() + 1
2758
+ >>> dataset = dataset.batch(batch_size=add_one)
2759
+ >>> print(list(dataset))
2760
+ [[Tensor(shape=[1], dtype=Int64, value= [0])], [Tensor(shape=[2], dtype=Int64, value= [1, 2])]]
2761
+ """
2762
+ return
2763
+
2764
+ def get_epoch_num(self):
2765
+ """
2766
+ Return the epoch number, start from 0.
2767
+
2768
+ Examples:
2769
+ >>> # Create a dataset where its batch size is dynamic
2770
+ >>> # Define a callable batch size function and let batch size increase 1 each epoch.
2771
+ >>> import mindspore.dataset as ds
2772
+ >>> from mindspore.dataset import BatchInfo
2773
+ >>>
2774
+ >>> dataset = ds.GeneratorDataset([i for i in range(4)], "column1", shuffle=False)
2775
+ >>> def add_one_by_epoch(BatchInfo):
2776
+ ... return BatchInfo.get_epoch_num() + 1
2777
+ >>> dataset = dataset.batch(batch_size=add_one_by_epoch)
2778
+ >>>
2779
+ >>> result = []
2780
+ >>> epoch = 2
2781
+ >>> iterator = dataset.create_tuple_iterator(num_epochs=epoch)
2782
+ >>> for i in range(epoch):
2783
+ ... result.extend(list(iterator))
2784
+ >>> # result:
2785
+ >>> # [[Tensor(shape=[1], dtype=Int64, value= [0])], [Tensor(shape=[1], dtype=Int64, value= [1])],
2786
+ >>> # [Tensor(shape=[1], dtype=Int64, value= [2])], [Tensor(shape=[1], dtype=Int64, value= [3])],
2787
+ >>> # [Tensor(shape=[2], dtype=Int64, value= [0, 1])], [Tensor(shape=[2], dtype=Int64, value= [2, 3])]]
2788
+ """
2789
+ return
2790
+
2791
+
2792
+ class BlockReleasePair:
2793
+ """
2794
+ The blocking condition class used by SyncWaitDataset.
2795
+
2796
+ Args:
2797
+ init_release_rows (int): Number of lines to allow through the pipeline.
2798
+ callback (function): The callback function that will be called when release is called. Default: ``None``.
2799
+ """
2800
+
2801
+ def __init__(self, init_release_rows, callback=None):
2802
+ if isinstance(init_release_rows, int) and init_release_rows <= 0:
2803
+ raise ValueError("release_rows need to be greater than 0.")
2804
+ self.row_count = -init_release_rows
2805
+ self.cv = threading.Condition()
2806
+ self.callback = callback
2807
+ self.default_rows = init_release_rows
2808
+ self.disable = False
2809
+
2810
+ def __deepcopy__(self, memodict):
2811
+ return self
2812
+
2813
+ def reset(self):
2814
+ with self.cv:
2815
+ self.row_count = -self.default_rows
2816
+ self.cv.notify_all()
2817
+
2818
+ def update_batched_size(self, batch_size):
2819
+ # sanity check
2820
+ if isinstance(batch_size, int) and batch_size <= 0:
2821
+ raise ValueError("batch_size need to be greater than 0.")
2822
+
2823
+ # should only use before the pipeline creates
2824
+ self.row_count *= batch_size
2825
+ self.default_rows *= batch_size
2826
+
2827
+ def block_func(self):
2828
+ """
2829
+ Function for handing blocking condition.
2830
+
2831
+ Returns:
2832
+ bool, True.
2833
+ """
2834
+ with self.cv:
2835
+ # if disable is true, the always evaluate to true
2836
+ not_time_out = self.cv.wait_for(lambda: (self.row_count < 0 or self.disable),
2837
+ timeout=get_callback_timeout())
2838
+ # time_out will be False if time out occurs
2839
+ if not not_time_out:
2840
+ logger.warning("Timeout happened in sync_wait, maybe dataset.sync_update(condition=...) "
2841
+ "is not added after dataset.create_dict_iterator(...), now disabling lock.")
2842
+ self.disable = True
2843
+ self.row_count += 1
2844
+ return True
2845
+
2846
+ def release_func(self, pass_rows=None, data=None):
2847
+ with self.cv:
2848
+ if pass_rows is None:
2849
+ pass_rows = self.default_rows
2850
+ self.row_count -= pass_rows
2851
+ if self.callback is not None:
2852
+ self.callback(data)
2853
+ self.cv.notify_all()
2854
+
2855
+ def disable_lock(self):
2856
+ with self.cv:
2857
+ self.disable = True
2858
+ self.cv.notify_all()
2859
+
2860
+
2861
+ class PaddedBatchDataset(UnionBaseDataset):
2862
+ """
2863
+ The result of applying Batch operation to the input dataset.
2864
+
2865
+ Args:
2866
+ input_dataset (Dataset): Input Dataset to be batched.
2867
+ batch_size (Union[int, function]): The number of rows each batch is created with. An
2868
+ int or callable which takes exactly 1 parameter, BatchInfo.
2869
+ drop_remainder (bool, optional): Determines whether or not to drop the last
2870
+ possibly incomplete batch. Default: ``False``. If True, and if there are less
2871
+ than batch_size rows available to make the last batch, then those rows will
2872
+ be dropped and not propagated to the child node.
2873
+ num_parallel_workers (int, optional): Number of workers to process the dataset in parallel. Default: ``None``.
2874
+ pad_info (dict, optional): Whether to perform padding on selected columns. pad_info={"col1":([224,224],0)}
2875
+ will pad column with name "col1" to a tensor of size [224,224] and fill the missing with 0.
2876
+ """
2877
+
2878
+ def __init__(self, input_dataset, batch_size, drop_remainder=False, num_parallel_workers=None, pad_info=None):
2879
+ super().__init__(children=input_dataset, num_parallel_workers=num_parallel_workers)
2880
+
2881
+ if PaddedBatchDataset._is_ancestor_of_repeat(input_dataset):
2882
+ logger.warning("Repeat is located before padded_batch, data from two epochs can be batched together.")
2883
+
2884
+ PaddedBatchDataset._update_batch_size_for_syncwait(input_dataset, batch_size)
2885
+
2886
+ # if batch_size is callable, set batch_size to 1 and batch_size_func to that callable function
2887
+ self.batch_size = batch_size if not callable(batch_size) else 1
2888
+ self.batch_size_func = None if not callable(batch_size) else batch_size
2889
+
2890
+ self.drop_remainder = replace_none(drop_remainder, False)
2891
+
2892
+ self.pad = bool(pad_info is not None)
2893
+ self.pad_info = replace_none(pad_info, dict())
2894
+
2895
+ def parse(self, children=None):
2896
+ return cde.BatchNode(children[0], self.batch_size, self.drop_remainder, self.pad, [],
2897
+ [], self.batch_size_func, None, self.pad_info, None)
2898
+
2899
+ @staticmethod
2900
+ def _is_ancestor_of_repeat(dataset):
2901
+ """
2902
+ Utility function to find the case where repeat is used before batch.
2903
+
2904
+ Args:
2905
+ dataset (Dataset): Dataset to be checked.
2906
+
2907
+ Returns:
2908
+ bool, whether repeat is used before batch.
2909
+ """
2910
+ if isinstance(dataset, RepeatDataset):
2911
+ return True
2912
+ flag = False
2913
+ for input_dataset in dataset.children:
2914
+ flag = flag | PaddedBatchDataset._is_ancestor_of_repeat(input_dataset)
2915
+ return flag
2916
+
2917
+ @staticmethod
2918
+ def _update_batch_size_for_syncwait(dataset, batch_size):
2919
+ """
2920
+ Utility function to notify batch size to sync_wait.
2921
+
2922
+ Args:
2923
+ dataset (Dataset): Dataset to be checked.
2924
+ batch_size (int): batch size to notify.
2925
+ """
2926
+ if isinstance(dataset, SyncWaitDataset):
2927
+ dataset.update_sync_batch_size(batch_size)
2928
+ for input_dataset in dataset.children:
2929
+ PaddedBatchDataset._update_batch_size_for_syncwait(input_dataset, batch_size)
2930
+
2931
+ def __deepcopy__(self, memodict):
2932
+ return self.__safe_deepcopy__(memodict, exclude=("batch_size_func", "__transfer_dataset__"))
2933
+
2934
+
2935
+ class SyncWaitDataset(UnionBaseDataset):
2936
+ """
2937
+ The result of adding a blocking condition to the input Dataset.
2938
+
2939
+ Args:
2940
+ input_dataset (Dataset): Input dataset to apply flow control.
2941
+ num_batch (int): Number of batches without blocking at the start of each epoch.
2942
+ condition_name (str): Condition name that is used to toggle sending next row.
2943
+ callback (function): Callback function that will be invoked when sync_update is called. Default: ``None``.
2944
+
2945
+ Raises:
2946
+ RuntimeError: If condition name already exists.
2947
+ """
2948
+
2949
+ def __init__(self, input_dataset, condition_name, num_batch, callback=None):
2950
+ super().__init__(children=input_dataset)
2951
+
2952
+ # set to the default value, waiting for the batch to update it
2953
+ self._condition_name = condition_name
2954
+ if isinstance(num_batch, int) and num_batch <= 0:
2955
+ raise ValueError("num_batch need to be greater than 0.")
2956
+
2957
+ self._pair = BlockReleasePair(num_batch, callback)
2958
+ if self._condition_name in self.children[0].get_sync_notifiers():
2959
+ raise RuntimeError("Condition name is already in use.")
2960
+ logger.info("Please remember to add dataset.sync_update(condition=%s), otherwise hanging will result. "
2961
+ "If dataset.sync_update(condition=%s) has already been added, you can ignore the info.",
2962
+ condition_name, condition_name)
2963
+
2964
+ def parse(self, children=None):
2965
+ return cde.SyncWaitNode(children[0], self._condition_name, self._pair.block_func)
2966
+
2967
+ def get_sync_notifiers(self):
2968
+ return {**self.children[0].get_sync_notifiers(), **{self._condition_name: self._pair.release_func}}
2969
+
2970
+ def is_sync(self):
2971
+ return True
2972
+
2973
+ def update_sync_batch_size(self, batch_size):
2974
+ if isinstance(batch_size, int) and batch_size <= 0:
2975
+ raise ValueError("num_batch need to be greater than 0.")
2976
+ self._pair.update_batched_size(batch_size)
2977
+
2978
+ def disable_sync(self):
2979
+ logger.info("Disabling Sync")
2980
+ self._pair.disable_lock()
2981
+
2982
+ @staticmethod
2983
+ def _is_ancestor_of_batch(dataset):
2984
+ """
2985
+ Utility function to find the case where sync_wait is used before batch.
2986
+
2987
+ Args:
2988
+ dataset (Dataset): Dataset to be checked.
2989
+
2990
+ Returns:
2991
+ bool, whether sync_wait is used before batch.
2992
+ """
2993
+ if isinstance(dataset, (BatchDataset, PaddedBatchDataset)):
2994
+ return True
2995
+ flag = False
2996
+ for input_dataset in dataset.children:
2997
+ flag = flag | SyncWaitDataset._is_ancestor_of_batch(input_dataset)
2998
+ return flag
2999
+
3000
+ def iterator_bootstrap(self):
3001
+ self._pair.reset()
3002
+
3003
+
3004
+ class ShuffleDataset(UnionBaseDataset):
3005
+ """
3006
+ The result of applying Shuffle operation to the input Dataset.
3007
+
3008
+ Args:
3009
+ input_dataset (Dataset): Input Dataset to be shuffled.
3010
+ buffer_size (int): Size of the buffer.
3011
+
3012
+ Raises:
3013
+ RuntimeError: If exist sync operations before shuffle.
3014
+ """
3015
+
3016
+ def __init__(self, input_dataset, buffer_size):
3017
+ super().__init__(children=input_dataset)
3018
+ self.buffer_size = buffer_size
3019
+ self.reshuffle_each_epoch = True
3020
+
3021
+ if self.is_sync():
3022
+ raise RuntimeError("No shuffle after sync operators.")
3023
+
3024
+ def parse(self, children=None):
3025
+ return cde.ShuffleNode(children[0], self.buffer_size, self.reshuffle_each_epoch)
3026
+
3027
+ def is_shuffled(self):
3028
+ return True
3029
+
3030
+
3031
+ # Pyfunc collection for multiprocess pyfunc
3032
+ # This global variable will only be used within subprocesses
3033
+ _OP_NAME = dict()
3034
+ _OP_PROCESS = dict()
3035
+
3036
+
3037
+ # PythonCallable wrapper for multiprocess pyfunc
3038
+ class _PythonCallable:
3039
+ """
3040
+ Internal Python function wrapper for multiprocessing pyfunc.
3041
+ """
3042
+
3043
+ def __init__(self, py_callable, idx, pool=None):
3044
+ # Original Python callable from user.
3045
+ self.py_callable = py_callable
3046
+ # Process pool created for current iterator.
3047
+ self.pool = pool
3048
+ # Python callable index
3049
+ self.idx = idx
3050
+
3051
+ def __call__(self, *args):
3052
+ result = None
3053
+ get_data_from_worker_process = False
3054
+ while get_data_from_worker_process is False:
3055
+ if self.pool.is_running() and check_iterator_cleanup() is False:
3056
+ try:
3057
+ result = self.pool.execute(self.idx, *args)
3058
+ except multiprocessing.TimeoutError:
3059
+ continue
3060
+ get_data_from_worker_process = True
3061
+ else:
3062
+ # worker process is stopped
3063
+ logger.info("The worker process of map operation is stopped. "
3064
+ "So return None to main thread and break the main thread.")
3065
+ return None
3066
+ # got value from worker process
3067
+ if not isinstance(result, tuple) and get_data_from_worker_process is True:
3068
+ result = (result,)
3069
+ return result
3070
+
3071
+ def to_json(self):
3072
+ return self.py_callable.to_json()
3073
+
3074
+
3075
+ # used when python_multiprocessing=True in map
3076
+ class Pipe:
3077
+ """
3078
+ Class to handle communication between the master process and the worker processes.
3079
+ """
3080
+
3081
+ def __init__(self, warning_ctl, shared_memory=False, max_rowsize=16):
3082
+ self.shared_memory = shared_memory
3083
+ self.eof = multiprocessing.Event()
3084
+ if self.shared_memory:
3085
+ self.in_queue = _SharedQueue(1, warning_ctl, max_rowsize=max_rowsize[0])
3086
+ self.res_queue = _SharedQueue(1, warning_ctl, max_rowsize=max_rowsize[1])
3087
+ else:
3088
+ self.in_queue = _Queue(1)
3089
+ self.res_queue = _Queue(1)
3090
+ self.in_queue.cancel_join_thread() # Ensure that the process does not hung when exiting
3091
+
3092
+ def master_send(self, func_index, data):
3093
+ self.in_queue.put_nowait((func_index, *data))
3094
+
3095
+ def master_receive(self):
3096
+ if self.eof is None:
3097
+ raise RuntimeError("EOF is none when get data from worker.")
3098
+ if self.eof.is_set():
3099
+ return None
3100
+ return self.res_queue.get(timeout=1)
3101
+
3102
+ def master_close(self):
3103
+ self.eof.set()
3104
+ self.send_finish_signal_to_worker()
3105
+ self.send_finish_signal()
3106
+
3107
+ def send_finish_signal(self):
3108
+ self.worker_send(None)
3109
+
3110
+ def send_finish_signal_to_worker(self):
3111
+ self.master_send(0, "QUIT")
3112
+
3113
+ def worker_send(self, data):
3114
+ self.res_queue.put_until(data, timeout=1, exit_signal=self.eof)
3115
+
3116
+ def worker_receive(self):
3117
+ result = self.in_queue.get_until(timeout=1, exit_signal=self.eof)
3118
+ if result is None:
3119
+ return result
3120
+ if len(result) == 1:
3121
+ raise RuntimeError(f"Corrupted data. Worker received {len(result)} elements, it should be more than 1.")
3122
+ func_index, *data = result
3123
+ return func_index, tuple(data)
3124
+
3125
+
3126
+ def _main_process_already_exit():
3127
+ """
3128
+ Judge whether main process already exit.
3129
+ """
3130
+ ppid = os.getppid()
3131
+
3132
+ if (platform.system().lower() != 'windows' and
3133
+ not _PythonMultiprocessing.is_process_alive(ppid)):
3134
+ return True
3135
+ return False
3136
+
3137
+
3138
+ def _worker_loop(operations, pipe, worker_id):
3139
+ """
3140
+ Multiprocess worker process loop.
3141
+ """
3142
+ # Ensure that the process does not hung when exiting
3143
+ pipe.res_queue.cancel_join_thread()
3144
+
3145
+ def _ignore_sigint():
3146
+ """
3147
+ We need to ignore sigint signal here so subprocesses can exit normally and clear.
3148
+ """
3149
+ signal.signal(signal.SIGINT, signal.SIG_IGN)
3150
+
3151
+ # If the default random seed has not been changed, there is no need to fix the randomness.
3152
+ # Otherwise, set the random seed for each child process to "base_seed + worker_id" to ensure
3153
+ # that the random results of each process are different.
3154
+ if get_seed() != 5489:
3155
+ set_seed(get_seed() + worker_id)
3156
+ while not _main_process_already_exit():
3157
+ _ignore_sigint()
3158
+
3159
+ result = pipe.worker_receive()
3160
+ if result is None:
3161
+ return
3162
+ (idx, input_tensors) = result
3163
+ if input_tensors == "QUIT":
3164
+ break
3165
+ try:
3166
+ output_tensors = operations[idx](*input_tensors)
3167
+
3168
+ pipe.worker_send(output_tensors)
3169
+ except Exception:
3170
+ pipe.worker_send(ExceptionHandler(where="in map(or batch) worker and execute Python function"))
3171
+ # Do not return
3172
+
3173
+ # release the queue when stop the worker by master
3174
+ del pipe.in_queue
3175
+ del pipe.res_queue
3176
+
3177
+
3178
+ def worker_target(operations, worker_id):
3179
+ return lambda pipe: _worker_loop(operations, pipe, worker_id)
3180
+
3181
+
3182
+ class _MPWorker(multiprocessing.Process):
3183
+ """
3184
+ Worker process for multiprocessing.
3185
+ """
3186
+
3187
+ def __init__(self, operations, warning_ctl, max_rowsize=16, worker_id=0):
3188
+ shared_memory = get_enable_shared_mem()
3189
+ self.pipe = Pipe(warning_ctl, shared_memory=shared_memory, max_rowsize=max_rowsize)
3190
+ self.check_interval = get_multiprocessing_timeout_interval()
3191
+ super().__init__(target=worker_target(operations, worker_id), name="MapWorker" + str(worker_id),
3192
+ args=(self.pipe,), daemon=True)
3193
+
3194
+ def execute(self, idx, *args):
3195
+ """Acquiring data from a worker in an infinite loop"""
3196
+ self.pipe.master_send(idx, args)
3197
+ time_s = time.time()
3198
+ wait_count = 1
3199
+ while True:
3200
+ cost_time = time.time() - time_s
3201
+ if cost_time / self.check_interval >= wait_count:
3202
+ wait_count += 1
3203
+ logger.warning("It has been waiting for " + "%.3f" % cost_time + "s because the sub-process "
3204
+ "worker of the map operation is hanging. "
3205
+ "Check whether the user defined data transform is too slow or the "
3206
+ "output data is too large. You can also set the timeout interval by "
3207
+ "ds.config.set_multiprocessing_timeout_interval to adjust the output frequency "
3208
+ "of this log.")
3209
+ pid = self.pid
3210
+ logger.warning("Map worker subprocess ID {} is stuck.".format(pid))
3211
+ install_status, _ = subprocess.getstatusoutput("py-spy --version")
3212
+ if install_status == 0:
3213
+ stack = subprocess.getoutput("py-spy dump -p {} -l".format(pid))
3214
+ logger.warning("Map worker subprocess stack:\n{}".format(stack))
3215
+ else:
3216
+ logger.warning("Please `pip install py-spy` to get the stacks of the stuck process.")
3217
+ try:
3218
+ res = self.pipe.master_receive()
3219
+ # Because there is no need to copy when creating Tensors in the C++layer, it reduces the time
3220
+ # from np.ndarray to C++Tensor creation. However, when using shared memory in multiple processes,
3221
+ # the address of the shared memory will always be passed to subsequent nodes in the dataset pipeline,
3222
+ # and the shared memory will also be written by the current node, causing dirty data to be accessed
3223
+ # by subsequent nodes in the pipeline. So make a memory copy here to solve the problem of
3224
+ # shared memory being contaminated.
3225
+ if get_enable_shared_mem():
3226
+ res = copy.deepcopy(res)
3227
+ except queue.Empty:
3228
+ continue
3229
+ if res is None:
3230
+ # receive finish signal
3231
+ return None
3232
+ if isinstance(res, ExceptionHandler):
3233
+ res.reraise()
3234
+ return res
3235
+
3236
+ def close(self):
3237
+ try:
3238
+ if self.is_alive():
3239
+ # release the eager executor which is used by current process
3240
+ transforms.transforms.clean_unused_executors()
3241
+
3242
+ logger.info(f"Closing worker with PID: {self.pid}")
3243
+ self.pipe.master_close()
3244
+ # del the handle which hold by master
3245
+ del self.pipe.in_queue
3246
+ del self.pipe.res_queue
3247
+ super().terminate()
3248
+ super().join()
3249
+ super().close()
3250
+
3251
+ except ValueError:
3252
+ # Process has been closed already
3253
+ return
3254
+ return
3255
+
3256
+ def is_alive(self):
3257
+ try:
3258
+ return super().is_alive()
3259
+ except ValueError:
3260
+ return False
3261
+
3262
+
3263
+ class _PythonMultiprocessing(cde.PythonMultiprocessingRuntime):
3264
+ """
3265
+ A wrapper to multiprocessing.pool that performs cleanup and ensure proper termination of forked processes.
3266
+ """
3267
+
3268
+ class _ExceptHookHandler:
3269
+ """
3270
+ Internal class ExceptionHandler
3271
+ """
3272
+
3273
+ def __init__(self):
3274
+ self.origin_hook = sys.excepthook
3275
+ sys.excepthook = self.__handler_exception
3276
+
3277
+ @staticmethod
3278
+ def mp_pool_exit_preprocess():
3279
+ if check_iterator_cleanup() is False:
3280
+ # Set the iterator_cleanup flag to True before exiting, and wait 3s for all apply_async
3281
+ # applied to the multiprocessing task to prevent multiprocessing from hang when exiting
3282
+ _set_iterator_cleanup()
3283
+ time.sleep(3)
3284
+
3285
+ def __handler_exception(self, ex_type, value, tb):
3286
+ self.origin_hook(ex_type, value, tb)
3287
+ self.mp_pool_exit_preprocess()
3288
+
3289
+ def __init__(self, op_name, num_parallel_workers, operations, max_rowsize=16):
3290
+ super(_PythonMultiprocessing, self).__init__()
3291
+ self.op_name = op_name
3292
+ self.num_parallel_workers = num_parallel_workers
3293
+ self.operations = operations
3294
+ self.max_rowsize = max_rowsize
3295
+
3296
+ self.workers = None
3297
+ self.pids = None
3298
+ self.op_id = -1
3299
+
3300
+ self.queues_map = {}
3301
+ self.next_queue = 0
3302
+
3303
+ self.eot = None
3304
+ self.watch_dog = None
3305
+ self.ppid = os.getpid()
3306
+ self.hook = None
3307
+ self.warning_ctl = None
3308
+ # cache thread (get_ident()) to worker_id mapping in Python layer
3309
+ self.python_threads_to_workers = {}
3310
+ self.eof = None
3311
+
3312
+ def __del__(self):
3313
+ try:
3314
+ self.terminate()
3315
+ except TypeError:
3316
+ pass
3317
+
3318
+ # This wait function is for cleaning zombie subprocesses
3319
+ @staticmethod
3320
+ def wait_pid():
3321
+ """
3322
+ This function is used by the main process to release subprocess resources.
3323
+ """
3324
+ try:
3325
+ while True:
3326
+ child_pid, _ = os.waitpid(-1, os.WNOHANG)
3327
+ if child_pid == 0:
3328
+ break
3329
+ except OSError:
3330
+ # waitpid may be failed for some reasons so we ignore this error
3331
+ pass
3332
+
3333
+ # Dataset need watch_dog thread to monitoring fork multi-processing,
3334
+ # and thread can't be a member function otherwise python won't collect and release resources.
3335
+ @staticmethod
3336
+ def _watch_dog(eot, workers):
3337
+ """
3338
+ This thread is for monitoring subprocesses forked by GeneratorDataset/map/batch
3339
+ """
3340
+ if not isinstance(workers, list):
3341
+ raise TypeError("[Internal Error] The 2nd parameter of watch dog thread should be list of process, "
3342
+ "but got {}.".format(type(workers)))
3343
+
3344
+ while not eot.is_set():
3345
+ # Monitoring and count how many subprocesses already exit
3346
+ clear_subprocess_timeout = _PythonMultiprocessing._monitor_subprocess_exit(workers)
3347
+ # If find subprocess exit, we will wait for 30s and do some waitpid operations
3348
+ if clear_subprocess_timeout > 0:
3349
+ start = time.time()
3350
+ while time.time() - start < clear_subprocess_timeout:
3351
+ # We need to distinguishing get_dataset_size or train finished normally and hang scenario.
3352
+ # If get_dataset_size or train finished normally, _stop_subprocess can be execute and
3353
+ # self.need_abort can be set to True. If main process is hang in get(), self.need_abort
3354
+ # will never set to True, then we wait for 30s and kill main process
3355
+ if eot.is_set():
3356
+ return
3357
+ # Sometimes subprocess may be zombie, so in 30s we can wait and do some useful tasks(waitpid).
3358
+ _PythonMultiprocessing.wait_pid()
3359
+ # multiprocessing.queue may hang in .get() forever when put() process was killed.
3360
+ # We have to exit main process otherwise main process will hang.
3361
+ _PythonMultiprocessing._terminate_processes(workers)
3362
+ logger.critical("The subprocess of dataset may exit unexpected or be killed, "
3363
+ "main process will exit. If this is not an artificial operation, you can use "
3364
+ "ds.config.set_enable_watchdog(False) to block this error.")
3365
+ os.kill(os.getpid(), signal.SIGTERM)
3366
+
3367
+ # release the workers
3368
+ del workers
3369
+
3370
+ @staticmethod
3371
+ def _terminate_processes(processes):
3372
+ """Terminate subprocesses"""
3373
+
3374
+ for p in processes:
3375
+ try:
3376
+ if p.exitcode is None:
3377
+ p.terminate()
3378
+ except Exception: # pylint: disable=broad-except
3379
+ # process has been closed already
3380
+ pass
3381
+ for p in processes:
3382
+ if p._closed is False: # pylint: disable=W0212
3383
+ # We don't use w.join because join can only used in main process or join will raise an error.
3384
+ p._popen.wait() # pylint: disable=W0212
3385
+
3386
+ # Monitor the exit number of subprocesses
3387
+ @staticmethod
3388
+ def _monitor_subprocess_exit(workers):
3389
+ """
3390
+ To monitor whether process is exit.
3391
+
3392
+ Args:
3393
+ workers (list of multiprocessing.Process): multiprocessing.Process.
3394
+
3395
+ Returns:
3396
+ int, the timeout(in seconds) when process exit.
3397
+ """
3398
+ for w in workers:
3399
+ try:
3400
+ exit_code = w.exitcode
3401
+ if exit_code is not None:
3402
+ # For kill -9, we can exit quickly
3403
+ if exit_code == -9:
3404
+ return 1
3405
+ # For kill -15, we still exit after 30s
3406
+ if exit_code == -15:
3407
+ return 30
3408
+ # In some cases the subprocess has been killed but the exitcode is still None.
3409
+ # So we use os.kill(pid, 0) to check if it is alive.
3410
+ subprocess_alive = _PythonMultiprocessing.is_process_alive(w.pid)
3411
+ if not subprocess_alive:
3412
+ # Like kill -15, we wait 30s before exit
3413
+ return 30
3414
+ except ValueError:
3415
+ # process has been closed already
3416
+ return 0
3417
+ return 0
3418
+
3419
+ @staticmethod
3420
+ def is_process_alive(pid):
3421
+ """
3422
+ Check if the process is alive or not.
3423
+ Note: We hit a deadlock when we use psutil or w.exitcode to check whether a process is alive.
3424
+ Instead we use os.kill(ppid, 0).
3425
+
3426
+ Args:
3427
+ pid: pid of the process to be checked
3428
+
3429
+ Returns:
3430
+ True if the process is alive
3431
+ """
3432
+
3433
+ try:
3434
+ os.kill(pid, 0)
3435
+ except OSError:
3436
+ return False
3437
+ return True
3438
+
3439
+ # When main process exit, subprocesses will be terminate
3440
+ @staticmethod
3441
+ def _clean_process(ppid, workers, quit_signal):
3442
+ """
3443
+ This is the execute function of clean process, if we found main process exited, we will clean subprocesses.
3444
+
3445
+ Args:
3446
+ ppid: The process id of main process.
3447
+ workers: The list of subprocesses.
3448
+ quit_signal: The flag of quit.
3449
+ """
3450
+ signal.signal(signal.SIGINT, signal.SIG_IGN)
3451
+ while _PythonMultiprocessing.is_process_alive(ppid):
3452
+ if quit_signal.is_set():
3453
+ return
3454
+ time.sleep(0.1)
3455
+
3456
+ _PythonMultiprocessing._terminate_processes(workers)
3457
+ del workers
3458
+ os.kill(os.getpid(), signal.SIGTERM)
3459
+
3460
+ def launch(self, op_id=-1):
3461
+ """
3462
+ Launch Python multiprocessing pool.
3463
+
3464
+ Args:
3465
+ pop_id: ID for operation to have Python multiprocessing pool launched
3466
+
3467
+ Returns:
3468
+ Python multiprocssing pool is launched.
3469
+ """
3470
+ self.python_threads_to_workers = {}
3471
+ self.op_id = op_id
3472
+ logger.info("Launching new Python Multiprocessing pool for Op:" + str(self.op_id))
3473
+ if self.is_mp_enabled():
3474
+ message = "Launching a new Python multiprocessing pool while a pool already exists!" + \
3475
+ " The existing pool will be terminated first."
3476
+ logger.warning(message)
3477
+ self.terminate()
3478
+ self.reset()
3479
+ self.create_pool()
3480
+
3481
+ def create_pool(self):
3482
+ """
3483
+
3484
+ Returns:
3485
+
3486
+ """
3487
+ if get_enable_shared_mem():
3488
+ _check_shm_usage(self.num_parallel_workers, 1, self.max_rowsize[0], self.max_rowsize[1])
3489
+
3490
+ if self.workers is not None:
3491
+ raise Exception("Pool was already created, close it first.")
3492
+
3493
+ # Let gc collect unreferenced memory to avoid child processes in the pool to do it
3494
+ gc.collect()
3495
+
3496
+ # Construct python worker processes
3497
+ self.workers = []
3498
+ self.warning_ctl = multiprocessing.Value('i', 0)
3499
+ for worker_id in range(self.num_parallel_workers):
3500
+ worker = _MPWorker(self.operations, self.warning_ctl, self.max_rowsize, worker_id)
3501
+ worker.start()
3502
+ self.workers.append(worker)
3503
+
3504
+ logger.info("Op: " + str(self.op_id) + " Python multiprocessing pool workers' PIDs: " + str(self.get_pids()))
3505
+
3506
+ self.hook = _PythonMultiprocessing._ExceptHookHandler()
3507
+
3508
+ # The op (Map, Batch, etc) multiprocessing will launch a watch dog thread for monitoring sub processes
3509
+ self._launch_watch_dog()
3510
+
3511
+ atexit.register(self.terminate)
3512
+
3513
+ def terminate(self):
3514
+ # close watch dog first and then close all the workers
3515
+ self.abort_watchdog()
3516
+ self.close_all_workers()
3517
+ if hasattr(self, "warning_ctl"):
3518
+ del self.warning_ctl
3519
+
3520
+ def get_pids(self):
3521
+ """
3522
+ Get list of worker's PIDs
3523
+
3524
+ Returns:
3525
+ list of strings
3526
+ """
3527
+ if not self.is_mp_enabled():
3528
+ return []
3529
+ if not self.pids:
3530
+ self.pids = []
3531
+ if self.workers:
3532
+ for w in self.workers:
3533
+ try:
3534
+ self.pids.append(w.pid)
3535
+ except ValueError:
3536
+ continue
3537
+ return self.pids
3538
+
3539
+ def add_new_workers(self, num_new_workers):
3540
+ logger.info(
3541
+ "Increasing num_parallel_workers of Python Multiprocessing pool for Op:" + str(self.op_id) +
3542
+ ", old num_workers=" + str(self.num_parallel_workers) + " new num_workers=" + str(
3543
+ self.num_parallel_workers +
3544
+ num_new_workers) + ".")
3545
+ self.terminate()
3546
+ self.num_parallel_workers += num_new_workers
3547
+ self.launch(self.op_id)
3548
+
3549
+ def remove_workers(self, num_removed_workers):
3550
+ logger.info(
3551
+ "Decreasing num_parallel_workers of Python Multiprocessing pool for Op:" + str(self.op_id) +
3552
+ ", old num_workers=" + str(self.num_parallel_workers) + " new num_workers=" + str(
3553
+ self.num_parallel_workers -
3554
+ num_removed_workers) + ".")
3555
+ self.terminate()
3556
+ self.num_parallel_workers -= num_removed_workers
3557
+ self.launch(self.op_id)
3558
+
3559
+ def is_mp_enabled(self):
3560
+ return self.workers is not None
3561
+
3562
+ def execute(self, idx, *args):
3563
+ """
3564
+ Execute
3565
+ """
3566
+ t_id = threading.get_ident()
3567
+ # get the worker_id from Python layer cache first, get from Cpp layer if not found.
3568
+ worker_id = self.python_threads_to_workers.setdefault(t_id, self.get_thread_to_worker())
3569
+ if worker_id >= len(self.workers):
3570
+ raise RuntimeError("[Internal] worker_id value is greater than number of available workers!")
3571
+
3572
+ # todo check_iterator_cleanup
3573
+ if self.is_running() and check_iterator_cleanup() is False:
3574
+ return self.workers[worker_id].execute(idx, *args)
3575
+
3576
+ return None
3577
+
3578
+ def _launch_watch_dog(self):
3579
+ """
3580
+ We will launch a watchdog thread and a clean process to cleaning subprocess when there is process was killed.
3581
+ The watchdog thread will cleanup subprocesses and main process when one of the subprocesses was killed.
3582
+ The cleaning subprocess will cleanup subprocesses when main process was killed.
3583
+ """
3584
+ if platform.system().lower() != 'windows':
3585
+ self.eof = multiprocessing.Event()
3586
+ self.cleaning_process = multiprocessing.Process(target=self._clean_process,
3587
+ name="MapCleanProcess",
3588
+ args=(self.ppid, self.workers, self.eof),
3589
+ daemon=True)
3590
+ self.cleaning_process.start()
3591
+
3592
+ if get_enable_watchdog():
3593
+ self.eot = threading.Event()
3594
+ self.watch_dog = threading.Thread(target=self._watch_dog,
3595
+ name="MapWatchDog",
3596
+ args=(self.eot, self.workers + [self.cleaning_process]),
3597
+ daemon=True)
3598
+ self.watch_dog.start()
3599
+
3600
+ def _abort_watchdog(self):
3601
+ if not self.eot.is_set():
3602
+ self.eot.set()
3603
+
3604
+ def abort_watchdog(self):
3605
+ if hasattr(self, 'watch_dog') and self.watch_dog is not None and hasattr(self, 'eot') and self.eot is not None:
3606
+ self._abort_watchdog()
3607
+ if hasattr(self, 'cleaning_process') and self.cleaning_process is not None:
3608
+ if hasattr(self, 'eof') and self.eof is not None and not self.eof.is_set():
3609
+ self.eof.set()
3610
+ _PythonMultiprocessing._terminate_processes([self.cleaning_process])
3611
+ del self.cleaning_process
3612
+
3613
+ def is_running(self):
3614
+ if hasattr(self, 'workers') and self.workers is not None:
3615
+ return all([w.is_alive() for w in self.workers])
3616
+ return False
3617
+
3618
+ def close_all_workers(self):
3619
+ """Close all the subprocess workers"""
3620
+ if hasattr(self, 'workers') and self.workers is not None:
3621
+ for w in self.workers:
3622
+ w.close()
3623
+ check_interval = get_multiprocessing_timeout_interval()
3624
+ for w in self.workers:
3625
+ try:
3626
+ subprocess_file_descriptor = w.sentinel
3627
+ st = time.time()
3628
+ while _PythonMultiprocessing.is_process_alive(w.pid):
3629
+ time.sleep(0.01) # sleep 10ms, waiting for the subprocess exit
3630
+ if time.time() - st > check_interval:
3631
+ logger.warning("Waiting for the subprocess worker [{}] to exit.".format(w.pid))
3632
+ st += check_interval
3633
+ except ValueError as e:
3634
+ if "process object is closed" in str(e):
3635
+ continue
3636
+ raise e
3637
+ try:
3638
+ if w.is_alive():
3639
+ os.close(subprocess_file_descriptor)
3640
+ except OSError as e:
3641
+ # Maybe the file descriptor had been released, so ignore the 'Bad file descriptor'
3642
+ if "Bad file descriptor" not in str(e):
3643
+ raise e
3644
+
3645
+ # use clear to release the handle which is better than self.workers = None
3646
+ self.workers.clear()
3647
+ self.workers = None
3648
+ self.pids = None
3649
+
3650
+
3651
+ class MapDataset(UnionBaseDataset):
3652
+ """
3653
+ The result of applying the Map operation to the input Dataset.
3654
+
3655
+ Args:
3656
+ input_dataset (Dataset): Input Dataset to be mapped.
3657
+ operations (Union[list[TensorOperation], list[functions]]): A function mapping a nested structure of tensors
3658
+ to another nested structure of tensor. Default: ``None``.
3659
+ input_columns (Union[str, list[str]]): List of names of the input columns.
3660
+ Default: ``None``, the operations will be applied on the first columns in the dataset.
3661
+ The size of the list should match the number of inputs of the first operation.
3662
+ output_columns (Union[str, list[str]], optional): List of names of the output columns.
3663
+ The size of the list should match the number of outputs of the last operation.
3664
+ Default: ``None``, output columns will be the input columns, i.e., the columns will
3665
+ be replaced.
3666
+ num_parallel_workers (int, optional): Number of workers to process the dataset
3667
+ in parallel. Default: ``None``.
3668
+ python_multiprocessing (bool, optional): Parallelize Python operations with multiple worker process. This
3669
+ option could be beneficial if the Python operation is computational heavy. Default: ``False``.
3670
+ cache (DatasetCache, optional): Use tensor caching service to speed up dataset processing.
3671
+ Default: ``None``, which means no cache is used.
3672
+ callbacks (DSCallback, list[DSCallback], optional): List of Dataset callbacks to be called. Default: ``None``.
3673
+ max_rowsize(Union[int, list[int]], optional): Maximum size of row in MB that is used for shared memory
3674
+ allocation to copy data between processes, the total occupied shared memory will increase as
3675
+ ``num_parallel_workers`` and :func:`mindspore.dataset.config.set_prefetch_size` increase. If set to -1,
3676
+ shared memory will be dynamically allocated with the actual size of data. This is only used if
3677
+ ``python_multiprocessing`` is set to True. If it is an int value, it represents ``input_columns`` and
3678
+ ``output_columns`` use this value as the unit to create shared memory. If it is a list, the first element
3679
+ represents the ``input_columns`` use this value as the unit to create shared memory, and the second element
3680
+ represents ``output_columns`` use this value as the unit to create shared memory. Default: 16.
3681
+ offload (bool, optional): Flag to indicate whether offload is used. Default: ``None``.
3682
+ """
3683
+
3684
+ def __init__(self, input_dataset, operations=None, input_columns=None, output_columns=None,
3685
+ num_parallel_workers=None, python_multiprocessing=False, cache=None, callbacks=None, max_rowsize=16,
3686
+ offload=None):
3687
+ super().__init__(children=input_dataset, num_parallel_workers=num_parallel_workers, cache=cache)
3688
+ self.operations = to_list(operations)
3689
+ for op in self.operations:
3690
+ # user define c_vision.HWC2CHW without parentheses is error
3691
+ if type(op) == type: # pylint: disable=unidiomatic-typecheck
3692
+ raise ValueError("Parameter operations's element of method map should be a dataset processing "
3693
+ "operation instance, but got: {}. It may be missing parentheses for "
3694
+ "instantiation.".format(op))
3695
+ if not isinstance(op, (c_transforms.TensorOperation, py_transforms.PyTensorOperation)) \
3696
+ and not callable(op):
3697
+ raise ValueError("Parameter operations's element of method map should be a python function or "
3698
+ "class method which should be callable, but got: {}. It doesn't need parentheses "
3699
+ "for python function or class method.".format(op))
3700
+
3701
+ self.input_columns = to_list(input_columns)
3702
+ self.output_columns = to_list(output_columns)
3703
+
3704
+ # If output_columns were not provided then use input_columns
3705
+ self.output_columns = self.input_columns if not self.output_columns else self.output_columns
3706
+
3707
+ self.python_multiprocessing = python_multiprocessing
3708
+ self.process_pool = None
3709
+
3710
+ self.callbacks = to_list(callbacks)
3711
+ if isinstance(max_rowsize, int):
3712
+ self.max_rowsize = [max_rowsize] * 2
3713
+ else:
3714
+ self.max_rowsize = max_rowsize
3715
+ self.offload = offload
3716
+
3717
+ def parse(self, children=None):
3718
+ operations = self.__decompose_callable_operations()
3719
+
3720
+ count_old_transforms, count_new_transforms, count_non_data_vision_transforms = \
3721
+ self.__count_transforms(operations)
3722
+ count_pyfunc = self.__count_pyfuncs(operations)
3723
+ if count_new_transforms + count_pyfunc == len(operations):
3724
+ prev_op = None
3725
+ for op in operations:
3726
+ # skip user added DebugHook to avoid changing to Py-implementation.
3727
+ if self.__is_debug_hook_op(op):
3728
+ if prev_op:
3729
+ # manually set previous_op_name
3730
+ prev_op_name = self.__parse_op_name(prev_op)
3731
+ op.set_previous_op_name(prev_op_name)
3732
+ continue
3733
+ if op.implementation is None:
3734
+ if prev_op and prev_op.implementation == Implementation.PY:
3735
+ op.implementation = Implementation.PY
3736
+ else:
3737
+ op.implementation = Implementation.C
3738
+ prev_op = op
3739
+ operations = self.__insert_debug_wrapper(operations)
3740
+ operations = transforms.transforms.Compose.reduce(operations)
3741
+ elif count_old_transforms + count_pyfunc + count_non_data_vision_transforms == len(operations):
3742
+ operations = self.__insert_debug_wrapper(operations)
3743
+ operations = transforms.py_transforms.Compose.reduce(operations)
3744
+ else:
3745
+ raise RuntimeError("Mixing old legacy c/py_transforms and new unified transforms is not allowed.")
3746
+
3747
+ self.operations = self.__process_final_operations(operations)
3748
+ self.prepare_multiprocessing()
3749
+
3750
+ callbacks = [cb.create_runtime_obj() for cb in self.callbacks]
3751
+ return cde.MapNode(children[0], self.operations, self.input_columns, self.output_columns,
3752
+ callbacks, OffloadToManualOffloadMode.get(self.offload), self.process_pool)
3753
+
3754
+ def __deepcopy__(self, memodict):
3755
+ return self.__safe_deepcopy__(memodict, exclude=("operations", "callbacks", "__transfer_dataset__"))
3756
+
3757
+ def __del__(self):
3758
+ if hasattr(self, "process_pool") and self.process_pool is not None:
3759
+ self.process_pool.terminate()
3760
+ del self.process_pool
3761
+
3762
+ @staticmethod
3763
+ def __parse_op_name(op):
3764
+ """
3765
+ Utility method to get operation name.
3766
+ """
3767
+ op_name = ""
3768
+ if isinstance(op, transforms.py_transforms_util.FuncWrapper):
3769
+ try:
3770
+ op_name = op.transform.__name__
3771
+ except (AttributeError,):
3772
+ op_name = op.transform.__class__.__name__
3773
+ else:
3774
+ op_name = op.__class__.__name__
3775
+ return op_name
3776
+
3777
+ @staticmethod
3778
+ def __construct_debug_hook(previous_op_name=None, is_first_op=False):
3779
+ """
3780
+ Wrap debug hook into FuncWrapper.
3781
+ """
3782
+ inserted_functions = []
3783
+ debug_hook_list = _get_debug_hook_list()
3784
+ if debug_hook_list:
3785
+ for fn in debug_hook_list:
3786
+ # making deep copy to allow each debug hook instance hold unique variables
3787
+ new_fn = copy.deepcopy(fn)
3788
+ new_fn.set_previous_op_name(previous_op_name)
3789
+ new_fn.set_is_first(is_first_op)
3790
+ inserted_func = transforms.py_transforms_util.FuncWrapper(new_fn)
3791
+ inserted_func.implementation = Implementation.PY
3792
+ inserted_functions.append(inserted_func)
3793
+ return inserted_functions
3794
+
3795
+ @staticmethod
3796
+ def __is_debug_hook_op(op):
3797
+ """
3798
+ Check if the op is user added DebugHook and skip it to avoid changing transforms implementation.
3799
+ """
3800
+ if isinstance(op, DebugHook):
3801
+ if not get_debug_mode():
3802
+ raise ValueError("It is not allowed to inject DebugHook object in non-debug mode.")
3803
+ return True
3804
+ return False
3805
+
3806
+ @staticmethod
3807
+ def __count_pyfuncs(operations):
3808
+ """
3809
+ Count the number of pyfuncs operations
3810
+ """
3811
+ return sum([1 if isinstance(op, FuncWrapper) else 0 for op in operations])
3812
+
3813
+ @staticmethod
3814
+ def __count_transforms(operations):
3815
+ """
3816
+ Count the various flavors of transforms operations
3817
+ """
3818
+ # Count the number of old legacy data and vision c_transforms and py_transforms
3819
+ count_old_transforms = sum(
3820
+ [1 if "c_transforms" in str(op)
3821
+ or isinstance(op, (c_transforms.TensorOperation, py_transforms.PyTensorOperation))
3822
+ or ("py_transforms" in str(op) and not isinstance(op, FuncWrapper))
3823
+ else 0 for op in operations])
3824
+ # Count the number of new unified data and vision transforms
3825
+ count_new_transforms = sum([1 if hasattr(op, "implementation") and not isinstance(op, FuncWrapper)
3826
+ else 0 for op in operations])
3827
+ # Count the number of non-data transforms and non-vision transforms
3828
+ count_non_data_vision_transforms = sum(
3829
+ [1 if "text.transforms" in str(op) or "audio.transforms" in str(op) else 0 for op in operations])
3830
+ return count_old_transforms, count_new_transforms, count_non_data_vision_transforms
3831
+
3832
+ @staticmethod
3833
+ def __operation_valid_for_multiprocessing(op):
3834
+ if callable(op) and str(op).find("c_transform") < 0:
3835
+ return True
3836
+ return False
3837
+
3838
+ @staticmethod
3839
+ def __process_final_operations(operations):
3840
+ """
3841
+ Build final list of operations
3842
+ """
3843
+ operations_fin = []
3844
+ for op in operations:
3845
+ if hasattr(op, "implementation"):
3846
+ if op.implementation == Implementation.C and not isinstance(op, (FuncWrapper, ToNumpy)):
3847
+ operations_fin.append(op.parse())
3848
+ elif op.implementation == Implementation.PY:
3849
+ operations_fin.append(op)
3850
+ elif isinstance(op, (FuncWrapper, ToNumpy)):
3851
+ operations_fin.append(op)
3852
+ else:
3853
+ raise RuntimeError("Wrong implementation")
3854
+ else:
3855
+ if op and getattr(op, 'parse', None):
3856
+ operations_fin.append(op.parse())
3857
+ else:
3858
+ operations_fin.append(op)
3859
+ return operations_fin
3860
+
3861
+ # Iterator bootstrap will be called on iterator construction.
3862
+ # A deep copy of Dataset object is created prior of iterator_bootstrap.
3863
+ # This method will create per iterator process pool and bind pyfunc execution to the pool.
3864
+ def prepare_multiprocessing(self):
3865
+ """
3866
+ Per iterator bootstrap callback.
3867
+ """
3868
+ if self.python_multiprocessing and platform.system().lower() == 'windows':
3869
+ logger.warning("Python multiprocessing is not supported on Windows platform.")
3870
+ return
3871
+ if self.python_multiprocessing and get_debug_mode():
3872
+ logger.warning("Python multiprocessing is not supported in debug mode."
3873
+ " Ignoring Python multiprocessing for map operation.")
3874
+ return
3875
+ if self.python_multiprocessing:
3876
+ iter_specific_operations = []
3877
+ callable_list = []
3878
+
3879
+ # If user didn't specify num_parallel_workers, set it to default
3880
+ if self.num_parallel_workers is None:
3881
+ self.num_parallel_workers = get_num_parallel_workers()
3882
+
3883
+ # Pass #1, look for Python callables and build list
3884
+ for op in self.operations:
3885
+ # our c transforms is now callable and should not be run in Python multithreading
3886
+ if MapDataset.__operation_valid_for_multiprocessing(op):
3887
+ callable_list.append(op)
3888
+
3889
+ if callable_list:
3890
+ self.process_pool = _PythonMultiprocessing(str(self), self.num_parallel_workers, callable_list,
3891
+ self.max_rowsize)
3892
+ # Pass #2
3893
+ idx = 0
3894
+ for op in self.operations:
3895
+ # our c transforms is now callable and should not be run in Python multithreading
3896
+ if MapDataset.__operation_valid_for_multiprocessing(op):
3897
+ # Wrap Python callable into _PythonCallable
3898
+ iter_specific_operations.append(_PythonCallable(op, idx, self.process_pool))
3899
+ idx += 1
3900
+ else:
3901
+ # CPP ops remain the same
3902
+ iter_specific_operations.append(op)
3903
+ self.operations = iter_specific_operations
3904
+
3905
+ def __insert_debug_wrapper(self, operations):
3906
+ """
3907
+ Insert DebuggerWrapper before and after each op if debug mode is on.
3908
+ """
3909
+ if not get_debug_mode():
3910
+ return operations
3911
+ first_op_name = self.__parse_op_name(operations[0])
3912
+ inserted_operations = self.__construct_debug_hook(first_op_name, is_first_op=True)
3913
+ for op in operations:
3914
+ inserted_operations.append(op)
3915
+ op_name = self.__parse_op_name(op)
3916
+ inserted_operations.extend(self.__construct_debug_hook(op_name))
3917
+ return inserted_operations
3918
+
3919
+ def __decompose_callable_operations(self):
3920
+ """
3921
+ Decompose operations and build list of old legacy ops which are callable
3922
+ """
3923
+ decomposed_operations = transforms.transforms.Compose.decompose(self.operations)
3924
+ operations = []
3925
+ for op in decomposed_operations:
3926
+ if callable(op) and not hasattr(op, "implementation") and str(op).find(
3927
+ "c_transform") < 0 and not isinstance(op, c_transforms.TensorOperation) and \
3928
+ not isinstance(op, py_transforms.PyTensorOperation):
3929
+ op = transforms.py_transforms_util.FuncWrapper(op)
3930
+ operations.append(op)
3931
+ return operations
3932
+
3933
+
3934
+ class FilterDataset(UnionBaseDataset):
3935
+ """
3936
+ The result of applying filter predicate to the input Dataset.
3937
+
3938
+ Args:
3939
+ input_dataset (Dataset): Input Dataset to be mapped.
3940
+ predicate (callable): Python callable which returns a boolean value. If False then filter the element.
3941
+ input_columns (Union[str, list[str]], optional): List of names of the input columns.
3942
+ Default: ``None``, the predicate will be applied to all columns in the dataset.
3943
+ num_parallel_workers (int, optional): Number of workers to process the dataset
3944
+ in parallel. Default: ``None``.
3945
+ """
3946
+
3947
+ def __init__(self, input_dataset, predicate, input_columns=None, num_parallel_workers=None):
3948
+ super().__init__(children=input_dataset, num_parallel_workers=num_parallel_workers)
3949
+ self.predicate = lambda *args: bool(predicate(*args))
3950
+ self.input_columns = to_list(input_columns)
3951
+
3952
+ def parse(self, children=None):
3953
+ return cde.FilterNode(children[0], self.predicate, self.input_columns)
3954
+
3955
+
3956
+ class RepeatDataset(UnionBaseDataset):
3957
+ """
3958
+ The result of applying Repeat operation to the input Dataset.
3959
+
3960
+ Args:
3961
+ input_dataset (Dataset): Input Dataset to be repeated.
3962
+ count (int): Number of times the dataset will be repeated. Default: -1, repeat indefinitely.
3963
+ """
3964
+
3965
+ def __init__(self, input_dataset, count):
3966
+ super().__init__(children=input_dataset)
3967
+ self.count = replace_none(count, -1)
3968
+
3969
+ def parse(self, children=None):
3970
+ return cde.RepeatNode(children[0], self.count)
3971
+
3972
+
3973
+ class SkipDataset(UnionBaseDataset):
3974
+ """
3975
+ The result of applying Skip operation to the input Dataset.
3976
+
3977
+ Args:
3978
+ input_dataset (Dataset): Input dataset to have elements skipped.
3979
+ count (int): Number of elements to be skipped in the dataset.
3980
+ """
3981
+
3982
+ def __init__(self, input_dataset, count):
3983
+ super().__init__(input_dataset)
3984
+ self.count = count
3985
+
3986
+ def parse(self, children=None):
3987
+ return cde.SkipNode(children[0], self.count)
3988
+
3989
+
3990
+ class TakeDataset(UnionBaseDataset):
3991
+ """
3992
+ The result of applying Take operation to the input Dataset.
3993
+
3994
+ Args:
3995
+ input_dataset (Dataset): Input Dataset to have elements taken from.
3996
+ count (int): Number of elements to be taken from the dataset.
3997
+ """
3998
+
3999
+ def __init__(self, input_dataset, count):
4000
+ super().__init__(children=input_dataset)
4001
+ self.count = count
4002
+
4003
+ def parse(self, children=None):
4004
+ return cde.TakeNode(children[0], self.count)
4005
+
4006
+
4007
+ class ZipDataset(UnionBaseDataset):
4008
+ """
4009
+ The result of applying Zip operation to the input Dataset.
4010
+
4011
+ Args:
4012
+ datasets (tuple): A tuple of datasets to be zipped together.
4013
+
4014
+ Raises:
4015
+ TypeError: If dataset is not an instance of Dataset.
4016
+ """
4017
+
4018
+ def __init__(self, datasets):
4019
+ super().__init__(children=datasets)
4020
+
4021
+ def parse(self, children=None):
4022
+ return cde.ZipNode(children)
4023
+
4024
+ def is_sync(self):
4025
+ return any([c.is_sync() for c in self.children])
4026
+
4027
+
4028
+ class ConcatDataset(UnionBaseDataset):
4029
+ """
4030
+ The result of applying Concat operation to the input Dataset.
4031
+
4032
+ Args:
4033
+ datasets (list): A list of datasets to be concatenated together.
4034
+
4035
+ Raises:
4036
+ TypeError: If dataset is not an instance of Dataset.
4037
+ ValueError: If there is no samples in the one of the datasets.
4038
+ """
4039
+
4040
+ def __init__(self, datasets):
4041
+ super().__init__(children=datasets)
4042
+ for dataset in datasets:
4043
+ if not isinstance(dataset, Dataset):
4044
+ raise TypeError("Invalid dataset, expected Dataset object, but got %s!" % type(dataset))
4045
+ self.datasets = datasets
4046
+ self._sampler = samplers.SequentialSampler(num_samples=None)
4047
+
4048
+ self.children_sizes_ = [c.get_dataset_size() for c in self.children]
4049
+ child_index = 0
4050
+ for item in self.children_sizes_:
4051
+ if item == 0:
4052
+ raise ValueError("There are no samples in the dataset number %d. Please make sure there are "
4053
+ "valid samples in the dataset." % child_index)
4054
+ child_index += 1
4055
+
4056
+ self._children_sizes = self.children_sizes_.copy()
4057
+
4058
+ # _children_flag_and_nums: A list of pair<int ,int>.The first element of pair is flag that characterizes
4059
+ # whether the dataset is mappable. The second element of pair is length of the dataset
4060
+ self._children_flag_and_nums = []
4061
+
4062
+ # _children_start_end_index_: A list of pair<int ,int>.The elements of pair are used to characterize
4063
+ # the valid position of the dataset corresponding to the subscript when sampling
4064
+ self._children_start_end_index_ = []
4065
+ for index, child in enumerate(self.children):
4066
+ tem_list = [-1, -1]
4067
+ self._children_start_end_index_.append(tem_list)
4068
+ dataset_len = self.children_sizes_[index]
4069
+
4070
+ from mindspore.dataset.engine.datasets_user_defined import GeneratorDataset
4071
+ if isinstance(child, GeneratorDataset) and not hasattr(child.source, "__getitem__"):
4072
+ dataset_len = 0
4073
+ self.children_sizes_[index] = 0
4074
+
4075
+ if isinstance(child, MappableDataset):
4076
+ self._children_flag_and_nums.append((0, dataset_len))
4077
+ else:
4078
+ self._children_flag_and_nums.append((1, dataset_len))
4079
+
4080
+ def parse(self, children=None):
4081
+ return cde.ConcatNode(children, self._sampler, self._children_flag_and_nums, self._children_start_end_index_,
4082
+ self._children_sizes)
4083
+
4084
+ def use_sampler(self, sampler):
4085
+ """
4086
+ Set the distributedSampler to concat dataset
4087
+
4088
+ Args:
4089
+ sampler (Sampler): The sampler to use for the current dataset.
4090
+ Currently supported: DistributedSampler.
4091
+
4092
+ Raises:
4093
+ TypeError: If the sampler is not an instance of DistributedSampler
4094
+ ValueError: If the parameter shuffle of sampler is True
4095
+ ValueError: If the parameter NumSamples of sampler is not None.
4096
+ ValueError: If num_shards <=0.
4097
+ """
4098
+ if not isinstance(sampler, (samplers.DistributedSampler, samplers.RandomSampler)):
4099
+ raise TypeError("The parameter %s of concat must be DistributedSampler or RandomSampler!" % sampler)
4100
+
4101
+ if isinstance(sampler, samplers.RandomSampler):
4102
+ if sampler.replacement:
4103
+ raise ValueError("The parameter replacement of RandomSampler must be False!")
4104
+
4105
+ if sampler.get_num_samples() is not None:
4106
+ raise ValueError("The parameter num_samples of RandomSampler is not support to be set!")
4107
+
4108
+ self._sampler = sampler
4109
+ self._children_sizes = [c.get_dataset_size() for c in self.children]
4110
+
4111
+ # Recursive access to other child concat nodes
4112
+ def set_child(node):
4113
+ for c in node.children:
4114
+ if isinstance(c, ConcatDataset):
4115
+ c.use_sampler(sampler)
4116
+ set_child(c)
4117
+ set_child(self)
4118
+
4119
+ return
4120
+
4121
+ if sampler.is_shuffled():
4122
+ raise ValueError("The parameter shuffle of DistributedSampler must be False!")
4123
+
4124
+ if sampler.num_shards <= 0:
4125
+ raise ValueError("The parameter num_shards of DistributedSampler must be positive int!")
4126
+
4127
+ if sampler.get_num_samples() is not None:
4128
+ raise ValueError("The parameter num_samples of DistributedSampler is not support to be set!")
4129
+
4130
+ self.dataset_size = None
4131
+
4132
+ self._sampler = sampler
4133
+ cumulative_samples_nums = 0
4134
+ for index, child in enumerate(self.children):
4135
+ if hasattr(child, 'sampler') and child.sampler.get_num_samples() is not None:
4136
+ raise ValueError("The parameter NumSamples of %s is not support to be set!" % child)
4137
+
4138
+ if isinstance(child, (BatchDataset, PaddedBatchDataset)):
4139
+ raise TypeError("The parameter %s of concat must not be BatchDataset or PaddedBatchDataset!" % child)
4140
+
4141
+ # if child is mappable and the length is greater than 0
4142
+ if not self._children_flag_and_nums[index][0] and self._children_flag_and_nums[index][1]:
4143
+
4144
+ tem_value = cumulative_samples_nums + self._children_flag_and_nums[index][1]
4145
+
4146
+ if not self._children_flag_and_nums[index][1] >= sampler.num_shards:
4147
+ if tem_value < sampler.num_shards:
4148
+ self._children_start_end_index_[index][0] = cumulative_samples_nums
4149
+ self._children_start_end_index_[index][1] = tem_value
4150
+ else:
4151
+ self._children_start_end_index_[index][0] = cumulative_samples_nums
4152
+ self._children_start_end_index_[index][1] = tem_value % sampler.num_shards
4153
+
4154
+ tem_sampler = copy.deepcopy(sampler)
4155
+ tem_sampler.set_offset(cumulative_samples_nums)
4156
+ child.use_sampler(tem_sampler)
4157
+
4158
+ cumulative_samples_nums += self.children_sizes_[index]
4159
+ cumulative_samples_nums %= sampler.num_shards
4160
+
4161
+
4162
+ class RenameDataset(UnionBaseDataset):
4163
+ """
4164
+ The result of applying Rename operation to the input Dataset.
4165
+
4166
+ Args:
4167
+ input_dataset (Dataset): Input Dataset to be Renamed.
4168
+ input_columns (Union[str, list[str]]): List of names of the input columns.
4169
+ output_columns (Union[str, list[str]]): List of names of the output columns.
4170
+ """
4171
+
4172
+ def __init__(self, input_dataset, input_columns, output_columns):
4173
+ super().__init__(children=input_dataset)
4174
+ self.input_column_names = to_list(input_columns)
4175
+ self.output_column_names = to_list(output_columns)
4176
+
4177
+ def parse(self, children=None):
4178
+ return cde.RenameNode(children[0], self.input_column_names, self.output_column_names)
4179
+
4180
+
4181
+ def to_list(items):
4182
+ if items is None:
4183
+ return []
4184
+ if isinstance(items, tuple):
4185
+ return list(items)
4186
+ if not isinstance(items, list):
4187
+ return [items]
4188
+ return items
4189
+
4190
+
4191
+ class ProjectDataset(UnionBaseDataset):
4192
+ """
4193
+ The result of applying Project operation to the input Dataset.
4194
+
4195
+ Args:
4196
+ input_dataset (Dataset): Input Dataset to be Projected.
4197
+ columns (Union[str, list[str]]): List of names of the columns to project.
4198
+ """
4199
+
4200
+ def __init__(self, input_dataset, columns):
4201
+ super().__init__(children=input_dataset)
4202
+ self.columns = to_list(columns)
4203
+
4204
+ def parse(self, children=None):
4205
+ return cde.ProjectNode(children[0], self.columns)
4206
+
4207
+
4208
+ class _ToDevice:
4209
+ """
4210
+ Internal class to handle sending data to device.
4211
+ """
4212
+
4213
+ def __init__(self, dataset, num_epochs):
4214
+ if get_debug_mode():
4215
+ logger.error("MindData debugger cannot be used in dataset sink mode. Please manually turn off "
4216
+ "sink mode and try debugger again.")
4217
+ ir_tree, self.api_tree = dataset.create_ir_tree()
4218
+
4219
+ self._runtime_context = cde.PythonRuntimeContext()
4220
+ self._runtime_context.Init()
4221
+ self._to_device = cde.ToDevice(num_epochs)
4222
+ if dataset.get_init_step() != 0:
4223
+ init_step = dataset.get_init_step()
4224
+ dataset_size = dataset.get_dataset_size()
4225
+ self._to_device.Init(ir_tree, init_step, dataset_size)
4226
+ else:
4227
+ self._to_device.Init(ir_tree, 0, -1)
4228
+ self._runtime_context.AssignConsumer(self._to_device)
4229
+
4230
+ ITERATORS_LIST.append(weakref.ref(self))
4231
+ _unset_iterator_cleanup()
4232
+
4233
+ def send(self):
4234
+ self._to_device.Send()
4235
+
4236
+ def stop_send(self):
4237
+ """
4238
+ send stop send signal to pipeline, it is used when end of sequence is sent at the epoch end.
4239
+ """
4240
+ self._to_device.StopSend()
4241
+
4242
+ def continue_send(self):
4243
+ """
4244
+ send continue send signal to pipeline, it is used when end of sequence is sent at the epoch end.
4245
+ """
4246
+ self._to_device.ContinueSend()
4247
+
4248
+ def get_data_info(self):
4249
+ """
4250
+ Get type and shape of current batch.
4251
+ """
4252
+ return self._to_device.GetDataInfo()
4253
+
4254
+ def get_mbuf_queue_size(self):
4255
+ """
4256
+ Get element numbers inside mbuf.
4257
+ """
4258
+ return self._to_device.GetMbufQueueSize()
4259
+
4260
+ def get_send_info(self):
4261
+ """
4262
+ In sink mode, it returns the send information of dataset at this moment.
4263
+ Send information includes number of send batches, time summary of fetching data on host
4264
+ and time summary of sending data.
4265
+ """
4266
+ return self._to_device.GetSendInfo()
4267
+
4268
+ def release(self):
4269
+ """
4270
+ Manually terminate Device Queue instead of relying on out of scope destruction.
4271
+ """
4272
+ if hasattr(self, '_runtime_context') and self._runtime_context:
4273
+ if hasattr(self, '_to_device') and self._to_device:
4274
+ self._runtime_context.Terminate()
4275
+ del self._to_device
4276
+ del self._runtime_context
4277
+
4278
+ def __deepcopy__(self, memodict):
4279
+ return self
4280
+
4281
+ def get_offload_model(self, col_names):
4282
+ """
4283
+ Get offload model containing removed offload ops from pipeline.
4284
+ """
4285
+ offload_model = GetOffloadModel(self._to_device, col_names)
4286
+ return offload_model
4287
+
4288
+ def _reset(self, step, dataset_size):
4289
+ self._to_device.Reset(step, dataset_size)
4290
+
4291
+
4292
+ class TransferDataset(Dataset):
4293
+ """
4294
+ The result of applying TDT operation to the input Dataset.
4295
+
4296
+ Args:
4297
+ input_dataset (Dataset): Input Dataset to be transferred.
4298
+ send_epoch_end (bool, optional): Whether to send end of sequence to device or not. Default: ``True``.
4299
+ create_data_info_queue (bool, optional): Whether to create queue which stores
4300
+ types and shapes of data or not. Default: ``False``.
4301
+
4302
+ Raises:
4303
+ TypeError: If device_type is empty.
4304
+ ValueError: If device_type is not 'Ascend', 'GPU' or 'CPU'.
4305
+ RuntimeError: If dataset is unknown.
4306
+ """
4307
+
4308
+ def __init__(self, input_dataset, send_epoch_end=True, create_data_info_queue=False, queue_name=""):
4309
+ super().__init__(children=input_dataset)
4310
+ if queue_name == "":
4311
+ self.queue_name = str(uuid.uuid1())
4312
+ logger.info(f"queue_name is newly generated. value is {self.queue_name}")
4313
+ else:
4314
+ self.queue_name = queue_name
4315
+ logger.info(f"queue_name is read from compile cache. value is {self.queue_name}")
4316
+ self.device_type = context.get_context("device_target") if context else "CPU"
4317
+ self.device_id = context.get_context("device_id") if context else 0
4318
+
4319
+ self._send_epoch_end = replace_none(send_epoch_end, True)
4320
+ self._create_data_info_queue = create_data_info_queue
4321
+ self._to_device = None
4322
+ self.column_name = input_dataset.get_col_names()
4323
+
4324
+ def parse(self, children=None):
4325
+ total_batch = 0
4326
+ if hasattr(self.children[0], "__total_batch__"):
4327
+ total_batch = self.children[0].__total_batch__
4328
+ check_total_batch(total_batch)
4329
+ return cde.DataQueueNode(children[0], self.queue_name, self.device_type, self.device_id, self._send_epoch_end,
4330
+ total_batch, self._create_data_info_queue)
4331
+
4332
+ def create_dict_iterator(self, num_epochs=-1, output_numpy=False):
4333
+ raise RuntimeError("TransferDataset is not iterable.")
4334
+
4335
+ def create_tuple_iterator(self, columns=None, num_epochs=-1, output_numpy=False, do_copy=True):
4336
+ raise RuntimeError("TransferDataset is not iterable.")
4337
+
4338
+ def __iter__(self):
4339
+ raise RuntimeError("TransferDataset is not iterable.")
4340
+
4341
+ def output_shapes(self):
4342
+ raise RuntimeError("TransferDataset does not support obtaining output_shapes.")
4343
+
4344
+ def output_types(self):
4345
+ raise RuntimeError("TransferDataset does not support obtaining output_types.")
4346
+
4347
+ @check_to_device_send
4348
+ def send(self, num_epochs=-1):
4349
+ """
4350
+ Send to device
4351
+ """
4352
+ if Dataset._noop_mode():
4353
+ return
4354
+ if self._to_device is not None:
4355
+ del self._to_device
4356
+ self._to_device = _ToDevice(self, num_epochs)
4357
+ self._to_device.send()
4358
+
4359
+ def stop_send(self):
4360
+ if self._to_device is not None:
4361
+ self._to_device.stop_send()
4362
+
4363
+ def continue_send(self):
4364
+ if self._to_device is not None:
4365
+ self._to_device.continue_send()
4366
+
4367
+ def get_data_info(self):
4368
+ """
4369
+ Get type and shape of current batch
4370
+ """
4371
+ if self._to_device is not None:
4372
+ return self._to_device.get_data_info()
4373
+ raise RuntimeError("Calling get_data_info with bad state.")
4374
+
4375
+ def get_mbuf_queue_size(self):
4376
+ """
4377
+ Get element numbers inside mbuf.
4378
+ """
4379
+ if self._to_device is not None:
4380
+ return self._to_device.get_mbuf_queue_size()
4381
+ raise RuntimeError("Device queue is not init, call get_mbuf_queue_size failed.")
4382
+
4383
+ def get_send_info(self):
4384
+ """
4385
+ In sink mode, it returns the send information of dataset at this moment.
4386
+ Send information includes number of send batches, time summary of fetching data on host
4387
+ and time summary of sending data.
4388
+ """
4389
+ if self._to_device is not None:
4390
+ return self._to_device.get_send_info()
4391
+ raise RuntimeError("Calling get_send_info with bad state, data queue is not initialized.")
4392
+
4393
+ def get_offload_model(self):
4394
+ if self._to_device is not None:
4395
+ return self._to_device.get_offload_model(self.column_name)
4396
+
4397
+ raise RuntimeError("get_offload_model, _to_device is None")
4398
+
4399
+ def release(self):
4400
+ """
4401
+ Manually terminate Device Queue instead of relying on out of scope destruction.
4402
+ """
4403
+ if self._to_device is not None:
4404
+ self._to_device.release()
4405
+
4406
+ def _reset(self, step, dataset_size):
4407
+ if self._to_device is not None:
4408
+ logger.info("Reset the dataset pipeline to step: " + str(step) + ", epoch: " + str(step // dataset_size))
4409
+ self._to_device._reset(step, dataset_size) # pylint: disable=protected-access
4410
+
4411
+
4412
+ class Schema:
4413
+ """
4414
+ Class to represent a schema of a dataset.
4415
+
4416
+ Args:
4417
+ schema_file (str): Path of the schema file. Default: ``None``.
4418
+
4419
+ Raises:
4420
+ RuntimeError: If schema file failed to load.
4421
+
4422
+ Examples:
4423
+ >>> import mindspore.dataset as ds
4424
+ >>> from mindspore import dtype as mstype
4425
+ >>>
4426
+ >>> # Create schema; specify column name, mindspore.dtype and shape of the column
4427
+ >>> schema = ds.Schema()
4428
+ >>> schema.add_column(name='col1', de_type=mstype.int64, shape=[2])
4429
+ """
4430
+
4431
+ @check_schema
4432
+ def __init__(self, schema_file=None):
4433
+ self.schema_file = replace_none(schema_file, "")
4434
+ self.cpp_schema = cde.SchemaObj(self.schema_file)
4435
+
4436
+ @check_add_column
4437
+ def add_column(self, name, de_type, shape=None):
4438
+ """
4439
+ Add new column to the schema.
4440
+
4441
+ Args:
4442
+ name (str): The new name of the column.
4443
+ de_type (str): Data type of the column.
4444
+ shape (list[int], optional): Shape of the column.
4445
+ Default: ``None``, [-1] which is an unknown shape of rank 1.
4446
+
4447
+ Raises:
4448
+ ValueError: If column type is unknown.
4449
+
4450
+ Examples:
4451
+ >>> import mindspore.dataset as ds
4452
+ >>> from mindspore import dtype as mstype
4453
+ >>>
4454
+ >>> schema = ds.Schema()
4455
+ >>> schema.add_column('col_1d', de_type=mstype.int64, shape=[2])
4456
+ """
4457
+ if isinstance(de_type, typing.Type):
4458
+ de_type = mstype_to_detype(de_type)
4459
+ col_type = str(de_type)
4460
+ else:
4461
+ col_type = str(cde.DataType(de_type))
4462
+ if shape is None:
4463
+ self.cpp_schema.add_column(name, col_type)
4464
+ else:
4465
+ self.cpp_schema.add_column(name, col_type, shape)
4466
+
4467
+ def parse_columns(self, columns):
4468
+ """
4469
+ Parse the columns and add it to self.
4470
+
4471
+ Args:
4472
+ columns (Union[dict, list[dict], tuple[dict]]): Dataset attribute information, decoded from schema file.
4473
+
4474
+ - list[dict], `name` and `type` must be in keys, `shape` optional.
4475
+
4476
+ - dict, columns.keys() as name, columns.values() is dict, and `type` inside, `shape` optional.
4477
+
4478
+ Raises:
4479
+ RuntimeError: If failed to parse columns.
4480
+ RuntimeError: If column's name field is missing.
4481
+ RuntimeError: If column's type field is missing.
4482
+
4483
+ Examples:
4484
+ >>> from mindspore.dataset import Schema
4485
+ >>> schema = Schema()
4486
+ >>> columns1 = [{'name': 'image', 'type': 'int8', 'shape': [3, 3]},
4487
+ ... {'name': 'label', 'type': 'int8', 'shape': [1]}]
4488
+ >>> schema.parse_columns(columns1)
4489
+ >>> columns2 = {'image': {'shape': [3, 3], 'type': 'int8'}, 'label': {'shape': [1], 'type': 'int8'}}
4490
+ >>> schema.parse_columns(columns2)
4491
+ """
4492
+ self.cpp_schema.parse_columns(json.dumps(columns, indent=2))
4493
+
4494
+ def to_json(self):
4495
+ """
4496
+ Get a JSON string of the schema.
4497
+
4498
+ Returns:
4499
+ str, JSON string of the schema.
4500
+
4501
+ Examples:
4502
+ >>> from mindspore.dataset import Schema
4503
+ >>> from mindspore import dtype as mstype
4504
+ >>>
4505
+ >>> schema = Schema()
4506
+ >>> schema.add_column('col_1d', de_type=mstype.int64, shape=[2])
4507
+ >>> json = schema.to_json()
4508
+ """
4509
+ return self.cpp_schema.to_json()
4510
+
4511
+ def from_json(self, json_obj):
4512
+ """
4513
+ Get schema file from JSON object.
4514
+
4515
+ Args:
4516
+ json_obj(dictionary): Object of JSON parsed.
4517
+
4518
+ Raises:
4519
+ RuntimeError: if there is unknown item in the object.
4520
+ RuntimeError: if dataset type is missing in the object.
4521
+ RuntimeError: if columns are missing in the object.
4522
+
4523
+ Examples:
4524
+ >>> import json
4525
+ >>> from mindspore.dataset import Schema
4526
+ >>>
4527
+ >>> with open("/path/to/schema_file", "r") as file:
4528
+ ... json_obj = json.load(file)
4529
+ ... schema = Schema()
4530
+ ... schema.from_json(json_obj)
4531
+ """
4532
+ self.cpp_schema.from_string(json.dumps(json_obj, indent=2))
4533
+
4534
+ def __str__(self):
4535
+ return self.to_json()
4536
+
4537
+ @staticmethod
4538
+ def get_num_rows(schema):
4539
+ schema_obj = schema
4540
+ if not isinstance(schema_obj, Schema):
4541
+ schema_obj = Schema(schema_obj)
4542
+ return schema_obj.cpp_schema.get_num_rows()
4543
+
4544
+
4545
+ class DeserializedDataset(Dataset):
4546
+ def __init__(self, input_obj):
4547
+ super().__init__()
4548
+ self.input_obj = input_obj
4549
+
4550
+ def parse(self, children=None):
4551
+ if isinstance(self.input_obj, dict):
4552
+ json_str = json.dumps(self.input_obj)
4553
+ return cde.Dataset.from_json_string(json_str)
4554
+ return cde.Dataset.from_json_file(self.input_obj)