#include "op_plugin/OpApiInterface.h"
#include "op_plugin/utils/op_api_common.h"
#include "op_plugin/AclOpsInterface.h"
namespace op_api {
using npu_preparation = at_npu::native::OpPreparation;
namespace {
constexpr int64_t NUM_TWO = 2;
constexpr int64_t SPLIT_BLOCK_SIZE = 64;
constexpr int64_t MIN_INPUT_DIM = 2;
};
std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor> npu_swiglu_mx_quant_with_dual_axis(
const at::Tensor& x,
const c10::optional<at::Tensor>& group_index,
bool activate_left,
c10::string_view round_mode,
int64_t scale_alg,
int64_t dst_type,
double dst_type_max)
{
at::Tensor y1;
at::Tensor mxscale1;
at::Tensor y2;
at::Tensor mxscale2;
TORCH_CHECK(x.dim() >= MIN_INPUT_DIM, "The input x should be at least 2D" + OPS_ERROR(ErrCode::PARAM));
TORCH_CHECK(x.size(-1) % NUM_TWO == 0, "The last dim of input must be divisible by 2" + OPS_ERROR(ErrCode::PARAM));
static const bool is_available = check_aclnn_kernel_available("aclnnSwigluMxQuantWithDualAxis");
TORCH_CHECK(is_available,
"Current CANN version do not support this api: npu_swiglu_mx_quant_with_dual_axis. Please try to update the version of CANN."
+ OPS_ERROR(ErrCode::PARAM));
const at::Tensor& group_index_opt = c10::value_or_else(group_index, [] { return at::Tensor(); });
char *round_mode_ptr = const_cast<char *>(round_mode.data());
auto y_shape = op_infer::array_to_small_vector(x.sizes());
y_shape[y_shape.size() - 1] = y_shape[y_shape.size() - 1] / NUM_TWO;
auto mxscale1_shape = op_infer::array_to_small_vector(y_shape);
int64_t last_dim = mxscale1_shape[mxscale1_shape.size() - 1];
mxscale1_shape[mxscale1_shape.size() - 1] = static_cast<int64_t>(std::ceil(static_cast<double>(last_dim) / SPLIT_BLOCK_SIZE));
mxscale1_shape.emplace_back(NUM_TWO);
auto mxscale2_shape = op_infer::array_to_small_vector(y_shape);
int64_t second_to_last_dim = mxscale2_shape[mxscale2_shape.size() - 2];
int64_t quant_size = static_cast<int64_t>(std::ceil(static_cast<double>(second_to_last_dim) / SPLIT_BLOCK_SIZE));
if (group_index_opt.defined()) {
quant_size = static_cast<int64_t>(std::floor(static_cast<double>(second_to_last_dim) / SPLIT_BLOCK_SIZE));
quant_size = quant_size + group_index_opt.size(0);
}
mxscale2_shape[mxscale2_shape.size() - 2] = quant_size;
mxscale2_shape.emplace_back(NUM_TWO);
aclDataType y_acltype;
bool special_output_type = (dst_type == static_cast<int64_t>(c10_npu::DType::FLOAT4_E2M1) ||
dst_type == static_cast<int64_t>(c10_npu::DType::FLOAT4_E1M2));
ASCEND_LOGI("[npu_swiglu_mx_quant_with_dual_axis]: Getting aclTensor y1 and y2 dtype by Parameter(dst_type): %ld", dst_type);
if (special_output_type) {
int64_t y_last_dim_val = y_shape[y_shape.size() - 1];
TORCH_CHECK(y_last_dim_val % NUM_TWO == 0,
"The last dim of y must be divisible by 2 if y dtype is float4_e2m1 or float4_e1m2"
+ OPS_ERROR(ErrCode::PARAM));
y_shape[y_shape.size() - 1] = y_last_dim_val / NUM_TWO;
y1 = npu_preparation::apply_tensor_without_format(y_shape, c10::ScalarType::Byte);
y2 = npu_preparation::apply_tensor_without_format(y_shape, c10::ScalarType::Byte);
y_acltype = c10_npu::GetAclDataType(dst_type);
} else {
y_acltype = c10_npu::GetAclDataType(dst_type);
at::ScalarType scalar_dtype = npu_preparation::convert_to_scalar_type(y_acltype);
y1 = npu_preparation::apply_tensor_without_format(y_shape, c10::dtype(scalar_dtype));
y2 = npu_preparation::apply_tensor_without_format(y_shape, c10::dtype(scalar_dtype));
}
mxscale1 = npu_preparation::apply_tensor_without_format(mxscale1_shape, c10::dtype(at::ScalarType::Byte));
mxscale2 = npu_preparation::apply_tensor_without_format(mxscale2_shape, c10::dtype(at::ScalarType::Byte));
TensorWrapper y1_wrapper = {y1, y_acltype};
TensorWrapper y2_wrapper = {y2, y_acltype};
TensorWrapper mxscale1_wrapper = {mxscale1, aclDataType::ACL_FLOAT8_E8M0};
TensorWrapper mxscale2_wrapper = {mxscale2, aclDataType::ACL_FLOAT8_E8M0};
EXEC_NPU_CMD(aclnnSwigluMxQuantWithDualAxis, x, group_index_opt, activate_left,
round_mode_ptr, scale_alg, y_acltype, dst_type_max,
y1_wrapper, mxscale1_wrapper, y2_wrapper, mxscale2_wrapper);
return std::make_tuple(y1, mxscale1, y2, mxscale2);
}
}