parent
b4ce0aa933
commit
6a2c1df5e3
@ -0,0 +1,56 @@
|
||||
# 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.
|
||||
# ============================================================================
|
||||
|
||||
"""EditDistance op"""
|
||||
from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType
|
||||
|
||||
edit_distance_op_info = AiCPURegOp("EditDistance") \
|
||||
.fusion_type("OPAQUE") \
|
||||
.input(0, "hypothesis_indices", "required") \
|
||||
.input(1, "hypothesis_values", "required") \
|
||||
.input(2, "hypothesis_shape", "required") \
|
||||
.input(3, "truth_indices", "required") \
|
||||
.input(4, "truth_values", "required") \
|
||||
.input(5, "truth_shape", "required") \
|
||||
.output(0, "y", "required") \
|
||||
.attr("normalize", "bool") \
|
||||
.dtype_format(DataType.I64_Default, DataType.I8_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.I8_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.I16_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.I16_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.I32_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.I32_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.U8_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.U8_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.U16_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.U16_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.U32_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.U32_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.U64_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.U64_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.F16_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.F16_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.F32_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.F32_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.dtype_format(DataType.I64_Default, DataType.F64_Default, DataType.I64_Default, \
|
||||
DataType.I64_Default, DataType.F64_Default, DataType.I64_Default, DataType.F32_Default,) \
|
||||
.get_op_info()
|
||||
|
||||
@op_info_register(edit_distance_op_info)
|
||||
def _edit_distance_aicpu():
|
||||
"""EditDistance AiCPU register"""
|
||||
return
|
@ -0,0 +1,34 @@
|
||||
# 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.
|
||||
# ============================================================================
|
||||
|
||||
"""TransData op"""
|
||||
from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType
|
||||
|
||||
trans_data_op_info = AiCPURegOp("TransData") \
|
||||
.fusion_type("OPAQUE") \
|
||||
.input(0, "src", "required") \
|
||||
.output(0, "dst", "required") \
|
||||
.attr("src_format", "str") \
|
||||
.attr("dst_format", "str") \
|
||||
.dtype_format(DataType.U16_NCHW, DataType.U16_5HD) \
|
||||
.dtype_format(DataType.U16_5HD, DataType.U16_NCHW) \
|
||||
.dtype_format(DataType.U16_Default, DataType.U16_5HD) \
|
||||
.dtype_format(DataType.U16_5HD, DataType.U16_Default) \
|
||||
.get_op_info()
|
||||
|
||||
@op_info_register(trans_data_op_info)
|
||||
def _trans_data_aicpu():
|
||||
"""TransData aicpu register"""
|
||||
return
|
@ -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.
|
||||
# ============================================================================
|
||||
|
||||
"""UniqueWithPad op"""
|
||||
from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType
|
||||
|
||||
unique_with_pad_op_info = AiCPURegOp("UniqueWithPad") \
|
||||
.fusion_type("OPAQUE") \
|
||||
.input(0, "x", "required") \
|
||||
.input(1, "pad_num", "required") \
|
||||
.output(0, "y", "required") \
|
||||
.output(1, "idx", "required") \
|
||||
.dtype_format(DataType.I32_Default, DataType.I32_Default, DataType.I32_Default, DataType.I32_Default) \
|
||||
.dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, DataType.I64_Default) \
|
||||
.get_op_info()
|
||||
|
||||
@op_info_register(unique_with_pad_op_info)
|
||||
def _unique_with_pad_aicpu():
|
||||
"""UniqueWithPad AiCPU register"""
|
||||
return
|
@ -0,0 +1,48 @@
|
||||
# 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
|
||||
|
||||
import mindspore.context as context
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE,
|
||||
device_target="Ascend")
|
||||
|
||||
|
||||
class EditDistance(nn.Cell):
|
||||
def __init__(self, hypothesis_shape, truth_shape, normalize=True):
|
||||
super(EditDistance, self).__init__()
|
||||
self.edit_distance = P.EditDistance(normalize)
|
||||
self.hypothesis_shape = hypothesis_shape
|
||||
self.truth_shape = truth_shape
|
||||
|
||||
def construct(self, hypothesis_indices, hypothesis_values, truth_indices, truth_values):
|
||||
return self.edit_distance(hypothesis_indices, hypothesis_values, self.hypothesis_shape,
|
||||
truth_indices, truth_values, self.truth_shape)
|
||||
|
||||
def test_edit_distance():
|
||||
h1, h2, h3 = np.array([[0, 0, 0], [1, 0, 1], [1, 1, 1]]), np.array([1, 2, 3]), np.array([2, 2, 2])
|
||||
t1, t2, t3 = np.array([[0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1]]), np.array([1, 2, 3, 1]), np.array([2, 2, 2])
|
||||
hypothesis_indices = Tensor(h1.astype(np.int64))
|
||||
hypothesis_values = Tensor(h2.astype(np.int64))
|
||||
hypothesis_shape = Tensor(h3.astype(np.int64))
|
||||
truth_indices = Tensor(t1.astype(np.int64))
|
||||
truth_values = Tensor(t2.astype(np.int64))
|
||||
truth_shape = Tensor(t3.astype(np.int64))
|
||||
edit_distance = EditDistance(hypothesis_shape, truth_shape)
|
||||
out = edit_distance(hypothesis_indices, hypothesis_values, truth_indices, truth_values)
|
||||
print(out)
|
@ -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 numpy as np
|
||||
|
||||
import mindspore.context as context
|
||||
import mindspore.nn as nn
|
||||
import mindspore.common.dtype as mstype
|
||||
from mindspore import Tensor
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE,
|
||||
device_target="Ascend")
|
||||
|
||||
|
||||
class Net(nn.Cell):
|
||||
def __init__(self, pad_num):
|
||||
super(Net, self).__init__()
|
||||
self.unique_with_pad = P.UniqueWithPad()
|
||||
self.pad_num = pad_num
|
||||
|
||||
def construct(self, x):
|
||||
return self.unique_with_pad(x, self.pad_num)
|
||||
|
||||
|
||||
def test_unique_with_pad():
|
||||
x = Tensor(np.array([1, 1, 5, 5, 4, 4, 3, 3, 2, 2]), mstype.int32)
|
||||
pad_num = 8
|
||||
unique_with_pad = Net(pad_num)
|
||||
out = unique_with_pad(x)
|
||||
expect_val = ([1, 5, 4, 3, 2, 8, 8, 8, 8, 8], [0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
|
||||
assert(out[0].asnumpy() == expect_val[0]).all()
|
||||
assert(out[1].asnumpy() == expect_val[1]).all()
|
Loading…
Reference in new issue