Skip to content

Numerical issues in integration tests in Linalg on tensors #5313

Closed

Description

@hanhanW

Describe the bug
There are some integration tests failed in Linalg on tensors because of numerical issue. This happens on both CPU and GPU backends.

Unfortunately, we can not debug it as before because VMLA does not work with -iree-flow-dispatch-linalg-on-tensors. And it fails on both backends. Luckily, the model file can be much smaller because we can use -iree-flow-strip-and-splat-constant-variables to strip the flow variables, and the issue can be reproduced with the faked weight model.

To Reproduce

Download keyword_spotting_tests_fake-mobilenet-v2.mlir: https://gist.github.com/hanhanW/dafdd5ae914f866e592e124428467aed

  1. Generate the module in Linalg on buffers: iree-translate --iree-mlir-to-vm-bytecode-module --iree-hal-target-backends=vulkan-spirv ~/keyword_spotting_tests_mobilenet-v2.mlir -o a.vmfb
  2. Generate the module in Linalg on tensors: iree-translate --iree-mlir-to-vm-bytecode-module --iree-hal-target-backends=vulkan-spirv ~/keyword_spotting_tests_mobilenet-v2.mlir -o b.vmfb -iree-flow-dispatch-linalg-on-tensors=true -iree-codegen-spirv-experimental-linalg-on-tensors=true
  3. Run the first module: iree-run-module --module_file=a.vmfb --driver=vulkan --entry_function=call --function_inputs=1x16000xf32=0 and you will see twelve 0.0431243.
  4. Run the second module: iree-run-module --module_file=b.vmfb --driver=vulkan --entry_function=call --function_inputs=1x16000xf32=0 and you will see twelve 0.0787472.

Activity

  1. added
    bug 🐞
    codegen
    on Apr 5, 2021
  2. self-assigned this
    on Apr 5, 2021
  3. hanhanW commented on Apr 7, 2021

    @hanhanW
    ContributorAuthor

    I feel that this is reduction fusion issue in generic op. Narrow down the test case to

    module  {
      func @call(%arg0: tensor<1x24x1x4xf32> -> tensor<1x4xf32> attributes {iree.module.export} {
        %0 = mhlo.constant dense<0.000000e+00> : tensor<f32>
        %1 = mhlo.constant dense<3.000000e+00> : tensor<1x24x1x4xf32>
        %2 = mhlo.add %arg0, %1 : tensor<1x24x1x4xf32>
        %3 = "mhlo.reduce"(%2, %0) ( {
        ^bb0(%arg1: tensor<f32>, %arg2: tensor<f32>):  // no predecessors
          %4 = mhlo.add %arg1, %arg2 : tensor<f32>
          "mhlo.return"(%4) : (tensor<f32>) -> ()
        }) {dimensions = dense<[1, 2]> : tensor<2xi64>} : (tensor<1x24x1x4xf32>, tensor<f32>) -> tensor<1x4xf32>
        return %3 : tensor<1x4xf32>
      }
    }

    Run build-tracy/iree/tools/iree-run-mlir b.mlir --iree-hal-target-backends=vulkan-spirv --function-input=1x24x1x4xf32=0

    You will get [72 72 72 72]

    Run iree-run-mlir b.mlir --iree-hal-target-backends=vulkan-spirv --function-input=1x24x1x4xf32=0 -iree-flow-dispatch-linalg-on-tensors=true -iree-codegen-spirv-experimental-linalg-on-tensors=true

    You will get [288 288 288 288].

    keep looking...

  4. hanhanW commented on Apr 7, 2021

    @hanhanW
    ContributorAuthor

    I probably was wrong on using generic ops that have reduction loop. Let me rethink a bit on it... #4219

  5. hanhanW commented on Apr 7, 2021

    @hanhanW
    ContributorAuthor

    The issue is in LinalgFoldUnitExtentDims.

    The below snippet is a bit messy, but I couldn't have a better example now.

    Input:

    func @call(%arg0: tensor<1x24x1x4xf32>, %arg1: tensor<1x24x1x4xf32>) -> tensor<1x4xf32> {
      %cst = constant dense<0.000000e+00> : tensor<f32>
      %0 = linalg.init_tensor [1, 24, 1, 4] : tensor<1x24x1x4xf32>
      %1 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", "parallel", "parallel", "parallel"]}
        ins(%arg0, %arg1 : tensor<1x24x1x4xf32>, tensor<1x24x1x4xf32>)
        outs(%0 : tensor<1x24x1x4xf32>) {
      ^bb0(%arg2: f32, %arg3: f32, %arg4: f32):  // no predecessors
        %5 = addf %arg2, %arg3 : f32
        linalg.yield %5 : f32
      } -> tensor<1x24x1x4xf32>
      %cst_0 = constant 0.000000e+00 : f32
      %2 = linalg.init_tensor [1, 4] : tensor<1x4xf32>
      %3 = linalg.fill(%2, %cst_0) : tensor<1x4xf32>, f32 -> tensor<1x4xf32>
      %4 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d2, d3, d1)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1)>],
        iterator_types = ["parallel", "parallel", "reduction", "reduction"]}
        ins(%1 : tensor<1x24x1x4xf32>)
        outs(%3 : tensor<1x4xf32>) {
      ^bb0(%arg2: f32, %arg3: f32):  // no predecessors
        %5 = addf %arg2, %arg3 : f32
        linalg.yield %5 : f32
      } -> tensor<1x4xf32>
      return %4 : tensor<1x4xf32>
    }

    After -linalg-fold-unit-extent-dims

    #map0 = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2)>
    #map1 = affine_map<(d0, d1, d2, d3) -> (d3)>
    #map2 = affine_map<(d0, d1) -> (d0, d1)>
    #map3 = affine_map<(d0, d1, d2) -> (d2, d1)>
    #map4 = affine_map<(d0, d1, d2) -> (d0)>
    module  {
      func @call(%arg0: tensor<1x24x1x4xf32>, %arg1: tensor<1x24x1x4xf32>) -> tensor<1x4xf32> {
        %cst = constant 0.000000e+00 : f32
        %0 = linalg.init_tensor [1, 24, 1, 4] : tensor<1x24x1x4xf32>
        %1 = linalg.tensor_reshape %arg0 [#map0, #map1] : tensor<1x24x1x4xf32> into tensor<24x4xf32>
        %2 = linalg.tensor_reshape %arg1 [#map0, #map1] : tensor<1x24x1x4xf32> into tensor<24x4xf32>
        %3 = linalg.tensor_reshape %0 [#map0, #map1] : tensor<1x24x1x4xf32> into tensor<24x4xf32>
        %4 = linalg.generic {indexing_maps = [#map2, #map2, #map2], iterator_types = ["parallel", "parallel"]} ins(%1, %2 : tensor<24x4xf32
    >, tensor<24x4xf32>) outs(%3 : tensor<24x4xf32>) {
        ^bb0(%arg2: f32, %arg3: f32, %arg4: f32):  // no predecessors
          %9 = addf %arg2, %arg3 : f32
          linalg.yield %9 : f32
        } -> tensor<24x4xf32>
        %5 = linalg.init_tensor [4] : tensor<4xf32>
        %6 = linalg.fill(%5, %cst) : tensor<4xf32>, f32 -> tensor<4xf32>
        %7 = linalg.generic {indexing_maps = [#map3, #map4], iterator_types = ["parallel", "reduction", "reduction"]} ins(%4 : tensor<24x4x
    f32>) outs(%6 : tensor<4xf32>) {
        ^bb0(%arg2: f32, %arg3: f32):  // no predecessors
          %9 = addf %arg2, %arg3 : f32
          linalg.yield %9 : f32
        } -> tensor<4xf32>
        %8 = linalg.tensor_reshape %7 [#map2] : tensor<4xf32> into tensor<1x4xf32>
        return %8 : tensor<1x4xf32>
      }
    }

    One reduction loop in the second generic op should be removed because:

    In the beginning d0 -> 1, d1 -> 4, d2 -> 24, d3 -> 1, which means reduction loops are d2 and d3. Now there is no unit dim, so there should be one reduction loop.

    Actually if you change the input to

    func @call(%1: tensor<1x24x1x4xf32>, %arg1: tensor<1x24x1x4xf32>) -> tensor<1x4xf32> {
      %cst_0 = constant 0.000000e+00 : f32
      %2 = linalg.init_tensor [1, 4] : tensor<1x4xf32>
      %3 = linalg.fill(%2, %cst_0) : tensor<1x4xf32>, f32 -> tensor<1x4xf32>
      %4 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d2, d3, d1)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1)>],
        iterator_types = ["parallel", "parallel", "reduction", "reduction"]}
        ins(%1 : tensor<1x24x1x4xf32>)
        outs(%3 : tensor<1x4xf32>) {
      ^bb0(%arg2: f32, %arg3: f32):  // no predecessors
        %5 = addf %arg2, %arg3 : f32
        linalg.yield %5 : f32
      } -> tensor<1x4xf32>
      return %4 : tensor<1x4xf32>
    }

    You will get the right IR:

    #map0 = affine_map<(d0, d1, d2, d3) -> (d0, d1)>
    #map1 = affine_map<(d0, d1, d2, d3) -> (d2)>
    #map2 = affine_map<(d0, d1, d2, d3) -> (d3)>
    #map3 = affine_map<(d0, d1, d2) -> (d1, d2, d0)>
    #map4 = affine_map<(d0, d1, d2) -> (d0)>
    #map5 = affine_map<(d0, d1) -> (d0, d1)>
    module  {
      func @call(%arg0: tensor<1x24x1x4xf32>, %arg1: tensor<1x24x1x4xf32>) -> tensor<1x4xf32> {
        %cst = constant 0.000000e+00 : f32
        %0 = linalg.tensor_reshape %arg0 [#map0, #map1, #map2] : tensor<1x24x1x4xf32> into tensor<24x1x4xf32>
        %1 = linalg.init_tensor [4] : tensor<4xf32>
        %2 = linalg.fill(%1, %cst) : tensor<4xf32>, f32 -> tensor<4xf32>
        %3 = linalg.generic {indexing_maps = [#map3, #map4], iterator_types = ["parallel", "reduction", "reduction"]} ins(%0 : tensor<24x1x4xf32>) outs(%2 : tensor<4xf32>) {
        ^bb0(%arg2: f32, %arg3: f32):  // no predecessors
          %5 = addf %arg2, %arg3 : f32
          linalg.yield %5 : f32
        } -> tensor<4xf32>
        %4 = linalg.tensor_reshape %3 [#map5] : tensor<4xf32> into tensor<1x4xf32>
        return %4 : tensor<1x4xf32>
      }
    }

    One of the unit dim is not deleted, so the iterator_types and indexing maps match the behavior.

  6. hanhanW commented on Apr 7, 2021

    @hanhanW
    ContributorAuthor
  7. hanhanW commented on Apr 7, 2021

    @hanhanW
    ContributorAuthor

    I have a quick fix for small test case, let me check if it applies to all cases.

  8. hanhanW commented on Apr 7, 2021

    @hanhanW
    ContributorAuthor

    It fixes all the numerical issues under integrations/tensorflow/e2e/

    Let me clean it up, add some tests and send out for review...

  9. MaheshRavishankar commented on Apr 7, 2021

    @MaheshRavishankar
    Collaborator

    Awesome! Thanks for looking into this. Havent studied your IR example, but will do so in a bit.

  10. MaheshRavishankar commented on Apr 7, 2021

    @MaheshRavishankar
    Collaborator

    I think there is a subtle issue here (and I see why your fix works). Take this reduction op

      %4 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d2, d3, d1)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1)>],
        iterator_types = ["parallel", "parallel", "reduction", "reduction"]}
        ins(%1 : tensor<1x24x1x4xf32>)
        outs(%3 : tensor<1x4xf32>) {
      ^bb0(%arg2: f32, %arg3: f32):  // no predecessors
        %5 = addf %arg2, %arg3 : f32
        linalg.yield %5 : f32
      } -> tensor<1x4xf32>
    

    There are two unit-extent dims d0 and d2. It is fine to drop d0, but dropping d2 is legal only if there is another reduction loop after the current reduction loop. I think we just got lucky with our e2e tests.
    If you can integrate this into your patch, that would be great. If not, I will fix that upstream after your patch.

  11. hanhanW commented on Apr 7, 2021

    @hanhanW
    ContributorAuthor

    No, we are not lucky, it's broken in unidirectional_lstm model in element-wise fusion (and only on dbg mode...) I feel we probably want to revert https://reviews.llvm.org/D99115 from @antiagainst ...

  12. MaheshRavishankar commented on Apr 7, 2021

    @MaheshRavishankar
    Collaborator

    I wonder why only debug mode. Thats weird.

    It shouldnt be too hard to fix that though. When you drop unit-dims need to check the iterator type as an extra check. You said you have a fix in progress. If you can adapt it, then thats good. If not I can fix it upstream.

    If there are any failing tests now, then we need to see if those can be disabled temporarily.

  13. antiagainst commented on Apr 7, 2021

    @antiagainst
    Member

    Oh, did I opened the pandora box? Sorry I wasn't super clear about the full logic there; just throwed in my case as a test to the modified code and it works fine. :D

    Reverting https://reviews.llvm.org/D99115 will cause a series of fallouts. It is used by the consumer elementwise op fusion in MobileNet. Better to fix it properly, given that Mahesh said it shouldn't be hard. We need to pay the waiting time of llvm integration anyway.

  14. MaheshRavishankar commented on Apr 8, 2021

    @MaheshRavishankar
    Collaborator
  15. hanhanW commented on Apr 9, 2021

    @hanhanW
    ContributorAuthor

    Verified that this is fixed in llvm/llvm-project@f4eb681

    It is passing on both CPU and GPU. (seems that there is an issue on SwiftShader, but it could be another issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐞codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions