Skip to content

Commit

Permalink
[Midend] Add RFFT op in Extend DAP Pass (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrokenArrow1404 authored Oct 22, 2024
1 parent 5d9d52b commit 2b2a8df
Show file tree
Hide file tree
Showing 6 changed files with 2,715 additions and 344 deletions.
7 changes: 7 additions & 0 deletions examples/DAPDialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ target_link_libraries(buddy-whisper-preprocess
BuddyLibDAP
mlir_c_runner_utils
)

add_executable(buddy-rfft RFFT.cpp)
add_dependencies(buddy-rfft buddy-opt)
target_link_libraries(buddy-rfft
BuddyLibDAP
mlir_c_runner_utils
)
75 changes: 75 additions & 0 deletions examples/DAPDialect/RFFT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===- RFFT.cpp - Example of DAP RFFT Operation ---------------------------===//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// An example of the RFFT function from Whisper Preprocessor operation.
//
//===----------------------------------------------------------------------===//

#include <buddy/DAP/DAP.h>
#include <chrono>
#include <fstream>
#include <iostream>

#define testLength 840

using namespace dap;
using namespace std;

// Print [Log] label in bold blue format.
void printLogLabel() { std::cout << "\033[34;1m[Log] \033[0m"; }

// Write preprocessing results to a text file.
void printResult(MemRef<double, 1> &outputMemRef) {
ofstream fout("whisperPreprocessResultRFFT.txt");
// Print title.
fout << "-----------------------------------------" << std::endl;
fout << "[ Buddy RFFT Result ]" << std::endl;
fout << "-----------------------------------------" << std::endl;
// Print reuslt data.
for (int i = 0; i < testLength; ++i) {
fout << outputMemRef[i] << std::endl;
}
fout.close();
}

int main() {
// Print the title of this example.
const std::string title = "RFFT Operation Powered by Buddy Compiler";
std::cout << "\033[33;1m" << title << "\033[0m" << std::endl;

double *inputAlign = new double[testLength];
for (int i = 0; i < testLength; ++i) {
inputAlign[i] = static_cast<double>(i);
}
intptr_t inputSizes[1] = {testLength};
MemRef<double, 1> inputMemRef(inputAlign, inputSizes);

printLogLabel();
std::cout << "Running RFFT operation" << std::endl;
const auto loadStart = std::chrono::high_resolution_clock::now();
dap::RFFT(&inputMemRef);
const auto loadEnd = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double, std::milli> loadTime =
loadEnd - loadStart;
printLogLabel();
std::cout << "RFFT time: " << (double)(loadTime.count()) / 1000
<< "s\n"
<< std::endl;

printResult(inputMemRef);

return 0;
}
7 changes: 7 additions & 0 deletions frontend/Interfaces/buddy/DAP/DSP/WhisperPreprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ extern "C" {
// first operand.
void _mlir_ciface_buddy_whisperPreprocess(MemRef<float, 3> *outputFeatures,
MemRef<double, 1> *inputRawSpeech);

void _mlir_ciface_buddy_RFFT(MemRef<double, 1> *inputRawSpeech);

}
} // namespace detail

Expand All @@ -49,6 +52,10 @@ void whisperPreprocess(MemRef<double, 1> *inputRawSpeech,
detail::_mlir_ciface_buddy_whisperPreprocess(outputFeatures, inputRawSpeech);
}


void RFFT(MemRef<double, 1> *inputRawSpeech) {
detail::_mlir_ciface_buddy_RFFT(inputRawSpeech);
}
} // namespace dap

#endif // FRONTEND_INTERFACES_BUDDY_DAP_DSP_WHISPERPREPROCESS
4 changes: 4 additions & 0 deletions frontend/Interfaces/lib/DAP-extend.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ func.func @buddy_whisperPreprocess(%in : memref<?xf64>) -> memref<1x80x3000xf32>
%out = dap.whisper_preprocess %in : memref<?xf64> to memref<1x80x3000xf32>
return %out : memref<1x80x3000xf32>
}
func.func @buddy_RFFT(%in : memref<?xf64>) -> () {
dap.rfft %in : memref<?xf64>
return
}
6 changes: 3 additions & 3 deletions midend/include/Dialect/DAP/DAPOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def DAP_IirOp : DAP_Op<"iir"> {
}];
}

def DAP_RFFT400Op : DAP_Op<"rfft400"> {
let summary = "RFFT operation for length 400.";
def DAP_RFFTOp : DAP_Op<"rfft"> {
let summary = "RFFT operation.";
let description = [{
The RFFT algorithm is designed to handle real-valued input signals. Real
signals exhibit conjugate symmetry in the frequency domain, meaning that
Expand All @@ -105,7 +105,7 @@ def DAP_RFFT400Op : DAP_Op<"rfft400"> {
Example:

```mlir
dap.rfft400 %data : memref<400xf64>
dap.rfft %data : memref<?xf64>
```
}];

Expand Down
Loading

0 comments on commit 2b2a8df

Please sign in to comment.