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

support noncontiguous binary op #9986

Merged
merged 33 commits into from
May 23, 2023
Merged

support noncontiguous binary op #9986

merged 33 commits into from
May 23, 2023

Conversation

ofhwei
Copy link
Contributor

@ofhwei ofhwei commented Mar 14, 2023

This PR now support the basic binary operation(+,-,*,/) when inputs is/are not contiguous.

for example,

import oneflow as flow

#case 1
x = flow.randn(3, 300, 400, 64).cuda()
y = flow.randn(3, 400, 300, 64).cuda()
z = x.transpose(1, 2)
ref = z / y
out = flow._C.noncontiguous_binary_op(z, y, op="div", inplace = False) 

# case 2
x = flow.randn(3, 400, 300, 64).cuda()
y = flow.randn(3, 400, 300, 64).cuda()
z = x.transpose(1, 2)
y = y.transpose(1, 2)
ref = z / y
out = flow._C.noncontiguous_binary_op(z, y, op="div", inplace = False) 

The nsys results below [green box for this pr and red box for oneflow naive implementation] show that we can effectively omit the ToContiguous stages.
image
image

However, transpose the last dim is not good supported now, we will make this better soon.

@CLAassistant
Copy link

CLAassistant commented Mar 14, 2023

CLA assistant check
All committers have signed the CLA.

@ofhwei ofhwei requested a review from daquexian as a code owner March 17, 2023 08:49
@ofhwei ofhwei changed the title support noncontiguous op support noncontiguous binary op Mar 17, 2023
@ofhwei ofhwei requested review from clackhan and zobinHuang April 24, 2023 08:02
@github-actions
Copy link
Contributor

Code got formatted by CI. Please request CI again if you still want to have this PR merged. If the PR is from a forked repo, please download the patch files from the GitHub Actions web page and apply them locally.

@github-actions
Copy link
Contributor

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

@github-actions
Copy link
Contributor

Speed stats:
GPU Name: NVIDIA GeForce RTX 3090 

❌ OneFlow resnet50 time: 43.2ms (= 4319.9ms / 100, input_shape=[16, 3, 224, 224])
PyTorch resnet50 time: 57.6ms (= 5756.1ms / 100, input_shape=[16, 3, 224, 224])
✔️ Relative speed: 1.33 (= 57.6ms / 43.2ms)

OneFlow resnet50 time: 26.3ms (= 2629.1ms / 100, input_shape=[8, 3, 224, 224])
PyTorch resnet50 time: 74.4ms (= 7438.0ms / 100, input_shape=[8, 3, 224, 224])
✔️ Relative speed: 2.83 (= 74.4ms / 26.3ms)

OneFlow resnet50 time: 56.8ms (= 11356.7ms / 200, input_shape=[4, 3, 224, 224])
PyTorch resnet50 time: 62.8ms (= 12564.8ms / 200, input_shape=[4, 3, 224, 224])
✔️ Relative speed: 1.11 (= 62.8ms / 56.8ms)

OneFlow resnet50 time: 52.8ms (= 10557.7ms / 200, input_shape=[2, 3, 224, 224])
PyTorch resnet50 time: 57.7ms (= 11549.3ms / 200, input_shape=[2, 3, 224, 224])
✔️ Relative speed: 1.09 (= 57.7ms / 52.8ms)

OneFlow resnet50 time: 44.3ms (= 8852.2ms / 200, input_shape=[1, 3, 224, 224])
PyTorch resnet50 time: 60.7ms (= 12134.3ms / 200, input_shape=[1, 3, 224, 224])
✔️ Relative speed: 1.37 (= 60.7ms / 44.3ms)

OneFlow swin dataloader time: 0.353s (= 70.550s / 200, num_workers=1)
PyTorch swin dataloader time: 0.133s (= 26.510s / 200, num_workers=1)
Relative speed: 0.376 (= 0.133s / 0.353s)

OneFlow swin dataloader time: 0.057s (= 11.495s / 200, num_workers=4)
PyTorch swin dataloader time: 0.033s (= 6.624s / 200, num_workers=4)
Relative speed: 0.576 (= 0.033s / 0.057s)

OneFlow swin dataloader time: 0.030s (= 6.060s / 200, num_workers=8)
PyTorch swin dataloader time: 0.017s (= 3.317s / 200, num_workers=8)
Relative speed: 0.547 (= 0.017s / 0.030s)

❌ OneFlow resnet50 time: 48.6ms (= 4864.0ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 65.2ms (= 6519.3ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.34 (= 65.2ms / 48.6ms)

OneFlow resnet50 time: 36.3ms (= 3630.1ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 45.4ms (= 4539.6ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.25 (= 45.4ms / 36.3ms)

OneFlow resnet50 time: 28.4ms (= 5688.8ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 39.0ms (= 7796.7ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.37 (= 39.0ms / 28.4ms)

OneFlow resnet50 time: 25.7ms (= 5139.6ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 38.7ms (= 7732.9ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.50 (= 38.7ms / 25.7ms)

OneFlow resnet50 time: 23.9ms (= 4782.4ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 36.1ms (= 7223.4ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.51 (= 36.1ms / 23.9ms)

@github-actions
Copy link
Contributor

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

@github-actions
Copy link
Contributor

Speed stats:
GPU Name: NVIDIA GeForce RTX 3080 Ti 

❌ OneFlow resnet50 time: 43.0ms (= 4300.0ms / 100, input_shape=[16, 3, 224, 224])
PyTorch resnet50 time: 57.3ms (= 5730.4ms / 100, input_shape=[16, 3, 224, 224])
✔️ Relative speed: 1.33 (= 57.3ms / 43.0ms)

OneFlow resnet50 time: 26.1ms (= 2607.7ms / 100, input_shape=[8, 3, 224, 224])
PyTorch resnet50 time: 37.8ms (= 3778.6ms / 100, input_shape=[8, 3, 224, 224])
✔️ Relative speed: 1.45 (= 37.8ms / 26.1ms)

OneFlow resnet50 time: 18.8ms (= 3769.5ms / 200, input_shape=[4, 3, 224, 224])
PyTorch resnet50 time: 35.2ms (= 7042.8ms / 200, input_shape=[4, 3, 224, 224])
✔️ Relative speed: 1.87 (= 35.2ms / 18.8ms)

OneFlow resnet50 time: 18.4ms (= 3687.4ms / 200, input_shape=[2, 3, 224, 224])
PyTorch resnet50 time: 31.6ms (= 6324.6ms / 200, input_shape=[2, 3, 224, 224])
✔️ Relative speed: 1.72 (= 31.6ms / 18.4ms)

OneFlow resnet50 time: 18.2ms (= 3640.7ms / 200, input_shape=[1, 3, 224, 224])
PyTorch resnet50 time: 27.1ms (= 5419.9ms / 200, input_shape=[1, 3, 224, 224])
✔️ Relative speed: 1.49 (= 27.1ms / 18.2ms)

OneFlow swin dataloader time: 0.200s (= 40.094s / 200, num_workers=1)
PyTorch swin dataloader time: 0.129s (= 25.849s / 200, num_workers=1)
Relative speed: 0.645 (= 0.129s / 0.200s)

OneFlow swin dataloader time: 0.053s (= 10.671s / 200, num_workers=4)
PyTorch swin dataloader time: 0.032s (= 6.405s / 200, num_workers=4)
Relative speed: 0.600 (= 0.032s / 0.053s)

OneFlow swin dataloader time: 0.032s (= 6.334s / 200, num_workers=8)
PyTorch swin dataloader time: 0.017s (= 3.307s / 200, num_workers=8)
Relative speed: 0.522 (= 0.017s / 0.032s)

❌ OneFlow resnet50 time: 47.6ms (= 4761.8ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 63.4ms (= 6337.4ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.33 (= 63.4ms / 47.6ms)

OneFlow resnet50 time: 32.3ms (= 3233.2ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 44.8ms (= 4476.7ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.38 (= 44.8ms / 32.3ms)

OneFlow resnet50 time: 24.2ms (= 4836.8ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 41.4ms (= 8281.5ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.71 (= 41.4ms / 24.2ms)

OneFlow resnet50 time: 22.3ms (= 4456.2ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 36.8ms (= 7365.8ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.65 (= 36.8ms / 22.3ms)

OneFlow resnet50 time: 21.0ms (= 4205.3ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 33.9ms (= 6788.1ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.61 (= 33.9ms / 21.0ms)

Comment on lines +28 to +30
.Broadcast(user_op::OpArg("lhs", 0))
.Broadcast(user_op::OpArg("rhs", 0))
.Broadcast(user_op::OpArg("y", 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.Broadcast(user_op::OpArg("lhs", 0))
.Broadcast(user_op::OpArg("rhs", 0))
.Broadcast(user_op::OpArg("y", 0))
.Broadcast(user_op::OpArg("x", 0))
.Broadcast(user_op::OpArg("y", 0))
.Broadcast(user_op::OpArg("z", 0))

namespace oneflow {

/*static*/ Maybe<void> NonContiguousBinaryOp::GetSbp(user_op::SbpContext* ctx) {
ctx->NewBuilder()
Copy link
Contributor

Choose a reason for hiding this comment

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

加个注释,只支持B

ctx->SetOutputShape("y", 0, lhs);
const bool inplace = ctx->Attr<bool>("inplace");
if (inplace)
ctx->SetOutputStride("y", 0, ctx->InputStride("lhs", 0));
Copy link
Contributor

Choose a reason for hiding this comment

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

最好加个花括号

@ofhwei ofhwei requested a review from oneflow-ci-bot May 21, 2023 16:17
@ofhwei ofhwei enabled auto-merge (squash) May 21, 2023 16:19
@github-actions
Copy link
Contributor

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

@github-actions
Copy link
Contributor

Speed stats:

@github-actions
Copy link
Contributor

Speed stats:

2 similar comments
@github-actions
Copy link
Contributor

Speed stats:

@github-actions
Copy link
Contributor

Speed stats:

@github-actions
Copy link
Contributor

Speed stats:

@github-actions
Copy link
Contributor

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

@github-actions
Copy link
Contributor

Speed stats:
GPU Name: NVIDIA GeForce RTX 3090 

❌ OneFlow resnet50 time: 42.7ms (= 4270.8ms / 100, input_shape=[16, 3, 224, 224])
PyTorch resnet50 time: 58.3ms (= 5831.5ms / 100, input_shape=[16, 3, 224, 224])
✔️ Relative speed: 1.37 (= 58.3ms / 42.7ms)

OneFlow resnet50 time: 26.2ms (= 2618.2ms / 100, input_shape=[8, 3, 224, 224])
PyTorch resnet50 time: 36.7ms (= 3674.0ms / 100, input_shape=[8, 3, 224, 224])
✔️ Relative speed: 1.40 (= 36.7ms / 26.2ms)

OneFlow resnet50 time: 19.6ms (= 3917.3ms / 200, input_shape=[4, 3, 224, 224])
PyTorch resnet50 time: 35.3ms (= 7057.3ms / 200, input_shape=[4, 3, 224, 224])
✔️ Relative speed: 1.80 (= 35.3ms / 19.6ms)

OneFlow resnet50 time: 18.9ms (= 3787.6ms / 200, input_shape=[2, 3, 224, 224])
PyTorch resnet50 time: 32.8ms (= 6566.8ms / 200, input_shape=[2, 3, 224, 224])
✔️ Relative speed: 1.73 (= 32.8ms / 18.9ms)

OneFlow resnet50 time: 17.2ms (= 3448.9ms / 200, input_shape=[1, 3, 224, 224])
PyTorch resnet50 time: 29.5ms (= 5903.4ms / 200, input_shape=[1, 3, 224, 224])
✔️ Relative speed: 1.71 (= 29.5ms / 17.2ms)

OneFlow swin dataloader time: 0.200s (= 39.918s / 200, num_workers=1)
PyTorch swin dataloader time: 0.130s (= 25.948s / 200, num_workers=1)
Relative speed: 0.650 (= 0.130s / 0.200s)

OneFlow swin dataloader time: 0.055s (= 11.005s / 200, num_workers=4)
PyTorch swin dataloader time: 0.033s (= 6.596s / 200, num_workers=4)
Relative speed: 0.599 (= 0.033s / 0.055s)

OneFlow swin dataloader time: 0.031s (= 6.119s / 200, num_workers=8)
PyTorch swin dataloader time: 0.017s (= 3.327s / 200, num_workers=8)
Relative speed: 0.544 (= 0.017s / 0.031s)

❌ OneFlow resnet50 time: 48.6ms (= 4862.8ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 65.7ms (= 6573.5ms / 100, input_shape=[16, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.35 (= 65.7ms / 48.6ms)

OneFlow resnet50 time: 37.4ms (= 3738.3ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 45.9ms (= 4592.4ms / 100, input_shape=[8, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.23 (= 45.9ms / 37.4ms)

OneFlow resnet50 time: 28.8ms (= 5755.9ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 38.7ms (= 7731.7ms / 200, input_shape=[4, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.34 (= 38.7ms / 28.8ms)

OneFlow resnet50 time: 25.8ms (= 5164.0ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 38.8ms (= 7765.5ms / 200, input_shape=[2, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.50 (= 38.8ms / 25.8ms)

OneFlow resnet50 time: 24.4ms (= 4884.3ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
PyTorch resnet50 time: 35.8ms (= 7158.0ms / 200, input_shape=[1, 3, 224, 224], ddp, world size=2)
✔️ Relative speed: 1.47 (= 35.8ms / 24.4ms)

@ofhwei ofhwei merged commit 08ded68 into master May 23, 2023
@ofhwei ofhwei deleted the dev_transpose_add branch May 23, 2023 17:11
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.

5 participants