Skip to content

Commit

Permalink
[TVMC] Refactoring to document the --target regex and simplify test c…
Browse files Browse the repository at this point in the history
…ases (apache#7654)

* Adds comments to document the regex being used to parse the
   --target=value string
 * Concatenate test cases without reducing the number of asserts
   or number of actual tests
  • Loading branch information
leandron authored and trevor-m committed May 11, 2021
1 parent 26fb1b3 commit 3d27729
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
12 changes: 11 additions & 1 deletion python/tvm/driver/tvmc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,19 @@ def tokenize_target(target):
a list of parsed tokens extracted from the target string
"""

# Regex to tokenize the "--target" value. It is split into five parts
# to match with:
# 1. target and option names e.g. llvm, -mattr=, -mcpu=
# 2. option values, all together, without quotes e.g. -mattr=+foo,+opt
# 3. option values, when single quotes are used e.g. -mattr='+foo, +opt'
# 4. option values, when double quotes are used e.g. -mattr="+foo ,+opt"
# 5. commas that separate different targets e.g. "my-target, llvm"
target_pattern = (
r"(\-{0,2}[\w\-]+\=?"
r"(?:[\w\+\-\.]+(?:,[\w\+\-\.])*|[\'][\w\+\-,\s\.]+[\']|[\"][\w\+\-,\s\.]+[\"])*|,)"
r"(?:[\w\+\-\.]+(?:,[\w\+\-\.])*"
r"|[\'][\w\+\-,\s\.]+[\']"
r"|[\"][\w\+\-,\s\.]+[\"])*"
r"|,)"
)

return re.findall(target_pattern, target)
Expand Down
26 changes: 10 additions & 16 deletions tests/python/driver/tvmc/test_tvmc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,22 +275,16 @@ def test_parse_multiple_target_with_opts():
assert "llvm" == targets[1]["name"]


def test_parse_multiple_separators_on_target():
targets = tvmc.common.parse_target("foo -option1=+v1.0x,+value,+bar")

assert len(targets) == 1
assert "+v1.0x,+value,+bar" == targets[0]["opts"]["option1"]
def test_parse_quotes_and_separators_on_options():
targets_no_quote = tvmc.common.parse_target("foo -option1=+v1.0x,+value,+bar")
targets_single_quote = tvmc.common.parse_target("foo -option1='+v1.0x,+value'")
targets_double_quote = tvmc.common.parse_target('foo -option1="+v1.0x,+value"')

assert len(targets_no_quote) == 1
assert "+v1.0x,+value,+bar" == targets_no_quote[0]["opts"]["option1"]

def test_parse_single_quoted_multiple_separators_on_target():
targets = tvmc.common.parse_target("foo -option1='+v1.0x,+value'")

assert len(targets) == 1
assert "+v1.0x,+value" == targets[0]["opts"]["option1"]
assert len(targets_single_quote) == 1
assert "+v1.0x,+value" == targets_single_quote[0]["opts"]["option1"]


def test_parse_double_quoted_multiple_separators_on_target():
targets = tvmc.common.parse_target('foo -option1="+v1.0x,+value"')

assert len(targets) == 1
assert "+v1.0x,+value" == targets[0]["opts"]["option1"]
assert len(targets_double_quote) == 1
assert "+v1.0x,+value" == targets_double_quote[0]["opts"]["option1"]

0 comments on commit 3d27729

Please sign in to comment.