Skip to content

Commit

Permalink
Add num_instructions program function
Browse files Browse the repository at this point in the history
Differential Revision: D66504180

Pull Request resolved: pytorch#7206
  • Loading branch information
dbort authored Dec 9, 2024
1 parent 4adbdce commit 29f5cac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions runtime/executor/method_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,25 @@ Result<int64_t> MethodMeta::memory_planned_buffer_size(size_t index) const {
return s_plan_->non_const_buffer_sizes()->Get(index + 1);
}

size_t MethodMeta::num_instructions() const {
const auto chains = s_plan_->chains();
if (chains == nullptr) {
return 0;
}
const auto num_chains = chains->size();
auto num_instructions = 0;
for (size_t i = 0; i < num_chains; ++i) {
auto s_chain = chains->Get(i);
if (s_chain == nullptr) {
continue;
}
auto s_instructions = s_chain->instructions();
if (s_instructions != nullptr) {
num_instructions += s_instructions->size();
}
}
return num_instructions;
}

} // namespace runtime
} // namespace executorch
7 changes: 7 additions & 0 deletions runtime/executor/method_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ class MethodMeta final {
*/
Result<int64_t> memory_planned_buffer_size(size_t index) const;

/**
* Get the number of instructions in this method.
*
* @returns The number of instructions.
*/
ET_EXPERIMENTAL size_t num_instructions() const;

/**
* DEPRECATED: Use num_memory_planned_buffers() instead.
*/
Expand Down
3 changes: 3 additions & 0 deletions runtime/executor/test/method_meta_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ TEST_F(MethodMetaTest, MethodMetaApi) {
method_meta->non_const_buffer_size(1).error(),
Error::InvalidArgument); // Deprecated API

// Number instructions in method is nonzero
EXPECT_NE(method_meta->num_instructions(), 0);

// Missing method fails
EXPECT_EQ(
program_->method_meta("not_a_method").error(), Error::InvalidArgument);
Expand Down

0 comments on commit 29f5cac

Please sign in to comment.