Skip to content
Closed
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
2 changes: 2 additions & 0 deletions include/tvm/relay/expr_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class ExprVisitor
protected:
// Internal visiting counter
std::unordered_map<const Object*, size_t> visit_counter_;
bool enter_primitives_ = false;
};

/*!
Expand Down Expand Up @@ -231,6 +232,7 @@ class ExprMutator
protected:
/*! \brief Internal map used for memoization. */
std::unordered_map<Expr, Expr, ObjectHash, ObjectEqual> memo_;
bool enter_primitives_ = false;
};

/*!
Expand Down
14 changes: 13 additions & 1 deletion src/relay/ir/expr_functor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ Expr ExprMutator::VisitExpr_(const TupleNode* op) {
Expr ExprMutator::VisitExpr_(const FunctionNode* op) {
tvm::Array<TypeVar> ty_params;
bool all_ty_params_unchanged = true;
// don't mutate primitive functions
const auto primitive = FunctionGetAttr(GetRef<Function>(op), attr::kPrimitive);
if (primitive->IsInstance<tir::IntImmNode>()) {
if (primitive.as<IntImmNode>()->value && !enter_primitives_) {
return GetRef<Expr>(op);
}
}

for (auto ty_param : op->type_params) {
TypeVar new_ty_param = Downcast<TypeVar>(VisitType(ty_param));
Expand Down Expand Up @@ -256,7 +263,12 @@ void ExprVisitor::ExprVisitor::VisitExpr_(const FunctionNode* op) {
for (auto param : op->params) {
this->VisitExpr(param);
}

const auto primitive = FunctionGetAttr(GetRef<Function>(op), attr::kPrimitive);
if (primitive->IsInstance<tir::IntImmNode>()) {
if (primitive.as<IntImmNode>()->value && !enter_primitives_) {
return;
}
}
this->VisitExpr(op->body);
}

Expand Down
1 change: 1 addition & 0 deletions src/relay/pass/type_infer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ class TypeInferencer::Resolver : public ExprMutator, PatternMutator {
Resolver(const std::unordered_map<Expr, ResolvedTypeInfo, ObjectHash, ObjectEqual>& tmap,
TypeSolver* solver)
: tmap_(tmap), solver_(solver) {
enter_primitives_ = true;
}

Expr VisitExpr_(const VarNode* op) final {
Expand Down