!37635 回退 'Pull Request !37407 : [MS][ASCEND] add smooth_l1_loss ascend op'

Merge pull request !37635 from yanghaoran/revert-merge-37407-master
This commit is contained in:
yanghaoran 2022-07-08 03:12:46 +00:00 committed by Gitee
commit f9de9bff39
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 77 additions and 189 deletions

View File

@ -26,7 +26,7 @@ mindspore.nn.SmoothL1Loss
其中,:math:`{\beta}` 代表阈值 `beta`
.. note::
- 在Ascend上,目前不支持 `logits` 的数据类型是float64
- 在Ascend上, 目前不支持将 `reduction` 设定成'sum'或'mean'
- SmoothL1Loss可以看成 :class:`mindspore.nn.L1Loss` 的修改版本,也可以看成 :class:`mindspore.nn.L1Loss`:class:`mindspore.ops.L2Loss` 的组合。
- :class:`mindspore.nn.L1Loss` 计算两个输入Tensor之间的绝对误差:class:`mindspore.ops.L2Loss` 计算两个输入Tensor之间的平方误差。
- :class:`mindspore.ops.L2Loss` 通常更快收敛,但对离群值的鲁棒性较差。该损失函数具有较好的鲁棒性。
@ -38,7 +38,7 @@ mindspore.nn.SmoothL1Loss
**输入:**
- **logits** (Tensor) - 预测值任意维度Tensor。数据类型为float16或float32 CPU和GPU后端还支持float64
- **logits** (Tensor) - 预测值任意维度Tensor。数据类型必须为float16或float32。
- **labels** (Tensor) - 目标值数据类型和shape与 `logits` 相同的Tensor。
**输出:**
@ -50,8 +50,7 @@ mindspore.nn.SmoothL1Loss
- **TypeError** - `beta` 不是float。
- **ValueError** - `reduction` 不是'none''mean'和'sum'中的任意一个。
- **TypeError** - `logits``labels` 不是Tensor。
- **TypeError** - `logits``labels` 的数据类型不是float16float32和float64中的任一者
- **TypeError** - `logits``labels` 的数据类型不是float16也不是float32。
- **TypeError** - `logits` 的数据类型与 `labels` 不同。
- **ValueError** - `beta` 小于或等于0。
- **ValueError** - `logits` 的shape与 `labels` 不同。
- **ValueError** - Ascend后端不支持数据类型是float64的 `logits` 输入。

View File

@ -28,11 +28,11 @@ mindspore.ops.smooth_l1_loss
其中, :math:`\beta` 代表阈值 `beta`:math:`N` 为batch size。
.. note::
在Ascend上目前不支持 `logits` 的数据类型是float64
在Ascend上目前不支持`reduction` 设定成'sum'或'mean'
**参数:**
- **logits** (Tensor) - shape :math:`(N, *)` ,其中 :math:`*` 表示任意数量的附加维度。数据类型为float16或float32 CPU和GPU后端还支持float64
- **logits** (Tensor) - shape :math:`(N, *)` ,其中 :math:`*` 表示任意数量的附加维度。数据类型支持float16或float32
- **labels** (Tensor) - shape :math:`(N, *)` ,与 `logits` 的shape和数据类型相同。
- **beta** (float) - 控制损失函数在L1Loss和L2Loss间变换的阈值。默认值1.0。
- **reduction** (str) - 缩减输出的方法。默认值:'none'。 其他选项:'mean'和'sum'。
@ -44,8 +44,7 @@ mindspore.ops.smooth_l1_loss
**异常:**
- **TypeError** - `beta` 不是float类型。
- **ValueError** - `reduction` 不是'none''mean'和'sum'中的任一者
- **TypeError** - `logits``labels` 的数据类型不是float16float32和float64中的任一者
- **ValueError** - `reduction` 不是'none''mean'和'sum'中的任意一个
- **TypeError** - `logits``labels` 的数据类型非float16或float32
- **ValueError** - `beta` 小于或等于0。
- **ValueError** - `logits``labels` 的shape不同。
- **ValueError** - Ascend后端不支持数据类型是float64的 `logits` 输入。

