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
4 changes: 2 additions & 2 deletions .github/workflows/ci_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copy of executorch/requirements-dev.txt as of v1.3.1
# Copy of executorch/requirements-dev.txt as of v1.4.0

cmake>=3.29, <4.0.0 # For building binary targets in the wheel.
packaging>=24.2 # Lower bound required by setuptools
Expand All @@ -10,4 +10,4 @@ zstd # Imported by resolve_buck.py.
certifi # Imported by resolve_buck.py.
lintrunner==0.12.7
lintrunner-adapters==0.13.0
torch==2.12.0
torch==2.13.0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ See `example/hello_world` for a complete example.
## Build
To use the library you must compile the C++ executorch library yourself, as there are many configurations that
determines which modules, backends, and operations are supported. See the `executorch-sys` crate for more info.
Currently the supported Cpp executorch version is `1.3.1`.
Currently the supported Cpp executorch version is `1.4.0`.

The table below shows which version of the Rust crate is compatible with which C++ ExecuTorch version:

Expand Down
6 changes: 1 addition & 5 deletions etc/download_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def main():
"extension/data_loader/mman.h",
"extension/data_loader/mman_windows.h",
"extension/module/bundled_module.h", # TODO
"runtime/core/device_allocator.h", # TODO
"runtime/core/device_memory_buffer.h", # TODO
"extension/flat_tensor/serialize/serialize.h",
"**/test/**",
"**/testing_util/**",
Expand All @@ -74,9 +72,7 @@ def main():
for include in includes:
files.update(glob.glob(include, root_dir=tmp_headers_dir, recursive=True))
for exclude in excludes:
files.difference_update(
glob.glob(exclude, root_dir=tmp_headers_dir, recursive=True)
)
files.difference_update(glob.glob(exclude, root_dir=tmp_headers_dir, recursive=True))
for f in files:
dst = HEADERS_DIR / f
if not dst.parent.exists():
Expand Down
20 changes: 6 additions & 14 deletions etc/setup_dev_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import warnings
from pathlib import Path

DEV_EXECUTORCH_DIR = (
Path(__file__).parent.parent.resolve() / "etc" / ".dev-env" / "executorch"
)
DEV_EXECUTORCH_DIR = Path(__file__).parent.parent.resolve() / "etc" / ".dev-env" / "executorch"


def main():
Expand Down Expand Up @@ -49,16 +47,14 @@ def main():
"install",
"-r",
DEV_EXECUTORCH_DIR / "requirements-dev.txt",
"torch==2.12.0",
"torch==2.13.0",
"--extra-index-url",
"https://download.pytorch.org/whl/test/cpu",
]
)
build_executorch_with_dev_cfg()

subprocess.check_call(
[sys.executable, "-m", "pip", "install", "huggingface_hub[cli]"]
)
subprocess.check_call([sys.executable, "-m", "pip", "install", "huggingface_hub[cli]"])


def clone_executorch():
Expand All @@ -71,7 +67,7 @@ def clone_executorch():
"--depth",
"1",
"--branch",
"v1.3.1",
"v1.4.0",
"https://github.com/pytorch/executorch.git",
".",
],
Expand All @@ -94,12 +90,8 @@ def clone_executorch():
cwd=DEV_EXECUTORCH_DIR / "backends" / "apple" / "coreml" / "scripts",
)

subprocess.check_call(
["git", "submodule", "update", "--init", "--recursive"], cwd=DEV_EXECUTORCH_DIR
)
subprocess.check_call(
["git", "submodule", "sync", "--recursive"], cwd=DEV_EXECUTORCH_DIR
)
subprocess.check_call(["git", "submodule", "update", "--init", "--recursive"], cwd=DEV_EXECUTORCH_DIR)
subprocess.check_call(["git", "submodule", "sync", "--recursive"], cwd=DEV_EXECUTORCH_DIR)


def build_executorch_with_dev_cfg():
Expand Down
2 changes: 1 addition & 1 deletion examples/llama3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ To run the example, follow these steps (note that some steps should run from the
The llama model can be exported with many options, such as quantization, different data types (f32, bf16), different backends, kv caching, etc.
This example use a specific set of options, as specified above.
Different options require different export commands and modifications to the code and build script which you can play around with.
See the [llama README](https://github.com/pytorch/executorch/blob/v1.3.1/examples/models/llama/README.md) at the Cpp executorch repository for more details.
See the [llama README](https://github.com/pytorch/executorch/blob/v1.4.0/examples/models/llama/README.md) at the Cpp executorch repository for more details.
4 changes: 2 additions & 2 deletions executorch-sys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ For a general description of the project, see the the `executorch` crate.

## Build
To build the library, you need to build the C++ library yourself first.
Currently the supported Cpp executorch version is `1.3.1`.
Currently the supported Cpp executorch version is `1.4.0`.
The C++ library allow for great flexibility with many flags, customizing which modules, kernels, and extensions are built.
Multiple static libraries are built, and the Rust library links to them.
In the following example we build the C++ library with the necessary flags to run example `hello_world`:
```bash
# Clone the C++ library
cd ${EXECUTORCH_CPP_DIR}
git clone --depth 1 --branch v1.3.1 https://github.com/pytorch/executorch.git .
git clone --depth 1 --branch v1.4.0 https://github.com/pytorch/executorch.git .
git submodule sync --recursive
git submodule update --init --recursive

Expand Down
2 changes: 1 addition & 1 deletion executorch-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

// const EXECUTORCH_VERSION: &str = "1.3.1";
// const EXECUTORCH_VERSION: &str = "1.4.0";

fn main() {
// TODO: verify on runtime we use the correct version of executorch
Expand Down
53 changes: 52 additions & 1 deletion executorch-sys/cpp/executorch_rs/c_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ namespace

static_assert(is_equal_layout<struct ET_Device, executorch::runtime::etensor::Device>());
static_assert(std::is_trivially_move_constructible_v<executorch::runtime::etensor::Device>);
static_assert(is_equal_layout<enum ET_DeviceType, executorch::runtime::etensor::DeviceType>());
static_assert(std::is_trivially_move_constructible_v<executorch::runtime::etensor::DeviceType>);
static_assert(is_equal_layout<int8_t, executorch::runtime::etensor::DeviceIndex>());
static_assert(std::is_trivially_move_constructible_v<executorch::runtime::etensor::DeviceIndex>);

static_assert(is_equal_layout<struct ET_BackendOption, executorch::runtime::BackendOption>());
static_assert(std::is_trivially_move_constructible_v<executorch::runtime::BackendOption>);
Expand Down Expand Up @@ -129,6 +133,7 @@ namespace
//
// static_assert(std::is_trivially_move_constructible_v<executorch::runtime::MemoryAllocator>);

static_assert(is_equal_layout<struct ET_SpanDevice, executorch::runtime::Span<const executorch::runtime::etensor::Device>>());
static_assert(is_equal_layout<struct ET_HierarchicalAllocator, executorch::runtime::HierarchicalAllocator>());
static_assert(std::is_trivially_move_constructible_v<executorch::runtime::HierarchicalAllocator>);

Expand Down Expand Up @@ -200,6 +205,16 @@ void *executorch_MemoryAllocator_allocate(struct ET_MemoryAllocator *self, size_
auto self_ = checked_reinterpret_cast<executorch::runtime::MemoryAllocator>(self);
return self_->allocate(size, alignment);
}
size_t executorch_MemoryAllocator_used_size(const struct ET_MemoryAllocator *self)
{
auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryAllocator>(self);
return self_->used_size();
}
size_t executorch_MemoryAllocator_free_size(const struct ET_MemoryAllocator *self)
{
auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryAllocator>(self);
return self_->free_size();
}
struct ET_HierarchicalAllocator executorch_HierarchicalAllocator_new(struct ET_SpanSpanU8 buffers)
{
auto buffers_ = *checked_reinterpret_cast<executorch::runtime::Span<executorch::runtime::Span<uint8_t>>>(&buffers);
Expand All @@ -210,6 +225,28 @@ struct ET_HierarchicalAllocator executorch_HierarchicalAllocator_new(struct ET_S
new (self_) executorch::runtime::HierarchicalAllocator(buffers_);
return self;
}
struct ET_HierarchicalAllocator executorch_HierarchicalAllocator_new_with_devices(struct ET_SpanSpanU8 buffers, struct ET_SpanDevice planned_buffer_devices)
{
auto buffers_ = *checked_reinterpret_cast<executorch::runtime::Span<executorch::runtime::Span<uint8_t>>>(&buffers);
ET_CHECK((void *)buffers_.begin() == (void *)buffers.data);
ET_CHECK(buffers_.size() == buffers.len);
auto devices_ = *checked_reinterpret_cast<executorch::runtime::Span<const executorch::runtime::etensor::Device>>(&planned_buffer_devices);
ET_CHECK((const void *)devices_.begin() == (const void *)planned_buffer_devices.data);
ET_CHECK(devices_.size() == planned_buffer_devices.len);
struct ET_HierarchicalAllocator self;
auto self_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(&self);
new (self_) executorch::runtime::HierarchicalAllocator(buffers_, devices_);
return self;
}
struct ET_SpanDevice executorch_HierarchicalAllocator_planned_buffer_devices(const struct ET_HierarchicalAllocator *self)
{
auto self_ = checked_reinterpret_cast<const executorch::runtime::HierarchicalAllocator>(self);
auto devices = self_->planned_buffer_devices();
return ET_SpanDevice{
.data = reinterpret_cast<const struct ET_Device *>(devices.data()),
.len = devices.size(),
};
}
void executorch_HierarchicalAllocator_destructor(struct ET_HierarchicalAllocator *self)
{
auto self_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(self);
Expand All @@ -229,6 +266,20 @@ struct ET_MemoryManager executorch_MemoryManager_new(
new (self_) executorch::runtime::MemoryManager(method_allocator_, planned_memory_, temp_allocator_);
return self;
}
struct ET_SpanDevice executorch_MemoryManager_planned_buffer_devices(const struct ET_MemoryManager *self)
{
auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryManager>(self);
auto devices = self_->planned_buffer_devices();
return ET_SpanDevice{
.data = reinterpret_cast<const struct ET_Device *>(devices.data()),
.len = devices.size(),
};
}
bool executorch_MemoryManager_has_device_memory(const struct ET_MemoryManager *self)
{
auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryManager>(self);
return self_->has_device_memory();
}

// Loaders
static executorch::runtime::DataLoader *cast_data_loader_mut(struct ET_DataLoaderRefMut loader)
Expand Down Expand Up @@ -448,7 +499,7 @@ enum ET_ScalarType executorch_Tensor_scalar_type(struct ET_TensorRef self)
struct ET_Device executorch_Tensor_device(struct ET_TensorRef self)
{
auto self_ = cast_tensor(self);
auto d = self_->unsafeGetTensorImpl()->device();
auto d = self_->device();
return ET_Device{static_cast<ET_DeviceType>(d.type()), static_cast<int8_t>(d.index())};
}
size_t executorch_Tensor_element_size(struct ET_TensorRef self)
Expand Down
14 changes: 13 additions & 1 deletion executorch-sys/cpp/executorch_rs/c_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ extern "C"
};
struct ET_HierarchicalAllocator
{
size_t _blob[34];
size_t _blob[36];
};
struct ET_MemoryManager
{
Expand Down Expand Up @@ -631,6 +631,11 @@ extern "C"
uint8_t *data;
size_t len;
};
struct ET_SpanDevice
{
const struct ET_Device *data;
size_t len;
};
struct ET_SpanSpanU8
{
struct ET_SpanU8 *data;
Expand Down Expand Up @@ -712,12 +717,19 @@ extern "C"

struct ET_MemoryAllocator executorch_MemoryAllocator_new(uint32_t size, uint8_t *base_address);
void *executorch_MemoryAllocator_allocate(struct ET_MemoryAllocator *self, size_t size, size_t alignment);
size_t executorch_MemoryAllocator_used_size(const struct ET_MemoryAllocator *self);
size_t executorch_MemoryAllocator_free_size(const struct ET_MemoryAllocator *self);
struct ET_HierarchicalAllocator executorch_HierarchicalAllocator_new(struct ET_SpanSpanU8 buffers);
struct ET_HierarchicalAllocator executorch_HierarchicalAllocator_new_with_devices(struct ET_SpanSpanU8 buffers, struct ET_SpanDevice planned_buffer_devices);
struct ET_SpanDevice executorch_HierarchicalAllocator_planned_buffer_devices(
const struct ET_HierarchicalAllocator *self);
void executorch_HierarchicalAllocator_destructor(struct ET_HierarchicalAllocator *self);
struct ET_MemoryManager executorch_MemoryManager_new(
struct ET_MemoryAllocator *method_allocator,
struct ET_HierarchicalAllocator *planned_memory,
struct ET_MemoryAllocator *temp_allocator);
struct ET_SpanDevice executorch_MemoryManager_planned_buffer_devices(const struct ET_MemoryManager *self);
bool executorch_MemoryManager_has_device_memory(const struct ET_MemoryManager *self);

// Loaders
struct ET_BufferDataLoader executorch_BufferDataLoader_new(const void *data, size_t size);
Expand Down
19 changes: 17 additions & 2 deletions executorch-sys/cpp/executorch_rs/cxx_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ namespace executorch_rs
std::unique_ptr<std::vector<int32_t>> strides,
ET_ScalarType scalar_type,
ET_TensorShapeDynamism dynamism,
rust::Box<executorch_rs::cxx_util::RustAny> allocation)
rust::Box<executorch_rs::cxx_util::RustAny> allocation,
struct ET_Device device)
{
// std::function must be copyable, so we need to wrap the allocation in a shared_ptr
std::shared_ptr<rust::Box<executorch_rs::cxx_util::RustAny>> allocation_ptr =
Expand All @@ -83,7 +84,10 @@ namespace executorch_rs
std::move(*strides),
static_cast<executorch::aten::ScalarType>(scalar_type),
static_cast<executorch::aten::TensorShapeDynamism>(dynamism),
[allocation_ptr = allocation_ptr](void *) mutable {});
[allocation_ptr = allocation_ptr](void *) mutable {},
executorch::aten::Device(
static_cast<executorch::aten::DeviceType>(device.type),
device.index));
}

std::shared_ptr<executorch::aten::Tensor> TensorPtr_clone(
Expand All @@ -93,6 +97,17 @@ namespace executorch_rs
return executorch::extension::clone_tensor_ptr(
tensor, static_cast<executorch::aten::ScalarType>(scalar_type));
}

std::shared_ptr<executorch::aten::Tensor> TensorPtr_clone_to(
std::shared_ptr<executorch::aten::Tensor> tensor,
struct ET_Device device)
{
return executorch::extension::clone_tensor_ptr_to(
tensor,
executorch::aten::Device(
static_cast<executorch::aten::DeviceType>(device.type),
device.index));
}
#endif

#if defined(EXECUTORCH_RS_MODULE)
Expand Down
6 changes: 5 additions & 1 deletion executorch-sys/cpp/executorch_rs/cxx_bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ namespace executorch_rs
std::unique_ptr<std::vector<int32_t>> strides,
ET_ScalarType scalar_type,
ET_TensorShapeDynamism dynamism,
rust::Box<executorch_rs::cxx_util::RustAny> allocation);
rust::Box<executorch_rs::cxx_util::RustAny> allocation,
struct ET_Device device);
std::shared_ptr<executorch::aten::Tensor> TensorPtr_clone(
const executorch::aten::Tensor &tensor,
ET_ScalarType scalar_type);
std::shared_ptr<executorch::aten::Tensor> TensorPtr_clone_to(
std::shared_ptr<executorch::aten::Tensor> tensor,
struct ET_Device device);
#endif

#if defined(EXECUTORCH_RS_MODULE)
Expand Down
1 change: 1 addition & 0 deletions executorch-sys/src/c_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl_ref_clone_copy!(ET_ArrayRefU8);
impl_ref_clone_copy!(ET_ArrayRefUsizeType);

// Span
impl_ref_clone_copy!(ET_SpanDevice);
impl_ref_clone_copy!(ET_SpanI64);
impl_ref_clone_copy!(ET_SpanOptionalTensor);
impl_ref_clone_copy!(ET_SpanSpanU8);
Expand Down
5 changes: 5 additions & 0 deletions executorch-sys/src/cxx_bridge/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ unsafe impl ExternType for crate::ET_MemoryAllocator {
type Id = type_id!("ET_MemoryAllocator");
type Kind = cxx::kind::Opaque;
}

unsafe impl ExternType for crate::ET_Device {
type Id = type_id!("ET_Device");
type Kind = cxx::kind::Trivial;
}
2 changes: 1 addition & 1 deletion executorch-sys/src/cxx_bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub use module::ffi::{
#[cfg(feature = "tensor-ptr")]
pub(crate) mod tensor_ptr;
#[cfg(feature = "tensor-ptr")]
pub use tensor_ptr::ffi::{Tensor, TensorPtr_clone, TensorPtr_new};
pub use tensor_ptr::ffi::{Tensor, TensorPtr_clone, TensorPtr_clone_to, TensorPtr_new};
Loading
Loading