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

Add mlir graph optimization to the build #39231

Merged
merged 1 commit into from
May 8, 2020

Conversation

yongtang
Copy link
Member

@yongtang yongtang commented May 6, 2020

This PR is part of the process to resolve #39135.

As was mentioned in #39135, 248bc00 enables MLIR graph optimizations. However, it is not part of the build for pip wheel. The reason was that MLIR graph optimizations is done through a registration (mlir_graph_optimization_pass_registration.cc) but this file is not included in libtensorflow_framerowk.so so pip wheel package is not enabled.

This PR add the mlir_graph_optimization_pass_registration to be part of the libtensorflow_framework.so. Due to the bazel dependency reasons, a direct inclusion will not work as ops in core and xla will be pulled in multiple times by bazel. There will be 2 copies of xla ops, 2 copies of lite and core ops in libtensorflow_framework.so

So the following change has been made. In general import_model.[h|cc] has been split into 3 parts:

  • new import_base.[h|cc] consists of ImporterBase class and common shared functions (by import_model.[h|cc]andimport_graphdef.[h|cc]`
  • new import_graphdef.[h|cc] consists of graph only conversion to mlir
  • the import_model.[h|cc] has also been updated to consist model only conversion to mlir

There are also some small bazel changes that removes unnecessary dependencies.

Note with the change, MLIR graph optimizations only need to depends on import_graphdef.cc and export_graphdef.cc. It will not depend on import_model.cc which pulled in xla and core ops multiple time by bazel.

Signed-off-by: Yong Tang yong.tang.github@outlook.com

@google-ml-butler google-ml-butler bot added the size:XL CL Change Size:Extra Large label May 6, 2020
@google-ml-butler google-ml-butler bot requested a review from joker-eph May 6, 2020 16:51
@joker-eph joker-eph added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels May 6, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label May 6, 2020
@joker-eph
Copy link
Contributor

Thanks for the fix! Seems like the CI is not yet happy with it right now.

@gbaned gbaned self-assigned this May 6, 2020
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation May 6, 2020
yongtang added a commit to yongtang/tensorflow that referenced this pull request May 7, 2020
…gistration" to tensorflow/python/_pywrap_mlir.so

This commit adds build target //tensorflow/compiler/mlir/tensorflow:graph_optimization_pass_registration
to tensorflow/python/_pywrap_mlir.so, so that graph optimization could be available in tf-nightly pip wheel.

In the past while graph_optimization_pass_registration is available, it is not packaged with
tf-nightly so it is not possible to access graph optimization if installed from pip wheel.

In order to enable graph optimization pass for pip wheel, graph_optimization_pass_registration
target has to be included in "somewhere" that will be loaded when pip installed tensorflow
is imported.

A natural place would be libtensorflow_framework.so which is the core .so always loaded with `import tensorflow`.

It is possible to include graph_optimization_pass_registration to target libtensorflow_framework.so, see
last attempt on this route:
tensorflow#39231

However, this caused many test failures like:
```
: CommandLine Error: Option 'help-list' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
```

The reason is that many tests as a binary also have a copy of the LLVM in its binary, thus causing
multiple copies (one in binary, another one in libtensorflow_framework.so where many tests depends on).

Because there are so many tests, it is really hard to make all the needed changes without break somewhere else.

This commit takes a different approach to include graph_optimization_pass_registration to
tensorflow/python/_pywrap_mlir.so. This shared object is dedicated to mlir related APIs. The current exposed one
is `tf.mlir.experimental.convert_graph_def`.

Because this tensorflow/python/_pywrap_mlir.so already depends on llvm, place graph_optimization_pass will avoid
multiple copies of llvm in multiple locations. This tensorflow/python/_pywrap_mlir.so is also loaded with
`import tensorflow` as part of the python binding.

Ideally it probably would be still preferrale to get the graph_optimization_pass into libtensorflow_framework.so.
That will be investigated further later.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label May 7, 2020
…gistration" to tensorflow/python/_pywrap_mlir.so

This commit adds build target //tensorflow/compiler/mlir/tensorflow:graph_optimization_pass_registration
to tensorflow/python/_pywrap_mlir.so, so that graph optimization could be available in tf-nightly pip wheel.

In the past while graph_optimization_pass_registration is available, it is not packaged with
tf-nightly so it is not possible to access graph optimization if installed from pip wheel.

In order to enable graph optimization pass for pip wheel, graph_optimization_pass_registration
target has to be included in "somewhere" that will be loaded when pip installed tensorflow
is imported.

A natural place would be libtensorflow_framework.so which is the core .so always loaded with `import tensorflow`.

It is possible to include graph_optimization_pass_registration to target libtensorflow_framework.so, see
last attempt on this route:
tensorflow#39231

However, this caused many test failures like:
```
: CommandLine Error: Option 'help-list' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
```

The reason is that many tests as a binary also have a copy of the LLVM in its binary, thus causing
multiple copies (one in binary, another one in libtensorflow_framework.so where many tests depends on).

Because there are so many tests, it is really hard to make all the needed changes without break somewhere else.

This commit takes a different approach to include graph_optimization_pass_registration to
tensorflow/python/_pywrap_mlir.so. This shared object is dedicated to mlir related APIs. The current exposed one
is `tf.mlir.experimental.convert_graph_def`.

Because this tensorflow/python/_pywrap_mlir.so already depends on llvm, place graph_optimization_pass will avoid
multiple copies of llvm in multiple locations. This tensorflow/python/_pywrap_mlir.so is also loaded with
`import tensorflow` as part of the python binding.

Ideally it probably would be still preferrale to get the graph_optimization_pass into libtensorflow_framework.so.
That will be investigated further later.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
@yongtang
Copy link
Member Author

yongtang commented May 7, 2020

Thanks @joker-eph for the review. The tests error was due to tests normally depends on LLVM so there are two copies again (one in libtensorflow_io.so and one in tests binary itself). While it is possible to updates the tests, there are so many and dependencies are just too interleaved.

I take another look and now the PR takes a different approach: it adds graph_optimization_pass_registration to tensorflow/python/_pywrap_mlir.so.

_pywrap_mlir.so is the python binding for mlir related API. At the moment only tf.mlir.experimental.convert_graph_def is exposed.

Since tensorflow/python/_pywrap_mlir.so is a python biding, it will be loaded once import tensorflow is executed. _pywrap_mlir.so already depends on LLVM anyway so it fits as well.

In the long term it still might makes sense to build graph_optimization_pass into libtensorflow_io.so, as it is the natural place for core components. Though for now, I think it will be a viable approach to build graph_optimization_pass to _pywrap_mlir.so.

@yongtang
Copy link
Member Author

yongtang commented May 7, 2020

@joker-eph The PR has been updated. The import_module.[cc|h] does not need to be split now. Please take a look and let me know if there are any issues.

@joker-eph joker-eph added the ready to pull PR ready for merge process label May 7, 2020
PR Queue automation moved this from Assigned Reviewer to Approved by Reviewer May 7, 2020
@google-ml-butler google-ml-butler bot added the kokoro:force-run Tests on submitted change label May 7, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label May 7, 2020
@tensorflow-copybara tensorflow-copybara merged commit bd17cdd into tensorflow:master May 8, 2020
@yongtang yongtang deleted the mlir branch May 8, 2020 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes ready to pull PR ready for merge process size:XL CL Change Size:Extra Large
Projects
PR Queue
  
Approved by Reviewer
Development

Successfully merging this pull request may close these issues.

mlir graph optimization is not built into tf-nightly
6 participants