Skip to content
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

[Relay] Merge analysis/context_analysis.cc and transforms/device_annotation.cc #9038

Merged
merged 2 commits into from
Sep 29, 2021
Merged
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
11 changes: 11 additions & 0 deletions include/tvm/relay/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,17 @@ TVM_DLL Pass RelayToTIRTargetHook();
*/
TVM_DLL Pass ManifestAlloc(Target target_host, Map<tvm::Integer, tvm::Target> targets);

/*!
* \brief Uses existing "on_device" and "device_copy" CallNodes to infer the device on which
* every Relay sub-expression should run (and the result stored). Captures the result of that
* analysis using new "on_device" and "device_copy" CallNodes. See
* tvm::relay::transform::{LexicalOnDeviceMixin,DeviceAwareExprVisitor,DeviceAwareExprMutator}
* for help recovering the device for an arbitrary sub-expression in downstream transformations.
*
* \param default_device_type DLDeviceType for default device.
*/
TVM_DLL Pass PlanDevices(DLDeviceType default_device_type);

} // namespace transform

/*!
Expand Down
10 changes: 10 additions & 0 deletions python/tvm/relay/transform/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,16 @@ def SimplifyExpr():
return _ffi_api.SimplifyExpr()


def PlanDevices(default_device):
"""
Uses existing "on_device" and "device_copy" CallNodes to infer the device on which
every Relay sub-expression should run (and the result stored). Captures the result of that
analysis using new "on_device" and "device_copy" CallNodes. Note that the device_id of
the default_device is ignored.
"""
return _ffi_api.PlanDevices(default_device)


def FoldExplicitPadding():
"""
FoldExplicitPadding finds explict padding before an op that can support
Expand Down
3 changes: 0 additions & 3 deletions src/relay/op/annotation/annotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ OnDeviceProps GetOnDeviceProps(const CallNode* call_node);
*/
OnDeviceProps GetOnDeviceProps(const Expr& expr);

/*! \brief Returns true if \p expr is an on_device CallNode. */
inline bool IsOnDeviceCall(const Expr& expr) { return GetOnDeviceProps(expr).body.defined(); }

/*!
* \brief Returns \p function annotated with "param_device_types" and "result_device_type"
* attributes capturing parameter and result devices types respectively.
Expand Down
285 changes: 285 additions & 0 deletions src/relay/transforms/device_aware_visitors.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*!
* \file src/relay/transforms/device_aware_visitors.cc
* \brief Visitors which track the device for the current Relay expression and Relay Vars.
*/

#include "./device_aware_visitors.h"

namespace tvm {
namespace relay {
namespace transform {

// TODO(mbs): We'd probably have less tendious code duplication if we redefined the memoizing
// mutator on top of the generic Functor.

DLDeviceType LexicalOnDeviceMixin::GetInScopeDeviceType(const Expr& expr) const {
auto props = GetOnDeviceProps(expr);
if (props.body.defined() && props.is_fixed) {
// Look through any fixed "on_device" annotations.
return props.device_type;
}
if (expr->IsInstance<VarNode>()) {
// Lookup variable binding.
auto itr = var_device_types_.find(Downcast<Var>(expr));
if (itr == var_device_types_.end()) {
return kInvalidDeviceType;
} else {
return itr->second;
}
}
// Otherwise use the currently in-scope device type.
if (expr_device_types_.empty()) {
return kInvalidDeviceType;
} else {
return expr_device_types_.back();
}
}

void LexicalOnDeviceMixin::EnterFunctionBody() { ++function_nesting_; }

void LexicalOnDeviceMixin::ExitFunctionBody() {
ICHECK_GT(function_nesting_, 0);
--function_nesting_;
}

void LexicalOnDeviceMixin::PushDeviceType(DLDeviceType device_type) {
if (device_type == kInvalidDeviceType) {
return;
}
expr_device_types_.emplace_back(device_type);
}

void LexicalOnDeviceMixin::PopDeviceType() {
if (expr_device_types_.empty()) {
return;
}
expr_device_types_.pop_back();
}

void LexicalOnDeviceMixin::PushBoundVar(Var var, DLDeviceType device_type) {
if (device_type == kInvalidDeviceType) {
return;
}
ICHECK(var_device_types_.find(var) == var_device_types_.end());
var_device_types_.emplace(std::move(var), device_type);
}

void LexicalOnDeviceMixin::PopBoundVar(const Var& var) {
auto itr = var_device_types_.find(var);
if (itr == var_device_types_.end()) {
return;
}
var_device_types_.erase(itr);
}

void DeviceAwareExprVisitor::VisitExpr_(const FunctionNode* function_node) {
if (function_node->HasNonzeroAttr(attr::kPrimitive)) {
// No tracking inside primitive functions.
DeviceAwareVisitExpr_(function_node);
} else {
// Function parameters come into scope.
for (size_t i = 0; i < function_node->params.size(); ++i) {
PushBoundVar(function_node->params[i], GetFunctionParamDeviceType(function_node, i));
}
// Entering scope of function body.
PushDeviceType(GetFunctionResultDeviceType(function_node));
EnterFunctionBody();

DeviceAwareVisitExpr_(function_node);

// Leaving scope of function body.
ExitFunctionBody();
PopDeviceType();
// Function parameters go out of scope.
for (size_t i = 0; i < function_node->params.size(); ++i) {
PopBoundVar(function_node->params[i]);
}
}
}

void DeviceAwareExprVisitor::VisitExpr_(const LetNode* let_node) {
PreVisitLetBlock_(let_node);
std::vector<const LetNode*> bindings;
Expr expr = GetRef<Expr>(let_node);
while (const auto* inner_let_node = expr.as<LetNode>()) {
// Let-bound var (in pre visited version) goes into scope.
// (We'll just assume this is a letrec).
PushBoundVar(inner_let_node->var, GetInScopeDeviceType(inner_let_node->value));
PreVisitLetBinding_(inner_let_node->var, inner_let_node->value);
bindings.emplace_back(inner_let_node);
expr = inner_let_node->body;
}

VisitExpr(expr);

for (auto itr = bindings.rbegin(); itr != bindings.rend(); ++itr) {
// Let-bound var goes out of scope.
PopBoundVar((*itr)->var);
PostVisitLet_(*itr);
}
PostVisitLetBlock_(let_node);
}

void DeviceAwareExprVisitor::VisitExpr_(const CallNode* call_node) {
auto props = GetOnDeviceProps(call_node);
if (props.body.defined() && props.is_fixed) {
// Entering lexical scope of fixed "on_device" call.
PushDeviceType(props.device_type);
VisitExpr(props.body);
// Leaving lexical scope of "on_device" call.
PopDeviceType();
} else {
DeviceAwareVisitExpr_(call_node);
}
}

void DeviceAwareExprVisitor::DeviceAwareVisitExpr_(const FunctionNode* function_node) {
ExprVisitor::VisitExpr_(function_node);
}

void DeviceAwareExprVisitor::DeviceAwareVisitExpr_(const CallNode* call_node) {
ExprVisitor::VisitExpr_(call_node);
}

void DeviceAwareExprVisitor::PreVisitLetBlock_(const LetNode* let_node) {
// no-op
}

void DeviceAwareExprVisitor::PreVisitLetBinding_(const Var& var, const Expr& value) {
VisitExpr(var);
VisitExpr(value);
}

void DeviceAwareExprVisitor::PostVisitLet_(const LetNode* let_node) {
// no-op
}

void DeviceAwareExprVisitor::PostVisitLetBlock_(const LetNode* let_node) {
// no-op
}

Expr DeviceAwareExprMutator::VisitExpr_(const FunctionNode* function_node) {
if (function_node->HasNonzeroAttr(attr::kPrimitive)) {
// No tracking inside primitive functions.
return DeviceAwareVisitExpr_(function_node);
} else {
// Function parameters come into scope.
for (size_t i = 0; i < function_node->params.size(); ++i) {
PushBoundVar(function_node->params[i], GetFunctionParamDeviceType(function_node, i));
}
// Entering scope of function body.
PushDeviceType(GetFunctionResultDeviceType(function_node));
EnterFunctionBody();

Expr result = DeviceAwareVisitExpr_(function_node);

// Leaving scope of function body.
ExitFunctionBody();
PopDeviceType();
// Function parameters go out of scope.
for (size_t i = 0; i < function_node->params.size(); ++i) {
PopBoundVar(function_node->params[i]);
}

return result;
}
}

Expr DeviceAwareExprMutator::VisitExpr_(const LetNode* let_node) {
PreVisitLetBlock_(let_node);
std::vector<std::tuple<Var, Expr, Span, const LetNode*>> bindings;
Expr expr = GetRef<Expr>(let_node);
while (const auto* inner_let_node = expr.as<LetNode>()) {
// Let-bound var (in pre visited version) goes into scope.
// (We'll just assume this is a letrec.)
PushBoundVar(inner_let_node->var, GetInScopeDeviceType(inner_let_node->value));
std::pair<Var, Expr> pair = PreVisitLetBinding_(inner_let_node->var, inner_let_node->value);
bindings.emplace_back(pair.first, pair.second, inner_let_node->span, inner_let_node);
expr = inner_let_node->body;
}

expr = VisitExpr(expr);

for (auto itr = bindings.rbegin(); itr != bindings.rend(); ++itr) {
// Let-bound var goes out of scope.
const LetNode* pre_let_node = std::get<3>(*itr);
PopBoundVar(pre_let_node->var);
Let post_let = Let(/*var=*/std::get<0>(*itr), /*value=*/std::get<1>(*itr),
/*body=*/expr, /*span=*/std::get<2>(*itr));
expr = PostVisitLet_(pre_let_node, post_let.get());
}
return PostVisitLetBlock_(let_node, expr.as<LetNode>());
}

Expr DeviceAwareExprMutator::VisitExpr_(const CallNode* call_node) {
auto props = GetOnDeviceProps(call_node);
if (props.body.defined() && props.is_fixed) {
// Entering lexical scope of fixed "on_device" call.
PushDeviceType(props.device_type);
Expr expr = VisitExpr(props.body);
// Leaving lexical scope of "on_device" call.
PopDeviceType();
return OnDevice(expr, props.device_type, props.is_fixed);
} else {
return DeviceAwareVisitExpr_(call_node);
}
}

Expr DeviceAwareExprMutator::DeviceAwareVisitExpr_(const FunctionNode* function_node) {
return ExprMutator::VisitExpr_(function_node);
}

Expr DeviceAwareExprMutator::DeviceAwareVisitExpr_(const CallNode* call_node) {
return ExprMutator::VisitExpr_(call_node);
}

void DeviceAwareExprMutator::PreVisitLetBlock_(const LetNode* let_node) { /* no-op */
}

std::pair<Var, Expr> DeviceAwareExprMutator::PreVisitLetBinding_(const Var& var,
const Expr& value) {
return std::make_pair(Downcast<Var>(VisitExpr(var)), VisitExpr(value));
}

Expr DeviceAwareExprMutator::PostVisitLet_(const LetNode* pre_let_node,
const LetNode* post_let_node) {
if (pre_let_node->var == post_let_node->var && pre_let_node->value == post_let_node->value &&
pre_let_node->body == post_let_node->body) {
return GetRef<Expr>(pre_let_node);
} else {
return GetRef<Expr>(post_let_node);
}
}

Expr DeviceAwareExprMutator::PostVisitLetBlock_(const LetNode* pre_let_node,
const LetNode* post_let_node) {
if (pre_let_node->var == post_let_node->var && pre_let_node->value == post_let_node->value &&
pre_let_node->body == post_let_node->body) {
return GetRef<Expr>(pre_let_node);
} else {
return GetRef<Expr>(post_let_node);
}
}

} // namespace transform
} // namespace relay
} // namespace tvm
Loading