parent
9f9af3c59a
commit
366364ba38
@ -0,0 +1,62 @@
|
||||
# Copyright 2020 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.
|
||||
# ============================================================================
|
||||
from __future__ import absolute_import
|
||||
from te import tvm
|
||||
from topi import generic
|
||||
import te.lang.cce
|
||||
from topi.cce import util
|
||||
from te.platform.fusion_manager import fusion_manager
|
||||
from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
|
||||
|
||||
@fusion_manager.register("add3")
|
||||
def add3_compute(input1, input2, const_bias):
|
||||
sum2 = te.lang.cce.vadd(input1, input2)
|
||||
sum3 = te.lang.cce.vadds(sum2, tvm.const(const_bias, dtype = input1.dtype))
|
||||
return sum3
|
||||
|
||||
|
||||
cus_add3_op_info = TBERegOp("CusAdd3") \
|
||||
.fusion_type("OPAQUE") \
|
||||
.async_flag(False) \
|
||||
.binfile_name("add3.so") \
|
||||
.compute_cost(10) \
|
||||
.kernel_name("CusAdd3Impl") \
|
||||
.partial_flag(True) \
|
||||
.attr("const_bias", "required", "float", "all") \
|
||||
.input(0, "input1", False, "required", "all") \
|
||||
.input(1, "input2", False, "required", "all") \
|
||||
.output(0, "sum", False, "required", "all") \
|
||||
.dtype_format(DataType.F32_Default, DataType.F32_Default, DataType.F32_Default) \
|
||||
.dtype_format(DataType.F16_Default, DataType.F16_Default, DataType.F16_Default) \
|
||||
.get_op_info()
|
||||
|
||||
|
||||
@op_info_register(cus_add3_op_info)
|
||||
def CusAdd3Impl(input1, inptu2, sum, const_bias, kernel_name="CusAdd3Impl"):
|
||||
shape = input1.get("shape")
|
||||
shape = util.shape_refine(shape)
|
||||
dtype = input1.get("dtype").lower()
|
||||
input1 = tvm.placeholder(shape, name="input1", dtype=dtype.lower())
|
||||
input2 = tvm.placeholder(shape, name="input2", dtype=dtype.lower())
|
||||
|
||||
with tvm.target.cce():
|
||||
res = add3_compute(input1, input2, const_bias)
|
||||
sch = generic.auto_schedule(res)
|
||||
|
||||
config = {"print_ir": False,
|
||||
"name": kernel_name,
|
||||
"tensor_list": [input1, input2, res]}
|
||||
|
||||
te.lang.cce.cce_build_code(sch, config)
|
@ -0,0 +1,32 @@
|
||||
# Copyright 2020 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.
|
||||
# ============================================================================
|
||||
import numpy as np
|
||||
from mindspore.ops import prim_attr_register, PrimitiveWithInfer
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore import Tensor
|
||||
|
||||
# sum = input1 + input2 + const_bias
|
||||
class CusAdd3(PrimitiveWithInfer):
|
||||
"""Custom add3 definition"""
|
||||
@prim_attr_register
|
||||
def __init__(self, const_bias=0.0):
|
||||
self.init_prim_io_names(inputs=['input1', 'input2'], outputs=['sum3'])
|
||||
from add3_impl import CusAdd3Impl
|
||||
|
||||
def infer_shape(self, input1, input2):
|
||||
return input1
|
||||
|
||||
def infer_dtype(self, input1, input2):
|
||||
return input1
|
@ -0,0 +1,44 @@
|
||||
# Copyright 2020 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.
|
||||
# ============================================================================
|
||||
import pytest
|
||||
import numpy as np
|
||||
import mindspore.nn as nn
|
||||
import mindspore.context as context
|
||||
from mindspore import Tensor
|
||||
from mindspore.ops import composite as C
|
||||
from cus_add3 import CusAdd3
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
|
||||
class Net(nn.Cell):
|
||||
"""Net definition"""
|
||||
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.add3 = CusAdd3(1.0)
|
||||
|
||||
def construct(self, input1, input2):
|
||||
return self.add3(input1, input2)
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.platform_arm_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_net():
|
||||
input1 = np.array([1.0, 4.0, 9.0]).astype(np.float32)
|
||||
input2 = np.array([1.0, 2.0, 3.0]).astype(np.float32)
|
||||
add3_net = Net()
|
||||
output = add3_net(Tensor(input1), Tensor(input2))
|
||||
expect = np.array([3.0, 7.0, 13.0]).astype(np.float32)
|
||||
assert (output.asnumpy() == expect).all()
|
Loading…
Reference in new issue