-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PJRT] Allow to pass extra compile options via env variables (#19418)
Sometime it's useful to pass some extra IREE compiler options to the PJRT plugin by environment variables to debug/do some experiment/performance tuning without recompilation. This is a rewrite to the following code which was commented as a TODO. https://github.com/iree-org/iree/blob/b68c535ece28e139492606f391493f3e95242420/integrations/pjrt/src/iree_pjrt/common/iree_compiler.cc#L231-L245 ci-exactly: build_packages, test_pjrt --------- Signed-off-by: PragmaTwice <[email protected]> Co-authored-by: Scott Todd <[email protected]>
- Loading branch information
1 parent
0cafee9
commit ffa0f42
Showing
8 changed files
with
150 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
integrations/pjrt/src/iree_pjrt/common/command_line_utils.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2024 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "command_line_utils.h" | ||
|
||
namespace iree { | ||
namespace pjrt { | ||
|
||
// TODO: currently this function doesn't handle escape sequences, | ||
// it just ensure that single/double quotes are interpreted corrently. | ||
std::optional<std::vector<std::string>> ParseOptionsFromCommandLine( | ||
std::string_view options_str) { | ||
std::vector<std::string> options; | ||
std::string current; | ||
|
||
enum { NORMAL, SINGLE_QUOTE, DOUBLE_QUOTE } state = NORMAL; | ||
for (auto it = options_str.begin(); it != options_str.end(); ++it) { | ||
if (std::isspace(*it) && state == NORMAL) { | ||
if (!current.empty()) { | ||
options.push_back(std::move(current)); | ||
current.clear(); | ||
} | ||
} else if (*it == '"' && state != SINGLE_QUOTE) { | ||
if (state == NORMAL) | ||
state = DOUBLE_QUOTE; | ||
else if (state == DOUBLE_QUOTE) | ||
state = NORMAL; | ||
} else if (*it == '\'' && state != DOUBLE_QUOTE) { | ||
if (state == NORMAL) | ||
state = SINGLE_QUOTE; | ||
else if (state == SINGLE_QUOTE) | ||
state = NORMAL; | ||
} else { | ||
current.push_back(*it); | ||
} | ||
} | ||
|
||
if (!current.empty()) { | ||
options.push_back(std::move(current)); | ||
} | ||
|
||
// if it's still in a quote, then return nullopt | ||
if (state != NORMAL) { | ||
return std::nullopt; | ||
} | ||
|
||
return options; | ||
} | ||
|
||
} // namespace pjrt | ||
} // namespace iree |
26 changes: 26 additions & 0 deletions
26
integrations/pjrt/src/iree_pjrt/common/command_line_utils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#ifndef IREE_PJRT_PLUGIN_PJRT_COMMON_COMMAND_LINE_UTILS_H_ | ||
#define IREE_PJRT_PLUGIN_PJRT_COMMON_COMMAND_LINE_UTILS_H_ | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace iree { | ||
namespace pjrt { | ||
|
||
// parse command line options (maybe with quotes) to an array of options | ||
// e.g. `a b "c d"` -> {"a", "b", "c d"} | ||
std::optional<std::vector<std::string>> ParseOptionsFromCommandLine( | ||
std::string_view options_str); | ||
|
||
} // namespace pjrt | ||
} // namespace iree | ||
|
||
#endif |
24 changes: 24 additions & 0 deletions
24
integrations/pjrt/src/iree_pjrt/common/command_line_utils_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2024 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "iree_pjrt/common/command_line_utils.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace iree::pjrt; | ||
|
||
TEST(CommandLineUtils, ParseOptionsFromCommandLine) { | ||
EXPECT_EQ(ParseOptionsFromCommandLine("--help --verbose"), | ||
(std::vector<std::string>{"--help", "--verbose"})); | ||
EXPECT_EQ(ParseOptionsFromCommandLine("-a='x y' -b \"n m\""), | ||
(std::vector<std::string>{"-a=x y", "-b", "n m"})); | ||
EXPECT_EQ(ParseOptionsFromCommandLine("'\"' \"'\""), | ||
(std::vector<std::string>{"\"", "'"})); | ||
EXPECT_EQ(ParseOptionsFromCommandLine("ab abc d 'e f g' h "), | ||
(std::vector<std::string>{"ab", "abc", "d", "e f g", "h"})); | ||
EXPECT_EQ(ParseOptionsFromCommandLine("a 'b"), std::nullopt); | ||
EXPECT_EQ(ParseOptionsFromCommandLine("x\"y"), std::nullopt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters