Skip to content

Commit c9fd1d7

Browse files
MasterJH5574junrushao
authored andcommitted
[Analysis] Optionally check structure info in well-formedness check (apache#321)
With the introduction of structure info in apache#314, the well-formedness check will report malformed whenever an Expr doesn’t have defined structure info. However, when writing tests for well-formedness check and normalizer, usually we will manually construct the Exprs, which means their structure info are not defined most of the time. As a consequence, the well-formedness check will always complain “the Expr xxx doesn’t have structure info populated.” Therefore, when the checker fails to complain about the original reason of malformed, which means the checker is not working, the tests will still pass and we won’t be able to realize there is something wrong with the checker. Thus, in this PR we add an optional flag to the well-formedness check. In well-formedness tests, we will turn off the structure info check so that the original reason of being malformed will be revealed correctly. --- This PR also cleans up the DiagnosticContext parameter in the WellFormed API - the diag_ctx has been unused since the merge of apache#99.
1 parent 317c7d9 commit c9fd1d7

File tree

4 files changed

+127
-106
lines changed

4 files changed

+127
-106
lines changed

include/tvm/relax/analysis.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ TVM_DLL StructInfo EraseToWellDefined(const StructInfo& info, Map<tir::Var, Prim
200200
*
201201
* For a given pair of lhs_struct_info, rhs_struct_info. We adopt
202202
* the following terminology:
203-
* - LSet = {value | value mactches lhs_struct_info}
204-
* - RSet = {value | value mactches rhs_struct_info}
203+
* - LSet = {value | value matches lhs_struct_info}
204+
* - RSet = {value | value matches rhs_struct_info}
205205
*
206206
* See the definition of each level below.
207207
*/
@@ -283,11 +283,14 @@ TVM_DLL StructInfo StructInfoLCA(const StructInfo& lhs, const StructInfo& rhs,
283283
* \brief Check if the IRModule is well formed.
284284
*
285285
* \param m the IRModule to check.
286-
* \param diag_ctx the diagnostic context.
286+
* \param check_struct_info A boolean flag indicating if the property "every Expr
287+
* must have defined structure info" will be checked.
287288
* \return true if the IRModule is well formed, false if not.
289+
* \note By default the structure info is always checked. It is only in test cases
290+
* where `check_struct_info` might be false, so that other well-formed requirements
291+
* will be well tested and will not be blocked by not having structure info.
288292
*/
289-
TVM_DLL bool WellFormed(const IRModule& m,
290-
Optional<DiagnosticContext> diag_ctx = Optional<DiagnosticContext>());
293+
TVM_DLL bool WellFormed(IRModule m, bool check_struct_info = true);
291294

292295
/*!
293296
* \brief Annotate Op Pattern Kind for PrimFunc, which is used in relax FuseOps.
@@ -346,7 +349,7 @@ TVM_DLL tvm::Array<Var> FreeVars(const Expr& expr);
346349
TVM_DLL tvm::Array<Var> AllVars(const Expr& expr);
347350

348351
/*!
349-
* \brief Get all glabal variables used in calls in expression expr.
352+
* \brief Get all global variables used in calls in expression expr.
350353
*
351354
* \param expr the expression.
352355
*
@@ -355,7 +358,7 @@ TVM_DLL tvm::Array<Var> AllVars(const Expr& expr);
355358
TVM_DLL tvm::Array<GlobalVar> CalledGlobalVars(const Expr& expr);
356359

357360
/*!
358-
* \brief Get all glabal variables from expression expr.
361+
* \brief Get all global variables from expression expr.
359362
*
360363
* AllVars is a superset of BoundVars and FreeVars.
361364
* The union of BoundVars and FreeVars is Allvars.
@@ -402,15 +405,15 @@ TVM_DLL Map<String, Array<Binding>> NameToBinding(const Function& fn);
402405
* \brief Get the use-def chain of variables inside a dataflow block.
403406
*
404407
* \param dfb The dataflow block to be analyzed.
405-
* \return A map mapping variable definitoins to a set of uses.
408+
* \return A map mapping variable definitions to a set of uses.
406409
*/
407410
TVM_DLL Map<Var, Array<Var>> DataflowBlockUseDef(const DataflowBlock& dfb);
408411

409412
/*!
410413
* \brief Get the use-def chain of variables inside a function.
411414
*
412415
* \param fn The function to be analyzed.
413-
* \return A map from variable definitoins to a set of uses and variables needed by return value.
416+
* \return A map from variable definitions to a set of uses and variables needed by return value.
414417
*/
415418
std::pair<Map<Var, Array<Var>>, Array<Var>> FunctionUseDef(const Function& fn);
416419

python/tvm/relax/analysis/analysis.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,30 @@ def post_order_visit(expr, fvisit):
197197
return _ffi_api.post_order_visit(expr, fvisit) # type: ignore
198198

199199

200-
def well_formed(mod: tvm.IRModule) -> bool:
200+
def well_formed(mod: tvm.IRModule, check_struct_info: bool = True) -> bool:
201201
"""Check if the IRModule is well formed.
202202
203203
Parameters
204204
----------
205205
mod : tvm.IRModule
206206
The input IRModule.
207207
208+
check_struct_info : bool
209+
A boolean flag indicating if the property "every Expr must
210+
have defined structure info" will be checked.
211+
208212
Returns
209213
-------
210214
ret: bool
211215
True if the IRModule is well formed, False if not.
216+
217+
Note
218+
----
219+
By default the structure info is always checked. It is only in test cases
220+
where `check_struct_info` might be false, so that other well-formed requirements
221+
will be well tested and will not be blocked by not having structure info.
212222
"""
213-
return _ffi_api.well_formed(mod) # type: ignore
223+
return _ffi_api.well_formed(mod, check_struct_info) # type: ignore
214224

215225

216226
def get_var2val(func: Function) -> Dict[Var, Expr]:

0 commit comments

Comments
 (0)