Skip to content

Canonicalize flow.tensor.clone to keep it more local to usage #5291

Closed

Description

@benvanik

Now that we are performing in-place operations there are flow.tensor.clones appearing to preserve correctness. In cases of wide fan-out these cause some pathological behavior:

      %22 = flow.dispatch @serving_default_dispatch_21::@serving_default_dispatch_21[%c384, %c4, %c1]() : () -> tensor<1x4x384xf32>
      %23 = flow.tensor.clone %22 : tensor<1x4x384xf32>
      %24 = flow.tensor.clone %23 : tensor<1x4x384xf32>
      %25 = flow.tensor.clone %24 : tensor<1x4x384xf32>
      %26 = flow.tensor.clone %25 : tensor<1x4x384xf32>
      %27 = flow.tensor.clone %26 : tensor<1x4x384xf32>
      %28 = flow.tensor.clone %27 : tensor<1x4x384xf32>
      %29 = flow.tensor.clone %28 : tensor<1x4x384xf32>
      %30 = flow.tensor.clone %29 : tensor<1x4x384xf32>
      %31 = flow.tensor.clone %30 : tensor<1x4x384xf32>
      %32 = flow.tensor.clone %31 : tensor<1x4x384xf32>
      %33 = flow.tensor.clone %32 : tensor<1x4x384xf32>
      %34 = flow.tensor.clone %33 : tensor<1x4x384xf32>
      %35 = flow.tensor.clone %34 : tensor<1x4x384xf32>
      %36 = flow.tensor.clone %35 : tensor<1x4x384xf32>
      %37 = flow.tensor.clone %36 : tensor<1x4x384xf32>
      %38 = flow.tensor.clone %37 : tensor<1x4x384xf32>
      %39 = flow.tensor.clone %38 : tensor<1x4x384xf32>
      %40 = flow.tensor.clone %39 : tensor<1x4x384xf32>
      %41 = flow.tensor.clone %40 : tensor<1x4x384xf32>
      %42 = flow.tensor.clone %41 : tensor<1x4x384xf32>
      %43 = flow.tensor.clone %42 : tensor<1x4x384xf32>
      %44 = flow.tensor.clone %43 : tensor<1x4x384xf32>
      %45 = flow.tensor.clone %44 : tensor<1x4x384xf32>
      %46 = flow.dispatch @serving_default_dispatch_22::@serving_default_dispatch_22[%c384, %c4, %c1](%21, %22) : (tensor<4x384x384xf32>, tensor<1x4x384xf32>) -> %22
      // uses of %23-%46 throughout the model

Some simple code motion will help here: sinking the clones to immediately prior to their use will shorten the lifetime of these transient tensors.

Activity

  1. added
    compiler/dialects
    performance ⚡
    on Apr 2, 2021
  2. self-assigned this
    on Apr 2, 2021
  3. added a commit that references this issue on Apr 2, 2021
    d5b34a8
  4. added a commit that references this issue on Apr 2, 2021
    2681dff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

compiler/dialectsperformance ⚡

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions