[feat] [assistant] [I48O9Q] cosh with dynamic infer shape

This commit is contained in:
zheng_pengfei 2021-12-16 19:46:57 +08:00
parent 901d81d16d
commit afef03ed2f
5 changed files with 140 additions and 9 deletions

View File

@ -0,0 +1,59 @@
/**
* 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/cosh.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 CoshInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
auto prim_name = primitive->name();
(void)CheckAndConvertUtils::CheckInteger("input numbers", SizeToLong(input_args.size()), kGreaterEqual, 1, prim_name);
(void)CheckAndConvertUtils::CheckArgs<abstract::AbstractTensor>(prim_name, input_args, 0);
auto x = input_args[0]->BuildShape();
MS_EXCEPTION_IF_NULL(x);
auto shape_element = x->cast<abstract::ShapePtr>();
MS_EXCEPTION_IF_NULL(shape_element);
return shape_element;
}
TypePtr CoshInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
auto x_dtype = input_args[0]->BuildType();
(void)CheckAndConvertUtils::CheckTensorTypeValid("x", x_dtype, common_valid_types_with_complex, prim->name());
return x_dtype;
}
} // namespace
AbstractBasePtr CoshInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
const int64_t kInputNum = 1;
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, kInputNum, primitive->name());
auto infer_type = CoshInferType(primitive, input_args);
auto infer_shape = CoshInferShape(primitive, input_args);
return abstract::MakeAbstract(infer_shape, infer_type);
}
REGISTER_PRIMITIVE_EVAL_IMPL(Cosh, prim::kPrimCosh, CoshInfer, nullptr, true);
} // namespace ops
} // namespace mindspore

41
mindspore/core/ops/cosh.h Normal file
View File

@ -0,0 +1,41 @@
/**
* 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_COSH_H_
#define MINDSPORE_CORE_OPS_COSH_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 kNameCosh = "Cosh";
class Cosh : public PrimitiveC {
public:
Cosh() : PrimitiveC(kNameCosh) { InitIOName({"x"}, {"output"}); }
~Cosh() = default;
MS_DECLARE_PARENT(Cosh, PrimitiveC);
void Init() {}
};
AbstractBasePtr CoshInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args);
using kPrimCoshPtr = std::shared_ptr<Cosh>;
} // namespace ops
} // namespace mindspore
#endif // MINDSPORE_CORE_OPS_COSH_H_

View File

@ -393,6 +393,7 @@ from .atan_grad_ds import _atan_grad_ds_tbe
from .atanh import _atanh_tbe
from .atanh_ds import _atanh_ds_tbe
from .cosh import _cosh_tbe
from .cosh_ds import _cosh_ds_tbe
from .sinh import _sinh_tbe
from .sinh_ds import _sinh_ds_tbe
from .inv import _inv_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.
# ============================================================================
"""Cosh op"""
from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
cosh_op_info = TBERegOp("Cosh") \
.fusion_type("ELEMWISE") \
.async_flag(False) \
.binfile_name("cosh.so") \
.compute_cost(10) \
.kernel_name("cosh") \
.partial_flag(True) \
.dynamic_shape(True) \
.op_pattern("formatAgnostic") \
.input(0, "x", False, "required", "all") \
.output(0, "y", True, "required", "all") \
.dtype_format(DataType.F16_None, DataType.F16_None) \
.dtype_format(DataType.F32_None, DataType.F32_None) \
.get_op_info()
@op_info_register(cosh_op_info)
def _cosh_ds_tbe():
"""Cosh TBE register"""
return

View File

@ -3231,8 +3231,7 @@ class Acosh(Primitive):
"""Initialize Acosh"""
self.init_prim_io_names(inputs=['x'], outputs='output')
class Cosh(PrimitiveWithInfer):
class Cosh(Primitive):
r"""
Computes hyperbolic cosine of input element-wise.
@ -3265,13 +3264,6 @@ class Cosh(PrimitiveWithInfer):
def __init__(self):
"""Initialize Cosh"""
def infer_shape(self, x_shape):
return x_shape
def infer_dtype(self, x_dtype):
validator.check_tensor_dtype_valid('x', x_dtype, mstype.number_type, self.name)
return x_dtype
class Asinh(PrimitiveWithInfer):
r"""