Skip to content

Commit 73cad19

Browse files
authored
[Relax][VM] Improved error messages for mismatched parameter count (#17118)
This commit improves validation of the parameter names used for a Relax VM function definition, using the parameter names for runtime error messages.
1 parent 63f9cd6 commit 73cad19

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/relax/backend/vm/exec_builder.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ void ExecBuilderNode::EmitFunction(const std::string& func_name, int64_t num_inp
113113
ICHECK_EQ(vmfunc.num_args, -2) << "Function " << func_name << " already defined";
114114
vmfunc.num_args = num_inputs;
115115
if (param_names.defined()) {
116+
ICHECK_EQ(num_inputs, param_names.value().size())
117+
<< "Function " << func_name << " defined with " << num_inputs << " arguments, "
118+
<< "but the list of parameter names has " << param_names.value().size() << " names ("
119+
<< param_names << ")";
116120
std::vector<std::string> names;
117121
for (auto name : param_names.value()) {
118122
names.push_back(name);

src/runtime/relax_vm/vm.cc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,23 @@ RegType VirtualMachineImpl::InvokeBytecode(Index gf_idx, const std::vector<RegTy
678678
}
679679

680680
// load arguments to the register file
681-
ICHECK_EQ(static_cast<size_t>(gfunc.num_args), args.size())
682-
<< "ValueError: Invoking function " << gfunc.name << " requires " << gfunc.num_args
683-
<< " inputs but only " << args.size() << " inputs are provided.";
681+
ICHECK_EQ(static_cast<size_t>(gfunc.num_args), args.size()) << "ValueError: Invoking function "
682+
<< gfunc.name << " expects "
683+
<< gfunc.num_args << " arguments" <<
684+
[&]() {
685+
std::stringstream ss;
686+
if (gfunc.param_names.size()) {
687+
ss << " (";
688+
for (size_t i = 0; i < gfunc.param_names.size(); i++) {
689+
if (i) {
690+
ss << ", ";
691+
}
692+
ss << gfunc.param_names[i];
693+
}
694+
ss << ")";
695+
}
696+
return ss.str();
697+
}() << ", but " << args.size() << " arguments were provided.";
684698
for (size_t i = 0; i < args.size(); ++i) {
685699
WriteRegister(frames_.back().get(), i, args[i]);
686700
}

0 commit comments

Comments
 (0)