!25394 [feat][assistant][I48OBZ]add dynamic shape for Invert operation

Merge pull request !25394 from zhixinaa/Invert
This commit is contained in:
i-robot 2021-11-12 09:30:22 +00:00 committed by Gitee
commit ca2076dede
6 changed files with 139 additions and 9 deletions

View File

@ -505,6 +505,7 @@ inline const PrimitivePtr kPrimMul = std::make_shared<Primitive>(kMul);
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");
inline const PrimitivePtr kPrimInvert = std::make_shared<Primitive>("Invert");
inline const PrimitivePtr kPrimDivNoNan = std::make_shared<Primitive>("DivNoNan");
inline const PrimitivePtr kPrimMinimum = std::make_shared<Primitive>("Minimum");
inline const PrimitivePtr kPrimMaximum = std::make_shared<Primitive>("Maximum");

View File

@ -0,0 +1,54 @@
/**
* 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/invert.h"
#include <map>
#include <set>
#include <string>
#include "ops/op_utils.h"
#include "utils/check_convert_utils.h"
namespace mindspore {
namespace ops {
namespace {
abstract::ShapePtr InvertInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
auto x_shape = input_args[0]->BuildShape();
MS_EXCEPTION_IF_NULL(x_shape);
auto output_shape = x_shape->cast<abstract::ShapePtr>();
return output_shape;
}
TypePtr InvertInferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
auto input_type = input_args[0]->BuildType();
std::set<TypePtr> check_list = {kInt16, kUInt16};
(void)CheckAndConvertUtils::CheckTensorTypeValid("x", input_type, check_list, primitive->name());
return input_type;
}
} // namespace
AbstractBasePtr InvertInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
const int64_t input_num = 1;
(void)CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, primitive->name());
auto infer_type = InvertInferType(primitive, input_args);
auto infer_shape = InvertInferShape(primitive, input_args);
return abstract::MakeAbstract(infer_shape, infer_type);
}
REGISTER_PRIMITIVE_EVAL_IMPL(Invert, prim::kPrimInvert, InvertInfer, nullptr, true);
} // namespace ops
} // namespace mindspore

View File

@ -0,0 +1,42 @@
/**
* 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_INVERT_H_
#define MINDSPORE_CORE_OPS_INVERT_H_
#include <vector>
#include <memory>
#include "ops/primitive_c.h"
#include "abstract/abstract_value.h"
#include "utils/check_convert_utils.h"
namespace mindspore {
namespace ops {
constexpr auto kNameInvert = "Invert";
class Invert : public PrimitiveC {
public:
Invert() : PrimitiveC(kNameInvert) { InitIOName({"x"}, {"y"}); }
~Invert() = default;
MS_DECLARE_PARENT(Invert, PrimitiveC);
};
AbstractBasePtr InvertInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args);
using kPrimInvertPtr = std::shared_ptr<Invert>;
} // namespace ops
} // namespace mindspore
#endif // MINDSPORE_CORE_OPS_INVERT_H_

View File

@ -348,6 +348,7 @@ from .sinh import _sinh_tbe
from .inv import _inv_tbe
from .inv_grad import _inv_grad_tbe
from .invert import _invert_tbe
from .invert_ds import _invert_ds_tbe
from .basic_lstm_cell import _basic_lstm_cell_tbe
from .basic_lstm_cell_c_state_grad_v2 import _basic_lstm_cell_c_state_grad_tbe_v2
from .basic_lstm_cell_c_state_grad import _basic_lstm_cell_c_state_grad_tbe

View File

@ -0,0 +1,38 @@
# 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.
# ============================================================================
"""Invert op"""
from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
invert_ds_op_info = TBERegOp("Invert") \
.fusion_type("OPAQUE") \
.async_flag(False) \
.binfile_name("invert.so") \
.compute_cost(10) \
.kernel_name("invert") \
.partial_flag(True) \
.dynamic_shape(True) \
.input(0, "x", False, "required", "all") \
.output(0, "y", False, "required", "all") \
.op_pattern("formatAgnostic") \
.dtype_format(DataType.I16_None, DataType.I16_None) \
.dtype_format(DataType.U16_None, DataType.U16_None) \
.get_op_info()
@op_info_register(invert_ds_op_info)
def _invert_ds_tbe():
"""Invert TBE register"""
return

View File

@ -5064,7 +5064,7 @@ class Inv(PrimitiveWithInfer):
return x_dtype
class Invert(PrimitiveWithInfer):
class Invert(Primitive):
r"""
Flips all bits of input tensor element-wise.
@ -5094,14 +5094,8 @@ class Invert(PrimitiveWithInfer):
@prim_attr_register
def __init__(self):
pass
def infer_shape(self, x_shape):
return x_shape
def infer_dtype(self, x_dtype):
validator.check_tensor_dtype_valid('x_dtype', x_dtype, [mstype.int16, mstype.uint16], self.name)
return x_dtype
"""Initialize Invert"""
self.init_prim_io_names(inputs=['x'], outputs=['y'])
class Eps(PrimitiveWithInfer):