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

fix softmax, math unary/binary kernel int overflow #8472

Merged
merged 5 commits into from
Jun 24, 2022
Merged

Conversation

chengtbf
Copy link
Contributor

修复 softmax, math unary / binary cuda kernel 可能遇到的 int32 溢出的问题。

对于 CUDA_1D_KERNEL_LOOP 中会对 i 进行整数除法的 kernel,则通过 if 判断进入不同的分支,防止在大部分情况 int32 可以工作的时候影响性能。

dx[i] = __hmul(dy[row_id], __hsub(prob[i], __float2half(1.0)));
} else {
dx[i] = __hmul(dy[row_id], prob[i]);
if (elem_cnt >= 2147483647) {
Copy link
Contributor

Choose a reason for hiding this comment

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

我觉得可以把这个判断放外面,加个模板参数IDX,在外面DispatchIndexType比较好。

Copy link
Contributor

Choose a reason for hiding this comment

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

if (elem_cnt < GetMaxVal<int32_t>() / 2) {

}

Copy link
Contributor Author

@chengtbf chengtbf Jun 23, 2022

Choose a reason for hiding this comment

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

我觉得可以把这个判断放外面,加个模板参数IDX,在外面DispatchIndexType比较好。

外面是这样?

if (elem_cnt < GetMaxVal<int32_t>() / 2) {

ComputeEntropyGpuHalf<K, int32_t><<<BlocksNum4ThreadsNum(num_instances), kCudaThreadsNumPerBlock, 0,
                               stream->As<ep::CudaStream>()->cuda_stream()>>>(
        num_instances, num_classes, depth, lower_bound, reinterpret_cast<const half*>(x), labels,
        reinterpret_cast<half*>(y));

} else {
ComputeEntropyGpuHalf<K, int64_t><<<BlocksNum4ThreadsNum(num_instances), kCudaThreadsNumPerBlock, 0,
                               stream->As<ep::CudaStream>()->cuda_stream()>>>(
        num_instances, num_classes, depth, lower_bound, reinterpret_cast<const half*>(x), labels,
        reinterpret_cast<half*>(y));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

嗯,这样可以的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (elem_cnt < GetMaxVal<int32_t>() / 2) {

这里为什么要除以 2 呢? @guo-ran

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Guo Ran
5:08 PM
这个是因为线程循环不是每次加1,而是加线程数,为了避免越界,我们系统里面好像统一都直接除2处理了。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里改了

@chengtbf chengtbf requested a review from oneflow-ci-bot June 24, 2022 10:14
@github-actions
Copy link
Contributor

View latest API docs preview at: https://staging.oneflow.info/docs/Oneflow-Inc/oneflow/pr/8472/

@github-actions
Copy link
Contributor

CI failed when running job: cuda-misc. PR label automerge has been removed

@github-actions
Copy link
Contributor

Speed stats:

@chengtbf chengtbf requested review from oneflow-ci-bot and removed request for oneflow-ci-bot June 24, 2022 15:30
@github-actions
Copy link
Contributor

Speed stats:
GPU Name: NVIDIA GeForce GTX 1080 

❌ OneFlow resnet50 time: 129.7ms (= 12972.7ms / 100, input_shape=[16, 3, 224, 224])
PyTorch resnet50 time: 149.4ms (= 14939.7ms / 100, input_shape=[16, 3, 224, 224])
✔️ Relative speed: 1.15 (= 149.4ms / 129.7ms)

OneFlow resnet50 time: 75.8ms (= 7582.8ms / 100, input_shape=[8, 3, 224, 224])
PyTorch resnet50 time: 85.2ms (= 8522.9ms / 100, input_shape=[8, 3, 224, 224])
✔️ Relative speed: 1.12 (= 85.2ms / 75.8ms)

OneFlow resnet50 time: 50.1ms (= 10028.3ms / 200, input_shape=[4, 3, 224, 224])
PyTorch resnet50 time: 65.6ms (= 13121.1ms / 200, input_shape=[4, 3, 224, 224])
✔️ Relative speed: 1.31 (= 65.6ms / 50.1ms)

OneFlow resnet50 time: 41.1ms (= 8228.9ms / 200, input_shape=[2, 3, 224, 224])
PyTorch resnet50 time: 44.2ms (= 8834.9ms / 200, input_shape=[2, 3, 224, 224])
✔️ Relative speed: 1.07 (= 44.2ms / 41.1ms)

OneFlow resnet50 time: 35.6ms (= 7127.2ms / 200, input_shape=[1, 3, 224, 224])
PyTorch resnet50 time: 37.9ms (= 7583.9ms / 200, input_shape=[1, 3, 224, 224])
✔️ Relative speed: 1.06 (= 37.9ms / 35.6ms)

OneFlow swin dataloader time: 0.406s (= 81.251s / 200, num_workers=1)
PyTorch swin dataloader time: 0.149s (= 29.871s / 200, num_workers=1)
Relative speed: 0.368 (= 0.149s / 0.406s)

OneFlow swin dataloader time: 0.128s (= 25.585s / 200, num_workers=4)
PyTorch swin dataloader time: 0.044s (= 8.717s / 200, num_workers=4)
Relative speed: 0.341 (= 0.044s / 0.128s)

OneFlow swin dataloader time: 0.063s (= 12.619s / 200, num_workers=8)
PyTorch swin dataloader time: 0.022s (= 4.386s / 200, num_workers=8)
Relative speed: 0.348 (= 0.022s / 0.063s)

❌ OneFlow resnet50 time: 145.6ms (= 14561.0ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 169.0ms (= 16898.9ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.16 (= 169.0ms / 145.6ms)

OneFlow resnet50 time: 94.1ms (= 9411.6ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 112.7ms (= 11272.8ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.20 (= 112.7ms / 94.1ms)

OneFlow resnet50 time: 69.6ms (= 13916.5ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 88.3ms (= 17655.7ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.27 (= 88.3ms / 69.6ms)

OneFlow resnet50 time: 57.0ms (= 11402.5ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 80.3ms (= 16058.7ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.41 (= 80.3ms / 57.0ms)

OneFlow resnet50 time: 52.2ms (= 10435.0ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 76.4ms (= 15288.6ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.47 (= 76.4ms / 52.2ms)

@github-actions
Copy link
Contributor

CI failed when running job: cpu-module. PR label automerge has been removed

@chengtbf chengtbf requested review from oneflow-ci-bot and removed request for oneflow-ci-bot June 24, 2022 16:28
@github-actions
Copy link
Contributor

Speed stats:

@chengtbf chengtbf requested review from oneflow-ci-bot and removed request for oneflow-ci-bot June 24, 2022 16:36
@github-actions
Copy link
Contributor

View latest API docs preview at: https://staging.oneflow.info/docs/Oneflow-Inc/oneflow/pr/8472/

@mergify mergify bot merged commit 3ea445a into master Jun 24, 2022
@mergify mergify bot deleted the dev_cc_int_overflow branch June 24, 2022 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants