Skip to content

Add support for lowering mhlo.fft in codegen #5299

Closed

Activity

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

    @hanhanW
    ContributorAuthor

    I will put more context later, because it should go through some patterns in HLOToHLOPreprocessPass, like PopulateComplexLoweringPatterns first.

  4. hanhanW commented on Apr 13, 2021

    @hanhanW
    ContributorAuthor

    After prototyping on this, I found that there is a slight numerical issue. I verified in many ways, and ended up with the implementation of sin/cos is different between our backends and g++/clang++. Although it is only like 1e-6 accuracy, the cumulative sum of matmul will increase errors to around 1e-4.

    I wrote a naive c++ code to compare results: https://gist.github.com/hanhanW/1a3d2bcf060eb96d3d698f95ec9aa115

    • func1: based on the DFT definition
    • func2: convert to matmul form
    • func3: use constant coefficients (which are generated from our backends) to compute.

    Log: https://gist.githubusercontent.com/hanhanW/1a3d2bcf060eb96d3d698f95ec9aa115/raw/19f795c1dbc30b9f04103ce36b7ff02d8f0d06e1/log

    You can see that the second number of real part is different. One is -0.000000, and another is -0.000010.

    If you dig into more, take the computed numbers as input to sin, it will generate

    std::vector<double> a = {-0.000000,-4.712389,-9.424778,-14.137167,-18.849556,-23.561945,-28.274334,-32.986723,-37.699112,-42.411501,-47.123890,-51.836279,-56.548668,-61.261057,-65.973446,-70.685835,-75.398224};
      for (auto i : a) printf("%.10lf ", sin(i));
      puts("");
    
    // Output: -0.0000000000 1.0000000000 0.0000000392 -1.0000000000 -0.0000000785 1.0000000000 0.0000001177 -1.0000000000 -0.0000001569 1.0000000000 0.0000001962 -1.0000000000 -0.0000002354 1.0000000000 0.0000002746 -1.0000000000 -0.0000003138

    But if it is computed through for-loops which have constant boundaries, it will generate 0 and 1:

    -0.0000000000,1.0000000000,-0.0000000000,-1.0000000000,0.0000000000,1.0000000000,-0.0000000000,-1.0000000000,0.0000000000,1.0000000000,-0.0000000000,-1.0000000000,0.0000000000,1.0000000000,0.0000000000,-1.0000000000,0.0000000000
    

    The former one matches the result from all of out backends.

    Tested with

    func @tensor() -> tensor<17xf32> attributes { iree.module.export } {
      %input = iree.unfoldable_constant dense<[-0.000000,-4.712389,-9.424778,-14.137167,-18.849556,-23.561945,-28.274334,-32.986723,-37.699112,-42.411501,-47.123890,-51.836279,-56.548668,-61.261057,-65.973446,-70.685835,-75.398224]> : tensor<17xf32>
      %result = "mhlo.cosine"(%input) : (tensor<17xf32>) -> tensor<17xf32>
      return %result : tensor<17xf32>
    }

    Output from VMLA, and vulkan:

    // VMLA:
    17xf32=1 1.19249E-08 -1 -3.57746E-08 1 5.96244E-08 -1 -1.99082E-06 1 2.01467E-06 -1 -2.03852E-06 1 2.06237E-06 -1 -2.08622E-06 1
    // vulkan:
    17xf32=1 1.19249E-08 -1 -3.57746E-08 1 5.96244E-08 -1 -1.99082E-06 1 2.01467E-06 -1 -2.03852E-06 1 2.06237E-06 -1 -2.08622E-06 1
    

    I highly suspect that the numerical difference in fft between VMLA and my prototype is coming from this kind of "issue". I think I'm doing correct things in the prototype, and I'm going to adjust the golden result based on what I have, and ignore VMLA one.

    CC @rsuderman @NatashaKnk who worked on FFT support in VMLA.

  5. benvanik commented on Apr 13, 2021

    @benvanik
    Collaborator

    SGTM - the VMLA one is likely to be specific quirks of the pffffffffft library and may not match source tensorflow/etc.

  6. benvanik commented on Apr 13, 2021

    @benvanik
    Collaborator

    (@rsuderman / @NatashaKnk may know how closely pfffffft matched tf, though)

  7. hanhanW commented on Apr 13, 2021

    @hanhanW
    ContributorAuthor

    1e-4 looks like a reasonable tolerance for fft to me. This test is small, we probably need to verify with larger tests.

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

Metadata

Metadata

Assignees

Labels

codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions