/* * 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 #include namespace torch::executor::native { Tensor& elu_out( KernelRuntimeContext& ctx, const Tensor& in, const Scalar& alpha, const Scalar& scale, const Scalar& input_scale, Tensor& out) { ET_KERNEL_CHECK(ctx, tensors_have_same_dtype(in, out), InvalidArgument, out); ET_KERNEL_CHECK( ctx, resize_tensor(out, in.sizes()) == Error::Ok, InvalidArgument, out); ET_KERNEL_CHECK( ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out); ET_KERNEL_CHECK(ctx, tensor_is_floating_type(in), InvalidArgument, out); ET_KERNEL_CHECK(ctx, tensors_have_same_dtype(in, out), InvalidArgument, out); static constexpr const char op_name[] = "elu.out"; ET_SWITCH_FLOATHBF16_TYPES(in.scalar_type(), ctx, op_name, CTYPE, [&]() { using MathT = std:: conditional_t, float, CTYPE>; MathT math_alpha = 0; MathT math_scale = 0; MathT math_input_scale = 0; ET_EXTRACT_SCALAR(alpha, math_alpha); ET_EXTRACT_SCALAR(scale, math_scale); ET_EXTRACT_SCALAR(input_scale, math_input_scale); const auto negcoef = math_alpha * math_scale; utils::apply_unitensor_elementwise_fn< CTYPE, op_name, utils::SupportedTensorDtypes::SAME_AS_COMMON>( [negcoef, math_scale, math_input_scale](const auto x) { // TODO: rewrite this to be vectorization-capable. return MathT(x) <= MathT(0) ? std::expm1(MathT(x) * math_input_scale) * negcoef : MathT(x) * math_scale; }, ctx, in, utils::SupportedTensorDtypes::FLOATHBF16, out); }); return out; } } // namespace torch::executor::native