Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
176 commits
Select commit Hold shift + click to select a range
b8e4110
[Unity] Relax VM (#13878)
YuchenJin Feb 1, 2023
2c7f480
[Unity] Relax expressions and types (#13901)
YuchenJin Feb 2, 2023
4e659d1
[Unity][IR] First-class StructInfo (#13907)
YuchenJin Feb 3, 2023
fb90fd1
[Unity][CI] Unity specific jenkins setup (do not upstream to main) (#…
tqchen Feb 3, 2023
6915444
[Unity] Basic StructInfo Analysis and Expr construction (#13916)
YuchenJin Feb 5, 2023
d1ad4e6
[Unity] Relax BlockBuilder and ExprMutator (#13926)
YuchenJin Feb 7, 2023
2001903
[Unity] Relax TVMScript Parser. (#13932)
Feb 8, 2023
e38a336
[Unity] Relax TVMScript Printer (#13944)
junrushao Feb 10, 2023
ea6cc94
[Unity] Relax VM codegen (#13954)
YuchenJin Feb 11, 2023
2c15871
[Unity] Relax VM shape lowering pass (#13956)
YuchenJin Feb 11, 2023
fadbb3f
[Unity] e2e Relax minimum build flow (#13961)
YuchenJin Feb 11, 2023
75eecf7
[Unity][TVMScript] Use explicit `R.shape` in TVMScript (#13979)
Feb 14, 2023
de164d2
[Unity] Relax op: index (#13987)
MasterJH5574 Feb 14, 2023
c8a1533
[Unity] Relax op: datatype (#13986)
MasterJH5574 Feb 14, 2023
c7a57ae
[Unity] Relax op: set (#13990)
MasterJH5574 Feb 14, 2023
4240920
[Unity] Relax op: image (#13994)
MasterJH5574 Feb 14, 2023
5385d6d
[Unity] Relax op: arithmetic, comparison (#13983)
MasterJH5574 Feb 14, 2023
a6a2e84
[Unity] Relax op: statistical (#13991)
MasterJH5574 Feb 14, 2023
a96f200
[Unity] Relax op: neural networks (#13993)
MasterJH5574 Feb 14, 2023
4dd591b
[Unity] Relax op: creation (#13984)
MasterJH5574 Feb 14, 2023
9694c67
[Unity] Relax op: linear algebra (#13988)
MasterJH5574 Feb 14, 2023
7a8765d
[Unity] Relax op: search (#13992)
MasterJH5574 Feb 14, 2023
4ab73ea
[Unity] Relax op: manipulation (#13989)
MasterJH5574 Feb 14, 2023
62daae4
[Unity] NestedMsg Support utility (#13995)
tqchen Feb 14, 2023
20adb37
[Unity][Pass] Operator Fusion Passes (#14001)
Feb 15, 2023
e78d523
[Unity][Pass] LambdaLift pass (#14012)
yongwww Feb 16, 2023
0e2bb80
[Unity][VM] Supporting "compiled" exec mode. (#14015)
tqchen Feb 17, 2023
63e2402
[Unity][Pass] BindParams pass, FoldConstant pass (#14016)
sunggg Feb 17, 2023
87659ea
[Unity][Pass][TuningAPI] Introduce TuningAPI and MetaSchedule pass (#…
sunggg Feb 17, 2023
388941a
[Unity] Relay -> Relax translator (#14026)
YuchenJin Feb 17, 2023
0d91c33
[Unity][Pass] Normalize Pass (#14031)
LeshengJin Feb 18, 2023
251a062
[Unity][BlockBuilder] CallTE convert PrimValue args (#14028)
MasterJH5574 Feb 18, 2023
b5d2304
[Unity][Pass] Wellformed Analysis (#14032)
LeshengJin Feb 18, 2023
5e2f2b9
[Unity][TVMScript] Move tir/relax import in script out of __init__.py…
MasterJH5574 Feb 18, 2023
814eb92
[Unity][Pass] Operator legalization (#14029)
MasterJH5574 Feb 18, 2023
130e362
[Unity][Op] Add ShapeExpr Tests for Reshape Op (#14035)
Ubospica Feb 18, 2023
85477ac
[Unity] Initial PyTorch Frontend (#14037)
MasterJH5574 Feb 18, 2023
96a9b6e
[Unity][Pass] Block-level static memory planning (#14038)
MasterJH5574 Feb 18, 2023
837a557
[Unity] Disallow inline prim_func in relax IR (#14040)
yongwww Feb 18, 2023
d349493
[Unity] Update tests to adapt to latest TVMScript syntax (#14039)
MasterJH5574 Feb 18, 2023
02cefd9
[Unity] Relax dataflow pattern language (matching) (#14041)
ganler Feb 18, 2023
f428a4a
[Unity] Statement rewriter for DataflowBlock (#14043)
ganler Feb 19, 2023
ac5bf3a
[Unity][Pass] FuseOps FuseTIR fixes (#14044)
MasterJH5574 Feb 19, 2023
81169f6
[Unity][TVMScript] Overload `__neg__` for relax expr (#14045)
SiriusNEO Feb 19, 2023
2bd1581
[Unity][VM] Add per-op profiling support (#14053)
masahi Feb 20, 2023
591b800
[Unity][BYOC] Add pattern-based partitioning pass (#14054)
masahi Feb 20, 2023
ff7d495
[Unity] Relax op: collapse sum (#14059)
SiriusNEO Feb 21, 2023
828edeb
[Unity][Fix][Pass] Fix FuseOps for lack graph edges (#14058)
MasterJH5574 Feb 21, 2023
7be4441
[Unity][Pass] Remove Unused Function (#14061)
sunggg Feb 21, 2023
0bd303c
[Unity][BYOC] Add pass to merge composite functions to offload large …
masahi Feb 21, 2023
466a004
[Unity][Frontend] Annotate number of non-static input of FX function …
vinx13 Feb 21, 2023
51b1ce1
[Unity][Transform] Add LiftTransformParams pass (#14069)
vinx13 Feb 21, 2023
6ba5cac
[Unity][BYOC][Pass] RunCodegen and TensorRT (#14078)
sunggg Feb 22, 2023
4b3794c
[Unity][Pass] Canonicalize Bindings (#14079)
YuchenJin Feb 22, 2023
d5fa61f
[Unity] Add testcases for `expr_args_converter` (#14080)
Feb 22, 2023
fe7e065
[Unity][BYOC] Add CUTLASS backend (#14081)
masahi Feb 22, 2023
f3ee944
[Unity][BYOC] Add DNNL backend (#14082)
masahi Feb 22, 2023
3a0f4c5
[Unity][Op] `log_softmax` and `cross_entropy_with_logits` (#14083)
SiriusNEO Feb 22, 2023
5aecfe4
[Unity][Analysis] TIR pattern kind analysis for multi-buffer write bl…
MasterJH5574 Feb 22, 2023
22c7b75
[Unity][Fix][Pass] FoldConstant with DCE in dataflow block (#14087)
MasterJH5574 Feb 22, 2023
99f6d67
[Unity] Refactor Relax Build JIT UX (#14088)
tqchen Feb 22, 2023
f15b80a
[Unity][Relax] Set Shape Function to Be Host Function (#14090)
zxybazh Feb 22, 2023
0eff29a
[Unity] Fix typo in the comment (#14096)
vinx13 Feb 22, 2023
03799a5
[Unity] Lower `shape_of` to a builtin (#14093)
YuchenJin Feb 22, 2023
70c8deb
[Unity] Relax Recursive function (#14092)
yongwww Feb 23, 2023
9df23f7
[Unity][Layout] Add layout transformation analysis for PrimFunc (#14066)
psrivas2 Feb 23, 2023
a31c856
[Unity] Remove attributes of relax.print, assert and unique (#14101)
yongwww Feb 23, 2023
b168949
[Unity][BYOC]Add relax backend pattern registry (#14106)
yelite Feb 24, 2023
fbf5647
[Unity] Update tests again to adapt to latest TVMScript syntax (#14115)
Ubospica Feb 24, 2023
4e5e81a
[Unity][Fix] Fix bug in MergeCompositeFunctions (#14117)
Ubospica Feb 24, 2023
cf9beab
[Unity][BlockBuilder] Add `name_hint` argument for `emit` and `emit_o…
SiriusNEO Feb 25, 2023
133b4ac
[Unity][WEB] Relax vm on web runtime (#14131)
tqchen Feb 25, 2023
fa47ee9
[Unity] Add Global info (#14132)
jinhongyii Feb 26, 2023
5939a6e
[Unity][BYOC] Add transposed matmul support to Relax CUTLASS BYOC (#1…
yelite Feb 27, 2023
1004bdf
[Unity][TVMScript] emit_te sugar (#14123)
yongwww Feb 27, 2023
a16021a
[Unity][BYOC] Assign group to unused bindings and ignroe PrimFunc (#1…
vinx13 Feb 27, 2023
3bdd801
[Unity] Add callback to FuseOpsByPattern to check match result is acc…
vinx13 Feb 27, 2023
019ef59
[Unity][Legalize] Fix Scalar Constant Legalization (#14127)
zxybazh Feb 28, 2023
43c5f29
[Unity][Pass] Enhance constant folding to fold relax ops by evaluatin…
psrivas2 Feb 28, 2023
82bfc57
[Unity][Debugging] AST printer (#14152)
slyubomirsky Mar 1, 2023
7e96a3a
[Unity][Pass] Support Symbolic Shape Deduction during BindParam (#14154)
Mar 1, 2023
1cdc3d3
[Unity][Analysis] Checking function return struct info in well-formed…
Mar 1, 2023
ef0f448
[Unity][BYOC] Use Relax legalize + CPU build for reference in tests (…
masahi Mar 1, 2023
7cad6ef
[Unity] Add bind_constants option to FuseOpsByPattern (#14151)
vinx13 Mar 1, 2023
c56b17f
[Unity][Analysis] Analysis for detecting recursion in Relax (#14149)
slyubomirsky Mar 1, 2023
1bbe881
[Unity][BYOC] Add batch matmul support to Relax CUTLASS BYOC (#14166)
yelite Mar 2, 2023
be532f2
[Unity][Op] Full support of Relax op `power` (#14171)
SiriusNEO Mar 2, 2023
1cc9bb0
[Unity][Analysis] Restore Python bindings for var analyses (#14180)
slyubomirsky Mar 4, 2023
8a46c21
[Unity][OP] Add an operator for fused multi head attention (#14150)
cyx-6 Mar 4, 2023
281cc20
[Unity][WEBGPU] Codegen improvements and WebRuntime (#14187)
tqchen Mar 4, 2023
cd88b0a
[Unity][Transform] LiftTransformParams handling multiple functions (#…
MasterJH5574 Mar 4, 2023
1927d7d
[Unity][Op] Group normalization (#14194)
MasterJH5574 Mar 4, 2023
50c1e7a
[Unity][Op] Argmax and argmin (#14195)
MasterJH5574 Mar 5, 2023
6731783
[Unity][Op] Legalize `round`, `floor`, `ceil`, `sign` (#14198)
MasterJH5574 Mar 5, 2023
439ec78
[Unity][Frontend] FX translator supporting more ops (#14196)
MasterJH5574 Mar 5, 2023
ec6e268
[Unity][Frontend] FX translator returning weights with `keep_params_a…
MasterJH5574 Mar 5, 2023
fb6b1ea
[Unity][Fix] FX translating dtype (#14201)
MasterJH5574 Mar 5, 2023
3b7db40
[Unity][TIR][Pass] ForceNarrowIndexToInt32 (#14203)
MasterJH5574 Mar 6, 2023
044080f
[Unity][Frontend] FX translator support torch.baddbmm (#14202)
MasterJH5574 Mar 6, 2023
b7193cf
[Unity] Introduce Default GPU Schedule Pass (#14182)
zxybazh Mar 6, 2023
399d9da
[Unity][Frontend] Attach imported model weights, deprecate ImporterOu…
MasterJH5574 Mar 6, 2023
c422505
[Unity][Frontend] FX translator supports unwrapping unit return tuple…
MasterJH5574 Mar 6, 2023
1ffc317
[Unity][WEB] Improve webgpu codegen options to skip readonly (#14213)
tqchen Mar 6, 2023
841f8a0
[Unity][Frontend] from_fx keeps parameters in order (#14214)
MasterJH5574 Mar 6, 2023
6cb1fe7
[Unity][BYOC] Add dynamic shape support to CUTLASS matmul (#14216)
yelite Mar 7, 2023
84c20b3
[Unity][Transform] Memory plan across the IRModule (#14220)
MasterJH5574 Mar 7, 2023
2531c7e
[Unity][WEB] Simplify WebGPU Codegen per spec (#14225)
tqchen Mar 7, 2023
afdf218
[Unity] ensure memory.alloc_tensor/storage roundtrippable (#14226)
yongwww Mar 7, 2023
6b75a40
[Unity] Improve implementation of FuseOps (#14229)
tqchen Mar 8, 2023
13c8c67
[Unity][Transform] SimplifyNormInference (#14221)
SiriusNEO Mar 8, 2023
f2804d1
[Unity][Bugfix] Do not include `PrimFunc`s in the dependency graph wh…
slyubomirsky Mar 8, 2023
198caa5
[Unity][TVMScript] Enable Context-Aware Parsing (#14234)
Mar 8, 2023
b270be8
[Unity][TVMScript] Fix prim_func lost issue in relax.emit_te (#14189)
yongwww Mar 8, 2023
81c38c5
[Unity][WEB] Update text prompts for syntactical correctness (#14237)
MasterJH5574 Mar 8, 2023
4c90f05
[Unity][WEB] Improve ndarray cache (#14236)
tqchen Mar 8, 2023
9b757c9
[Unity][Op][Tweak] Improve `StructInfo` inference for `shape_of` (#14…
slyubomirsky Mar 9, 2023
f5b6ac8
[Unity][Op] Add repeat, tile, conv2d_transpose, avg_pool2d (#14238)
Ubospica Mar 9, 2023
24b8e7b
[Unity] Allow user defined func attrs in emit_te (#14255)
psrivas2 Mar 9, 2023
6ae1c52
[Unity][BYOC] Add conv2d and residual block patterns for Relax cutlas…
yelite Mar 9, 2023
1456f99
[Unity][Frontend] Import `tanh` and fix `layer_norm` (#14247)
SiriusNEO Mar 10, 2023
72c9510
[Unity] Consider target context for Relay to Relax conversion (#14269)
ibsidorenko Mar 10, 2023
7f89e22
[Unity] Introduce call_dps_packed (#14183)
yongwww Mar 10, 2023
e6f3db1
[Unity][ci] Use CPU-SMALL instances (#14256)
driazati Mar 11, 2023
581889a
[TVMScript][Unity] Improve PyLint Compatibility (#14276)
Mar 12, 2023
8c34de2
[Unity] Mark tests that need python3.8 compact.
tqchen Mar 13, 2023
c7f40bd
[Unity] Fix StructInfo Infer for `vm.alloc_tensor` (#14283)
Mar 13, 2023
c1783b8
[Unity][Op] Cumsum (#14297)
SiriusNEO Mar 14, 2023
15de2c2
[Unity][DEBUG] Add Instrument (#14302)
tqchen Mar 15, 2023
765375f
[Unity][Pass] Add a pass to alter the TIR implementation of an operat…
psrivas2 Mar 15, 2023
b10498b
[Unity][Op] Enable special dimension value 0 in reshape (#14311)
Mar 16, 2023
001f178
[Unity][Web] WebGPU explicit max buffer size (#14321)
MasterJH5574 Mar 16, 2023
f5ee097
[Unity] Support pattern-based rewriting (#14312)
masahi Mar 16, 2023
10b834f
[Unity][BYOC] Support matmul + residual block fusion in CUTLASS BYOC …
masahi Mar 16, 2023
981a822
[Unity][BYOC] Improve expressiveness of the pattern check function in…
yelite Mar 17, 2023
850e549
[Unity][Transform] Enhance RewriteDataflowReshape transform (#14265)
spectrometerHBH Mar 18, 2023
e2b1d93
[Unity][Contrib] Introduce several features of cutlass profiler (#14275)
spectrometerHBH Mar 18, 2023
5529830
[Unity][TOPI] fp16 LayerNorm & GroupNorm (#14264)
spectrometerHBH Mar 19, 2023
fdf86e4
[Unity][Transform] Automatic Layout Conversion (#14257)
spectrometerHBH Mar 19, 2023
3ca23b3
[Unity][Transform] Simple Dead Code Elimination (#14262)
spectrometerHBH Mar 19, 2023
795568e
[Unity][Transform] Automatic Mixed Precision (#14242)
spectrometerHBH Mar 19, 2023
1d35ef2
[Unity][Lint] Fix cpplint casting (#14333)
ganler Mar 19, 2023
0b47f0b
[Unity][Transform] DefaultSchedule pass (#14266)
spectrometerHBH Mar 19, 2023
9aae685
[Unity][Op] Fix Strided Slice Shape Inference (#14324)
zxybazh Mar 20, 2023
a0bd299
[Unity][Pass] Reuse prior infra to implement more complete DCE (#14334)
ganler Mar 20, 2023
06fe80b
[Unity] Remove Python interface of RemoveUnusedFunction (#14336)
MasterJH5574 Mar 20, 2023
6c69859
[Unity][BYOC] Update testcases to follow recent changes (#14339)
sunggg Mar 20, 2023
18c19fb
[Unity][Frontend] FX exp and strided_slice fix (#14338)
MasterJH5574 Mar 20, 2023
4d57272
[Unity] Support model kwargs in dynamo_capture_subgraph (#14349)
jinhongyii Mar 21, 2023
8b5d64a
[Unity][BYOC] Check leaked intermediate variables in cutlass patterns…
yelite Mar 21, 2023
c6cf403
[Unity][Transform] AMP out_dtype=float16 testcases (#14358)
spectrometerHBH Mar 21, 2023
922e18c
[Unity][Fix] Fix block memory plan to handle bool (#14357)
psrivas2 Mar 21, 2023
675a22e
[Unity][Transform] Introduce data-dependent operation of reshape and …
sunggg Mar 21, 2023
b78ead8
[Unity][Transform] Fix AMP tests (#14360)
spectrometerHBH Mar 21, 2023
2073063
[Unity] Add support to append relay op attrs in translator (#14356)
quic-sanirudh Mar 21, 2023
ee5a2c2
[Unity][WEB] Support async pipeline creation (#14362)
tqchen Mar 22, 2023
ecdc68c
[Unity][Pass] Fix FuseOps error if there is no output of a given grou…
Mar 22, 2023
04c6d7a
[Unity][Fix] Infer Layout must support negative axes (#14365)
psrivas2 Mar 22, 2023
db96ee8
[Unity] Add More Ops For FX Translator (#14348)
jinhongyii Mar 22, 2023
043e4f1
[Unity][TVMScript] Update GlobalVar `checked_type_` when `emit_te` (#…
Mar 22, 2023
b8eb779
[Unity][Fix] Allow scalar layout initialization (#14370)
psrivas2 Mar 22, 2023
4e46ad4
[Unity] Also include output dtype in simt MathInstruction (#14372)
areusch Mar 22, 2023
559fac7
[Unity][VM] Add CUDA graph vm builtins (#14371)
vinx13 Mar 22, 2023
cc03017
[Unity] Add missing #include <array> (#14383)
Mar 23, 2023
517d045
[Unity][Transform] SplitCallTIRByPattern and CUTLASS backend (#14274)
spectrometerHBH Mar 24, 2023
23803b7
[Unity] Support simple dynamic-shape-aware fusion (#14396)
Mar 24, 2023
eff9a0a
[Unity][Op] Add stop_lift_params (#14368)
jinhongyii Mar 25, 2023
d377f69
[Unity][TVMScript] Fix Shape Var occurrence in Tensor annotation (#14…
Ubospica Mar 26, 2023
f4d5964
[Unity][Transform] Common Subexpression Elimination (#14361)
slyubomirsky Mar 27, 2023
d97c43b
[Unity][QNN][Hexagon]Support Relax Constants in the QNN TOPI operatio…
farshidsp Mar 27, 2023
9a3ec23
[Unity][Op] Conv1d (#14388)
LeshengJin Mar 27, 2023
cd3e107
[Unity] Fix getting shapes for cutlass BYOC kernels (#14411)
vinx13 Mar 28, 2023
23146d6
[Unity][Op] Expose scale in `R.nn.attention` and add its legalize op …
cyx-6 Mar 28, 2023
854f2e9
[Unity][Hexagon] Enable Relax VM for Hexagon (#14415)
ibsidorenko Mar 28, 2023
51b29ef
[Unity][Fix] Copy over module attrs in FuseTIR (#14418)
psrivas2 Mar 29, 2023
465994e
[Unity] Handle extern func calls in static memory planning (#14419)
vinx13 Mar 29, 2023
9a2a244
[Unity][Hexagon] Allow scalar tensors to have null shape during alloc…
Mar 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion 3rdparty/cutlass
Submodule cutlass updated 25 files
+1 −1 docs/annotated.html
+1 −1 docs/default__mma__core__simt_8h_source.html
+1 −1 docs/hierarchy.html
+1 −1 docs/namespacecutlass_1_1transform.html
+1 −1 docs/pitch__linear__thread__map_8h.html
+1 −1 docs/pitch__linear__thread__map_8h_source.html
+1 −1 docs/structcutlass_1_1transform_1_1TransposePitchLinearThreadMap2DThreadTile.html
+46 −7 examples/41_fused_multi_head_attention/debug_utils.h
+6 −6 examples/41_fused_multi_head_attention/default_fmha_grouped.h
+0 −0 examples/41_fused_multi_head_attention/epilogue/epilogue_pipelined.h
+0 −0 examples/41_fused_multi_head_attention/epilogue/epilogue_rescale_output.h
+0 −0 examples/41_fused_multi_head_attention/epilogue/epilogue_thread_apply_logsumexp.h
+132 −11 examples/41_fused_multi_head_attention/fmha_grouped.h
+7 −2 examples/41_fused_multi_head_attention/fused_multihead_attention_fixed_seqlen.cu
+2 −0 examples/41_fused_multi_head_attention/gemm/find_default_mma.h
+17 −152 examples/41_fused_multi_head_attention/gemm/mma_accum_lambda_iterator.h
+263 −23 examples/41_fused_multi_head_attention/gemm/mma_from_smem.h
+437 −52 examples/41_fused_multi_head_attention/kernel_forward.h
+88 −0 examples/41_fused_multi_head_attention/transform/tile_smem_loader.h
+1 −1 include/cutlass/gemm/device/base_grouped.h
+52 −126 include/cutlass/gemm/kernel/gemm_universal_streamk.h
+2 −9 include/cutlass/gemm/threadblock/threadblock_swizzle_streamk.h
+1 −1 include/cutlass/transform/pitch_linear_thread_map.h
+1 −1 test/unit/common/cutlass_unit_test.h
+36 −16 tools/library/scripts/generator.py
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ tvm_file_glob(GLOB_RECURSE COMPILER_SRCS
src/driver/*.cc
src/support/*.cc
src/script/*.cc
src/relax/ir/*.cc
src/relax/op/*.cc
src/relax/analysis/*.cc
src/relax/transform/*.cc
src/relax/backend/vm/*.cc
src/relax/backend/task_extraction.cc
src/relax/backend/pattern_registry.cc
src/relax/utils.cc
)

tvm_file_glob(GLOB CODEGEN_SRCS
Expand Down Expand Up @@ -335,6 +343,7 @@ tvm_file_glob(GLOB RUNTIME_SRCS
src/runtime/*.cc
src/runtime/vm/*.cc
src/runtime/minrpc/*.cc
src/runtime/relax_vm/*.cc
)

if(BUILD_FOR_HEXAGON)
Expand Down
253 changes: 253 additions & 0 deletions apps/relax_examples/e2e_auto_tir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
# 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.
import datetime
import os
import csv
import json
import argparse
import logging
from typing import Dict
import numpy as np # type: ignore

import tvm
from tvm import relay, relax, runtime, transform
from tvm.ir.module import IRModule
from tvm import meta_schedule as ms
from tvm.meta_schedule.testing.relay_workload import get_network
from tvm.meta_schedule.testing.custom_builder_runner import run_module_via_rpc
from tvm.relax.testing import relay_translator
from tvm.target.target import Target


def _parse_args():
args = argparse.ArgumentParser()
args.add_argument(
"--workload",
type=str,
required=True,
)
args.add_argument(
"--input-shape",
type=str,
required=True,
)
args.add_argument(
"--target",
type=str,
required=True,
)
args.add_argument(
"--num-trials",
type=int,
required=True,
)
args.add_argument(
"--rpc-host",
type=str,
default=None,
)
args.add_argument(
"--rpc-port",
type=int,
default=None,
)
args.add_argument(
"--rpc-key",
type=str,
default=None,
)
args.add_argument(
"--work-dir",
type=str,
required=True,
)
args.add_argument(
"--cache-dir",
type=str,
default=None,
)
args.add_argument(
"--rpc-timeout-sec",
type=int,
default=180,
)
args.add_argument("--num-measurement-repeats", type=int, default=5)
args.add_argument("--num-measurements", type=int, default=10)
args.add_argument("--results-file", type=str, required=False, default=None)
parsed = args.parse_args()
parsed.target = tvm.target.Target(parsed.target)
parsed.input_shape = json.loads(parsed.input_shape)
if parsed.target.attrs.get("mtriple", None) == "aarch64-linux-gnu":
parsed.alloc_repeat = 3
else:
parsed.alloc_repeat = 1
if parsed.rpc_host and parsed.rpc_port and parsed.rpc_key:
parsed.rpc_config = ms.runner.RPCConfig(
tracker_host=parsed.rpc_host,
tracker_port=parsed.rpc_port,
tracker_key=parsed.rpc_key,
session_timeout_sec=parsed.rpc_timeout_sec,
)
parsed.workers = parsed.rpc_config.count_num_servers(allow_missing=False)
else:
# check all rpc configs are None
assert (
(parsed.rpc_host is None) and (parsed.rpc_port is None) and (parsed.rpc_key is None)
), "Please set all 'rpc_host', 'rpc_port' and 'rpc_key' to use PRC server"
parsed.rpc_config = None
parsed.workers = 1
return parsed


logging.basicConfig()
logging.getLogger("tvm.meta_schedule").setLevel(logging.DEBUG)
ARGS = _parse_args()


def apply_opt_before_tuning(
relay_mod: IRModule, params: Dict[str, runtime.NDArray], target: Target
):
with transform.PassContext(opt_level=3):
main_func = relay_mod["main"]
bind_main_func = relay.build_module.bind_params_by_name(main_func, params)
relay_mod = IRModule.from_expr(bind_main_func)
relay_mod = relay.transform.SimplifyInference()(relay_mod)
relay_mod = relay.transform.FoldConstant()(relay_mod)
relay_mod = relay.transform.FoldScaleAxis()(relay_mod)
relay_mod = relay.transform.CanonicalizeOps()(relay_mod)
relay_mod = relay.transform.AlterOpLayout()(relay_mod)
relay_mod = relay.transform.FoldConstant()(relay_mod)

relax_mod = relay_translator.from_relay(relay_mod["main"], target=target)
relax_mod = relax.transform.AnnotateTIROpPattern()(relax_mod)
relax_mod = relax.transform.FuseOps()(relax_mod)
relax_mod = relax.transform.FuseTIR()(relax_mod)
return relax_mod


def f_measurement(
rt_mod: runtime.Module, device: runtime.ndarray.Device, input_data: Dict[str, runtime.NDArray]
):
vm = relax.VirtualMachine(rt_mod, device=device)
vm.save_function("main", "measure_func", **input_data, include_return=False)
evaluator = vm.time_evaluator(
func_name="measure_func",
dev=device,
repeat=ARGS.num_measurement_repeats,
number=ARGS.num_measurements,
min_repeat_ms=500,
)
return evaluator()


def get_runner():
runner_config = {
"evaluator_config": ms.runner.EvaluatorConfig(
number=3,
repeat=1,
min_repeat_ms=100,
enable_cpu_cache_flush=False,
),
"alloc_repeat": ARGS.alloc_repeat,
}
if ARGS.rpc_config:
runner = ms.runner.RPCRunner(
rpc_config=ARGS.rpc_config, max_workers=ARGS.workers, **runner_config
)
else:
runner = ms.runner.LocalRunner(**runner_config)

return runner


def main():
relay_mod, params, (input_name, input_shape, input_dtype) = get_network(
ARGS.workload,
ARGS.input_shape,
cache_dir=ARGS.cache_dir,
)
input_info = {input_name: input_shape}
input_data = {}
for input_name, input_shape in input_info.items():
print(f" input_name: {input_name}")
print(f" input_shape: {input_shape}")
print(f" input_dtype: {input_dtype}")

# translate the ResNet model from Relay to Relax
relax_mod = apply_opt_before_tuning(relay_mod, params, target=ARGS.target)
assert isinstance(relax_mod, tvm.IRModule)

db = ms.relax_integration.tune_relax(
mod=relax_mod,
target=ARGS.target,
params=params,
num_trials_per_iter=64,
max_trials_per_task=ARGS.num_trials,
max_trials_global=ARGS.num_trials,
runner=get_runner(),
work_dir=ARGS.work_dir,
)
executable = ms.relax_integration.compile_relax(
db,
mod=relax_mod,
target=ARGS.target,
params=params,
)

for input_name, input_shape in input_info.items():
if input_dtype.startswith("float"):
input_data[input_name] = np.random.uniform(size=input_shape).astype(input_dtype)
else:
input_data[input_name] = np.random.randint(
low=0, high=10000, size=input_shape, dtype=input_dtype
)

# for documentation purposes
start_time = datetime.datetime.now()

if ARGS.rpc_config:
result = run_module_via_rpc(
rpc_config=ARGS.rpc_config,
lib=executable.mod,
dev_type=ARGS.target.kind.name,
args=input_data,
continuation=f_measurement,
)
else:
dev = tvm.device(ARGS.target.kind.name)
result = f_measurement(executable.mod, dev, input_data)

print(result)

if not ARGS.results_file:
return

out_path = os.path.abspath(os.path.expanduser(ARGS.results_file))
with open(out_path, "w") as out_file:
writer = csv.writer(out_file)
# write experiment parameters at the top as a record
writer.writerow(["start", str(start_time)])
writer.writerow(["workload", ARGS.workload])
writer.writerow(["input_shape", ARGS.input_shape])
writer.writerow(["target", ARGS.target])
writer.writerow(["num_measurement_repeats", ARGS.num_measurement_repeats])
for res in result.results:
writer.writerow([str(res)])


if __name__ == "__main__":
main()
57 changes: 57 additions & 0 deletions apps/relax_examples/mlp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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.

# Example code on creating, compiling, and running an MLP model in relax


import tvm
from tvm import relax, tir, topi
import numpy as np


def build_mlp(data, weight):
bb = relax.BlockBuilder()

with bb.function("mlp", [data, weight]):
gv0 = bb.emit_te(tvm.contrib.cblas.matmul, data, weight, transa=False, transb=False)
gv1 = bb.emit_te(topi.nn.relu, gv0)
bb.emit_func_output(gv1)

mod = bb.get()
return mod


if __name__ == "__main__":
# symbolic dimensions
n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
# create data and weight variables
data = relax.Var("data", relax.TensorStructInfo([n, m], "float32"))
weight = relax.Var("weight", relax.TensorStructInfo([m, n], "float32"))

# construct a mlp model
mod = build_mlp(data, weight)

# build and create vm executor
target = tvm.target.Target("llvm", host="llvm")
ex = relax.build(mod, target)
vm = relax.VirtualMachine(ex, tvm.cpu())

# run the mlp model on relax vm
data = tvm.nd.array(np.random.rand(16, 32).astype(np.float32))
weight = tvm.nd.array(np.random.rand(32, 16).astype(np.float32))
res = vm["mlp"](data, weight)
print(res)
Loading