-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add forward support for PReLU (#14940)
### Ticket Link to Github Issue #8544 ### Problem description Currently PReLU is aliased to LeakyRelu which is not correct. It needs to be properly implemented in the eltwise operation. ### What's changed - Update the prelu implementation In composite structure. - It also support sfpu(Added in this PR) for certain cases which will handled soon in #14933 . ### Additional Information In Torch PReLU, the second input tensor can only have two valid shapes: either a tensor with a single value (1) or a tensor with a size equal to the number of input channels (default is 1). Currently, This implementation only supports cases where it matches the number of channels. Support for a single-value tensor requires additional handling at the low-level kernel. ### Checklist - [ ] [All Post commit CI ](https://github.com/tenstorrent/tt-metal/actions/runs/11796920085)
- Loading branch information
1 parent
019a5cc
commit 0ab59b7
Showing
25 changed files
with
376 additions
and
27 deletions.
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
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
39 changes: 39 additions & 0 deletions
39
tt_metal/hw/ckernels/blackhole/metal/llk_api/llk_sfpu/ckernel_sfpu_prelu.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,39 @@ | ||
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "ckernel.h" | ||
#include "ckernel_defs.h" | ||
#include "noc_nonblocking_api.h" | ||
#include "ckernel_sfpu_converter.h" | ||
|
||
|
||
using namespace sfpi; | ||
|
||
namespace ckernel { | ||
namespace sfpu { | ||
|
||
template <bool APPROXIMATION_MODE, int ITERATIONS = 8> | ||
inline void calculate_prelu(const uint value) { | ||
|
||
// SFPU microcode | ||
Converter c_value; | ||
c_value.u = value; | ||
vFloat init = c_value.f; | ||
|
||
#pragma GCC unroll 0 | ||
for (int d = 0; d < ITERATIONS; d++) | ||
{ | ||
vFloat a = dst_reg[0]; | ||
v_if(a < 0.0f) { | ||
a = a * init; | ||
} | ||
v_endif; | ||
dst_reg[0] = a; | ||
dst_reg++; | ||
} | ||
} | ||
} // namespace sfpu | ||
} // namespace ckernel |
29 changes: 29 additions & 0 deletions
29
tt_metal/hw/ckernels/blackhole/metal/llk_api/llk_sfpu/llk_math_eltwise_unary_sfpu_prelu.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,29 @@ | ||
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "ckernel_sfpu_prelu.h" | ||
#include "llk_math_eltwise_unary_sfpu_params.h" | ||
#include "llk_math_eltwise_unary_sfpu_init.h" | ||
|
||
namespace ckernel { | ||
|
||
// New LLK SFPU APIs | ||
|
||
template <bool APPROXIMATE> | ||
inline void llk_math_eltwise_unary_sfpu_prelu_init() { | ||
llk_math_eltwise_unary_sfpu_init<SfpuType::prelu, APPROXIMATE>(); | ||
} | ||
|
||
template <bool APPROXIMATE> | ||
inline void llk_math_eltwise_unary_sfpu_prelu(uint dst_index, uint param0, int vector_mode = (int)VectorMode::RC) { | ||
llk_math_eltwise_unary_sfpu_params<APPROXIMATE>( | ||
ckernel::sfpu::calculate_prelu<APPROXIMATE>, | ||
dst_index, | ||
vector_mode, | ||
param0); | ||
} | ||
|
||
} // namespace ckernel |
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 |
---|---|---|
|
@@ -87,5 +87,6 @@ enum SfpuType { | |
ceil, | ||
unused, | ||
cumsum, | ||
fill | ||
fill, | ||
prelu, | ||
}; |
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
39 changes: 39 additions & 0 deletions
39
tt_metal/hw/ckernels/wormhole_b0/metal/llk_api/llk_sfpu/ckernel_sfpu_prelu.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,39 @@ | ||
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "ckernel.h" | ||
#include "ckernel_defs.h" | ||
#include "noc_nonblocking_api.h" | ||
#include "ckernel_sfpu_converter.h" | ||
|
||
|
||
using namespace sfpi; | ||
|
||
namespace ckernel { | ||
namespace sfpu { | ||
|
||
|
||
template <bool APPROXIMATION_MODE, int ITERATIONS = 8> | ||
inline void calculate_prelu(uint value) { | ||
// SFPU microcode | ||
Converter c_value; | ||
c_value.u = value; | ||
vFloat init = c_value.f; | ||
|
||
#pragma GCC unroll 8 | ||
for (int d = 0; d < ITERATIONS; d++) | ||
{ | ||
vFloat a = dst_reg[0]; | ||
v_if(a < 0.0f) { | ||
a = a * init; | ||
} | ||
v_endif; | ||
dst_reg[0] = a; | ||
dst_reg++; | ||
} | ||
} | ||
} // namespace sfpu | ||
} // namespace ckernel |
29 changes: 29 additions & 0 deletions
29
tt_metal/hw/ckernels/wormhole_b0/metal/llk_api/llk_sfpu/llk_math_eltwise_unary_sfpu_prelu.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,29 @@ | ||
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "ckernel_sfpu_prelu.h" | ||
#include "llk_math_eltwise_unary_sfpu_params.h" | ||
#include "llk_math_eltwise_unary_sfpu_init.h" | ||
|
||
namespace ckernel { | ||
|
||
// New LLK SFPU APIs | ||
|
||
template <bool APPROXIMATE> | ||
inline void llk_math_eltwise_unary_sfpu_prelu_init() { | ||
llk_math_eltwise_unary_sfpu_init<SfpuType::prelu, APPROXIMATE>(); | ||
} | ||
|
||
template <bool APPROXIMATE> | ||
inline void llk_math_eltwise_unary_sfpu_prelu(uint dst_index, uint param0, int vector_mode = (int)VectorMode::RC) { | ||
llk_math_eltwise_unary_sfpu_params<APPROXIMATE>( | ||
ckernel::sfpu::calculate_prelu<APPROXIMATE>, | ||
dst_index, | ||
vector_mode, | ||
param0); | ||
} | ||
|
||
} // namespace ckernel |
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ enum SfpuType { | |
reciprocal, | ||
sqrt, | ||
lrelu, | ||
prelu, | ||
power, | ||
square, | ||
tanh_derivative, | ||
|
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,43 @@ | ||
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
|
||
#include "compute_kernel_api/common_globals.h" | ||
#ifdef TRISC_MATH | ||
#include "llk_math_eltwise_unary_sfpu_prelu.h" | ||
#define MAIN math_main() | ||
#define MATH(x) x | ||
#else | ||
#define MATH(x) | ||
#endif | ||
|
||
|
||
|
||
namespace ckernel { | ||
|
||
/** | ||
* Performs element-wise prelu operation. The value to be prelued in the tile is provided as const param0. The DST register buffer must be in | ||
* acquired state via *acquire_dst* call. This call is blocking and is only | ||
* available on the compute engine. | ||
* | ||
* Return value: None | ||
* | ||
* | Argument | Description | Type | Valid Range | Required | | ||
* |-----------------|----------------------------------------------------------------------------|----------|-------------------------------------------------------|----------| | ||
* | idst | The index of the tile in DST register buffer to perform the computation on | uint32_t | Must be less than the size of the DST register buffer | True | | ||
* | param0 | Constant value that is being multiplied if the input is lesser than 0 | uint32_t | | True | | ||
*/ | ||
ALWI void prelu_tile(uint32_t idst, uint32_t param0) { | ||
MATH((llk_math_eltwise_unary_sfpu_prelu<APPROX>(idst, param0))); | ||
} | ||
|
||
/** | ||
* Please refer to documentation for any_init. | ||
*/ | ||
ALWI void prelu_tile_init() { MATH((llk_math_eltwise_unary_sfpu_prelu_init<APPROX>())); } | ||
|
||
|
||
} // namespace ckernel |
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
Oops, something went wrong.