!44913 暂时回退reduce输入不转属性,待图算适配后再合入

Merge pull request !44913 from huoxinyou/1101reduce_rollback
This commit is contained in:
i-robot 2022-11-02 03:44:14 +00:00 committed by Gitee
commit 5bffe2984e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
20 changed files with 188 additions and 178 deletions

View File

@ -1,7 +1,7 @@
mindspore.ops.ReduceSum
=========================
.. py:class:: mindspore.ops.ReduceSum(keep_dims=False, skip_mode=False)
.. py:class:: mindspore.ops.ReduceSum(keep_dims=False)
默认情况下输出Tensor各维度上的和以达到对所有维度进行归约的目的。也可以对指定维度进行求和归约。
@ -9,18 +9,15 @@ mindspore.ops.ReduceSum
参数:
- **keep_dims** (bool) - 如果为True则保留计算维度长度为1。如果为False则不保留计算维度。默认值False输出结果会降低维度。
- **skip_mode** (bool) - 如果为True并且axis为空tuple或空list不进行ReduceSum计算,axis为其他值正常运算。如果为False则正常进行运算。默认值False。
输入:
- **x** (Tensor[Number]) - ReduceSum的输入其数据类型为Number。shape :math:`(N, *)` ,其中 :math:`*` 表示任意数量的附加维度。秩应小于8。
- **axis** (Union[int, tuple(int), list(int)]) - 要减少的维度。默认值: ()当skip_mode为False时缩小所有维度。只允许常量值,取值范围[-rank(`x`), rank(`x`))。
- **axis** (Union[int, tuple(int), list(int)]) - 要减少的维度。默认值: (),缩小所有维度。只允许常量值,取值范围[-rank(`x`), rank(`x`))。
输出:
Tensor具有与输入 `x` 相同的shape。
- 如果轴为()且keep_dims为Falseskip_mode为False则输出一个0维Tensor表示输入Tensor中所有元素的和。
- 如果轴为()且skip_mode为True则不进行ReduceSum运算输出Tensor等于输入Tensor。
- 如果轴为()且keep_dims为False则输出一个0维Tensor表示输入Tensor中所有元素的和。
- 如果轴为int取值为2并且keep_dims为False则输出的shape为 :math:`(x_1, x_3, ..., x_R)`
@ -28,6 +25,5 @@ mindspore.ops.ReduceSum
异常:
- **TypeError** - `keep_dims` 不是bool。
- **TypeError** - `skip_mode` 不是bool。
- **TypeError** - `x` 不是Tensor。
- **ValueError** - `axis` 取值为None。

View File

