-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[TIR][REFACTOR][API-CHANGE] Change Call.name to Call.op(RelayExpr) #5863
Conversation
a87289c
to
1190dec
Compare
I like the idea how it organizes intrins. Is this PR ready for review? |
…Op/RelayExpr) This PR brings a major refactor to the tir::Call structure. The current Call structure uses a string field(name) to identify the function/intrinsic being called. This approach is limited as we start to expand TIR to be more structured. In particular, we are interested in the following aspects: - Type a function and perform better compile time type checking so that we can find errors early. - Register additional properties about an operator, such as: - Whether an intrinsic can be vectorized - What is the adjoint function of the intrinsic(for tensor expression AD) - Whether the operator has side effect. - Perform specific codegen about an intrinsic if necessary. - Call into another function in the same module. The refactor changes the Call.name field to Call.op. The Call.op field has a RelayExpr type, and we can pass: - A tvm::Op which represents the corresponding intrinsic. - A tvm::GlobalVar for calling into another function in the IRModule. All the current intrinsics are migrated by registering an tvm::Op. Because the unified IR shares a single Op registry. We use the "tir" namespace for tir related intrinsics, for example bitwise and is now registered under `tir.bitwise_and`. To simplify upgrade, we introduce a `tir.call_extern` intrinsic that allows us to call into arbitary external function without type checking. However, we should move towards more type checked variants in the system. Under the new op design. We should no longer try to pattern match all the specific intrincis. Instead, we should rely on attr of each Op to do transformation. For example, the vectorization pass depends on the TVectorizable property of the op, which can be registered independently. In this way, we can still grow the number of intrinsics when necessary without having to change all the passes. The same rule applies for tensor expression AD. Currently we are performing AD by pattern match on operators like exp, sin, cos. We should instead change to the ajoint registeration mechanism like those in relay. Followup refactors need to be performed, including: - Fold the Call.call_type into operator's attribute. - Enrich the operator registry information - Refactor passes(e.g. AD, intrin lowering) to use the attribute based transformation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The overall design and the ad part change look good to me.
* - It can be tvm::Op which corresponds to the primitive operators(intrinsics). | ||
* - It can also be another function in the IRModule (GlobalVar). | ||
*/ | ||
RelayExpr op; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we should someday move GlobalVar out of RelayExpr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggested alternatives and rationale?
} | ||
return ""; | ||
return Op(nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we just LOG(FATAL) here and throw?
I have a question on |
|
||
TIR_DEFINE_BUILTIN_FUNC(tvm_stack_make_array).set_num_inputs(6); | ||
|
||
// When num_inputs are not set, the function is assumed to be variable length. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a checker somewhere in the codebase on the number of inputs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not at the moment, so far we are only providing these information without checking them to ease migration. A checker is certainly an important followup step
@junrushao1994 you are right. There are two ways to deal with such situation:
The current PR simply migrates the previous code in a minimum way, and we should perform follow ups along this two directions |
auto res = v(std::move(body)); | ||
CHECK(res.as<EvaluateNode>()->value.as<CallNode>()->args[0].same_as(x)); | ||
CHECK(res.as<EvaluateNode>()->value.as<CallNode>()->args[1].same_as(x)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay it is...just ignore me
@tqchen I am in favor of the second solution. For example, we can change |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…Expr) (apache#5863)" This reverts commit 82d157f.
Followup PR #5937 |
…pache#5863) * [TIR][REFACTOR][API-CHANGE] Change Call.name(string) to Call.op(tvm::Op/RelayExpr) This PR brings a major refactor to the tir::Call structure. The current Call structure uses a string field(name) to identify the function/intrinsic being called. This approach is limited as we start to expand TIR to be more structured. In particular, we are interested in the following aspects: - Type a function and perform better compile time type checking so that we can find errors early. - Register additional properties about an operator, such as: - Whether an intrinsic can be vectorized - What is the adjoint function of the intrinsic(for tensor expression AD) - Whether the operator has side effect. - Perform specific codegen about an intrinsic if necessary. - Call into another function in the same module. The refactor changes the Call.name field to Call.op. The Call.op field has a RelayExpr type, and we can pass: - A tvm::Op which represents the corresponding intrinsic. - A tvm::GlobalVar for calling into another function in the IRModule. All the current intrinsics are migrated by registering an tvm::Op. Because the unified IR shares a single Op registry. We use the "tir" namespace for tir related intrinsics, for example bitwise and is now registered under `tir.bitwise_and`. To simplify upgrade, we introduce a `tir.call_extern` intrinsic that allows us to call into arbitary external function without type checking. However, we should move towards more type checked variants in the system. Under the new op design. We should no longer try to pattern match all the specific intrincis. Instead, we should rely on attr of each Op to do transformation. For example, the vectorization pass depends on the TVectorizable property of the op, which can be registered independently. In this way, we can still grow the number of intrinsics when necessary without having to change all the passes. The same rule applies for tensor expression AD. Currently we are performing AD by pattern match on operators like exp, sin, cos. We should instead change to the ajoint registeration mechanism like those in relay. Followup refactors need to be performed, including: - Fold the Call.call_type into operator's attribute. - Enrich the operator registry information - Refactor passes(e.g. AD, intrin lowering) to use the attribute based transformation * Fix nms * Fix remaining testcase * Address review comment
…pache#5863) * [TIR][REFACTOR][API-CHANGE] Change Call.name(string) to Call.op(tvm::Op/RelayExpr) This PR brings a major refactor to the tir::Call structure. The current Call structure uses a string field(name) to identify the function/intrinsic being called. This approach is limited as we start to expand TIR to be more structured. In particular, we are interested in the following aspects: - Type a function and perform better compile time type checking so that we can find errors early. - Register additional properties about an operator, such as: - Whether an intrinsic can be vectorized - What is the adjoint function of the intrinsic(for tensor expression AD) - Whether the operator has side effect. - Perform specific codegen about an intrinsic if necessary. - Call into another function in the same module. The refactor changes the Call.name field to Call.op. The Call.op field has a RelayExpr type, and we can pass: - A tvm::Op which represents the corresponding intrinsic. - A tvm::GlobalVar for calling into another function in the IRModule. All the current intrinsics are migrated by registering an tvm::Op. Because the unified IR shares a single Op registry. We use the "tir" namespace for tir related intrinsics, for example bitwise and is now registered under `tir.bitwise_and`. To simplify upgrade, we introduce a `tir.call_extern` intrinsic that allows us to call into arbitary external function without type checking. However, we should move towards more type checked variants in the system. Under the new op design. We should no longer try to pattern match all the specific intrincis. Instead, we should rely on attr of each Op to do transformation. For example, the vectorization pass depends on the TVectorizable property of the op, which can be registered independently. In this way, we can still grow the number of intrinsics when necessary without having to change all the passes. The same rule applies for tensor expression AD. Currently we are performing AD by pattern match on operators like exp, sin, cos. We should instead change to the ajoint registeration mechanism like those in relay. Followup refactors need to be performed, including: - Fold the Call.call_type into operator's attribute. - Enrich the operator registry information - Refactor passes(e.g. AD, intrin lowering) to use the attribute based transformation * Fix nms * Fix remaining testcase * Address review comment
This PR brings a major refactor to the tir::Call structure.
The current Call structure uses a string field(name) to identify the
function/intrinsic being called. This approach is limited as we start
to expand TIR to be more structured. In particular, we are interested in
the following aspects:
can find errors early.
The refactor changes the Call.name field to Call.op.
The Call.op field has a RelayExpr type, and we can pass:
All the current intrinsics are migrated by registering an tvm::Op.
Because the unified IR shares a single Op registry. We use the "tir"
namespace for tir related intrinsics, for example bitwise and is now registered
under
tir.bitwise_and
.To simplify upgrade, we introduce
tir.call_extern
intrinsicthat allows us to call into arbitary external function without type checking.
However, we should move towards more type checked variants in the system.
Under the new op design. We should no longer try to pattern match all the
specific intrincis. Instead, we should rely on attr of each Op to do transformation.
For example, the vectorization pass depends on the TVectorizable property of the op,
which can be registered independently.
In this way, we can still grow the number of intrinsics when necessary
without having to change all the passes.
The same rule applies for tensor expression AD. Currently we are performing
AD by pattern match on operators like exp, sin, cos. We should instead
change to the ajoint registeration mechanism like those in relay.
Followup refactors need to be performed, including:
Upgrade Note
If you are using an raw intrinsic API, likely we need to add the
tir.
prefix to it.call_pure_intrin(x.dtype, 'tir.exp', [x])