Skip to content

Quant model conv1x1 uses fallback codegen flow #8149

Closed

Description

@hcindyl

conv1x1 is changed to FC in IREE codegen with the naming match in TOSA and IREE.

The quantized models use a different set of conv named ops (with _q suffix), and are not remapped to a more efficient FC codegen flow.

Mahesh suggested TOSA level mapping update should work, so that's the first step of the improvement.

Activity

  1. added
    bug 🐞
    integrations/tosa
    on Jan 20, 2022
  2. rsuderman commented on Jan 21, 2022

    @rsuderman
    Collaborator

    Can you clarify the difference? The _q prefix means it is executing the quantized variant of the operation. We do not execute the other variant as it is the floating point version.

  3. hcindyl commented on Jan 21, 2022

    @hcindyl
    ContributorAuthor

    Summary of the email discussion:

    Currently for the quant conv1x1 op, it is treated as just another convolution, which is yet to have a good codegen flow; however, it should be treated as matmul, same as in the floating point version, which IREE already has a better codegen path in place.

    With quantized variant not being mapped to the naming matching functions, the codegen uses a fallback lowing pass, which generates a lot of repetition/inefficient inner loop ops. For example, following is the current codegen behavior for the conv1x1 in mobilenet_v1_0.25_quantized model

    ########################################################################
    # OUTER LOOP {                   224                            340db9ec
    ########################################################################
        c6c4:       00000993                li      s3,0
        c6c8:       70000513                li      a0,1792
        c6cc:       02a38fb3                mul     t6,t2,a0
        c6d0:       00028593                mv      a1,t0
    ########################################################################
    #   MIDDLE LOOP {              3,136                            340db9fc
    ########################################################################
        c6d4:       00000d13                li      s10,0
        c6d8:       00599513                slli    a0,s3,0x5
        c6dc:       01f50533                add     a0,a0,t6
        c6e0:       01750ab3                add     s5,a0,s7
        c6e4:       000a0c93                mv      s9,s4
    ########################################################################
    #     INNER LOOP {           100,352                            340dba10
    ########################################################################
        c6e8:       01aa8533                add     a0,s5,s10
        c6ec:       00251513                slli    a0,a0,0x2
        c6f0:       00a886b3                add     a3,a7,a0
        c6f4:       0006a783                lw      a5,0(a3)
        c6f8:       00000493                li      s1,0
        c6fc:       000c8513                mv      a0,s9
    ########################################################################
    #       INNER-MOST LOOP {  3,211,264  (#1 basic block hotspot)  340dba28
    ########################################################################
        c700:       00958733                add     a4,a1,s1
        c704:       00070703                lb      a4,0(a4)
        c708:       00050603                lb      a2,0(a0)
        c70c:       08070713                addi    a4,a4,128
        c710:       01760613                addi    a2,a2,23
        c714:       02e60633                mul     a2,a2,a4
        c718:       00f607b3                add     a5,a2,a5
        c71c:       00f6a023                sw      a5,0(a3)
        c720:       00148493                addi    s1,s1,1
        c724:       02050513                addi    a0,a0,32
        c728:       fde49ce3                bne     s1,t5,c700 <iree_hal_executable_library_query-0x1b594>
    ########################################################################
    #       } INNER-MOST LOOP
    ########################################################################
        c72c:       001d0d13                addi    s10,s10,1
        c730:       001c8c93                addi    s9,s9,1
        c734:       fa6d1ae3                bne     s10,t1,c6e8 <iree_hal_executable_library_query-0x1b5ac>
    ########################################################################
    #     } INNER LOOP
    ########################################################################
        c738:       00198993                addi    s3,s3,1
        c73c:       02058593                addi    a1,a1,32
        c740:       f9099ae3                bne     s3,a6,c6d4 <iree_hal_executable_library_query-0x1b5c0>
    ########################################################################
    #   } MIDDLE LOOP
    ########################################################################
        c744:       00138393                addi    t2,t2,1 # c0000001 <_DYNAMIC+0xbffd7221>
        c748:       70028293                addi    t0,t0,1792
        c74c:       f7d39ce3                bne     t2,t4,c6c4 <iree_hal_executable_library_query-0x1b5d0>
    ########################################################################
    # } OUTER LOOP
    ########################################################################

    There are unfortunately unnecessary memory updates in c71c for the partial accumulation result. @MaheshRavishankar suggested this is a signal that the op is lowered with in the fallback path.

  4. MaheshRavishankar commented on Jan 21, 2022

    @MaheshRavishankar
    Collaborator

    I think the conclusion was that even the quantized conv 1x1 ops should be converted to matmuls. Better codegen for convolution is being worked on, but for this case there is no reason to wait for that.

  5. linked a pull request that will close this issueUse default TosaToLinalg passes #8161on Jan 21, 2022
  6. rsuderman commented on Jan 22, 2022

    @rsuderman
    Collaborator

    Reopened until we can validate

  7. rsuderman commented on Jan 27, 2022

    @rsuderman
    Collaborator

    Verified it should be at head. If there is a memory regression keep me up to date.

  8. hcindyl commented on Feb 4, 2022

    @hcindyl
    ContributorAuthor

    Use candidate-20220128.31 to build a very simple example of mlir as following

    module {
       func @main(%arg0: tensor<1x112x112x8xi8>) -> tensor<1x112x112x16xi32> {
        %0 = "tosa.const"() {value = dense<[-918, -4433, 87, -234, -21393, 7738, 529, -8835, -16817, -375, -199, 572, 5082, 15569, -186, 4955]> : tensor<16xi32>} : () -> tensor<16xi32>
        %1 = "tosa.const"() {value = dense<"0xE2E2E2E2E2EBF6E206E2E2E8EBE6DEE2E0E2E2FDDCE7E1E2E2E2E2FDDFE6E0E3EBE2E2DCEFAC213DE2E2E2E3E2D3C9E4E2E2E2E2E3E1E1FA3EE2E2E9E9E5E3E47FE2E2E4EDE2E2E3E0E2E2E4DFE9DCFEE3E2E2E2E2E2E2E2F9E2E2E6EBEADCE4E2E2E2CEE5E1E0E181E2E2D9D3DEE1DDE2E2E2E2E2E2E2E2C5E2E2E2DBE1E3E1"> : tensor<16x1x1x8xi8>} : () -> tensor<16x1x1x8xi8>
        %2 = "tosa.conv2d"(%arg0, %1, %0) {dilation = [1, 1], pad = [0, 0, 0, 0], quantization_info = {input_zp = -128 : i32, weight_zp = -30 : i32}, stride = [1, 1]} : (tensor<1x112x112x8xi8>, tensor<16x1x1x8xi8>, tensor<16xi32>) -> tensor<1x112x112x16xi32>
        return %2 : tensor<1x112x112x16xi32>
      }
    }
    

    Enable --print-ir-after-all during iree-translate, I can see tosa.conv2d converted to linalg.quantized_matmul, but don't see IR change after the ConvertConv2D1x1ConvToMatmul pass. I am not sure if quantized_matmul is vectorized efficiently in this case.

    // -----// IR Dump After Symb
    [build_conv1x1.txt](https://github.com/google/iree/files/7999048/build_conv1x1.txt)
    olDCE //----- //
    #map0 = affine_map<(d0, d1) -> (d1)>
    #map1 = affine_map<(d0, d1) -> (d0, d1)>
    module {
      func @main(%arg0: !hal.buffer_view) -> !hal.buffer_view attributes {iree.abi.stub} {
        %c-30_i32 = arith.constant -30 : i32
        %c-128_i32 = arith.constant -128 : i32
        %cst = arith.constant dense<"0xE206E0E2EBE2E23E7FE0E3F9E281E2C5E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E8FDFDDCE3E2E9E4E4E2E6CED9E2E2E2EBDCDFEFE2E3E9EDDFE2EBE5D3E2DBEBE6E7E6ACD3E1E5
    E2E9E2EAE1DEE2E1F6DEE1E021C9E1E3E2DCE2DCE0E1E2E3E2E2E2E33DE4FAE4E3FEE2E4E1DDE2E1"> : tensor<8x16xi8>
        %c0_i32 = arith.constant 0 : i32
        %cst_0 = arith.constant dense<[-918, -4433, 87, -234, -21393, 7738, 529, -8835, -16817, -375, -199, 572, 5082, 15569, -186, 4955]> : tensor<16xi32>
        %0 = hal.tensor.import %arg0 : !hal.buffer_view -> tensor<1x112x112x8xi8>
        %1 = tensor.collapse_shape %0 [[0, 1, 2], [3]] : tensor<1x112x112x8xi8> into tensor<12544x8xi8>
        %2 = linalg.init_tensor [12544, 16] : tensor<12544x16xi32>
        %3 = linalg.fill(%c0_i32, %2) : i32, tensor<12544x16xi32> -> tensor<12544x16xi32> 
        %4 = linalg.quantized_matmul ins(%1, %cst, %c-128_i32, %c-30_i32 : tensor<12544x8xi8>, tensor<8x16xi8>, i32, i32) outs(%3 : tensor<12544x16xi32>) -> tensor<12544x16xi32>
        %5 = linalg.generic {indexing_maps = [#map0, #map1, #map1], iterator_types = ["parallel", "parallel"]} ins(%cst_0, %4 : tensor<16xi32>, tensor<12544x16xi32>) outs(%2 : tensor<12544x16xi32>) {
        ^bb0(%arg1: i32, %arg2: i32, %arg3: i32):
          %8 = arith.addi %arg1, %arg2 : i32
          linalg.yield %8 : i32
        } -> tensor<12544x16xi32>
        %6 = tensor.expand_shape %5 [[0, 1, 2], [3]] : tensor<12544x16xi32> into tensor<1x112x112x16xi32>
        %7 = hal.tensor.export %6 : tensor<1x112x112x16xi32> -> !hal.buffer_view
        return %7 : !hal.buffer_view
      }
    }
    
    
    // -----// IR Dump After ConvertConv2D1x1ConvToMatmul //----- //
    func @main(%arg0: !hal.buffer_view) -> !hal.buffer_view attributes {iree.abi.stub} {
      %c-30_i32 = arith.constant -30 : i32
      %c-128_i32 = arith.constant -128 : i32
      %cst = arith.constant dense<"0xE206E0E2EBE2E23E7FE0E3F9E281E2C5E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E8FDFDDCE3E2E9E4E4E2E6CED9E2E2E2EBDCDFEFE2E3E9EDDFE2EBE5D3E2DBEBE6E7E6ACD3E1E5E2E9E2EAE1DEE2E1F6DEE1E021C9E1E3E2DCE2DCE0E1E2E3E2E2E2E33DE4FAE4E3FEE2E4E1DDE2E1"> : tensor<8x16xi8>
      %c0_i32 = arith.constant 0 : i32
      %cst_0 = arith.constant dense<[-918, -4433, 87, -234, -21393, 7738, 529, -8835, -16817, -375, -199, 572, 5082, 15569, -186, 4955]> : tensor<16xi32>
      %0 = hal.tensor.import %arg0 : !hal.buffer_view -> tensor<1x112x112x8xi8>
      %1 = tensor.collapse_shape %0 [[0, 1, 2], [3]] : tensor<1x112x112x8xi8> into tensor<12544x8xi8>
      %2 = linalg.init_tensor [12544, 16] : tensor<12544x16xi32>
      %3 = linalg.fill(%c0_i32, %2) : i32, tensor<12544x16xi32> -> tensor<12544x16xi32> 
      %4 = linalg.quantized_matmul ins(%1, %cst, %c-128_i32, %c-30_i32 : tensor<12544x8xi8>, tensor<8x16xi8>, i32, i32) outs(%3 : tensor<12544x16xi32>) -> tensor<12544x16xi32>
      %5 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%cst_0, %4 : tensor<16xi32>, tensor<12544x16xi32>) outs(%2 : tensor<12544x16xi32>) {
      ^bb0(%arg1: i32, %arg2: i32, %arg3: i32):
        %8 = arith.addi %arg1, %arg2 : i32
        linalg.yield %8 : i32
      } -> tensor<12544x16xi32>
      %6 = tensor.expand_shape %5 [[0, 1, 2], [3]] : tensor<12544x16xi32> into tensor<1x112x112x16xi32>
      %7 = hal.tensor.export %6 : tensor<1x112x112x16xi32> -> !hal.buffer_view
      return %7 : !hal.buffer_view
    }
    

    build_conv1x1.txt.tar.gz

  9. MaheshRavishankar commented on Feb 4, 2022

    @MaheshRavishankar
    Collaborator

    No it is not. The quantized_matmul needs to have the contraction operation interface for it to get picked up for vectorization.

  10. 2 remaining items

  11. MaheshRavishankar commented on Feb 4, 2022

    @MaheshRavishankar
    Collaborator

    @rsuderman this is the change to add the contraction op interface upstream.

    --- a/mlir/python/mlir/dialects/linalg/opdsl/ops/core_named_ops.py
    +++ b/mlir/python/mlir/dialects/linalg/opdsl/ops/core_named_ops.py
    @@ -52,6 +52,7 @@ def quantized_matmul(
       matmul.
       """
       domain(D.m, D.n, D.k)
    +  implements(ContractionOpInterface)
       C[D.m, D.n] += (TypeFn.cast(U, A[D.m, D.k]) - TypeFn.cast(U, AZp)) * (
           TypeFn.cast(U, B[D.k, D.n]) - TypeFn.cast(U, BZp))
    
    

    I dont have the python stuff built to try this out easily. But if you think you can do that, this should get vectorized, but will have to see the IR to make sure.

  12. hcindyl commented on Feb 4, 2022

    @hcindyl
    ContributorAuthor

    I am not sure I understand why the change would be in python. The IR dump I provided above was from running iree-translate CLI with the .mlir as the text file input. At which point would python code being used?

  13. MaheshRavishankar commented on Feb 4, 2022

    @MaheshRavishankar
    Collaborator

    That is the op definition of the linalg.quantized_matmul operation. That needs to define the ContractionOpInterface for it to get picked up by the vectorization path.

  14. bjacob commented on Feb 15, 2022

    @bjacob
    Collaborator

    Filed Issue #8330 for quantized_matmul to matmul lowering pattern.

  15. removed their assignment
    on Jun 7, 2022
  16. allieculp commented on Jul 13, 2022

    @allieculp

    @not-jenni Can you help review and prioritize this on frontend rotation this week?

  17. rsuderman commented on Jul 14, 2022

    @rsuderman
    Collaborator

    #8330 is now merged which fixes this issue.

  18. moved this to Done in IREEon Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐞integrations/tosa

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions