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

Do not add explicit reshards for ReverseOp. #288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

copybara-service[bot]
Copy link

Do not add explicit reshards for ReverseOp.

GSPMD partitioner adds extra collective permute when an explicit reshards added for the case that the operand is not sharded but the result is sharded on the reversing dimension.

Hence, for the moment, turn off explicit reshard inserting for ReverseOp.

Comparing the output of GSPMD partitioner for:

func.func @main(%arg0: tensor<4x32xf32> [{}, {}]) {
  %0 = stablehlo.reverse %arg0, dims = [1] {
    sdy.sharding = [{}, {"x"}],
    sdy.sharding_rule = ([i, j])->([i, j])}
  : tensor<4x32xf32>
  return %0 : tensor<4x32xf32>
} -> tensor<4x32xf32> [{}, {"x"}]

vs

func.func @main(%arg0: tensor<4x32xf32> [{}, {}]) {
  %0 = sdy.reshard %arg0 [{}, {"x"}]
  %1 = stablehlo.reverse %0, dims = [1] {
    sdy.sharding = [{}, {"x"}],
    sdy.sharding_rule = ([i, j])->([i, j])}
  : tensor<4x32xf32>
  return %1 : tensor<4x32xf32>
} -> tensor<4x32xf32> [{}, {"x"}]

GSPMD partitioner outputs the following diff:

ENTRY %main.6_spmd (param: f32[4,32]) -> f32[4,16] {
  %param = f32[4,32]{1,0} 
    parameter(0), 
    sharding={replicated}

  %constant.1 = s32[] constant(0)
  %constant.2 = s32[2]{0} constant({0, 16})
  < %constant = u32[8]{0} constant({1, 1, 1, 1, 0, 0, 0, 0})
  > %constant = u32[8]{0} constant({0, 0, 0, 0, 1, 1, 1, 1})
  %partition-id = u32[] partition-id()
  %dynamic-slice = u32[1]{0} 
    dynamic-slice(u32[8]{0} %constant, u32[] %partition-id),
    dynamic_slice_sizes={1}
  %reshape = u32[] 
    reshape(u32[1]{0} %dynamic-slice)
  %dynamic-slice.1 = s32[1]{0} 
    dynamic-slice(s32[2]{0} %constant.2, u32[] %reshape),
    dynamic_slice_sizes={1}
  %reshape.1 = s32[] reshape(s32[1]{0} %dynamic-slice.1)
  %dynamic-slice.2 = f32[4,16]{1,0} 
    dynamic-slice(f32[4,32]{1,0} %param, s32[] %constant.1, s32[] %reshape.1),
    dynamic_slice_sizes={4,16}

  < ROOT %reverse.0 = f32[4,16]{1,0}
      reverse(f32[4,16]{1,0} %dynamic-slice.2)
  > %collective-permute = f32[4,16]{1,0} 
      collective-permute(f32[4,16]{1,0} %dynamic-slice.2), 
      channel_id=1,
      source_target_pairs={{0,4},{1,5},{2,6},{3,7},{4,0},{5,1},{6,2},{7,3}}
  > ROOT %reverse.0 = f32[4,16]{1,0} 
      reverse(f32[4,16]{1,0} %collective-permute), 
      dimensions={1}

GSPMD partitioner adds extra collective permute when an explicit reshards added for the case that the operand is not sharded but the result is sharded on the reversing dimension.

Hence, for the moment, turn off explicit reshard inserting for ReverseOp.

Comparing the output of GSPMD partitioner for:

```
func.func @main(%arg0: tensor<4x32xf32> [{}, {}]) {
  %0 = stablehlo.reverse %arg0, dims = [1] {
    sdy.sharding = [{}, {"x"}],
    sdy.sharding_rule = ([i, j])->([i, j])}
  : tensor<4x32xf32>
  return %0 : tensor<4x32xf32>
} -> tensor<4x32xf32> [{}, {"x"}]
```
vs
```
func.func @main(%arg0: tensor<4x32xf32> [{}, {}]) {
  %0 = sdy.reshard %arg0 [{}, {"x"}]
  %1 = stablehlo.reverse %0, dims = [1] {
    sdy.sharding = [{}, {"x"}],
    sdy.sharding_rule = ([i, j])->([i, j])}
  : tensor<4x32xf32>
  return %1 : tensor<4x32xf32>
} -> tensor<4x32xf32> [{}, {"x"}]
```
GSPMD partitioner outputs the following diff:
```
ENTRY %main.6_spmd (param: f32[4,32]) -> f32[4,16] {
  %param = f32[4,32]{1,0}
    parameter(0),
    sharding={replicated}

  %constant.1 = s32[] constant(0)
  %constant.2 = s32[2]{0} constant({0, 16})
  < %constant = u32[8]{0} constant({1, 1, 1, 1, 0, 0, 0, 0})
  > %constant = u32[8]{0} constant({0, 0, 0, 0, 1, 1, 1, 1})
  %partition-id = u32[] partition-id()
  %dynamic-slice = u32[1]{0}
    dynamic-slice(u32[8]{0} %constant, u32[] %partition-id),
    dynamic_slice_sizes={1}
  %reshape = u32[]
    reshape(u32[1]{0} %dynamic-slice)
  %dynamic-slice.1 = s32[1]{0}
    dynamic-slice(s32[2]{0} %constant.2, u32[] %reshape),
    dynamic_slice_sizes={1}
  %reshape.1 = s32[] reshape(s32[1]{0} %dynamic-slice.1)
  %dynamic-slice.2 = f32[4,16]{1,0}
    dynamic-slice(f32[4,32]{1,0} %param, s32[] %constant.1, s32[] %reshape.1),
    dynamic_slice_sizes={4,16}

  < ROOT %reverse.0 = f32[4,16]{1,0}
      reverse(f32[4,16]{1,0} %dynamic-slice.2)
  > %collective-permute = f32[4,16]{1,0}
      collective-permute(f32[4,16]{1,0} %dynamic-slice.2),
      channel_id=1,
      source_target_pairs={{0,4},{1,5},{2,6},{3,7},{4,0},{5,1},{6,2},{7,3}}
  > ROOT %reverse.0 = f32[4,16]{1,0}
      reverse(f32[4,16]{1,0} %collective-permute),
      dimensions={1}
```

PiperOrigin-RevId: 712495848
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.

1 participant