@ -311,7 +311,12 @@ void InferOp(const CNodePtr &cnode, void *args) {
}
kernel::KernelArgs kernel_args;
InferShape(cnode, &kernel_args.depend_tensor_map, args);
if (AnfAlgo::IsDynamicShapeSkipExecute(cnode)) {
std::vector<TypeId> dtypes{common::AnfAlgo::GetOutputInferDataType(cnode, 0)};
common::AnfAlgo::SetOutputInferTypeAndShape(dtypes, {AnfAlgo::GetInputDeviceShape(cnode, 0)}, cnode.get());
} else {
InferShape(cnode, &kernel_args.depend_tensor_map, args);
}
if (auto kernel_mod_type = kernel_mod->GetKernelModType(); IsCpuGpuKernelMod(kernel_mod_type)) {
auto update = kernel::AbstractArgsFromCNode(cnode, IsDeprecatedCpuOrGpuKernelMod(kernel_mod_type));

View File

@ -257,21 +257,12 @@ tensor::TensorPtr CreateTensorWithValueTuple(const ValueTuplePtr &value_tuple_pt
return tensor;
}
tensor::TensorPtr CreateEmptyTupleTensor() {
std::vector<int64_t> tensor_shape = {0};
tensor::TensorPtr tensor = std::make_shared<tensor::Tensor>(kInt64->type_id(), tensor_shape);
MS_EXCEPTION_IF_NULL(tensor);
tensor::DeviceInfo device_info{kOpFormat_DEFAULT, kInt64};
tensor->set_device_info(device_info);
return tensor;
}
tensor::TensorPtr CreateTupleTensor(const ValueTuplePtr &value_tuple) {
MS_EXCEPTION_IF_NULL(value_tuple);
tensor::TensorPtr tensor = nullptr;
if (value_tuple->value().empty()) {
tensor = CreateEmptyTupleTensor();
return tensor;
MS_LOG(WARNING) << "The value tuple is empty.";
return nullptr;
}
ValuePtr v = *(value_tuple->value().begin());
MS_EXCEPTION_IF_NULL(v);

View File

@ -1359,9 +1359,12 @@ void AnfRuntimeAlgorithm::UpdateGraphValidRefPair(const KernelGraphPtr &graph) {
graph->set_ref_out_in_map(new_ref_map);
}
bool AnfRuntimeAlgorithm::IsDynamicShapeSkipExecute(const bool skip_mode, const ShapeVector &axes_shape) {
bool AnfRuntimeAlgorithm::IsDynamicShapeSkipExecute(const std::string &op_name, const ShapeVector &axes_shape) {
// Skip run ReduceSum when axis is a Empty Tensor
if (std::any_of(axes_shape.begin(), axes_shape.end(), [](int64_t shape) { return shape == 0; }) && skip_mode) {
if (op_name != kReduceSumOpName) {
return false;
}
if (std::any_of(axes_shape.begin(), axes_shape.end(), [](int64_t shape) { return shape == 0; })) {
return true;
}
return false;
@ -1375,11 +1378,6 @@ bool AnfRuntimeAlgorithm::IsDynamicShapeSkipExecute(const CNodePtr &cnode) {
return false;
}
bool skip_mode = false;
if (common::AnfAlgo::HasNodeAttr(kAttrSkipMode, cnode)) {
skip_mode = common::AnfAlgo::GetNodeAttr<bool>(cnode, kAttrSkipMode);
}
const size_t axes_index = 1;
if (cnode->inputs().size() <= axes_index + 1) {
return false;
@ -1391,7 +1389,7 @@ bool AnfRuntimeAlgorithm::IsDynamicShapeSkipExecute(const CNodePtr &cnode) {
MS_EXCEPTION_IF_NULL(axes_abs);
auto axes_shape = AnfAlgo::GetInputDeviceShape(cnode, axes_index);
if (axes_abs->isa<abstract::AbstractTensor>()) {
if (std::any_of(axes_shape.begin(), axes_shape.end(), [](int64_t shape) { return shape == 0; }) && skip_mode) {
if (std::any_of(axes_shape.begin(), axes_shape.end(), [](int64_t shape) { return shape == 0; })) {
return true;
}
}

View File

@ -172,7 +172,7 @@ class BACKEND_EXPORT AnfRuntimeAlgorithm {
static void CacheAddrForAtomicClean(const AnfNodePtr &node, kernel::KernelMod *kernel_mod);
static void UpdateGraphValidRefPair(const KernelGraphPtr &graph);
static bool IsDynamicShapeSkipExecute(const bool skip_mode, const ShapeVector &axes_shape);
static bool IsDynamicShapeSkipExecute(const std::string &op_name, const ShapeVector &axes_shape);
static bool IsDynamicShapeSkipExecute(const CNodePtr &cnode);
// return true if need to update output's shape and type after launch
static bool IsNeedUpdateShapeAndTypeAfterLaunch(const AnfNodePtr &cnode);

View File

@ -465,7 +465,6 @@ constexpr auto kAttrReshapeType = "reshape_type";
constexpr auto kAttrAxis = "axis";
constexpr auto kAttrAxes = "axes";
constexpr auto kAttrKeepDims = "keep_dims";
constexpr auto kAttrSkipMode = "skip_mode";
constexpr auto kAttrShapeGamma = "shape_gamma";
constexpr auto kAttrPerm = "perm";
constexpr auto kAttrTransposeFirst = "transpose_first";

View File

@ -27,6 +27,8 @@ RER_ASCEND_DYNAMIC_CONST_TO_ATTR(kConcatOpName, 0);
RER_ASCEND_DYNAMIC_CONST_TO_ATTR(kEmbeddingLookupOpName, 2, 3, 4, 5);
RER_ASCEND_DYNAMIC_CONST_TO_ATTR(kExpandDimsOpName, 1);
RER_ASCEND_DYNAMIC_CONST_TO_ATTR(kGatherDGradV2OpName, 1);
RER_ASCEND_DYNAMIC_CONST_TO_ATTR(kReduceAllOpName, 1);
RER_ASCEND_DYNAMIC_CONST_TO_ATTR(kReduceAnyOpName, 1);
RER_ASCEND_DYNAMIC_CONST_TO_ATTR(kROIAlignGradName, 2);
RER_ASCEND_STATIC_CONST_TO_ATTR(kApplyRMSPropOpName, 5, 6, 7);

View File

@ -50,7 +50,7 @@ void EmbeddingLookUpCommGradCpuKernelMod::InitKernel(const CNodePtr &kernel_node
if (IsDynamic(input_shape_)) {
return;
}
if (input_shape_.empty()) {
if (input_shape_.size() < 1) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_
<< "', the dimension of input must be at least 1-D, but got: " << input_shape_.size() << "-D";
}

View File

@ -23,7 +23,7 @@ namespace kernel {
namespace {
constexpr size_t kEmbeddingLookupInputsNum = 3;
constexpr size_t kEmbeddingLookUpInputParamsMaxDim = 2;
constexpr size_t kOffsetIndex = 2;
constexpr size_t kIndex2 = 2;
using KernelRunFunc = EmbeddingLookUpCpuKernelMod::KernelRunFunc;
#define ADD_KERNEL(input_params_dtype, input_indices_dtype, output_dtype, input_params_type, input_indices_type) \
@ -152,7 +152,7 @@ bool EmbeddingLookUpCpuKernelMod::LaunchKernel(const std::vector<AddressPtr> &in
T *input_params_addr = reinterpret_cast<T *>(inputs[0]->addr);
S *input_indices_addr = reinterpret_cast<S *>(inputs[1]->addr);
T *output_addr = reinterpret_cast<T *>(outputs[0]->addr);
G offset = static_cast<G *>(inputs[kOffsetIndex]->addr)[0];
G offset = static_cast<G *>(inputs[kIndex2]->addr)[0];
offset_ = static_cast<int64_t>(offset);
auto task = [&](size_t start, size_t end) {

View File

@ -81,7 +81,6 @@ class ReduceCpuKernelFunc : public CpuKernelFunc {
bool simple_execute_{false};
std::string kernel_name_;
bool need_skip_execute_{false};
bool skip_mode_{false};
};
template <typename T>
@ -235,11 +234,13 @@ int ReduceCpuKernelFunc<T>::Resize(const BaseOperatorPtr &base_operator, const s
const std::vector<KernelTensorPtr> &,
const std::map<uint32_t, tensor::TensorPtr> &inputsOnHost) {
input_shape_ = inputs[0]->GetDeviceShapeAdaptively();
if (!TryGetIntValue(inputs, kIndex1, kernel_name_, &axis_, false)) {
MS_LOG(EXCEPTION) << "For " << kernel_name_ << " can't get axis input! ";
auto kernel_ptr = std::dynamic_pointer_cast<ops::Reduce>(base_operator);
if (kernel_ptr->HasAttr(kAttrAxis)) {
axis_ = kernel_ptr->get_axis();
}
(void)TryGetIntValue(inputs, kAxisIndex_, kernel_name_, &axis_, false);
if (inputs.size() > kAxisIndex_ &&
AnfAlgo::IsDynamicShapeSkipExecute(skip_mode_, inputs[kAxisIndex_]->GetShapeVector())) {
AnfAlgo::IsDynamicShapeSkipExecute(kernel_name_, inputs[kAxisIndex_]->GetShapeVector())) {
need_skip_execute_ = true;
} else {
need_skip_execute_ = false;
@ -301,8 +302,6 @@ void ReduceCpuKernelFunc<T>::InitFunc(const BaseOperatorPtr &base_operator, cons
MS_EXCEPTION_IF_NULL(base_operator);
kernel_name_ = base_operator->name();
ChooseFunc(kernel_name_);
auto kernel_ptr = std::dynamic_pointer_cast<ops::Reduce>(base_operator);
skip_mode_ = kernel_ptr->get_skip_mode();
}
template <typename T>
@ -443,8 +442,20 @@ using SpecializeReduceFuncCreator = std::function<std::shared_ptr<CpuKernelFunc>
#define REDUCE_CPU_REG(MS_T, MS_S, T) \
KernelAttr().AddInputAttr(MS_T).AddInputAttr(MS_S).AddOutputAttr(MS_T), SpecializeReduceFunc<T>
static std::vector<std::pair<KernelAttr, SpecializeReduceFuncCreator>> kernel_all_any_list = {
{REDUCE_CPU_REG(kNumberTypeBool, kNumberTypeInt32, bool)}, {REDUCE_CPU_REG(kNumberTypeBool, kNumberTypeInt64, bool)}};
{KernelAttr().AddInputAttr(kNumberTypeBool).AddOutputAttr(kNumberTypeBool), SpecializeReduceFunc<bool>},
{REDUCE_CPU_REG(kNumberTypeBool, kNumberTypeInt32, bool)},
{REDUCE_CPU_REG(kNumberTypeBool, kNumberTypeInt64, bool)}};
static std::vector<std::pair<KernelAttr, SpecializeReduceFuncCreator>> kernel_max_min_list = {
{KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), SpecializeReduceFunc<float>},
{KernelAttr().AddInputAttr(kNumberTypeFloat64).AddOutputAttr(kNumberTypeFloat64), SpecializeReduceFunc<double>},
{KernelAttr().AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8), SpecializeReduceFunc<int8_t>},
{KernelAttr().AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16), SpecializeReduceFunc<int16_t>},
{KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), SpecializeReduceFunc<int32_t>},
{KernelAttr().AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64), SpecializeReduceFunc<int64_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt8).AddOutputAttr(kNumberTypeUInt8), SpecializeReduceFunc<uint8_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt16).AddOutputAttr(kNumberTypeUInt16), SpecializeReduceFunc<uint16_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt32).AddOutputAttr(kNumberTypeUInt32), SpecializeReduceFunc<uint32_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt64).AddOutputAttr(kNumberTypeUInt64), SpecializeReduceFunc<uint64_t>},
{REDUCE_CPU_REG(kNumberTypeFloat32, kNumberTypeInt32, float)},
{REDUCE_CPU_REG(kNumberTypeFloat32, kNumberTypeInt64, float)},
{REDUCE_CPU_REG(kNumberTypeFloat64, kNumberTypeInt32, double)},
@ -466,6 +477,20 @@ static std::vector<std::pair<KernelAttr, SpecializeReduceFuncCreator>> kernel_ma
{REDUCE_CPU_REG(kNumberTypeUInt64, kNumberTypeInt32, uint64_t)},
{REDUCE_CPU_REG(kNumberTypeUInt64, kNumberTypeInt64, uint64_t)}};
static std::vector<std::pair<KernelAttr, SpecializeReduceFuncCreator>> kernel_sum_prod_mean_list = {
{KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), SpecializeReduceFunc<float>},
{KernelAttr().AddInputAttr(kNumberTypeFloat64).AddOutputAttr(kNumberTypeFloat64), SpecializeReduceFunc<double>},
{KernelAttr().AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8), SpecializeReduceFunc<int8_t>},
{KernelAttr().AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16), SpecializeReduceFunc<int16_t>},
{KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), SpecializeReduceFunc<int32_t>},
{KernelAttr().AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64), SpecializeReduceFunc<int64_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt8).AddOutputAttr(kNumberTypeUInt8), SpecializeReduceFunc<uint8_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt16).AddOutputAttr(kNumberTypeUInt16), SpecializeReduceFunc<uint16_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt32).AddOutputAttr(kNumberTypeUInt32), SpecializeReduceFunc<uint32_t>},
{KernelAttr().AddInputAttr(kNumberTypeUInt64).AddOutputAttr(kNumberTypeUInt64), SpecializeReduceFunc<uint64_t>},
{KernelAttr().AddInputAttr(kNumberTypeComplex64).AddOutputAttr(kNumberTypeComplex64),
SpecializeReduceFunc<complex64>},
{KernelAttr().AddInputAttr(kNumberTypeComplex128).AddOutputAttr(kNumberTypeComplex128),
SpecializeReduceFunc<complex128>},
{REDUCE_CPU_REG(kNumberTypeFloat32, kNumberTypeInt32, float)},
{REDUCE_CPU_REG(kNumberTypeFloat32, kNumberTypeInt64, float)},
{REDUCE_CPU_REG(kNumberTypeFloat64, kNumberTypeInt32, double)},

