-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added mlir->LLVMIR conversion for tanh op using LUT #1718
Open
aidansander
wants to merge
5
commits into
Xilinx:main
Choose a base branch
from
aidansander:tanh-lut
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9bae491
Added getTanhBf16 aievec unit test
62abc98
Added mlir->llvm pass for math.tanh
e83b9b2
Updating helper name, removing references to inv LLVM pass
4b97dc8
Moved match to helper and renamed test case
a974856
Adding conversion test and appropriate requires to llvm test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// RUN: aie-opt %s --convert-vector-to-aievec="aie-target=aie2 target-backend=llvmir" | FileCheck %s | ||
|
||
// CHECK-LABEL: func private @getTanhBf16(vector<16xbf16>) -> vector<16xbf16> | ||
// CHECK-LABEL: func @test_tanh_lut | ||
// CHECK-SAME: %[[A:[A-Za-z0-9]+]]: vector<16xbf16> | ||
module{ | ||
func.func @test_tanh_lut(%a: vector<16xbf16>) -> vector<16xbf16> { | ||
// CHECK: %[[CALL:.*]] = call @getTanhBf16(%[[A]]) : (vector<16xbf16>) -> vector<16xbf16> | ||
%0 = math.tanh %a : vector<16xbf16> | ||
// CHECK: return %[[CALL]] : vector<16xbf16> | ||
return %0 : vector<16xbf16> | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test/unit_tests/aievec_tests/bf16_tanh_lut/bf16_tanh_lut-llvm.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// Copyright (C) 2023, Advanced Micro Devices, Inc. | ||
|
||
// REQUIRES: valid_xchess_license | ||
// REQUIRES: peano, peano_and_chess | ||
// RUN: mkdir -p %t/data; cd %t | ||
// RUN: aie-opt %s -affine-super-vectorize="virtual-vector-size=16" %vector-to-llvmir% -o llvmir.mlir | ||
// RUN: aie-translate --mlir-to-llvmir llvmir.mlir -o dut_part.ll | ||
// RUN: %PEANO_INSTALL_DIR/bin/clang -S -emit-llvm %clang_aie2_lib_args -I%aie_runtime_lib%/AIE2/ -c %S/dut_simple.cc -o lut_based_ops.ll | ||
// RUN: %PEANO_INSTALL_DIR/bin/clang -S -emit-llvm %clang_aie2_lib_args -c %aie_runtime_lib%/AIE2/lut_based_ops.cpp -o lut_constants.ll | ||
// RUN: llvm-link -S lut_based_ops.ll dut_part.ll -o dut_functions.ll | ||
// RUN: llvm-link -S lut_constants.ll dut_functions.ll -o dut.ll | ||
// RUN: %PEANO_INSTALL_DIR/bin/clang %clang_aie2_args -c dut.ll -o dut.o | ||
// RUN: xchesscc_wrapper aie2 -f -g +s +w work +o work -I%S -I%aie_runtime_lib%/AIE2 -I %aietools/include -DTO_LLVM -D__AIEARCH__=20 -D__AIENGINE__ -I. %S/testbench.cc dut.o | ||
// RUN: xca_udm_dbg --aiearch aie-ml -qf -T -P %aietools/data/aie_ml/lib/ -t "%S/../profiling.tcl ./work/a.out" >& xca_udm_dbg.stdout | ||
// RUN: FileCheck --input-file=./xca_udm_dbg.stdout %s | ||
// CHECK: TEST PASSED | ||
|
||
module { | ||
func.func @dut(%arg0: memref<1024xbf16>{llvm.noalias}, %arg1: memref<1024xbf16>{llvm.noalias}) { | ||
memref.assume_alignment %arg0, 32 : memref<1024xbf16> | ||
memref.assume_alignment %arg1, 32 : memref<1024xbf16> | ||
affine.for %arg3 = 0 to 1024 { | ||
%0 = affine.load %arg0[%arg3] : memref<1024xbf16> | ||
%1 = math.tanh %0 : bf16 | ||
affine.store %1, %arg1[%arg3] : memref<1024xbf16> | ||
} | ||
return | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
test/unit_tests/aievec_tests/bf16_tanh_lut/bf16_tanh_lut.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// Copyright (C) 2023, Advanced Micro Devices, Inc. | ||
|
||
// REQUIRES: valid_xchess_license | ||
// RUN: mkdir -p %t/data; cd %t | ||
// RUN: aie-opt %s -affine-super-vectorize="virtual-vector-size=16" --convert-vector-to-aievec="aie-target=aie2" -lower-affine | aie-translate -aie2=true --aievec-to-cpp -o dut.cc | ||
// RUN: xchesscc_wrapper aie2 -f -g +s +w work +o work -I%S -I%aie_runtime_lib%/AIE2 -I %aietools/include -D__AIEARCH__=20 -D__AIENGINE__ -I. -c %aie_runtime_lib%/AIE2/lut_based_ops.cpp -o lut_based_ops.o | ||
// RUN: xchesscc_wrapper aie2 -f -g +s +w work +o work -I%S -I%aie_runtime_lib%/AIE2 -I %aietools/include -D__AIEARCH__=20 -D__AIENGINE__ -I. -c dut.cc -o dut.o | ||
// RUN: xchesscc_wrapper aie2 -f -g +s +w work +o work -I%S -I%aie_runtime_lib%/AIE2 -I %aietools/include -D__AIEARCH__=20 -D__AIENGINE__ -I. %S/testbench.cc work/dut.o work/lut_based_ops.o | ||
// RUN: xca_udm_dbg --aiearch aie-ml -qf -T -P %aietools/data/aie_ml/lib/ -t "%S/../profiling.tcl ./work/a.out" >& xca_udm_dbg.stdout | ||
// RUN: FileCheck --input-file=./xca_udm_dbg.stdout %s | ||
// CHECK: TEST PASSED | ||
|
||
module { | ||
func.func @dut(%arg0: memref<1024xbf16>, %arg1: memref<1024xbf16>) { | ||
affine.for %arg3 = 0 to 1024 { | ||
%0 = affine.load %arg0[%arg3] : memref<1024xbf16> | ||
%1 = math.tanh %0 : bf16 | ||
affine.store %1, %arg1[%arg3] : memref<1024xbf16> | ||
} | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
constexpr unsigned const IN0_SIZE = 1024; | ||
constexpr unsigned const OUT0_SIZE = 1024; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "lut_based_ops.h" | ||
void dut(bfloat16 * restrict v1, bfloat16 * restrict v2) { | ||
size_t v3 = 0; | ||
size_t v4 = 1024; | ||
size_t v5 = 16; | ||
for (size_t v6 = v3; v6 < v4; v6 += v5) | ||
chess_prepare_for_pipelining | ||
chess_loop_range(64, 64) | ||
{ | ||
v16bfloat16 v7 = *(v16bfloat16 *)(v1 + v6); | ||
v16bfloat16 v8 = getTanhBf16(v7); | ||
*(v16bfloat16 *)(v2 + v6) = v8; | ||
} | ||
return; | ||
} | ||
|
||
|
||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit.: too many blank lines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "lut_based_ops.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "../common/testbench.h" | ||
#include "defines.h" | ||
#include <algorithm> | ||
#include <cstdint> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
#ifdef TO_LLVM | ||
extern "C" { | ||
#endif | ||
void dut(bfloat16 *restrict in0, bfloat16 *restrict out0); | ||
#ifdef TO_LLVM | ||
} | ||
#endif | ||
|
||
void dut_ref(bfloat16 *in0, bfloat16 *out0); | ||
|
||
alignas(32) bfloat16 g_in0[IN0_SIZE]; | ||
alignas(32) bfloat16 g_out0[OUT0_SIZE]; | ||
alignas(32) bfloat16 g_out0Ref[OUT0_SIZE]; | ||
|
||
int main(int argc, char *argv[]) { | ||
std::string dataDir(TO_STR(DATA_DIR)); | ||
srand(10); | ||
std::generate(g_in0, g_in0 + IN0_SIZE, | ||
[&]() { return random_bfloat16(-4, 1, 3); }); | ||
|
||
writeData(g_in0, IN0_SIZE, dataDir + "/in0.txt"); | ||
|
||
chess_memory_fence(); | ||
auto cyclesBegin = chess_cycle_count(); | ||
dut(g_in0, g_out0); | ||
auto cyclesEnd = chess_cycle_count(); | ||
chess_memory_fence(); | ||
|
||
auto cycleCount = (int)(cyclesEnd - cyclesBegin); | ||
reportCycleCount(cycleCount, dataDir + "/cycle_count.txt"); | ||
|
||
writeData(g_out0, OUT0_SIZE, dataDir + "/out0.txt"); | ||
|
||
dut_ref(g_in0, g_out0Ref); | ||
writeData(g_out0Ref, OUT0_SIZE, dataDir + "/out0_ref.txt"); | ||
|
||
bool ok = true; | ||
ok &= checkData(g_out0, g_out0Ref, OUT0_SIZE, 0, 1e-2, 1e-2); | ||
|
||
if (ok) | ||
printf("TEST PASSED\n"); | ||
else | ||
printf("TEST FAILED\n"); | ||
|
||
return ok ? 0 : 1; | ||
} | ||
|
||
void dut_ref(bfloat16 *in0, bfloat16 *out0) { | ||
for (unsigned k = 0; k < OUT0_SIZE; k += 1) { | ||
float in = in0[k]; | ||
//implements by tanh(x) = (e^(2x)-1)/(e^(2x)+1) | ||
//tanh is not in libcxx-lite | ||
float temp_var = expf(2*in); | ||
float out = (temp_var-1)/(temp_var+1); | ||
out0[k] = (bfloat16)out; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any benefit to having the alignment and size of these arrays declared here?