Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions backends/cortex_m/ops/op_quantize_per_tensor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -193,8 +194,8 @@ Tensor& quantize_per_tensor_out(
int32x4_t out_CDEF =
vsubq_s32(vreinterpretq_s32_f32(outf_CDEF), vmagic_int_less_zp);

int16x8_t out_04152637;
int16x8_t out_8C9DAEBF;
int16x8_t out_04152637{};
int16x8_t out_8C9DAEBF{};
out_04152637 = vmovnbq_s32(out_04152637, out_0123);
out_04152637 = vmovntq_s32(out_04152637, out_4567);
out_8C9DAEBF = vmovnbq_s32(out_8C9DAEBF, out_89AB);
Expand All @@ -205,7 +206,7 @@ Tensor& quantize_per_tensor_out(
int16x8_t out_8C9DAEBF_clamped =
vminq_s16(vmaxq_s16(out_8C9DAEBF, vqmin), vqmax);

int8x16_t out_084C195D2A6E3B7F;
int8x16_t out_084C195D2A6E3B7F{};
out_084C195D2A6E3B7F =
vmovnbq_s16(out_084C195D2A6E3B7F, out_04152637_clamped);
out_084C195D2A6E3B7F =
Expand Down
2 changes: 1 addition & 1 deletion backends/cortex_m/ops/op_quantized_linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Tensor& quantized_linear_impl(
auto in_feat = input.size(input.dim() - 1);
auto out_feat = out.size(out.dim() - 1);
auto batches = 1;
for (size_t i = 0; i < input.dim() - 1; i++) {
for (int64_t i = 0; i < input.dim() - 1; i++) {
batches *= input.size(i);
}
ET_LOG(
Expand Down
18 changes: 13 additions & 5 deletions extension/tensor/tensor_ptr_maker.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -26,9 +27,16 @@ bool extract_scalar(executorch::aten::Scalar scalar, INT_T* out_val) {
return false;
}
int64_t val = scalar.to<int64_t>();
if (val < std::numeric_limits<INT_T>::lowest() ||
val > std::numeric_limits<INT_T>::max()) {
return false;
if constexpr (std::is_signed_v<INT_T>) {
if (val < static_cast<int64_t>(std::numeric_limits<INT_T>::lowest()) ||
val > static_cast<int64_t>(std::numeric_limits<INT_T>::max())) {
return false;
}
} else {
if (val < 0 ||
static_cast<uint64_t>(val) > std::numeric_limits<INT_T>::max()) {
return false;
}
}
*out_val = static_cast<INT_T>(val);
return true;
Expand All @@ -46,8 +54,8 @@ bool extract_scalar(executorch::aten::Scalar scalar, FLOAT_T* out_val) {
if (scalar.isFloatingPoint()) {
val = scalar.to<double>();
if (std::isfinite(val) &&
(val < std::numeric_limits<FLOAT_T>::lowest() ||
val > std::numeric_limits<FLOAT_T>::max())) {
(val < static_cast<double>(std::numeric_limits<FLOAT_T>::lowest()) ||
val > static_cast<double>(std::numeric_limits<FLOAT_T>::max()))) {
return false;
}
} else if (scalar.isIntegral(/*includeBool=*/false)) {
Expand Down
6 changes: 5 additions & 1 deletion kernels/portable/cpu/op__adaptive_avg_pool2d.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -74,6 +75,7 @@ Tensor& _adaptive_avg_pool2d_out(
static constexpr const char op_name[] = "_adaptive_avg_pool2d.out";

ET_SWITCH_FLOATHBF16_TYPES_AND(Long, in_type, ctx, op_name, CTYPE, [&]() {
using AccumulateType = decltype(float{} + CTYPE{});
const CTYPE* const in_ptr = in.const_data_ptr<CTYPE>();
CTYPE* const out_ptr = out.mutable_data_ptr<CTYPE>();

Expand Down Expand Up @@ -106,7 +108,9 @@ Tensor& _adaptive_avg_pool2d_out(
float sum = 0;
for (int64_t ih = ih0; ih < ih1; ++ih) {
for (int64_t iw = iw0; iw < iw1; ++iw) {
sum += plane_in[ih * in_W + iw];
sum = static_cast<float>(
static_cast<AccumulateType>(sum) +
static_cast<AccumulateType>(plane_in[ih * in_W + iw]));
}
}

Expand Down
5 changes: 4 additions & 1 deletion kernels/portable/cpu/op__to_dim_order_copy.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -79,7 +80,9 @@ Tensor& _to_dim_order_copy_out(
/*support_noncontiguous_input_tensors=*/true>(
/*dummy output*/ self, self, out)) {
(void)unused_index;
out_data[out_data_index].real_ = self_data[self_data_index];
out_data[out_data_index].real_ =
static_cast<decltype(CTYPE_OUT{}.real_)>(
self_data[self_data_index]);
out_data[out_data_index].imag_ = 0;
}
});
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_abs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -49,8 +50,8 @@ Tensor& abs_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
ET_SWITCH_FLOATH_TYPES(out.scalar_type(), ctx, op_name, CTYPE_OUT, [&] {
apply_unary_map_fn<CTYPE_IN, CTYPE_OUT>(
[](const CTYPE_IN val_in) -> CTYPE_OUT {
return sqrt(
val_in.real_ * val_in.real_ + val_in.imag_ * val_in.imag_);
return static_cast<CTYPE_OUT>(sqrt(
val_in.real_ * val_in.real_ + val_in.imag_ * val_in.imag_));
},
in.const_data_ptr<CTYPE_IN>(),
out.mutable_data_ptr<CTYPE_OUT>(),
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_allclose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ bool data_is_close(
return false;
}
} else {
auto allowed_error = atol + std::fabs(rtol * b[i]);
auto allowed_error = atol + std::fabs(rtol * static_cast<double>(b[i]));
auto actual_error = std::fabs(a[i] - b[i]);
if (!std::isfinite(actual_error) || actual_error > allowed_error) {
if (!std::isfinite(actual_error) ||
static_cast<double>(actual_error) > allowed_error) {
return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion kernels/portable/cpu/op_cdist_forward.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -106,7 +107,7 @@ void cdist(const Tensor& x1, const Tensor& x2, Tensor& out, double p) {
cdist<CTYPE, L1<CTYPE>>(x1, x2, out, p);
} else if (p == 2.0) {
cdist<CTYPE, L2<CTYPE>>(x1, x2, out, p);
} else if (p == INFINITY) {
} else if (p == static_cast<double>(INFINITY)) {
cdist<CTYPE, Linf<CTYPE>>(x1, x2, out, p);
} else {
cdist<CTYPE, Lp<CTYPE>>(x1, x2, out, p);
Expand Down
6 changes: 4 additions & 2 deletions kernels/portable/cpu/op_clamp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -29,8 +30,9 @@ namespace {
template <typename CTYPE_OUT, typename CTYPE_CAST>
/** Check if val, when cast to CTYPE_CAST, is not in the range of CTYPE_OUT */
bool is_out_of_bounds(CTYPE_CAST val_cast) {
return val_cast < std::numeric_limits<CTYPE_OUT>::lowest() ||
val_cast > std::numeric_limits<CTYPE_OUT>::max();
return val_cast <
static_cast<CTYPE_CAST>(std::numeric_limits<CTYPE_OUT>::lowest()) ||
val_cast > static_cast<CTYPE_CAST>(std::numeric_limits<CTYPE_OUT>::max());
}

ET_NODISCARD bool check_bounds(
Expand Down
12 changes: 9 additions & 3 deletions kernels/portable/cpu/op_gelu.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -47,11 +48,14 @@ Tensor& gelu_out(
return std::numeric_limits<CTYPE>::infinity();
}
const CTYPE kBeta = M_SQRT2 * M_2_SQRTPI * 0.5;
const CTYPE kKappa = static_cast<float>(0.044715);
const CTYPE kKappa =
static_cast<CTYPE>(static_cast<float>(0.044715));

const CTYPE x_cubed = x * x * x;
const CTYPE inner = kBeta * (x + kKappa * x_cubed);
const CTYPE ret = 0.5 * x * (1.0 + std::tanh(inner));
const CTYPE ret = static_cast<CTYPE>(
0.5 * static_cast<double>(x) *
(1.0 + static_cast<double>(std::tanh(inner))));

return ret;
},
Expand All @@ -66,7 +70,9 @@ Tensor& gelu_out(
} else if (x == std::numeric_limits<CTYPE>::infinity()) {
return std::numeric_limits<CTYPE>::infinity();
}
return static_cast<CTYPE>(0.5 * x * (1 + std::erf(x * M_SQRT1_2)));
return static_cast<CTYPE>(
0.5 * static_cast<double>(x) *
(1 + std::erf(static_cast<double>(x) * M_SQRT1_2)));
},
in.const_data_ptr<CTYPE>(),
out.mutable_data_ptr<CTYPE>(),
Expand Down
9 changes: 5 additions & 4 deletions kernels/portable/cpu/op_logit.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -41,10 +42,10 @@ Tensor& logit_out(
[eps](const CTYPE_IN val_in) {
CTYPE_OUT xi = static_cast<CTYPE_OUT>(val_in);
if (eps.has_value()) {
if (xi < eps.value()) {
xi = eps.value();
} else if (xi > 1 - eps.value()) {
xi = 1 - eps.value();
if (static_cast<double>(xi) < eps.value()) {
xi = static_cast<CTYPE_OUT>(eps.value());
} else if (static_cast<double>(xi) > 1 - eps.value()) {
xi = static_cast<CTYPE_OUT>(1 - eps.value());
}
}
return static_cast<CTYPE_OUT>(
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_mean.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -118,8 +119,8 @@ Tensor& mean_dim_out(
ix /= out.size(d);
}
}
out_data[out_offset] =
static_cast<CTYPE_OUT>(sum / static_cast<float>(num));
out_data[out_offset] = static_cast<CTYPE_OUT>(
sum / static_cast<ACC>(static_cast<float>(num)));
}
});
ET_KERNEL_CHECK_MSG(ctx, success, Internal, , "parallel_for failed");
Expand Down
3 changes: 2 additions & 1 deletion kernels/portable/cpu/op_native_batch_norm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -182,7 +183,7 @@ std::tuple<Tensor&, Tensor&, Tensor&> _native_batch_norm_legit_no_training_out(
for (size_t c = 0; c < C; ++c) {
CTYPE mean = mean_data[c];
CTYPE var = var_data[c];
CTYPE invstd = 1.0 / std::sqrt(var + eps);
CTYPE invstd = 1.0 / std::sqrt(static_cast<double>(var) + eps);
CTYPE weight_val = 1;
if (weight.has_value()) {
weight_val = weight.value().const_data_ptr<CTYPE>()[c];
Expand Down
3 changes: 2 additions & 1 deletion kernels/portable/cpu/op_reciprocal.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -16,7 +17,7 @@ namespace {

template <typename T>
T reciprocal(T x) {
return 1.0 / x;
return static_cast<T>(1.0 / static_cast<double>(x));
}

} // namespace
Expand Down
6 changes: 5 additions & 1 deletion kernels/portable/cpu/op_round.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -25,7 +26,10 @@ namespace {
// the implementation from torch/src/jit/runtime/register_ops_utils.h.
template <typename CTYPE>
inline CTYPE round_to_even(CTYPE a) {
return a - std::floor(a) == 0.5 ? (std::round(a * 0.5) * 2.0) : std::round(a);
return static_cast<CTYPE>(
static_cast<double>(a - std::floor(a)) == 0.5
? (std::round(static_cast<double>(a) * 0.5) * 2.0)
: static_cast<double>(std::round(a)));
}

} // namespace
Expand Down
3 changes: 2 additions & 1 deletion kernels/portable/cpu/op_rsqrt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -16,7 +17,7 @@ namespace {

template <typename T>
T rsqrt(T x) {
return 1.0 / std::sqrt(x);
return static_cast<T>(1.0 / static_cast<double>(std::sqrt(x)));
}

} // namespace
Expand Down
23 changes: 15 additions & 8 deletions kernels/portable/cpu/op_upsample_bilinear2d.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -22,6 +23,7 @@ void upsample_bilinear2d_kernel_impl_nchw(
const float scale_h,
const float scale_w,
Tensor& out) {
using ComputeType = decltype(CTYPE{} * float{});
const auto in_data = in.const_data_ptr<CTYPE>();
auto out_data = out.mutable_data_ptr<CTYPE>();

Expand Down Expand Up @@ -68,10 +70,12 @@ void upsample_bilinear2d_kernel_impl_nchw(
const auto bottom_right =
in_plane[in_h2 * in.strides()[2] + in_w2 * in.strides()[3]];

const auto top = top_left * weight_w + top_right * inv_weight_w;
const auto bottom =
bottom_left * weight_w + bottom_right * inv_weight_w;
const auto val = top * weight_h + bottom * inv_weight_h;
const auto top = top_left * static_cast<ComputeType>(weight_w) +
top_right * static_cast<ComputeType>(inv_weight_w);
const auto bottom = bottom_left * static_cast<ComputeType>(weight_w) +
bottom_right * static_cast<ComputeType>(inv_weight_w);
const auto val = top * static_cast<ComputeType>(weight_h) +
bottom * static_cast<ComputeType>(inv_weight_h);

*out_data = val;
out_data++;
Expand All @@ -90,6 +94,7 @@ void upsample_bilinear2d_kernel_impl_nhwc(
const float scale_h,
const float scale_w,
Tensor& out) {
using ComputeType = decltype(CTYPE{} * float{});
auto in_data = in.const_data_ptr<CTYPE>();
auto out_data = out.mutable_data_ptr<CTYPE>();

Expand Down Expand Up @@ -139,10 +144,12 @@ void upsample_bilinear2d_kernel_impl_nhwc(
[in_h2 * in.strides()[2] + in_w2 * in.strides()[3] +
c * in.strides()[1]];

const auto top = top_left * weight_w + top_right * inv_weight_w;
const auto bottom =
bottom_left * weight_w + bottom_right * inv_weight_w;
const auto val = top * weight_h + bottom * inv_weight_h;
const auto top = top_left * static_cast<ComputeType>(weight_w) +
top_right * static_cast<ComputeType>(inv_weight_w);
const auto bottom = bottom_left * static_cast<ComputeType>(weight_w) +
bottom_right * static_cast<ComputeType>(inv_weight_w);
const auto val = top * static_cast<ComputeType>(weight_h) +
bottom * static_cast<ComputeType>(inv_weight_h);

*out_data = val;
out_data++;
Expand Down
Loading
Loading