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

[Relax] Require correct input/output shapes R.call_tir #17285

Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions include/tvm/relax/op_attr_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,40 @@ using FCallPacked = String;
* expressed in multiple syntactically valid and semantically
* equivalent forms, to normalize to a single representation.
*
* Note: `FNormalize` is applied for each expression as part of the
* `relax::BlockBuilder`. While operator-specific validation may
* be performed within the `FNormalize` implementation, ensuring
* that errors are caught as early as possible, this should only be
* used when validation is fast to apply. If the validation logic
* may be slow, it should instead be implemented in `FValidate`,
* which is only run as part of the well-formed checker.
*
* \param bb The BlockBuilder context.
*
* \param call The call to be normalized. It is provided by-value, to
* avoid copies for the common case where the call is already normalized.
*/
using FNormalize = runtime::TypedPackedFunc<Expr(const BlockBuilder& bb, Call call)>;

/*!
* \brief The function type of a validation function.
*
* A validation function is used to define constraints that should be
* verified for an operator as part of the well-formed checker.
*
* Note: `FValidate` is only applied as part of the well-formed
* checker. While this minimizes overhead while compiling Relax,
* this delay between generating an ill-formed `relax::Call` and
* identifying the ill-formed call may complicate debugging. If
* the validation logic is very fast to check, and doing so would
* not introduce a signficant overhead, consider validating as part
* of `FNormalize`, which is applied by the block builder for each
* `relax::Call`.
*
* \param call The call to be validated.
*/
using FValidate = runtime::TypedPackedFunc<void(const Call& call)>;

/*! \brief The function type of a legalization function.
*
* A legalization function is used to replace a `relax::Call` with
Expand Down
11 changes: 11 additions & 0 deletions src/relax/analysis/well_formed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ class WellFormedChecker : public relax::ExprVisitor,
<< after_normalize);
}
}

if (auto func_validate = op_map_validate_.get(call->op, nullptr); func_validate != nullptr) {
try {
func_validate(GetRef<Call>(call));
} catch (std::exception& err) {
Malformed(Diagnostic::Error(call) << "Operator-specific validation (FValidate) for "
<< call->op << " identified error: \n"
<< err.what());
}
}
}

void VisitExpr_(const IfNode* op) final {
Expand Down Expand Up @@ -574,6 +584,7 @@ class WellFormedChecker : public relax::ExprVisitor,
std::unordered_map<tir::Var, const FunctionNode*> symbolic_var_func_map_;

tvm::OpAttrMap<FNormalize> op_map_normalize_ = Op::GetAttrMap<FNormalize>("FNormalize");
tvm::OpAttrMap<FValidate> op_map_validate_ = Op::GetAttrMap<FValidate>("FValidate");
};

bool WellFormed(Variant<IRModule, Function> obj, bool check_struct_info) {
Expand Down
Loading
Loading