junrushao1994 commented on a change in pull request #5: URL: https://github.com/apache/tvm-rfcs/pull/5#discussion_r668319732
########## File path: rfcs/0001-meta-schedule-autotensorir.md ########## @@ -0,0 +1,444 @@ +<!--- 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. --> + +* Feature Name: Meta Schedule (Formerly AutoTIR) +* Start Date: 2021-05-28 +* RFC PR: TBD (apache/tvm-rfcs#0000) +* GitHub Issue: TBD (apache/tvm-rfcs#0000) + +## 1. Summary + +This proposal introduces Meta Schedule: a probabilistic scheduling DSL on TIR that unifies the +approaches of AutoTVM and Auto Scheduler (Ansor). Meta schedule provides a pragmatic way to define +the space of automatic tuning, extensibility in terms of all possible TIR schedule primitives like +tensorization and loop partitioning, and customizability on every layer of the automation system. + +Meta Schedule is our 3rd generation automatic scheduling system. + +## 2. Motivation + +**Scheduling and design space.** In TVM TensorIR, optimization of a TensorIR program is done via a +sequence of transformations. For example, we reorder loops for better locality and we tensorize for +specific hardware intrinsics. The process of invoking such a set of pre-defined transformations is +called "**scheduling**", and each transformation is called a "**schedule primitive**". These +primitives form a domain-specific language (DSL) describing the transformation of TensorIR programs. +**Design space** is the set of all possible schedulings with respect to a TensorIR program. + +**Problems with the current scheduling system.** Currently we have 3 sets of scheduling APIs: +* **Manual schedule**: Developers optimize their programs by manually invoking schedule primitives, + i.e. explore points in the design space with humans in the loop. This can be a tedious and + error-prone approach, hence the creation of AutoTVM and AutoScheduler (Ansor). +* **AutoTVM**: The automation system requires users to define "schedule templates" as the design + space for each operator. Therefore, it is inextensible to hundreds of operators and variety + hardware platforms. +* **AutoScheduler (Ansor)**: It automatically generates schedule templates as the design space, + according to a set of predefined "search rules". However, it is non-trivial to extend + AutoScheduler to new schedule primitives (tensorize, loop partition, software pipelining, etc). +* The three systems above have isolated sets of APIs with several layers of their own abstraction, + which are not only hard to learn, but also engineering-intensive to customize. + +**Benefits of Meta Schedule.** Meta schedule provides: +* Succinct syntax, consistent APIs to TensorIR schedule with no other layer of abstraction. +* Unified APIs for implementing manual schedules, AutoTVM-style schedules, and AutoScheduler-style + schedules. +* Extensibility of all the schedule primitives, including tensorization and loop partitioning. + Almost no extra effort is needed to use a new primitive in auto-tuning. +* The automation infrastructure is extensible on every of its components. Every component of the + system can be customized easily in pure python or C++ or both. For example, one can develop a new + design space generator in python, a new ProgramRunner in python, etc. + + +## 3. Guide-level explanation + +In this section, we describe the syntax of meta schedule DSL, and how it could be used to describe +and auto-generate the design space. + +### 3.1. Manual Schedule + +Meta schedule APIs are almost the same as TE or TensorIR scheduling. Here is an example of a manual +schedule for matrix multiplication: + +```python +# Designate a set of tile sizes +i_tiles = [16, 8, 8, 8] +j_tiles = [16, 8, 8, 8] +k_tiles = [256, 8] + +# Tile the loops according to the tile sizes +i_0, i_1, i_2, i_3 = sch.split(loop=i, factors=i_tiles) +j_0, j_1, j_2, j_3 = sch.split(loop=j, factors=j_tiles) +k_0, k_1 = sch.split(loop=k, factors=k_tiles) + +# Organize the loops into "SSRSRS" 6-level tiles +sch.reorder( + i_0, j_0, # S: the 1st spatial tile + i_1, j_1, # S: the 2nd spatial tile + k_0, # R: the 1st reduction tile + i_2, j_2, # S: the 3rd spatial tile + k_1, # R: the 2nd reduction tile + i_3, j_3, # S: the 4th spatial tile +) +``` + +In this example, the developers may tweak the tile sizes and measure the performance of the +generated kernels to explore the opportunities of potential optimization. + +Generally speaking, while writing a schedule, there are often some parameters that are hard to +determine ahead of time, for example, tile sizes, unroll steps, or which tensor intrinsics to use. +Developers may manually enumerate possible combinations of these unknown factors, and then pick the +best schedule according to measurement results on their device. + +### 3.2. Composite Schedule Rules + +As introduced in the previous section, in TensorIR, each schedule primitive handles only a very +basic transformation of the IR. For example, `split` only splits a loop into two new loops. In the +real world, the over-fine granularity of those primitives usually leads to repetitive and verbose +scheduling code, as +[mentioned](https://discuss.tvm.apache.org/t/rfc-tensorir-a-schedulable-ir-for-tvm/7872/43?u=junrushao1994) +by developers in our community. + +To make it more convenient and modular, we allow users to register "composite schedules" that apply +a sequence of schedule primitives according to certain analysis of the IR. For instance, a composite +schedule may inspect a TensorIR block and decide whether we should call `compute_inline` on it. + +### 3.3. AutoTVM-style Design Space Description + +Meta schedule extends the schedule DSL with sampling instructions. When included in a schedule, +these instructions parametrize the schedule from a single deterministic point to a space supported +by random variables (tile size, etc.), making it possible for developers to describe the design +space with meta schedule APIs. + +We can extend the matmul example above to cover all possible tilings using these sampling +instructions: + +```python +# Sample tile sizes +i_tiles = sch.sample_perfect_tile(i, n=4) +j_tiles = sch.sample_perfect_tile(j, n=4) +k_tiles = sch.sample_perfect_tile(k, n=2) +# Tile the loops according to the random variables +i_0, i_1, i_2, i_3 = sch.split(loop=i, factors=i_tiles) +j_0, j_1, j_2, j_3 = sch.split(loop=j, factors=j_tiles) +k_0, k_1 = sch.split(loop=k, factors=k_tiles) +# Organize the loops into "SSRSRS" 6-level tiles +sch.reorder( + i_0, j_0, # S: the 1st spatial tile + i_1, j_1, # S: the 2nd spatial tile + k_0, # R: the 1st reduction tile + i_2, j_2, # S: the 3rd spatial tile + k_1, # R: the 2nd reduction tile + i_3, j_3, # S: the 4th spatial tile +) +``` + +### 3.4. AutoScheduler-style Design Space Generation + +AutoScheduler (Ansor) generates schedule templates by applying their SearchRules to each stage. +SearchRule analyzes TE and eagerly trigger schedule primitives accordingly in its internally +maintained mini IR. + +As introduced in Section 3.2, composite schedule rules are equivalent to AutoScheduler's SearchRule +in TensorIR scheduling. To further generate a design space for scheduling, sampling instructions are Review comment: Revised as the following: ```markdown AutoScheduler (Ansor) generates schedule templates by applying a set of **SearchRule** to each stage. SearchRule analyzes TE and eagerly trigger schedule primitives accordingly in its internally maintained mini IR. Composite schedule rules work in a similar way scheduling TensorIR, as introduced in Section 3.2. It analyzes the TensorIR and apply schedule primitives directly to TensorIR accordingly. When applying such rules to each TensorIR block in certain order (e.g. Post-DFS Order), it generates a sequence of schedule primitives. If the sampling instructions are present in this sequence, the support of the probability space form a design space of possible schedulings. This process is similar to the *sketch generation* phase in AutoScheduler. ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org