mindspore 1.10.0__cp37-cp37m-win_amd64.whl → 2.0.0rc1__cp37-cp37m-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of mindspore might be problematic. Click here for more details.
- mindspore/.commit_id +1 -1
- mindspore/ConcurrencyCheck.dll +0 -0
- mindspore/CppBuildInsights.dll +0 -0
- mindspore/CppCoreCheck.dll +0 -0
- mindspore/EnumIndex.dll +0 -0
- mindspore/EspXEngine.dll +0 -0
- mindspore/HResultCheck.dll +0 -0
- mindspore/KernelTraceControl.dll +0 -0
- mindspore/LocalESPC.dll +0 -0
- mindspore/Microsoft.Diagnostics.Tracing.EventSource.dll +0 -0
- mindspore/Microsoft.VisualStudio.RemoteControl.dll +0 -0
- mindspore/Microsoft.VisualStudio.Telemetry.dll +0 -0
- mindspore/Microsoft.VisualStudio.Utilities.Internal.dll +0 -0
- mindspore/Newtonsoft.Json.dll +0 -0
- mindspore/System.Runtime.CompilerServices.Unsafe.dll +0 -0
- mindspore/VariantClear.dll +0 -0
- mindspore/__init__.py +9 -4
- mindspore/_c_dataengine.cp37-win_amd64.pyd +0 -0
- mindspore/_c_expression.cp37-win_amd64.pyd +0 -0
- mindspore/_c_mindrecord.cp37-win_amd64.pyd +0 -0
- mindspore/_check_jit_forbidden_api.py +102 -0
- mindspore/_checkparam.py +1066 -1001
- mindspore/_extends/builtin_operations.py +32 -4
- mindspore/_extends/graph_kernel/model/graph_split.py +66 -222
- mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +12 -9
- mindspore/_extends/parallel_compile/akg_compiler/build_tbe_kernel.py +119 -26
- mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +50 -50
- mindspore/_extends/parallel_compile/akg_compiler/util.py +9 -6
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py +4 -25
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py +9 -4
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_job_manager.py +1 -27
- mindspore/_extends/parse/__init__.py +5 -3
- mindspore/_extends/parse/namespace.py +17 -2
- mindspore/_extends/parse/parser.py +193 -34
- mindspore/_extends/parse/resources.py +7 -8
- mindspore/_extends/parse/standard_method.py +1780 -435
- mindspore/_extends/parse/trope.py +3 -1
- mindspore/amp.py +53 -58
- mindspore/atlprov.dll +0 -0
- mindspore/boost/adasum.py +3 -2
- mindspore/boost/boost.py +2 -2
- mindspore/boost/boost_cell_wrapper.py +46 -26
- mindspore/boost/dim_reduce.py +6 -5
- mindspore/boost/grad_accumulation.py +2 -1
- mindspore/boost/group_loss_scale_manager.py +1 -1
- mindspore/c1.dll +0 -0
- mindspore/c1xx.dll +0 -0
- mindspore/c2.dll +0 -0
- mindspore/cfgpersist.dll +0 -0
- mindspore/clang_rt.asan_dbg_dynamic-x86_64.dll +0 -0
- mindspore/clang_rt.asan_dynamic-x86_64.dll +0 -0
- mindspore/common/__init__.py +11 -10
- mindspore/common/_decorator.py +2 -0
- mindspore/common/_register_for_adapter.py +55 -0
- mindspore/common/_stub_tensor.py +201 -0
- mindspore/common/_utils.py +57 -0
- mindspore/common/api.py +582 -297
- mindspore/common/dtype.py +66 -18
- mindspore/common/dump.py +2 -2
- mindspore/common/initializer.py +38 -1
- mindspore/common/jit_config.py +25 -13
- mindspore/common/mutable.py +53 -24
- mindspore/common/parameter.py +60 -37
- mindspore/common/seed.py +8 -24
- mindspore/common/sparse_tensor.py +927 -0
- mindspore/common/tensor.py +1627 -3900
- mindspore/communication/__init__.py +10 -5
- mindspore/communication/_comm_helper.py +78 -214
- mindspore/communication/_hccl_management.py +2 -1
- mindspore/communication/management.py +136 -47
- mindspore/config/op_info.config +501 -1008
- mindspore/context.py +291 -56
- mindspore/d3dcompiler_47.dll +0 -0
- mindspore/dataset/__init__.py +12 -8
- mindspore/dataset/audio/__init__.py +9 -9
- mindspore/dataset/audio/transforms.py +1090 -228
- mindspore/dataset/audio/utils.py +87 -39
- mindspore/dataset/audio/validators.py +223 -1
- mindspore/dataset/callback/ds_callback.py +17 -15
- mindspore/dataset/core/config.py +246 -17
- mindspore/dataset/core/py_util_helpers.py +4 -3
- mindspore/dataset/core/validator_helpers.py +10 -10
- mindspore/{parallel/nn/layers.py → dataset/debug/__init__.py} +7 -8
- mindspore/dataset/debug/debug_hook.py +65 -0
- mindspore/dataset/debug/pre_defined_hook.py +67 -0
- mindspore/dataset/engine/__init__.py +7 -3
- mindspore/dataset/engine/cache_client.py +9 -9
- mindspore/dataset/engine/datasets.py +648 -477
- mindspore/dataset/engine/datasets_audio.py +165 -167
- mindspore/dataset/engine/datasets_standard_format.py +93 -67
- mindspore/dataset/engine/datasets_text.py +492 -342
- mindspore/dataset/engine/datasets_user_defined.py +85 -50
- mindspore/dataset/engine/datasets_vision.py +1224 -699
- mindspore/dataset/engine/graphdata.py +134 -69
- mindspore/dataset/engine/iterators.py +50 -9
- mindspore/dataset/engine/offload.py +52 -31
- mindspore/dataset/engine/samplers.py +27 -24
- mindspore/dataset/engine/serializer_deserializer.py +14 -15
- mindspore/dataset/engine/validators.py +213 -52
- mindspore/dataset/text/__init__.py +10 -8
- mindspore/dataset/text/transforms.py +152 -57
- mindspore/dataset/text/utils.py +98 -49
- mindspore/dataset/text/validators.py +25 -0
- mindspore/dataset/transforms/__init__.py +4 -2
- mindspore/dataset/transforms/c_transforms.py +11 -13
- mindspore/dataset/transforms/py_transforms.py +2 -2
- mindspore/dataset/transforms/py_transforms_util.py +10 -0
- mindspore/dataset/transforms/transforms.py +13 -15
- mindspore/dataset/transforms/validators.py +7 -7
- mindspore/dataset/utils/__init__.py +2 -1
- mindspore/dataset/utils/browse_dataset.py +13 -13
- mindspore/dataset/utils/line_reader.py +121 -0
- mindspore/dataset/vision/__init__.py +8 -7
- mindspore/dataset/vision/c_transforms.py +125 -126
- mindspore/dataset/vision/py_transforms.py +37 -37
- mindspore/dataset/vision/py_transforms_util.py +23 -20
- mindspore/dataset/vision/transforms.py +316 -315
- mindspore/dataset/vision/utils.py +313 -17
- mindspore/dataset/vision/validators.py +6 -6
- mindspore/default_config.py +0 -1
- mindspore/dpcmi.dll +0 -0
- mindspore/{compression → experimental}/__init__.py +6 -5
- mindspore/experimental/map_parameter.py +275 -0
- mindspore/include/OWNERS +0 -1
- mindspore/include/api/callback/callback.h +9 -13
- mindspore/include/api/callback/ckpt_saver.h +2 -2
- mindspore/include/api/callback/loss_monitor.h +2 -2
- mindspore/include/api/callback/lr_scheduler.h +5 -5
- mindspore/include/api/callback/time_monitor.h +2 -2
- mindspore/include/api/callback/train_accuracy.h +4 -6
- mindspore/include/api/cfg.h +19 -6
- mindspore/include/api/context.h +70 -9
- mindspore/include/api/delegate.h +8 -1
- mindspore/include/api/dual_abi_helper.h +8 -24
- mindspore/include/api/metrics/accuracy.h +2 -2
- mindspore/include/api/metrics/metrics.h +4 -3
- mindspore/include/api/model.h +9 -4
- mindspore/include/api/model_group.h +68 -0
- mindspore/include/api/model_parallel_runner.h +17 -17
- mindspore/include/api/net.h +12 -11
- mindspore/include/api/serialization.h +20 -4
- mindspore/include/api/status.h +7 -1
- mindspore/include/api/types.h +25 -21
- mindspore/include/api/visible.h +4 -0
- mindspore/include/c_api/model_c.h +5 -0
- mindspore/include/c_api/status_c.h +1 -1
- mindspore/include/dataset/config.h +1 -1
- mindspore/include/dataset/constants.h +14 -0
- mindspore/include/dataset/text.h +59 -0
- mindspore/include/dataset/vision.h +56 -117
- mindspore/include/dataset/vision_lite.h +102 -0
- mindspore/jpeg62.dll +0 -0
- mindspore/log.py +28 -28
- mindspore/mindrecord/common/exceptions.py +2 -4
- mindspore/mindrecord/filereader.py +19 -1
- mindspore/mindrecord/filewriter.py +250 -88
- mindspore/mindrecord/mindpage.py +13 -13
- mindspore/mindrecord/shardheader.py +15 -15
- mindspore/mindrecord/shardreader.py +9 -0
- mindspore/mindrecord/shardwriter.py +29 -29
- mindspore/mindrecord/tools/cifar100_to_mr.py +9 -9
- mindspore/mindrecord/tools/cifar10_to_mr.py +9 -9
- mindspore/mindrecord/tools/csv_to_mr.py +4 -4
- mindspore/mindrecord/tools/imagenet_to_mr.py +70 -65
- mindspore/mindrecord/tools/mnist_to_mr.py +41 -41
- mindspore/mindrecord/tools/tfrecord_to_mr.py +6 -6
- mindspore/{libmindspore_backend.dll → mindspore_backend.dll} +0 -0
- mindspore/mindspore_common.dll +0 -0
- mindspore/mindspore_core.dll +0 -0
- mindspore/mindspore_glog.dll +0 -0
- mindspore/mindspore_shared_lib.dll +0 -0
- mindspore/msobj140.dll +0 -0
- mindspore/mspdb140.dll +0 -0
- mindspore/mspdbcore.dll +0 -0
- mindspore/mspdbst.dll +0 -0
- mindspore/mspft140.dll +0 -0
- mindspore/msvcdis140.dll +0 -0
- mindspore/msvcp140_1.dll +0 -0
- mindspore/msvcp140_2.dll +0 -0
- mindspore/msvcp140_atomic_wait.dll +0 -0
- mindspore/msvcp140_codecvt_ids.dll +0 -0
- mindspore/nn/__init__.py +1 -5
- mindspore/nn/cell.py +297 -234
- mindspore/nn/dynamic_lr.py +1 -1
- mindspore/nn/grad/cell_grad.py +17 -42
- mindspore/nn/layer/__init__.py +7 -4
- mindspore/nn/layer/activation.py +131 -88
- mindspore/nn/layer/basic.py +313 -613
- mindspore/nn/layer/channel_shuffle.py +103 -0
- mindspore/nn/layer/combined.py +1 -1
- mindspore/nn/layer/container.py +52 -6
- mindspore/nn/layer/conv.py +112 -43
- mindspore/nn/layer/dense.py +10 -9
- mindspore/nn/layer/embedding.py +36 -34
- mindspore/nn/layer/image.py +123 -27
- mindspore/nn/layer/math.py +108 -107
- mindspore/nn/layer/normalization.py +212 -366
- mindspore/nn/layer/padding.py +370 -42
- mindspore/nn/layer/pooling.py +1443 -219
- mindspore/nn/layer/rnn_cells.py +11 -16
- mindspore/nn/layer/rnns.py +38 -39
- mindspore/nn/layer/thor_layer.py +24 -25
- mindspore/nn/layer/timedistributed.py +5 -5
- mindspore/nn/layer/transformer.py +701 -0
- mindspore/nn/learning_rate_schedule.py +8 -8
- mindspore/nn/loss/__init__.py +9 -6
- mindspore/nn/loss/loss.py +678 -142
- mindspore/nn/metrics.py +53 -0
- mindspore/nn/optim/_dist_optimizer_registry.py +2 -2
- mindspore/nn/optim/ada_grad.py +8 -8
- mindspore/nn/optim/adadelta.py +2 -3
- mindspore/nn/optim/adafactor.py +18 -14
- mindspore/nn/optim/adam.py +429 -87
- mindspore/nn/optim/adamax.py +5 -6
- mindspore/nn/optim/adasum.py +10 -8
- mindspore/nn/optim/asgd.py +7 -7
- mindspore/nn/optim/ftrl.py +81 -11
- mindspore/nn/optim/lamb.py +7 -8
- mindspore/nn/optim/lars.py +4 -4
- mindspore/nn/optim/lazyadam.py +82 -7
- mindspore/nn/optim/momentum.py +8 -7
- mindspore/nn/optim/optimizer.py +19 -10
- mindspore/nn/optim/proximal_ada_grad.py +6 -5
- mindspore/nn/optim/rmsprop.py +3 -3
- mindspore/nn/optim/rprop.py +20 -16
- mindspore/nn/optim/sgd.py +21 -15
- mindspore/nn/optim/thor.py +23 -21
- mindspore/nn/probability/__init__.py +0 -2
- mindspore/nn/probability/bijector/bijector.py +7 -6
- mindspore/nn/probability/bijector/invert.py +4 -2
- mindspore/nn/probability/bijector/softplus.py +2 -2
- mindspore/nn/probability/bnn_layers/dense_variational.py +1 -1
- mindspore/nn/probability/bnn_layers/layer_distribution.py +2 -2
- mindspore/nn/probability/distribution/__init__.py +6 -0
- mindspore/nn/probability/distribution/_utils/custom_ops.py +3 -2
- mindspore/nn/probability/distribution/_utils/utils.py +11 -17
- mindspore/nn/probability/distribution/bernoulli.py +6 -6
- mindspore/nn/probability/distribution/beta.py +1 -1
- mindspore/nn/probability/distribution/categorical.py +9 -9
- mindspore/nn/probability/distribution/cauchy.py +8 -8
- mindspore/nn/probability/distribution/distribution.py +12 -6
- mindspore/nn/probability/distribution/exponential.py +5 -5
- mindspore/nn/probability/distribution/gamma.py +3 -3
- mindspore/nn/probability/distribution/geometric.py +6 -5
- mindspore/nn/probability/distribution/gumbel.py +5 -5
- mindspore/nn/probability/distribution/half_normal.py +133 -0
- mindspore/nn/probability/distribution/laplace.py +128 -0
- mindspore/nn/probability/distribution/log_normal.py +0 -1
- mindspore/nn/probability/distribution/logistic.py +4 -5
- mindspore/nn/probability/distribution/normal.py +11 -15
- mindspore/nn/probability/distribution/poisson.py +6 -2
- mindspore/nn/probability/distribution/student_t.py +150 -0
- mindspore/nn/probability/distribution/transformed_distribution.py +4 -4
- mindspore/nn/probability/distribution/uniform.py +5 -5
- mindspore/nn/reinforcement/_tensors_queue.py +3 -3
- mindspore/nn/reinforcement/tensor_array.py +2 -2
- mindspore/nn/sparse/sparse.py +8 -1
- mindspore/nn/wrap/cell_wrapper.py +55 -27
- mindspore/nn/wrap/grad_reducer.py +20 -11
- mindspore/nn/wrap/loss_scale.py +47 -30
- mindspore/numpy/array_creations.py +33 -22
- mindspore/numpy/array_ops.py +46 -42
- mindspore/numpy/logic_ops.py +6 -27
- mindspore/numpy/math_ops.py +26 -19
- mindspore/numpy/utils.py +1 -8
- mindspore/numpy/utils_const.py +112 -62
- mindspore/opencv_core452.dll +0 -0
- mindspore/opencv_imgcodecs452.dll +0 -0
- mindspore/opencv_imgproc452.dll +0 -0
- mindspore/ops/__init__.py +6 -3
- mindspore/ops/_constants.py +0 -6
- mindspore/ops/_grad/__init__.py +2 -1
- mindspore/ops/_grad/grad_array_ops.py +209 -152
- mindspore/ops/_grad/grad_base.py +55 -17
- mindspore/ops/_grad/grad_clip_ops.py +11 -3
- mindspore/ops/_grad/grad_comm_ops.py +58 -47
- mindspore/ops/_grad/grad_implementations.py +21 -61
- mindspore/ops/_grad/grad_inner_ops.py +48 -6
- mindspore/ops/_grad/grad_math_ops.py +306 -161
- mindspore/ops/_grad/grad_nn_ops.py +192 -181
- mindspore/ops/_grad/grad_other_ops.py +1 -1
- mindspore/ops/_grad/grad_quant_ops.py +5 -5
- mindspore/ops/_grad/grad_sequence_ops.py +296 -0
- mindspore/ops/_grad/grad_sparse.py +15 -9
- mindspore/ops/_grad_experimental/__init__.py +1 -0
- mindspore/ops/_grad_experimental/grad_array_ops.py +441 -55
- mindspore/ops/_grad_experimental/grad_image_ops.py +25 -7
- mindspore/ops/_grad_experimental/grad_inner_ops.py +3 -44
- mindspore/ops/_grad_experimental/grad_linalg_ops.py +16 -21
- mindspore/ops/_grad_experimental/grad_math_ops.py +979 -49
- mindspore/ops/_grad_experimental/grad_nn_ops.py +78 -8
- mindspore/ops/_grad_experimental/grad_scalar_ops.py +112 -0
- mindspore/ops/_grad_experimental/grad_sparse_ops.py +197 -13
- mindspore/ops/_op_impl/__init__.py +3 -3
- mindspore/ops/_op_impl/_custom_op/__init__.py +0 -1
- mindspore/ops/_op_impl/_custom_op/_basic.py +0 -1
- mindspore/ops/_op_impl/_custom_op/batch_matmul_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold.py +4 -2
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold2.py +2 -2
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad_reduce.py +5 -5
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold_grad.py +3 -3
- mindspore/ops/_op_impl/_custom_op/cholesky_trsm_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/correction_mul.py +3 -3
- mindspore/ops/_op_impl/_custom_op/correction_mul_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +4 -8
- mindspore/ops/_op_impl/_custom_op/dsd_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad_reduce.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad_reduce.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fused_abs_max1_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/img2col_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_left_impl.py +2 -2
- mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_right_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_left_cast_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_right_mul_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_impl.py +2 -2
- mindspore/ops/_op_impl/_custom_op/matmul_dds_grad_impl.py +0 -1
- mindspore/ops/_op_impl/_custom_op/matmul_dds_impl.py +0 -1
- mindspore/ops/_op_impl/_custom_op/matrix_combine_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/minmax_update_perchannel.py +2 -2
- mindspore/ops/_op_impl/_custom_op/minmax_update_perlayer.py +2 -2
- mindspore/ops/_op_impl/_custom_op/transpose02314_impl.py +1 -1
- mindspore/ops/_op_impl/aicpu/__init__.py +238 -3
- mindspore/ops/_op_impl/aicpu/abs.py +36 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d.py +34 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d_grad.py +34 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d.py +39 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d_grad.py +39 -0
- mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d_grad.py +37 -0
- mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d.py +42 -0
- mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d_grad.py +152 -0
- mindspore/ops/_op_impl/aicpu/add.py +43 -0
- mindspore/ops/_op_impl/aicpu/addcdiv.py +0 -32
- mindspore/ops/_op_impl/aicpu/addcmul.py +0 -84
- mindspore/ops/_op_impl/aicpu/affine_grid_grad.py +35 -0
- mindspore/ops/_op_impl/aicpu/arg_max.py +75 -0
- mindspore/ops/_op_impl/aicpu/arg_min.py +75 -0
- mindspore/ops/_op_impl/aicpu/argmin_with_value.py +43 -0
- mindspore/ops/_op_impl/aicpu/batch_matmul.py +43 -0
- mindspore/ops/_op_impl/aicpu/batch_norm_grad_grad.py +49 -0
- mindspore/ops/_op_impl/aicpu/bernoulli.py +48 -0
- mindspore/ops/_op_impl/aicpu/bessel_i0.py +31 -0
- mindspore/ops/_op_impl/aicpu/bias_add.py +44 -0
- mindspore/ops/_op_impl/aicpu/bias_add_grad.py +43 -0
- mindspore/ops/_op_impl/aicpu/bincount.py +33 -0
- mindspore/{nn/probability/infer/variational/__init__.py → ops/_op_impl/aicpu/cauchy.py} +17 -10
- mindspore/ops/_op_impl/aicpu/channel_shuffle.py +40 -0
- mindspore/ops/_op_impl/aicpu/cholesky.py +1 -1
- mindspore/ops/_op_impl/{cpu/bias_add.py → aicpu/choleskygrad.py} +9 -7
- mindspore/ops/_op_impl/aicpu/combined_non_max_suppression.py +42 -0
- mindspore/ops/_op_impl/aicpu/concat_offset.py +42 -0
- mindspore/ops/_op_impl/aicpu/concat_offset_v1.py +31 -0
- mindspore/ops/_op_impl/aicpu/conj.py +11 -0
- mindspore/ops/_op_impl/aicpu/crop_and_resize_grad_image.py +38 -0
- mindspore/ops/_op_impl/aicpu/cumulative_logsumexp.py +36 -0
- mindspore/ops/_op_impl/aicpu/deformable_offsets.py +38 -0
- mindspore/ops/_op_impl/aicpu/deformable_offsets_grad.py +2 -2
- mindspore/ops/_op_impl/aicpu/dense_to_sparse_set_operation.py +48 -0
- mindspore/ops/_op_impl/aicpu/diag.py +36 -0
- mindspore/ops/_op_impl/aicpu/diag_part.py +36 -0
- mindspore/ops/_op_impl/aicpu/diagonal.py +35 -0
- mindspore/ops/_op_impl/{cpu/bias_add_grad.py → aicpu/digamma.py} +9 -7
- mindspore/ops/_op_impl/aicpu/eig.py +35 -0
- mindspore/ops/_op_impl/aicpu/fft_with_size.py +41 -0
- mindspore/ops/_op_impl/aicpu/flatten.py +1 -0
- mindspore/ops/_op_impl/aicpu/fmax.py +36 -0
- mindspore/ops/_op_impl/aicpu/fmin.py +37 -0
- mindspore/ops/_op_impl/aicpu/fractional_max_pool3d_with_fixed_ksize.py +1 -1
- mindspore/ops/_op_impl/aicpu/fse_decode.py +43 -0
- mindspore/ops/_op_impl/aicpu/glu.py +33 -0
- mindspore/ops/_op_impl/aicpu/glu_grad.py +34 -0
- mindspore/ops/_op_impl/aicpu/greater.py +41 -0
- mindspore/ops/_op_impl/aicpu/greater_equal.py +41 -0
- mindspore/ops/_op_impl/aicpu/index_put.py +50 -0
- mindspore/ops/_op_impl/{tbe/scatter_add_ds.py → aicpu/inplace_index_add.py} +17 -21
- mindspore/ops/_op_impl/aicpu/instance_norm_v2.py +41 -0
- mindspore/ops/_op_impl/aicpu/instance_norm_v2_grad.py +44 -0
- mindspore/ops/_op_impl/aicpu/layer_norm_grad_grad.py +47 -0
- mindspore/ops/_op_impl/aicpu/less.py +41 -0
- mindspore/ops/_op_impl/aicpu/less_equal.py +41 -0
- mindspore/ops/_op_impl/aicpu/lgamma.py +32 -0
- mindspore/ops/_op_impl/aicpu/log_normal_reverse.py +33 -0
- mindspore/ops/_op_impl/aicpu/logit.py +33 -0
- mindspore/ops/_op_impl/aicpu/logit_grad.py +34 -0
- mindspore/ops/_op_impl/aicpu/masked_fill.py +42 -0
- mindspore/ops/_op_impl/aicpu/masked_scatter.py +39 -0
- mindspore/ops/_op_impl/aicpu/matmul.py +39 -0
- mindspore/ops/_op_impl/aicpu/matrix_logarithm.py +31 -0
- mindspore/ops/_op_impl/aicpu/matrix_power.py +32 -0
- mindspore/ops/_op_impl/aicpu/matrix_solve_ls.py +36 -0
- mindspore/ops/_op_impl/aicpu/matrix_triangular_solve.py +36 -0
- mindspore/ops/_op_impl/aicpu/mirror_pad.py +2 -0
- mindspore/ops/_op_impl/aicpu/mirror_pad_grad.py +0 -4
- mindspore/ops/_op_impl/aicpu/mul.py +3 -1
- mindspore/ops/_op_impl/aicpu/multinomial.py +14 -6
- mindspore/ops/_op_impl/aicpu/multinomial_with_replacement.py +35 -0
- mindspore/ops/_op_impl/aicpu/nan_to_num.py +34 -0
- mindspore/ops/_op_impl/aicpu/nllloss.py +38 -0
- mindspore/ops/_op_impl/aicpu/nllloss_grad.py +39 -0
- mindspore/ops/_op_impl/aicpu/ones_like.py +0 -2
- mindspore/ops/_op_impl/aicpu/polar.py +32 -0
- mindspore/ops/_op_impl/aicpu/polygamma.py +34 -0
- mindspore/ops/_op_impl/aicpu/qr.py +36 -0
- mindspore/ops/_op_impl/aicpu/quant_dtype_cast.py +40 -0
- mindspore/ops/_op_impl/aicpu/quantile.py +35 -0
- mindspore/ops/_op_impl/aicpu/ragged_tensor_to_sparse.py +73 -0
- mindspore/ops/_op_impl/aicpu/ragged_tensor_to_tensor.py +74 -0
- mindspore/ops/_op_impl/aicpu/random_shuffle.py +3 -0
- mindspore/ops/_op_impl/aicpu/randperm_v2.py +41 -0
- mindspore/ops/_op_impl/aicpu/range.py +36 -0
- mindspore/ops/_op_impl/aicpu/reciprocal.py +34 -0
- mindspore/ops/_op_impl/aicpu/reciprocal_grad.py +35 -0
- mindspore/ops/_op_impl/aicpu/reduce_sum.py +57 -0
- mindspore/ops/_op_impl/aicpu/resize_bicubic.py +2 -8
- mindspore/ops/_op_impl/aicpu/resize_bicubic_grad.py +1 -1
- mindspore/ops/_op_impl/aicpu/resize_v2.py +68 -0
- mindspore/ops/_op_impl/aicpu/resize_v2_grad.py +68 -0
- mindspore/ops/_op_impl/aicpu/scatter_elements.py +4 -0
- mindspore/ops/_op_impl/aicpu/scatter_nd_update.py +2 -0
- mindspore/ops/_op_impl/aicpu/search_sorted.py +12 -6
- mindspore/ops/_op_impl/aicpu/self_adjoint_eig.py +34 -0
- mindspore/ops/_op_impl/aicpu/sequence_add.py +34 -0
- mindspore/ops/_op_impl/aicpu/sequence_add_offset.py +34 -0
- mindspore/ops/_op_impl/aicpu/sequence_addn.py +38 -0
- mindspore/ops/_op_impl/aicpu/slice_grad.py +76 -0
- mindspore/ops/_op_impl/aicpu/smooth_l1_loss.py +35 -0
- mindspore/ops/_op_impl/aicpu/smooth_l1_loss_grad.py +37 -0
- mindspore/ops/_op_impl/aicpu/sort.py +39 -0
- mindspore/ops/_op_impl/aicpu/sparse_apply_adagrad_da.py +0 -24
- mindspore/ops/_op_impl/aicpu/sparse_cross.py +42 -0
- mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows.py +63 -0
- mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows_grad.py +45 -0
- mindspore/ops/_op_impl/aicpu/sparse_matrix_mat_mul.py +56 -0
- mindspore/ops/_op_impl/{tbe/slice_ds.py → aicpu/sparse_segment_sum.py} +16 -24
- mindspore/ops/_op_impl/aicpu/sparse_segment_sum_with_num_segments.py +68 -0
- mindspore/ops/_op_impl/aicpu/sparse_slice.py +63 -0
- mindspore/ops/_op_impl/aicpu/sparse_slice_grad.py +61 -0
- mindspore/ops/_op_impl/aicpu/squared_difference.py +2 -0
- mindspore/ops/_op_impl/aicpu/strided_slice_v2.py +93 -0
- mindspore/ops/_op_impl/aicpu/strided_slice_v2_grad.py +66 -0
- mindspore/ops/_op_impl/aicpu/tensor_scatter_update.py +59 -0
- mindspore/ops/_op_impl/{tbe/gather_v2.py → aicpu/tile.py} +24 -24
- mindspore/ops/_op_impl/aicpu/tridiagonal_solve.py +35 -0
- mindspore/ops/_op_impl/aicpu/tril_indices.py +34 -0
- mindspore/ops/_op_impl/aicpu/triu_indices.py +34 -0
- mindspore/ops/_op_impl/aicpu/uniform.py +34 -0
- mindspore/ops/_op_impl/aicpu/uniform_candidate_sampler.py +1 -0
- mindspore/ops/_op_impl/aicpu/unique_consecutive.py +10 -2
- mindspore/ops/_op_impl/cpu/__init__.py +1 -2
- mindspore/ops/_op_impl/cpu/dynamic_shape.py +5 -1
- mindspore/ops/_op_impl/cpu/maximum_grad.py +2 -0
- mindspore/{compression/common/__init__.py → ops/_op_impl/cpu/pyexecute.py} +13 -8
- mindspore/ops/_op_impl/cpu/reduce_sum.py +8 -0
- mindspore/ops/_op_impl/cpu/sparse_slice.py +62 -0
- mindspore/ops/_op_impl/cpu/sparse_slice_grad.py +60 -0
- mindspore/ops/_op_impl/cpu/tensor_shape.py +5 -1
- mindspore/ops/_op_impl/tbe/__init__.py +27 -608
- mindspore/ops/_op_impl/tbe/addcdiv_ds.py +42 -0
- mindspore/ops/_op_impl/tbe/addcmul_ds.py +44 -0
- mindspore/ops/_op_impl/tbe/assign_add_ds.py +1 -0
- mindspore/ops/_op_impl/tbe/atomic_addr_clean.py +1 -1
- mindspore/ops/_op_impl/tbe/avg_pool_3d_grad.py +1 -1
- mindspore/ops/_op_impl/tbe/basic_lstm_cell_c_state_grad_v2.py +0 -1
- mindspore/ops/_op_impl/tbe/batch_to_space.py +1 -1
- mindspore/ops/_op_impl/tbe/batch_to_space_nd.py +1 -1
- mindspore/ops/_op_impl/tbe/batch_to_space_nd_v2.py +41 -0
- mindspore/ops/_op_impl/tbe/bce_with_logits_loss.py +1 -0
- mindspore/ops/_op_impl/tbe/bias_add_grad.py +2 -0
- mindspore/ops/_op_impl/tbe/bn_infer_grad.py +4 -2
- mindspore/ops/_op_impl/tbe/bn_infer_grad_ds.py +40 -0
- mindspore/ops/_op_impl/tbe/bn_training_update.py +0 -1
- mindspore/ops/_op_impl/tbe/bn_training_update_ds.py +0 -1
- mindspore/ops/_op_impl/tbe/broadcast_to_ds.py +6 -4
- mindspore/ops/_op_impl/tbe/cast.py +0 -2
- mindspore/ops/_op_impl/tbe/cast_ds.py +3 -3
- mindspore/ops/_op_impl/tbe/ctc_loss_v2.py +0 -2
- mindspore/ops/_op_impl/tbe/ctc_loss_v2_grad.py +0 -2
- mindspore/ops/_op_impl/tbe/data_format_dim_map_ds.py +1 -0
- mindspore/ops/_op_impl/tbe/deformable_offsets.py +1 -0
- mindspore/ops/_op_impl/tbe/depthwise_conv2d.py +1 -1
- mindspore/ops/_op_impl/tbe/dynamic_atomic_addr_clean.py +1 -1
- mindspore/ops/_op_impl/tbe/gather_nd.py +1 -0
- mindspore/ops/_op_impl/tbe/greater.py +2 -0
- mindspore/ops/_op_impl/tbe/{index_add.py → inplace_index_add.py} +3 -6
- mindspore/ops/_op_impl/tbe/layer_norm_beta_gamma_backprop_v2.py +0 -1
- mindspore/ops/_op_impl/tbe/npu_clear_float_status_v2.py +35 -0
- mindspore/ops/_op_impl/tbe/npu_get_float_status_v2.py +35 -0
- mindspore/ops/_op_impl/tbe/one_hot_ds.py +0 -6
- mindspore/ops/_op_impl/tbe/{greater_ds.py → reduce_all_ds.py} +13 -16
- mindspore/ops/_op_impl/tbe/reduce_any_ds.py +39 -0
- mindspore/ops/_op_impl/tbe/roi_align_ds.py +44 -0
- mindspore/ops/_op_impl/tbe/roi_align_grad_ds.py +44 -0
- mindspore/ops/_op_impl/tbe/scatter_add.py +2 -0
- mindspore/ops/_op_impl/tbe/scatter_nd_add.py +2 -2
- mindspore/ops/_op_impl/tbe/slice.py +26 -15
- mindspore/ops/_op_impl/tbe/space_to_batch.py +1 -1
- mindspore/ops/_op_impl/tbe/space_to_batch_nd.py +1 -1
- mindspore/ops/_op_impl/tbe/strided_slice_grad_d.py +1 -0
- mindspore/ops/_op_impl/tbe/trans_data_ds.py +15 -5
- mindspore/ops/_op_impl/tbe/unsorted_segment_sum.py +1 -1
- mindspore/ops/_op_impl/tbe/unsorted_segment_sum_ds.py +2 -0
- mindspore/ops/_primitive_cache.py +3 -2
- mindspore/ops/_register_for_op.py +11 -0
- mindspore/ops/_utils/__init__.py +1 -1
- mindspore/ops/_utils/utils.py +20 -41
- mindspore/ops/_vmap/__init__.py +2 -2
- mindspore/ops/_vmap/vmap_array_ops.py +170 -78
- mindspore/ops/_vmap/vmap_base.py +24 -10
- mindspore/ops/_vmap/vmap_convolution_ops.py +7 -10
- mindspore/ops/_vmap/vmap_grad_math_ops.py +4 -4
- mindspore/ops/_vmap/vmap_grad_nn_ops.py +41 -9
- mindspore/ops/_vmap/vmap_image_ops.py +52 -0
- mindspore/ops/_vmap/vmap_math_ops.py +77 -6
- mindspore/ops/_vmap/vmap_nn_ops.py +78 -29
- mindspore/ops/_vmap/vmap_other_ops.py +3 -1
- mindspore/ops/_vmap/vmap_random_ops.py +55 -3
- mindspore/ops/_vmap/vmap_sparse_ops.py +1 -0
- mindspore/ops/bprop_mindir/AdaptiveAvgPool2D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/AdaptiveMaxPool2D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ApproximateEqual_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/Argmax_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/Argmin_bprop.mindir +14 -13
- mindspore/ops/bprop_mindir/AssignSub_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/Assign_bprop.mindir +16 -16
- mindspore/ops/bprop_mindir/AvgPool3D_bprop.mindir +150 -0
- mindspore/ops/bprop_mindir/AvgPool_bprop.mindir +66 -0
- mindspore/ops/bprop_mindir/BCEWithLogitsLoss_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/BNTrainingReduce_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/BatchNormGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/BatchToSpaceND_bprop.mindir +28 -0
- mindspore/ops/bprop_mindir/BiasAddGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/BinaryCrossEntropy_bprop.mindir +33 -0
- mindspore/ops/bprop_mindir/BroadcastTo_bprop.mindir +306 -0
- mindspore/ops/bprop_mindir/Broadcast_bprop.mindir +12 -8
- mindspore/ops/bprop_mindir/CTCLoss_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Concat_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Conv2DBackpropFilter_bprop.mindir +240 -0
- mindspore/ops/bprop_mindir/Conv2DBackpropInput_bprop.mindir +247 -0
- mindspore/ops/bprop_mindir/Conv2DTranspose_bprop.mindir +247 -0
- mindspore/ops/bprop_mindir/Conv3DTranspose_bprop.mindir +315 -0
- mindspore/ops/bprop_mindir/Conv3D_bprop.mindir +278 -0
- mindspore/ops/bprop_mindir/DType_bprop.mindir +12 -12
- mindspore/ops/bprop_mindir/DeformableOffsets_bprop.mindir +58 -0
- mindspore/ops/bprop_mindir/Depend_bprop.mindir +12 -13
- mindspore/ops/bprop_mindir/DepthToSpace_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/DepthwiseConv2dNative_bprop.mindir +138 -0
- mindspore/ops/bprop_mindir/DiagPart_bprop.mindir +15 -0
- mindspore/ops/bprop_mindir/Dropout2D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Dropout3D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DropoutDoMask_bprop.mindir +22 -24
- mindspore/ops/bprop_mindir/DropoutGenMask_bprop.mindir +16 -14
- mindspore/ops/bprop_mindir/DropoutGrad_bprop.mindir +27 -0
- mindspore/ops/bprop_mindir/Dropout_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DynamicGRUV2_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DynamicRNN_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DynamicShape_bprop.mindir +12 -12
- mindspore/ops/bprop_mindir/Elu_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/EmbeddingLookup_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Equal_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/ExpandDims_bprop.mindir +58 -0
- mindspore/ops/bprop_mindir/FastGeLU_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Flatten_bprop.mindir +54 -0
- mindspore/ops/bprop_mindir/FloorDiv_bprop.mindir +18 -15
- mindspore/ops/bprop_mindir/GatherD_bprop.mindir +26 -0
- mindspore/ops/bprop_mindir/GatherNd_bprop.mindir +57 -0
- mindspore/ops/bprop_mindir/Gather_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/GreaterEqual_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/Greater_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/HSigmoid_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/HSwish_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/IOU_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/InstanceNorm_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/IsFinite_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/IsInf_bprop.mindir +13 -10
- mindspore/ops/bprop_mindir/IsNan_bprop.mindir +14 -11
- mindspore/ops/bprop_mindir/KLDivLoss_bprop.mindir +126 -0
- mindspore/ops/bprop_mindir/L2Loss_bprop.mindir +15 -0
- mindspore/ops/bprop_mindir/L2Normalize_bprop.mindir +30 -0
- mindspore/ops/bprop_mindir/LRN_bprop.mindir +43 -0
- mindspore/ops/bprop_mindir/LayerNormGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/LessEqual_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/Less_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/LinSpace_bprop.mindir +22 -19
- mindspore/ops/bprop_mindir/Load_bprop.mindir +12 -13
- mindspore/ops/bprop_mindir/LogSoftmax_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/LogicalAnd_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/LogicalNot_bprop.mindir +14 -13
- mindspore/ops/bprop_mindir/MaskedSelect_bprop.mindir +21 -0
- mindspore/ops/bprop_mindir/MaxPool3DGradGrad_bprop.mindir +74 -0
- mindspore/ops/bprop_mindir/MaxPool3DGrad_bprop.mindir +74 -0
- mindspore/ops/bprop_mindir/MaxPool3D_bprop.mindir +75 -0
- mindspore/ops/bprop_mindir/MaxPoolGradGrad_bprop.mindir +65 -0
- mindspore/ops/bprop_mindir/MaxPoolWithArgmax_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Maximum_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Minimum_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/MirrorPad_bprop.mindir +27 -0
- mindspore/ops/bprop_mindir/Mish_bprop.mindir +35 -0
- mindspore/ops/bprop_mindir/MulNoNan_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/NLLLoss_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/NonZero_bprop.mindir +14 -0
- mindspore/ops/bprop_mindir/NotEqual_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/OneHot_bprop.mindir +25 -23
- mindspore/ops/bprop_mindir/OnesLike_bprop.mindir +13 -13
- mindspore/ops/bprop_mindir/PReLU_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Pad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Padding_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/RNNTLoss_bprop.mindir +29 -0
- mindspore/ops/bprop_mindir/ROIAlign_bprop.mindir +82 -0
- mindspore/ops/bprop_mindir/Range_bprop.mindir +21 -19
- mindspore/ops/bprop_mindir/Rank_bprop.mindir +11 -11
- mindspore/ops/bprop_mindir/ReLU6_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/ReLUV2_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ReduceAll_bprop.mindir +18 -17
- mindspore/ops/bprop_mindir/ReduceAny_bprop.mindir +18 -17
- mindspore/ops/bprop_mindir/ReluGrad_bprop.mindir +19 -23
- mindspore/ops/bprop_mindir/Reshape_bprop.mindir +60 -0
- mindspore/ops/bprop_mindir/ResizeBilinear_bprop.mindir +29 -0
- mindspore/ops/bprop_mindir/ResizeNearestNeighbor_bprop.mindir +89 -0
- mindspore/ops/bprop_mindir/ReverseSequence_bprop.mindir +52 -0
- mindspore/ops/bprop_mindir/ReverseV2_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/Round_bprop.mindir +14 -13
- mindspore/ops/bprop_mindir/ScatterMax_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ScatterMin_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ScatterNdUpdate_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/ScatterNd_bprop.mindir +24 -0
- mindspore/ops/bprop_mindir/ScatterNonAliasingAdd_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/ScatterUpdate_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/SeLU_bprop.mindir +21 -0
- mindspore/ops/bprop_mindir/Select_bprop.mindir +30 -34
- mindspore/ops/bprop_mindir/Shape_bprop.mindir +12 -12
- mindspore/ops/bprop_mindir/SigmoidCrossEntropyWithLogits_bprop.mindir +21 -0
- mindspore/ops/bprop_mindir/SigmoidGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Sigmoid_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Sign_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/Slice_bprop.mindir +26 -0
- mindspore/ops/bprop_mindir/SmoothL1Loss_bprop.mindir +36 -0
- mindspore/ops/bprop_mindir/SoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Softplus_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Softsign_bprop.mindir +33 -0
- mindspore/ops/bprop_mindir/Sort_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/SpaceToBatchND_bprop.mindir +28 -0
- mindspore/ops/bprop_mindir/SpaceToDepth_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/SparseGatherV2_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/SparseSoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Split_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/Squeeze_bprop.mindir +54 -0
- mindspore/ops/bprop_mindir/StridedSliceGrad_bprop.mindir +95 -0
- mindspore/ops/bprop_mindir/StridedSlice_bprop.mindir +98 -0
- mindspore/ops/bprop_mindir/Switch_bprop.mindir +28 -32
- mindspore/ops/bprop_mindir/TanhGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Tanh_bprop.mindir +66 -0
- mindspore/ops/bprop_mindir/TensorScatterAdd_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/TensorScatterUpdate_bprop.mindir +29 -0
- mindspore/ops/bprop_mindir/TensorShape_bprop.mindir +14 -0
- mindspore/ops/bprop_mindir/Tile_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/TopK_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/TransShape_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/TruncateDiv_bprop.mindir +18 -15
- mindspore/ops/bprop_mindir/TupleGetItem_bprop.mindir +11 -13
- mindspore/ops/bprop_mindir/Unique_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Unstack_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/UpsampleNearest3D_bprop.mindir +32 -0
- mindspore/ops/bprop_mindir/UpsampleTrilinear3D_bprop.mindir +38 -0
- mindspore/ops/bprop_mindir/ZerosLike_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/__init__.py +1 -4
- mindspore/ops/bprop_mindir/generate_mindir.py +32 -20
- mindspore/ops/composite/__init__.py +12 -13
- mindspore/ops/composite/base.py +261 -254
- mindspore/ops/composite/env_ops.py +41 -0
- mindspore/ops/composite/math_ops.py +197 -156
- mindspore/ops/composite/multitype_ops/_compile_utils.py +428 -176
- mindspore/ops/composite/multitype_ops/_constexpr_utils.py +188 -87
- mindspore/ops/composite/multitype_ops/add_impl.py +23 -1
- mindspore/ops/composite/multitype_ops/div_impl.py +3 -3
- mindspore/ops/composite/multitype_ops/equal_impl.py +1 -0
- mindspore/ops/composite/multitype_ops/floordiv_impl.py +1 -1
- mindspore/ops/composite/multitype_ops/getitem_impl.py +52 -5
- mindspore/ops/composite/multitype_ops/greater_equal_impl.py +31 -0
- mindspore/ops/composite/multitype_ops/greater_impl.py +31 -0
- mindspore/ops/composite/multitype_ops/in_impl.py +15 -3
- mindspore/ops/composite/multitype_ops/less_equal_impl.py +33 -2
- mindspore/ops/composite/multitype_ops/less_impl.py +33 -0
- mindspore/ops/composite/multitype_ops/logical_and_impl.py +2 -2
- mindspore/ops/composite/multitype_ops/logical_or_impl.py +2 -1
- mindspore/ops/composite/multitype_ops/mod_impl.py +1 -1
- mindspore/ops/composite/multitype_ops/mul_impl.py +21 -7
- mindspore/ops/composite/multitype_ops/not_in_impl.py +15 -3
- mindspore/ops/composite/multitype_ops/ones_like_impl.py +2 -4
- mindspore/ops/composite/multitype_ops/pow_impl.py +1 -0
- mindspore/ops/composite/multitype_ops/setitem_impl.py +62 -70
- mindspore/ops/composite/multitype_ops/sub_impl.py +3 -3
- mindspore/ops/composite/multitype_ops/zeros_like_impl.py +41 -4
- mindspore/ops/function/__init__.py +323 -8
- mindspore/ops/function/array_func.py +3511 -780
- mindspore/ops/function/clip_func.py +329 -0
- mindspore/ops/function/debug_func.py +6 -6
- mindspore/ops/function/grad/__init__.py +5 -1
- mindspore/ops/function/grad/grad_func.py +736 -65
- mindspore/ops/function/image_func.py +270 -0
- mindspore/ops/function/linalg_func.py +268 -8
- mindspore/ops/function/math_func.py +8032 -3164
- mindspore/ops/function/nn_func.py +5619 -1855
- mindspore/ops/function/other_func.py +115 -0
- mindspore/ops/function/parameter_func.py +11 -10
- mindspore/ops/function/random_func.py +939 -77
- mindspore/ops/function/sparse_func.py +249 -84
- mindspore/ops/function/sparse_unary_func.py +2303 -0
- mindspore/ops/function/spectral_func.py +146 -0
- mindspore/ops/function/vmap_func.py +114 -0
- mindspore/ops/functional.py +182 -254
- mindspore/ops/op_info_register.py +79 -34
- mindspore/ops/operations/__init__.py +210 -118
- mindspore/ops/operations/_csr_ops.py +7 -7
- mindspore/ops/operations/_embedding_cache_ops.py +25 -15
- mindspore/ops/operations/_grad_ops.py +447 -322
- mindspore/ops/operations/_inner_ops.py +547 -176
- mindspore/ops/operations/_map_tensor_ops.py +112 -0
- mindspore/ops/operations/_ms_kernel.py +29 -27
- mindspore/ops/operations/_ocr_ops.py +11 -11
- mindspore/ops/operations/_opaque_predicate_registry.py +41 -0
- mindspore/ops/operations/_quant_ops.py +186 -101
- mindspore/ops/operations/_rl_inner_ops.py +122 -61
- mindspore/ops/operations/_scalar_ops.py +466 -0
- mindspore/ops/operations/_sequence_ops.py +1047 -0
- mindspore/ops/operations/_tensor_array.py +10 -11
- mindspore/ops/operations/_thor_ops.py +4 -4
- mindspore/ops/operations/array_ops.py +1428 -1226
- mindspore/ops/operations/comm_ops.py +180 -117
- mindspore/ops/operations/control_ops.py +4 -2
- mindspore/ops/operations/custom_ops.py +185 -98
- mindspore/ops/operations/debug_ops.py +92 -54
- mindspore/ops/operations/image_ops.py +406 -211
- mindspore/ops/operations/inner_ops.py +42 -53
- mindspore/ops/operations/linalg_ops.py +32 -29
- mindspore/ops/operations/math_ops.py +2076 -897
- mindspore/ops/operations/nn_ops.py +1282 -1252
- mindspore/ops/operations/other_ops.py +124 -278
- mindspore/ops/operations/random_ops.py +345 -178
- mindspore/ops/operations/rl_ops.py +8 -9
- mindspore/ops/operations/sparse_ops.py +502 -157
- mindspore/ops/operations/spectral_ops.py +107 -0
- mindspore/ops/primitive.py +192 -15
- mindspore/ops/vm_impl_registry.py +23 -2
- mindspore/parallel/__init__.py +6 -1
- mindspore/parallel/_auto_parallel_context.py +199 -92
- mindspore/parallel/_cell_wrapper.py +4 -2
- mindspore/parallel/_cost_model_context.py +3 -0
- mindspore/parallel/_dp_allreduce_fusion.py +2 -1
- mindspore/parallel/_offload_context.py +185 -0
- mindspore/parallel/_parallel_serialization.py +167 -28
- mindspore/parallel/_ps_context.py +9 -5
- mindspore/parallel/_recovery_context.py +1 -1
- mindspore/parallel/_tensor.py +9 -1
- mindspore/{nn/transformer → parallel/_transformer}/__init__.py +6 -6
- mindspore/{nn/transformer → parallel/_transformer}/layers.py +59 -37
- mindspore/{nn/transformer → parallel/_transformer}/loss.py +4 -7
- mindspore/{nn/transformer → parallel/_transformer}/moe.py +160 -35
- mindspore/{nn/transformer → parallel/_transformer}/op_parallel_config.py +3 -3
- mindspore/{nn/transformer → parallel/_transformer}/transformer.py +235 -196
- mindspore/parallel/_utils.py +47 -7
- mindspore/parallel/algo_parameter_config.py +5 -1
- mindspore/parallel/checkpoint_transform.py +329 -0
- mindspore/parallel/shard.py +229 -0
- mindspore/perf_msvcbuildinsights.dll +0 -0
- mindspore/pgodb140.dll +0 -0
- mindspore/pgort140.dll +0 -0
- mindspore/profiler/__init__.py +2 -1
- mindspore/profiler/common/util.py +4 -3
- mindspore/profiler/common/validator/validate_path.py +2 -2
- mindspore/profiler/envprofiling.py +249 -0
- mindspore/profiler/parser/aicpu_data_parser.py +38 -39
- mindspore/profiler/parser/ascend_timeline_generator.py +497 -0
- mindspore/profiler/parser/base_timeline_generator.py +471 -0
- mindspore/profiler/parser/cpu_gpu_timeline_generator.py +684 -0
- mindspore/profiler/parser/framework_parser.py +42 -16
- mindspore/profiler/parser/hccl_parser.py +158 -158
- mindspore/profiler/parser/hwts_log_parser.py +7 -6
- mindspore/profiler/parser/integrator.py +18 -1579
- mindspore/profiler/parser/minddata_analyzer.py +8 -8
- mindspore/profiler/parser/msadvisor_analyzer.py +14 -27
- mindspore/profiler/parser/msadvisor_parser.py +2 -4
- mindspore/profiler/parser/optime_parser.py +17 -18
- mindspore/profiler/parser/profiler_info.py +108 -0
- mindspore/profiler/parser/step_trace_parser.py +1 -1
- mindspore/profiler/profiling.py +396 -194
- mindspore/rewrite/__init__.py +6 -2
- mindspore/rewrite/api/node.py +51 -110
- mindspore/rewrite/api/node_type.py +10 -6
- mindspore/rewrite/api/pattern_engine.py +51 -7
- mindspore/rewrite/api/scoped_value.py +64 -53
- mindspore/rewrite/api/symbol_tree.py +108 -61
- mindspore/rewrite/api/tree_node_helper.py +2 -3
- mindspore/{compression/quant/__init__.py → rewrite/ast_creator_register.py} +20 -11
- mindspore/rewrite/ast_helpers/__init__.py +6 -3
- mindspore/rewrite/ast_helpers/ast_creator.py +115 -0
- mindspore/rewrite/ast_helpers/ast_finder.py +99 -1
- mindspore/rewrite/ast_helpers/ast_modifier.py +17 -4
- mindspore/rewrite/ast_helpers/ast_replacer.py +1 -1
- mindspore/rewrite/ast_transformers/__init__.py +0 -1
- mindspore/rewrite/ast_transformers/flatten_recursive_stmt.py +46 -5
- mindspore/rewrite/ast_transformers/remove_return_out_of_if.py +6 -3
- mindspore/rewrite/common/__init__.py +2 -0
- mindspore/rewrite/common/event.py +1 -1
- mindspore/rewrite/common/observable.py +1 -1
- mindspore/rewrite/common/observer.py +1 -1
- mindspore/rewrite/common/rewrite_elog.py +35 -0
- mindspore/rewrite/namer.py +2 -2
- mindspore/rewrite/namespace.py +14 -4
- mindspore/rewrite/node.py +161 -13
- mindspore/rewrite/parser.py +0 -1
- mindspore/rewrite/parser_register.py +0 -1
- mindspore/rewrite/parsers/arguments_parser.py +3 -2
- mindspore/rewrite/parsers/assign_parser.py +267 -67
- mindspore/rewrite/parsers/attribute_parser.py +56 -0
- mindspore/rewrite/parsers/class_def_parser.py +191 -108
- mindspore/rewrite/parsers/constant_parser.py +101 -0
- mindspore/rewrite/parsers/container_parser.py +88 -0
- mindspore/rewrite/parsers/for_parser.py +28 -15
- mindspore/rewrite/parsers/function_def_parser.py +21 -5
- mindspore/rewrite/parsers/if_parser.py +11 -28
- mindspore/rewrite/parsers/module_parser.py +9 -6
- mindspore/rewrite/parsers/return_parser.py +3 -2
- mindspore/rewrite/sparsify/__init__.py +0 -0
- mindspore/rewrite/sparsify/sparse_transformer.py +448 -0
- mindspore/rewrite/sparsify/sparsify.py +109 -0
- mindspore/rewrite/sparsify/utils.py +173 -0
- mindspore/rewrite/symbol_tree.py +322 -109
- mindspore/rewrite/symbol_tree_builder.py +45 -8
- mindspore/rewrite/symbol_tree_dumper.py +0 -1
- mindspore/rewrite/topological_manager.py +1 -2
- mindspore/run_check/_check_version.py +209 -112
- mindspore/run_check/run_check.py +2 -1
- mindspore/tbbmalloc.dll +0 -0
- mindspore/tinyxml2.dll +0 -0
- mindspore/train/__init__.py +6 -4
- mindspore/train/_utils.py +28 -5
- mindspore/train/amp.py +321 -50
- mindspore/train/callback/__init__.py +3 -1
- mindspore/train/callback/_backup_and_restore.py +120 -0
- mindspore/train/callback/_callback.py +8 -8
- mindspore/train/callback/_checkpoint.py +12 -9
- mindspore/train/callback/_early_stop.py +13 -7
- mindspore/train/callback/_history.py +8 -8
- mindspore/train/callback/_lambda_callback.py +6 -6
- mindspore/train/callback/_landscape.py +36 -38
- mindspore/train/callback/_loss_monitor.py +12 -6
- mindspore/train/callback/_lr_scheduler_callback.py +2 -4
- mindspore/train/callback/_on_request_exit.py +212 -0
- mindspore/train/callback/_reduce_lr_on_plateau.py +13 -7
- mindspore/train/callback/_summary_collector.py +27 -19
- mindspore/train/callback/_time_monitor.py +13 -7
- mindspore/train/checkpoint_pb2.py +68 -8
- mindspore/train/data_sink.py +122 -33
- mindspore/train/dataset_helper.py +28 -87
- mindspore/train/loss_scale_manager.py +4 -7
- mindspore/{nn → train}/metrics/__init__.py +20 -20
- mindspore/{nn → train}/metrics/accuracy.py +12 -10
- mindspore/{nn → train}/metrics/auc.py +4 -4
- mindspore/{nn → train}/metrics/bleu_score.py +4 -4
- mindspore/{nn → train}/metrics/confusion_matrix.py +10 -8
- mindspore/{nn → train}/metrics/cosine_similarity.py +4 -4
- mindspore/{nn → train}/metrics/dice.py +6 -5
- mindspore/{nn → train}/metrics/error.py +7 -5
- mindspore/{nn → train}/metrics/fbeta.py +9 -7
- mindspore/{nn → train}/metrics/hausdorff_distance.py +8 -6
- mindspore/{nn → train}/metrics/loss.py +4 -3
- mindspore/{nn → train}/metrics/mean_surface_distance.py +6 -5
- mindspore/{nn → train}/metrics/metric.py +6 -5
- mindspore/{nn → train}/metrics/occlusion_sensitivity.py +4 -3
- mindspore/{nn → train}/metrics/perplexity.py +5 -4
- mindspore/{nn → train}/metrics/precision.py +5 -4
- mindspore/{nn → train}/metrics/recall.py +5 -4
- mindspore/{nn → train}/metrics/roc.py +7 -6
- mindspore/{nn → train}/metrics/root_mean_square_surface_distance.py +6 -5
- mindspore/{nn → train}/metrics/topk.py +7 -5
- mindspore/train/mind_ir_pb2.py +339 -32
- mindspore/train/model.py +113 -84
- mindspore/train/serialization.py +547 -167
- mindspore/train/summary/_summary_adapter.py +1 -1
- mindspore/train/summary/summary_record.py +43 -12
- mindspore/train/train_thor/convert_utils.py +7 -1
- mindspore/train/train_thor/dataset_helper.py +3 -3
- mindspore/train/train_thor/model_thor.py +0 -4
- mindspore/turbojpeg.dll +0 -0
- mindspore/vcmeta.dll +0 -0
- mindspore/vcruntime140.dll +0 -0
- mindspore/vcruntime140_1.dll +0 -0
- mindspore/version.py +1 -1
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/METADATA +4 -3
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/RECORD +901 -660
- mindspore/compression/common/constant.py +0 -124
- mindspore/compression/export/__init__.py +0 -19
- mindspore/compression/export/quant_export.py +0 -514
- mindspore/compression/quant/qat.py +0 -636
- mindspore/compression/quant/quant_utils.py +0 -462
- mindspore/compression/quant/quantizer.py +0 -68
- mindspore/libatomic-1.dll +0 -0
- mindspore/libgcc_s_seh-1.dll +0 -0
- mindspore/libgfortran-4.dll +0 -0
- mindspore/libgomp-1.dll +0 -0
- mindspore/libjpeg-62.dll +0 -0
- mindspore/libmindspore.dll +0 -0
- mindspore/libmindspore_common.dll +0 -0
- mindspore/libmindspore_core.dll +0 -0
- mindspore/libmindspore_glog.dll +0 -0
- mindspore/libnnacl.dll +0 -0
- mindspore/libopencv_core452.dll +0 -0
- mindspore/libopencv_imgcodecs452.dll +0 -0
- mindspore/libopencv_imgproc452.dll +0 -0
- mindspore/libquadmath-0.dll +0 -0
- mindspore/libsqlite3.dll +0 -0
- mindspore/libssp-0.dll +0 -0
- mindspore/libstdc++-6.dll +0 -0
- mindspore/libtinyxml2.dll +0 -0
- mindspore/libturbojpeg.dll +0 -0
- mindspore/libwinpthread-1.dll +0 -0
- mindspore/nn/layer/quant.py +0 -1868
- mindspore/nn/layer/rnn_utils.py +0 -90
- mindspore/nn/probability/dpn/__init__.py +0 -22
- mindspore/nn/probability/dpn/vae/__init__.py +0 -25
- mindspore/nn/probability/dpn/vae/cvae.py +0 -138
- mindspore/nn/probability/dpn/vae/vae.py +0 -122
- mindspore/nn/probability/infer/__init__.py +0 -22
- mindspore/nn/probability/infer/variational/elbo.py +0 -70
- mindspore/nn/probability/infer/variational/svi.py +0 -84
- mindspore/nn/probability/toolbox/__init__.py +0 -22
- mindspore/nn/probability/toolbox/anomaly_detection.py +0 -99
- mindspore/nn/probability/toolbox/uncertainty_evaluation.py +0 -363
- mindspore/nn/probability/transforms/__init__.py +0 -22
- mindspore/nn/probability/transforms/transform_bnn.py +0 -262
- mindspore/nn/probability/zhusuan/__init__.py +0 -18
- mindspore/nn/probability/zhusuan/framework/__init__.py +0 -18
- mindspore/nn/probability/zhusuan/framework/bn.py +0 -95
- mindspore/nn/probability/zhusuan/variational/__init__.py +0 -18
- mindspore/nn/probability/zhusuan/variational/elbo.py +0 -46
- mindspore/ops/_op_impl/tbe/bias_add_grad_ds.py +0 -52
- mindspore/ops/_op_impl/tbe/scatter_nd_add_ds.py +0 -43
- mindspore/ops/bprop_mindir/AssignAdd_bprop.mindir +0 -20
- mindspore/ops/bprop_mindir/Identity_bprop.mindir +0 -9
- mindspore/ops/bprop_mindir/LogicalOr_bprop.mindir +0 -20
- mindspore/ops/bprop_mindir/ReLU_bprop.mindir +0 -16
- mindspore/ops/bprop_mindir/UpdateState_bprop.mindir +0 -17
- mindspore/ops/bprop_mindir/stop_gradient_bprop.mindir +0 -12
- mindspore/ops/composite/array_ops.py +0 -210
- mindspore/ops/composite/clip_ops.py +0 -238
- mindspore/ops/composite/random_ops.py +0 -426
- mindspore/ops/composite/vmap_ops.py +0 -38
- mindspore/ops/operations/sponge_ops.py +0 -3531
- mindspore/ops/operations/sponge_update_ops.py +0 -2546
- mindspore/parallel/nn/__init__.py +0 -42
- mindspore/parallel/nn/loss.py +0 -22
- mindspore/parallel/nn/moe.py +0 -21
- mindspore/parallel/nn/op_parallel_config.py +0 -22
- mindspore/parallel/nn/transformer.py +0 -31
- mindspore/run_check/_check_deps_version.py +0 -84
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/WHEEL +0 -0
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/entry_points.txt +0 -0
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/top_level.txt +0 -0
mindspore/nn/layer/quant.py
DELETED
|
@@ -1,1868 +0,0 @@
|
|
|
1
|
-
# Copyright 2021 Huawei Technologies Co., Ltd
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
# ============================================================================
|
|
15
|
-
"""Quantization aware training."""
|
|
16
|
-
from __future__ import absolute_import
|
|
17
|
-
|
|
18
|
-
from functools import partial
|
|
19
|
-
from collections import namedtuple
|
|
20
|
-
import numpy as np
|
|
21
|
-
|
|
22
|
-
import mindspore.common.dtype as mstype
|
|
23
|
-
from mindspore.ops.primitive import Primitive
|
|
24
|
-
from mindspore.ops import operations as P
|
|
25
|
-
from mindspore.common.parameter import Parameter
|
|
26
|
-
from mindspore.common.initializer import initializer
|
|
27
|
-
from mindspore.common.tensor import Tensor
|
|
28
|
-
from mindspore._checkparam import Validator, twice
|
|
29
|
-
from mindspore.compression.common import QuantDtype
|
|
30
|
-
import mindspore.context as context
|
|
31
|
-
from mindspore.nn.layer.normalization import BatchNorm2d
|
|
32
|
-
from mindspore.nn.layer.activation import get_activation
|
|
33
|
-
from mindspore.nn.cell import Cell
|
|
34
|
-
from mindspore import nn
|
|
35
|
-
from mindspore.ops.operations import _quant_ops as Q
|
|
36
|
-
from mindspore.nn.layer.combined import Conv2dBnAct
|
|
37
|
-
from mindspore.nn.layer.conv import Conv2d
|
|
38
|
-
from mindspore.nn.layer.basic import Dense
|
|
39
|
-
|
|
40
|
-
__all__ = [
|
|
41
|
-
'FakeQuantWithMinMaxObserver',
|
|
42
|
-
'Conv2dBnFoldQuantOneConv',
|
|
43
|
-
'Conv2dBnFoldQuant',
|
|
44
|
-
'Conv2dBnWithoutFoldQuant',
|
|
45
|
-
'Conv2dQuant',
|
|
46
|
-
'DenseQuant',
|
|
47
|
-
'ActQuant',
|
|
48
|
-
'TensorAddQuant',
|
|
49
|
-
'MulQuant',
|
|
50
|
-
]
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
class BatchNormFoldCell(Cell):
|
|
54
|
-
"""
|
|
55
|
-
Batch Normalization folded.
|
|
56
|
-
|
|
57
|
-
Args:
|
|
58
|
-
momentum (float): Momentum value must be [0, 1]. Default: 0.9.
|
|
59
|
-
epsilon (float): A small float number to avoid dividing by 0. 1e-5 if dtype in
|
|
60
|
-
float32 else 1e-3. Default: 1e-5.
|
|
61
|
-
freeze_bn (int): Delay in steps at which computation switches from regular batch
|
|
62
|
-
norm to frozen mean and std. Default: 0.
|
|
63
|
-
|
|
64
|
-
Inputs:
|
|
65
|
-
- **x** (Tensor) - Tensor of shape :math:`(N, C, H, W)`.
|
|
66
|
-
- **mean** (Tensor) - Tensor of shape :math:`(C,)`.
|
|
67
|
-
- **variance** (Tensor) - Tensor of shape :math:`(C,)`.
|
|
68
|
-
- **global_step** (Tensor) - Tensor to record current global step.
|
|
69
|
-
|
|
70
|
-
Outputs:
|
|
71
|
-
Tuple of 4 Tensor, the normalized input and the updated parameters.
|
|
72
|
-
|
|
73
|
-
- **batch_mean** (Tensor) - Tensor of shape :math:`(C,)`.
|
|
74
|
-
- **batch_std** (Tensor) - Tensor of shape :math:`(C,)`.
|
|
75
|
-
- **running_mean** (Tensor) - Tensor of shape :math:`(C,)`.
|
|
76
|
-
- **running_std** (Tensor) - Tensor of shape :math:`(C,)`.
|
|
77
|
-
"""
|
|
78
|
-
|
|
79
|
-
def __init__(self, momentum=0.9, epsilon=1e-5, freeze_bn=0):
|
|
80
|
-
"""Initialize batch norm fold layer"""
|
|
81
|
-
super(BatchNormFoldCell, self).__init__()
|
|
82
|
-
self.epsilon = epsilon
|
|
83
|
-
self.is_gpu = context.get_context('device_target') == "GPU"
|
|
84
|
-
if self.is_gpu:
|
|
85
|
-
self.bn_train = Q.BatchNormFold(momentum, epsilon, is_training=True, freeze_bn=freeze_bn)
|
|
86
|
-
self.bn_infer = Q.BatchNormFold(momentum, epsilon, is_training=False, freeze_bn=freeze_bn)
|
|
87
|
-
else:
|
|
88
|
-
self.bn_reduce = P.BNTrainingReduce()
|
|
89
|
-
self.bn_update = Q.BatchNormFoldD(momentum, epsilon, is_training=True, freeze_bn=freeze_bn)
|
|
90
|
-
|
|
91
|
-
def construct(self, x, mean, variance, global_step):
|
|
92
|
-
if self.is_gpu:
|
|
93
|
-
if self.training:
|
|
94
|
-
batch_mean, batch_std, running_mean, running_std = self.bn_train(x, mean, variance, global_step)
|
|
95
|
-
else:
|
|
96
|
-
batch_mean, batch_std, running_mean, running_std = self.bn_infer(x, mean, variance, global_step)
|
|
97
|
-
else:
|
|
98
|
-
if self.training:
|
|
99
|
-
x_sum, x_square_sum = self.bn_reduce(x)
|
|
100
|
-
_, batch_mean, batch_std, running_mean, running_std, mean_updated, variance_updated = \
|
|
101
|
-
self.bn_update(x, x_sum, x_square_sum, mean, variance)
|
|
102
|
-
P.Assign()(mean, mean_updated)
|
|
103
|
-
P.Assign()(variance, variance_updated)
|
|
104
|
-
else:
|
|
105
|
-
batch_mean = P.ZerosLike()(variance)
|
|
106
|
-
batch_std = P.OnesLike()(variance)
|
|
107
|
-
running_mean = P.Add()(mean, 0.)
|
|
108
|
-
running_std = P.Sqrt()(P.Add()(variance, self.epsilon))
|
|
109
|
-
return batch_mean, batch_std, running_mean, running_std
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def _partial_init(cls_or_self, **kwargs):
|
|
113
|
-
"""
|
|
114
|
-
Wrapper that allows creation of class factories.
|
|
115
|
-
|
|
116
|
-
This can be useful when there is a need to create classes with the same
|
|
117
|
-
constructor arguments, but different instances.
|
|
118
|
-
|
|
119
|
-
Examples:
|
|
120
|
-
>>> class Foo:
|
|
121
|
-
... def __init__(self, a, b, answer):
|
|
122
|
-
... pass
|
|
123
|
-
>>> Foo.partial_init = classmethod(_partial_init)
|
|
124
|
-
>>> foo_builder = Foo.partial_init(a=3, b=4).partial_init(answer=42)
|
|
125
|
-
>>> foo_instance1 = foo_builder()
|
|
126
|
-
>>> foo_instance2 = foo_builder()
|
|
127
|
-
>>> result = (id(foo_instance1) == id(foo_instance2))
|
|
128
|
-
>>> print(result)
|
|
129
|
-
False
|
|
130
|
-
"""
|
|
131
|
-
|
|
132
|
-
class _PartialWrapper:
|
|
133
|
-
r"""
|
|
134
|
-
class of wrapper that allows creation of class factories.
|
|
135
|
-
"""
|
|
136
|
-
|
|
137
|
-
partial_init = _partial_init
|
|
138
|
-
|
|
139
|
-
def __init__(self, p):
|
|
140
|
-
self.p = p
|
|
141
|
-
|
|
142
|
-
def __call__(self, *args, **keywords):
|
|
143
|
-
return self.p(*args, **keywords)
|
|
144
|
-
|
|
145
|
-
def __repr__(self):
|
|
146
|
-
return self.p.__repr__()
|
|
147
|
-
|
|
148
|
-
r = _PartialWrapper(partial(cls_or_self, **kwargs))
|
|
149
|
-
return r
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
class _Observer(Cell):
|
|
153
|
-
"""
|
|
154
|
-
Base class of Observer. Observer is used to calculate the statistics of specific layer.
|
|
155
|
-
|
|
156
|
-
Notes:
|
|
157
|
-
This class is an abstract class.
|
|
158
|
-
|
|
159
|
-
Args:
|
|
160
|
-
quant_dtype (QuantDtype): The type of FakeQuant data.
|
|
161
|
-
"""
|
|
162
|
-
|
|
163
|
-
partial_init = classmethod(_partial_init)
|
|
164
|
-
|
|
165
|
-
def __init__(self, quant_dtype):
|
|
166
|
-
"""Initialize _Observer."""
|
|
167
|
-
super(_Observer, self).__init__()
|
|
168
|
-
self.quant_dtype = quant_dtype
|
|
169
|
-
|
|
170
|
-
def extend_repr(self):
|
|
171
|
-
s = f"quant_dtype={self.quant_dtype}"
|
|
172
|
-
return s
|
|
173
|
-
|
|
174
|
-
def construct(self):
|
|
175
|
-
pass
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
class UniformQuantObserver(_Observer):
|
|
179
|
-
"""
|
|
180
|
-
The base class of Uniform Quantization Observer.
|
|
181
|
-
|
|
182
|
-
Args:
|
|
183
|
-
quant_dtype (QuantDtype): The type of FakeQuant data. Default: QuantDtype.INT8.
|
|
184
|
-
per_channel (bool): Quantization granularity based on layer or on channel. Default: False.
|
|
185
|
-
symmetric (bool): Whether the quantization algorithm is symmetric or not. Default: False.
|
|
186
|
-
narrow_range (bool): Whether the quantization algorithm uses narrow range or not. Default: False.
|
|
187
|
-
num_channels (int): declarate the min and max channel size, Default: 1.
|
|
188
|
-
|
|
189
|
-
Returns:
|
|
190
|
-
Tensor.
|
|
191
|
-
"""
|
|
192
|
-
|
|
193
|
-
min_max_map = {
|
|
194
|
-
QuantDtype.INT2: (-2, 1),
|
|
195
|
-
QuantDtype.INT3: (-4, 3),
|
|
196
|
-
QuantDtype.INT4: (-8, 7),
|
|
197
|
-
QuantDtype.INT5: (-16, 15),
|
|
198
|
-
QuantDtype.INT6: (-32, 31),
|
|
199
|
-
QuantDtype.INT7: (-64, 63),
|
|
200
|
-
QuantDtype.INT8: (-128, 127),
|
|
201
|
-
|
|
202
|
-
QuantDtype.UINT2: (0, 3),
|
|
203
|
-
QuantDtype.UINT3: (0, 7),
|
|
204
|
-
QuantDtype.UINT4: (0, 15),
|
|
205
|
-
QuantDtype.UINT5: (0, 31),
|
|
206
|
-
QuantDtype.UINT6: (0, 63),
|
|
207
|
-
QuantDtype.UINT7: (0, 127),
|
|
208
|
-
QuantDtype.UINT8: (0, 255)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
def __init__(self, quant_dtype=QuantDtype.INT8, per_channel=False, symmetric=False, narrow_range=False,
|
|
212
|
-
num_channels=1):
|
|
213
|
-
"""Initialize UniformQuantObserver."""
|
|
214
|
-
super(UniformQuantObserver, self).__init__(quant_dtype)
|
|
215
|
-
self.per_channel = per_channel
|
|
216
|
-
self.symmetric = symmetric
|
|
217
|
-
self.narrow_range = narrow_range
|
|
218
|
-
self.num_channels = num_channels
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
class FakeQuantWithMinMaxObserver(UniformQuantObserver):
|
|
222
|
-
r"""
|
|
223
|
-
Quantization aware operation which provides the fake quantization observer function on data with min and max.
|
|
224
|
-
|
|
225
|
-
The detail of the quantization mode `DEFAULT` is described as below:
|
|
226
|
-
|
|
227
|
-
The running min/max :math:`x_{min}` and :math:`x_{max}` are computed as:
|
|
228
|
-
|
|
229
|
-
.. math::
|
|
230
|
-
|
|
231
|
-
\begin{array}{ll} \\
|
|
232
|
-
x_{min} =
|
|
233
|
-
\begin{cases}
|
|
234
|
-
\min(\min(X), 0)
|
|
235
|
-
& \text{ if } ema = \text{False} \\
|
|
236
|
-
\min((1 - c) \min(X) + \text{c } x_{min}, 0)
|
|
237
|
-
& \text{ if } \text{otherwise}
|
|
238
|
-
\end{cases}\\
|
|
239
|
-
x_{max} =
|
|
240
|
-
\begin{cases}
|
|
241
|
-
\max(\max(X), 0)
|
|
242
|
-
& \text{ if } ema = \text{False} \\
|
|
243
|
-
\max((1 - c) \max(X) + \text{c } x_{max}, 0)
|
|
244
|
-
& \text{ if } \text{otherwise}
|
|
245
|
-
\end{cases}
|
|
246
|
-
\end{array}
|
|
247
|
-
|
|
248
|
-
where X is the input tensor, and :math:`c` is the `ema_decay`.
|
|
249
|
-
|
|
250
|
-
The scale and zero point zp is computed as:
|
|
251
|
-
|
|
252
|
-
.. math::
|
|
253
|
-
|
|
254
|
-
\begin{array}{ll} \\
|
|
255
|
-
scale =
|
|
256
|
-
\begin{cases}
|
|
257
|
-
\frac{x_{max} - x_{min}}{Q_{max} - Q_{min}}
|
|
258
|
-
& \text{ if } symmetric = \text{False} \\
|
|
259
|
-
\frac{2\max(x_{max}, \left | x_{min} \right |) }{Q_{max} - Q_{min}}
|
|
260
|
-
& \text{ if } \text{otherwise}
|
|
261
|
-
\end{cases}\\
|
|
262
|
-
zp\_min = Q_{min} - \frac{x_{min}}{scale} \\
|
|
263
|
-
zp = \left \lfloor \min(Q_{max}, \max(Q_{min}, zp\_min)) + 0.5 \right \rfloor
|
|
264
|
-
\end{array}
|
|
265
|
-
|
|
266
|
-
where :math:`Q_{max}` and :math:`Q_{min}` is decided by quant_dtype, for example, if quant_dtype=INT8,
|
|
267
|
-
then :math:`Q_{max} = 127` and :math:`Q_{min} = -128`.
|
|
268
|
-
|
|
269
|
-
The fake quant output is computed as:
|
|
270
|
-
|
|
271
|
-
.. math::
|
|
272
|
-
|
|
273
|
-
\begin{array}{ll} \\
|
|
274
|
-
u_{min} = (Q_{min} - zp) * scale \\
|
|
275
|
-
u_{max} = (Q_{max} - zp) * scale \\
|
|
276
|
-
u_X = \left \lfloor \frac{\min(u_{max}, \max(u_{min}, X)) - u_{min}}{scale}
|
|
277
|
-
+ 0.5 \right \rfloor \\
|
|
278
|
-
output = u_X * scale + u_{min}
|
|
279
|
-
\end{array}
|
|
280
|
-
|
|
281
|
-
The detail of the quantization mode `LEARNED_SCALE` is described as below:
|
|
282
|
-
|
|
283
|
-
The fake quant output is computed as:
|
|
284
|
-
|
|
285
|
-
.. math::
|
|
286
|
-
|
|
287
|
-
\bar{X}=\left\{\begin{matrix}
|
|
288
|
-
clip\left ( \frac{X}{maxq},0,1\right ) \qquad \quad if\quad neg\_trunc\\
|
|
289
|
-
clip\left ( \frac{X}{maxq},-1,1\right )\qquad \ if\quad otherwise
|
|
290
|
-
\end{matrix}\right. \\
|
|
291
|
-
|
|
292
|
-
output=\frac{floor\left ( \bar{X}\ast Q_{max}+0.5 \right ) \ast scale }{Q_{max}}
|
|
293
|
-
|
|
294
|
-
where X is the input tensor.
|
|
295
|
-
where :math:`Q_{max}` (quant_max) is decided by quant_dtype and neg_trunc, for example, if quant_dtype=INT8
|
|
296
|
-
and neg_trunc works, :math:`Q_{max} = 256` , otherwise :math:`Q_{max} = 127`.
|
|
297
|
-
|
|
298
|
-
The maxq is updated by training, and its gradient is calculated as follows:
|
|
299
|
-
|
|
300
|
-
.. math::
|
|
301
|
-
|
|
302
|
-
\frac{\partial \ output}{\partial \ maxq} = \left\{\begin{matrix}
|
|
303
|
-
-\frac{X}{maxq}+\left \lfloor \frac{X}{maxq} \right \rceil \qquad if\quad bound_{lower}< \frac{X}{maxq}< 1\\
|
|
304
|
-
-1 \qquad \quad \qquad \quad if\quad \frac{X}{maxq}\le bound_{lower}\\
|
|
305
|
-
1 \qquad \quad \qquad \quad if\quad \frac{X}{maxq}\ge 1 \qquad \quad
|
|
306
|
-
\end{matrix}\right. \\
|
|
307
|
-
|
|
308
|
-
bound_{lower}=
|
|
309
|
-
\left\{\begin{matrix}
|
|
310
|
-
0\qquad \quad if\quad neg\_trunc\\
|
|
311
|
-
-1\qquad if\quad otherwise
|
|
312
|
-
\end{matrix}\right.
|
|
313
|
-
|
|
314
|
-
Then minq is computed as:
|
|
315
|
-
|
|
316
|
-
.. math::
|
|
317
|
-
|
|
318
|
-
minq=\left\{\begin{matrix}
|
|
319
|
-
0 \qquad \qquad \quad if\quad neg\_trunc\\
|
|
320
|
-
-maxq\qquad if\quad otherwise
|
|
321
|
-
\end{matrix}\right.
|
|
322
|
-
|
|
323
|
-
When exporting, the scale and zero point zp is computed as:
|
|
324
|
-
|
|
325
|
-
.. math::
|
|
326
|
-
|
|
327
|
-
scale=\frac{maxq}{quant\_max} ,\quad zp=0 \\
|
|
328
|
-
|
|
329
|
-
zp is equal to 0 consistently, due to the LEARNED_SCALE`s symmetric nature.
|
|
330
|
-
|
|
331
|
-
Args:
|
|
332
|
-
min_init (int, float, list): The initialized min value. Default: -6.
|
|
333
|
-
max_init (int, float, list): The initialized max value. Default: 6.
|
|
334
|
-
ema (bool): The exponential Moving Average algorithm updates min and max. Default: False.
|
|
335
|
-
ema_decay (float): Exponential Moving Average algorithm parameter. Default: 0.999.
|
|
336
|
-
per_channel (bool): Quantization granularity based on layer or on channel. Default: False.
|
|
337
|
-
channel_axis (int): Quantization by channel axis. Default: 1.
|
|
338
|
-
num_channels (int): declarate the min and max channel size, Default: 1.
|
|
339
|
-
quant_dtype (QuantDtype): The datatype of quantization, supporting 4 and 8bits. Default: QuantDtype.INT8.
|
|
340
|
-
symmetric (bool): Whether the quantization algorithm is symmetric or not. Default: False.
|
|
341
|
-
narrow_range (bool): Whether the quantization algorithm uses narrow range or not. Default: False.
|
|
342
|
-
quant_delay (int): Quantization delay parameters according to the global step. Default: 0.
|
|
343
|
-
neg_trunc (bool): Whether the quantization algorithm uses negative truncation or not. Default: False.
|
|
344
|
-
mode (str): Optional quantization mode, currently only `DEFAULT`(QAT) and `LEARNED_SCALE` are supported.
|
|
345
|
-
Default: ("DEFAULT")
|
|
346
|
-
Inputs:
|
|
347
|
-
- **x** (Tensor) - The input of FakeQuantWithMinMaxObserver. The input dimension is preferably 2D or 4D.
|
|
348
|
-
|
|
349
|
-
Outputs:
|
|
350
|
-
Tensor, with the same type and shape as the `x`.
|
|
351
|
-
|
|
352
|
-
Raises:
|
|
353
|
-
TypeError: If `min_init` or `max_init` is not int, float or list.
|
|
354
|
-
TypeError: If `quant_delay` is not an int.
|
|
355
|
-
ValueError: If `quant_delay` is less than 0.
|
|
356
|
-
ValueError: If `min_init` is not less than `max_init`.
|
|
357
|
-
ValueError: If `mode` is neither `DEFAULT` nor `LEARNED_SCALE`.
|
|
358
|
-
ValueError: If `mode` is `LEARNED_SCALE` and `symmetric` is not `True`.
|
|
359
|
-
ValueError: If `mode` is `LEARNED_SCALE`, and `narrow_range` is not `True` unless when `neg_trunc` is `True`.
|
|
360
|
-
|
|
361
|
-
Supported Platforms:
|
|
362
|
-
``Ascend`` ``GPU``
|
|
363
|
-
|
|
364
|
-
Examples:
|
|
365
|
-
>>> import mindspore
|
|
366
|
-
>>> from mindspore import Tensor
|
|
367
|
-
>>> fake_quant = nn.FakeQuantWithMinMaxObserver()
|
|
368
|
-
>>> x = Tensor(np.array([[1, 2, 1], [-2, 0, -1]]), mindspore.float32)
|
|
369
|
-
>>> result = fake_quant(x)
|
|
370
|
-
>>> print(result)
|
|
371
|
-
[[ 0.9882355 1.9764705 0.9882355]
|
|
372
|
-
[-1.9764705 0. -0.9882355]]
|
|
373
|
-
"""
|
|
374
|
-
|
|
375
|
-
def __init__(self,
|
|
376
|
-
min_init=-6,
|
|
377
|
-
max_init=6,
|
|
378
|
-
ema=False,
|
|
379
|
-
ema_decay=0.999,
|
|
380
|
-
per_channel=False,
|
|
381
|
-
channel_axis=1,
|
|
382
|
-
num_channels=1,
|
|
383
|
-
quant_dtype=QuantDtype.INT8,
|
|
384
|
-
symmetric=False,
|
|
385
|
-
narrow_range=False,
|
|
386
|
-
quant_delay=0,
|
|
387
|
-
neg_trunc=False,
|
|
388
|
-
mode="DEFAULT"):
|
|
389
|
-
"""Initialize FakeQuantWithMinMaxObserver"""
|
|
390
|
-
super(FakeQuantWithMinMaxObserver, self).__init__(quant_dtype=quant_dtype, per_channel=per_channel,
|
|
391
|
-
symmetric=symmetric, narrow_range=narrow_range,
|
|
392
|
-
num_channels=num_channels)
|
|
393
|
-
Validator.check_value_type("min_init", min_init, [int, float, list], type(self).__name__)
|
|
394
|
-
Validator.check_value_type("max_init", max_init, [int, float, list], type(self).__name__)
|
|
395
|
-
Validator.check_non_negative_int(quant_delay, 'quant_delay', self.cls_name)
|
|
396
|
-
self.min_init = min_init
|
|
397
|
-
self.max_init = max_init
|
|
398
|
-
self.quant_dtype = quant_dtype
|
|
399
|
-
self.num_bits = quant_dtype.num_bits
|
|
400
|
-
self.ema = ema
|
|
401
|
-
self.ema_decay = ema_decay
|
|
402
|
-
self.per_channel = per_channel
|
|
403
|
-
self.num_channels = num_channels
|
|
404
|
-
self.channel_axis = channel_axis
|
|
405
|
-
self.quant_delay = quant_delay
|
|
406
|
-
self.symmetric = symmetric
|
|
407
|
-
self.narrow_range = narrow_range
|
|
408
|
-
self.neg_trunc = neg_trunc
|
|
409
|
-
self.mode = mode
|
|
410
|
-
self.is_ascend = context.get_context('device_target') == "Ascend"
|
|
411
|
-
self.Neg = P.Neg()
|
|
412
|
-
|
|
413
|
-
min_array = self._get_init_array(self.min_init)
|
|
414
|
-
max_array = self._get_init_array(self.max_init)
|
|
415
|
-
if not np.greater(max_array, min_array).all():
|
|
416
|
-
raise ValueError(f"For '{self.cls_name}', the 'max_init' must be greater than 'min_init', "
|
|
417
|
-
f"but got 'max_init': {max_init}, 'min_init': {min_init}.")
|
|
418
|
-
if self.mode == "DEFAULT":
|
|
419
|
-
self._default_init(min_array, max_array)
|
|
420
|
-
elif self.mode == "LEARNED_SCALE":
|
|
421
|
-
self._learned_scale_init(min_array, max_array)
|
|
422
|
-
else:
|
|
423
|
-
raise ValueError(f"For '{self.cls_name}', only `DEFAULT` and `LEARNED_SCALE` mode are valid, but got "
|
|
424
|
-
f"'mode': {self.mode}.")
|
|
425
|
-
|
|
426
|
-
def reset(self, quant_dtype=QuantDtype.INT8, min_init=-6, max_init=6):
|
|
427
|
-
r"""
|
|
428
|
-
Reset the quant max parameter (eg. 256) and the initial value of the minq parameter and maxq parameter,
|
|
429
|
-
this function is currently only valid for `LEARNED_SCALE` mode.
|
|
430
|
-
|
|
431
|
-
Args:
|
|
432
|
-
quant_dtype (QuantDtype): The datatype of quantization, supporting 4 and 8bits. Default: QuantDtype.INT8.
|
|
433
|
-
min_init (int, float, list): The initialized min value. Default: -6.
|
|
434
|
-
max_init (int, float, list): The initialized max value. Default: 6.
|
|
435
|
-
"""
|
|
436
|
-
if self.mode == "LEARNED_SCALE":
|
|
437
|
-
self.quant_dtype = quant_dtype
|
|
438
|
-
self.num_bits = quant_dtype.num_bits
|
|
439
|
-
self._calculate_quant_max()
|
|
440
|
-
if self.neg_trunc:
|
|
441
|
-
min_init = 0
|
|
442
|
-
|
|
443
|
-
self.min_init = min_init
|
|
444
|
-
self.max_init = max_init
|
|
445
|
-
min_array = self._get_init_array(self.min_init)
|
|
446
|
-
max_array = self._get_init_array(self.max_init)
|
|
447
|
-
if not np.greater(max_array, min_array).all():
|
|
448
|
-
raise ValueError(f"For '{self.cls_name}', the 'max_init' must be greater than 'min_init', "
|
|
449
|
-
f"but got 'max_init': {max_init}, 'min_init': {min_init}.")
|
|
450
|
-
|
|
451
|
-
self.minq.set_data(Tensor(min_array))
|
|
452
|
-
self.maxq.set_data(Tensor(max_array))
|
|
453
|
-
self.quant_max.set_data(Tensor(np.array([self._quant_max]).astype(np.float32)))
|
|
454
|
-
else:
|
|
455
|
-
raise ValueError(f"For '{self.cls_name}', only `LEARNED_SCALE` mode is valid, but got 'mode': {self.mode}.")
|
|
456
|
-
|
|
457
|
-
def _default_init(self, min_array, max_array):
|
|
458
|
-
"""
|
|
459
|
-
Initialization of `DEFAULT`(QAT) mode.
|
|
460
|
-
"""
|
|
461
|
-
# init tensor min and max for fake quantized operation
|
|
462
|
-
self.minq = Parameter(Tensor(min_array), name='quant_min', requires_grad=False)
|
|
463
|
-
self.maxq = Parameter(Tensor(max_array), name='quant_max', requires_grad=False)
|
|
464
|
-
|
|
465
|
-
# init fake quant relative op
|
|
466
|
-
if self.per_channel:
|
|
467
|
-
quant_fun = partial(Q.FakeQuantPerChannel, channel_axis=self.channel_axis)
|
|
468
|
-
ema_fun = partial(Q.MinMaxUpdatePerChannel, channel_axis=self.channel_axis)
|
|
469
|
-
else:
|
|
470
|
-
quant_fun = Q.FakeQuantPerLayer
|
|
471
|
-
ema_fun = Q.MinMaxUpdatePerLayer
|
|
472
|
-
|
|
473
|
-
self.ema_update = ema_fun(ema=self.ema, ema_decay=self.ema_decay)
|
|
474
|
-
if self.is_ascend:
|
|
475
|
-
self.fake_quant_train = quant_fun(num_bits=self.quant_dtype.num_bits,
|
|
476
|
-
symmetric=self.symmetric,
|
|
477
|
-
narrow_range=self.narrow_range,
|
|
478
|
-
quant_delay=self.quant_delay)
|
|
479
|
-
self.fake_quant_infer = self.fake_quant_train
|
|
480
|
-
else:
|
|
481
|
-
quant_fun = partial(quant_fun,
|
|
482
|
-
ema=self.ema,
|
|
483
|
-
ema_decay=self.ema_decay,
|
|
484
|
-
num_bits=self.quant_dtype.num_bits,
|
|
485
|
-
symmetric=self.symmetric,
|
|
486
|
-
narrow_range=self.narrow_range,
|
|
487
|
-
quant_delay=self.quant_delay)
|
|
488
|
-
self.fake_quant_train = quant_fun(training=True)
|
|
489
|
-
self.fake_quant_infer = quant_fun(training=False)
|
|
490
|
-
|
|
491
|
-
def _learned_scale_init(self, min_array, max_array):
|
|
492
|
-
"""
|
|
493
|
-
Initialization of `LEARNED_SCALE` mode.
|
|
494
|
-
"""
|
|
495
|
-
if not self.symmetric:
|
|
496
|
-
raise ValueError(f"For '{self.cls_name}', the 'LEARNED_SCALE' mode only support 'symmetric' quant, "
|
|
497
|
-
f"but got 'symmetric': {self.symmetric}. Please set 'symmetric' to True.")
|
|
498
|
-
if self.neg_trunc:
|
|
499
|
-
min_array = self._get_init_array(0)
|
|
500
|
-
if self.narrow_range:
|
|
501
|
-
raise ValueError(f"For '{self.cls_name}', the 'LEARNED_SCALE' mode only support the combination of "
|
|
502
|
-
f"'neg_trunc=True and narrow_range=False' config scenario, but got 'narrow_range': "
|
|
503
|
-
f"{self.narrow_range}.")
|
|
504
|
-
elif not self.narrow_range:
|
|
505
|
-
raise ValueError(f"For '{self.cls_name}', the 'LEARNED_SCALE' mode only support 'narrow_range=True' "
|
|
506
|
-
f"config, except for 'neg_trunc=True' scenario. But got 'narrow_range': "
|
|
507
|
-
f"{self.narrow_range}.")
|
|
508
|
-
|
|
509
|
-
self._calculate_quant_max()
|
|
510
|
-
|
|
511
|
-
self.minq = Parameter(Tensor(min_array), name='minq')
|
|
512
|
-
self.maxq = Parameter(Tensor(max_array), name='maxq')
|
|
513
|
-
self.quant_max = Parameter(Tensor(np.array([self._quant_max]).astype(np.float32)),
|
|
514
|
-
name="quant_max", requires_grad=False)
|
|
515
|
-
|
|
516
|
-
# init fake quant relative op
|
|
517
|
-
if self.per_channel:
|
|
518
|
-
quant_fun = partial(Q.FakeLearnedScaleQuantPerChannel, channel_axis=self.channel_axis)
|
|
519
|
-
else:
|
|
520
|
-
quant_fun = Q.FakeLearnedScaleQuantPerLayer
|
|
521
|
-
|
|
522
|
-
quant_fun = partial(quant_fun,
|
|
523
|
-
quant_delay=self.quant_delay,
|
|
524
|
-
neg_trunc=self.neg_trunc)
|
|
525
|
-
self.fake_quant_train = quant_fun(training=True)
|
|
526
|
-
self.fake_quant_infer = quant_fun(training=False)
|
|
527
|
-
|
|
528
|
-
def _get_init_array(self, init_date):
|
|
529
|
-
"""
|
|
530
|
-
Convert the initial value to array.
|
|
531
|
-
"""
|
|
532
|
-
if isinstance(init_date, list) and self.per_channel and len(init_date) != self.num_channels:
|
|
533
|
-
raise ValueError(f"For '{self.cls_name}', the length of 'min_init/max_init' list must be equal to "
|
|
534
|
-
f"'num_channels' for perchannel quant scenario, but got 'min_init/max_init': {init_date} "
|
|
535
|
-
f"and num_channels: {self.num_channels}.")
|
|
536
|
-
if isinstance(init_date, list) and not self.per_channel and len(init_date) != 1:
|
|
537
|
-
raise ValueError(f"For '{self.cls_name}', the length of the 'min_init/max_init' list must be 1 for "
|
|
538
|
-
f"perlayer quant scenario, but got {len(init_date)}.")
|
|
539
|
-
|
|
540
|
-
if isinstance(init_date, list):
|
|
541
|
-
min_max_array = np.array(init_date).astype(np.float32)
|
|
542
|
-
elif self.per_channel and not isinstance(init_date, list):
|
|
543
|
-
min_max_array = np.array([init_date] * self.num_channels).astype(np.float32)
|
|
544
|
-
else:
|
|
545
|
-
min_max_array = np.array([init_date]).astype(np.float32)
|
|
546
|
-
return min_max_array
|
|
547
|
-
|
|
548
|
-
def _calculate_quant_max(self):
|
|
549
|
-
"""
|
|
550
|
-
The quantization range is calculated according to num_bits.
|
|
551
|
-
"""
|
|
552
|
-
if not self.neg_trunc:
|
|
553
|
-
self._quant_max = (1 << (self.num_bits - 1)) - 1
|
|
554
|
-
else:
|
|
555
|
-
self._quant_max = (1 << self.num_bits) - 1
|
|
556
|
-
|
|
557
|
-
def extend_repr(self):
|
|
558
|
-
"""Display instance object as string."""
|
|
559
|
-
s = 'quant_dtype={}, symmetric={}, narrow_range={}, ema={}({}), per_channel={}({}, {}), ' \
|
|
560
|
-
'quant_delay={}, min_init={}, max_init={}'.format(self.quant_dtype, self.symmetric, self.narrow_range,
|
|
561
|
-
self.ema, self.ema_decay, self.per_channel,
|
|
562
|
-
self.channel_axis, self.num_channels, self.quant_delay,
|
|
563
|
-
self.min_init, self.max_init)
|
|
564
|
-
return s
|
|
565
|
-
|
|
566
|
-
def construct(self, x):
|
|
567
|
-
if self.mode == "LEARNED_SCALE":
|
|
568
|
-
if self.training:
|
|
569
|
-
out = self.fake_quant_train(x, self.maxq, self.quant_max)
|
|
570
|
-
if not self.neg_trunc:
|
|
571
|
-
self.minq = self.Neg(self.maxq)
|
|
572
|
-
else:
|
|
573
|
-
out = self.fake_quant_infer(x, self.maxq, self.quant_max)
|
|
574
|
-
else:
|
|
575
|
-
if self.training:
|
|
576
|
-
min_up, max_up = self.ema_update(x, self.minq, self.maxq)
|
|
577
|
-
self.minq = min_up
|
|
578
|
-
self.maxq = max_up
|
|
579
|
-
out = self.fake_quant_train(x, self.minq, self.maxq)
|
|
580
|
-
else:
|
|
581
|
-
out = self.fake_quant_infer(x, self.minq, self.maxq)
|
|
582
|
-
return out
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
QuantConfig = namedtuple("QuantConfig", ['weight', 'activation'])
|
|
586
|
-
|
|
587
|
-
quant_config_default = QuantConfig(weight=FakeQuantWithMinMaxObserver.partial_init(),
|
|
588
|
-
activation=FakeQuantWithMinMaxObserver.partial_init())
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
class Conv2dBnFoldQuantOneConv(Cell):
|
|
592
|
-
r"""
|
|
593
|
-
2D convolution which use the convolution layer statistics once to calculate Batch Normalization
|
|
594
|
-
operation folded construct.
|
|
595
|
-
|
|
596
|
-
This part is a more detailed overview of Conv2d operation. For more details about Quantization,
|
|
597
|
-
please refer to the implementation of class of `FakeQuantWithMinMaxObserver`,
|
|
598
|
-
:class:`FakeQuantWithMinMaxObserver`.
|
|
599
|
-
|
|
600
|
-
.. math::
|
|
601
|
-
w_{q}=quant(\frac{w}{\sqrt{var_{G}+\epsilon}}*\gamma )
|
|
602
|
-
|
|
603
|
-
b=\frac{-\mu _{G} }{\sqrt{var_{G}+\epsilon }}*\gamma +\beta
|
|
604
|
-
|
|
605
|
-
y=w_{q}\times x+b
|
|
606
|
-
|
|
607
|
-
where :math:`quant` is the continuous execution of quant and dequant, you can refer to the implementation of
|
|
608
|
-
subclass of `FakeQuantWithMinMaxObserver`, :class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
609
|
-
`mu _{G}` and `var_{G}` represent the global mean and variance respectively.
|
|
610
|
-
|
|
611
|
-
Args:
|
|
612
|
-
in_channels (int): The number of input channel :math:`C_{in}`.
|
|
613
|
-
out_channels (int): The number of output channel :math:`C_{out}`.
|
|
614
|
-
kernel_size (Union[int, tuple[int]]): Specifies the height and width of the 2D convolution window.
|
|
615
|
-
stride (Union[int, tuple[int]]): Specifies stride for all spatial dimensions with the same value. Default: 1.
|
|
616
|
-
pad_mode (str): Specifies padding mode. The optional values are "same", "valid", "pad". Default: "same".
|
|
617
|
-
padding (Union[int, tuple[int]]): Implicit paddings on both sides of the `x`. Default: 0.
|
|
618
|
-
dilation (Union[int, tuple[int]]): Specifies the dilation rate to use for dilated convolution. Default: 1.
|
|
619
|
-
group (int): Splits filter into groups, `in_channels` and `out_channels` must be
|
|
620
|
-
divisible by the number of groups. Default: 1.
|
|
621
|
-
eps (float): Parameters for Batch Normalization. Default: 1e-5.
|
|
622
|
-
momentum (float): Parameters for Batch Normalization op. Default: 0.997.
|
|
623
|
-
has_bias (bool): Specifies whether the layer uses a bias vector, which is temporarily invalid. Default: False.
|
|
624
|
-
weight_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
625
|
-
convolution kernel. Default: 'normal'.
|
|
626
|
-
bias_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
627
|
-
bias vector. Default: 'zeros'.
|
|
628
|
-
beta_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
629
|
-
beta vector. Default: 'zeros'.
|
|
630
|
-
gamma_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
631
|
-
gamma vector. Default: 'ones'.
|
|
632
|
-
mean_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
633
|
-
mean vector. Default: 'zeros'.
|
|
634
|
-
var_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
635
|
-
variance vector. Default: 'ones'.
|
|
636
|
-
fake (bool): Whether Conv2dBnFoldQuant Cell adds FakeQuantWithMinMaxObserver. Default: True.
|
|
637
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
638
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
639
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
640
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
641
|
-
quant_dtype (QuantDtype): Specifies the FakeQuant datatype. Default: QuantDtype.INT8.
|
|
642
|
-
|
|
643
|
-
Inputs:
|
|
644
|
-
- **x** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
|
645
|
-
|
|
646
|
-
Outputs:
|
|
647
|
-
Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`.
|
|
648
|
-
|
|
649
|
-
Raises:
|
|
650
|
-
TypeError: If `in_channels`, `out_channels` or `group` is not an int.
|
|
651
|
-
TypeError: If `kernel_size`, `stride`, `padding` or `dilation` is neither an int nor a tuple.
|
|
652
|
-
TypeError: If `has_bias` or `fake` is not a bool.
|
|
653
|
-
TypeError: If `data_format` is not a string.
|
|
654
|
-
ValueError: If `in_channels`, `out_channels`, `kernel_size`, `stride` or `dilation` is less than 1.
|
|
655
|
-
ValueError: If `padding` is less than 0.
|
|
656
|
-
ValueError: If `pad_mode` is not one of 'same', 'valid', 'pad'.
|
|
657
|
-
|
|
658
|
-
Supported Platforms:
|
|
659
|
-
``Ascend`` ``GPU``
|
|
660
|
-
|
|
661
|
-
Examples:
|
|
662
|
-
>>> import mindspore
|
|
663
|
-
>>> from mindspore.compression import quant
|
|
664
|
-
>>> from mindspore import Tensor
|
|
665
|
-
>>> qconfig = quant.create_quant_config()
|
|
666
|
-
>>> conv2d_bnfold = nn.Conv2dBnFoldQuantOneConv(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
|
|
667
|
-
... weight_init="ones", quant_config=qconfig)
|
|
668
|
-
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
|
|
669
|
-
>>> result = conv2d_bnfold(x)
|
|
670
|
-
>>> print(result)
|
|
671
|
-
[[[[5.9296875 13.8359375]
|
|
672
|
-
[11.859375 17.78125]]]]
|
|
673
|
-
"""
|
|
674
|
-
|
|
675
|
-
def __init__(self,
|
|
676
|
-
in_channels,
|
|
677
|
-
out_channels,
|
|
678
|
-
kernel_size,
|
|
679
|
-
stride=1,
|
|
680
|
-
pad_mode='same',
|
|
681
|
-
padding=0,
|
|
682
|
-
dilation=1,
|
|
683
|
-
group=1,
|
|
684
|
-
eps=1e-5,
|
|
685
|
-
momentum=0.997,
|
|
686
|
-
has_bias=False,
|
|
687
|
-
weight_init='normal',
|
|
688
|
-
bias_init='zeros',
|
|
689
|
-
beta_init='zeros',
|
|
690
|
-
gamma_init='ones',
|
|
691
|
-
mean_init='zeros',
|
|
692
|
-
var_init='ones',
|
|
693
|
-
fake=True,
|
|
694
|
-
quant_config=quant_config_default,
|
|
695
|
-
quant_dtype=QuantDtype.INT8):
|
|
696
|
-
"""Initialize Conv2dBnFoldQuant layer"""
|
|
697
|
-
super(Conv2dBnFoldQuantOneConv, self).__init__()
|
|
698
|
-
self.in_channels = Validator.check_positive_int(in_channels, "in_channels", self.cls_name)
|
|
699
|
-
self.out_channels = Validator.check_positive_int(out_channels, "out_channels", self.cls_name)
|
|
700
|
-
self.kernel_size = twice(kernel_size)
|
|
701
|
-
self.stride = twice(stride)
|
|
702
|
-
self.dilation = twice(dilation)
|
|
703
|
-
for kernel_size_elem in self.kernel_size:
|
|
704
|
-
Validator.check_positive_int(kernel_size_elem, 'kernel_size item', self.cls_name)
|
|
705
|
-
for stride_elem in self.stride:
|
|
706
|
-
Validator.check_positive_int(stride_elem, 'stride item', self.cls_name)
|
|
707
|
-
for dilation_elem in self.dilation:
|
|
708
|
-
Validator.check_positive_int(dilation_elem, 'dilation item', self.cls_name)
|
|
709
|
-
if pad_mode not in ('valid', 'same', 'pad'):
|
|
710
|
-
raise ValueError(f"For '{self.cls_name}', the 'pad_mode' must be one of values "
|
|
711
|
-
f"in ('valid', 'same', 'pad'), but got {pad_mode}.")
|
|
712
|
-
self.pad_mode = pad_mode
|
|
713
|
-
if isinstance(padding, int):
|
|
714
|
-
Validator.check_non_negative_int(padding, 'padding', self.cls_name)
|
|
715
|
-
self.padding = padding
|
|
716
|
-
elif isinstance(padding, tuple):
|
|
717
|
-
for pad in padding:
|
|
718
|
-
Validator.check_non_negative_int(pad, 'padding item', self.cls_name)
|
|
719
|
-
self.padding = padding
|
|
720
|
-
else:
|
|
721
|
-
raise TypeError(f"For '{self.cls_name}', the type of 'padding' must be int/tuple(int), but got "
|
|
722
|
-
f"{type(padding).__name__}!")
|
|
723
|
-
self.group = Validator.check_positive_int(group, "group", self.cls_name)
|
|
724
|
-
self.eps = eps
|
|
725
|
-
self.momentum = 1 - momentum
|
|
726
|
-
self.has_bias = has_bias
|
|
727
|
-
self.fake = Validator.check_bool(fake, "fake", self.cls_name)
|
|
728
|
-
self.quant_config = quant_config
|
|
729
|
-
data_format = 'NCHW'
|
|
730
|
-
self.format = Validator.check_string(data_format, ['NCHW', 'NHWC'], 'format', self.cls_name)
|
|
731
|
-
self._target = context.get_context("device_target")
|
|
732
|
-
self.is_graph_mode = context.get_context("mode") == context.GRAPH_MODE
|
|
733
|
-
self.is_ge_backend = False
|
|
734
|
-
if context.get_context("enable_ge"):
|
|
735
|
-
self.is_ge_backend = True
|
|
736
|
-
self.enable_default_train = self.is_graph_mode and \
|
|
737
|
-
(self.is_ge_backend or self._target == "Ascend")
|
|
738
|
-
|
|
739
|
-
# initialize convolution op and Parameter
|
|
740
|
-
self.conv = P.Conv2D(out_channel=out_channels,
|
|
741
|
-
kernel_size=self.kernel_size,
|
|
742
|
-
pad_mode=pad_mode,
|
|
743
|
-
pad=padding,
|
|
744
|
-
stride=self.stride,
|
|
745
|
-
dilation=self.dilation,
|
|
746
|
-
group=group)
|
|
747
|
-
weight_shape = [out_channels, in_channels // group, *self.kernel_size]
|
|
748
|
-
channel_axis = 0
|
|
749
|
-
self.channel_axis = channel_axis
|
|
750
|
-
self.weight = Parameter(initializer(weight_init, weight_shape), name='weight')
|
|
751
|
-
self.bias_add = P.BiasAdd()
|
|
752
|
-
self.bias = None
|
|
753
|
-
if Validator.check_bool(has_bias, "has_bias", self.cls_name):
|
|
754
|
-
self.bias = Parameter(initializer(bias_init, [out_channels]), name='bias')
|
|
755
|
-
|
|
756
|
-
# initialize BatchNorm Parameter
|
|
757
|
-
self.gamma = Parameter(initializer(gamma_init, [out_channels]), name='gamma')
|
|
758
|
-
self.beta = Parameter(initializer(beta_init, [out_channels]), name='beta')
|
|
759
|
-
self.moving_mean = Parameter(initializer(mean_init, [out_channels]), name='moving_mean', requires_grad=False)
|
|
760
|
-
self.moving_variance = Parameter(initializer(var_init, [out_channels]), name='moving_variance',
|
|
761
|
-
requires_grad=False)
|
|
762
|
-
|
|
763
|
-
# initialize fake ops
|
|
764
|
-
self.fake_quant_weight = quant_config.weight(channel_axis=channel_axis,
|
|
765
|
-
num_channels=out_channels)
|
|
766
|
-
self.freeze_bn = False
|
|
767
|
-
self.bn_train = P.BatchNorm(is_training=True, epsilon=self.eps,
|
|
768
|
-
momentum=self.momentum, data_format=self.format)
|
|
769
|
-
|
|
770
|
-
self.bn_infer = P.BatchNorm(is_training=False, epsilon=self.eps, data_format=self.format)
|
|
771
|
-
self.sub_mean = P.Sub()
|
|
772
|
-
self.sub_var = P.Sub()
|
|
773
|
-
self.mul_mean = P.Mul()
|
|
774
|
-
self.mul_var = P.Mul()
|
|
775
|
-
self.assign_sub_mean = P.AssignSub()
|
|
776
|
-
self.assign_sub_var = P.AssignSub()
|
|
777
|
-
self.reshape = P.Reshape()
|
|
778
|
-
|
|
779
|
-
@classmethod
|
|
780
|
-
def from_float(cls, convbn: Conv2dBnAct, quant_config: QuantConfig):
|
|
781
|
-
"""
|
|
782
|
-
A class method to create `Conv2dBnFoldQuantOneConv` from a `Conv2dBnAct`
|
|
783
|
-
|
|
784
|
-
Examples:
|
|
785
|
-
>>> from mindspore import nn
|
|
786
|
-
>>> ic = 10
|
|
787
|
-
>>> oc = 100
|
|
788
|
-
>>> kernel_size = 3
|
|
789
|
-
>>> conv_bn_op = nn.Conv2dBnAct(ic, oc, kernel_size)
|
|
790
|
-
>>> # when apply QAT on `conv_bn_op`, QAT need to create a quant Conv2dBnAct whose weight is fake-quanted,
|
|
791
|
-
>>> quant_config: QuantConfig = QuantConfig(weight=FakeQuantWithMinMaxObserver.partial_init(),
|
|
792
|
-
... activation=FakeQuantWithMinMaxObserver.partial_init())
|
|
793
|
-
>>> conv_bn_quant = nn.Conv2dBnFoldQuantOneConv.from_float(conv_bn_op, quant_config)
|
|
794
|
-
"""
|
|
795
|
-
|
|
796
|
-
kwargs = {'in_channels': convbn.conv.in_channels,
|
|
797
|
-
'out_channels': convbn.conv.out_channels,
|
|
798
|
-
'kernel_size': convbn.conv.kernel_size,
|
|
799
|
-
'stride': convbn.conv.stride,
|
|
800
|
-
'pad_mode': convbn.conv.pad_mode,
|
|
801
|
-
'padding': convbn.conv.padding,
|
|
802
|
-
'dilation': convbn.conv.dilation,
|
|
803
|
-
'group': convbn.conv.group,
|
|
804
|
-
'has_bias': convbn.conv.has_bias,
|
|
805
|
-
'bias_init': convbn.conv.bias_init,
|
|
806
|
-
'weight_init': convbn.conv.weight_init,
|
|
807
|
-
'quant_config': quant_config,
|
|
808
|
-
'fake': True,
|
|
809
|
-
}
|
|
810
|
-
if hasattr(convbn, 'batchnorm'):
|
|
811
|
-
kwargs['eps'] = convbn.batchnorm.eps
|
|
812
|
-
kwargs['momentum'] = convbn.batchnorm.momentum
|
|
813
|
-
kwargs['beta_init'] = convbn.batchnorm.beta_init
|
|
814
|
-
kwargs['gamma_init'] = convbn.batchnorm.gamma_init
|
|
815
|
-
kwargs['mean_init'] = convbn.batchnorm.moving_mean_init
|
|
816
|
-
kwargs['var_init'] = convbn.batchnorm.moving_var_init
|
|
817
|
-
return cls(**kwargs)
|
|
818
|
-
|
|
819
|
-
def extend_repr(self):
|
|
820
|
-
"""Display instance object as string."""
|
|
821
|
-
s = 'in_channels={}, out_channels={}, kernel_size={}, stride={}, ' \
|
|
822
|
-
'pad_mode={}, padding={}, dilation={}, group={}, ' \
|
|
823
|
-
'fake={}, momentum={}'.format(self.in_channels, self.out_channels, self.kernel_size, self.stride,
|
|
824
|
-
self.pad_mode, self.padding, self.dilation, self.group, self.fake,
|
|
825
|
-
self.momentum)
|
|
826
|
-
return s
|
|
827
|
-
|
|
828
|
-
def construct(self, x):
|
|
829
|
-
running_std = P.Sqrt()(P.Add()(self.moving_variance, self.eps))
|
|
830
|
-
scale_factor = self.gamma / running_std
|
|
831
|
-
if self.channel_axis:
|
|
832
|
-
scale_factor = self.reshape(scale_factor, (1, -1, 1, 1))
|
|
833
|
-
else:
|
|
834
|
-
scale_factor = self.reshape(scale_factor, (-1, 1, 1, 1))
|
|
835
|
-
weight = self.weight * scale_factor
|
|
836
|
-
if self.fake:
|
|
837
|
-
weight = self.fake_quant_weight(weight)
|
|
838
|
-
conv = self.conv(x, weight)
|
|
839
|
-
|
|
840
|
-
if self.freeze_bn:
|
|
841
|
-
return conv + self.reshape((self.beta - self.gamma * self.moving_mean / running_std), (1, -1, 1, 1))
|
|
842
|
-
scale_factor = self.reshape(scale_factor, (1, -1, 1, 1))
|
|
843
|
-
if self.enable_default_train:
|
|
844
|
-
scale_factor = P.Reciprocal()(scale_factor)
|
|
845
|
-
conv_orig = conv * scale_factor
|
|
846
|
-
else:
|
|
847
|
-
conv_orig = conv / scale_factor
|
|
848
|
-
if self.training:
|
|
849
|
-
return self.bn_train(conv_orig,
|
|
850
|
-
self.gamma,
|
|
851
|
-
self.beta,
|
|
852
|
-
self.moving_mean,
|
|
853
|
-
self.moving_variance)[0]
|
|
854
|
-
|
|
855
|
-
return self.bn_infer(conv_orig,
|
|
856
|
-
self.gamma,
|
|
857
|
-
self.beta,
|
|
858
|
-
self.moving_mean,
|
|
859
|
-
self.moving_variance)[0]
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
class Conv2dBnFoldQuant(Cell):
|
|
863
|
-
r"""
|
|
864
|
-
2D convolution with Batch Normalization operation folded construct.
|
|
865
|
-
|
|
866
|
-
This part is a more detailed overview of Conv2d operation. For more details about Quantization,
|
|
867
|
-
please refer to the implementation of class of `FakeQuantWithMinMaxObserver`,
|
|
868
|
-
:class:`FakeQuantWithMinMaxObserver`.
|
|
869
|
-
|
|
870
|
-
.. math::
|
|
871
|
-
y = x\times w+ b
|
|
872
|
-
|
|
873
|
-
w_{q}=quant(\frac{w}{\sqrt{Var[y]+\epsilon}}*\gamma )
|
|
874
|
-
|
|
875
|
-
y_{out}= w_{q}\times x+\frac{b-E[y]}{\sqrt{Var[y]+\epsilon}}*\gamma +\beta
|
|
876
|
-
|
|
877
|
-
where :math:`quant` is the continuous execution of quant and dequant. Two convolution
|
|
878
|
-
and Batch Normalization operation are used here, the purpose of the first convolution and Batch Normalization
|
|
879
|
-
is to count the mean `E[y]` and variance `Var[y]` of current batch output for quantization.
|
|
880
|
-
|
|
881
|
-
Args:
|
|
882
|
-
in_channels (int): The number of input channel :math:`C_{in}`.
|
|
883
|
-
out_channels (int): The number of output channel :math:`C_{out}`.
|
|
884
|
-
kernel_size (Union[int, tuple[int]]): Specifies the height and width of the 2D convolution window.
|
|
885
|
-
stride (Union[int, tuple[int]]): Specifies stride for all spatial dimensions with the same value. Default: 1.
|
|
886
|
-
pad_mode (str): Specifies padding mode. The optional values are "same", "valid", "pad". Default: "same".
|
|
887
|
-
padding (Union[int, tuple[int]]): Implicit paddings on both sides of the `x`. Default: 0.
|
|
888
|
-
dilation (Union[int, tuple[int]]): Specifies the dilation rate to use for dilated convolution. Default: 1.
|
|
889
|
-
group (int): Splits filter into groups, `in_channels` and `out_channels` must be
|
|
890
|
-
divisible by the number of groups. Default: 1.
|
|
891
|
-
eps (float): Parameters for Batch Normalization. Default: 1e-5.
|
|
892
|
-
momentum (float): Parameters for Batch Normalization op. Default: 0.997.
|
|
893
|
-
has_bias (bool): Specifies whether the layer uses a bias vector. Default: False.
|
|
894
|
-
weight_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
895
|
-
convolution kernel. Default: 'normal'.
|
|
896
|
-
bias_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
897
|
-
bias vector. Default: 'zeros'.
|
|
898
|
-
beta_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
899
|
-
beta vector. Default: 'zeros'.
|
|
900
|
-
gamma_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
901
|
-
gamma vector. Default: 'ones'.
|
|
902
|
-
mean_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
903
|
-
mean vector. Default: 'zeros'.
|
|
904
|
-
var_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the
|
|
905
|
-
variance vector. Default: 'ones'.
|
|
906
|
-
fake (bool): Whether Conv2dBnFoldQuant Cell adds FakeQuantWithMinMaxObserver. Default: True.
|
|
907
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
908
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
909
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
910
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
911
|
-
quant_dtype (QuantDtype): Specifies the FakeQuant datatype. Default: QuantDtype.INT8.
|
|
912
|
-
freeze_bn (int): The quantization freeze Batch Normalization op is according to the global step.
|
|
913
|
-
Default: 100000.
|
|
914
|
-
|
|
915
|
-
Inputs:
|
|
916
|
-
- **x** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
|
917
|
-
|
|
918
|
-
Outputs:
|
|
919
|
-
Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`.
|
|
920
|
-
|
|
921
|
-
Raises:
|
|
922
|
-
TypeError: If `in_channels`, `out_channels` or `group` is not an int.
|
|
923
|
-
TypeError: If `kernel_size`, `stride`, `padding` or `dilation` is neither an int nor a tuple.
|
|
924
|
-
TypeError: If `has_bias` or `fake` is not a bool.
|
|
925
|
-
ValueError: If `in_channels`, `out_channels`, `kernel_size`, `stride` or `dilation` is less than 1.
|
|
926
|
-
ValueError: If `padding` is less than 0.
|
|
927
|
-
ValueError: If `pad_mode` is not one of 'same', 'valid', 'pad'.
|
|
928
|
-
|
|
929
|
-
Supported Platforms:
|
|
930
|
-
``Ascend`` ``GPU``
|
|
931
|
-
|
|
932
|
-
Examples:
|
|
933
|
-
>>> import mindspore
|
|
934
|
-
>>> from mindspore.compression import quant
|
|
935
|
-
>>> from mindspore import Tensor
|
|
936
|
-
>>> qconfig = quant.create_quant_config()
|
|
937
|
-
>>> conv2d_bnfold = nn.Conv2dBnFoldQuant(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
|
|
938
|
-
... weight_init="ones", quant_config=qconfig)
|
|
939
|
-
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
|
|
940
|
-
>>> result = conv2d_bnfold(x)
|
|
941
|
-
>>> print(result)
|
|
942
|
-
[[[[5.9296875 13.8359375]
|
|
943
|
-
[11.859375 17.78125]]]]
|
|
944
|
-
"""
|
|
945
|
-
|
|
946
|
-
def __init__(self,
|
|
947
|
-
in_channels,
|
|
948
|
-
out_channels,
|
|
949
|
-
kernel_size,
|
|
950
|
-
stride=1,
|
|
951
|
-
pad_mode='same',
|
|
952
|
-
padding=0,
|
|
953
|
-
dilation=1,
|
|
954
|
-
group=1,
|
|
955
|
-
eps=1e-5,
|
|
956
|
-
momentum=0.997,
|
|
957
|
-
has_bias=False,
|
|
958
|
-
weight_init='normal',
|
|
959
|
-
bias_init='zeros',
|
|
960
|
-
beta_init='zeros',
|
|
961
|
-
gamma_init='ones',
|
|
962
|
-
mean_init='zeros',
|
|
963
|
-
var_init='ones',
|
|
964
|
-
fake=True,
|
|
965
|
-
quant_config=quant_config_default,
|
|
966
|
-
quant_dtype=QuantDtype.INT8,
|
|
967
|
-
freeze_bn=100000):
|
|
968
|
-
"""Initialize Conv2dBnFoldQuant layer"""
|
|
969
|
-
super(Conv2dBnFoldQuant, self).__init__()
|
|
970
|
-
if context.get_context('device_target') == "CPU":
|
|
971
|
-
raise ValueError(f"For '{self.cls_name}', only the 'Ascend' and 'GPU' platforms"
|
|
972
|
-
f" are supported, but got {context.get_context('device_target')}.")
|
|
973
|
-
self.in_channels = Validator.check_positive_int(in_channels, "in_channels", self.cls_name)
|
|
974
|
-
self.out_channels = Validator.check_positive_int(out_channels, "out_channels", self.cls_name)
|
|
975
|
-
self.kernel_size = twice(kernel_size)
|
|
976
|
-
self.stride = twice(stride)
|
|
977
|
-
self.dilation = twice(dilation)
|
|
978
|
-
for kernel_size_elem in self.kernel_size:
|
|
979
|
-
Validator.check_positive_int(kernel_size_elem, 'kernel_size item', self.cls_name)
|
|
980
|
-
for stride_elem in self.stride:
|
|
981
|
-
Validator.check_positive_int(stride_elem, 'stride item', self.cls_name)
|
|
982
|
-
for dilation_elem in self.dilation:
|
|
983
|
-
Validator.check_positive_int(dilation_elem, 'dilation item', self.cls_name)
|
|
984
|
-
if pad_mode not in ('valid', 'same', 'pad'):
|
|
985
|
-
raise ValueError(f"For '{self.cls_name}', the 'pad_mode' must be one of values in "
|
|
986
|
-
f"('valid', 'same', 'pad'), but got {pad_mode}.")
|
|
987
|
-
self.pad_mode = pad_mode
|
|
988
|
-
if isinstance(padding, int):
|
|
989
|
-
Validator.check_non_negative_int(padding, 'padding', self.cls_name)
|
|
990
|
-
self.padding = padding
|
|
991
|
-
elif isinstance(padding, tuple):
|
|
992
|
-
for pad in padding:
|
|
993
|
-
Validator.check_non_negative_int(pad, 'padding item', self.cls_name)
|
|
994
|
-
self.padding = padding
|
|
995
|
-
else:
|
|
996
|
-
raise TypeError(f"For '{self.cls_name}', the type of 'padding' must be int/tuple(int), "
|
|
997
|
-
f"but got {type(padding).__name__}!")
|
|
998
|
-
self.group = Validator.check_positive_int(group, "group", self.cls_name)
|
|
999
|
-
self.eps = eps
|
|
1000
|
-
self.momentum = momentum
|
|
1001
|
-
self.has_bias = has_bias
|
|
1002
|
-
self.freeze_bn = freeze_bn
|
|
1003
|
-
self.fake = Validator.check_bool(fake, "fake", self.cls_name)
|
|
1004
|
-
self.quant_config = quant_config
|
|
1005
|
-
self.quant_dtype = quant_dtype
|
|
1006
|
-
self.is_gpu = context.get_context('device_target') == "GPU"
|
|
1007
|
-
|
|
1008
|
-
# initialize convolution op and Parameter
|
|
1009
|
-
self.conv = P.Conv2D(out_channel=out_channels,
|
|
1010
|
-
kernel_size=self.kernel_size,
|
|
1011
|
-
pad_mode=pad_mode,
|
|
1012
|
-
pad=padding,
|
|
1013
|
-
stride=self.stride,
|
|
1014
|
-
dilation=self.dilation,
|
|
1015
|
-
group=group)
|
|
1016
|
-
weight_shape = [out_channels, in_channels // group, *self.kernel_size]
|
|
1017
|
-
channel_axis = 0
|
|
1018
|
-
self.weight = Parameter(initializer(weight_init, weight_shape), name='weight')
|
|
1019
|
-
self.bias_add = P.BiasAdd()
|
|
1020
|
-
self.bias = None
|
|
1021
|
-
if Validator.check_bool(has_bias, "has_bias", self.cls_name):
|
|
1022
|
-
self.bias = Parameter(initializer(bias_init, [out_channels]), name='bias')
|
|
1023
|
-
|
|
1024
|
-
# initialize BatchNorm Parameter
|
|
1025
|
-
self.gamma = Parameter(initializer(gamma_init, [out_channels]), name='gamma')
|
|
1026
|
-
self.beta = Parameter(initializer(beta_init, [out_channels]), name='beta')
|
|
1027
|
-
self.moving_mean = Parameter(initializer(mean_init, [out_channels]), name='moving_mean', requires_grad=False)
|
|
1028
|
-
self.moving_variance = Parameter(initializer(var_init, [out_channels]), name='moving_variance',
|
|
1029
|
-
requires_grad=False)
|
|
1030
|
-
|
|
1031
|
-
# initialize fake ops
|
|
1032
|
-
self.fake_quant_weight = quant_config.weight(channel_axis=channel_axis,
|
|
1033
|
-
num_channels=out_channels)
|
|
1034
|
-
self.batchnorm_fold = BatchNormFoldCell(epsilon=eps, momentum=momentum, freeze_bn=freeze_bn)
|
|
1035
|
-
self.correct_mul = Q.CorrectionMul(channel_axis)
|
|
1036
|
-
if context.get_context('device_target') == "Ascend":
|
|
1037
|
-
self.batchnorm_fold2_train = Q.BatchNormFold2D(freeze_bn=freeze_bn)
|
|
1038
|
-
self.batchnorm_fold2_infer = Q.BatchNormFold2D(freeze_bn=0)
|
|
1039
|
-
elif context.get_context('device_target') == "GPU":
|
|
1040
|
-
self.batchnorm_fold2_train = Q.BatchNormFold2(freeze_bn=freeze_bn)
|
|
1041
|
-
self.batchnorm_fold2_infer = Q.BatchNormFold2(freeze_bn=0)
|
|
1042
|
-
self.step = Parameter(initializer('normal', [1], dtype=mstype.int32), name='step', requires_grad=False)
|
|
1043
|
-
self.one = Tensor(1, mstype.int32)
|
|
1044
|
-
self.assignadd = P.AssignAdd()
|
|
1045
|
-
|
|
1046
|
-
@classmethod
|
|
1047
|
-
def from_float(cls, convbn: Conv2dBnAct, quant_config: QuantConfig, extra_args: dict):
|
|
1048
|
-
"""
|
|
1049
|
-
A class method to create `Conv2dBnFoldQuantOneConv` from a `Conv2dBnAct`
|
|
1050
|
-
|
|
1051
|
-
Examples:
|
|
1052
|
-
>>> from mindspore import nn
|
|
1053
|
-
>>> ic = 10
|
|
1054
|
-
>>> oc = 100
|
|
1055
|
-
>>> kernel_size = 3
|
|
1056
|
-
>>> conv_bn_op = nn.Conv2dBnAct(ic, oc, kernel_size)
|
|
1057
|
-
>>> # when apply QAT on `conv_bn_op`, QAT need to create a quant Conv2dBnAct whose weight is fake-quanted
|
|
1058
|
-
>>> quant_config: QuantConfig = QuantConfig(weight=FakeQuantWithMinMaxObserver.partial_init(),
|
|
1059
|
-
... activation=FakeQuantWithMinMaxObserver.partial_init())
|
|
1060
|
-
>>> extra_args = {"freeze_bn": 100000}
|
|
1061
|
-
>>> conv_bn_quant = nn.Conv2dBnFoldQuant.from_float(conv_bn_op, quant_config, extra_args)
|
|
1062
|
-
"""
|
|
1063
|
-
|
|
1064
|
-
kwargs = {'in_channels': convbn.conv.in_channels,
|
|
1065
|
-
'out_channels': convbn.conv.out_channels,
|
|
1066
|
-
'kernel_size': convbn.conv.kernel_size,
|
|
1067
|
-
'stride': convbn.conv.stride,
|
|
1068
|
-
'pad_mode': convbn.conv.pad_mode,
|
|
1069
|
-
'padding': convbn.conv.padding,
|
|
1070
|
-
'dilation': convbn.conv.dilation,
|
|
1071
|
-
'group': convbn.conv.group,
|
|
1072
|
-
'has_bias': convbn.conv.has_bias,
|
|
1073
|
-
'bias_init': convbn.conv.bias_init,
|
|
1074
|
-
'weight_init': convbn.conv.weight_init,
|
|
1075
|
-
'quant_config': quant_config,
|
|
1076
|
-
'fake': True,
|
|
1077
|
-
}
|
|
1078
|
-
if hasattr(convbn, 'batchnorm'):
|
|
1079
|
-
kwargs['eps'] = convbn.batchnorm.eps
|
|
1080
|
-
kwargs['momentum'] = convbn.batchnorm.momentum
|
|
1081
|
-
kwargs['beta_init'] = convbn.batchnorm.beta_init
|
|
1082
|
-
kwargs['gamma_init'] = convbn.batchnorm.gamma_init
|
|
1083
|
-
kwargs['mean_init'] = convbn.batchnorm.moving_mean_init
|
|
1084
|
-
kwargs['var_init'] = convbn.batchnorm.moving_var_init
|
|
1085
|
-
kwargs = {**kwargs, **extra_args}
|
|
1086
|
-
return cls(**kwargs)
|
|
1087
|
-
|
|
1088
|
-
def extend_repr(self):
|
|
1089
|
-
"""Display instance object as string."""
|
|
1090
|
-
s = 'in_channels={}, out_channels={}, kernel_size={}, stride={}, ' \
|
|
1091
|
-
'pad_mode={}, padding={}, dilation={}, group={}, ' \
|
|
1092
|
-
'fake={}, freeze_bn={}, momentum={}'.format(self.in_channels, self.out_channels, self.kernel_size,
|
|
1093
|
-
self.stride, self.pad_mode, self.padding, self.dilation,
|
|
1094
|
-
self.group, self.fake, self.freeze_bn, self.momentum)
|
|
1095
|
-
return s
|
|
1096
|
-
|
|
1097
|
-
def construct(self, x):
|
|
1098
|
-
out_conv = self.conv(x, self.weight)
|
|
1099
|
-
if self.has_bias:
|
|
1100
|
-
out_conv = self.bias_add(out_conv, self.bias)
|
|
1101
|
-
# BN fold1
|
|
1102
|
-
batch_mean, batch_std, running_mean, running_std = self.batchnorm_fold(out_conv,
|
|
1103
|
-
self.moving_mean,
|
|
1104
|
-
self.moving_variance,
|
|
1105
|
-
self.step)
|
|
1106
|
-
# fake weight
|
|
1107
|
-
weight = self.correct_mul(self.weight, self.gamma, running_std)
|
|
1108
|
-
if self.fake:
|
|
1109
|
-
weight = self.fake_quant_weight(weight)
|
|
1110
|
-
out = self.conv(x, weight)
|
|
1111
|
-
if self.has_bias:
|
|
1112
|
-
out = self.bias_add(out, self.bias)
|
|
1113
|
-
# BN fold2
|
|
1114
|
-
if self.is_gpu:
|
|
1115
|
-
if self.training:
|
|
1116
|
-
out = self.batchnorm_fold2_train(out, self.beta, self.gamma,
|
|
1117
|
-
batch_std, batch_mean, running_std, running_mean, self.step)
|
|
1118
|
-
self.assignadd(self.step, self.one)
|
|
1119
|
-
else:
|
|
1120
|
-
out = self.batchnorm_fold2_infer(out, self.beta, self.gamma,
|
|
1121
|
-
batch_std, batch_mean, running_std, running_mean, self.step)
|
|
1122
|
-
else:
|
|
1123
|
-
if self.training:
|
|
1124
|
-
out = self.batchnorm_fold2_train(out, self.beta, self.gamma, batch_std, batch_mean, running_std)
|
|
1125
|
-
self.assignadd(self.step, self.one)
|
|
1126
|
-
else:
|
|
1127
|
-
out = self.batchnorm_fold2_infer(out, self.beta, self.gamma, running_std, running_mean, running_std)
|
|
1128
|
-
return out
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
class Conv2dBnWithoutFoldQuant(Cell):
|
|
1132
|
-
r"""
|
|
1133
|
-
2D convolution and batchnorm without fold with fake quantized construct.
|
|
1134
|
-
|
|
1135
|
-
This part is a more detailed overview of Conv2d operation. For more details about Quantization,
|
|
1136
|
-
please refer to the implementation of class of `FakeQuantWithMinMaxObserver`,
|
|
1137
|
-
:class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
1138
|
-
|
|
1139
|
-
.. math::
|
|
1140
|
-
y =x\times quant(w)+ b
|
|
1141
|
-
|
|
1142
|
-
y_{bn} =\frac{y-E[y] }{\sqrt{Var[y]+ \epsilon } } *\gamma + \beta
|
|
1143
|
-
|
|
1144
|
-
where :math:`quant` is the continuous execution of quant and dequant, you can refer to the implementation of
|
|
1145
|
-
class of `FakeQuantWithMinMaxObserver`, :class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
1146
|
-
|
|
1147
|
-
Args:
|
|
1148
|
-
in_channels (int): The number of input channel :math:`C_{in}`.
|
|
1149
|
-
out_channels (int): The number of output channel :math:`C_{out}`.
|
|
1150
|
-
kernel_size (Union[int, tuple[int]]): Specifies the height and width of the 2D convolution window.
|
|
1151
|
-
stride (Union[int, tuple[int]]): Specifies stride for all spatial dimensions with the same value. Default: 1.
|
|
1152
|
-
pad_mode (str): Specifies padding mode. The optional values are "same", "valid", "pad". Default: "same".
|
|
1153
|
-
padding (Union[int, tuple[int]]): Implicit paddings on both sides of the `x`. Default: 0.
|
|
1154
|
-
dilation (Union[int, tuple[int]]): Specifies the dilation rate to use for dilated convolution. Default: 1.
|
|
1155
|
-
group (int): Splits filter into groups, `in_ channels` and `out_channels` must be
|
|
1156
|
-
divisible by the number of groups. Default: 1.
|
|
1157
|
-
has_bias (bool): Specifies whether the layer uses a bias vector. Default: False.
|
|
1158
|
-
eps (float): Parameters for Batch Normalization. Default: 1e-5.
|
|
1159
|
-
momentum (float): Parameters for Batch Normalization op. Default: 0.997.
|
|
1160
|
-
weight_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the convolution kernel.
|
|
1161
|
-
Default: 'normal'.
|
|
1162
|
-
bias_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the bias vector. Default: 'zeros'.
|
|
1163
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
1164
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
1165
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
1166
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
1167
|
-
|
|
1168
|
-
Inputs:
|
|
1169
|
-
- **x** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
|
1170
|
-
|
|
1171
|
-
Outputs:
|
|
1172
|
-
Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`.
|
|
1173
|
-
|
|
1174
|
-
Supported Platforms:
|
|
1175
|
-
``Ascend`` ``GPU``
|
|
1176
|
-
|
|
1177
|
-
Raises:
|
|
1178
|
-
TypeError: If `in_channels`, `out_channels` or `group` is not an int.
|
|
1179
|
-
TypeError: If `kernel_size`, `stride`, `padding` or `dilation` is neither an int nor a tuple.
|
|
1180
|
-
TypeError: If `has_bias` is not a bool.
|
|
1181
|
-
ValueError: If `in_channels`, `out_channels`, `kernel_size`, `stride` or `dilation` is less than 1.
|
|
1182
|
-
ValueError: If `padding` is less than 0.
|
|
1183
|
-
ValueError: If `pad_mode` is not one of 'same', 'valid', 'pad'.
|
|
1184
|
-
|
|
1185
|
-
Examples:
|
|
1186
|
-
>>> import mindspore
|
|
1187
|
-
>>> from mindspore.compression import quant
|
|
1188
|
-
>>> from mindspore import Tensor
|
|
1189
|
-
>>> qconfig = quant.create_quant_config()
|
|
1190
|
-
>>> conv2d_no_bnfold = nn.Conv2dBnWithoutFoldQuant(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
|
|
1191
|
-
... weight_init='ones', quant_config=qconfig)
|
|
1192
|
-
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
|
|
1193
|
-
>>> result = conv2d_no_bnfold(x)
|
|
1194
|
-
>>> print(result)
|
|
1195
|
-
[[[[5.929658 13.835868]
|
|
1196
|
-
[11.859316 17.78116]]]]
|
|
1197
|
-
"""
|
|
1198
|
-
|
|
1199
|
-
def __init__(self,
|
|
1200
|
-
in_channels,
|
|
1201
|
-
out_channels,
|
|
1202
|
-
kernel_size,
|
|
1203
|
-
stride=1,
|
|
1204
|
-
pad_mode='same',
|
|
1205
|
-
padding=0,
|
|
1206
|
-
dilation=1,
|
|
1207
|
-
group=1,
|
|
1208
|
-
has_bias=False,
|
|
1209
|
-
eps=1e-5,
|
|
1210
|
-
momentum=0.997,
|
|
1211
|
-
weight_init='normal',
|
|
1212
|
-
bias_init='zeros',
|
|
1213
|
-
quant_config=quant_config_default):
|
|
1214
|
-
"""Initialize Conv2dBnWithoutFoldQuant."""
|
|
1215
|
-
super(Conv2dBnWithoutFoldQuant, self).__init__()
|
|
1216
|
-
self.in_channels = Validator.check_positive_int(in_channels, "in_channels", self.cls_name)
|
|
1217
|
-
self.out_channels = Validator.check_positive_int(out_channels, "out_channels", self.cls_name)
|
|
1218
|
-
self.has_bias = has_bias
|
|
1219
|
-
self.kernel_size = twice(kernel_size)
|
|
1220
|
-
self.stride = twice(stride)
|
|
1221
|
-
self.dilation = twice(dilation)
|
|
1222
|
-
for kernel_size_elem in self.kernel_size:
|
|
1223
|
-
Validator.check_positive_int(kernel_size_elem, 'kernel_size item', self.cls_name)
|
|
1224
|
-
for stride_elem in self.stride:
|
|
1225
|
-
Validator.check_positive_int(stride_elem, 'stride item', self.cls_name)
|
|
1226
|
-
for dilation_elem in self.dilation:
|
|
1227
|
-
Validator.check_positive_int(dilation_elem, 'dilation item', self.cls_name)
|
|
1228
|
-
if pad_mode not in ('valid', 'same', 'pad'):
|
|
1229
|
-
raise ValueError(f"For '{self.cls_name}', the 'pad_mode' must be one of values in "
|
|
1230
|
-
f"('valid', 'same', 'pad'), but got {pad_mode}.")
|
|
1231
|
-
self.pad_mode = pad_mode
|
|
1232
|
-
if isinstance(padding, int):
|
|
1233
|
-
Validator.check_non_negative_int(padding, 'padding', self.cls_name)
|
|
1234
|
-
self.padding = padding
|
|
1235
|
-
elif isinstance(padding, tuple):
|
|
1236
|
-
for pad in padding:
|
|
1237
|
-
Validator.check_non_negative_int(pad, 'padding item', self.cls_name)
|
|
1238
|
-
self.padding = padding
|
|
1239
|
-
else:
|
|
1240
|
-
raise TypeError(f"For '{self.cls_name}', the type of 'padding' must be int/tuple(int), "
|
|
1241
|
-
f"but got {type(padding).__name__}!")
|
|
1242
|
-
self.group = Validator.check_positive_int(group, "group", self.cls_name)
|
|
1243
|
-
self.bias_add = P.BiasAdd()
|
|
1244
|
-
if Validator.check_bool(has_bias, "has_bias", self.cls_name):
|
|
1245
|
-
self.bias = Parameter(initializer(bias_init, [out_channels]), name='bias')
|
|
1246
|
-
else:
|
|
1247
|
-
self.bias = None
|
|
1248
|
-
# initialize convolution op and Parameter
|
|
1249
|
-
self.conv = P.Conv2D(out_channel=self.out_channels,
|
|
1250
|
-
kernel_size=self.kernel_size,
|
|
1251
|
-
mode=1,
|
|
1252
|
-
pad_mode=self.pad_mode,
|
|
1253
|
-
pad=self.padding,
|
|
1254
|
-
stride=self.stride,
|
|
1255
|
-
dilation=self.dilation,
|
|
1256
|
-
group=self.group)
|
|
1257
|
-
weight_shape = [out_channels, in_channels // group, *self.kernel_size]
|
|
1258
|
-
channel_axis = 0
|
|
1259
|
-
self.weight = Parameter(initializer(weight_init, weight_shape), name='weight')
|
|
1260
|
-
self.fake_quant_weight = quant_config.weight(channel_axis=channel_axis,
|
|
1261
|
-
num_channels=out_channels)
|
|
1262
|
-
self.batchnorm = BatchNorm2d(out_channels, eps=eps, momentum=momentum)
|
|
1263
|
-
|
|
1264
|
-
@classmethod
|
|
1265
|
-
def from_float(cls, convbn: Conv2dBnAct, quant_config: QuantConfig):
|
|
1266
|
-
"""
|
|
1267
|
-
A class method to create `Conv2dBnFoldQuantOneConv` from a `Conv2dBnAct`
|
|
1268
|
-
|
|
1269
|
-
Examples:
|
|
1270
|
-
>>> from mindspore import nn
|
|
1271
|
-
>>> ic = 10
|
|
1272
|
-
>>> oc = 100
|
|
1273
|
-
>>> kernel_size = 3
|
|
1274
|
-
>>> conv_bn_op = nn.Conv2dBnAct(ic, oc, kernel_size)
|
|
1275
|
-
>>> # when apply QAT on `conv_bn_op`, QAT need to create a quant Conv2dBnAct whose weight is fake-quanted
|
|
1276
|
-
>>> quant_config: QuantConfig = QuantConfig(weight=FakeQuantWithMinMaxObserver.partial_init(),
|
|
1277
|
-
... activation=FakeQuantWithMinMaxObserver.partial_init())
|
|
1278
|
-
>>> conv_bn_quant = nn.Conv2dBnFoldQuant.from_float(conv_bn_op, quant_config)
|
|
1279
|
-
"""
|
|
1280
|
-
|
|
1281
|
-
kwargs = {'in_channels': convbn.conv.in_channels,
|
|
1282
|
-
'out_channels': convbn.conv.out_channels,
|
|
1283
|
-
'kernel_size': convbn.conv.kernel_size,
|
|
1284
|
-
'stride': convbn.conv.stride,
|
|
1285
|
-
'pad_mode': convbn.conv.pad_mode,
|
|
1286
|
-
'padding': convbn.conv.padding,
|
|
1287
|
-
'dilation': convbn.conv.dilation,
|
|
1288
|
-
'group': convbn.conv.group,
|
|
1289
|
-
'has_bias': convbn.conv.has_bias,
|
|
1290
|
-
'bias_init': convbn.conv.bias_init,
|
|
1291
|
-
'weight_init': convbn.conv.weight_init,
|
|
1292
|
-
'quant_config': quant_config,
|
|
1293
|
-
}
|
|
1294
|
-
if hasattr(convbn, 'batchnorm'):
|
|
1295
|
-
kwargs['eps'] = convbn.batchnorm.eps
|
|
1296
|
-
kwargs['momentum'] = convbn.batchnorm.momentum
|
|
1297
|
-
kwargs = {**kwargs}
|
|
1298
|
-
return cls(**kwargs)
|
|
1299
|
-
|
|
1300
|
-
def construct(self, x):
|
|
1301
|
-
weight = self.fake_quant_weight(self.weight)
|
|
1302
|
-
out = self.conv(x, weight)
|
|
1303
|
-
if self.has_bias:
|
|
1304
|
-
out = self.bias_add(out, self.bias)
|
|
1305
|
-
out = self.batchnorm(out)
|
|
1306
|
-
return out
|
|
1307
|
-
|
|
1308
|
-
def extend_repr(self):
|
|
1309
|
-
"""Display instance object as string."""
|
|
1310
|
-
s = 'in_channels={}, out_channels={}, kernel_size={}, stride={}, ' \
|
|
1311
|
-
'pad_mode={}, padding={}, dilation={}, group={}, ' \
|
|
1312
|
-
'has_bias={}'.format(self.in_channels, self.out_channels, self.kernel_size, self.stride, self.pad_mode,
|
|
1313
|
-
self.padding, self.dilation, self.group, self.has_bias)
|
|
1314
|
-
return s
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
class Conv2dQuant(Cell):
|
|
1318
|
-
r"""
|
|
1319
|
-
2D convolution with fake quantized operation layer.
|
|
1320
|
-
|
|
1321
|
-
This part is a more detailed overview of Conv2d operation. For more details about Quantization,
|
|
1322
|
-
please refer to the implementation of class of `FakeQuantWithMinMaxObserver`,
|
|
1323
|
-
:class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
1324
|
-
|
|
1325
|
-
Args:
|
|
1326
|
-
in_channels (int): The number of input channel :math:`C_{in}`.
|
|
1327
|
-
out_channels (int): The number of output channel :math:`C_{out}`.
|
|
1328
|
-
kernel_size (Union[int, tuple[int]]): Specifies the height and width of the 2D convolution window.
|
|
1329
|
-
stride (Union[int, tuple[int]]): Specifies stride for all spatial dimensions with the same value. Default: 1.
|
|
1330
|
-
pad_mode (str): Specifies padding mode. The optional values are "same", "valid", "pad". Default: "same".
|
|
1331
|
-
padding (Union[int, tuple[int]]): Implicit paddings on both sides of the `x`. Default: 0.
|
|
1332
|
-
dilation (Union[int, tuple[int]]): Specifies the dilation rate to use for dilated convolution. Default: 1.
|
|
1333
|
-
group (int): Splits filter into groups, `in_ channels` and `out_channels` must be
|
|
1334
|
-
divisible by the number of groups. Default: 1.
|
|
1335
|
-
has_bias (bool): Specifies whether the layer uses a bias vector. Default: False.
|
|
1336
|
-
weight_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the convolution kernel.
|
|
1337
|
-
Default: 'normal'.
|
|
1338
|
-
bias_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the bias vector. Default: 'zeros'.
|
|
1339
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
1340
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
1341
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
1342
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
1343
|
-
quant_dtype (QuantDtype): Specifies the FakeQuant datatype. Default: QuantDtype.INT8.
|
|
1344
|
-
|
|
1345
|
-
Inputs:
|
|
1346
|
-
- **x** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
|
1347
|
-
The input dimension is preferably 2D or 4D.
|
|
1348
|
-
|
|
1349
|
-
Outputs:
|
|
1350
|
-
Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`.
|
|
1351
|
-
|
|
1352
|
-
Raises:
|
|
1353
|
-
TypeError: If `in_channels`, `out_channels` or `group` is not an int.
|
|
1354
|
-
TypeError: If `kernel_size`, `stride`, `padding` or `dilation` is neither an int nor a tuple.
|
|
1355
|
-
TypeError: If `has_bias` is not a bool.
|
|
1356
|
-
ValueError: If `in_channels`, `out_channels`, `kernel_size`, `stride` or `dilation` is less than 1.
|
|
1357
|
-
ValueError: If `padding` is less than 0.
|
|
1358
|
-
ValueError: If `pad_mode` is not one of 'same', 'valid', 'pad'.
|
|
1359
|
-
|
|
1360
|
-
Supported Platforms:
|
|
1361
|
-
``Ascend`` ``GPU``
|
|
1362
|
-
|
|
1363
|
-
Examples:
|
|
1364
|
-
>>> import mindspore
|
|
1365
|
-
>>> from mindspore.compression import quant
|
|
1366
|
-
>>> from mindspore import Tensor
|
|
1367
|
-
>>> qconfig = quant.create_quant_config()
|
|
1368
|
-
>>> conv2d_quant = nn.Conv2dQuant(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
|
|
1369
|
-
... weight_init='ones', quant_config=qconfig)
|
|
1370
|
-
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
|
|
1371
|
-
>>> result = conv2d_quant(x)
|
|
1372
|
-
>>> print(result)
|
|
1373
|
-
[[[[5.9296875 13.8359375]
|
|
1374
|
-
[11.859375 17.78125]]]]
|
|
1375
|
-
"""
|
|
1376
|
-
|
|
1377
|
-
def __init__(self,
|
|
1378
|
-
in_channels,
|
|
1379
|
-
out_channels,
|
|
1380
|
-
kernel_size,
|
|
1381
|
-
stride=1,
|
|
1382
|
-
pad_mode='same',
|
|
1383
|
-
padding=0,
|
|
1384
|
-
dilation=1,
|
|
1385
|
-
group=1,
|
|
1386
|
-
has_bias=False,
|
|
1387
|
-
weight_init='normal',
|
|
1388
|
-
bias_init='zeros',
|
|
1389
|
-
quant_config=quant_config_default,
|
|
1390
|
-
quant_dtype=QuantDtype.INT8):
|
|
1391
|
-
"""Initialize Conv2dQuant."""
|
|
1392
|
-
super(Conv2dQuant, self).__init__()
|
|
1393
|
-
self.in_channels = Validator.check_positive_int(in_channels, "in_channels", self.cls_name)
|
|
1394
|
-
self.out_channels = Validator.check_positive_int(out_channels, "out_channels", self.cls_name)
|
|
1395
|
-
self.has_bias = has_bias
|
|
1396
|
-
self.kernel_size = twice(kernel_size)
|
|
1397
|
-
self.stride = twice(stride)
|
|
1398
|
-
self.dilation = twice(dilation)
|
|
1399
|
-
for kernel_size_elem in self.kernel_size:
|
|
1400
|
-
Validator.check_positive_int(kernel_size_elem, 'kernel_size item', self.cls_name)
|
|
1401
|
-
for stride_elem in self.stride:
|
|
1402
|
-
Validator.check_positive_int(stride_elem, 'stride item', self.cls_name)
|
|
1403
|
-
for dilation_elem in self.dilation:
|
|
1404
|
-
Validator.check_positive_int(dilation_elem, 'dilation item', self.cls_name)
|
|
1405
|
-
if pad_mode not in ('valid', 'same', 'pad'):
|
|
1406
|
-
raise ValueError(f"For '{self.cls_name}', the 'pad_mode' must be one of values "
|
|
1407
|
-
f"in ('valid', 'same', 'pad'), but got {pad_mode}.")
|
|
1408
|
-
self.pad_mode = pad_mode
|
|
1409
|
-
if isinstance(padding, int):
|
|
1410
|
-
Validator.check_non_negative_int(padding, 'padding', self.cls_name)
|
|
1411
|
-
self.padding = padding
|
|
1412
|
-
elif isinstance(padding, tuple):
|
|
1413
|
-
for pad in padding:
|
|
1414
|
-
Validator.check_non_negative_int(pad, 'padding item', self.cls_name)
|
|
1415
|
-
self.padding = padding
|
|
1416
|
-
else:
|
|
1417
|
-
raise TypeError(f"For '{self.cls_name}', the type of 'padding' must be int/tuple(int), "
|
|
1418
|
-
f"but got {type(padding).__name__}!")
|
|
1419
|
-
self.group = Validator.check_positive_int(group, "group", self.cls_name)
|
|
1420
|
-
|
|
1421
|
-
weight_shape = [out_channels, in_channels // group, *self.kernel_size]
|
|
1422
|
-
self.weight = Parameter(initializer(weight_init, weight_shape), name='weight')
|
|
1423
|
-
|
|
1424
|
-
self.bias_add = P.BiasAdd()
|
|
1425
|
-
if Validator.check_bool(has_bias, "has_bias", self.cls_name):
|
|
1426
|
-
self.bias = Parameter(initializer(bias_init, [out_channels]), name='bias')
|
|
1427
|
-
else:
|
|
1428
|
-
self.bias = None
|
|
1429
|
-
|
|
1430
|
-
self.conv = P.Conv2D(out_channel=self.out_channels,
|
|
1431
|
-
kernel_size=self.kernel_size,
|
|
1432
|
-
mode=1,
|
|
1433
|
-
pad_mode=self.pad_mode,
|
|
1434
|
-
pad=self.padding,
|
|
1435
|
-
stride=self.stride,
|
|
1436
|
-
dilation=self.dilation,
|
|
1437
|
-
group=self.group)
|
|
1438
|
-
channel_axis = 0
|
|
1439
|
-
self.fake_quant_weight = quant_config.weight(channel_axis=channel_axis,
|
|
1440
|
-
num_channels=out_channels)
|
|
1441
|
-
|
|
1442
|
-
@classmethod
|
|
1443
|
-
def from_float(cls, conv: Conv2d, quant_config: QuantConfig):
|
|
1444
|
-
"""
|
|
1445
|
-
A class method to create `Conv2dQuant` from a `Conv2d`
|
|
1446
|
-
|
|
1447
|
-
Examples:
|
|
1448
|
-
>>> from mindspore import nn
|
|
1449
|
-
>>> ic = 10
|
|
1450
|
-
>>> oc = 100
|
|
1451
|
-
>>> kernel_size = 3
|
|
1452
|
-
>>> conv_op = nn.Conv2d(ic, oc, kernel_size)
|
|
1453
|
-
>>> # when apply QAT on `conv_op`, QAT need to create a quant conv2d whose weight is fake-quanted
|
|
1454
|
-
>>> quant_config: QuantConfig = QuantConfig(weight=FakeQuantWithMinMaxObserver.partial_init(),
|
|
1455
|
-
... activation=FakeQuantWithMinMaxObserver.partial_init())
|
|
1456
|
-
>>> conv_quant = nn.Conv2dQuant.from_float(conv_op, quant_config)
|
|
1457
|
-
"""
|
|
1458
|
-
conv_quant = cls(
|
|
1459
|
-
conv.in_channels,
|
|
1460
|
-
conv.out_channels,
|
|
1461
|
-
kernel_size=conv.kernel_size,
|
|
1462
|
-
stride=conv.stride,
|
|
1463
|
-
pad_mode=conv.pad_mode,
|
|
1464
|
-
padding=conv.padding,
|
|
1465
|
-
dilation=conv.dilation,
|
|
1466
|
-
group=conv.group,
|
|
1467
|
-
has_bias=conv.has_bias,
|
|
1468
|
-
bias_init=conv.bias_init,
|
|
1469
|
-
weight_init=conv.weight_init,
|
|
1470
|
-
quant_config=quant_config)
|
|
1471
|
-
return conv_quant
|
|
1472
|
-
|
|
1473
|
-
def construct(self, x):
|
|
1474
|
-
weight = self.fake_quant_weight(self.weight)
|
|
1475
|
-
out = self.conv(x, weight)
|
|
1476
|
-
if self.has_bias:
|
|
1477
|
-
return self.bias_add(out, self.bias)
|
|
1478
|
-
return out
|
|
1479
|
-
|
|
1480
|
-
def extend_repr(self):
|
|
1481
|
-
"""Display instance object as string."""
|
|
1482
|
-
s = 'in_channels={}, out_channels={}, kernel_size={}, stride={}, ' \
|
|
1483
|
-
'pad_mode={}, padding={}, dilation={}, group={}, ' \
|
|
1484
|
-
'has_bias={}'.format(self.in_channels, self.out_channels, self.kernel_size, self.stride, self.pad_mode,
|
|
1485
|
-
self.padding, self.dilation, self.group, self.has_bias)
|
|
1486
|
-
return s
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
class DenseQuant(Cell):
|
|
1490
|
-
r"""
|
|
1491
|
-
The fully connected layer with fake quantized operation.
|
|
1492
|
-
|
|
1493
|
-
This part is a more detailed overview of Dense operation. For more details about Quantization,
|
|
1494
|
-
please refer to the implementation of class of `FakeQuantWithMinMaxObserver`,
|
|
1495
|
-
:class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
1496
|
-
|
|
1497
|
-
Args:
|
|
1498
|
-
in_channels (int): The dimension of the input space.
|
|
1499
|
-
out_channels (int): The dimension of the output space.
|
|
1500
|
-
weight_init (Union[Tensor, str, Initializer, numbers.Number]): The trainable weight_init parameter. The dtype
|
|
1501
|
-
is same as `x`. The values of str refer to the function `initializer`. Default: 'normal'.
|
|
1502
|
-
bias_init (Union[Tensor, str, Initializer, numbers.Number]): The trainable bias_init parameter. The dtype is
|
|
1503
|
-
same as `x`. The values of str refer to the function `initializer`. Default: 'zeros'.
|
|
1504
|
-
has_bias (bool): Specifies whether the layer uses a bias vector. Default: True.
|
|
1505
|
-
activation (Union[str, Cell, Primitive]): The regularization function applied to the output of the layer,
|
|
1506
|
-
eg. 'relu'. Default: None.
|
|
1507
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
1508
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
1509
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
1510
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
1511
|
-
quant_dtype (QuantDtype): Specifies the FakeQuant datatype. Default: QuantDtype.INT8.
|
|
1512
|
-
|
|
1513
|
-
Inputs:
|
|
1514
|
-
- **x** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
|
1515
|
-
The input dimension is preferably 2D or 4D.
|
|
1516
|
-
|
|
1517
|
-
Outputs:
|
|
1518
|
-
Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`.
|
|
1519
|
-
|
|
1520
|
-
Raises:
|
|
1521
|
-
TypeError: If `in_channels`, `out_channels` is not an int.
|
|
1522
|
-
TypeError: If `has_bias` is not a bool.
|
|
1523
|
-
TypeError: If `activation` is not str, Cell and Primitive.
|
|
1524
|
-
ValueError: If `in_channels` or `out_channels` is less than 1.
|
|
1525
|
-
ValueError: If the dims of `weight_init` is not equal to 2 or the first element of `weight_init` is not equal
|
|
1526
|
-
to `out_channels` or the second element of `weight_init` is not equal to `in_channels`.
|
|
1527
|
-
ValueError: If the dims of `bias_init` is not equal to 1 or the element of `bias_init` is not equal
|
|
1528
|
-
to `out_channels`.
|
|
1529
|
-
|
|
1530
|
-
Supported Platforms:
|
|
1531
|
-
``Ascend`` ``GPU``
|
|
1532
|
-
|
|
1533
|
-
Examples:
|
|
1534
|
-
>>> import mindspore
|
|
1535
|
-
>>> from mindspore.compression import quant
|
|
1536
|
-
>>> from mindspore import Tensor
|
|
1537
|
-
>>> qconfig = quant.create_quant_config()
|
|
1538
|
-
>>> dense_quant = nn.DenseQuant(2, 1, weight_init='ones', quant_config=qconfig)
|
|
1539
|
-
>>> x = Tensor(np.array([[1, 5], [3, 4]]), mindspore.float32)
|
|
1540
|
-
>>> result = dense_quant(x)
|
|
1541
|
-
>>> print(result)
|
|
1542
|
-
[[5.929413]
|
|
1543
|
-
[6.9176483]]
|
|
1544
|
-
"""
|
|
1545
|
-
|
|
1546
|
-
def __init__(self,
|
|
1547
|
-
in_channels,
|
|
1548
|
-
out_channels,
|
|
1549
|
-
weight_init='normal',
|
|
1550
|
-
bias_init='zeros',
|
|
1551
|
-
has_bias=True,
|
|
1552
|
-
activation=None,
|
|
1553
|
-
quant_config=quant_config_default,
|
|
1554
|
-
quant_dtype=QuantDtype.INT8):
|
|
1555
|
-
"""Initialize DenseQuant."""
|
|
1556
|
-
super(DenseQuant, self).__init__()
|
|
1557
|
-
self.in_channels = Validator.check_positive_int(in_channels, "in_channels", self.cls_name)
|
|
1558
|
-
self.out_channels = Validator.check_positive_int(out_channels, "out_channels", self.cls_name)
|
|
1559
|
-
self.has_bias = Validator.check_bool(has_bias, "has_bias", self.cls_name)
|
|
1560
|
-
|
|
1561
|
-
if isinstance(weight_init, Tensor):
|
|
1562
|
-
if weight_init.ndim != 2 or weight_init.shape[0] != out_channels or \
|
|
1563
|
-
weight_init.shape[1] != in_channels:
|
|
1564
|
-
raise ValueError(f"For '{self.cls_name}', weight init shape error. The ndim of 'weight_init' should "
|
|
1565
|
-
f"be equal to 2, and the first dim must be equal to 'out_channels', and the "
|
|
1566
|
-
f"second dim must be equal to 'in_channels'. But got 'weight_init': {weight_init}, "
|
|
1567
|
-
f"'out_channels': {out_channels}, 'in_channels': {in_channels}.")
|
|
1568
|
-
|
|
1569
|
-
self.weight = Parameter(initializer(
|
|
1570
|
-
weight_init, [out_channels, in_channels]), name="weight")
|
|
1571
|
-
|
|
1572
|
-
if self.has_bias:
|
|
1573
|
-
if isinstance(bias_init, Tensor):
|
|
1574
|
-
if bias_init.ndim != 1 or bias_init.shape[0] != out_channels:
|
|
1575
|
-
raise ValueError(f"For '{self.cls_name}', bias init shape error. The ndim of 'bias_init' should "
|
|
1576
|
-
f"be equal to 1, and the first dim must be equal to 'out_channels'. But got "
|
|
1577
|
-
f"'bias_init': {bias_init}, 'out_channels': {out_channels}.")
|
|
1578
|
-
|
|
1579
|
-
self.bias = Parameter(initializer(
|
|
1580
|
-
bias_init, [out_channels]), name="bias")
|
|
1581
|
-
|
|
1582
|
-
self.matmul = P.MatMul(transpose_b=True)
|
|
1583
|
-
self.bias_add = P.BiasAdd()
|
|
1584
|
-
|
|
1585
|
-
self.activation = get_activation(activation) if isinstance(activation, str) else activation
|
|
1586
|
-
if activation is not None and not isinstance(self.activation, (Cell, Primitive)):
|
|
1587
|
-
raise TypeError(f"For '{self.cls_name}', the 'activation' must be str or Cell or Primitive, "
|
|
1588
|
-
f"but got {activation}.")
|
|
1589
|
-
|
|
1590
|
-
self.activation_flag = self.activation is not None
|
|
1591
|
-
self.fake_quant_weight = quant_config.weight(channel_axis=0,
|
|
1592
|
-
num_channels=out_channels)
|
|
1593
|
-
|
|
1594
|
-
@classmethod
|
|
1595
|
-
def from_float(cls, dense: Dense, quant_config: QuantConfig):
|
|
1596
|
-
"""
|
|
1597
|
-
A class method to create `DenseQuant` from a `Dense`
|
|
1598
|
-
|
|
1599
|
-
Examples:
|
|
1600
|
-
>>> from mindspore import nn
|
|
1601
|
-
>>> ic = 10
|
|
1602
|
-
>>> oc = 100
|
|
1603
|
-
>>> dense_op = nn.Dense(ic, oc)
|
|
1604
|
-
>>> # when apply QAT on `dense_op`, QAT need to create a quant dense whose weight is fake-quanted
|
|
1605
|
-
>>> quant_config: QuantConfig = QuantConfig(weight=FakeQuantWithMinMaxObserver.partial_init(),
|
|
1606
|
-
... activation=FakeQuantWithMinMaxObserver.partial_init())
|
|
1607
|
-
>>> dense_quant = nn.DenseQuant.from_float(dense_op, quant_config)
|
|
1608
|
-
"""
|
|
1609
|
-
dense_quant = cls(
|
|
1610
|
-
dense.in_channels,
|
|
1611
|
-
dense.out_channels,
|
|
1612
|
-
dense.weight,
|
|
1613
|
-
dense.bias,
|
|
1614
|
-
dense.has_bias,
|
|
1615
|
-
dense.activation,
|
|
1616
|
-
quant_config=quant_config)
|
|
1617
|
-
return dense_quant
|
|
1618
|
-
|
|
1619
|
-
def construct(self, x):
|
|
1620
|
-
"""Use operators to construct the Dense layer.
|
|
1621
|
-
|
|
1622
|
-
Args:
|
|
1623
|
-
x (Tensor): Input tensor.
|
|
1624
|
-
"""
|
|
1625
|
-
output = self.fake_quant_weight(self.weight)
|
|
1626
|
-
output = self.matmul(x, output)
|
|
1627
|
-
if self.has_bias:
|
|
1628
|
-
output = self.bias_add(output, self.bias)
|
|
1629
|
-
if self.activation_flag:
|
|
1630
|
-
return self.activation(output)
|
|
1631
|
-
return output
|
|
1632
|
-
|
|
1633
|
-
def extend_repr(self):
|
|
1634
|
-
"""A pretty print for Dense layer."""
|
|
1635
|
-
s = 'in_channels={}, out_channels={}, weight={}, has_bias={}'.format(
|
|
1636
|
-
self.in_channels, self.out_channels, self.weight, self.has_bias)
|
|
1637
|
-
if self.has_bias:
|
|
1638
|
-
s += ', bias={}'.format(self.bias)
|
|
1639
|
-
if self.activation_flag:
|
|
1640
|
-
s += ', activation={}'.format(self.activation)
|
|
1641
|
-
return s
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
class _QuantActivation(Cell):
|
|
1645
|
-
r"""
|
|
1646
|
-
Base class for quantization aware training activation function. Adds fake quantized operation
|
|
1647
|
-
after activation operation.
|
|
1648
|
-
"""
|
|
1649
|
-
|
|
1650
|
-
def get_origin(self):
|
|
1651
|
-
raise NotImplementedError
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
class ActQuant(_QuantActivation):
|
|
1655
|
-
r"""
|
|
1656
|
-
Quantization aware training activation function.
|
|
1657
|
-
|
|
1658
|
-
Add the fake quantized operation to the end of activation operation, by which the output of activation
|
|
1659
|
-
operation will be truncated. For more details about Quantization, please refer to the implementation
|
|
1660
|
-
of subclass of `FakeQuantWithMinMaxObserver`, :class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
1661
|
-
|
|
1662
|
-
Args:
|
|
1663
|
-
activation (Cell): Activation cell.
|
|
1664
|
-
ema (bool): The exponential Moving Average algorithm updates min and max. Default: False.
|
|
1665
|
-
ema_decay (float): Exponential Moving Average algorithm parameter. Default: 0.999.
|
|
1666
|
-
fake_before (bool): Whether add fake quantized operation before activation. Default: False.
|
|
1667
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
1668
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
1669
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
1670
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
1671
|
-
quant_dtype (QuantDtype): Specifies the FakeQuant datatype. Default: QuantDtype.INT8.
|
|
1672
|
-
|
|
1673
|
-
Inputs:
|
|
1674
|
-
- **x** (Tensor) - The input of ActQuant. The input dimension is preferably 2D or 4D.
|
|
1675
|
-
|
|
1676
|
-
Outputs:
|
|
1677
|
-
Tensor, with the same type and shape as the `x`.
|
|
1678
|
-
|
|
1679
|
-
Raises:
|
|
1680
|
-
TypeError: If `activation` is not an instance of Cell.
|
|
1681
|
-
TypeError: If `fake_before` is not a bool.
|
|
1682
|
-
|
|
1683
|
-
Supported Platforms:
|
|
1684
|
-
``Ascend`` ``GPU``
|
|
1685
|
-
|
|
1686
|
-
Examples:
|
|
1687
|
-
>>> import mindspore
|
|
1688
|
-
>>> from mindspore.compression import quant
|
|
1689
|
-
>>> from mindspore import Tensor
|
|
1690
|
-
>>> qconfig = quant.create_quant_config()
|
|
1691
|
-
>>> act_quant = nn.ActQuant(nn.ReLU(), quant_config=qconfig)
|
|
1692
|
-
>>> x = Tensor(np.array([[1, 2, -1], [-2, 0, -1]]), mindspore.float32)
|
|
1693
|
-
>>> result = act_quant(x)
|
|
1694
|
-
>>> print(result)
|
|
1695
|
-
[[0.9882355 1.9764705 0. ]
|
|
1696
|
-
[0. 0. 0. ]]
|
|
1697
|
-
"""
|
|
1698
|
-
|
|
1699
|
-
def __init__(self,
|
|
1700
|
-
activation,
|
|
1701
|
-
ema=False,
|
|
1702
|
-
ema_decay=0.999,
|
|
1703
|
-
fake_before=False,
|
|
1704
|
-
quant_config=quant_config_default,
|
|
1705
|
-
quant_dtype=QuantDtype.INT8):
|
|
1706
|
-
"""Initialize ActQuant."""
|
|
1707
|
-
super(ActQuant, self).__init__()
|
|
1708
|
-
act_class = activation.__class__
|
|
1709
|
-
act_list = [nn.ReLU, nn.ReLU6]
|
|
1710
|
-
self.act = Validator.check_isinstance("activation", activation, Cell)
|
|
1711
|
-
self.fake_before = Validator.check_bool(fake_before, "fake_before", self.cls_name)
|
|
1712
|
-
if self.fake_before:
|
|
1713
|
-
self.fake_quant_act_before = quant_config.activation(min_init=-6,
|
|
1714
|
-
max_init=6,
|
|
1715
|
-
ema=ema,
|
|
1716
|
-
ema_decay=ema_decay,
|
|
1717
|
-
quant_dtype=quant_dtype)
|
|
1718
|
-
self.neg_trunc = False
|
|
1719
|
-
self.narrow_range = False
|
|
1720
|
-
preset_dict = quant_config.activation.p.keywords
|
|
1721
|
-
if 'mode' in preset_dict and preset_dict['mode'] == "LEARNED_SCALE" and act_class in act_list:
|
|
1722
|
-
self.neg_trunc = True
|
|
1723
|
-
elif 'narrow_range' in preset_dict:
|
|
1724
|
-
self.narrow_range = preset_dict['narrow_range']
|
|
1725
|
-
|
|
1726
|
-
self.fake_quant_act = quant_config.activation(min_init=-6,
|
|
1727
|
-
max_init=6,
|
|
1728
|
-
ema=ema,
|
|
1729
|
-
ema_decay=ema_decay,
|
|
1730
|
-
quant_dtype=quant_dtype,
|
|
1731
|
-
neg_trunc=self.neg_trunc,
|
|
1732
|
-
narrow_range=self.narrow_range)
|
|
1733
|
-
|
|
1734
|
-
def construct(self, x):
|
|
1735
|
-
if self.fake_before:
|
|
1736
|
-
x = self.fake_quant_act_before(x)
|
|
1737
|
-
x = self.act(x)
|
|
1738
|
-
x = self.fake_quant_act(x)
|
|
1739
|
-
return x
|
|
1740
|
-
|
|
1741
|
-
def get_origin(self):
|
|
1742
|
-
return self.act
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
class TensorAddQuant(Cell):
|
|
1746
|
-
r"""
|
|
1747
|
-
Adds fake quantized operation after TensorAdd operation.
|
|
1748
|
-
|
|
1749
|
-
This part is a more detailed overview of TensorAdd operation. For more details about Quantization,
|
|
1750
|
-
please refer to the implementation of class of `FakeQuantWithMinMaxObserver`,
|
|
1751
|
-
:class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
1752
|
-
|
|
1753
|
-
Args:
|
|
1754
|
-
ema_decay (float): Exponential Moving Average algorithm parameter. Default: 0.999.
|
|
1755
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
1756
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
1757
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
1758
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
1759
|
-
quant_dtype (QuantDtype): Specifies the FakeQuant datatype. Default: QuantDtype.INT8.
|
|
1760
|
-
|
|
1761
|
-
Inputs:
|
|
1762
|
-
- **x1** (Tensor) - The first tensor of TensorAddQuant. The input dimension is preferably 2D or 4D.
|
|
1763
|
-
- **x2** (Tensor) - The second tensor of TensorAddQuant. Has the same shape with `x1`.
|
|
1764
|
-
|
|
1765
|
-
Outputs:
|
|
1766
|
-
Tensor, with the same type and shape as the `x1`.
|
|
1767
|
-
|
|
1768
|
-
Raises:
|
|
1769
|
-
TypeError: If `ema_decay` is not a float.
|
|
1770
|
-
ValueError: If the shape of `x2` is different with `x1`.
|
|
1771
|
-
|
|
1772
|
-
Supported Platforms:
|
|
1773
|
-
``Ascend`` ``GPU``
|
|
1774
|
-
|
|
1775
|
-
Examples:
|
|
1776
|
-
>>> import mindspore
|
|
1777
|
-
>>> from mindspore.compression import quant
|
|
1778
|
-
>>> from mindspore import Tensor
|
|
1779
|
-
>>> qconfig = quant.create_quant_config()
|
|
1780
|
-
>>> add_quant = nn.TensorAddQuant(quant_config=qconfig)
|
|
1781
|
-
>>> x1 = Tensor(np.array([[1, 2, 1], [-2, 0, -1]]), mindspore.float32)
|
|
1782
|
-
>>> x2 = Tensor(np.ones((2, 3)), mindspore.float32)
|
|
1783
|
-
>>> output = add_quant(x1, x2)
|
|
1784
|
-
>>> print(output)
|
|
1785
|
-
[[ 1.9764705 3.011765 1.9764705]
|
|
1786
|
-
[-0.9882355 0.9882355 0. ]]
|
|
1787
|
-
"""
|
|
1788
|
-
|
|
1789
|
-
def __init__(self,
|
|
1790
|
-
ema_decay=0.999,
|
|
1791
|
-
quant_config=quant_config_default,
|
|
1792
|
-
quant_dtype=QuantDtype.INT8):
|
|
1793
|
-
"""Initialize TensorAddQuant."""
|
|
1794
|
-
super(TensorAddQuant, self).__init__()
|
|
1795
|
-
self.fake_quant_act = quant_config.activation(min_init=-6,
|
|
1796
|
-
max_init=6,
|
|
1797
|
-
ema=True,
|
|
1798
|
-
ema_decay=ema_decay,
|
|
1799
|
-
quant_dtype=quant_dtype)
|
|
1800
|
-
self.add = P.Add()
|
|
1801
|
-
|
|
1802
|
-
def construct(self, x1, x2):
|
|
1803
|
-
x = self.add(x1, x2)
|
|
1804
|
-
x = self.fake_quant_act(x)
|
|
1805
|
-
return x
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
class MulQuant(Cell):
|
|
1809
|
-
r"""
|
|
1810
|
-
Adds fake quantized operation after `Mul` operation.
|
|
1811
|
-
|
|
1812
|
-
This part is a more detailed overview of `Mul` operation. For more details about Quantization,
|
|
1813
|
-
please refer to the implementation of class of `FakeQuantWithMinMaxObserver`,
|
|
1814
|
-
:class:`mindspore.nn.FakeQuantWithMinMaxObserver`.
|
|
1815
|
-
|
|
1816
|
-
Args:
|
|
1817
|
-
ema_decay (float): Exponential Moving Average algorithm parameter. Default: 0.999.
|
|
1818
|
-
quant_config (QuantConfig): Configures the types of quant observer and quant settings of weight and
|
|
1819
|
-
activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization
|
|
1820
|
-
and can be generated by :func:`mindspore.compression.quant.create_quant_config` method.
|
|
1821
|
-
Default: QuantConfig with both items set to default :class:`FakeQuantWithMinMaxObserver`.
|
|
1822
|
-
quant_dtype (QuantDtype): Specifies the FakeQuant datatype. Default: QuantDtype.INT8.
|
|
1823
|
-
|
|
1824
|
-
Inputs:
|
|
1825
|
-
- **x1** (Tensor) - The first tensor of MulQuant. The input dimension is preferably 2D or 4D.
|
|
1826
|
-
- **x2** (Tensor) - The second tensor of MulQuant. Has the same shape with `x1`.
|
|
1827
|
-
|
|
1828
|
-
Outputs:
|
|
1829
|
-
Tensor, with the same type and shape as the `x1`.
|
|
1830
|
-
|
|
1831
|
-
Raises:
|
|
1832
|
-
TypeError: If `ema_decay` is not a float.
|
|
1833
|
-
ValueError: If the shape of `x2` is different with `x1`.
|
|
1834
|
-
|
|
1835
|
-
Supported Platforms:
|
|
1836
|
-
``Ascend`` ``GPU``
|
|
1837
|
-
|
|
1838
|
-
Examples:
|
|
1839
|
-
>>> import mindspore
|
|
1840
|
-
>>> from mindspore.compression import quant
|
|
1841
|
-
>>> from mindspore import Tensor
|
|
1842
|
-
>>> qconfig = quant.create_quant_config()
|
|
1843
|
-
>>> mul_quant = nn.MulQuant(quant_config=qconfig)
|
|
1844
|
-
>>> x1 = Tensor(np.array([[1, 2, 1], [-2, 0, -1]]), mindspore.float32)
|
|
1845
|
-
>>> x2 = Tensor(np.ones((2, 3)) * 2, mindspore.float32)
|
|
1846
|
-
>>> output = mul_quant(x1, x2)
|
|
1847
|
-
>>> print(output)
|
|
1848
|
-
[[ 1.9764705 4.0000005 1.9764705]
|
|
1849
|
-
[-4. 0. -1.9764705]]
|
|
1850
|
-
"""
|
|
1851
|
-
|
|
1852
|
-
def __init__(self,
|
|
1853
|
-
ema_decay=0.999,
|
|
1854
|
-
quant_config=quant_config_default,
|
|
1855
|
-
quant_dtype=QuantDtype.INT8):
|
|
1856
|
-
"""Initialize MulQuant."""
|
|
1857
|
-
super(MulQuant, self).__init__()
|
|
1858
|
-
self.fake_quant_act = quant_config.activation(min_init=-6,
|
|
1859
|
-
max_init=6,
|
|
1860
|
-
ema=True,
|
|
1861
|
-
ema_decay=ema_decay,
|
|
1862
|
-
quant_dtype=quant_dtype)
|
|
1863
|
-
self.mul = P.Mul()
|
|
1864
|
-
|
|
1865
|
-
def construct(self, x1, x2):
|
|
1866
|
-
x = self.mul(x1, x2)
|
|
1867
|
-
x = self.fake_quant_act(x)
|
|
1868
|
-
return x
|