mindspore 2.3.0__cp39-cp39-win_amd64.whl → 2.4.0__cp39-cp39-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of mindspore might be problematic. Click here for more details.
- mindspore/.commit_id +1 -1
- mindspore/__init__.py +3 -1
- mindspore/_c_dataengine.cp39-win_amd64.pyd +0 -0
- mindspore/_c_expression.cp39-win_amd64.pyd +0 -0
- mindspore/_c_mindrecord.cp39-win_amd64.pyd +0 -0
- mindspore/_checkparam.py +50 -9
- mindspore/_extends/parse/compile_config.py +41 -0
- mindspore/_extends/parse/parser.py +9 -7
- mindspore/_extends/parse/standard_method.py +52 -14
- mindspore/_extends/pijit/pijit_func_white_list.py +350 -24
- mindspore/amp.py +24 -10
- mindspore/avcodec-59.dll +0 -0
- mindspore/avdevice-59.dll +0 -0
- mindspore/avfilter-8.dll +0 -0
- mindspore/avformat-59.dll +0 -0
- mindspore/avutil-57.dll +0 -0
- mindspore/common/__init__.py +6 -4
- mindspore/common/_pijit_context.py +190 -0
- mindspore/common/_register_for_tensor.py +2 -1
- mindspore/common/_tensor_overload.py +139 -0
- mindspore/common/api.py +102 -87
- mindspore/common/dump.py +5 -6
- mindspore/common/generator.py +1 -7
- mindspore/common/hook_handle.py +14 -26
- mindspore/common/mindir_util.py +2 -2
- mindspore/common/parameter.py +46 -13
- mindspore/common/recompute.py +39 -9
- mindspore/common/sparse_tensor.py +7 -3
- mindspore/common/tensor.py +209 -29
- mindspore/communication/__init__.py +1 -1
- mindspore/communication/_comm_helper.py +38 -3
- mindspore/communication/comm_func.py +310 -55
- mindspore/communication/management.py +14 -14
- mindspore/context.py +123 -22
- mindspore/dataset/__init__.py +1 -1
- mindspore/dataset/audio/__init__.py +1 -1
- mindspore/dataset/core/config.py +7 -0
- mindspore/dataset/core/validator_helpers.py +7 -0
- mindspore/dataset/engine/cache_client.py +1 -1
- mindspore/dataset/engine/datasets.py +72 -44
- mindspore/dataset/engine/datasets_audio.py +7 -7
- mindspore/dataset/engine/datasets_standard_format.py +53 -3
- mindspore/dataset/engine/datasets_text.py +20 -20
- mindspore/dataset/engine/datasets_user_defined.py +174 -104
- mindspore/dataset/engine/datasets_vision.py +33 -33
- mindspore/dataset/engine/iterators.py +29 -0
- mindspore/dataset/engine/obs/util.py +7 -0
- mindspore/dataset/engine/queue.py +114 -60
- mindspore/dataset/engine/serializer_deserializer.py +2 -2
- mindspore/dataset/engine/validators.py +34 -14
- mindspore/dataset/text/__init__.py +1 -4
- mindspore/dataset/transforms/__init__.py +0 -3
- mindspore/dataset/utils/line_reader.py +2 -0
- mindspore/dataset/vision/__init__.py +1 -4
- mindspore/dataset/vision/utils.py +1 -1
- mindspore/dataset/vision/validators.py +2 -1
- mindspore/dnnl.dll +0 -0
- mindspore/{nn/extend → experimental/es}/__init__.py +4 -11
- mindspore/experimental/es/embedding_service.py +883 -0
- mindspore/{nn/layer → experimental/es}/embedding_service_layer.py +218 -30
- mindspore/experimental/llm_boost/__init__.py +21 -0
- mindspore/{nn/extend/layer → experimental/llm_boost/atb}/__init__.py +4 -8
- mindspore/experimental/llm_boost/atb/boost_base.py +211 -0
- mindspore/experimental/llm_boost/atb/llama_boost.py +115 -0
- mindspore/experimental/llm_boost/atb/qwen_boost.py +101 -0
- mindspore/experimental/llm_boost/register.py +129 -0
- mindspore/experimental/llm_boost/utils.py +31 -0
- mindspore/experimental/optim/adamw.py +85 -0
- mindspore/experimental/optim/optimizer.py +3 -0
- mindspore/hal/__init__.py +3 -3
- mindspore/hal/contiguous_tensors_handle.py +175 -0
- mindspore/hal/stream.py +18 -0
- mindspore/include/api/model_group.h +13 -1
- mindspore/include/api/types.h +10 -10
- mindspore/include/dataset/config.h +2 -2
- mindspore/include/dataset/constants.h +2 -2
- mindspore/include/dataset/execute.h +2 -2
- mindspore/include/dataset/vision.h +4 -0
- mindspore/jpeg62.dll +0 -0
- mindspore/log.py +1 -1
- mindspore/mindrecord/filewriter.py +68 -51
- mindspore/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_np_dtype.dll +0 -0
- mindspore/mindspore_ops.dll +0 -0
- mindspore/mint/__init__.py +495 -46
- mindspore/mint/distributed/__init__.py +31 -0
- mindspore/mint/distributed/distributed.py +254 -0
- mindspore/mint/nn/__init__.py +266 -21
- mindspore/mint/nn/functional.py +125 -19
- mindspore/mint/nn/layer/__init__.py +39 -0
- mindspore/mint/nn/layer/activation.py +133 -0
- mindspore/mint/nn/layer/normalization.py +477 -0
- mindspore/mint/nn/layer/pooling.py +110 -0
- mindspore/mint/optim/adamw.py +28 -7
- mindspore/mint/special/__init__.py +63 -0
- mindspore/multiprocessing/__init__.py +2 -1
- mindspore/nn/__init__.py +0 -1
- mindspore/nn/cell.py +275 -93
- mindspore/nn/layer/activation.py +211 -44
- mindspore/nn/layer/basic.py +113 -3
- mindspore/nn/layer/embedding.py +120 -2
- mindspore/nn/layer/normalization.py +101 -5
- mindspore/nn/layer/padding.py +34 -48
- mindspore/nn/layer/pooling.py +161 -7
- mindspore/nn/layer/transformer.py +3 -3
- mindspore/nn/loss/__init__.py +2 -2
- mindspore/nn/loss/loss.py +84 -6
- mindspore/nn/optim/__init__.py +2 -1
- mindspore/nn/optim/adadelta.py +1 -1
- mindspore/nn/optim/adam.py +1 -1
- mindspore/nn/optim/lamb.py +1 -1
- mindspore/nn/optim/tft_wrapper.py +127 -0
- mindspore/nn/wrap/cell_wrapper.py +12 -23
- mindspore/nn/wrap/grad_reducer.py +5 -5
- mindspore/nn/wrap/loss_scale.py +17 -3
- mindspore/numpy/__init__.py +1 -1
- mindspore/numpy/array_creations.py +65 -68
- mindspore/numpy/array_ops.py +64 -60
- mindspore/numpy/fft.py +610 -75
- mindspore/numpy/logic_ops.py +11 -10
- mindspore/numpy/math_ops.py +85 -84
- mindspore/numpy/utils_const.py +4 -4
- mindspore/opencv_core452.dll +0 -0
- mindspore/opencv_imgcodecs452.dll +0 -0
- mindspore/opencv_imgproc452.dll +0 -0
- mindspore/ops/__init__.py +6 -4
- mindspore/ops/_grad_experimental/grad_comm_ops.py +47 -3
- mindspore/ops/_grad_experimental/grad_math_ops.py +0 -22
- mindspore/ops/_vmap/vmap_array_ops.py +2 -4
- mindspore/ops/_vmap/vmap_math_ops.py +17 -1
- mindspore/ops/_vmap/vmap_nn_ops.py +43 -2
- mindspore/ops/auto_generate/cpp_create_prim_instance_helper.py +85 -7
- mindspore/ops/auto_generate/gen_arg_dtype_cast.py +2 -0
- mindspore/ops/auto_generate/gen_extend_func.py +734 -13
- mindspore/ops/auto_generate/gen_ops_def.py +2420 -381
- mindspore/ops/auto_generate/gen_ops_prim.py +5196 -1659
- mindspore/ops/auto_generate/pyboost_inner_prim.py +176 -56
- mindspore/ops/composite/base.py +85 -48
- mindspore/ops/composite/multitype_ops/_compile_utils.py +1 -0
- mindspore/ops/composite/multitype_ops/not_in_impl.py +2 -2
- mindspore/ops/function/__init__.py +22 -0
- mindspore/ops/function/array_func.py +490 -153
- mindspore/ops/function/debug_func.py +113 -1
- mindspore/ops/function/fft_func.py +15 -2
- mindspore/ops/function/grad/grad_func.py +3 -2
- mindspore/ops/function/math_func.py +558 -207
- mindspore/ops/function/nn_func.py +817 -383
- mindspore/ops/function/other_func.py +3 -2
- mindspore/ops/function/random_func.py +184 -8
- mindspore/ops/function/reshard_func.py +13 -11
- mindspore/ops/function/sparse_unary_func.py +1 -1
- mindspore/ops/function/vmap_func.py +3 -2
- mindspore/ops/functional.py +24 -14
- mindspore/ops/op_info_register.py +3 -3
- mindspore/ops/operations/__init__.py +6 -1
- mindspore/ops/operations/_grad_ops.py +2 -76
- mindspore/ops/operations/_infer_ops.py +1 -1
- mindspore/ops/operations/_inner_ops.py +71 -94
- mindspore/ops/operations/array_ops.py +12 -146
- mindspore/ops/operations/comm_ops.py +42 -53
- mindspore/ops/operations/custom_ops.py +83 -19
- mindspore/ops/operations/debug_ops.py +42 -10
- mindspore/ops/operations/manually_defined/_inner.py +12 -0
- mindspore/ops/operations/manually_defined/ops_def.py +265 -10
- mindspore/ops/operations/math_ops.py +12 -223
- mindspore/ops/operations/nn_ops.py +20 -114
- mindspore/ops/operations/other_ops.py +7 -4
- mindspore/ops/operations/random_ops.py +46 -1
- mindspore/ops/primitive.py +18 -6
- mindspore/ops_generate/arg_dtype_cast.py +2 -0
- mindspore/ops_generate/gen_aclnn_implement.py +11 -11
- mindspore/ops_generate/gen_constants.py +36 -0
- mindspore/ops_generate/gen_ops.py +67 -52
- mindspore/ops_generate/gen_ops_inner_prim.py +1 -1
- mindspore/ops_generate/gen_pyboost_func.py +131 -47
- mindspore/ops_generate/op_proto.py +10 -3
- mindspore/ops_generate/pyboost_utils.py +14 -1
- mindspore/ops_generate/template.py +43 -21
- mindspore/parallel/__init__.py +3 -1
- mindspore/parallel/_auto_parallel_context.py +28 -8
- mindspore/parallel/_cell_wrapper.py +83 -0
- mindspore/parallel/_parallel_serialization.py +47 -19
- mindspore/parallel/_tensor.py +81 -11
- mindspore/parallel/_utils.py +13 -1
- mindspore/parallel/algo_parameter_config.py +5 -5
- mindspore/parallel/checkpoint_transform.py +46 -39
- mindspore/parallel/cluster/process_entity/__init__.py +1 -1
- mindspore/parallel/cluster/process_entity/_api.py +31 -23
- mindspore/parallel/cluster/process_entity/_utils.py +2 -27
- mindspore/parallel/parameter_broadcast.py +3 -4
- mindspore/parallel/shard.py +162 -31
- mindspore/parallel/transform_safetensors.py +993 -0
- mindspore/profiler/__init__.py +2 -1
- mindspore/profiler/common/constant.py +29 -0
- mindspore/profiler/common/registry.py +47 -0
- mindspore/profiler/common/util.py +28 -0
- mindspore/profiler/dynamic_profiler.py +694 -0
- mindspore/profiler/envprofiling.py +17 -19
- mindspore/profiler/parser/ascend_analysis/constant.py +18 -0
- mindspore/profiler/parser/ascend_analysis/file_manager.py +25 -4
- mindspore/profiler/parser/ascend_analysis/function_event.py +43 -19
- mindspore/profiler/parser/ascend_analysis/fwk_cann_parser.py +31 -26
- mindspore/profiler/parser/ascend_analysis/fwk_file_parser.py +56 -10
- mindspore/profiler/parser/ascend_analysis/msprof_timeline_parser.py +55 -8
- mindspore/profiler/parser/ascend_analysis/path_manager.py +313 -0
- mindspore/profiler/parser/ascend_analysis/profiler_info_parser.py +27 -20
- mindspore/profiler/parser/ascend_analysis/trace_event_manager.py +9 -2
- mindspore/profiler/parser/ascend_msprof_exporter.py +5 -4
- mindspore/profiler/parser/ascend_timeline_generator.py +27 -25
- mindspore/profiler/parser/base_timeline_generator.py +19 -25
- mindspore/profiler/parser/cpu_gpu_timeline_generator.py +25 -12
- mindspore/profiler/parser/framework_parser.py +1 -391
- mindspore/profiler/parser/gpu_analysis/__init__.py +14 -0
- mindspore/profiler/parser/gpu_analysis/function_event.py +44 -0
- mindspore/profiler/parser/gpu_analysis/fwk_file_parser.py +89 -0
- mindspore/profiler/parser/gpu_analysis/profiler_info_parser.py +72 -0
- mindspore/profiler/parser/memory_usage_parser.py +0 -154
- mindspore/profiler/parser/profiler_info.py +78 -6
- mindspore/profiler/profiler.py +153 -0
- mindspore/profiler/profiling.py +280 -412
- mindspore/rewrite/__init__.py +1 -2
- mindspore/rewrite/common/namespace.py +4 -4
- mindspore/rewrite/symbol_tree/symbol_tree.py +3 -3
- mindspore/run_check/_check_version.py +36 -103
- mindspore/safeguard/rewrite_obfuscation.py +591 -247
- mindspore/swresample-4.dll +0 -0
- mindspore/swscale-6.dll +0 -0
- mindspore/tinyxml2.dll +0 -0
- mindspore/train/__init__.py +4 -3
- mindspore/train/_utils.py +28 -2
- mindspore/train/amp.py +171 -53
- mindspore/train/callback/__init__.py +2 -2
- mindspore/train/callback/_callback.py +4 -4
- mindspore/train/callback/_checkpoint.py +85 -22
- mindspore/train/callback/_cluster_monitor.py +1 -1
- mindspore/train/callback/_flops_collector.py +1 -0
- mindspore/train/callback/_loss_monitor.py +3 -3
- mindspore/train/callback/_on_request_exit.py +134 -31
- mindspore/train/callback/_summary_collector.py +5 -5
- mindspore/train/callback/_tft_register.py +352 -0
- mindspore/train/dataset_helper.py +7 -3
- mindspore/train/metrics/metric.py +3 -3
- mindspore/train/metrics/roc.py +4 -4
- mindspore/train/mind_ir_pb2.py +44 -39
- mindspore/train/model.py +134 -58
- mindspore/train/serialization.py +336 -112
- mindspore/turbojpeg.dll +0 -0
- mindspore/utils/__init__.py +21 -0
- mindspore/utils/utils.py +60 -0
- mindspore/version.py +1 -1
- {mindspore-2.3.0.dist-info → mindspore-2.4.0.dist-info}/METADATA +6 -2
- {mindspore-2.3.0.dist-info → mindspore-2.4.0.dist-info}/RECORD +258 -252
- mindspore/include/c_api/ms/abstract.h +0 -67
- mindspore/include/c_api/ms/attribute.h +0 -197
- mindspore/include/c_api/ms/base/handle_types.h +0 -43
- mindspore/include/c_api/ms/base/macros.h +0 -32
- mindspore/include/c_api/ms/base/status.h +0 -33
- mindspore/include/c_api/ms/base/types.h +0 -283
- mindspore/include/c_api/ms/context.h +0 -102
- mindspore/include/c_api/ms/graph.h +0 -160
- mindspore/include/c_api/ms/node.h +0 -606
- mindspore/include/c_api/ms/tensor.h +0 -161
- mindspore/include/c_api/ms/value.h +0 -84
- mindspore/mindspore_shared_lib.dll +0 -0
- mindspore/nn/extend/basic.py +0 -140
- mindspore/nn/extend/embedding.py +0 -143
- mindspore/nn/extend/layer/normalization.py +0 -109
- mindspore/nn/extend/pooling.py +0 -117
- mindspore/nn/layer/embedding_service.py +0 -531
- mindspore/ops/_op_impl/aicpu/strided_slice_v2.py +0 -93
- mindspore/ops/_op_impl/aicpu/strided_slice_v2_grad.py +0 -66
- mindspore/ops/extend/__init__.py +0 -53
- mindspore/ops/extend/array_func.py +0 -218
- mindspore/ops/extend/math_func.py +0 -76
- mindspore/ops/extend/nn_func.py +0 -308
- mindspore/ops/silent_check.py +0 -162
- mindspore/profiler/parser/msadvisor_analyzer.py +0 -82
- mindspore/profiler/parser/msadvisor_parser.py +0 -240
- mindspore/train/callback/_mindio_ttp.py +0 -443
- {mindspore-2.3.0.dist-info → mindspore-2.4.0.dist-info}/WHEEL +0 -0
- {mindspore-2.3.0.dist-info → mindspore-2.4.0.dist-info}/entry_points.txt +0 -0
- {mindspore-2.3.0.dist-info → mindspore-2.4.0.dist-info}/top_level.txt +0 -0
mindspore/nn/layer/embedding.py
CHANGED
|
@@ -16,13 +16,14 @@
|
|
|
16
16
|
from __future__ import absolute_import
|
|
17
17
|
|
|
18
18
|
import mindspore.common.dtype as mstype
|
|
19
|
+
import mindspore.ops as ops
|
|
19
20
|
from mindspore import log as logger
|
|
20
21
|
from mindspore.common.tensor import Tensor
|
|
21
22
|
from mindspore.ops import operations as P
|
|
22
23
|
from mindspore.ops import functional as F
|
|
23
24
|
from mindspore.common.parameter import Parameter
|
|
24
25
|
from mindspore.common.parameter import _get_unique_parameter_key
|
|
25
|
-
from mindspore.common.initializer import initializer
|
|
26
|
+
from mindspore.common.initializer import initializer, Normal
|
|
26
27
|
from mindspore.communication.management import get_group_size, get_rank
|
|
27
28
|
from mindspore.context import ParallelMode
|
|
28
29
|
from mindspore.parallel._utils import _get_parallel_mode, _get_full_batch
|
|
@@ -34,7 +35,7 @@ from mindspore.ops.primitive import constexpr, _primexpr
|
|
|
34
35
|
from mindspore.nn.layer.basic import ClipByNorm
|
|
35
36
|
from mindspore.nn.cell import Cell
|
|
36
37
|
|
|
37
|
-
__all__ = ['Embedding', 'EmbeddingLookup', 'MultiFieldEmbeddingLookup']
|
|
38
|
+
__all__ = ['Embedding', 'EmbeddingExt', 'EmbeddingLookup', 'MultiFieldEmbeddingLookup']
|
|
38
39
|
|
|
39
40
|
|
|
40
41
|
@_primexpr
|
|
@@ -161,6 +162,123 @@ class Embedding(Cell):
|
|
|
161
162
|
f'embedding_table={self.embedding_table}, dtype={self.dtype}, padding_idx={self.padding_idx}'
|
|
162
163
|
|
|
163
164
|
|
|
165
|
+
class EmbeddingExt(Cell):
|
|
166
|
+
r"""
|
|
167
|
+
Embedding layer.
|
|
168
|
+
Retrieve the word embeddings in weight stored in the layer using indices specified in `input`.
|
|
169
|
+
|
|
170
|
+
.. warning::
|
|
171
|
+
On Ascend, the behavior is unpredictable when the value of `input` is invalid.
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
num_embeddings (int): Size of the dictionary of embeddings.
|
|
175
|
+
embedding_dim (int): The size of each embedding vector.
|
|
176
|
+
padding_idx (int, optional): If the value is not None, the corresponding row of embedding vector
|
|
177
|
+
will not be updated in training. The value of embedding vector at `padding_idx` will default
|
|
178
|
+
to zeros when the Embedding layer is newly constructed. The value should be in range
|
|
179
|
+
`[-num_embeddings, num_embeddings)` if it's not ``None``. Default ``None``.
|
|
180
|
+
max_norm (float, optional): If the value is not None, firstly get the p-norm result of the embedding
|
|
181
|
+
vector specified by `input` where p is specified by `norm_type`; if the result is larger then `max_norm`,
|
|
182
|
+
update the embedding vector` with :math:`\frac{max\_norm}{result+1e^{-7}}`. Default ``None``.
|
|
183
|
+
norm_type (float, optional): Indicated the value of p in p-norm. Default ``2.0``.
|
|
184
|
+
scale_grad_by_freq (bool, optional): If ``True`` the gradients will be scaled by the inverse of frequency
|
|
185
|
+
of the index in `input`. Default ``False``.
|
|
186
|
+
_weight (Tensor, optional): Used to initialize the weight of Embedding. If ``None``, the weight will be
|
|
187
|
+
initialized from normal distribution :math:`{N}(\text{sigma=1.0}, \text{mean=0.0})`. Default ``None``.
|
|
188
|
+
dtype (mindspore.dtype, optional) : Dtype of Parameters. It is meaningless when `_weight` is not None.
|
|
189
|
+
Default: ``mindspore.float32``.
|
|
190
|
+
|
|
191
|
+
Inputs:
|
|
192
|
+
- **input** (Tensor) - The indices used to lookup in the embedding vector. The data type must be
|
|
193
|
+
mindspore.int32 or mindspore.int64, and the value should be in range `[0, num_embeddings)`.
|
|
194
|
+
|
|
195
|
+
Outputs:
|
|
196
|
+
Tensor, has the same data type as weight, the shape is :math:`(*input.shape, embedding\_dim)`.
|
|
197
|
+
|
|
198
|
+
Raises:
|
|
199
|
+
TypeError: If `num_embeddings` is not an int.
|
|
200
|
+
TypeError: If `embedding_dim` is not an int.
|
|
201
|
+
ValueError: If `padding_idx` is out of valid range.
|
|
202
|
+
TypeError: If `max_norm` is not a float.
|
|
203
|
+
TypeError: If `norm_type` is not a float.
|
|
204
|
+
TypeError: If `scale_grad_by_freq` is not a bool.
|
|
205
|
+
TypeError: If `dtype` is not one of mindspore.dtype.
|
|
206
|
+
|
|
207
|
+
Supported Platforms:
|
|
208
|
+
``Ascend``
|
|
209
|
+
|
|
210
|
+
Examples:
|
|
211
|
+
>>> import mindspore
|
|
212
|
+
>>> import numpy as np
|
|
213
|
+
>>> from mindspore import Tensor, nn
|
|
214
|
+
>>> input = Tensor([[1, 0, 1, 1], [0, 0, 1, 0]])
|
|
215
|
+
>>> embedding = nn.mint.nn.Embedding(num_embeddings=10, embedding_dim=3)
|
|
216
|
+
>>> output = embedding(input)
|
|
217
|
+
>>> print(output)
|
|
218
|
+
[[[-0.0024154 -0.01203444 0.00811537]
|
|
219
|
+
[ 0.00233847 -0.00596091 0.00536799]
|
|
220
|
+
[-0.0024154 -0.01203444 0.00811537]
|
|
221
|
+
[-0.0024154 -0.01203444 0.00811537]]
|
|
222
|
+
[[ 0.00233847 -0.00596091 0.00536799]
|
|
223
|
+
[ 0.00233847 -0.00596091 0.00536799]
|
|
224
|
+
[-0.0024154 -0.01203444 0.00811537]
|
|
225
|
+
[ 0.00233847 -0.00596091 0.00536799]]]
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
def __init__(self, num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2.0,
|
|
229
|
+
scale_grad_by_freq=False, _weight=None, dtype=mstype.float32):
|
|
230
|
+
"""Initialize Embedding."""
|
|
231
|
+
super().__init__()
|
|
232
|
+
self.num_embeddings = Validator.check_value_type(
|
|
233
|
+
'num_embeddings', num_embeddings, [int], self.cls_name)
|
|
234
|
+
self.embedding_dim = Validator.check_value_type(
|
|
235
|
+
'embedding_dim', embedding_dim, [int], self.cls_name)
|
|
236
|
+
Validator.check_subclass(
|
|
237
|
+
"dtype", dtype, mstype.number_type, self.cls_name)
|
|
238
|
+
self.dtype = dtype
|
|
239
|
+
self.padding_idx = padding_idx
|
|
240
|
+
if _weight is None:
|
|
241
|
+
init_tensor = Tensor(shape=[num_embeddings, embedding_dim], dtype=dtype, init=Normal(1, 0))
|
|
242
|
+
init_tensor = self._zero_weight_by_index(init_tensor)
|
|
243
|
+
self.weight = Parameter(init_tensor, name='weight')
|
|
244
|
+
else:
|
|
245
|
+
self.weight = Parameter(_weight)
|
|
246
|
+
|
|
247
|
+
self.max_norm = max_norm
|
|
248
|
+
if max_norm is not None:
|
|
249
|
+
self.max_norm = Validator.check_value_type('max_norm', max_norm, [float], self.cls_name)
|
|
250
|
+
|
|
251
|
+
self.norm_type = norm_type
|
|
252
|
+
if norm_type is not None:
|
|
253
|
+
self.norm_type = Validator.check_value_type('norm_type', norm_type,
|
|
254
|
+
[float], self.cls_name)
|
|
255
|
+
|
|
256
|
+
self.scale_grad_by_freq = scale_grad_by_freq
|
|
257
|
+
if scale_grad_by_freq is not None:
|
|
258
|
+
self.scale_grad_by_freq = Validator.check_value_type('scale_grad_by_freq',
|
|
259
|
+
scale_grad_by_freq,
|
|
260
|
+
[bool], self.cls_name)
|
|
261
|
+
|
|
262
|
+
def _zero_weight_by_index(self, init_tensor):
|
|
263
|
+
if self.padding_idx is not None:
|
|
264
|
+
self.padding_idx = Validator.check_int_range(self.padding_idx, -self.num_embeddings, self.num_embeddings,
|
|
265
|
+
Validator.INC_LEFT, "padding_idx", self.cls_name)
|
|
266
|
+
if isinstance(init_tensor, Tensor) and init_tensor.init is not None:
|
|
267
|
+
init_tensor = init_tensor.init_data()
|
|
268
|
+
init_tensor[self.padding_idx] = 0
|
|
269
|
+
|
|
270
|
+
return init_tensor
|
|
271
|
+
|
|
272
|
+
def construct(self, input):
|
|
273
|
+
return ops.embedding(input, self.weight, self.padding_idx, self.max_norm,
|
|
274
|
+
self.norm_type, self.scale_grad_by_freq)
|
|
275
|
+
|
|
276
|
+
def extend_repr(self):
|
|
277
|
+
return f'num_embeddings={self.num_embeddings}, embedding_dim={self.embedding_dim}, ' \
|
|
278
|
+
f'padding_idx={self.padding_idx}, max_norm={self.max_norm}, norm_type={self.norm_type}, ' \
|
|
279
|
+
f'scale_grad_by_freq={self.scale_grad_by_freq}, dtype={self.dtype}'
|
|
280
|
+
|
|
281
|
+
|
|
164
282
|
@_primexpr
|
|
165
283
|
def _make_axis_range(start, end):
|
|
166
284
|
axis = tuple(range(start, end))
|
|
@@ -19,7 +19,8 @@ from __future__ import division
|
|
|
19
19
|
import itertools
|
|
20
20
|
import numbers
|
|
21
21
|
import hashlib
|
|
22
|
-
|
|
22
|
+
import numpy as np
|
|
23
|
+
import mindspore.ops as ops
|
|
23
24
|
from mindspore.ops import operations as P
|
|
24
25
|
from mindspore.ops.operations import _inner_ops as inner
|
|
25
26
|
from mindspore.common.parameter import Parameter
|
|
@@ -37,7 +38,7 @@ from mindspore.nn.cell import Cell
|
|
|
37
38
|
from mindspore import log as logger
|
|
38
39
|
from mindspore.ops import group_norm
|
|
39
40
|
|
|
40
|
-
__all__ = ['BatchNorm1d', 'BatchNorm2d', 'BatchNorm3d', 'LayerNorm', 'GroupNorm',
|
|
41
|
+
__all__ = ['BatchNorm1d', 'BatchNorm2d', 'BatchNorm3d', 'LayerNorm', 'LayerNormExt', 'GroupNorm',
|
|
41
42
|
'SyncBatchNorm', 'InstanceNorm1d', 'InstanceNorm2d', 'InstanceNorm3d']
|
|
42
43
|
|
|
43
44
|
|
|
@@ -558,14 +559,14 @@ class SyncBatchNorm(_BatchNorm):
|
|
|
558
559
|
|
|
559
560
|
For the Ascend devices, users need to prepare the rank table, set rank_id and device_id.
|
|
560
561
|
Please see the `Ascend tutorial
|
|
561
|
-
<https://www.mindspore.cn/
|
|
562
|
+
<https://www.mindspore.cn/docs/en/master/model_train/parallel/rank_table.html>`_
|
|
562
563
|
for more details.
|
|
563
564
|
|
|
564
565
|
For the GPU devices, users need to prepare the host file and mpi, please see the `mpirun Startup
|
|
565
|
-
<https://www.mindspore.cn/
|
|
566
|
+
<https://www.mindspore.cn/docs/en/master/model_train/parallel/mpirun.html>`_ .
|
|
566
567
|
|
|
567
568
|
For the CPU device, users need to write a dynamic cluster startup script, please see the `Dynamic Cluster
|
|
568
|
-
Startup <https://www.mindspore.cn/
|
|
569
|
+
Startup <https://www.mindspore.cn/docs/en/master/model_train/parallel/dynamic_cluster.html>`_ .
|
|
569
570
|
|
|
570
571
|
This example should be run with multiple devices.
|
|
571
572
|
|
|
@@ -774,6 +775,101 @@ class LayerNorm(Cell):
|
|
|
774
775
|
self.normalized_shape, self.begin_norm_axis, self.begin_params_axis, self.gamma, self.beta)
|
|
775
776
|
|
|
776
777
|
|
|
778
|
+
class LayerNormExt(Cell):
|
|
779
|
+
r"""
|
|
780
|
+
Applies Layer Normalization over a mini-batch of inputs.
|
|
781
|
+
|
|
782
|
+
Layer Normalization is widely used in recurrent neural networks. It applies
|
|
783
|
+
normalization on a mini-batch of inputs for each single training case as described
|
|
784
|
+
in the paper `Layer Normalization <https://arxiv.org/pdf/1607.06450.pdf>`_.
|
|
785
|
+
|
|
786
|
+
Unlike Batch Normalization, Layer Normalization performs exactly the same computation at training and
|
|
787
|
+
testing time. It is applied across all channels and pixel but only one batch size.
|
|
788
|
+
:math:`\gamma` is the scale value learned through training and :math:`\beta` is the shift value.
|
|
789
|
+
It can be described using the following formula:
|
|
790
|
+
|
|
791
|
+
.. math::
|
|
792
|
+
y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
|
|
793
|
+
|
|
794
|
+
.. warning::
|
|
795
|
+
This is an experimental API that is subject to change or deletion.
|
|
796
|
+
|
|
797
|
+
Args:
|
|
798
|
+
normalized_shape (Union(tuple[int], list[int], int)): The normalized shape of `x` for LayerNorm
|
|
799
|
+
eps (float): A value added to the denominator for numerical stability(:math:`\epsilon`). Default: ``1e-5`` .
|
|
800
|
+
elementwise_affine (bool): Whether affine transformation is required. When this parameter is set to ``True``,
|
|
801
|
+
the weight parameter is initialized to 1 and the offset is initialized to 0. Default: ``True``.
|
|
802
|
+
bias (bool): If set to ``False``, the layer will not learn an additive bias (only relevant if
|
|
803
|
+
`elementwise_affine` is ``True``). Default: ``True``.
|
|
804
|
+
dtype (:class:`mindspore.dtype`): Dtype of Parameters. Default: ``None`` .
|
|
805
|
+
|
|
806
|
+
Inputs:
|
|
807
|
+
- **x** (Tensor) - The shape is :math:`(N, *)`, where :math:`*` is equal to normalized_shape.
|
|
808
|
+
|
|
809
|
+
Outputs:
|
|
810
|
+
Tensor, the normalized and scaled offset tensor, has the same shape and data type as the `x`.
|
|
811
|
+
|
|
812
|
+
Raises:
|
|
813
|
+
TypeError: If `eps` is not a float.
|
|
814
|
+
|
|
815
|
+
Supported Platforms:
|
|
816
|
+
``Ascend``
|
|
817
|
+
|
|
818
|
+
Examples:
|
|
819
|
+
>>> import mindspore as ms
|
|
820
|
+
>>> import numpy as np
|
|
821
|
+
>>> x = ms.Tensor(np.ones([20, 5, 10, 10]), ms.float32)
|
|
822
|
+
>>> shape1 = x.shape[1:]
|
|
823
|
+
>>> m = ms.nn.LayerNormExt(shape1)
|
|
824
|
+
>>> output = m(x).shape
|
|
825
|
+
>>> print(output)
|
|
826
|
+
(20, 5, 10, 10)
|
|
827
|
+
"""
|
|
828
|
+
|
|
829
|
+
def __init__(self,
|
|
830
|
+
normalized_shape,
|
|
831
|
+
eps=1e-5,
|
|
832
|
+
elementwise_affine=True,
|
|
833
|
+
bias=True,
|
|
834
|
+
dtype=None
|
|
835
|
+
):
|
|
836
|
+
"""Initialize LayerNormExt."""
|
|
837
|
+
super(LayerNormExt, self).__init__()
|
|
838
|
+
if isinstance(normalized_shape, numbers.Integral):
|
|
839
|
+
# mypy error: incompatible types in assignment
|
|
840
|
+
normalized_shape = (normalized_shape,) # type: ignore[assignment]
|
|
841
|
+
if not isinstance(normalized_shape, (tuple, list)):
|
|
842
|
+
raise TypeError(f"For '{self.cls_name}', the type of 'normalized_shape' must be tuple[int] or list[int], "
|
|
843
|
+
f"but got {normalized_shape} and the type is {type(normalized_shape)}.")
|
|
844
|
+
if not normalized_shape:
|
|
845
|
+
raise ValueError(
|
|
846
|
+
f"Expected normalized_shape to be at least 1-dimensional, i.e., containing at "
|
|
847
|
+
f"least one element, but got normalized_shape = {normalized_shape}"
|
|
848
|
+
)
|
|
849
|
+
self.normalized_shape = tuple(normalized_shape)
|
|
850
|
+
self.eps = eps
|
|
851
|
+
self.elementwise_affine = elementwise_affine
|
|
852
|
+
ms_dtype = mstype.float32 if dtype is None else dtype
|
|
853
|
+
if self.elementwise_affine:
|
|
854
|
+
self.weight = Parameter(Tensor(np.ones(normalized_shape), ms_dtype), name="weight")
|
|
855
|
+
if bias:
|
|
856
|
+
self.bias = Parameter(Tensor(np.zeros(normalized_shape), ms_dtype), name="bias")
|
|
857
|
+
else:
|
|
858
|
+
self.bias = None
|
|
859
|
+
else:
|
|
860
|
+
self.weight = None
|
|
861
|
+
self.bias = None
|
|
862
|
+
|
|
863
|
+
def construct(self, input):
|
|
864
|
+
y = ops.layer_norm(input, self.normalized_shape, self.weight,
|
|
865
|
+
self.bias, self.eps)
|
|
866
|
+
return y
|
|
867
|
+
|
|
868
|
+
def extend_repr(self):
|
|
869
|
+
return 'normalized_shape={}, eps={}, elementwise_affine={}'.format(
|
|
870
|
+
self.normalized_shape, self.eps, self.elementwise_affine)
|
|
871
|
+
|
|
872
|
+
|
|
777
873
|
class _InstanceNorm(Cell):
|
|
778
874
|
"""Instance Normalization base class."""
|
|
779
875
|
@cell_attr_register
|
mindspore/nn/layer/padding.py
CHANGED
|
@@ -279,20 +279,6 @@ class ConstantPad1d(_ConstantPadNd):
|
|
|
279
279
|
[0.5 1. 1. 1. 1. 0.5]]]]
|
|
280
280
|
>>> print(out.shape)
|
|
281
281
|
(1, 2, 3, 6)
|
|
282
|
-
>>> # padding is negative
|
|
283
|
-
>>> padding = (-1, 0)
|
|
284
|
-
>>> value = 0.5
|
|
285
|
-
>>> pad1d = ms.nn.ConstantPad1d(padding, value)
|
|
286
|
-
>>> out = pad1d(x)
|
|
287
|
-
>>> print(out)
|
|
288
|
-
[[[[1. 1. 1.]
|
|
289
|
-
[1. 1. 1.]
|
|
290
|
-
[1. 1. 1.]]
|
|
291
|
-
[[1. 1. 1.]
|
|
292
|
-
[1. 1. 1.]
|
|
293
|
-
[1. 1. 1.]]]]
|
|
294
|
-
>>> print(out.shape)
|
|
295
|
-
(1, 2, 3, 3)
|
|
296
282
|
"""
|
|
297
283
|
|
|
298
284
|
def __init__(self, padding, value):
|
|
@@ -336,21 +322,21 @@ class ConstantPad2d(_ConstantPadNd):
|
|
|
336
322
|
>>> import mindspore as ms
|
|
337
323
|
>>> x = np.ones(shape=(1, 2, 3, 4)).astype(np.float32)
|
|
338
324
|
>>> x = ms.Tensor(x)
|
|
339
|
-
>>> padding = (
|
|
325
|
+
>>> padding = (1, 1, 0, 1)
|
|
340
326
|
>>> value = 0.5
|
|
341
327
|
>>> pad2d = ms.nn.ConstantPad2d(padding, value)
|
|
342
328
|
>>> out = pad2d(x)
|
|
343
329
|
>>> print(out)
|
|
344
|
-
[[[[1. 1. 1. 0.5]
|
|
345
|
-
[1. 1. 1. 0.5]
|
|
346
|
-
[1. 1. 1. 0.5]
|
|
347
|
-
[0.5 0.5 0.5 0.5]]
|
|
348
|
-
[[1. 1. 1. 0.5]
|
|
349
|
-
[1. 1. 1. 0.5]
|
|
350
|
-
[1. 1. 1. 0.5]
|
|
351
|
-
[0.5 0.5 0.5 0.5]]]]
|
|
330
|
+
[[[[0.5 1. 1. 1. 1. 0.5]
|
|
331
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
332
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
333
|
+
[0.5 0.5 0.5 0.5 0.5 0.5]]
|
|
334
|
+
[[0.5 1. 1. 1. 1. 0.5]
|
|
335
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
336
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
337
|
+
[0.5 0.5 0.5 0.5 0.5 0.5]]]]
|
|
352
338
|
>>> print(out.shape)
|
|
353
|
-
(1, 2, 4,
|
|
339
|
+
(1, 2, 4, 6)
|
|
354
340
|
"""
|
|
355
341
|
|
|
356
342
|
def __init__(self, padding, value):
|
|
@@ -396,25 +382,25 @@ class ConstantPad3d(_ConstantPadNd):
|
|
|
396
382
|
>>> import mindspore as ms
|
|
397
383
|
>>> x = np.ones(shape=(1, 2, 3, 4)).astype(np.float32)
|
|
398
384
|
>>> x = ms.Tensor(x)
|
|
399
|
-
>>> padding = (
|
|
385
|
+
>>> padding = (1, 1, 0, 1, 1, 0)
|
|
400
386
|
>>> value = 0.5
|
|
401
387
|
>>> pad3d = ms.nn.ConstantPad3d(padding, value)
|
|
402
388
|
>>> out = pad3d(x)
|
|
403
389
|
>>> print(out)
|
|
404
|
-
[[[[0.5 0.5 0.5 0.5]
|
|
405
|
-
[0.5 0.5 0.5 0.5]
|
|
406
|
-
[0.5 0.5 0.5 0.5]
|
|
407
|
-
[0.5 0.5 0.5 0.5]]
|
|
408
|
-
[[1. 1. 1. 0.5]
|
|
409
|
-
[1. 1. 1. 0.5]
|
|
410
|
-
[1. 1. 1. 0.5]
|
|
411
|
-
[0.5 0.5 0.5 0.5]]
|
|
412
|
-
[[1. 1. 1. 0.5]
|
|
413
|
-
[1. 1. 1. 0.5]
|
|
414
|
-
[1. 1. 1. 0.5]
|
|
415
|
-
[0.5 0.5 0.5 0.5]]]]
|
|
390
|
+
[[[[0.5 0.5 0.5 0.5 0.5 0.5]
|
|
391
|
+
[0.5 0.5 0.5 0.5 0.5 0.5]
|
|
392
|
+
[0.5 0.5 0.5 0.5 0.5 0.5]
|
|
393
|
+
[0.5 0.5 0.5 0.5 0.5 0.5]]
|
|
394
|
+
[[0.5 1. 1. 1. 1. 0.5]
|
|
395
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
396
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
397
|
+
[0.5 0.5 0.5 0.5 0.5 0.5]]
|
|
398
|
+
[[0.5 1. 1. 1. 1. 0.5]
|
|
399
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
400
|
+
[0.5 1. 1. 1. 1. 0.5]
|
|
401
|
+
[0.5 0.5 0.5 0.5 0.5 0.5]]]]
|
|
416
402
|
>>> print(out.shape)
|
|
417
|
-
(1, 3, 4,
|
|
403
|
+
(1, 3, 4, 6)
|
|
418
404
|
"""
|
|
419
405
|
|
|
420
406
|
def __init__(self, padding, value):
|
|
@@ -652,20 +638,20 @@ class ZeroPad2d(_ConstantPadNd):
|
|
|
652
638
|
>>> import mindspore as ms
|
|
653
639
|
>>> x = np.ones(shape=(1, 2, 3, 4)).astype(np.float32)
|
|
654
640
|
>>> x = ms.Tensor(x)
|
|
655
|
-
>>> padding = (
|
|
641
|
+
>>> padding = (1, 1, 0, 1)
|
|
656
642
|
>>> pad = ms.nn.ZeroPad2d(padding)
|
|
657
643
|
>>> out = pad(x)
|
|
658
644
|
>>> print(out)
|
|
659
|
-
[[[[1. 1. 1. 0.]
|
|
660
|
-
[1. 1. 1. 0.]
|
|
661
|
-
[1. 1. 1. 0.]
|
|
662
|
-
[0. 0. 0. 0.]]
|
|
663
|
-
[[1. 1. 1. 0.]
|
|
664
|
-
[1. 1. 1. 0.]
|
|
665
|
-
[1. 1. 1. 0.]
|
|
666
|
-
[0. 0. 0. 0.]]]]
|
|
645
|
+
[[[[0. 1. 1. 1. 1. 0.]
|
|
646
|
+
[0. 1. 1. 1. 1. 0.]
|
|
647
|
+
[0. 1. 1. 1. 1. 0.]
|
|
648
|
+
[0. 0. 0. 0. 0. 0.]]
|
|
649
|
+
[[0. 1. 1. 1. 1. 0.]
|
|
650
|
+
[0. 1. 1. 1. 1. 0.]
|
|
651
|
+
[0. 1. 1. 1. 1. 0.]
|
|
652
|
+
[0. 0. 0. 0. 0. 0.]]]]
|
|
667
653
|
>>> print(out.shape)
|
|
668
|
-
(1, 2, 4,
|
|
654
|
+
(1, 2, 4, 6)
|
|
669
655
|
"""
|
|
670
656
|
|
|
671
657
|
def __init__(self, padding):
|
mindspore/nn/layer/pooling.py
CHANGED
|
@@ -18,6 +18,7 @@ from __future__ import absolute_import
|
|
|
18
18
|
from mindspore.ops import operations as P
|
|
19
19
|
from mindspore.ops import functional as F
|
|
20
20
|
import mindspore.ops as ops
|
|
21
|
+
from mindspore.ops.function.nn_func import avg_pool2d_ext
|
|
21
22
|
from mindspore._checkparam import _check_3d_int_or_tuple
|
|
22
23
|
from mindspore import _checkparam as validator
|
|
23
24
|
from mindspore.ops.primitive import constexpr, _primexpr
|
|
@@ -26,13 +27,14 @@ import mindspore.context as context
|
|
|
26
27
|
from mindspore.common import dtype as mstype
|
|
27
28
|
from mindspore.ops.operations.nn_ops import AdaptiveMaxPool2D
|
|
28
29
|
from mindspore.ops.operations.nn_ops import AdaptiveMaxPool3D, AdaptiveAvgPool3D
|
|
30
|
+
from mindspore.ops.auto_generate.gen_ops_prim import MaxPoolWithIndices, MaxPoolWithMask
|
|
29
31
|
from mindspore.nn.cell import Cell
|
|
30
32
|
from mindspore._c_expression import MSContext
|
|
31
33
|
|
|
32
34
|
__all__ = ['AvgPool3d', 'MaxPool3d', 'AvgPool2d', 'MaxPool2d', 'AvgPool1d', 'MaxPool1d', 'FractionalMaxPool2d',
|
|
33
35
|
'FractionalMaxPool3d', 'AdaptiveAvgPool1d', 'AdaptiveMaxPool1d', 'AdaptiveMaxPool2d', 'AdaptiveMaxPool3d',
|
|
34
36
|
'AdaptiveAvgPool2d', 'AdaptiveAvgPool3d', 'MaxUnpool1d', 'MaxUnpool2d', 'MaxUnpool3d', 'LPPool1d',
|
|
35
|
-
'LPPool2d']
|
|
37
|
+
'LPPool2d', 'AvgPool2dExt', 'MaxPool2dExt']
|
|
36
38
|
|
|
37
39
|
|
|
38
40
|
class _PoolNd(Cell):
|
|
@@ -96,6 +98,9 @@ class LPPool1d(Cell):
|
|
|
96
98
|
.. math::
|
|
97
99
|
f(X) = \sqrt[p]{\sum_{x \in X} x^{p}}
|
|
98
100
|
|
|
101
|
+
Note:
|
|
102
|
+
This interface currently does not support Atlas A2 training series products.
|
|
103
|
+
|
|
99
104
|
Args:
|
|
100
105
|
norm_type (Union[int, float]): Type of normalization, represents :math:`p` in the formula, can not be 0.
|
|
101
106
|
|
|
@@ -169,6 +174,9 @@ class LPPool2d(Cell):
|
|
|
169
174
|
.. math::
|
|
170
175
|
f(X) = \sqrt[p]{\sum_{x \in X} x^{p}}
|
|
171
176
|
|
|
177
|
+
Note:
|
|
178
|
+
This interface currently does not support Atlas A2 training series products.
|
|
179
|
+
|
|
172
180
|
Args:
|
|
173
181
|
norm_type(Union[int, float]): Type of normalization, represents :math:`p` in the formula, can not be 0.
|
|
174
182
|
|
|
@@ -374,6 +382,7 @@ class MaxPool3d(_PoolNd):
|
|
|
374
382
|
Examples:
|
|
375
383
|
>>> import mindspore as ms
|
|
376
384
|
>>> import mindspore.nn as nn
|
|
385
|
+
>>> from mindspore import Tensor
|
|
377
386
|
>>> import numpy as np
|
|
378
387
|
>>> np_x = np.random.randint(0, 10, [5, 3, 4, 6, 7])
|
|
379
388
|
>>> x = Tensor(np_x, ms.float32)
|
|
@@ -592,6 +601,102 @@ class MaxPool2d(_PoolNd):
|
|
|
592
601
|
return out
|
|
593
602
|
|
|
594
603
|
|
|
604
|
+
class MaxPool2dExt(Cell):
|
|
605
|
+
r"""
|
|
606
|
+
Applies a 2D max pooling over an input Tensor which can be regarded as a composition of 2D planes.
|
|
607
|
+
|
|
608
|
+
Typically the input is of shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})`, MaxPool2d outputs
|
|
609
|
+
regional maximum in the :math:`(H_{in}, W_{in})`-dimension. Given kernel size
|
|
610
|
+
:math:`(h_{ker}, w_{ker})` and stride :math:`(s_0, s_1)`, the operation is as follows.
|
|
611
|
+
|
|
612
|
+
.. math::
|
|
613
|
+
\text{output}(N_i, C_j, h, w) = \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1}
|
|
614
|
+
\text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)
|
|
615
|
+
|
|
616
|
+
.. warning::
|
|
617
|
+
Only support on Atlas training series.
|
|
618
|
+
|
|
619
|
+
Args:
|
|
620
|
+
kernel_size (Union[int, tuple[int]]): The size of kernel used to take the max value,
|
|
621
|
+
is an int number or a single element tuple that represents height and width are both kernel_size,
|
|
622
|
+
or a tuple of two int numbers that represent height and width respectively.
|
|
623
|
+
Default: ``1`` .
|
|
624
|
+
stride (Union[int, tuple[int], None]): The distance of kernel moving, an int number or a single element tuple
|
|
625
|
+
that represents the height and width of movement are both stride, or a tuple of two int numbers that
|
|
626
|
+
represent height and width of movement respectively.
|
|
627
|
+
Default: ``None`` , which indicates the moving step is `kernel_size` .
|
|
628
|
+
padding (Union(int, tuple[int], list[int])): Specifies the padding value of the pooling operation.
|
|
629
|
+
Default: ``0`` . `padding` can only be an integer or a tuple/list containing one or two integers. If
|
|
630
|
+
`padding` is an integer or a tuple/list containing one integer, it will be padded `padding` times in the
|
|
631
|
+
four directions of the input. If `padding` is a tuple/list containing two integers, it will be padded
|
|
632
|
+
`padding[0]` times in the up-down direction of the input and `padding[1]` times in the left-right direction
|
|
633
|
+
of the input.
|
|
634
|
+
dilation (Union(int, tuple[int])): The spacing between the elements of the kernel in convolution,
|
|
635
|
+
used to increase the receptive field of the pooling operation. If it is a tuple, it must contain one or two
|
|
636
|
+
integers. Default: ``1`` .
|
|
637
|
+
return_indices (bool): If ``True`` , the function will return both the result of max pooling and the indices of
|
|
638
|
+
the max elements. Default: ``False`` .
|
|
639
|
+
ceil_mode (bool): If ``True`` , use ceil to compute the output shape instead of floor. Default: ``False`` .
|
|
640
|
+
|
|
641
|
+
Inputs:
|
|
642
|
+
- **input** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
|
643
|
+
|
|
644
|
+
Outputs:
|
|
645
|
+
If `return_indices` is ``False`` , return a Tensor `output`, else return a tuple (`output`, `argmax`).
|
|
646
|
+
|
|
647
|
+
- **output** (Tensor) - Maxpooling result, with shape :math:`(N_{out}, C_{out}, H_{out}, W_{out})`. It has the
|
|
648
|
+
same data type as `input`.
|
|
649
|
+
- **argmax** (Tensor) - Index corresponding to the maximum value. Data type is int32.
|
|
650
|
+
|
|
651
|
+
.. math::
|
|
652
|
+
H_{out} = \left\lfloor\frac{H_{in} + 2 * \text{padding[0]} - \text{dilation[0]}
|
|
653
|
+
\times (\text{kernel_size[0]} - 1) - 1}{\text{stride[0]}} + 1\right\rfloor
|
|
654
|
+
|
|
655
|
+
.. math::
|
|
656
|
+
W_{out} = \left\lfloor\frac{W_{in} + 2 * \text{padding[1]} - \text{dilation[1]}
|
|
657
|
+
\times (\text{kernel_size[1]} - 1) - 1}{\text{stride[1]}} + 1\right\rfloor
|
|
658
|
+
|
|
659
|
+
Raises:
|
|
660
|
+
TypeError: If `input` is not a Tensor.
|
|
661
|
+
ValueError: If length of shape of `input` is not equal to 4.
|
|
662
|
+
TypeError: If `kernel_size` , `stride` , `padding` or `dilation` is not int or tuple.
|
|
663
|
+
ValueError: If `kernel_size`, `stride` or `dilation` is less than 1.
|
|
664
|
+
ValueError: If `dilation` is not all 1.
|
|
665
|
+
ValueError: If `padding` is less than 0.
|
|
666
|
+
ValueError: If `padding` is more than half of `kernel_size`.
|
|
667
|
+
TypeError: If `ceil_mode` is not bool.
|
|
668
|
+
|
|
669
|
+
Supported Platforms:
|
|
670
|
+
``Ascend``
|
|
671
|
+
|
|
672
|
+
Examples:
|
|
673
|
+
>>> import mindspore as ms
|
|
674
|
+
>>> import numpy as np
|
|
675
|
+
>>> pool = ms.mint.nn.MaxPool2d(kernel_size=3, stride=1)
|
|
676
|
+
>>> input = ms.Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), ms.float32)
|
|
677
|
+
>>> output = pool(input)
|
|
678
|
+
>>> print(output.shape)
|
|
679
|
+
(1, 2, 2, 2)
|
|
680
|
+
"""
|
|
681
|
+
|
|
682
|
+
def __init__(self, kernel_size=1, stride=None, padding=0, dilation=1, return_indices=False,
|
|
683
|
+
ceil_mode=False):
|
|
684
|
+
"""Initialize MaxPool2d."""
|
|
685
|
+
super(MaxPool2dExt, self).__init__()
|
|
686
|
+
self.return_indices = return_indices
|
|
687
|
+
strides = stride if (stride is not None) else kernel_size
|
|
688
|
+
if return_indices:
|
|
689
|
+
self.max_pool_func_ = MaxPoolWithIndices(kernel_size, strides, padding, dilation, ceil_mode)
|
|
690
|
+
else:
|
|
691
|
+
self.max_pool_func_ = MaxPoolWithMask(kernel_size, strides, padding, dilation, ceil_mode)
|
|
692
|
+
|
|
693
|
+
def construct(self, input):
|
|
694
|
+
out, indices = self.max_pool_func_(input)
|
|
695
|
+
if self.return_indices:
|
|
696
|
+
return out, indices
|
|
697
|
+
return out
|
|
698
|
+
|
|
699
|
+
|
|
595
700
|
class MaxPool1d(_PoolNd):
|
|
596
701
|
r"""
|
|
597
702
|
Applies a 1D max pooling over an input Tensor which can be regarded as a composition of 1D planes.
|
|
@@ -793,6 +898,9 @@ class AvgPool3d(_PoolNd):
|
|
|
793
898
|
\frac{1}{d_{ker} * h_{ker} * w_{ker}} \sum_{l=0}^{d_{ker}-1} \sum_{m=0}^{h_{ker}-1} \sum_{n=0}^{w_{ker}-1}
|
|
794
899
|
\text{input}(N_i, C_j, s_0 \times d + l, s_1 \times h + m, s_2 \times w + n)
|
|
795
900
|
|
|
901
|
+
Note:
|
|
902
|
+
This interface currently does not support Atlas A2 training series products.
|
|
903
|
+
|
|
796
904
|
Args:
|
|
797
905
|
kernel_size (Union[int, tuple[int]], optional): The size of kernel used to take the average value,
|
|
798
906
|
can be an int number or a single element tuple that represents depth, height and width, or a tuple of three
|
|
@@ -910,6 +1018,46 @@ class AvgPool3d(_PoolNd):
|
|
|
910
1018
|
return out
|
|
911
1019
|
|
|
912
1020
|
|
|
1021
|
+
class AvgPool2dExt(Cell):
|
|
1022
|
+
r"""
|
|
1023
|
+
Applies a 2D average pooling over an input Tensor which can be regarded as
|
|
1024
|
+
a composition of 2D input planes.
|
|
1025
|
+
|
|
1026
|
+
For details, please refer to :func:`mindspore.mint.nn.functional.avg_pool2d`.
|
|
1027
|
+
|
|
1028
|
+
Supported Platforms:
|
|
1029
|
+
``Ascend``
|
|
1030
|
+
|
|
1031
|
+
Examples:
|
|
1032
|
+
>>> import numpy as np
|
|
1033
|
+
>>> from mindspore import Tensor, nn
|
|
1034
|
+
>>> from mindspore import dtype as mstype
|
|
1035
|
+
>>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4), mstype.float32)
|
|
1036
|
+
>>> m = nn.AvgPool2dExt(x, kernel_size=2, stride=1)
|
|
1037
|
+
>>> output = m(x)
|
|
1038
|
+
>>> print(output)
|
|
1039
|
+
[[[[ 2.5 3.5 4.5]
|
|
1040
|
+
[ 6.5 7.5 8.5]]
|
|
1041
|
+
[[14.5 15.5 16.5]
|
|
1042
|
+
[18.5 19.5 20.5]]
|
|
1043
|
+
[[26.5 27.5 28.5]
|
|
1044
|
+
[30.5 31.5 32.5]]]]
|
|
1045
|
+
"""
|
|
1046
|
+
def __init__(self, kernel_size, stride=None, padding=0, ceil_mode=False,
|
|
1047
|
+
count_include_pad=True, divisor_override=None):
|
|
1048
|
+
super(AvgPool2dExt, self).__init__()
|
|
1049
|
+
self.kernel_size = kernel_size
|
|
1050
|
+
self.stride = stride
|
|
1051
|
+
self.padding = padding
|
|
1052
|
+
self.ceil_mode = ceil_mode
|
|
1053
|
+
self.count_include_pad = count_include_pad
|
|
1054
|
+
self.divisor_override = divisor_override
|
|
1055
|
+
|
|
1056
|
+
def construct(self, input):
|
|
1057
|
+
return avg_pool2d_ext(input, self.kernel_size, self.stride, self.padding,
|
|
1058
|
+
self.ceil_mode, self.count_include_pad, self.divisor_override)
|
|
1059
|
+
|
|
1060
|
+
|
|
913
1061
|
class AvgPool2d(_PoolNd):
|
|
914
1062
|
r"""
|
|
915
1063
|
Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes.
|
|
@@ -922,6 +1070,9 @@ class AvgPool2d(_PoolNd):
|
|
|
922
1070
|
\text{output}(N_i, C_j, h, w) = \frac{1}{h_{ker} * w_{ker}} \sum_{m=0}^{h_{ker}-1} \sum_{n=0}^{w_{ker}-1}
|
|
923
1071
|
\text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)
|
|
924
1072
|
|
|
1073
|
+
Note:
|
|
1074
|
+
This interface currently does not support Atlas A2 training series products.
|
|
1075
|
+
|
|
925
1076
|
Args:
|
|
926
1077
|
kernel_size (Union[int, tuple[int]]): The size of kernel used to take the average value.
|
|
927
1078
|
The data type of kernel_size must be int or a single element tuple and the value represents the height
|
|
@@ -1015,12 +1166,12 @@ class AvgPool2d(_PoolNd):
|
|
|
1015
1166
|
data_format="NCHW"):
|
|
1016
1167
|
"""Initialize AvgPool2d."""
|
|
1017
1168
|
super(AvgPool2d, self).__init__(kernel_size, stride, pad_mode, data_format)
|
|
1018
|
-
self.
|
|
1169
|
+
self.ascend_910b_target = (MSContext.get_instance().get_ascend_soc_version() in ['ascend910b', 'ascend910_93'])
|
|
1019
1170
|
if pad_mode.upper() == 'PAD' or padding != 0 or ceil_mode or not count_include_pad \
|
|
1020
1171
|
or divisor_override is not None:
|
|
1021
|
-
if self.
|
|
1022
|
-
raise ValueError(f"For '{self.cls_name}, the pad_mod 'PAD' is not support in
|
|
1023
|
-
f"it will be supported in the future.")
|
|
1172
|
+
if self.ascend_910b_target:
|
|
1173
|
+
raise ValueError(f"For '{self.cls_name}, the pad_mod 'PAD' is not support in Ascend910B or Ascend910_93"
|
|
1174
|
+
f" now, it will be supported in the future.")
|
|
1024
1175
|
if self.format == "NHWC":
|
|
1025
1176
|
raise ValueError(f"For '{self.cls_name}, the 'NHWC' format are not support when 'pad_mode' is 'pad' or "
|
|
1026
1177
|
f"'padding' is not 0 or 'ceil_mode' is not False or 'count_include_pad' is not True"
|
|
@@ -1083,6 +1234,9 @@ class AvgPool1d(_PoolNd):
|
|
|
1083
1234
|
\text{output}(N_i, C_j, l) = \frac{1}{l_{ker}} \sum_{n=0}^{l_{ker}-1}
|
|
1084
1235
|
\text{input}(N_i, C_j, s_0 \times l + n)
|
|
1085
1236
|
|
|
1237
|
+
Note:
|
|
1238
|
+
This interface currently does not support Atlas A2 training series products.
|
|
1239
|
+
|
|
1086
1240
|
Args:
|
|
1087
1241
|
kernel_size (int): The size of kernel window used to take the average value, Default: ``1`` .
|
|
1088
1242
|
stride (int): The distance of kernel moving, an int number that represents
|
|
@@ -1682,7 +1836,7 @@ class AdaptiveMaxPool3d(Cell):
|
|
|
1682
1836
|
|
|
1683
1837
|
class FractionalMaxPool2d(Cell):
|
|
1684
1838
|
r"""
|
|
1685
|
-
Applies the 2D FractionalMaxPool
|
|
1839
|
+
Applies the 2D FractionalMaxPool operation over input. The output Tensor shape can be determined by either
|
|
1686
1840
|
`output_size` or `output_ratio`, and the step size is determined by `_random_samples`. `output_size` will take
|
|
1687
1841
|
effect when `output_size` and `output_ratio` are set at the same time.
|
|
1688
1842
|
And `output_size` and `output_ratio` can not be ``None`` at the same time.
|
|
@@ -1783,7 +1937,7 @@ class FractionalMaxPool2d(Cell):
|
|
|
1783
1937
|
|
|
1784
1938
|
class FractionalMaxPool3d(Cell):
|
|
1785
1939
|
r"""
|
|
1786
|
-
Applies the 3D FractionalMaxPool
|
|
1940
|
+
Applies the 3D FractionalMaxPool operation over `input`. The output Tensor shape can be determined by either
|
|
1787
1941
|
`output_size` or `output_ratio`, and the step size is determined by `_random_samples`. `output_size` will take
|
|
1788
1942
|
effect when `output_size` and `output_ratio` are set at the same time.
|
|
1789
1943
|
And `output_size` and `output_ratio` can not be ``None`` at the same time.
|