You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by tq...@apache.org on 2021/03/15 15:41:46 UTC

[tvm] branch main updated: [TVMC] Refactoring to document the --target regex and simplify test cases (#7654)

This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 10f5d17  [TVMC] Refactoring to document the --target regex and simplify test cases (#7654)
10f5d17 is described below

commit 10f5d17a668a1bb8fbd021cadbe6d052a180f706
Author: Leandro Nunes <le...@arm.com>
AuthorDate: Mon Mar 15 15:41:31 2021 +0000

    [TVMC] Refactoring to document the --target regex and simplify test cases (#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
---
 python/tvm/driver/tvmc/common.py             | 12 +++++++++++-
 tests/python/driver/tvmc/test_tvmc_common.py | 26 ++++++++++----------------
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/python/tvm/driver/tvmc/common.py b/python/tvm/driver/tvmc/common.py
index c5cb5f2..fbd7bc8 100644
--- a/python/tvm/driver/tvmc/common.py
+++ b/python/tvm/driver/tvmc/common.py
@@ -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)
diff --git a/tests/python/driver/tvmc/test_tvmc_common.py b/tests/python/driver/tvmc/test_tvmc_common.py
index 23ea4f4..474649d 100644
--- a/tests/python/driver/tvmc/test_tvmc_common.py
+++ b/tests/python/driver/tvmc/test_tvmc_common.py
@@ -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"]