Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Target] Add python binding to new JSON target construction. #6315

Merged
merged 5 commits into from
Aug 21, 2020

Conversation

jwfromm
Copy link
Contributor

@jwfromm jwfromm commented Aug 20, 2020

This PR adds the ability to create targets from a configuration dictionary. It's a very thin wrapper around the c++ version of this feature introduced in #6218.

@jwfromm
Copy link
Contributor Author

jwfromm commented Aug 20, 2020

@junrushao1994 can you review this PR?

@tqchen
Copy link
Member

tqchen commented Aug 20, 2020

also cc @zhiics @comaniac @yzhliu @leandron

Comment on lines 401 to 402
if isinstance(target, str):
return _ffi_api.TargetFromString(target)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we also do some guessing work as well for strings? If it is a string but seems like a JSON, we can use json.loads instead.

Suggested change
if isinstance(target, str):
return _ffi_api.TargetFromString(target)
if isinstance(target, str):
if target.startswith("{"):
return _ffi_api.TargetFromConfig(json.loads(target))
else:
return _ffi_api.TargetFromString(target)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to support the following use case as well:

target = target.create('./target.json') # Input is a string file path.

This case could be supported by using os to check if the string is a file path or not.

In addition, if we would like to support the JSON string, I personally don't think checking the first character is a proper approach, as you may have spaces or something else. A better way should be just trying to parse the string with JSON parser first and use string parser instead if the JSON parser throws an exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we should try to find a unified approach to create target from

  1. file
  2. json string
  3. json-like dict
  4. legacy raw string
  5. tag

Copy link
Member

@tqchen tqchen Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that it is one line to open and read the file. I think we can just allow it takes in string and not support the file path feature(because supporting both could be confusing). It also helps to avoid conflcits(e.g. what if there is a file with the same name).

target = target.create(open("target.json").read()) is not as bad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a try except block to see if the input string can be parsed to json. We now can accept traditional target string, python dicts, or json strings. Let me know we need other options like tag (not sure what this means).

Comment on lines 401 to 402
if isinstance(target, str):
return _ffi_api.TargetFromString(target)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to support the following use case as well:

target = target.create('./target.json') # Input is a string file path.

This case could be supported by using os to check if the string is a file path or not.

In addition, if we would like to support the JSON string, I personally don't think checking the first character is a proper approach, as you may have spaces or something else. A better way should be just trying to parse the string with JSON parser first and use string parser instead if the JSON parser throws an exception.

"""
Test that constructing a target from a dictionary works.
"""
target_config = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also test map-type attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on this a little? Do you mean add testing that confirms it fails when invalid attribute types are provided?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just meant we should also test the attribute which type is a map. Since one purpose of testing Python binding is to make sure we can correctly pass supported data structures to the C++ container. Specifically, the Python version of this test: https://github.com/apache/incubator-tvm/blob/master/tests/cpp/target_test.cc#L121

Copy link
Contributor Author

@jwfromm jwfromm Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. It's a little strange to do this since none of the supported options are maps. But I could add a test case that passes a map and then confirm it fails like the cpp version you linked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an analogous test in the latest commit. Let me know if this is what you were looking for.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw the map test you added, but it is intented to fail so I'm not sure if it is sufficient.
@junrushao1994 could you comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, there are no valid arguments that have type map. The tests you linked in target_test.cc also are intentionally failing.

Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good from your side :-)

@jwfromm
Copy link
Contributor Author

jwfromm commented Aug 21, 2020

@tqchen, tests are passing and reviewers are happy, can we merge this?

@leandron
Copy link
Contributor

leandron commented Aug 21, 2020

I tested it integrating with my ongoing PR #6302 and it works.

One small comment is that I didn't see here a test checking that plain JSON strings, such as { "kind": "llvm" }. Would that make sense to add one, to guarantee the internal behaviour is validated, and the _ffi_api.TargetFromConfig branch is also covered? Let me know in case I'm missing something. (PS: maybe on a separate patch, so that it doesn't block your merge)

edit: found it now.

@tqchen tqchen merged commit 005dfb7 into apache:master Aug 21, 2020
@jwfromm jwfromm deleted the python_target branch August 21, 2020 14:52
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…6315)

* Add python binding to new JSON target construction.

* Added json string parsing and new test.

* Add error type.

* Add error type in json decoding check.

* Fix sphinx formatting.
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…6315)

* Add python binding to new JSON target construction.

* Added json string parsing and new test.

* Add error type.

* Add error type in json decoding check.

* Fix sphinx formatting.
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…6315)

* Add python binding to new JSON target construction.

* Added json string parsing and new test.

* Add error type.

* Add error type in json decoding check.

* Fix sphinx formatting.
electriclilies pushed a commit to electriclilies/tvm that referenced this pull request Aug 26, 2020
…6315)

* Add python binding to new JSON target construction.

* Added json string parsing and new test.

* Add error type.

* Add error type in json decoding check.

* Fix sphinx formatting.
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Sep 2, 2020
…6315)

* Add python binding to new JSON target construction.

* Added json string parsing and new test.

* Add error type.

* Add error type in json decoding check.

* Fix sphinx formatting.
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Sep 3, 2020
…6315)

* Add python binding to new JSON target construction.

* Added json string parsing and new test.

* Add error type.

* Add error type in json decoding check.

* Fix sphinx formatting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants