-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[RELAY] [VIRTUALDEVICE] Change syntax for device planning and store parameter virtual devices in virtual_device_ field #10352
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
Changes from 30 commits
dc6053b
69acc6d
a92ab15
30fff16
167e4fd
e3a2ec2
a95ae9d
e62bce8
904b93a
5224b64
fd97879
3236a2d
e269437
e642a5a
6c6e5e5
a344e15
231d040
0f5e6d2
f71601d
e8e5a09
ad4c97a
78d063e
1f10769
b20dce7
8bacaba
41870f0
3d346d9
c4939e6
001a063
2075dad
9dda0c1
5bf526a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -252,21 +252,16 @@ class VMFunctionCompiler : DeviceAwareExprFunctor<void(const Expr& n)> { | |
| // Do that flattening on-the-fly here. | ||
| Function inner_func = Downcast<Function>(func->body); | ||
| std::vector<Var> params; | ||
| std::vector<VirtualDevice> param_virtual_devices; | ||
| params.reserve(func->params.size() + inner_func->params.size()); | ||
| param_virtual_devices.reserve(func->params.size() + inner_func->params.size()); | ||
| param_device_indexes.reserve(func->params.size() + inner_func->params.size()); | ||
| for (size_t i = 0; i < func->params.size(); ++i) { | ||
| params.emplace_back(func->params[i]); | ||
| VirtualDevice param_virtual_device = GetFunctionParamVirtualDevice(func.get(), i); | ||
| param_virtual_devices.push_back(param_virtual_device); | ||
| param_device_indexes.push_back(GetDeviceIndex(param_virtual_device)); | ||
| param_device_indexes.push_back(GetDeviceIndex(func->params[i]->virtual_device())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we get some payoff at last!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah!! |
||
| } | ||
| for (size_t i = 0; i < inner_func->params.size(); ++i) { | ||
| params.emplace_back(inner_func->params[i]); | ||
| VirtualDevice param_virtual_device = GetFunctionParamVirtualDevice(inner_func.get(), i); | ||
| param_virtual_devices.push_back(param_virtual_device); | ||
| param_device_indexes.push_back(GetDeviceIndex(param_virtual_device)); | ||
|
|
||
| param_device_indexes.push_back(GetDeviceIndex(inner_func->params[i]->virtual_device())); | ||
| } | ||
| std::vector<TypeVar> type_params; | ||
| type_params.reserve(func->type_params.size() + inner_func->type_params.size()); | ||
|
|
@@ -278,13 +273,12 @@ class VMFunctionCompiler : DeviceAwareExprFunctor<void(const Expr& n)> { | |
| } | ||
| Function flattened_func = Function(params, inner_func->body, inner_func->ret_type, | ||
| type_params, func->attrs, func->span); | ||
| VisitExpr(MaybeFunctionOnDevice(flattened_func, param_virtual_devices, | ||
| GetFunctionResultVirtualDevice(inner_func.get()))); | ||
| flattened_func->virtual_device_ = inner_func->virtual_device(); | ||
| VisitExpr(flattened_func); | ||
| } else { | ||
| param_device_indexes.reserve(func->params.size()); | ||
| for (size_t i = 0; i < func->params.size(); ++i) { | ||
| param_device_indexes.push_back( | ||
| GetDeviceIndex(GetFunctionParamVirtualDevice(func.get(), i))); | ||
| param_device_indexes.push_back(GetDeviceIndex(func->params[i]->virtual_device())); | ||
| } | ||
| VisitExpr(func); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -472,45 +472,38 @@ class ExprBinder : public MixedModeMutator, PatternMutator { | |
| const tvm::Map<Var, Expr>& args_map_; | ||
| }; | ||
|
|
||
| // This function should be called SubstAndBind, since it assumes any variables introduced | ||
| // in the substitution right hand side should be implicitly bound in the function. | ||
| Expr Bind(const Expr& expr, const tvm::Map<Var, Expr>& args_map) { | ||
| if (const FunctionNode* func = expr.as<FunctionNode>()) { | ||
| Expr new_body = ExprBinder(args_map).VisitExpr(func->body); | ||
| Array<Var> new_params; | ||
| std::vector<VirtualDevice> new_param_virtual_devices; | ||
| for (size_t i = 0; i < func->params.size(); ++i) { | ||
| if (!args_map.count(func->params[i])) { | ||
| new_params.push_back(func->params[i]); | ||
| new_param_virtual_devices.push_back(GetFunctionParamVirtualDevice(func, i)); | ||
| } | ||
| } | ||
| if (new_body.same_as(func->body) && new_params.size() == func->params.size()) { | ||
| return expr; | ||
| } | ||
|
|
||
| auto ret = | ||
| Function(new_params, new_body, func->ret_type, func->type_params, func->attrs, func->span); | ||
| ret = | ||
| MaybeFunctionOnDevice(ret, new_param_virtual_devices, GetFunctionResultVirtualDevice(func)); | ||
| ret->virtual_device_ = func->virtual_device(); | ||
|
|
||
| std::unordered_set<Var, ObjectPtrHash, ObjectPtrEqual> set; | ||
electriclilies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (const auto& v : FreeVars(expr)) { | ||
| set.insert(v); | ||
| } | ||
| for (const auto& v : FreeVars(ret)) { | ||
| if (set.count(v) == 0) { | ||
| new_params.push_back(v); | ||
| if (!GetFunctionResultVirtualDevice(func)->IsFullyUnconstrained()) { | ||
| // TODO(mbs): The function has been annotated with a device, which means we are supposed | ||
| // to be preserving device annotations on every transformation. However there's no | ||
| // such context for the free vars in args_map. | ||
| LOG(WARNING) << "introduced free var '" << PrettyPrint(v) | ||
| << "' into function body but no device is known for it"; | ||
| } | ||
| new_param_virtual_devices.push_back(VirtualDevice::FullyUnconstrained()); | ||
| } | ||
| } | ||
| ret = | ||
electriclilies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Function(new_params, new_body, func->ret_type, func->type_params, func->attrs, func->span); | ||
| ret = | ||
| MaybeFunctionOnDevice(ret, new_param_virtual_devices, GetFunctionResultVirtualDevice(func)); | ||
|
|
||
| VLOG(4) << "Expr:\n" << expr; | ||
| VLOG(4) << "Ret:\n" << ret; | ||
|
|
||
| ICHECK_EQ(FreeVars(expr).size(), FreeVars(ret).size()); | ||
| return std::move(ret); | ||
| } else { | ||
|
|
@@ -528,6 +521,31 @@ TVM_REGISTER_GLOBAL("relay.ir.Bind").set_body([](TVMArgs args, TVMRetValue* ret) | |
| } | ||
| }); | ||
|
|
||
| Expr SubstituteVars(const Expr& expr, const tvm::Map<Var, Expr>& args_map) { | ||
|
||
| if (const FunctionNode* func = expr.as<FunctionNode>()) { | ||
| Expr new_body = ExprBinder(args_map).VisitExpr(func->body); | ||
| Array<Var> new_params; | ||
| for (size_t i = 0; i < func->params.size(); i++) { | ||
| if (!args_map.count(func->params[i])) { | ||
| new_params.push_back(func->params[i]); | ||
| } else { | ||
| if (const VarNode* var = args_map[func->params[i]].as<VarNode>()) { | ||
| new_params.push_back(GetRef<Var>(var)); | ||
| } else { | ||
| ICHECK(false) << "Expected all values in args_map to be vars, but found " | ||
| << args_map[func->params[i]]->GetTypeKey(); | ||
| } | ||
| } | ||
| } | ||
| auto ret = | ||
| Function(new_params, new_body, func->ret_type, func->type_params, func->attrs, func->span); | ||
| ret->virtual_device_ = func->virtual_device(); | ||
| return ret; | ||
| } else { | ||
| return ExprBinder(args_map).VisitExpr(expr); | ||
| } | ||
| } | ||
|
|
||
| void ExpandANormalForm(const LetNode* op, std::function<void(const LetNode*)> pre_visit, | ||
| std::function<void(const LetNode*)> post_visit) { | ||
| std::stack<const LetNode*> stack; | ||
|
|
||
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 issue with this being used for both param- and let-bound vars even though we don't parse annots for let-bound vars?
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.
I guess for now, let-bound variables don't have their virtual devices set so it theoretically won't be triggered.. I haven't seen any issues in CI related to this but I could split the function into two if you'd like
Eventually we will annotate let-bound variables and at that point we will have to parse the fake attrs for let bound variables