View File

@ -25,6 +25,13 @@ namespace mindspore::opt {
RER_CPU_DYNAMIC_CONST_TO_ATTR(kCastOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kConcatOpName, 0);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kFillOpName, 0);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kReduceAllOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kReduceAnyOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kReduceMaxOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kReduceMeanOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kReduceMinOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kReduceSumOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kReduceProdOpName, 1);
RER_CPU_DYNAMIC_CONST_TO_ATTR(kTransposeOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kApplyRMSPropOpName, 5, 6, 7);
@ -51,6 +58,13 @@ RER_CPU_STATIC_CONST_TO_ATTR(kParallelResizeBilinearGradOpName, 2);
RER_CPU_STATIC_CONST_TO_ATTR(kPullWeightOpName, 1, 2);
RER_CPU_STATIC_CONST_TO_ATTR(kPushOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kPushWeightOpName, 1, 2);
RER_CPU_STATIC_CONST_TO_ATTR(kReduceAllOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kReduceAnyOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kReduceMaxOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kReduceMeanOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kReduceMinOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kReduceProdOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kReduceSumOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kReshapeOpName, 1);
RER_CPU_STATIC_CONST_TO_ATTR(kROIAlignGradName, 2);
RER_CPU_STATIC_CONST_TO_ATTR(kSimpleMeanGradOpName, 1);

