Skip to content

Commit 35f45df

Browse files
YunanAZcopybara-github
authored andcommitted
Prepare compiler partition option and add flag parse/unparse helper for tooling usage.
LiteRT-PiperOrigin-RevId: 828691730
1 parent 0eef0fe commit 35f45df

13 files changed

+328
-59
lines changed

litert/c/options/litert_compiler_options.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "litert/core/cache/hash_util.h"
2424

2525
struct LiteRtCompilerOptionsT {
26+
LiteRtCompilerOptionsPartitionStrategy partition_strategy =
27+
kLiteRtCompilerOptionsPartitionStrategyDefault;
2628
bool dummy_option = false;
2729
};
2830

@@ -66,6 +68,26 @@ LiteRtStatus LiteRtFindCompilerOptions(
6668

6769
const char* LiteRtGetCompilerOptionsIdentifier() { return "litert_compiler"; }
6870

71+
LiteRtStatus LiteRtSetCompilerOptionsPartitionStrategy(
72+
LiteRtCompilerOptions options,
73+
LiteRtCompilerOptionsPartitionStrategy partition_strategy) {
74+
if (options == nullptr) {
75+
return kLiteRtStatusErrorInvalidArgument;
76+
}
77+
options->partition_strategy = partition_strategy;
78+
return kLiteRtStatusOk;
79+
}
80+
81+
LiteRtStatus LiteRtGetCompilerOptionsPartitionStrategy(
82+
LiteRtCompilerOptionsConst options,
83+
LiteRtCompilerOptionsPartitionStrategy* partition_strategy) {
84+
if (options == nullptr || partition_strategy == nullptr) {
85+
return kLiteRtStatusErrorInvalidArgument;
86+
}
87+
*partition_strategy = options->partition_strategy;
88+
return kLiteRtStatusOk;
89+
}
90+
6991
LiteRtStatus LiteRtSetDummyCompilerOptions(LiteRtCompilerOptions options,
7092
bool dummy_option) {
7193
if (options == nullptr) {

litert/c/options/litert_compiler_options.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ extern "C" {
2626

2727
LITERT_DEFINE_HANDLE(LiteRtCompilerOptions);
2828

29+
// Partition strategy for the compiler.
30+
//
31+
// Default:
32+
// Default partition strategy is to partition the graph based on the naive
33+
// cutting algorithm.
34+
//
35+
// Weakly connected (experimental feature):
36+
// Partitions the graph into weakly connected components, this may result in
37+
// incorrect partition where there exists a path from a selected to another,
38+
// where there is an unselected node in between. This feature is currently in
39+
// experimental stage.
40+
typedef enum LiteRtCompilerOptionsPartitionStrategy {
41+
kLiteRtCompilerOptionsPartitionStrategyDefault = 0,
42+
kLiteRtCompilerOptionsPartitionStrategyWeaklyConnected = 1,
43+
} LiteRtCompilerOptionsPartitionStrategy;
44+
2945
// Creates an opaque options object holding Compiler options.
3046
LiteRtStatus LiteRtCreateCompilerOptions(LiteRtOpaqueOptions* options);
3147

@@ -36,6 +52,15 @@ LiteRtStatus LiteRtFindCompilerOptions(LiteRtOpaqueOptions opaque_options,
3652
// Gets the identifier for Compiler options stored in opaque options.
3753
const char* LiteRtGetCompilerOptionsIdentifier();
3854

55+
// Sets the partition strategy for the compiler.
56+
LiteRtStatus LiteRtSetCompilerOptionsPartitionStrategy(
57+
LiteRtCompilerOptions options,
58+
LiteRtCompilerOptionsPartitionStrategy partition_strategy);
59+
60+
LiteRtStatus LiteRtGetCompilerOptionsPartitionStrategy(
61+
LiteRtCompilerOptionsConst options,
62+
LiteRtCompilerOptionsPartitionStrategy* partition_strategy);
63+
3964
// Dummy options for testing.
4065
LiteRtStatus LiteRtSetDummyCompilerOptions(LiteRtCompilerOptions options,
4166
bool dummy_option);

litert/c/options/litert_compiler_options_test.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ TEST(LiteRtCompilerOptionsTest, DummyOptions) {
5757
LiteRtDestroyOpaqueOptions(options);
5858
}
5959

60+
TEST(LiteRtCompilerOptionsTest, GetDefaultPartitionStrategy) {
61+
LiteRtOpaqueOptions options;
62+
LITERT_ASSERT_OK(LiteRtCreateCompilerOptions(&options));
63+
LiteRtCompilerOptions compiler_options;
64+
LITERT_ASSERT_OK(LiteRtFindCompilerOptions(options, &compiler_options));
65+
66+
LiteRtCompilerOptionsPartitionStrategy partition_strategy;
67+
LITERT_ASSERT_OK(LiteRtGetCompilerOptionsPartitionStrategy(
68+
compiler_options, &partition_strategy));
69+
EXPECT_EQ(partition_strategy, kLiteRtCompilerOptionsPartitionStrategyDefault);
70+
71+
LiteRtDestroyOpaqueOptions(options);
72+
}
73+
74+
TEST(LiteRtCompilerOptionsTest, SetAndGetWCPartitionStrategy) {
75+
LiteRtOpaqueOptions options;
76+
LITERT_ASSERT_OK(LiteRtCreateCompilerOptions(&options));
77+
LiteRtCompilerOptions compiler_options;
78+
LITERT_ASSERT_OK(LiteRtFindCompilerOptions(options, &compiler_options));
79+
80+
LiteRtCompilerOptionsPartitionStrategy partition_strategy;
81+
LITERT_ASSERT_OK(LiteRtSetCompilerOptionsPartitionStrategy(
82+
compiler_options,
83+
kLiteRtCompilerOptionsPartitionStrategyWeaklyConnected));
84+
LITERT_ASSERT_OK(LiteRtGetCompilerOptionsPartitionStrategy(
85+
compiler_options, &partition_strategy));
86+
EXPECT_EQ(partition_strategy,
87+
kLiteRtCompilerOptionsPartitionStrategyWeaklyConnected);
88+
89+
LiteRtDestroyOpaqueOptions(options);
90+
}
91+
6092
TEST(LiteRtCompilerOptionsTest, Hash) {
6193
LiteRtOpaqueOptions options1;
6294
LITERT_ASSERT_OK(LiteRtCreateCompilerOptions(&options1));
@@ -71,6 +103,9 @@ TEST(LiteRtCompilerOptionsTest, Hash) {
71103
LiteRtCompilerOptions compiler_options;
72104
LITERT_ASSERT_OK(LiteRtFindCompilerOptions(options1, &compiler_options));
73105
LITERT_ASSERT_OK(LiteRtSetDummyCompilerOptions(compiler_options, true));
106+
LITERT_ASSERT_OK(LiteRtSetCompilerOptionsPartitionStrategy(
107+
compiler_options,
108+
kLiteRtCompilerOptionsPartitionStrategyWeaklyConnected));
74109
LITERT_ASSERT_OK(LiteRtGetOpaqueOptionsHash(options1, &hash1));
75110
LITERT_ASSERT_OK(LiteRtGetOpaqueOptionsHash(options2, &hash2));
76111
EXPECT_NE(hash1, hash2);

litert/cc/options/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ cc_test(
8080
srcs = ["compiler_options_test.cc"],
8181
deps = [
8282
":compiler_options",
83+
"//litert/c/options:litert_compiler_options",
8384
"//litert/test:matchers",
8485
"@com_google_googletest//:gtest_main",
8586
],

litert/cc/options/compiler_options.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,31 @@ Expected<CompilerOptions> CompilerOptions::Create() {
3535

3636
Expected<CompilerOptions> CompilerOptions::Create(OpaqueOptions& original) {
3737
const auto id = original.GetIdentifier();
38-
if (!id || *id != Identifier()) {
38+
if (!id || *id != Discriminator()) {
3939
return Error(kLiteRtStatusErrorInvalidArgument);
4040
}
4141
return CompilerOptions(original.Get(), OwnHandle::kNo);
4242
}
4343

44+
Expected<void> CompilerOptions::SetPartitionStrategy(
45+
LiteRtCompilerOptionsPartitionStrategy partition_strategy) {
46+
LiteRtCompilerOptions compiler_options;
47+
LITERT_RETURN_IF_ERROR(LiteRtFindCompilerOptions(Get(), &compiler_options));
48+
LITERT_RETURN_IF_ERROR(LiteRtSetCompilerOptionsPartitionStrategy(
49+
compiler_options, partition_strategy));
50+
return {};
51+
}
52+
53+
Expected<LiteRtCompilerOptionsPartitionStrategy>
54+
CompilerOptions::GetPartitionStrategy() const {
55+
LiteRtCompilerOptions compiler_options;
56+
LITERT_RETURN_IF_ERROR(LiteRtFindCompilerOptions(Get(), &compiler_options));
57+
LiteRtCompilerOptionsPartitionStrategy partition_strategy;
58+
LITERT_RETURN_IF_ERROR(LiteRtGetCompilerOptionsPartitionStrategy(
59+
compiler_options, &partition_strategy));
60+
return partition_strategy;
61+
}
62+
4463
Expected<void> CompilerOptions::SetDummyOption(bool dummy_option) {
4564
LiteRtCompilerOptions compiler_options;
4665
LITERT_RETURN_IF_ERROR(LiteRtFindCompilerOptions(Get(), &compiler_options));

litert/cc/options/compiler_options.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ class CompilerOptions : public OpaqueOptions {
2626
public:
2727
using OpaqueOptions::OpaqueOptions;
2828

29-
static absl::string_view Identifier() {
29+
static absl::string_view Discriminator() {
3030
return LiteRtGetCompilerOptionsIdentifier();
3131
};
3232

3333
static Expected<CompilerOptions> Create();
3434
static Expected<CompilerOptions> Create(OpaqueOptions& original);
3535

36+
Expected<void> SetPartitionStrategy(
37+
LiteRtCompilerOptionsPartitionStrategy partition_strategy);
38+
Expected<LiteRtCompilerOptionsPartitionStrategy> GetPartitionStrategy() const;
39+
3640
Expected<void> SetDummyOption(bool dummy_option);
3741
Expected<bool> GetDummyOption() const;
3842
};

litert/cc/options/compiler_options_test.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "litert/cc/options/compiler_options.h"
1818

1919
#include <gtest/gtest.h>
20+
#include "litert/c/options/litert_compiler_options.h"
2021
#include "litert/test/matchers.h"
2122

2223
namespace litert {
@@ -29,5 +30,16 @@ TEST(CompilerOptionsTest, CreateSetAndGetDummyOptionWorks) {
2930
EXPECT_TRUE(options.GetDummyOption());
3031
}
3132

33+
TEST(CompilerOptionsTest, SetAndGetPartitionStrategyReturnsSetValue) {
34+
LITERT_ASSERT_OK_AND_ASSIGN(::litert::CompilerOptions options,
35+
::litert::CompilerOptions::Create());
36+
LITERT_EXPECT_OK(options.SetPartitionStrategy(
37+
kLiteRtCompilerOptionsPartitionStrategyWeaklyConnected));
38+
LITERT_ASSERT_OK_AND_ASSIGN(auto partition_strategy,
39+
options.GetPartitionStrategy());
40+
EXPECT_EQ(partition_strategy,
41+
kLiteRtCompilerOptionsPartitionStrategyWeaklyConnected);
42+
}
43+
3244
} // namespace
3345
} // namespace litert

litert/tools/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ litert_bin(
135135
":apply_plugin_runtimecapi",
136136
":outstream",
137137
"//litert/cc:litert_api_with_dynamic_runtime",
138+
"//litert/cc/options:compiler_options",
138139
"//litert/core:build_stamp",
139140
"//litert/tools/flags:apply_plugin_flags",
140141
"//litert/tools/flags:common_flags",
@@ -145,6 +146,7 @@ litert_bin(
145146
"//litert/tools/flags/vendors:qualcomm_flags_with_dynamic_runtime",
146147
"@com_google_absl//absl/flags:flag",
147148
"@com_google_absl//absl/flags:parse",
149+
"@com_google_absl//absl/functional:function_ref",
148150
"@com_google_absl//absl/strings:str_format",
149151
"@com_google_absl//absl/strings:string_view",
150152
],
@@ -174,6 +176,7 @@ litert_bin(
174176
":apply_plugin_runtimecapi",
175177
":outstream",
176178
"//litert/cc:litert_api_with_dynamic_runtime",
179+
"//litert/cc/options:compiler_options",
177180
"//litert/core:build_stamp",
178181
"//litert/tools/flags:apply_plugin_flags",
179182
"//litert/tools/flags:common_flags",
@@ -184,6 +187,7 @@ litert_bin(
184187
"//litert/tools/flags/vendors:qualcomm_flags_with_dynamic_runtime",
185188
"@com_google_absl//absl/flags:flag",
186189
"@com_google_absl//absl/flags:parse",
190+
"@com_google_absl//absl/functional:function_ref",
187191
"@com_google_absl//absl/strings:str_format",
188192
"@com_google_absl//absl/strings:string_view",
189193
],
@@ -257,6 +261,7 @@ cc_binary(
257261
":apply_plugin_runtimecapi",
258262
":outstream",
259263
"//litert/cc:litert_api_with_dynamic_runtime",
264+
"//litert/cc/options:compiler_options",
260265
"//litert/tools/flags:apply_plugin_flags",
261266
"//litert/tools/flags:common_flags",
262267
"//litert/tools/flags:flag_types",
@@ -266,6 +271,7 @@ cc_binary(
266271
"//litert/tools/flags/vendors:qualcomm_flags_with_dynamic_runtime",
267272
"@com_google_absl//absl/flags:flag",
268273
"@com_google_absl//absl/flags:parse",
274+
"@com_google_absl//absl/functional:function_ref",
269275
"@com_google_absl//absl/strings:str_format",
270276
],
271277
)

litert/tools/apply_plugin_main.cc

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expruns or implied.
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

@@ -24,8 +24,16 @@
2424

2525
#include "absl/flags/flag.h" // from @com_google_absl
2626
#include "absl/flags/parse.h" // from @com_google_absl
27+
#include "absl/functional/function_ref.h" // from @com_google_absl
2728
#include "absl/strings/str_format.h" // from @com_google_absl
29+
#include "litert/cc/litert_expected.h"
30+
#include "litert/cc/litert_opaque_options.h"
2831
#include "litert/cc/litert_options.h"
32+
#include "litert/cc/options/compiler_options.h"
33+
#include "litert/cc/options/litert_google_tensor_options.h"
34+
#include "litert/cc/options/litert_intel_openvino_options.h"
35+
#include "litert/cc/options/litert_mediatek_options.h"
36+
#include "litert/cc/options/litert_qualcomm_options.h"
2937
#include "litert/tools/apply_plugin.h"
3038
#include "litert/tools/flags/apply_plugin_flags.h"
3139
#include "litert/tools/flags/common_flags.h"
@@ -36,6 +44,8 @@
3644
#include "litert/tools/flags/vendors/qualcomm_flags.h" // IWYU pragma: keep
3745
#include "litert/tools/outstream.h"
3846

47+
namespace {
48+
3949
using ::litert::tools::ApplyPlugin;
4050
using ::litert::tools::ApplyPluginRun;
4151
using ::litert::tools::IntList;
@@ -80,6 +90,28 @@ ApplyPluginRun::Ptr ParseFlags() {
8090
return res;
8191
}
8292

93+
template <typename T>
94+
bool ParseAndAddOptions(
95+
litert::Options& opts, ApplyPluginRun::Ptr& run,
96+
absl::FunctionRef<litert::Expected<T>()> options_from_flags) {
97+
static_assert(std::is_base_of_v<litert::OpaqueOptions, T>);
98+
litert::Expected<T> options = options_from_flags();
99+
if (!options) {
100+
run->dump_out.Get().get()
101+
<< "Failed to create " << T::Discriminator()
102+
<< " options, Error:" << options.Error().Message() << "\n";
103+
return false;
104+
}
105+
if (!opts.AddOpaqueOptions(std::move(*options))) {
106+
run->dump_out.Get().get()
107+
<< "Failed to add " << T::Discriminator() << " options to list\n";
108+
return false;
109+
}
110+
return true;
111+
}
112+
113+
} // namespace
114+
83115
int main(int argc, char* argv[]) {
84116
absl::ParseCommandLine(argc, argv);
85117

@@ -110,62 +142,20 @@ int main(int argc, char* argv[]) {
110142
return 1;
111143
}
112144

113-
{
114-
auto qnn_opts = litert::qualcomm::QualcommOptionsFromFlags();
115-
if (!qnn_opts) {
116-
run->dump_out.Get().get() << "Failed to create Qualcomm options\n";
117-
return 1;
118-
}
119-
120-
if (!opts->AddOpaqueOptions(std::move(*qnn_opts))) {
121-
run->dump_out.Get().get() << "Failed to add Qualcomm options to list\n";
122-
return 1;
123-
}
124-
}
125-
126-
{
127-
auto google_tensor_opts =
128-
litert::google_tensor::GoogleTensorOptionsFromFlags();
129-
if (!google_tensor_opts) {
130-
run->dump_out.Get().get() << "Failed to create Google Tensor options\n";
131-
return 1;
132-
}
133-
134-
if (!opts->AddOpaqueOptions(std::move(*google_tensor_opts))) {
135-
run->dump_out.Get().get()
136-
<< "Failed to add google tensor options to list\n";
137-
return 1;
138-
}
139-
}
140-
141-
{
142-
auto intel_openvino_opts =
143-
litert::intel_openvino::IntelOpenVinoOptionsFromFlags();
144-
if (!intel_openvino_opts) {
145-
run->dump_out.Get().get() << "Failed to create Intel OpenVINO options\n";
146-
return 1;
147-
}
148-
149-
if (!opts->AddOpaqueOptions(std::move(*intel_openvino_opts))) {
150-
run->dump_out.Get().get()
151-
<< "Failed to add Intel OpenVINO options to list\n";
152-
return 1;
153-
}
154-
}
155-
156-
{
157-
auto mediatek_opts =
158-
litert::mediatek::MediatekOptionsFromFlags();
159-
if (!mediatek_opts) {
160-
run->dump_out.Get().get() << "Failed to create Mediatek options\n";
161-
return 1;
162-
}
163-
164-
if (!opts->AddOpaqueOptions(std::move(*mediatek_opts))) {
165-
run->dump_out.Get().get()
166-
<< "Failed to add Mediatek options to list\n";
167-
return 1;
168-
}
145+
bool all_flags_parsed =
146+
(ParseAndAddOptions<litert::CompilerOptions>(
147+
*opts, run, litert::CompilerOptionsFromFlags) ||
148+
ParseAndAddOptions<litert::qualcomm::QualcommOptions>(
149+
*opts, run, litert::qualcomm::QualcommOptionsFromFlags) ||
150+
ParseAndAddOptions<litert::google_tensor::GoogleTensorOptions>(
151+
*opts, run, litert::google_tensor::GoogleTensorOptionsFromFlags) ||
152+
ParseAndAddOptions<litert::intel_openvino::IntelOpenVinoOptions>(
153+
*opts, run, litert::intel_openvino::IntelOpenVinoOptionsFromFlags) ||
154+
ParseAndAddOptions<litert::mediatek::MediatekOptions>(
155+
*opts, run, litert::mediatek::MediatekOptionsFromFlags));
156+
157+
if (!all_flags_parsed) {
158+
return 1;
169159
}
170160

171161
run->options = std::move(*opts);

0 commit comments

Comments
 (0)