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

[Doc] Relax Deep Dive #17380

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ def jupyter_notebook(script_blocks, gallery_conf, target_dir, real_func):
# New tutorial structure under docs folder
tvm_path.joinpath("docs", "get_started", "tutorials"),
tvm_path.joinpath("docs", "how_to", "tutorials"),
tvm_path.joinpath("docs", "deep_dive", "relax", "tutorials"),
tvm_path.joinpath("docs", "deep_dive", "tensor_ir", "tutorials"),
]

Expand All @@ -443,6 +444,7 @@ def jupyter_notebook(script_blocks, gallery_conf, target_dir, real_func):
# New tutorial structure under docs folder
"get_started/tutorials/",
"how_to/tutorials/",
"deep_dive/relax/tutorials/",
"deep_dive/tensor_ir/tutorials/",
]

Expand Down Expand Up @@ -598,10 +600,10 @@ def force_gc(gallery_conf, fname):
## Setup header and other configs
import tlcpack_sphinx_addon

footer_copyright = "© 2023 Apache Software Foundation | All rights reserved"
footer_copyright = "© 2024 Apache Software Foundation | All rights reserved"
footer_note = " ".join(
"""
Copyright © 2023 The Apache Software Foundation. Apache TVM, Apache, the Apache feather,
Copyright © 2024 The Apache Software Foundation. Apache TVM, Apache, the Apache feather,
and the Apache TVM project logo are either trademarks or registered trademarks of
the Apache Software Foundation.""".split(
"\n"
Expand All @@ -614,7 +616,6 @@ def force_gc(gallery_conf, fname):
header_links = [
("Community", "https://tvm.apache.org/community"),
("Download", "https://tvm.apache.org/download"),
("VTA", "https://tvm.apache.org/vta"),
("Blog", "https://tvm.apache.org/blog"),
("Docs", "https://tvm.apache.org/docs"),
("Conference", "https://tvmconf.org"),
Expand Down
73 changes: 73 additions & 0 deletions docs/deep_dive/relax/abstraction.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
.. Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

.. http://www.apache.org/licenses/LICENSE-2.0

.. Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

.. _relax-abstraction:

Graph Abstraction for ML Models
-------------------------------
Graph abstraction is a key technique used in machine learning (ML) compilers
to represent and reason about the structure and data flow of ML models. By
abstracting the model into a graph representation, the compiler can perform
various optimizations to improve performance and efficiency. This tutorial will
cover the basics of graph abstraction, its key elements of Relax IR, and how it enables optimization in ML compilers.

What is Graph Abstraction?
~~~~~~~~~~~~~~~~~~~~~~~~~~
Graph abstraction is the process of representing an ML model as a directed graph,
where the nodes represent computational operations (e.g., matrix multiplication,
convolution) and the edges represent the flow of data between these operations.
This abstraction allows the compiler to analyze the dependencies and
relationships between different parts of the model.

.. code:: python

from tvm.script import relax as R

@R.function
def main(
x: R.Tensor((1, 784), dtype="float32"),
weight: R.Tensor((784, 256), dtype="float32"),
bias: R.Tensor((256,), dtype="float32"),
) -> R.Tensor((1, 256), dtype="float32"):
with R.dataflow():
lv0 = R.matmul(x, weight)
lv1 = R.add(lv0, bias)
gv = R.nn.relu(lv1)
R.output(gv)
return gv

Key Features of Relax
~~~~~~~~~~~~~~~~~~~~~
Relax, the graph representation utilized in Apache TVM's Unity strategy,
facilitates end-to-end optimization of ML models through several crucial
features:

- **First-class symbolic shape**: Relax employs symbolic shapes to represent
tensor dimensions, enabling global tracking of dynamic shape relationships
across tensor operators and function calls.

- **Multi-level abstractions**: Relax supports cross-level abstractions, from
high-level neural network layers to low-level tensor operations, enabling
optimizations that span different hierarchies within the model.

- **Composable transformations**: Relax offers a framework for composable
transformations that can be selectively applied to different model components.
This includes capabilities such as partial lowering and partial specialization,
providing flexible customization and optimization options.

These features collectively empower Relax to offer a powerful and adaptable approach
to ML model optimization within the Apache TVM ecosystem.
34 changes: 34 additions & 0 deletions docs/deep_dive/relax/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.. Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

.. http://www.apache.org/licenses/LICENSE-2.0

.. Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

.. _relax:

Relax
=====
Relax is a high-level abstraction for graph optimization and transformation in Apache TVM stack.
Additionally, Apache TVM combine Relax and TensorIR together as a unity strategy for cross-level
optimization. Hence, Relax is usually working closely with TensorIR for representing and optimizing
the whole IRModule


.. toctree::
:maxdepth: 2

abstraction
learning
tutorials/relax_creation
tutorials/relax_transformation
Loading
Loading