View File

@ -48,51 +48,72 @@ const std::map<std::string, cudnnReduceTensorOp_t> kReduceTypeMap = {
{"ReduceProd", CUDNN_REDUCE_TENSOR_MUL},
};
#define REDUCE_REGISTER(INPUTX, AXIS, T) \
#define STATIC_REGISTER(INPUTX, T) \
KernelAttr().AddInputAttr(INPUTX).AddOutputAttr(INPUTX), &ArrayReduceGpuKernelMod::LaunchKernel<T>
#define DYN_REGISTER(INPUTX, AXIS, T) \
KernelAttr().AddInputAttr(INPUTX).AddInputAttr(AXIS).AddOutputAttr(INPUTX), &ArrayReduceGpuKernelMod::LaunchKernel<T>
std::vector<std::pair<KernelAttr, ArrayReduceGpuKernelMod::ReduceFunc>> ArrayReduceGpuKernelMod::all_any_list_ = {
{REDUCE_REGISTER(kNumberTypeBool, kNumberTypeInt32, bool)},
{REDUCE_REGISTER(kNumberTypeBool, kNumberTypeInt64, bool)}};
{STATIC_REGISTER(kNumberTypeBool, bool)},
{DYN_REGISTER(kNumberTypeBool, kNumberTypeInt32, bool)},
{DYN_REGISTER(kNumberTypeBool, kNumberTypeInt64, bool)}};
std::vector<std::pair<KernelAttr, ArrayReduceGpuKernelMod::ReduceFunc>> ArrayReduceGpuKernelMod::prod_list_ = {
{REDUCE_REGISTER(kNumberTypeInt8, kNumberTypeInt32, int8_t)},
{REDUCE_REGISTER(kNumberTypeInt8, kNumberTypeInt64, int8_t)},
{REDUCE_REGISTER(kNumberTypeFloat16, kNumberTypeInt32, half)},
{REDUCE_REGISTER(kNumberTypeFloat16, kNumberTypeInt64, half)},
{REDUCE_REGISTER(kNumberTypeFloat32, kNumberTypeInt32, float)},
{REDUCE_REGISTER(kNumberTypeFloat32, kNumberTypeInt64, float)},
{REDUCE_REGISTER(kNumberTypeFloat64, kNumberTypeInt32, double)},
{REDUCE_REGISTER(kNumberTypeFloat64, kNumberTypeInt64, double)},
{REDUCE_REGISTER(kNumberTypeComplex64, kNumberTypeInt32, Complex<float>)},
{REDUCE_REGISTER(kNumberTypeComplex64, kNumberTypeInt64, Complex<float>)},
{REDUCE_REGISTER(kNumberTypeComplex128, kNumberTypeInt32, Complex<double>)},
{REDUCE_REGISTER(kNumberTypeComplex128, kNumberTypeInt64, Complex<double>)},
{STATIC_REGISTER(kNumberTypeFloat64, double)},
{STATIC_REGISTER(kNumberTypeFloat32, float)},
{STATIC_REGISTER(kNumberTypeFloat16, half)},
{STATIC_REGISTER(kNumberTypeInt8, int8_t)},
{STATIC_REGISTER(kNumberTypeComplex64, Complex<float>)},
{STATIC_REGISTER(kNumberTypeComplex128, Complex<double>)},
{DYN_REGISTER(kNumberTypeInt8, kNumberTypeInt32, int8_t)},
{DYN_REGISTER(kNumberTypeInt8, kNumberTypeInt64, int8_t)},
{DYN_REGISTER(kNumberTypeFloat16, kNumberTypeInt32, half)},
{DYN_REGISTER(kNumberTypeFloat16, kNumberTypeInt64, half)},
{DYN_REGISTER(kNumberTypeFloat32, kNumberTypeInt32, float)},
{DYN_REGISTER(kNumberTypeFloat32, kNumberTypeInt64, float)},
{DYN_REGISTER(kNumberTypeFloat64, kNumberTypeInt32, double)},
{DYN_REGISTER(kNumberTypeFloat64, kNumberTypeInt64, double)},
{DYN_REGISTER(kNumberTypeComplex64, kNumberTypeInt32, Complex<float>)},
{DYN_REGISTER(kNumberTypeComplex64, kNumberTypeInt64, Complex<float>)},
{DYN_REGISTER(kNumberTypeComplex128, kNumberTypeInt32, Complex<double>)},
{DYN_REGISTER(kNumberTypeComplex128, kNumberTypeInt64, Complex<double>)},
};
std::vector<std::pair<KernelAttr, ArrayReduceGpuKernelMod::ReduceFunc>> ArrayReduceGpuKernelMod::sum_list_ = {
{REDUCE_REGISTER(kNumberTypeBool, kNumberTypeInt32, bool)},
{REDUCE_REGISTER(kNumberTypeBool, kNumberTypeInt64, bool)},
{REDUCE_REGISTER(kNumberTypeFloat16, kNumberTypeInt32, half)},
{REDUCE_REGISTER(kNumberTypeFloat16, kNumberTypeInt64, half)},
{REDUCE_REGISTER(kNumberTypeFloat32, kNumberTypeInt32, float)},
{REDUCE_REGISTER(kNumberTypeFloat32, kNumberTypeInt64, float)},
{REDUCE_REGISTER(kNumberTypeFloat64, kNumberTypeInt32, double)},
{REDUCE_REGISTER(kNumberTypeFloat64, kNumberTypeInt64, double)},
{REDUCE_REGISTER(kNumberTypeComplex64, kNumberTypeInt32, Complex<float>)},
{REDUCE_REGISTER(kNumberTypeComplex64, kNumberTypeInt64, Complex<float>)},
{REDUCE_REGISTER(kNumberTypeComplex128, kNumberTypeInt32, Complex<double>)},
{REDUCE_REGISTER(kNumberTypeComplex128, kNumberTypeInt64, Complex<double>)},
{STATIC_REGISTER(kNumberTypeFloat64, double)},
{STATIC_REGISTER(kNumberTypeFloat32, float)},
{STATIC_REGISTER(kNumberTypeFloat16, half)},
{STATIC_REGISTER(kNumberTypeBool, bool)},
{STATIC_REGISTER(kNumberTypeComplex64, Complex<float>)},
{STATIC_REGISTER(kNumberTypeComplex128, Complex<double>)},
{DYN_REGISTER(kNumberTypeBool, kNumberTypeInt32, bool)},
{DYN_REGISTER(kNumberTypeBool, kNumberTypeInt64, bool)},
{DYN_REGISTER(kNumberTypeFloat16, kNumberTypeInt32, half)},
{DYN_REGISTER(kNumberTypeFloat16, kNumberTypeInt64, half)},
{DYN_REGISTER(kNumberTypeFloat32, kNumberTypeInt32, float)},
{DYN_REGISTER(kNumberTypeFloat32, kNumberTypeInt64, float)},
{DYN_REGISTER(kNumberTypeFloat64, kNumberTypeInt32, double)},
{DYN_REGISTER(kNumberTypeFloat64, kNumberTypeInt64, double)},
{DYN_REGISTER(kNumberTypeComplex64, kNumberTypeInt32, Complex<float>)},
{DYN_REGISTER(kNumberTypeComplex64, kNumberTypeInt64, Complex<float>)},
{DYN_REGISTER(kNumberTypeComplex128, kNumberTypeInt32, Complex<double>)},
{DYN_REGISTER(kNumberTypeComplex128, kNumberTypeInt64, Complex<double>)},
};
std::vector<std::pair<KernelAttr, ArrayReduceGpuKernelMod::ReduceFunc>> ArrayReduceGpuKernelMod::max_min_mean_list_ = {
{REDUCE_REGISTER(kNumberTypeFloat16, kNumberTypeInt32, half)},
{REDUCE_REGISTER(kNumberTypeFloat16, kNumberTypeInt64, half)},
{REDUCE_REGISTER(kNumberTypeFloat32, kNumberTypeInt32, float)},
{REDUCE_REGISTER(kNumberTypeFloat32, kNumberTypeInt64, float)},
{REDUCE_REGISTER(kNumberTypeFloat64, kNumberTypeInt32, double)},
{REDUCE_REGISTER(kNumberTypeFloat64, kNumberTypeInt64, double)},
{REDUCE_REGISTER(kNumberTypeComplex64, kNumberTypeInt32, Complex<float>)},
{REDUCE_REGISTER(kNumberTypeComplex64, kNumberTypeInt64, Complex<float>)},
{REDUCE_REGISTER(kNumberTypeComplex128, kNumberTypeInt32, Complex<double>)},
{REDUCE_REGISTER(kNumberTypeComplex128, kNumberTypeInt64, Complex<double>)},
{STATIC_REGISTER(kNumberTypeFloat64, double)},
{STATIC_REGISTER(kNumberTypeFloat32, float)},
{STATIC_REGISTER(kNumberTypeFloat16, half)},
{STATIC_REGISTER(kNumberTypeComplex64, Complex<float>)},
{STATIC_REGISTER(kNumberTypeComplex128, Complex<double>)},
{DYN_REGISTER(kNumberTypeFloat16, kNumberTypeInt32, half)},
{DYN_REGISTER(kNumberTypeFloat16, kNumberTypeInt64, half)},
{DYN_REGISTER(kNumberTypeFloat32, kNumberTypeInt32, float)},
{DYN_REGISTER(kNumberTypeFloat32, kNumberTypeInt64, float)},
{DYN_REGISTER(kNumberTypeFloat64, kNumberTypeInt32, double)},
{DYN_REGISTER(kNumberTypeFloat64, kNumberTypeInt64, double)},
{DYN_REGISTER(kNumberTypeComplex64, kNumberTypeInt32, Complex<float>)},
{DYN_REGISTER(kNumberTypeComplex64, kNumberTypeInt64, Complex<float>)},
{DYN_REGISTER(kNumberTypeComplex128, kNumberTypeInt32, Complex<double>)},
{DYN_REGISTER(kNumberTypeComplex128, kNumberTypeInt64, Complex<double>)},
};
std::map<std::string, std::vector<std::pair<KernelAttr, ArrayReduceGpuKernelMod::ReduceFunc>>>
ArrayReduceGpuKernelMod::kernel_attr_list_ = {
@ -150,10 +171,6 @@ bool ArrayReduceGpuKernelMod::Init(const BaseOperatorPtr &base_operator, const s
data_type_ = GetCudnnDataType(type_name);
}
auto kernel_ptr = std::dynamic_pointer_cast<ops::Reduce>(base_operator);
keep_dims_ = kernel_ptr->get_keep_dims();
skip_mode_ = kernel_ptr->get_skip_mode();
InitResource();
return true;
}
@ -185,17 +202,27 @@ int ArrayReduceGpuKernelMod::Resize(const BaseOperatorPtr &base_operator, const
return ret;
}
auto kernel_ptr = std::dynamic_pointer_cast<ops::Reduce>(base_operator);
auto inputA_shape = inputs[kIndex0]->GetDeviceShapeAdaptively();
std::vector<int64_t> attr_axis;
if (!TryGetIntValue(inputs, kIndex1, kernel_name_, &attr_axis)) {
MS_LOG(EXCEPTION) << "For " << kernel_name_ << " can't get axis input! ";
}
if (AnfAlgo::IsDynamicShapeSkipExecute(skip_mode_, inputs[kIndex1]->GetShapeVector())) {
need_skip_execute_ = true;
// As size of input_size_list_ is equal to size of inputs, input_size_list_[0] is safe.
input_size_ = input_size_list_[0];
return KRET_OK;
if (inputs.size() == kDynamicAxisInputNum) {
if (AnfAlgo::IsDynamicShapeSkipExecute(kernel_name_, inputs[kIndex1]->GetShapeVector())) {
need_skip_execute_ = true;
// As size of input_size_list_ is equal to size of inputs, input_size_list_[0] is safe.
input_size_ = input_size_list_[0];
return KRET_OK;
}
if (!TryGetIntValue(inputs, kIndex1, kernel_name_, &attr_axis)) {
InitCudnnResource();
return KRET_OK;
}
} else {
if (kernel_ptr->HasAttr(kAttrAxis)) {
attr_axis = kernel_ptr->get_axis();
}
}
keep_dims_ = kernel_ptr->get_keep_dims();
int input_dim_length = SizeToInt(inputA_shape.size());
axis_.clear();

View File

@ -68,7 +68,6 @@ class ArrayReduceGpuKernelMod : public NativeGpuKernelMod {
outputC_descriptor_ = nullptr;
need_skip_execute_ = false;
keep_dims_ = false;
skip_mode_ = false;
all_match_ = false;
is_null_input_ = false;
complex_op_type = 0;
@ -121,7 +120,6 @@ class ArrayReduceGpuKernelMod : public NativeGpuKernelMod {
std::vector<int> axis_;
bool keep_dims_;
bool skip_mode_;
bool need_skip_execute_;
bool all_match_;
bool is_null_input_;

View File

@ -24,6 +24,13 @@ namespace mindspore::opt {
RER_GPU_DYNAMIC_CONST_TO_ATTR(kCastOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kFillOpName, 0);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReduceAllOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReduceAnyOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReduceMaxOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReduceMeanOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReduceMinOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReduceSumOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReduceProdOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kReshapeOpName, 1);
RER_GPU_DYNAMIC_CONST_TO_ATTR(kTransposeOpName, 1);
@ -55,6 +62,13 @@ RER_GPU_STATIC_CONST_TO_ATTR(kParallelResizeBilinearGradOpName, 2);
RER_GPU_STATIC_CONST_TO_ATTR(kPullWeightOpName, 1, 2);
RER_GPU_STATIC_CONST_TO_ATTR(kPushOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kPushWeightOpName, 1, 2);
RER_GPU_STATIC_CONST_TO_ATTR(kReduceAllOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kReduceAnyOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kReduceMaxOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kReduceMeanOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kReduceMinOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kReduceProdOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kReduceSumOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kReshapeOpName, 1);
RER_GPU_STATIC_CONST_TO_ATTR(kROIAlignGradName, 2);
RER_GPU_STATIC_CONST_TO_ATTR(kSimpleMeanGradOpName, 1);

View File

@ -116,7 +116,6 @@ constexpr auto kIoFormat = "io_format";
constexpr auto kIsScale = "is_scale";
constexpr auto kIsTraining = "is_training";
constexpr auto kKeepDims = "keep_dims";
constexpr auto kSkipMode = "skip_mode";
constexpr auto kKeepProb = "keep_prob";
constexpr auto kPadDimSize = "pad_dim_size";
constexpr auto kUnbiased = "unbiased";

View File

@ -174,17 +174,11 @@ bool CheckAndGetAxisValue(const std::vector<abstract::AbstractBasePtr> &input_ar
input_args[kInputIndex1]->isa<abstract::AbstractTuple>() ||
input_args[kInputIndex1]->isa<abstract::AbstractList>()) {
*axis_value = CheckAndConvertUtils::CheckIntOrTupleInt("axis", input_value, op_name);
if (axis_value->empty()) {
*axis_shape_v = 0;
}
} else if (input_args[kInputIndex1]->isa<abstract::AbstractTensor>()) {
(void)CheckAndConvertUtils::CheckTensorTypeValid("axis", input_args[kInputIndex1]->BuildType(), {kInt32, kInt64},
op_name);
if (input_value->isa<tensor::Tensor>()) {
*axis_value = CheckAndConvertUtils::CheckTensorIntValue("axis", input_value, op_name);
if (axis_value->empty()) {
*axis_shape_v = 0;
}
} else {
is_dynamic = true;
auto axis_shape = CheckAndConvertUtils::GetTensorInputShape(op_name, input_args, 1);
@ -205,14 +199,6 @@ bool CheckAndGetAxisValue(const std::vector<abstract::AbstractBasePtr> &input_ar
return is_dynamic;
}
bool IsDynamicShapeSkipExecute(const bool skip_mode, const ShapeVector &axes_shape) {
// Skip run ReduceSum when axis is a Empty Tensor
if (std::any_of(axes_shape.begin(), axes_shape.end(), [](int64_t shape) { return shape == 0; }) && skip_mode) {
return true;
}
return false;
}
abstract::ShapePtr ReduceBaseInferShape(const PrimitivePtr &primitive,
const std::vector<abstract::AbstractBasePtr> &input_args,
const std::string &prim_name) {
@ -220,12 +206,6 @@ abstract::ShapePtr ReduceBaseInferShape(const PrimitivePtr &primitive,
auto shape_ptr = CheckAndConvertUtils::GetTensorInputShape(prim_name, input_args, 0);
MS_EXCEPTION_IF_NULL(shape_ptr);
auto x_shape = shape_ptr->shape();
bool skip_mode = false;
if (primitive->HasAttr(kSkipMode)) {
auto skip_mode_value_ptr = primitive->GetAttr(kSkipMode);
MS_EXCEPTION_IF_NULL(skip_mode_value_ptr);
skip_mode = GetValue<bool>(skip_mode_value_ptr);
}
auto keep_dimis_value_ptr = primitive->GetAttr(kKeepDims);
MS_EXCEPTION_IF_NULL(keep_dimis_value_ptr);
if (!keep_dimis_value_ptr->isa<BoolImm>()) {
@ -233,11 +213,8 @@ abstract::ShapePtr ReduceBaseInferShape(const PrimitivePtr &primitive,
}
bool keep_dims = GetValue<bool>(keep_dimis_value_ptr);
std::vector<int64_t> axis_value;
int64_t axis_shape = 1;
int64_t axis_shape = 0;
bool axis_is_dynamic = CheckAndGetAxisValue(input_args, &axis_value, &axis_shape, primitive);
if (IsDynamicShapeSkipExecute(skip_mode, {axis_shape})) {
return std::make_shared<abstract::Shape>(x_shape);
}
ShapeVector out_shape = {};
constexpr int dynamic_rank_value = -2;
if (IsDynamicRank(x_shape)) {

View File

@ -28,13 +28,23 @@ void Reduce::set_keep_dims(const bool keep_dims) { (void)this->AddAttr(kKeepDims
bool Reduce::get_keep_dims() const { return GetValue<bool>(GetAttr(kKeepDims)); }
void Reduce::set_skip_mode(const bool skip_mode) { (void)this->AddAttr(kSkipMode, api::MakeValue(skip_mode)); }
void Reduce::Init(const bool keep_dims) { this->set_keep_dims(keep_dims); }
bool Reduce::get_skip_mode() const { return GetValue<bool>(GetAttr(kSkipMode)); }
void Reduce::set_axis(const std::vector<int64_t> &axis) { (void)this->AddAttr(kAxis, api::MakeValue(axis)); }
void Reduce::Init(const bool keep_dims, const bool skip_mode) {
this->set_keep_dims(keep_dims);
this->set_skip_mode(skip_mode);
std::vector<int64_t> Reduce::get_axis() {
PrimitivePtr prim = this->GetPrim();
MS_EXCEPTION_IF_NULL(prim);
std::vector<int64_t> axis = {};
if (prim->HasAttr(kAttrAxis)) {
auto value_ptr = prim->GetAttr(kAttrAxis);
if (value_ptr->isa<tensor::Tensor>()) {
axis = CheckAndConvertUtils::CheckTensorIntValue(kAttrAxis, value_ptr, prim->name());
} else {
axis = CheckAndConvertUtils::CheckIntOrTupleInt(kAttrAxis, value_ptr, prim->name());
}
}
return axis;
}
MIND_API_OPERATOR_IMPL(Reduce, BaseOperator);

View File

@ -39,8 +39,7 @@ class MIND_API Reduce : public BaseOperator {
/// \brief Method to init the op's attributes.
///
/// \param[in] keep_dims Define whether keep the dims reduced, default false.
/// \param[in] skip_mode Define whether skip reduce, default false.
void Init(const bool keep_dims = false, const bool skip_mode = false);
void Init(const bool keep_dims = false);
/// \brief Method to set keep_dims attribute.
///
@ -52,15 +51,9 @@ class MIND_API Reduce : public BaseOperator {
/// \return keep_dims attribute.
bool get_keep_dims() const;
/// \brief Method to set skip_mode attribute.
///
/// \param[in] skip_mode Define whether skip reduce, default false.
void set_skip_mode(const bool skip_mode);
void set_axis(const std::vector<int64_t> &axis);
/// \brief Method to get skip_mode attribute.
///
/// \return skip_mode attribute.
bool get_skip_mode() const;
std::vector<int64_t> get_axis();
};
abstract::AbstractBasePtr ReduceInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<abstract::AbstractBasePtr> &input_args);

View File

@ -149,5 +149,5 @@ def dyn_ones(shape, value_type):
def sum_grad_reduce_axis(x, rx, keep_dims=False):
"""sum the grad_reduce_axis. when rx is an empty tensor and skip_mode is True, we need skip this reduce"""
return P.ReduceSum(keep_dims=keep_dims, skip_mode=True)(x, rx)
"""sum the grad_reduce_axis"""
return P.ReduceSum(keep_dims=keep_dims)(x, rx)

View File

@ -513,6 +513,7 @@ class _Reduce(PrimitiveWithCheck):
value = None
if input_x is not None and axis is not None:
prim_map = {
'ReduceSum': np.sum,
'ReduceMax': np.max,
'ReduceMin': np.min,
'ReduceProd': np.prod,
@ -694,7 +695,7 @@ class CumulativeLogsumexp(Primitive):
validator.check_bool(reverse, "reverse", self.name)
class ReduceSum(PrimitiveWithCheck):
class ReduceSum(_Reduce):
"""
Reduces a dimension of a tensor by summing all elements in the dimension, by default. And also can reduce a
dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by
@ -703,24 +704,18 @@ class ReduceSum(PrimitiveWithCheck):
Args:
keep_dims (bool): If true, keep these reduced dimensions and the length is 1.
If false, don't keep these dimensions. Default: False.
skip_mode (bool): If true and axis is empty tuple or empty list, the ReduceSum operation isn't performed,
skip it.
If true and axis is other values, the ReduceSum calculation is performed normally.
If false, do reduce. Default: False.
Inputs:
- **x** (Tensor[Number]) - The input tensor. The dtype of the tensor to be reduced is number.
:math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8.
- **axis** (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions
when skip_mode is false. Only constant value is allowed. Must be in the range [-rank(`x`), rank(`x`)).
- **axis** (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions.
Only constant value is allowed. Must be in the range [-rank(`x`), rank(`x`)).
Outputs:
Tensor, has the same dtype as the `x`.
- If axis is (), keep_dims is False, and skip_mode is False,
- If axis is (), and keep_dims is False,
the output is a 0-D tensor representing the sum of all elements in the input tensor.
- If axis is (), and skip_mode is True,
the ReduceSum operation is not performed, output tensor is equal to the input tensor.
- If axis is int, set as 2, and keep_dims is False,
the shape of output is :math:`(x_1, x_3, ..., x_R)`.
- If axis is tuple(int) or list(int), set as (2, 3), and keep_dims is False,
@ -728,7 +723,6 @@ class ReduceSum(PrimitiveWithCheck):
Raises:
TypeError: If `keep_dims` is not a bool.
TypeError: If `skip_mode` is not a bool.
TypeError: If `x` is not a Tensor.
ValueError: If `axis` is None.
@ -776,44 +770,12 @@ class ReduceSum(PrimitiveWithCheck):
[54.]]]
"""
__mindspore_signature__ = (
sig.make_sig('input_x'),
sig.make_sig('axis', default=())
)
@prim_attr_register
def __init__(self, keep_dims=False, skip_mode=False):
"""Initialize Reduce"""
validator.check_value_type('keep_dims', keep_dims, [bool], self.name)
validator.check_value_type('skip_mode', skip_mode, [bool], self.name)
self.init_prim_io_names(inputs=['input_x', 'axis'], outputs=['y'])
self.keep_dims = keep_dims
self.skip_mode = skip_mode
def __init__(self, keep_dims=False):
"""Initialize ReduceSum"""
super(ReduceSum, self).__init__(keep_dims)
self.__setattr_flag__ = True
def __call__(self, x, axis=()):
args = [x, axis]
output = _run_op(self, self.name, args)
return output
def infer_value(self, input_x, axis):
""" return reduce op value"""
value = None
if input_x is not None and axis is not None:
value = input_x.asnumpy()
if isinstance(axis, int):
pass
elif axis:
axis = tuple(set(axis))
elif axis in ((), []) and self.skip_mode:
return input_x
else:
axis = tuple(range(len(value.shape)))
value = np.sum(value, axis, keepdims=self.keep_dims)
value = np.array(value)
value = Tensor(value)
return value
class ReduceAll(_Reduce):
"""