Skip to content

Commit

Permalink
[CMSIS-NN] enable USMP with CMSIS-NN
Browse files Browse the repository at this point in the history
"NULL" should be supplied as an extern arg if
the context buffer is not allocated.

Change-Id: I58866df69a27ce976ac45a57e2f261094a48e48e
  • Loading branch information
manupak committed Feb 24, 2022
1 parent 0e4b0d0 commit 8c711d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 6 additions & 8 deletions src/relay/backend/contrib/cmsisnn/relay_to_tir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class RelayToTIRVisitor : public MixedModeMutator {
void CreatePrimFuncForExtern(const GlobalVar& global_var, Array<tir::Var> func_signature,
const Map<tir::Var, tir::Buffer>& buffer_map,
tvm::Array<PrimExpr> call_extern_args,
tir::Var context_buffer_var = tir::Var{"NULL"},
PrimExpr context_buffer_var = PrimExpr(),
int context_buffer_size = 0) {
Map<String, ObjectRef> dict_attrs;
dict_attrs.Set(tvm::attr::kGlobalSymbol, global_var->name_hint);
Expand All @@ -103,8 +103,8 @@ class RelayToTIRVisitor : public MixedModeMutator {
tvm::tir::Call(DataType::Int(8), tir::builtin::call_extern(), call_extern_args));

if (context_buffer_size) {
body = tir::Allocate(context_buffer_var, DataType::Int(8), {context_buffer_size},
tir::const_true(), body);
body = tir::Allocate(Downcast<tir::Var>(context_buffer_var), DataType::Int(8),
{context_buffer_size}, tir::const_true(), body);
}

tir::PrimFunc replacement_func(func_signature, body, VoidType(), buffer_map,
Expand Down Expand Up @@ -233,7 +233,7 @@ class RelayToTIRVisitor : public MixedModeMutator {
call_ext_args.push_back(shift);
call_ext_args.push_back(output);

tir::Var buffer_var{"NULL"};
PrimExpr buffer_var = tir::StringImm("NULL");
CMSISNNFlags flags = GetCompilerFlags(transform::PassContext::Current());
size_t context_buffer_size;
if (is_depthwise) {
Expand Down Expand Up @@ -350,9 +350,7 @@ class RelayToTIRVisitor : public MixedModeMutator {
call_ext_args.push_back(output);

int context_buffer_size = 0;
std::string context_buffer_name = "NULL";
tir::Var buffer_var =
tir::Var(context_buffer_name, PointerType(PrimType(DataType::Int(8)), "global.workspace"));
PrimExpr buffer_var = tir::StringImm("NULL");
tvm::Array<PrimExpr> context_buffer_args = {buffer_var, ToArg(context_buffer_size)};

scalar_args = tvm::runtime::Concat(context_buffer_args, scalar_args);
Expand Down Expand Up @@ -435,7 +433,7 @@ class RelayToTIRVisitor : public MixedModeMutator {
tvm::Array<PrimExpr> call_ext_args = {tir::StringImm(cmsisnn_api), input, output};

int context_buffer_size = 0;
tir::Var context_buffer_var{"NULL"};
PrimExpr context_buffer_var = tir::StringImm("NULL");
if (pool_name == "cmsisnn.qnn_avg_pool2d") {
CMSISNNFlags flags = GetCompilerFlags(transform::PassContext::Current());
int32_t input_c = qnn::get_const_int(input_shape[3]);
Expand Down
9 changes: 8 additions & 1 deletion src/relay/backend/contrib/cmsisnn/tir_to_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,14 @@ class CodeGenCMSISNN : public codegen::CodeGenCHost {
/*! * \brief extracts CMSIS-NN context buffer information */
CMSISNNContextBuffer extract_context_buffer_info(const CallNode* op, int base_pos) {
CMSISNNContextBuffer context_buffer;
context_buffer.name = op->args[base_pos].as<VarNode>()->name_hint;

// The argument could be a Var if it is allocated to hold the
// context buffer OR it will be a StringImm with "NULL"
if (op->args[base_pos]->IsInstance<VarNode>()) {
context_buffer.name = op->args[base_pos].as<VarNode>()->name_hint;
} else {
context_buffer.name = op->args[base_pos].as<StringImmNode>()->value;
}
context_buffer.size = ValueFromArg(op, base_pos + 1);
return context_buffer;
}
Expand Down

0 comments on commit 8c711d9

Please sign in to comment.