Skip to content

Commit 262f270

Browse files
authored
feat(sym): Introduce Symbolic Analysis (#45)
(Most of the code is directly copied from `tvm.arith` with very minimal code change) This PR establishes a submodule `mlc.sym.`, which does symbolic analysis and expression simplification for use cases in ML Compilers. It includes a few pieces: 1) Expression IR (`mlc.sym.expr`): It’s a subset of and thus fully compatible with TIR. It includes: - Leaf variables: Var, ShapeVar (nonnegative variables) - Immediate values: IntImm, FloatImm, BoolImm - Arithmetics: Add, Sub, Mul, Div, Mod, FloorDiv, FloorMod, Min, Max - Comparison: EQ, NE, LT, LE, GT, GE - Logical ops: And, Or, Not - Vectorization: Ramp, Broadcast, Shuffle - Others: Cast, Select, Let, Call 2) Analyzer (`mlc.sym.analyzer`): The prover and expression simplifier. It supports - `Analyzer.bind`: Bind a `Var` to a certain range, a constant value or an expression - `Analyzer.can_prove`: the prover, which is certainly specialized to bounds we particularly care about in deep learning, e.g. const int bound. This will not be as powerful as general-purpose solvers such as Z3, but should suffice for ML compiler use cases (and thus can be faster); - `Analyzer.simplify`: the expression simplifier for ML compiler use cases. 3) `mlc.sym.op`: convenient methods to construct expressions. Example: ```python from mlc import sym as S i0 = S.Var("i0", "int64") i1 = S.Var("i1", "int64") i = i0 * 3 + i1 j = i // 12 * 12 + i % 12 // 4 * 4 + i % 4 analyzer = S.Analyzer() analyzer.bind(i1, S.Range.from_const("int64", 0, 3)) result = analyzer.simplify(j) print(result) ```
1 parent aea14f3 commit 262f270

37 files changed

+14107
-0
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ add_library(mlc_objs OBJECT
5050
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/c_api.cc"
5151
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/printer.cc"
5252
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/structure.cc"
53+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/sym.cc"
54+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/text_format.cc"
55+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/analyzer_const_int_bound.cc"
56+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/analyzer_modular_set.cc"
57+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/analyzer_rewrite_simplify.cc"
58+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/analyzer_canonical_simplify.cc"
59+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/analyzer_interval_set.cc"
60+
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/sym/analyzer_transitive_comparisons.cc"
5361
)
5462
set_target_properties(
5563
mlc_objs PROPERTIES
@@ -63,6 +71,9 @@ set_target_properties(
6371
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"
6472
)
6573
add_cxx_warning(mlc_objs)
74+
if(MSVC)
75+
target_compile_options(mlc_objs PRIVATE /bigobj)
76+
endif()
6677
target_include_directories(mlc_objs PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
6778
target_include_directories(mlc_objs PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/dlpack/include")
6879
target_link_libraries(mlc_objs PUBLIC mlc::mlc_backtrace-static)

0 commit comments

Comments
 (0)