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

[TVMC][UMA] Support using UMA with TVMC #14165

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rafzi
Copy link
Contributor

@rafzi rafzi commented Mar 1, 2023

This change introduces:

  • A TVMExtension interface to extend TVMC with custom code. Currently its only use is to add UMABackends.
  • A command line option --experimental-tvm-extension <dir> to point to extension modules.
  • An envvar $TVM_EXTENSION_DIR for the same purpose.

See discussion thread: https://discuss.tvm.apache.org/t/tvmc-uma-integration/14309

@Mousius @cgerum @MichaelJKlaiber @PhilippvK

@tvm-bot
Copy link
Collaborator

tvm-bot commented Mar 1, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

Copy link
Contributor

@lhutton1 lhutton1 left a comment

Choose a reason for hiding this comment

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

This looks really nice, thanks @rafzi :) I had a couple of questions and some suggestions for tests

"""Include parser for 'compile' subcommand"""

parser = subparsers.add_parser("compile", help="compile a model.")
parser = subparsers.add_parser("compile", help="compile a model.", add_help=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious, why do we need to remove help option?

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 encountered an issue where the TVMCSuppressedArgumentParser still caused a SystemExit exception. However, I cannot seem to reproduce the issue right now. So I'll remove this again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I found the input again: "tvmc compile --help". When removing the help option from the subparser, this is resolved. The help option still works because it is enabled through the main parser.

Copy link
Contributor

@lhutton1 lhutton1 Mar 7, 2023

Choose a reason for hiding this comment

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

I tried this out locally but see some difference in the help prompt when executing tvmc compile --help. Before the patch we get something like:

$ tvmc compile --help
usage: tvmc compile [-h] [--cross-compiler CROSS_COMPILER]
                    [--cross-compiler-options CROSS_COMPILER_OPTIONS]
                    [--desired-layout {NCHW,NHWC}] [--dump-code FORMAT]
                    [--model-format {keras,onnx,pb,tflite,pytorch,paddle,relay}]
                    [-o OUTPUT] [-f {so,mlf}] [--pass-config name=value]
                    [--target TARGET]
...
positional arguments:
  FILE                  path to the input model file.

optional arguments:
  -h, --help            show this help message and exit
  --cross-compiler CROSS_COMPILER
                        the cross compiler to generate target libraries, e.g.
                        'aarch64-linux-gnu-gcc'.
  --cross-compiler-options CROSS_COMPILER_OPTIONS
                        the cross compiler options to generate target
                        libraries, e.g. '-mfpu=neon-vfpv4'.
  --desired-layout {NCHW,NHWC}
                        change the data layout of the whole graph.

While after seems to lead to:

$ tvmc compile --help
usage: tvmc compile [--experimental-tvm-extension EXPERIMENTAL_TVM_EXTENSION]
                    [--cross-compiler CROSS_COMPILER]
                    [--cross-compiler-options CROSS_COMPILER_OPTIONS]
                    [--desired-layout {NCHW,NHWC}] [--dump-code FORMAT]
                    [--model-format {keras,onnx,pb,tflite,pytorch,paddle,relay}]
                    [-o OUTPUT] [-f {so,mlf}] [--pass-config name=value]
                    [--target TARGET]
...
tvmc compile: error: the following arguments are required: FILE

Could you confirm if you see the same behaviour? I think it would be really helpful if we can keep the argument descriptions as before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I can confirm this issue. The underlying issue is that the TVMCSuppressedArgumentParser still throws SystemExit when given such a command line. We also cannot override error of ArgumentParser, because the failing parser is the subparser, which is not a TVMCSuppressedArgumentParser.

Before this patch, this problem already occurs. For example with tvmc run --help, SystemExit is thrown and a partial help message is printed (missing --project-option). So my suggested fix is to move the parse_known_args as far down as possible to yield a mostly complete help message.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for investigating! This looks much better, so I won't block on it. Perhaps we can create a separate issue to track this bug?

for _name, obj in inspect.getmembers(loaded_mod):
if _is_concrete_extension_type(obj):
scanned_extensions.append(obj)
except ImportError as err:
Copy link
Contributor

Choose a reason for hiding this comment

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

would be good to add a test for this case

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 tried adding this, but encountered an issue with race conditions. The way the sys.path is currently modified by extensions.py is problematic when more than one test is run in parallel. Is there already an approach for serializing tests in TVM? There doesn't seem to be an upstream solution: pytest-dev/pytest-xdist#18

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we instead avoid modifying sys.path when loading a module? I had a naive search and wondered if this could help? cc @Mousius @leandron who might know more about these things

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While such an approach should work for single source files, I'm not sure how that would work for packages or namespace packages.

Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies for the delay, I had a think about this but wasn't able to come up with a better solution myself, so I won't block on this

tests/python/contrib/test_uma/test_tvmc.py Show resolved Hide resolved
python/tvm/driver/tvmc/extensions.py Show resolved Hide resolved
Copy link
Contributor

@cgerum cgerum left a comment

Choose a reason for hiding this comment

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

Thanks @rafzi for the proposal, thanks @lhutton1 for the feedback.

I also have a few minor requests.

python/tvm/driver/tvmc/extensions.py Outdated Show resolved Hide resolved
python/tvm/relay/backend/contrib/uma/api/lower.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/extensions.py Show resolved Hide resolved
Copy link
Contributor

@MichaelJKlaiber MichaelJKlaiber left a comment

Choose a reason for hiding this comment

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

I agreed with @lhutton1 and @cgerum 's comments.
Besides that everything LGTM.
Thanks @rafzi for the contribution

@PhilippvK
Copy link
Contributor

@tvm-bot rerun

@rafzi
Copy link
Contributor Author

rafzi commented Apr 15, 2023

Sorry for the late update.

The outstanding test failure is likely because of the outstanding issue that the sys.path is modified in order to support all module loading. This probably inteferres with running the tests in parallel.

https://github.com/tum-ei-eda/tvm/blob/c1c2d3ca2db1c56d3cc8d695530ab7aa8b90ea00/python/tvm/driver/tvmc/extensions.py#L24-L34

I'm not sure how to resolve this as I did not find another method to achieve the same kind of module loading. Do we need to compromise here and settle for a simple module load? I unresolved the relevant discussion with @lhutton1.

@cgerum @PhilippvK

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

Successfully merging this pull request may close these issues.

None yet

6 participants