-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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 tvm/relay/feature.h | ||
* \brief Detect features used in Expr/Module. | ||
*/ | ||
#ifndef TVM_RELAY_FEATURE_H_ | ||
#define TVM_RELAY_FEATURE_H_ | ||
|
||
#include <bitset> | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
enum Feature : int { | ||
fVar = 0, | ||
fGlobalVar = 1, | ||
fConstant = 2, | ||
fTuple = 3, | ||
fTupleGetItem = 4, | ||
fFunction = 5, | ||
fOp = 6, | ||
fCall = 7, | ||
fLet = 8, | ||
fIf = 9, | ||
fRefCreate = 10, | ||
fRefRead = 11, | ||
fRefWrite = 12, | ||
fConstructor = 13, | ||
fMatch = 14, | ||
fGraph = 15, | ||
fLetRec = 16 | ||
}; | ||
|
||
constexpr size_t feature_count = 17; | ||
|
||
class FeatureSet { | ||
public: | ||
FeatureSet(const FeatureSet&) = default; | ||
explicit FeatureSet(Feature ft) { | ||
bs_.set(static_cast<size_t>(ft)); | ||
} | ||
static FeatureSet AllFeature() { | ||
FeatureSet fs; | ||
return fs; | ||
} | ||
static FeatureSet NoFeature() { | ||
FeatureSet fs; | ||
fs.bs_.flip(); | ||
return fs; | ||
} | ||
template<typename T> | ||
FeatureSet& operator+=(const T& rhs) { | ||
bs_ |= FeatureSet(rhs).bs_; | ||
return *this; | ||
} | ||
template<typename T> | ||
FeatureSet operator+(const T& rhs) const { | ||
FeatureSet fs(*this); | ||
fs += rhs; | ||
return fs; | ||
} | ||
template<typename T> | ||
FeatureSet& operator-=(const T& rhs) { | ||
bs_ &= ~(FeatureSet(rhs)).bs_; | ||
return *this; | ||
} | ||
template<typename T> | ||
FeatureSet operator-(const T& rhs) const { | ||
FeatureSet fs(*this); | ||
fs -= rhs; | ||
return fs; | ||
} | ||
bool is_subset_of(const FeatureSet& rhs) const { | ||
return ((*this) - rhs).bs_.none(); | ||
} | ||
|
||
private: | ||
std::bitset<feature_count> bs_; | ||
FeatureSet() = default; | ||
explicit FeatureSet(const std::bitset<feature_count>& bs) : bs_(bs) { } | ||
}; | ||
|
||
class Expr; | ||
FeatureSet DetectFeature(const Expr& expr); | ||
struct Module; | ||
FeatureSet DetectFeature(const Module& mod); | ||
|
||
} // namespace relay | ||
} // namespace tvm | ||
|
||
#endif // TVM_RELAY_FEATURE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2019 by Contributors | ||
* \file feature.cc | ||
* \brief Detect features used in Expr/Module | ||
*/ | ||
#include <tvm/relay/feature.h> | ||
#include <tvm/relay/pass.h> | ||
#include <tvm/relay/expr.h> | ||
#include <tvm/relay/expr_functor.h> | ||
#include <tvm/relay/module.h> | ||
#include "pass_util.h" | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
FeatureSet DetectFeature(const Expr& expr) { | ||
struct FeatureDetector : ExprVisitor { | ||
std::unordered_set<Expr, NodeHash, NodeEqual> visited_; | ||
FeatureSet fs = FeatureSet::NoFeature(); | ||
void VisitExpr(const Expr& expr) final { | ||
if (visited_.count(expr) == 0) { | ||
ExprVisitor::VisitExpr(expr); | ||
} else { | ||
if (!IsAtomic(expr)) { | ||
fs += fGraph; | ||
} | ||
} | ||
} | ||
#define DETECT_CONSTRUCT(CONSTRUCT_NAME, STMT) \ | ||
void VisitExpr_(const CONSTRUCT_NAME##Node* op) final { \ | ||
STMT \ | ||
fs += f##CONSTRUCT_NAME; \ | ||
ExprVisitor::VisitExpr_(op); \ | ||
} | ||
#define DETECT_DEFAULT_CONSTRUCT(CONSTRUCT_NAME) DETECT_CONSTRUCT(CONSTRUCT_NAME, {}) | ||
DETECT_DEFAULT_CONSTRUCT(Var) | ||
DETECT_DEFAULT_CONSTRUCT(GlobalVar) | ||
DETECT_DEFAULT_CONSTRUCT(Constant) | ||
DETECT_DEFAULT_CONSTRUCT(Tuple) | ||
DETECT_DEFAULT_CONSTRUCT(TupleGetItem) | ||
DETECT_DEFAULT_CONSTRUCT(Function) | ||
DETECT_DEFAULT_CONSTRUCT(Op) | ||
DETECT_DEFAULT_CONSTRUCT(Call) | ||
DETECT_CONSTRUCT(Let, { | ||
for (const Var& v : FreeVars(op->value)) { | ||
if (op->var == v) { | ||
fs += fLetRec; | ||
} | ||
} | ||
}) | ||
DETECT_DEFAULT_CONSTRUCT(If) | ||
DETECT_DEFAULT_CONSTRUCT(RefCreate) | ||
DETECT_DEFAULT_CONSTRUCT(RefRead) | ||
DETECT_DEFAULT_CONSTRUCT(RefWrite) | ||
DETECT_DEFAULT_CONSTRUCT(Constructor) | ||
DETECT_DEFAULT_CONSTRUCT(Match) | ||
#undef DETECT_DEFAULT_CONSTRUCT | ||
} fd; | ||
fd(expr); | ||
return fd.fs; | ||
} | ||
|
||
FeatureSet DetectFeature(const Module& mod) { | ||
FeatureSet fs = FeatureSet::NoFeature(); | ||
if (mod.defined()) { | ||
for (const auto& f : mod->functions) { | ||
fs += DetectFeature(f.second); | ||
} | ||
} | ||
return fs; | ||
} | ||
|
||
} // namespace relay | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters