Skip to content

add begin/end to ExecuTorch pytree::arr #7653

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 6 commits into from
Feb 7, 2025
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 extension/pytree/aten_util/ivalue_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ std::pair<std::vector<at::Tensor>, std::unique_ptr<TreeSpec<Empty>>> flatten(
auto p = flatten(c);

std::vector<at::Tensor> tensors;
for (int i = 0; i < p.first.size(); ++i) {
tensors.emplace_back(p.first[i]->toTensor());
for (const auto& item : p.first) {
tensors.emplace_back(item->toTensor());
}

return {tensors, std::move(p.second)};
Expand Down
16 changes: 16 additions & 0 deletions extension/pytree/pytree.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,22 @@ struct arr {
return data_.get();
}

T* begin() {
return data_.get();
}

T* end() {
return begin() + size();
}

const T* begin() const {
return data_.get();
}

const T* end() const {
return begin() + size();
}

inline size_t size() const {
return n_;
}
Expand Down
14 changes: 14 additions & 0 deletions extension/pytree/test/test_pytree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@
#include <gtest/gtest.h>
#include <string>

using ::executorch::extension::pytree::arr;
using ::executorch::extension::pytree::ContainerHandle;
using ::executorch::extension::pytree::Key;
using ::executorch::extension::pytree::Kind;
using ::executorch::extension::pytree::unflatten;

using Leaf = int32_t;

TEST(PyTreeTest, ArrBasic) {
arr<int> x(5);
ASSERT_EQ(x.size(), 5);
for (int ii = 0; ii < x.size(); ++ii) {
x[ii] = 2 * ii;
}
int idx = 0;
for (const auto item : x) {
EXPECT_EQ(item, 2 * idx);
++idx;
}
}

TEST(PyTreeTest, List) {
Leaf items[2] = {11, 12};
std::string spec = "L2#1#1($,$)";
Expand Down
Loading