Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
init

lint

rename

ci
  • Loading branch information
MarisaKirisame committed May 26, 2019
1 parent c5fdb00 commit 70b5790
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 7 deletions.
109 changes: 109 additions & 0 deletions include/tvm/relay/feature.h
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_
6 changes: 3 additions & 3 deletions src/relay/pass/alter_op_layout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand All @@ -18,7 +18,7 @@
*/

/*!
* Copyright (c) 2018 by Contributors
* Copyright (c) 2019 by Contributors
* \file alter_op_layout.cc
* \brief Alternate the layouts of operators or replace primitive operators with
other expressions. This pass can be used for computing convolution in
Expand Down
93 changes: 93 additions & 0 deletions src/relay/pass/feature.cc
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
8 changes: 6 additions & 2 deletions src/relay/pass/pass_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down Expand Up @@ -97,6 +97,10 @@ inline Expr TransformF(const std::function<Expr(const Expr&)>& func, const Expr&
}
}

inline bool IsAtomic(const Expr& e) {
return e.as<VarNode>() || e.as<OpNode>() || e.as<ConstructorNode>() || e.as<GlobalVarNode>();
}

} // namespace relay
} // namespace tvm
#endif // TVM_RELAY_PASS_PASS_UTIL_H_
4 changes: 2 additions & 2 deletions src/relay/pass/well_formed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down

0 comments on commit 70b5790

Please sign in to comment.