/* * Copyright (c) Meta Platforms, Inc. and affiliates. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ #include #include #include #include namespace torch { namespace executor { namespace native { Tensor& pow_Tensor_Tensor_out( KernelRuntimeContext& ctx, const Tensor& a, const Tensor& b, Tensor& out) { // Common Dtype ScalarType common_type = promoteTypes(a.scalar_type(), b.scalar_type()); // Check Common Dtype ET_KERNEL_CHECK( ctx, (canCast(common_type, out.scalar_type()) && common_type != ScalarType::Bool), InvalidArgument, out); // Check Dim Order ET_KERNEL_CHECK( ctx, tensors_have_same_dim_order(a, b, out), InvalidArgument, out); // Resize ET_KERNEL_CHECK( ctx, resize_to_broadcast_target_size(a, b, out) == Error::Ok, InvalidArgument, out); // Compute Dtype ScalarType compute_type = utils::get_compute_type(common_type); if (compute_type != ScalarType::Float) { compute_type = ScalarType::Double; } // @lint-ignore CLANGTIDY facebook-hte-CArray static constexpr const char op_name[] = "pow.Tensor_Tensor_out"; ET_SWITCH_FLOAT_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() { utils::apply_bitensor_elementwise_fn< CTYPE_COMPUTE, op_name, utils::SupportedTensorDtypes::REALHBF16>( [](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) { // TODO: rewrite this to be vectorization-capable. return std::pow(val_a, val_b); }, ctx, a, utils::SupportedTensorDtypes::REALHBBF16, b, utils::SupportedTensorDtypes::REALHBBF16, out); }); return out; } Tensor& pow_Tensor_Scalar_out( KernelRuntimeContext& ctx, const Tensor& a, const Scalar& b, Tensor& out) { // Common Dtype ScalarType common_type = utils::promote_type_with_scalar(a.scalar_type(), b); // Check Common Dtype ET_KERNEL_CHECK( ctx, (canCast(common_type, out.scalar_type()) && common_type != ScalarType::Bool), InvalidArgument, out); // Check Dim Order ET_KERNEL_CHECK( ctx, tensors_have_same_dim_order(a, out), InvalidArgument, out); // Resize ET_KERNEL_CHECK( ctx, resize_tensor(out, a.sizes()) == Error::Ok, InvalidArgument, out); // Compute Dtype ScalarType compute_type = utils::get_compute_type(common_type); if (compute_type != ScalarType::Float) { compute_type = ScalarType::Double; } // @lint-ignore CLANGTIDY facebook-hte-CArray static constexpr const char op_name[] = "pow.Tensor_Scalar_out"; ET_SWITCH_FLOAT_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() { const CTYPE_COMPUTE val_b = utils::scalar_to(b); utils::apply_unitensor_elementwise_fn< CTYPE_COMPUTE, op_name, utils::SupportedTensorDtypes::REALHBF16>( // TODO: rewrite this to be vectorization-capable. [val_b](const CTYPE_COMPUTE val_a) { return std::pow(val_a, val_b); }, ctx, a, utils::SupportedTensorDtypes::REALHBBF16, out); }); return out; } Tensor& pow_Scalar_out( KernelRuntimeContext& ctx, const Scalar& a, const Tensor& b, Tensor& out) { // Common Dtype ScalarType common_type = utils::promote_type_with_scalar(b.scalar_type(), a); // Check Common Dtype ET_KERNEL_CHECK( ctx, (canCast(common_type, out.scalar_type()) && common_type != ScalarType::Bool), InvalidArgument, out); // Check Dim Order ET_KERNEL_CHECK( ctx, tensors_have_same_dim_order(b, out), InvalidArgument, out); // Resize ET_KERNEL_CHECK( ctx, resize_tensor(out, b.sizes()) == Error::Ok, InvalidArgument, out); // Compute Dtype ScalarType compute_type = utils::get_compute_type(common_type); if (compute_type != ScalarType::Float) { compute_type = ScalarType::Double; } // @lint-ignore CLANGTIDY facebook-hte-CArray static constexpr const char op_name[] = "pow.Scalar_out"; ET_SWITCH_FLOAT_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() { const CTYPE_COMPUTE val_a = utils::scalar_to(a); utils::apply_unitensor_elementwise_fn< CTYPE_COMPUTE, op_name, utils::SupportedTensorDtypes::REALHBF16>( // TODO: rewrite this to be vectorization-capable. [val_a](const CTYPE_COMPUTE val_b) { return std::pow(val_a, val_b); }, ctx, b, utils::SupportedTensorDtypes::REALHBBF16, out); }); return out; } } // namespace native } // namespace executor } // namespace torch