!26028 [feat] [assistant] [I48OAM] MulNoNan with dynamic infer shape

Merge pull request !26028 from 郑鹏飞/mulnonan_1
This commit is contained in:
i-robot 2021-12-18 06:58:29 +00:00 committed by Gitee
commit 76bec49d75
6 changed files with 170 additions and 10 deletions

View File

@ -54,6 +54,7 @@ constexpr auto kNotEqual = "NotEqual";
constexpr auto kNeg = "Neg";
constexpr auto kSub = "Sub";
constexpr auto kMul = "Mul";
constexpr auto kMulNoNan = "MulNoNan";
constexpr auto kRealDiv = "RealDiv";
constexpr auto kReciprocal = "Reciprocal";
constexpr auto kLog = "Log";
@ -533,6 +534,7 @@ inline const PrimitivePtr kPrimSin = std::make_shared<Primitive>("Sin");
inline const PrimitivePtr kPrimCos = std::make_shared<Primitive>(kCos);
inline const PrimitivePtr kPrimSub = std::make_shared<Primitive>(kSub);
inline const PrimitivePtr kPrimMul = std::make_shared<Primitive>(kMul);
inline const PrimitivePtr kPrimMulNoNan = std::make_shared<Primitive>(kMulNoNan);
inline const PrimitivePtr kPrimDiv = std::make_shared<Primitive>("Div");
inline const PrimitivePtr kPrimMod = std::make_shared<Primitive>("Mod");
inline const PrimitivePtr kPrimFloor = std::make_shared<Primitive>("Floor");

View File

@ -0,0 +1,83 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ops/mulnonan.h"
#include <string>
#include <algorithm>
#include <memory>
#include <set>
#include <vector>
#include "ops/op_utils.h"
#include "utils/check_convert_utils.h"
#include "abstract/primitive_infer_map.h"
namespace mindspore {
namespace ops {
namespace {
abstract::ShapePtr MulNoNanInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
auto prim_name = primitive->name();
return BroadCastInferShape(prim_name, input_args);
}
TypePtr MulNoNanInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
for (const auto &item : input_args) {
MS_EXCEPTION_IF_NULL(item);
}
auto op_name = prim->name();
const int64_t kInputNum = 2;
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kGreaterEqual, kInputNum,
op_name);
std::map<std::string, TypePtr> types;
(void)types.emplace("x", input_args[0]->BuildType());
(void)types.emplace("y", input_args[1]->BuildType());
auto type_x = input_args[0]->BuildType();
auto type_y = input_args[1]->BuildType();
MS_EXCEPTION_IF_NULL(type_x);
MS_EXCEPTION_IF_NULL(type_y);
if (type_x->isa<Complex>() || type_y->isa<Complex>()) {
if (type_x->type_id() == kNumberTypeComplex64 && type_y->type_id() == kNumberTypeComplex64) {
return type_x;
} else if (type_x->type_id() == kNumberTypeComplex64 && type_y->type_id() == kNumberTypeFloat32) {
return type_x;
} else if (type_x->type_id() == kNumberTypeComplex128 && type_y->type_id() == kNumberTypeComplex128) {
return type_x;
} else if (type_x->type_id() == kNumberTypeComplex128 && type_y->type_id() == kNumberTypeFloat64) {
return type_x;
} else if (type_x->type_id() == kNumberTypeFloat32 && type_y->type_id() == kNumberTypeComplex64) {
return type_y;
} else if (type_x->type_id() == kNumberTypeFloat64 && type_y->type_id() == kNumberTypeComplex128) {
return type_y;
} else {
MS_EXCEPTION(TypeError)
<< "Complex math binary op expecting Tensor [complex64, complex64],[complex64, float32], [float32, "
"complex64],[complex128, complex128],[complex128, float64], [float64, complex128], but got["
<< type_x->ToString() << ", " << type_y->ToString() << "].";
}
}
return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types_with_complex, prim->name());
}
} // namespace
AbstractBasePtr MulNoNanInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args) {
auto infer_type = MulNoNanInferType(primitive, input_args);
auto infer_shape = MulNoNanInferShape(primitive, input_args);
return abstract::MakeAbstract(infer_shape, infer_type);
}
REGISTER_PRIMITIVE_EVAL_IMPL(MulNoNan, prim::kPrimMulNoNan, MulNoNanInfer, nullptr, true);
} // namespace ops
} // namespace mindspore

View File

@ -0,0 +1,44 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CORE_OPS_MULNONAN_H_
#define MINDSPORE_CORE_OPS_MULNONAN_H_
#include <map>
#include <vector>
#include <string>
#include <memory>
#include "ops/primitive_c.h"
#include "abstract/abstract_value.h"
#include "utils/check_convert_utils.h"
namespace mindspore {
namespace ops {
constexpr auto kNameMulNoNan = prim::kMulNoNan;
class MulNoNan : public PrimitiveC {
public:
MulNoNan() : PrimitiveC(kNameMulNoNan) { InitIOName({"x", "y"}, {"output"}); }
explicit MulNoNan(const std::string k_name) : PrimitiveC(k_name) { InitIOName({"x", "y"}, {"output"}); }
~MulNoNan() = default;
MS_DECLARE_PARENT(MulNoNan, PrimitiveC);
void Init() {}
};
AbstractBasePtr MulNoNanInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args);
using kPrimMulNoNanPtr = std::shared_ptr<MulNoNan>;
} // namespace ops
} // namespace mindspore
#endif // MINDSPORE_CORE_OPS_MULNONAN_H_

View File

@ -476,6 +476,7 @@ from .nll_loss_grad import _nll_loss_grad_tbe
from .masked_fill import _masked_fill_tbe
from .mish import _mish_tbe
from .mul_no_nan import _mul_no_nan_tbe
from .mul_no_nan_ds import _mul_no_nan_ds_tbe
from .selu import _selu_tbe
from .centralization import _centralization_tbe
from .exp_ds import _exp_ds_tbe

View File

@ -0,0 +1,40 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""MulNoNan op"""
from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
mul_no_nan_op_info = TBERegOp("MulNoNan") \
.fusion_type("ELEMWISE") \
.async_flag(False) \
.binfile_name("mul_no_nan.so") \
.compute_cost(10) \
.kernel_name("mul_no_nan") \
.partial_flag(True) \
.dynamic_shape(True) \
.input(0, "x1", False, "required", "all") \
.input(1, "x2", False, "required", "all") \
.output(0, "y", False, "required", "all") \
.op_pattern("broadcast") \
.dtype_format(DataType.F16_None, DataType.F16_None, DataType.F16_None) \
.dtype_format(DataType.F32_None, DataType.F32_None, DataType.F32_None) \
.dtype_format(DataType.I32_None, DataType.I32_None, DataType.I32_None) \
.get_op_info()
@op_info_register(mul_no_nan_op_info)
def _mul_no_nan_ds_tbe():
"""MulNoNan TBE register"""
return

View File

@ -2788,16 +2788,6 @@ class MulNoNan(_MathBinaryOp):
"""Initialize _BinaryOp"""
self.init_prim_io_names(inputs=['x', 'y'], outputs=['output'])
def infer_value(self, x, y):
if x is not None and y is not None:
x = x.asnumpy()
y = y.asnumpy()
with np.errstate(divide='ignore', invalid='ignore'):
out = np.multiply(x, y)
out[y == 0] = 0
return out
return None
class FloorDiv(Primitive):
"""