View File

@ -18,8 +18,8 @@
#include <map>
#include <string>
#include <vector>
#include "ops/smooth_l1_loss.h"
#include "utils/ms_context.h"
#include "ops/op_utils.h"
#include "utils/check_convert_utils.h"
#include "mindapi/src/helper.h"
@ -72,16 +72,7 @@ abstract::ShapePtr SmoothL1LossInferShape(const PrimitivePtr &primitive,
TypePtr SmoothL1LossInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
// Infer type
std::set<TypePtr> valid_types{};
auto context = MsContext::GetInstance();
MS_EXCEPTION_IF_NULL(context);
bool is_ascend = (context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kAscendDevice);
if (is_ascend) {
valid_types = {kFloat16, kFloat32};
} else {
valid_types = {kFloat16, kFloat32, kFloat64};
}
const std::set<TypePtr> valid_types = {kFloat16, kFloat32, kFloat64};
std::map<std::string, TypePtr> args;
(void)args.emplace("scale", input_args[kInputIndex0]->BuildType());
(void)args.emplace("bias", input_args[kInputIndex1]->BuildType());

View File

@ -458,12 +458,12 @@ class SmoothL1Loss(LossBase):
.. math::
L =
\begin{cases}
\operatorname{mean}(L_{i}), & \text{if reduction} = \text{'mean';}\\
\operatorname{sum}(L_{i}), & \text{if reduction} = \text{'sum'.}
\operatorname{mean}(L_{i}), & \text{if reduction} = \text{`mean';}\\
\operatorname{sum}(L_{i}), & \text{if reduction} = \text{`sum'.}
\end{cases}
.. note::
For Ascend platform, the float64 data type of `logits` is not support now.
For Ascend platform, the 'reduction' is not support set to 'sum' or 'mean'.
SmoothL1Loss can be regarded as modified version of L1Loss or a combination of L1Loss and L2Loss.
L1Loss computes the element-wise absolute difference between two input tensors while L2Loss computes the
squared difference between two input tensors. L2Loss often leads to faster convergence but it is less
@ -476,8 +476,7 @@ class SmoothL1Loss(LossBase):
Default: "none".
Inputs:
- **logits** (Tensor) - Predictive value. Tensor of any dimension. Data type must be one of float16,
float32 and float64.
- **logits** (Tensor) - Predictive value. Tensor of any dimension. Data type must be float16 or float32.
- **labels** (Tensor) - Ground truth data, same shape and dtype as the `logits`.
Outputs:
@ -492,7 +491,6 @@ class SmoothL1Loss(LossBase):
TypeError: If dtype of `logits` is not the same as `labels`.
ValueError: If `beta` is less than or equal to 0.
ValueError: If shape of `logits` is not the same as `labels`.
ValueError: The float64 data type of `logits` is support on Ascend platform.
Supported Platforms:
``Ascend`` ``GPU`` ``CPU``
@ -509,11 +507,17 @@ class SmoothL1Loss(LossBase):
def __init__(self, beta=1.0, reduction='none'):
"""Initialize SmoothL1Loss."""
super(SmoothL1Loss, self).__init__(reduction)
target = context.get_context("device_target")
if reduction != 'none' and target.lower() == "ascend":
raise ValueError(f"Currently Ascend device_target only support `reduction`='none', "
f"but got {reduction}")
self.beta = beta
self.reduction = reduction
self.smooth_l1_loss = P.SmoothL1Loss(self.beta, self.reduction)
def construct(self, logits, labels):
_check_is_tensor('logits', logits, self.cls_name)
_check_is_tensor('labels', labels, self.cls_name)
return self.smooth_l1_loss(logits, labels)

View File

@ -19,12 +19,11 @@ from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
smooth_l1_loss_op_info = TBERegOp("SmoothL1Loss") \
.fusion_type("OPAQUE") \
.async_flag(False) \
.binfile_name("smooth_l1_loss_v2.so") \
.binfile_name("smooth_l1_loss.so") \
.compute_cost(10) \
.kernel_name("smooth_l1_loss_v2") \
.kernel_name("smooth_l1_loss") \
.partial_flag(True) \
.attr("beta", "optional", "float", "all") \
.attr("reduction", "optional", "str", "all") \
.attr("beta", "required", "float", "all") \
.input(0, "predict", False, "required", "all") \
.input(1, "label", False, "required", "all") \
.output(0, "loss", False, "required", "all") \

View File

@ -19,13 +19,12 @@ from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
smooth_l1_loss_op_info = TBERegOp("SmoothL1Loss") \
.fusion_type("OPAQUE") \
.async_flag(False) \
.binfile_name("smooth_l1_loss_v2.so") \
.binfile_name("smooth_l1_loss.so") \
.compute_cost(10) \
.kernel_name("smooth_l1_loss_v2") \
.kernel_name("smooth_l1_loss") \
.partial_flag(True) \
.dynamic_shape(True) \
.attr("beta", "optional", "float", "all") \
.attr("reduction", "optional", "str", "all") \
.attr("beta", "required", "float", "all") \
.input(0, "predict", False, "required", "all") \
.input(1, "label", False, "required", "all") \
.output(0, "loss", False, "required", "all") \

View File

@ -19,12 +19,11 @@ from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
smooth_l1_loss_grad_op_info = TBERegOp("SmoothL1LossGrad") \
.fusion_type("OPAQUE") \
.async_flag(False) \
.binfile_name("smooth_l1_loss_grad_v2.so") \
.binfile_name("smooth_l1_loss_grad.so") \
.compute_cost(10) \
.kernel_name("smooth_l1_loss_grad_v2") \
.kernel_name("smooth_l1_loss_grad") \
.partial_flag(True) \
.attr("beta", "optional", "float", "all") \
.attr("reduction", "optional", "str", "all") \
.attr("beta", "required", "float", "all") \
.input(0, "predict", False, "required", "all") \
.input(1, "label", False, "required", "all") \
.input(2, "dout", False, "required", "all") \

View File

@ -19,13 +19,12 @@ from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
smooth_l1_loss_grad_op_info = TBERegOp("SmoothL1LossGrad") \
.fusion_type("OPAQUE") \
.async_flag(False) \
.binfile_name("smooth_l1_loss_grad_v2.so") \
.binfile_name("smooth_l1_loss_grad.so") \
.compute_cost(10) \
.kernel_name("smooth_l1_loss_grad_v2") \
.kernel_name("smooth_l1_loss_grad") \
.partial_flag(True) \
.dynamic_shape(True) \
.attr("beta", "optional", "float", "all") \
.attr("reduction", "optional", "str", "all") \
.attr("beta", "required", "float", "all") \
.input(0, "predict", False, "required", "all") \
.input(1, "label", False, "required", "all") \
.input(2, "dout", False, "required", "all") \

View File

@ -1302,7 +1302,7 @@ def smooth_l1_loss(logits, labels, beta=1.0, reduction='none'):
Its default value is 1.0. :math:`N` is the batch size.
Note:
For Ascend platform, the float64 data type of `logits` is not support now.
For Ascend platform, the 'reduction' is not support set to 'sum' or 'mean' for now.
Args:
logits (Tensor): Tensor of shape :math:`(N, *)` where :math:`*` means, any number of additional dimensions.
@ -1321,7 +1321,6 @@ def smooth_l1_loss(logits, labels, beta=1.0, reduction='none'):
TypeError: If dtype of `logits` or `labels` is neither float16 nor float32.
ValueError: If `beta` is less than or equal to 0.
ValueError: If shape of `logits` is not the same as `labels`.
ValueError: The float64 data type of `logits` is support on Ascend platform.
Supported Platforms:
``Ascend`` ``GPU`` ``CPU``

View File

@ -2835,6 +2835,10 @@ class SmoothL1Loss(Primitive):
validator.check_string(
reduction, ['none', 'sum', 'mean'], 'reduction', self.name)
self.init_prim_io_names(inputs=['prediction', 'target'], outputs=['output'])
target = context.get_context("device_target")
if reduction != 'none' and target.lower() == "ascend":
raise ValueError(f"Currently Ascend device_target only support `reduction`='none', "
f"but got {reduction}")
class SoftMarginLoss(Primitive):

View File

@ -14,62 +14,31 @@
# ============================================================================
import numpy as np
import pytest
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")
def smoothl1loss(beta, reduction):
np.random.seed(42)
prediction = np.random.randn(20).astype(np.float32)
target = np.random.randn(20).astype(np.float32)
class Net(nn.Cell):
def __init__(self, sigma=1.0):
super(Net, self).__init__()
self.SmoothL1Loss = P.SmoothL1Loss(sigma)
net = nn.SmoothL1Loss(beta, reduction)
return net(Tensor(prediction), Tensor(target))
def construct(self, pred, gt):
return self.SmoothL1Loss(pred, gt)
def verify_forward(reduction, loss, expect):
if reduction == 'none':
np.testing.assert_array_almost_equal(loss, expect)
elif reduction == "sum":
expect_sum = np.sum(expect)
np.testing.assert_array_almost_equal(loss, expect_sum, decimal=5)
elif reduction == "mean":
expect_mean = np.mean(expect)
np.testing.assert_array_almost_equal(loss, expect_mean)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.parametrize("reduction", ['none', 'mean', 'sum'])
def test_smoothl1loss(reduction):
"""
Feature: SmoothL1Loss cpu kernel.
Description: test the rightness of SmoothL1Loss cpu kernel.
Expectation: the output is same as expect.
"""
beta = 1.0
loss = smoothl1loss(beta, reduction)
expect = np.array([0.46941718, 0.00382918, 0.16829303, 2.447778, 0.04812113, 0.05953304,
2.2302065, 0.07672881, 0.00860204, 0.34798968, 0.00956192, 1.818008,
0.03262977, 0.36599946, 2.047463, 0.2168481, 0.7216947, 1.7739174,
0.08826803, 1.109165])
verify_forward(reduction, loss.asnumpy(), expect)
beta = 1 / 9
loss = smoothl1loss(beta, reduction)
expect = np.array([0.9133791, 0.03446258, 0.5246048, 2.8922224, 0.2546738, 0.289504,
2.674651, 0.33618113, 0.07560876, 0.7786982, 0.08273339, 2.2624524,
0.19990394, 0.8000138, 2.4919074, 0.6030006, 1.1661391, 2.2183619,
0.3646064, 1.5536094])
verify_forward(reduction, loss.asnumpy(), expect)
def test_net():
pred = np.random.randn(2, 4).astype(np.float32)
gt = np.random.randn(2, 4).astype(np.float32)
smooth_l1_loss = Net()
loss = smooth_l1_loss(Tensor(pred), Tensor(gt))
print("------------- input ---------------")
print("predict:\n", pred)
print("grount truth:\n", gt)
print("------------- output ---------------")
print("loss:\n", loss.asnumpy())

View File

@ -14,117 +14,44 @@
# ============================================================================
import numpy as np
import pytest
import mindspore.context as context
import mindspore.nn as nn
from mindspore import Tensor
from mindspore.ops import operations as P
from mindspore.ops.composite import GradOperation
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
class Net(nn.Cell):
def __init__(self, sigma=1.0):
super(Net, self).__init__()
self.SmoothL1Loss = P.SmoothL1Loss(sigma)
def construct(self, pred, gt):
return self.SmoothL1Loss(pred, gt)
class Grad(nn.Cell):
def __init__(self, network):
super(Grad, self).__init__()
self.grad = GradOperation(get_all=True, sens_param=True)
self.network = network
def construct(self, x1, x2, sens):
gout = self.grad(self.network)(x1, x2, sens)
return gout
def construct(self, pred, gt, dout):
return self.grad(self.network)(pred, gt, dout)
def smoothl1loss_grad(beta):
np.random.seed(42)
prediction = np.random.randn(20).astype(np.float32)
target = np.random.randn(20).astype(np.float32)
sens = np.random.randn(20).astype(np.float32)
net = nn.SmoothL1Loss(beta)
grad = Grad(net)
return grad(Tensor(prediction), Tensor(target), Tensor(sens))
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_smoothl1loss_grad_no_reduce():
"""
Feature: SmoothL1LossGrad cpu kernel.
Description: test the rightness of SmoothL1LossGrad cpu kernel.
Expectation: the output is same as expect.
"""
epsilon = 1e-6
beta = 1.0
dx = smoothl1loss_grad(beta)
dx1_expect = np.array([-0.71552587, 0.01499678, -0.06709455, -0.30110368, -0.45868093,
0.24838912, -0.46063876, 0.41411355, 0.04507046, -1.4708229,
0.04481723, 0.38508227, -0.17292616, -0.52333146, -1.0309995,
0.61330026, 0.83921754, -0.3092124, 0.1391843, -0.9755451], dtype=np.float32)
dx2_expect = -dx1_expect
diff1 = np.absolute(dx[0].asnumpy() - dx1_expect)
diff2 = np.absolute(dx[1].asnumpy() - dx2_expect)
assert(diff1 < epsilon).all()
assert(diff2 < epsilon).all()
beta = 1 / 9
dx = smoothl1loss_grad(beta)
dx1_expect = np.array([-0.73846656, 0.13497104, -0.11564828, -0.30110368, -1.478522,
0.7198442, -0.46063876, 1.0571222, 0.3436183, -1.7630402,
0.32408398, 0.38508227, -0.676922, -0.6116763, -1.0309995,
0.93128014, 0.83921754, -0.3092124, 0.33126342, -0.9755451], dtype=np.float32)
dx2_expect = -dx1_expect
diff1 = np.absolute(dx[0].asnumpy() - np.array(dx1_expect))
diff2 = np.absolute(dx[1].asnumpy() - np.array(dx2_expect))
assert(diff1 < epsilon).all()
assert(diff2 < epsilon).all()
def smoothl1loss_grad_2(beta, reduction):
prediction = np.array([1, 2, 3, 4, 5, 6], dtype=np.float32)
target = np.array([100, 2, 7, 32, 34, 1], dtype=np.float32)
net = nn.SmoothL1Loss(beta, reduction)
grad = Grad(net)
return grad(Tensor(prediction), Tensor(target), 9.)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.parametrize("reduction", ['mean', 'sum'])
def test_smoothl1loss_grad_sum(reduction):
"""
Feature: SmoothL1LossGrad cpu kernel, reduction = sum.
Description: test the rightness of SmoothL1LossGrad cpu kernel.
Expectation: the output is same as expect.
"""
beta = 1.0
dx = smoothl1loss_grad_2(beta, reduction)
sum_dx1_expect = np.array([-9, 0, -9, -9, -9, 9], dtype=np.float32)
sum_dx2_expect = -sum_dx1_expect
mean_dx1_expect = np.array(
[-1.5, 0, -1.5, -1.5, -1.5, 1.5], dtype=np.float32)
mean_dx2_expect = -mean_dx1_expect
print("dx[0].asnumpy()", dx[0].asnumpy())
print("dx[1].asnumpy()", dx[1].asnumpy())
if reduction == 'sum':
np.testing.assert_array_almost_equal(dx[0].asnumpy(), sum_dx1_expect)
np.testing.assert_array_almost_equal(dx[1].asnumpy(), sum_dx2_expect)
if reduction == 'mean':
np.testing.assert_array_almost_equal(dx[0].asnumpy(), mean_dx1_expect)
np.testing.assert_array_almost_equal(dx[1].asnumpy(), mean_dx2_expect)
def test_net():
pred = np.random.randn(2, 4).astype(np.float32)
gt = np.random.randn(2, 4).astype(np.float32)
dout = np.random.randn(2, 4).astype(np.float32)
smooth_l1_loss_grad = Grad(Net())
output = smooth_l1_loss_grad(Tensor(pred), Tensor(gt), Tensor(dout))
print("------------- input ---------------")
print("predict:\n", pred)
print("grount truth:\n", gt)
print("dout:\n", dout)
print("------------- output ---------------")
print("predict grad:\n", output[0].asnumpy())