Skip to content

Commit

Permalink
[Relax][VM] Improved error messages for mismatched parameter count (#…
Browse files Browse the repository at this point in the history
…17118)

This commit improves validation of the parameter names used for a
Relax VM function definition, using the parameter names for runtime
error messages.
  • Loading branch information
Lunderberg authored Jun 27, 2024
1 parent 63f9cd6 commit 73cad19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/relax/backend/vm/exec_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ void ExecBuilderNode::EmitFunction(const std::string& func_name, int64_t num_inp
ICHECK_EQ(vmfunc.num_args, -2) << "Function " << func_name << " already defined";
vmfunc.num_args = num_inputs;
if (param_names.defined()) {
ICHECK_EQ(num_inputs, param_names.value().size())
<< "Function " << func_name << " defined with " << num_inputs << " arguments, "
<< "but the list of parameter names has " << param_names.value().size() << " names ("
<< param_names << ")";
std::vector<std::string> names;
for (auto name : param_names.value()) {
names.push_back(name);
Expand Down
20 changes: 17 additions & 3 deletions src/runtime/relax_vm/vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,23 @@ RegType VirtualMachineImpl::InvokeBytecode(Index gf_idx, const std::vector<RegTy
}

// load arguments to the register file
ICHECK_EQ(static_cast<size_t>(gfunc.num_args), args.size())
<< "ValueError: Invoking function " << gfunc.name << " requires " << gfunc.num_args
<< " inputs but only " << args.size() << " inputs are provided.";
ICHECK_EQ(static_cast<size_t>(gfunc.num_args), args.size()) << "ValueError: Invoking function "
<< gfunc.name << " expects "
<< gfunc.num_args << " arguments" <<
[&]() {
std::stringstream ss;
if (gfunc.param_names.size()) {
ss << " (";
for (size_t i = 0; i < gfunc.param_names.size(); i++) {
if (i) {
ss << ", ";
}
ss << gfunc.param_names[i];
}
ss << ")";
}
return ss.str();
}() << ", but " << args.size() << " arguments were provided.";
for (size_t i = 0; i < args.size(); ++i) {
WriteRegister(frames_.back().get(), i, args[i]);
}
Expand Down

0 comments on commit 73cad19

Please sign in to comment.