Skip to content

[executorch] Migrate exec_aten utils to new namespace #4609

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
Aug 20, 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
3 changes: 0 additions & 3 deletions extension/aten_util/make_aten_functor_from_et_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
namespace torch {
namespace executor {

class KernelRuntimeContext; // Forward declaration
using RuntimeContext = KernelRuntimeContext; // TODO(T147221312): Remove

// Map types from ETen to ATen.
// This is used to convert ETen arguments into ATen.
template <typename T>
Expand Down
24 changes: 15 additions & 9 deletions extension/kernel_util/make_boxed_from_unboxed_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
/// Example usage:
/// ```
/// Tensor&
/// my_op(RuntimeContext& ctx, const Tensor& self, const Tensor& other, Tensor&
/// out) {
/// my_op(KernelRuntimeContext& ctx, const Tensor& self, const Tensor& other,
/// Tensor& out)
/// {
/// // ...
/// return out;
/// }
Expand Down Expand Up @@ -47,12 +48,15 @@
#include <type_traits>
#include <typeinfo>

namespace executorch {
namespace runtime {
class KernelRuntimeContext; // Forward declaration
} // namespace runtime
} // namespace executorch

namespace torch {
namespace executor {

class KernelRuntimeContext; // Forward declaration
using RuntimeContext = KernelRuntimeContext; // TODO(T147221312): Remove

// evalue_to_arg
template <class T>
struct decay_if_not_tensor final {
Expand Down Expand Up @@ -106,7 +110,7 @@ struct evalue_to_arg<exec_aten::ArrayRef<exec_aten::optional<T>>> final {

template <class Functor, size_t... evalue_arg_indices, typename... ArgTypes>
void call_functor_with_args_from_stack_(
RuntimeContext& ctx,
::executorch::runtime::KernelRuntimeContext& ctx,
EValue** stack,
std::index_sequence<evalue_arg_indices...>,
typelist<ArgTypes...>*) {
Expand All @@ -129,16 +133,18 @@ struct WrapUnboxedIntoFunctor {
using TrueType = typename FuncType::FuncType;
using ReturnType = typename infer_function_traits_t<TrueType>::return_type;
using ArgsType = typename infer_function_traits_t<TrueType>::parameter_types;
// check if the first argument is RuntimeContext, if so, remove it
// check if the first argument is KernelRuntimeContext, if so, remove it
static constexpr bool first_arg_is_context = std::is_same<
RuntimeContext,
::executorch::runtime::KernelRuntimeContext,
std::remove_reference_t<head_with_default_t<void, ArgsType>>>::value;
using ContextRemovedArgsType = std::conditional_t<
first_arg_is_context,
drop_if_nonempty_t<ArgsType, 1>,
ArgsType>;

static void call(RuntimeContext& ctx, EValue** stack) {
static void call(
::executorch::runtime::KernelRuntimeContext& ctx,
EValue** stack) {
constexpr size_t num_inputs = size<ContextRemovedArgsType>::value;
return call_functor_with_args_from_stack_<FuncType>(
ctx,
Expand Down
12 changes: 10 additions & 2 deletions runtime/backend/backend_execution_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <executorch/runtime/core/event_tracer.h>
#include <executorch/runtime/core/memory_allocator.h>

namespace torch {
namespace executor {
namespace executorch {
namespace runtime {

/**
* BackendExecutionContext will be used to inject run time context.
Expand Down Expand Up @@ -57,5 +57,13 @@ class BackendExecutionContext final {
MemoryAllocator* temp_allocator_ = nullptr;
};

} // namespace runtime
} // namespace executorch

namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::runtime::BackendExecutionContext;
} // namespace executor
} // namespace torch
12 changes: 10 additions & 2 deletions runtime/backend/backend_init_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#pragma once
#include <executorch/runtime/core/memory_allocator.h>

namespace torch {
namespace executor {
namespace executorch {
namespace runtime {

/**
* BackendInitContext will be used to inject runtime info for to initialize
Expand All @@ -33,5 +33,13 @@ class BackendInitContext final {
MemoryAllocator* runtime_allocator_ = nullptr;
};

} // namespace runtime
} // namespace executorch

namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::runtime::BackendInitContext;
} // namespace executor
} // namespace torch
10 changes: 5 additions & 5 deletions runtime/backend/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include <executorch/runtime/backend/interface.h>
#include <executorch/runtime/platform/assert.h>

namespace torch {
namespace executor {
namespace executorch {
namespace runtime {

PyTorchBackendInterface::~PyTorchBackendInterface() {}

// Task t128866626: Remove global static variables.
// TODO(T128866626): Remove global static variables.
// We want to be able to run multiple Executor instances
// and having a global registration isn't a viable solution
// in the long term.
Expand Down Expand Up @@ -56,5 +56,5 @@ Error BackendRegistry::register_backend(const Backend& backend) {
return Error::Ok;
}

} // namespace executor
} // namespace torch
} // namespace runtime
} // namespace executorch
20 changes: 18 additions & 2 deletions runtime/backend/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/platform/compiler.h>

namespace torch {
namespace executor {
namespace executorch {
namespace runtime {

struct SizedBuffer {
void* buffer;
Expand Down Expand Up @@ -170,5 +170,21 @@ PyTorchBackendInterface* get_backend_class(const char* name);
*/
ET_NODISCARD Error register_backend(const Backend& backend);

} // namespace runtime
} // namespace executorch

namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::runtime::Backend;
using ::executorch::runtime::BackendRegistry;
using ::executorch::runtime::CompileSpec;
using ::executorch::runtime::DelegateHandle;
using ::executorch::runtime::get_backend_class;
// using ::executorch::runtime::kRegistrationTableMaxSize;
using ::executorch::runtime::PyTorchBackendInterface;
using ::executorch::runtime::register_backend;
using ::executorch::runtime::SizedBuffer;
} // namespace executor
} // namespace torch
50 changes: 32 additions & 18 deletions runtime/core/exec_aten/testing_util/tensor_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include <vector>
#endif // !USE_ATEN_LIB

namespace torch {
namespace executor {
namespace executorch {
namespace runtime {
namespace testing {

namespace internal {
Expand Down Expand Up @@ -269,7 +269,7 @@ class TensorFactory {
data.size(),
expected_numel);

Tensor t;
at::Tensor t;
if (strides.empty()) {
t = zeros(sizes);
} else {
Expand Down Expand Up @@ -311,7 +311,7 @@ class TensorFactory {
data.size(),
expected_numel);

Tensor t;
at::Tensor t;
if (dim_order.empty()) {
t = zeros(sizes);
} else {
Expand Down Expand Up @@ -417,7 +417,7 @@ class TensorFactory {
* @return A new Tensor with the specified shape.
*/
at::Tensor zeros_like(
const Tensor& input,
const at::Tensor& input,
ET_UNUSED TensorShapeDynamism dynamism =
TensorShapeDynamism::DYNAMIC_UNBOUND) {
std::vector<int64_t> sizes64 = {input.sizes().begin(), input.sizes().end()};
Expand All @@ -432,7 +432,7 @@ class TensorFactory {
* @return A new Tensor with the specified shape.
*/
at::Tensor ones_like(
const Tensor& input,
const at::Tensor& input,
ET_UNUSED TensorShapeDynamism dynamism =
TensorShapeDynamism::DYNAMIC_UNBOUND) {
std::vector<int64_t> sizes64 = {input.sizes().begin(), input.sizes().end()};
Expand Down Expand Up @@ -564,7 +564,8 @@ namespace internal {
// values while using the defaults for everything else.
template <torch::executor::ScalarType DTYPE>
struct ScalarTypeToCppTypeWrapper {
using ctype = typename torch::executor::ScalarTypeToCppType<DTYPE>::type;
using ctype =
typename ::executorch::runtime::ScalarTypeToCppType<DTYPE>::type;
};

// Use a C type of `uint8_t` instead of `bool`. The C type will be used to
Expand Down Expand Up @@ -830,7 +831,7 @@ class TensorFactory {
* @return A new Tensor with the specified shape.
*/
torch::executor::Tensor zeros_like(
const Tensor& input,
const torch::executor::Tensor& input,
TensorShapeDynamism dynamism = TensorShapeDynamism::STATIC) {
std::vector<int32_t> sizes = {input.sizes().begin(), input.sizes().end()};
return full(sizes, 0, dynamism);
Expand All @@ -844,7 +845,7 @@ class TensorFactory {
* @return A new Tensor with the specified shape.
*/
torch::executor::Tensor ones_like(
const Tensor& input,
const torch::executor::Tensor& input,
TensorShapeDynamism dynamism = TensorShapeDynamism::STATIC) {
std::vector<int32_t> sizes = {input.sizes().begin(), input.sizes().end()};
return full(sizes, 1, dynamism);
Expand Down Expand Up @@ -878,7 +879,7 @@ class TensorFactory {
std::vector<ctype> data_;
std::vector<uint8_t> dim_order_;
std::vector<int32_t> strides_;
TensorImpl impl_;
torch::executor::TensorImpl impl_;
};

/**
Expand All @@ -898,7 +899,7 @@ class TensorFactory {
* (and Tensors they contain), and must live longer than those TensorLists and
* Tensors.
*/
template <ScalarType DTYPE>
template <exec_aten::ScalarType DTYPE>
class TensorListFactory final {
public:
TensorListFactory() = default;
Expand All @@ -909,13 +910,15 @@ class TensorListFactory final {
* provided Tensors, but filled with zero elements. The dtypes of the template
* entries are ignored.
*/
TensorList zeros_like(const std::vector<Tensor>& templates) {
memory_.emplace_back(std::make_unique<std::vector<Tensor>>());
exec_aten::TensorList zeros_like(
const std::vector<exec_aten::Tensor>& templates) {
memory_.emplace_back(std::make_unique<std::vector<exec_aten::Tensor>>());
auto& vec = memory_.back();
std::for_each(templates.begin(), templates.end(), [&](const Tensor& t) {
vec->push_back(tf_.zeros_like(t));
});
return TensorList(vec->data(), vec->size());
std::for_each(
templates.begin(), templates.end(), [&](const exec_aten::Tensor& t) {
vec->push_back(tf_.zeros_like(t));
});
return exec_aten::TensorList(vec->data(), vec->size());
}

private:
Expand All @@ -925,9 +928,20 @@ class TensorListFactory final {
* vector of pointers so that the elements won't move if the vector needs to
* resize/realloc.
*/
std::vector<std::unique_ptr<std::vector<Tensor>>> memory_;
std::vector<std::unique_ptr<std::vector<exec_aten::Tensor>>> memory_;
};

} // namespace testing
} // namespace runtime
} // namespace executorch

namespace torch {
namespace executor {
namespace testing {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::runtime::testing::TensorFactory;
using ::executorch::runtime::testing::TensorListFactory;
} // namespace testing
} // namespace executor
} // namespace torch
17 changes: 13 additions & 4 deletions runtime/core/exec_aten/testing_util/tensor_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
using exec_aten::ScalarType;
using exec_aten::Tensor;

namespace torch {
namespace executor {
namespace executorch {
namespace runtime {
namespace testing {

namespace {
Expand Down Expand Up @@ -175,10 +175,19 @@ bool tensor_lists_are_close(
}

} // namespace testing
} // namespace runtime
} // namespace executorch

// ATen already defines operator<<() for Tensor and ScalarType.
#ifndef USE_ATEN_LIB

/*
* These functions must be declared in the original namespaces of their
* associated types so that C++ can find them.
*/
namespace torch {
namespace executor {

/**
* Prints the ScalarType to the stream as a human-readable string.
*/
Expand Down Expand Up @@ -266,7 +275,7 @@ std::ostream& operator<<(std::ostream& os, const Tensor& t) {
return os;
}

#endif // !USE_ATEN_LIB

} // namespace executor
} // namespace torch

#endif // !USE_ATEN_LIB
Loading
Loading