#include "op_plugin/AclOpsInterface.h"
#include "op_plugin/utils/OpAdapter.h"
namespace acl_op {
using npu_preparation = at_npu::native::OpPreparation;
at::Tensor bincount(const at::Tensor &self, const c10::optional<at::Tensor> &weight_opt, int64_t minlength)
{
const at::Tensor &weights = c10::value_or_else(weight_opt, [] { return at::Tensor(); });
TORCH_CHECK(self.numel() > 0, "The input tensor is an empty tensor." + OPS_ERROR(ErrCode::PARAM));
if (self.sizes()[0] == 0) {
auto result = npu_preparation::ApplyTensorWithSizes({0}, self.options().dtype(at::kLong));
return result;
}
auto sizes = static_cast<int64_t>(op_plugin::utils::get_scalar_float_value(acl_op::max(self).item()));
sizes = (sizes < minlength) ? minlength : (sizes + 1);
if (self.dtype() == at::kLong) {
TORCH_NPU_WARN_ONCE("CANN: Bincount cann't support dtype int64, input will be cast to int32.");
}
auto input = (self.dtype() == at::kInt) ? self : at_npu::native::custom_ops::_npu_dtype_cast(self, at::kInt);
auto weight = weights;
if (!weights.defined()) {
at::TensorOptions options = input.options();
weight = acl_op::ones(input.sizes(), at::kLong, options.layout(), options.device(), options.pinned_memory());
} else if (!(weights.dtype() == at::kFloat)) {
weight = at_npu::native::custom_ops::_npu_dtype_cast(weights, at::kDouble);
}
auto result = npu_preparation::apply_tensor(weight, {sizes});
at_npu::native::OpCommand cmd;
cmd.Name("Bincount").Input(input).Input(at::Scalar(sizes), at::kInt).Input(weight).Output(result).Run();
return result;
}
}