Skip to content
Merged
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
28 changes: 27 additions & 1 deletion kernels/portable/cpu/scalar_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#pragma once

#include <algorithm>
#include <cmath>
#include <limits>

Expand Down Expand Up @@ -261,6 +260,33 @@ bool extract_scalar(Scalar scalar, BOOL_T* out_val) {
return false;
}

/*
* Convert Scalar to C++ type
*/

template <typename T>
T scalar_to(const Scalar& s) {
if (s.isBoolean()) {
return static_cast<T>(s.to<bool>());
} else if (s.isFloatingPoint()) {
return static_cast<T>(s.to<double>());
} else {
return static_cast<T>(s.to<int64_t>());
}
}

template <>
inline double scalar_to<double>(const Scalar& s) {
return s.isFloatingPoint() ? s.to<double>()
: static_cast<double>(s.to<int64_t>());
}

template <>
inline int64_t scalar_to<int64_t>(const Scalar& s) {
return s.isFloatingPoint() ? static_cast<int64_t>(s.to<double>())
: s.to<int64_t>();
}

} // namespace utils
} // namespace native
} // namespace executor
Expand Down
29 changes: 1 addition & 28 deletions kernels/portable/cpu/util/elementwise_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include <c10/util/irange.h>
#include <executorch/kernels/portable/cpu/scalar_utils.h>
#include <executorch/kernels/portable/cpu/util/broadcast_indexes_range.h>
#include <executorch/kernels/portable/cpu/util/broadcast_util.h>
#include <executorch/kernels/portable/cpu/util/dtype_util.h>
Expand All @@ -27,34 +28,6 @@ namespace torch {
namespace executor {
namespace native {
namespace utils {

/*
* Convert Scalar to C++ type
*/

template <typename T>
T scalar_to(const Scalar& s) {
if (s.isBoolean()) {
return static_cast<T>(s.to<bool>());
} else if (s.isFloatingPoint()) {
return static_cast<T>(s.to<double>());
} else {
return static_cast<T>(s.to<int64_t>());
}
}

template <>
inline double scalar_to<double>(const Scalar& s) {
return s.isFloatingPoint() ? s.to<double>()
: static_cast<double>(s.to<int64_t>());
}

template <>
inline int64_t scalar_to<int64_t>(const Scalar& s) {
return s.isFloatingPoint() ? static_cast<int64_t>(s.to<double>())
: s.to<int64_t>();
}

namespace internal {
/**
* Causes these utility functions to make sure to respect Tensor
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/util/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def define_common_targets():
"//executorch/runtime/core/portable_type/c10/c10:aten_headers_for_executorch",
"//executorch/runtime/kernel:kernel_runtime_context",
"//executorch/extension/threadpool:threadpool",
"//executorch/kernels/portable/cpu:scalar_utils",
],
deps = [
"//executorch/kernels/portable/cpu:scalar_utils",
"//executorch/runtime/kernel:kernel_includes",
],
visibility = ["//executorch/kernels/portable/cpu/...", "//executorch/kernels/optimized/cpu/...", "@EXECUTORCH_CLIENTS"],
Expand Down
Loading