Skip to content

Commit

Permalink
[Relay] Merge analysis/context_analysis.cc and transforms/device_anno…
Browse files Browse the repository at this point in the history
…tation.cc (apache#9038)

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

Currently LowerTEPass (backend/te_compiler.cc) is a 'special' pass because it
depends on a side-input DeviceMap. We'd like to remove that side-input, and
instead recover the Device (and, ultimately, Target) for each (fused) primitive
call from the AST alone.

By doing so we also avoid needing to perform device planning twice:
 - It needs to be done before lowering so we know which primitives need
   to be compiled for which devices.
 - It then needs to be re-done after lowering and optimization as a prelude
   to memory planning.
By baking the device plan into the AST we can simply do device planning before
lowering, and run memory planning later, both as ordinary passes.

While working on that issue we realized we currently have 3 'device planners':
 - transforms/device_annotation.cc, which supports only a small subset of Relay
   and uses a simple top-down algorithm to assign a device to every
   sub-expression.
 - analysis/context_analysis.cc, which makes a galant effort to support most of
   Relay, is based on unification rather than a top-down algorithm, but handles
   higher order functions by ad-hoc and fragile inlining.
 - transforms/annotate_target.cc, which works on Targets instead of Devices, but
   is otherwise like 'device planning'.
We'd like to bring these together.

In this PR we introduce a new transforms/device_planner.cc intended to replace
transforms/device_annotation.cc and analysis/context_analysis.cc. We don't
delete those two just yet since we need to switch all users off of them in the
next PR. We also leave transforms/annotate_target.cc alone pending a proper RFC
to bring devices and targets together sensibly, but have it firmly in our
sights.

transforms/device_planner.cc is based on analysis/context_analysis.cc, but
is heavily reworked to:
 1. Handle starting from existing "on_device" annotations as well as existing
    "device_copy" calls.
 2. Be idempotent, with the idea we'll probably need to re-run it to 'refine'
    device planning to account for storge scopes.
 3. Robustly handle all of Relay, particularly higher-order functions. For that
    we replace the inlining approach in analysis/context_analysis.cc with a
    higher-order unification domain.
 4. Be a little more systematic with defaulting.
 5. Capture the result of the analysis within the AST as new "device_copy" calls
    at device boundaries, and new/replaced "on_device" calls wherever the device
    for a sub-expression is not already 'obvious' from the sub-expression's
    lexical scope.
 6. Provide helper visitors for passes which need to ask for the device for
    any sub-expression they are processing and/or preserve device information
    on rewrites. Those passes include:
     - backend/aot_executor_codegen.cc (AOTOnDemandAllocator)
     - backend/graph_plan_memory.cc (StorageAllocaBaseVisitor etc)
     - backend/te_compiler.cc (LowerTensorExprMutator)
     - backend/vm/lambda_lift.cc (LambdaLifter)
     - transforms/memory_alloc.cc (DialectRewriter)
     - transforms/to_a_normal_form.cc (Fill)
     - backend/vm/compiler.cc (VMFunctionCompiler)
    However we won't change any of those in this PR.

See the draft apache#8788 for the end game.

* [checkpoint] Use Relay script for all unit tests.

* [checkpoint] Hoist out DeviceDomain and DeviceDomains.

* [checkpoint] Hoist out visitors

* [checkpoint] Woops, left debug-only code in
  • Loading branch information
mbs-octoml authored and ylc committed Jan 7, 2022
1 parent 9b7ac9b commit b671813
Show file tree
Hide file tree
Showing 10 changed files with 3,923 additions and 3 deletions.
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

0 comments on commit b671813

Please sign in to comment.