Skip to content
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

#16165: Add binary SFPU divide init function #16250

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,6 @@ def test_nei_ttnn(input_shapes, scalar, device):
assert comp_pass


@pytest.mark.skip(reason="#16165: Test is broken if you run after individually.")
@pytest.mark.parametrize(
"input_shapes",
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ namespace ckernel {
namespace sfpu {

template <bool APPROXIMATION_MODE, int BINOP_MODE, int ITERATIONS = 8>
inline void calculate_sfpu_binary(const uint dst_offset)
{
inline void calculate_sfpu_binary(const uint dst_offset) {
_calculate_sfpu_binary_<APPROXIMATION_MODE, BINOP_MODE, ITERATIONS>(dst_offset);
}

template <bool APPROXIMATION_MODE /*unused*/, int BINOP_MODE>
inline void sfpu_binary_init() {
_sfpu_binary_init_<APPROXIMATION_MODE, BINOP_MODE>();
}

} // namespace sfpu
} // namespace ckernel
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace ckernel {

// New LLK SFPU APIs

template <bool APPROXIMATE>
template <bool APPROXIMATE, int binop_mode>
inline void llk_math_eltwise_binary_sfpu_binop_init() {
llk_math_eltwise_binary_sfpu_init<SfpuType::unused, APPROXIMATE>();
llk_math_eltwise_binary_sfpu_init<SfpuType::unused, APPROXIMATE>(
ckernel::sfpu::sfpu_binary_init<APPROXIMATE, binop_mode>);
}

template <bool APPROXIMATE, int binop_mode>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ namespace ckernel {
namespace sfpu {

template <bool APPROXIMATION_MODE, int BINOP_MODE, int ITERATIONS = 8>
inline void calculate_sfpu_binary(const uint dst_offset)
{
inline void calculate_sfpu_binary(const uint dst_offset) {
_calculate_sfpu_binary_<APPROXIMATION_MODE, BINOP_MODE, ITERATIONS>(dst_offset);
}

template <bool APPROXIMATION_MODE /*unused*/, int BINOP_MODE>
inline void sfpu_binary_init() {
_sfpu_binary_init_<APPROXIMATION_MODE, BINOP_MODE>();
}

} // namespace sfpu
} // namespace ckernel
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace ckernel {

// New LLK SFPU APIs

template <bool APPROXIMATE>
template <bool APPROXIMATE, int binop_mode>
inline void llk_math_eltwise_binary_sfpu_binop_init() {
llk_math_eltwise_binary_sfpu_init<SfpuType::unused, APPROXIMATE>();
llk_math_eltwise_binary_sfpu_init<SfpuType::unused, APPROXIMATE>(
ckernel::sfpu::sfpu_binary_init<APPROXIMATE, binop_mode>);
}

template <bool APPROXIMATE, int binop_mode>
Expand Down
12 changes: 11 additions & 1 deletion tt_metal/include/compute_kernel_api/eltwise_binary_sfpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ ALWI void power_binary_tile(uint32_t idst0, uint32_t idst1) {
/**
* Please refer to documentation for any_init.
*/
ALWI void eltwise_binop_tile_init() { MATH((llk_math_eltwise_binary_sfpu_binop_init<APPROX>())); }
ALWI void add_binary_tile_init() { MATH((llk_math_eltwise_binary_sfpu_binop_init<APPROX, ADD_BINARY>())); }

ALWI void sub_binary_tile_init() { MATH((llk_math_eltwise_binary_sfpu_binop_init<APPROX, SUB_BINARY>())); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So each binary op is potentially a different init.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please give the enum a name, make it an enum class, and use that as the template parameter type instead of int?


ALWI void mul_binary_tile_init() { MATH((llk_math_eltwise_binary_sfpu_binop_init<APPROX, MUL_BINARY>())); }

ALWI void div_binary_tile_init() { MATH((llk_math_eltwise_binary_sfpu_binop_init<APPROX, DIV_BINARY>())); }

ALWI void rsub_binary_tile_init() { MATH((llk_math_eltwise_binary_sfpu_binop_init<APPROX, RSUB_BINARY>())); }

ALWI void power_binary_tile_init() { MATH((llk_math_eltwise_binary_sfpu_binop_init<APPROX, POW_BINARY>())); }

} // namespace ckernel
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,30 @@ std::map<std::string, std::string> get_defines_fp32(
new_defines.insert({"ADD_INT32_INIT", fmt::format("add_int32_tile_init();")});
op_name = "add_int32_tile";
} else {
new_defines.insert({"BINOP_INIT", fmt::format("add_binary_tile_init();")});
op_name = "add_binary_tile";
}
break;
case BinaryOpType::SUB: op_name = "sub_binary_tile"; break;
case BinaryOpType::MUL: op_name = "mul_binary_tile"; break;
case BinaryOpType::RSUB: op_name = "rsub_binary_tile"; break;
case BinaryOpType::POWER: op_name = "power_binary_tile"; break;
case BinaryOpType::DIV_FAST: op_name = "div_binary_tile"; break;
case BinaryOpType::SUB:
new_defines.insert({"BINOP_INIT", fmt::format("sub_binary_tile_init();")});
op_name = "sub_binary_tile";
break;
case BinaryOpType::MUL:
new_defines.insert({"BINOP_INIT", fmt::format("mul_binary_tile_init();")});
op_name = "mul_binary_tile";
break;
case BinaryOpType::RSUB:
new_defines.insert({"BINOP_INIT", fmt::format("rsub_binary_tile_init();")});
op_name = "rsub_binary_tile";
break;
case BinaryOpType::POWER:
new_defines.insert({"BINOP_INIT", fmt::format("power_binary_tile_init();")});
op_name = "power_binary_tile";
break;
case BinaryOpType::DIV_FAST:
new_defines.insert({"BINOP_INIT", fmt::format("div_binary_tile_init();")});
op_name = "div_binary_tile";
break;
case BinaryOpType::BITWISE_AND:
new_defines.insert({"BITWISE_INIT", fmt::format("binary_bitwise_tile_init();")});
op_name = "and_binary_tile";
Expand All @@ -217,12 +233,14 @@ std::map<std::string, std::string> get_defines_fp32(
// PRE_IN1_0 ====> Applies prescaling for second input
new_defines.merge(get_defines(UnaryOpType::EXP, std::vector<float>{0}, "PRE_IN0_0"));
new_defines.merge(get_defines(UnaryOpType::EXP, std::vector<float>{0}, "PRE_IN1_0"));
new_defines.insert({"BINOP_INIT", fmt::format("add_binary_tile_init();")});
op_name = "add_binary_tile";
new_defines.merge(get_defines(UnaryOpType::LOG, std::nullopt, "0", idst1));
break;
case BinaryOpType::LOGADDEXP2:
new_defines.merge(get_defines(UnaryOpType::EXP2, std::nullopt, "PRE_IN0_0"));
new_defines.merge(get_defines(UnaryOpType::EXP2, std::nullopt, "PRE_IN1_0"));
new_defines.insert({"BINOP_INIT", fmt::format("add_binary_tile_init();")});
op_name = "add_binary_tile";
new_defines.merge(get_defines(UnaryOpType::LOG2, std::nullopt, "0", idst1));
break;
Expand All @@ -239,12 +257,14 @@ std::map<std::string, std::string> get_defines_fp32(
new_defines.merge(get_defines(UnaryOpType::NEZ, std::nullopt, "0", idst1));
break;
case BinaryOpType::BIAS_GELU:
new_defines.insert({"BINOP_INIT", fmt::format("add_binary_tile_init();")});
op_name = "add_binary_tile";
new_defines.merge(get_defines(UnaryOpType::GELU, std::vector<float>{0}, "0", idst1));
break;
case BinaryOpType::LOGICAL_OR:
new_defines.merge(get_defines(UnaryOpType::NEZ, std::nullopt, "PRE_IN0_0"));
new_defines.merge(get_defines(UnaryOpType::NEZ, std::nullopt, "PRE_IN1_0"));
new_defines.insert({"BINOP_INIT", fmt::format("add_binary_tile_init();")});
op_name = "add_binary_tile";
new_defines.merge(get_defines(UnaryOpType::GTZ, std::nullopt, "0", idst1));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

#define PRE_SCALE defined SFPU_OP_INIT_PRE_IN0_0 || defined SFPU_OP_INIT_PRE_IN1_0

#if defined(ADD_INT32_INIT) || defined(BITWISE_INIT) || defined(SHIFT_INIT)
#define INT32_INIT
#endif

namespace NAMESPACE {
void MAIN {
uint32_t per_core_block_cnt = get_arg_val<uint32_t>(0);
Expand Down Expand Up @@ -111,10 +107,9 @@ void MAIN {
for (uint32_t i = 0; i < per_core_block_size; ++i) {
copy_tile(cb_inp1, i, i * 2 + 1);

#ifndef INT32_INIT
eltwise_binop_tile_init();
#ifdef BINOP_INIT
BINOP_INIT
#endif

#ifdef ADD_INT32_INIT
ADD_INT32_INIT
#endif
Expand Down
Loading