Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove paddle.where in cross_tropy_loss #38456

Merged

Conversation

HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Dec 26, 2021

PR types

Function optimization

PR changes

APIs

Describe

  1. 使用已修复BUG的paddle.min和paddle.max代替paddle.where来检查labels合法性,减少GPU和CPU之间不必要的同步

@paddle-bot-old
Copy link

paddle-bot-old bot commented Dec 26, 2021

✅ This PR's description meets the template requirements!
Please wait for other CI results.

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Contributor

@pangyoki pangyoki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以描述下删掉的这些check的功能和删掉之后有啥影响吗?为什么之前需要加上这个checking,现在直接删掉的时候,出现这些case是有其它代码会检查吗?

@HydrogenSulfate
Copy link
Contributor Author

可以描述下删掉的这些check的功能和删掉之后有啥影响吗?为什么之前需要加上这个checking,现在直接删掉的时候,出现这些case是有其它代码会检查吗?

去掉这个检查后,如果labels范围超出边界,则会在gather_nd这里报错

def test_LabelValue_ExceedMax():
                input_data = paddle.rand(shape=[20, 100])
                label_data = paddle.randint(
                    0, 100, shape=[20, 1], dtype="int64")  # hard label
                label_data[0] = 100
                weight_data = paddle.rand([100])  # provide weight
                paddle.nn.functional.cross_entropy(
                    input=input_data,
                    label=label_data,
                    weight=weight_data,
                    ignore_index=-100)


Error: /paddle/paddle/fluid/operators/gather.cu.h:62 Assertion `index_value >= 0 && index_value < input_dims[j]` failed. The index is out of bounds, please check whether the dimensions of index and input meet the requirements. It should be less than [100] and greater than or equal to 0, but received [0]
def test_LabelValue_ExceedMin():
                input_data = paddle.rand(shape=[20, 100])
                label_data = paddle.randint(
                    0, 100, shape=[20, 1], dtype="int64")  # hard label
                label_data[0] = -1
                weight_data = paddle.rand([100])  # provide weight
                paddle.nn.functional.cross_entropy(
                    input=input_data,
                    label=label_data,
                    weight=weight_data,
                    ignore_index=-100)

Error: /paddle/paddle/fluid/operators/gather.cu.h:62 Assertion `index_value >= 0 && index_value < input_dims[j]` failed. The index is out of bounds, please check whether the dimensions of index and input meet the requirements. It should be less than [100] and greater than or equal to 0, but received [0]

@pangyoki
Copy link
Contributor

可以描述下删掉的这些check的功能和删掉之后有啥影响吗?为什么之前需要加上这个checking,现在直接删掉的时候,出现这些case是有其它代码会检查吗?

去掉这个检查后,如果labels范围超出边界,则会在gather_nd这里报错

def test_LabelValue_ExceedMax():
                input_data = paddle.rand(shape=[20, 100])
                label_data = paddle.randint(
                    0, 100, shape=[20, 1], dtype="int64")  # hard label
                label_data[0] = 100
                weight_data = paddle.rand([100])  # provide weight
                paddle.nn.functional.cross_entropy(
                    input=input_data,
                    label=label_data,
                    weight=weight_data,
                    ignore_index=-100)


Error: /paddle/paddle/fluid/operators/gather.cu.h:62 Assertion `index_value >= 0 && index_value < input_dims[j]` failed. The index is out of bounds, please check whether the dimensions of index and input meet the requirements. It should be less than [100] and greater than or equal to 0, but received [0]
def test_LabelValue_ExceedMin():
                input_data = paddle.rand(shape=[20, 100])
                label_data = paddle.randint(
                    0, 100, shape=[20, 1], dtype="int64")  # hard label
                label_data[0] = -1
                weight_data = paddle.rand([100])  # provide weight
                paddle.nn.functional.cross_entropy(
                    input=input_data,
                    label=label_data,
                    weight=weight_data,
                    ignore_index=-100)

Error: /paddle/paddle/fluid/operators/gather.cu.h:62 Assertion `index_value >= 0 && index_value < input_dims[j]` failed. The index is out of bounds, please check whether the dimensions of index and input meet the requirements. It should be less than [100] and greater than or equal to 0, but received [0]

这样是不是就不容易判断出是cross_entropy的报错了啊,有python栈可以看出是cross_entropy的问题吗?
静态图也是相同的报错吗?是应该把动态图位置的报错删掉,还是说应该为静态图加上报错呢?

@paddle-bot-old
Copy link

paddle-bot-old bot commented Jan 5, 2022

Sorry to inform you that 056394c's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@HydrogenSulfate HydrogenSulfate changed the title Remove check cross entropy Remove paddle.where in cross_tropy_loss Jan 9, 2022
raise ValueError(
"Target({}) is out of class_dimension's upper bound({})".
format(invalid_label[0], input.shape[axis] - 1))
valid_label = paddle.cast(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前讨论过在weight is not None的条件下,才需要判断label的合法性。需要把检查移动到weight is not None中吗?

Copy link
Contributor Author

@HydrogenSulfate HydrogenSulfate Jan 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于softmax_with_cross_entropy中没有对labels进行充分的合法性检查,所以这里在hard label条件下就判断label的合法性,无论是否用了weight

Copy link
Contributor

@pangyoki pangyoki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chajchaj
Copy link
Contributor

LGTM

@chajchaj chajchaj merged commit e30150d into PaddlePaddle:develop Jan 10, 2022
@HydrogenSulfate HydrogenSulfate deleted the remove_check_cross_entropy branch October 6, 2024 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants