Skip to content

Commit 240fc77

Browse files
dsharletgxnnpack-bot
authored andcommitted
Add ynn_subgraph_optimize
Ultimately, we should not call this automatically from `ynn_create_runtime`, but it is still there for now, so we can replace the call in client uses of YNNPACK. PiperOrigin-RevId: 822833148
1 parent 49192a1 commit 240fc77

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

ynnpack/include/ynnpack.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ enum ynn_status {
3535

3636
typedef struct ynn_subgraph* ynn_subgraph_t;
3737

38+
// This type is an alias for `slinky::thread_pool`. A `slinky::thread_pool`
39+
// instance may be casted to `ynn_threadpool` and passed to YNNPACK APIs.
40+
typedef struct ynn_threadpool* ynn_threadpool_t;
41+
3842
// Create a new subgraph, with `external_value_ids` reserved ids for external
3943
// values.
4044
enum ynn_status ynn_create_subgraph(uint32_t external_value_ids, uint32_t flags,
@@ -43,6 +47,10 @@ enum ynn_status ynn_create_subgraph(uint32_t external_value_ids, uint32_t flags,
4347
// Delete a subgraph previously created with `ynn_create_subgraph`.
4448
void ynn_delete_subgraph(ynn_subgraph_t subgraph);
4549

50+
// Apply subgraph rewrites and other optimizations to the subgraph.
51+
ynn_status ynn_optimize_subgraph(ynn_subgraph_t subgraph,
52+
ynn_threadpool_t threadpool, uint32_t flags);
53+
4654
// Describes a type for a value.
4755
enum ynn_type {
4856
ynn_type_invalid = -1,
@@ -398,10 +406,6 @@ struct ynn_scheduler {
398406

399407
typedef const struct ynn_scheduler* ynn_scheduler_t;
400408

401-
// This type is an alias for `slinky::thread_pool`. A `slinky::thread_pool`
402-
// instance may be casted to `ynn_threadpool` and passed to YNNPACK APIs.
403-
typedef struct ynn_threadpool* ynn_threadpool_t;
404-
405409
// Create a threadpool that uses `scheduler` to start work on other threads.
406410
// `scheduler` is not copied, since it is stateless we expect it to be stored
407411
// globally.

ynnpack/subgraph/runtime.cc

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -640,23 +640,14 @@ ynn_status ynn_runtime::setup() {
640640
ynn_status ynn_create_runtime(ynn_subgraph_t subgraph,
641641
ynn_threadpool_t threadpool, uint32_t flags,
642642
ynn_runtime_t* runtime_out) {
643-
#if YNN_LOG_LEVEL >= YNN_LOG_LEVEL_DEBUG
644-
YNN_LOG_DEBUG() << "subgraph before optimization:\n";
645-
subgraph->dump(std::cout);
646-
#endif
647-
648-
slinky::thread_pool* slinky_threadpool =
649-
reinterpret_cast<slinky::thread_pool*>(threadpool);
650-
ynn_status status = subgraph->optimize(slinky_threadpool);
643+
// TODO(b/454450773): Don't call this here.
644+
ynn_status status = ynn_optimize_subgraph(subgraph, threadpool, 0);
651645
if (status != ynn_status_success) {
652646
return status;
653647
}
654648

655-
#if YNN_LOG_LEVEL >= YNN_LOG_LEVEL_DEBUG
656-
YNN_LOG_DEBUG() << "subgraph after optimization:\n";
657-
subgraph->dump(std::cout);
658-
#endif
659-
649+
slinky::thread_pool* slinky_threadpool =
650+
reinterpret_cast<slinky::thread_pool*>(threadpool);
660651
std::unique_ptr<ynn_runtime> runtime(
661652
new ynn_runtime(*subgraph, slinky_threadpool, flags));
662653
status = runtime->build();

ynnpack/subgraph/subgraph.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,28 @@ ynn_status ynn_create_subgraph(uint32_t external_value_ids, uint32_t flags,
806806
return ynn_status_success;
807807
}
808808

809+
ynn_status ynn_optimize_subgraph(ynn_subgraph_t subgraph,
810+
ynn_threadpool_t threadpool, uint32_t flags) {
811+
#if YNN_LOG_LEVEL >= YNN_LOG_LEVEL_DEBUG
812+
YNN_LOG_DEBUG() << "subgraph before optimization:\n";
813+
subgraph->dump(std::cout);
814+
#endif
815+
816+
slinky::thread_pool* slinky_threadpool =
817+
reinterpret_cast<slinky::thread_pool*>(threadpool);
818+
ynn_status status = subgraph->optimize(slinky_threadpool);
819+
if (status != ynn_status_success) {
820+
return status;
821+
}
822+
823+
#if YNN_LOG_LEVEL >= YNN_LOG_LEVEL_DEBUG
824+
YNN_LOG_DEBUG() << "subgraph after optimization:\n";
825+
subgraph->dump(std::cout);
826+
#endif
827+
828+
return ynn_status_success;
829+
}
830+
809831
void ynn_delete_subgraph(ynn_subgraph_t subgraph) { delete subgraph; }
810832

811833
} // extern "C"

ynnpack/subgraph/test/subgraph_builder.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ Runtime::Runtime(ynn_subgraph_t subgraph, TestScheduler* scheduler,
290290
std::unique_ptr<ynn_threadpool, decltype(&ynn_delete_threadpool)>(
291291
threadpool, ynn_delete_threadpool);
292292
}
293+
294+
status_ = ynn_optimize_subgraph(subgraph, threadpool_.get(), 0);
295+
if (status_ != ynn_status_success) {
296+
return;
297+
}
298+
293299
status_ = ynn_create_runtime(subgraph, threadpool_.get(), flags, &runtime);
294300
runtime_ = std::unique_ptr<ynn_runtime, decltype(&ynn_delete_runtime)>(
295301
runtime, ynn_delete_runtime);

ynnpack/xnnpack/runtime.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,17 @@ static xnn_status create_runtime_impl(xnn_subgraph_t subgraph,
122122
ynn_flags |= YNN_RUNTIME_FLAG_NO_SCHEDULE;
123123
}
124124

125+
ynn_threadpool_t ynn_threadpool =
126+
xnn_threadpool ? xnn_threadpool->ynn : nullptr;
127+
128+
ynn_status status =
129+
ynn_optimize_subgraph(subgraph->ynn, ynn_threadpool, ynn_flags);
130+
if (status != ynn_status_success) {
131+
return ynn::xnn_status_from_ynn(status);
132+
}
133+
125134
return ynn::xnn_status_from_ynn(ynn_create_runtime(
126-
subgraph->ynn, xnn_threadpool ? xnn_threadpool->ynn : nullptr, ynn_flags,
127-
(ynn_runtime_t*)runtime_out));
135+
subgraph->ynn, ynn_threadpool, ynn_flags, (ynn_runtime_t*)runtime_out));
128136
}
129137

130138
xnn_status xnn_create_runtime_with_threadpool(xnn_subgraph_t subgraph,

0 commit comments

Comments
 (0)