#include <climits>
#include "op_plugin/AclOpsInterface.h"
#include "op_plugin/OpApiInterface.h"
#include "op_plugin/utils/op_api_common.h"
#include "torch_npu/csrc/framework/utils/RandomOpAdapter.h"
namespace op_api {
static const uint64_t PHILOX_DEFAULT_NUM = 10;
using npu_preparation = at_npu::native::OpPreparation;
at::Tensor& bernoulli_(at::Tensor& self, double p, c10::optional<at::Generator> gen) {
DO_COMPATIBILITY(aclnnInplaceBernoulli, acl_op::bernoulli_(self, p, gen));
auto gen_ = at::get_generator_or_default<at_npu::NPUGeneratorImpl>(gen, at_npu::detail::getDefaultNPUGenerator());
auto pair = gen_->philox_engine_inputs(PHILOX_DEFAULT_NUM);
const uint64_t seed = pair.first;
const uint64_t offset = pair.second;
const c10::Scalar& pScalar = at::Scalar(p);
EXEC_NPU_CMD(aclnnInplaceBernoulli, self, pScalar, seed, offset);
return self;
}
at::Tensor& bernoulli_(at::Tensor& self, const at::Tensor& p, c10::optional<at::Generator> gen) {
DO_COMPATIBILITY(aclnnInplaceBernoulliTensor, acl_op::bernoulli_(self, p, gen));
auto gen_ = at::get_generator_or_default<at_npu::NPUGeneratorImpl>(gen, at_npu::detail::getDefaultNPUGenerator());
auto pair = gen_->philox_engine_inputs(PHILOX_DEFAULT_NUM);
const uint64_t seed = pair.first;
const uint64_t offset = pair.second;
EXEC_NPU_CMD(aclnnInplaceBernoulliTensor, self, p, seed, offset);
return self;
}
at::Tensor bernoulli(const at::Tensor& self, c10::optional<at::Generator> gen) {
DO_COMPATIBILITY(aclnnInplaceBernoulliTensor, acl_op::bernoulli(self, gen));
at::Tensor self_copy = npu_preparation::apply_tensor_without_format(self);
at::namedinference::propagate_names(self_copy, self);
return op_api::bernoulli_(self_copy, self, gen);
}
at::Tensor bernoulli(const at::Tensor& self, double p, c10::optional<at::Generator> gen) {
DO_COMPATIBILITY(aclnnInplaceBernoulli, acl_op::bernoulli(self, p, gen));
return at::empty_like(self, LEGACY_CONTIGUOUS_MEMORY_FORMAT).bernoulli_(p, gen);
}
at::Tensor& bernoulli_out(const at::Tensor& self, c10::optional<at::Generator> gen, at::Tensor& result) {
DO_COMPATIBILITY(aclnnInplaceBernoulliTensor, acl_op::bernoulli_out(self, gen, result));
result.resize_(self.sizes()).bernoulli_(self, gen);
at::namedinference::propagate_names(result, self);
return result;
}
}