Skip to content

Expose the compute number of elements helper function. #5166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2024
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
8 changes: 8 additions & 0 deletions runtime/core/exec_aten/exec_aten.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ using IntArrayRef = at::IntArrayRef;
template <typename T>
using OptionalArrayRef = c10::OptionalArrayRef<T>;

inline ssize_t compute_numel(const SizesType* sizes, ssize_t dim) {
return static_cast<ssize_t>(
c10::multiply_integers(c10::ArrayRef<SizesType>(sizes, dim)));
}

#else // Use executor types

using Tensor = torch::executor::Tensor;
Expand Down Expand Up @@ -127,9 +132,12 @@ template <typename T>
using OptionalArrayRef =
torch::executor::optional<torch::executor::ArrayRef<T>>;

using torch::executor::compute_numel;

#endif // Use executor types

} // namespace exec_aten

namespace torch {
namespace executor {
using TensorList = exec_aten::TensorList;
Expand Down
13 changes: 11 additions & 2 deletions runtime/core/exec_aten/util/scalar_type_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <type_traits>

#include <executorch/runtime/platform/assert.h>

#ifdef USE_ATEN_LIB
// Note that a lot of the macros/functions defined in this ScalarTypeUtil.h file
// are also defined in c10/core/ScalarType.h, which is included via
Expand All @@ -39,14 +40,14 @@
namespace exec_aten {
using ScalarType = at::ScalarType;
}
#else
#else // !USE_ATEN_LIB
#include <executorch/runtime/core/portable_type/scalar_type.h>
#include <executorch/runtime/core/portable_type/string_view.h>
namespace exec_aten {
using ScalarType = torch::executor::ScalarType;
using string_view = torch::executor::string_view;
} // namespace exec_aten
#endif
#endif // USE_ATEN_LIB

namespace executorch {
namespace runtime {
Expand Down Expand Up @@ -1361,6 +1362,14 @@ inline exec_aten::ScalarType promoteTypes(
} // namespace runtime
} // namespace executorch

namespace exec_aten {
#ifdef USE_ATEN_LIB
using ::at::elementSize;
#else // USE_ATEN_LIB
using ::executorch::runtime::elementSize;
#endif // USE_ATEN_LIB
} // namespace exec_aten

namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
Expand Down
2 changes: 0 additions & 2 deletions runtime/core/portable_type/tensor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
namespace torch {
namespace executor {

namespace {
/**
* Compute the number of elements based on the sizes of a tensor.
*/
Expand All @@ -39,7 +38,6 @@ ssize_t compute_numel(const TensorImpl::SizesType* sizes, ssize_t dim) {
}
return numel;
}
} // namespace

TensorImpl::TensorImpl(
ScalarType type,
Expand Down
15 changes: 15 additions & 0 deletions runtime/core/portable_type/tensor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,20 @@ class TensorImpl {
const TensorShapeDynamism shape_dynamism_;
};

/**
* Compute the number of elements based on the sizes of a tensor.
*/
ssize_t compute_numel(
const ::torch::executor::TensorImpl::SizesType* sizes,
ssize_t dim);

} // namespace executor
} // namespace torch

namespace executorch {
namespace runtime {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using torch::executor::compute_numel;
} // namespace runtime
} // namespace executorch
Loading