-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[High level OPT][RFC] NNVMv2 IR - Relay #1672
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
Merged
Merged
Changes from all commits
Commits
Show all changes
136 commits
Select commit
Hold shift + click to select a range
cc7706e
Add stripped down version of expr.h and type.h
jroesch 8b79ae0
Add InternTable data structure
jroesch e2b6a2f
Add placeholder defn of Operator
jroesch 4d0a60d
Add initial port of environment.h
jroesch dd392be
Add expr_functor.h
jroesch 336fe2b
Add initial version of type_functor.h
jroesch e31e945
Make type_functor.h a private header
jroesch 425d5f2
Add ir.h
jroesch a429689
Add back Relay's logging.h
jroesch 9033747
Add type checker header
jroesch bb1e501
Add alpha_eq
jroesch 4a84f25
Add incomplete_type.h
jroesch 2a2131c
Add type call
jroesch 9e7b18a
Add test for let with IR builder
jroesch 133b1dc
Add initial version of unifier and old tests
jroesch bc9754f
Update type_functor.h for incomplete type.
jroesch d9d1131
Add Python side of unifier
jroesch 860d7e6
Add to incomplete_type and add impl in typechecker.cc
jroesch 32bb3c2
Add type_visitor.h
jroesch e044e89
Add expr_visitor.h
jroesch 0795d41
Start reparing unifier and tests
jroesch b6803b5
Fix test_unifier.py, now runs but all tests fail
jroesch fee67de
Remove tests for ommitted features and fix
jroesch 4cdf3f9
Start refactoring type checker
jroesch a1027bf
Get a failing test for the type checker
jroesch fe6b31a
Iterate on first test case
jroesch 02f6b1e
First simple test passes
jroesch 7d2624a
Iterate towards second test
jroesch 1ca8c4a
Remove placeholder op.h
jroesch 2c6b521
[OP] Current op system
tqchen e197681
WIP
jroesch b2fa023
WIP
jroesch bf5e16d
Change over to new Node construction
jroesch b73e3c5
Basic tests working
jroesch e9f8bd5
Remove unifier from Python interface
jroesch 920768f
[OP] Structral refactor
tqchen 4cec4f1
Add type_subst back
jroesch b1ba347
Clean up code while refactoring inference
jroesch 3a4ff26
Restore old TVM backend code
jroesch b91ffb5
Type checker is working for one op case
jroesch 26755ec
op step 2
tqchen f8540ec
WIP
jroesch 0325fa6
Address comments from Friday
jroesch 7ad860f
Repair tests
jroesch 5353663
Work on type relation
jroesch ac254c3
Fix find and replace bug
jroesch b79f63a
Add normalization for type relations
jroesch 23e64bc
Iterating on Broadcast
jroesch 918d081
Address CR feedback
jroesch 5f9529f
Address more CR feedback
jroesch 66f01ad
Add SourceMap and clean up environment.h
jroesch e0b9ed7
Reogranize a bit
jroesch b29e17a
Kill dead code in env.py
jroesch 0449680
Fix commit mistake
jroesch f151ea9
Move type_infer into pass.py
jroesch 78a8d64
Reorganize passes a bit
jroesch 1dbf636
More cleaning
jroesch 3445b9f
Remove dead code
jroesch d716e6c
Clean up code in Unifier
jroesch 33ef935
Clean up environment.h
jroesch 3c3d719
Fix up Python imports
jroesch c075bd3
Add first pass add broadcast inference
jroesch c080d7b
Add ability to build and check a global
jroesch 124381f
Address first round of CR comments
jroesch 8db9d57
Add skeleton for kind checker
jroesch cd32bb9
Tweak docs in pass.h
jroesch 4feda67
Refactor kind_check.{h, cc}
jroesch 4e747c2
Improve type checking, can check control-flow-y program.
jroesch f25865e
Rename TVM compiler to to_tvm.py
jroesch 8941d41
Begin work on lowering Relay to TVM
jroesch fb21edb
WIP debugging
jroesch 9fcd45d
Add another test case and do a little clean up
jroesch 6323e39
Port docs from previous Relay version
jroesch 9fac901
Update docs
jroesch d6ed957
Add skeleton for converting from NNVM models
jroesch f4a3358
Address more code review feedback
jroesch 142d4e3
Fix cpplint
jroesch e0c6143
Fix pylint
jroesch 356a810
Fix doc error
jroesch 12dcc2c
Fix doc error again
jroesch 2c4f54c
Fix signed/unsigned compare
jroesch 91757a7
Kill a few more warnings
jroesch 2d72cb5
Remove another size_t
jroesch 0f48f49
Fix warning
jroesch 98efaf9
Rewriting language reference to newest version of Relay.
jroesch ef2a229
Revert "Port docs from previous Relay version"
jroesch 059727a
Rewrite type inferencer to use new relations
jroesch cd02ec7
Convert printing to logging
jroesch 561e710
Remove Normalizer
jroesch b327d05
Address a bunch of CR feedback
jroesch 032c040
Rename LocalVar to Var
jroesch 1c0007b
Fix cpplint
jroesch c72d3a6
More clean up to type inference
jroesch 4495bda
Remove error reporting and RTS code.
jroesch 3f5e2c8
Add documentation for env.py
jroesch c908b7a
Clean up and docs
jroesch c03be26
Add docs to unifier.h
jroesch bc94272
resolve.h docs and clean up whitespace
jroesch 255cf15
Clean up interface a little more
jroesch 1e19248
Fix a few issues from clean up
jroesch 1b625a6
Another few tweaks
jroesch 6d386c8
Add SourceName::Get and serialization
jroesch 303e512
Add serialization for op.cc
jroesch ac0bb19
Fix lint
jroesch 0fae0de
mend
jroesch 2c81356
Restore alpha_eq.cc and refactor type_relations to use EnvFunc.
jroesch 5c0a84c
Rename ExprFVisitor to ExprMutator
jroesch bee3325
Remove compiler bits
jroesch 0352410
Address CR feedback
jroesch 19e4419
Refactor expr_visitor.h
jroesch dc73305
WIP trying to rearrange type_relation
jroesch 63cdbd2
Fix crash
jroesch 4e4ee00
Address more CR feedback
jroesch d100018
Restore AlphaEqual for Expr
jroesch bd45407
Fix linting
jroesch 91c7b45
Fix doc issue
jroesch fb74e2c
Address majority of in-person feedback.
jroesch defa8e4
Remove alpha_eq tests
jroesch 167d235
Style fix in expr.cc
jroesch a6b6038
Fix & style
jroesch 41098c4
Fix more style issues
jroesch bee4234
Remove whitespace
jroesch 7e86f56
Address final comments modulo type_infer.cc
jroesch 8339eaf
Address CR and work on 2.7 support
jroesch a7b67ad
Move annotations to expr.pyi
jroesch a1734d7
Fix bug introduced by refactor
jroesch 4976808
Python2 and Python3 tests pass locally
jroesch f074edd
Refactor type relation solver.
jroesch d7835fa
Fix PyLint
jroesch 9281077
Fix PyLint
jroesch b1c8ecd
Repair tests
jroesch b5c5626
Add initial version of concat
jroesch 388b275
Fix issue in type_relations.cc
jroesch 81c9d6d
Fix PyLint ... again
jroesch 4e0c7b8
Fix issue with test case
jroesch 052d619
Fix & style
jroesch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,203 @@ | ||
| /*! | ||
| * Copyright (c) 2018 by Contributors | ||
| * \file tvm/relay/base.h | ||
| * \brief Base classes for the Relay IR. | ||
| */ | ||
| #ifndef TVM_RELAY_BASE_H_ | ||
| #define TVM_RELAY_BASE_H_ | ||
|
|
||
| #include <tvm/api_registry.h> | ||
| #include <tvm/ir.h> | ||
| #include <tvm/node.h> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace tvm { | ||
| /*! | ||
| * \brief Relay: a high level functional IR for TVM. | ||
| * | ||
| * This namespace contains the abstract syntax tree, and other | ||
| * essential data structures for the Relay IR. | ||
| * | ||
| * You can find more about Relay by reading the language reference. | ||
| */ | ||
| namespace relay { | ||
| /*! | ||
| * \brief we always used NodeRef for referencing nodes. | ||
| * | ||
| * By default, NodeRef is a std::shared_ptr of node | ||
| */ | ||
| using NodeRef = tvm::NodeRef; | ||
|
|
||
| /*! | ||
| * \brief Content data type. | ||
| */ | ||
| using DataType = ::tvm::Type; | ||
|
|
||
| /*! | ||
| * \brief Symbolic expression for tensor shape. | ||
| */ | ||
| using ShapeExpr = ::tvm::Expr; | ||
|
|
||
| /*! | ||
| * \brief Hash function for nodes. | ||
| * e.g. std::unordered_map<Expr, Value, NodeHash, NodeEqual> | ||
| */ | ||
| using NodeHash = ::tvm::NodeHash; | ||
| /*! | ||
| * \brief Equality check function for nodes. | ||
| */ | ||
| using NodeEqual = ::tvm::NodeEqual; | ||
|
|
||
| /*! | ||
| * \brief Macro to make it easy to define node ref type given node | ||
| * \param TypeName The name of the reference type. | ||
| * \param NodeName The internal container name. | ||
| * \param NodeRefBase The base type. | ||
| */ | ||
| #define RELAY_DEFINE_NODE_REF(TypeName, NodeName, NodeRefBase) \ | ||
| class TypeName : public NodeRefBase { \ | ||
| public: \ | ||
| TypeName() {} \ | ||
| explicit TypeName(std::shared_ptr<::tvm::Node> n) : NodeRefBase(n) {} \ | ||
| const NodeName* operator->() const { \ | ||
| return static_cast<const NodeName*>(node_.get()); \ | ||
| } \ | ||
| operator bool() { return this->defined(); } \ | ||
| using ContainerType = NodeName; \ | ||
| }; | ||
|
|
||
| /*! | ||
| * \brief The source name in the Span | ||
| * \sa SourceNameNode, Span | ||
| */ | ||
| class SourceName; | ||
| /*! | ||
| * \brief The name of a source fragment. | ||
| */ | ||
| class SourceNameNode : public Node { | ||
| public: | ||
| /*! \brief The source name. */ | ||
| std::string name; | ||
| // override attr visitor | ||
| void VisitAttrs(AttrVisitor* v) final { v->Visit("name", &name); } | ||
|
|
||
| TVM_DLL static SourceName make(std::string name); | ||
|
|
||
| static constexpr const char* _type_key = "relay.SourceName"; | ||
| TVM_DECLARE_NODE_TYPE_INFO(SourceNameNode, Node); | ||
| }; | ||
|
|
||
| /*! | ||
| * \brief The source name of a file span. | ||
| * \sa SourceNameNode, Span | ||
| */ | ||
| class SourceName : public NodeRef { | ||
| public: | ||
| /*! \brief default constructor */ | ||
| SourceName() {} | ||
|
|
||
| /*! \brief constructor from node pointer */ | ||
| explicit SourceName(std::shared_ptr<Node> n) : NodeRef(n) {} | ||
| /*! | ||
| * \brief access the internal node container | ||
| * \return the pointer to the internal node container | ||
| */ | ||
| inline const SourceNameNode* operator->() const; | ||
|
|
||
| /*! | ||
| * \brief Get an SourceName for a given operator name. | ||
| * Will raise an error if the source name has not been registered. | ||
| * \param name Name of the operator. | ||
| * \return Reference to a SourceName valid throughout program lifetime. | ||
| */ | ||
| TVM_DLL static const SourceName& Get(const std::string& name); | ||
|
|
||
| /*! \brief specify container node */ | ||
| using ContainerType = SourceNameNode; | ||
| }; | ||
|
|
||
| /*! | ||
| * \brief Span information for debugging purposes | ||
| */ | ||
| class Span; | ||
| /*! | ||
| * \brief Stores locations in frontend source that generated a node. | ||
| */ | ||
| class SpanNode : public Node { | ||
| public: | ||
| /*! \brief The source name */ | ||
| SourceName source; | ||
| /*! \brief Line number */ | ||
| int lineno; | ||
| /*! \brief column offset */ | ||
| int col_offset; | ||
| // override attr visitor | ||
| void VisitAttrs(AttrVisitor* v) final { | ||
| v->Visit("source", &source); | ||
| v->Visit("lineno", &lineno); | ||
| v->Visit("col_offset", &col_offset); | ||
| } | ||
|
|
||
| TVM_DLL static Span make(SourceName source, int lineno, int col_offset); | ||
|
|
||
| static constexpr const char* _type_key = "relay.Span"; | ||
| TVM_DECLARE_NODE_TYPE_INFO(SpanNode, Node); | ||
| }; | ||
|
|
||
| RELAY_DEFINE_NODE_REF(Span, SpanNode, NodeRef); | ||
|
|
||
| /*! | ||
| * \brief This is the base node container of all relay structures. | ||
| */ | ||
| class RelayNode : public Node { | ||
| public: | ||
| /*! \brief The location of the program in a SourceFragment can be null, | ||
| * check with span.defined() */ | ||
| mutable Span span; | ||
|
|
||
| static constexpr const char* _type_key = "relay.Node"; | ||
| TVM_DECLARE_BASE_NODE_INFO(RelayNode, Node); | ||
| }; | ||
|
|
||
| /*! | ||
| * \brief Get a reference type from a Node ptr type | ||
| * | ||
| * It is always important to get a reference type | ||
| * if we want to return a value as reference or keep | ||
| * the node alive beyond the scope of the function. | ||
| * | ||
| * \param ptr The node pointer | ||
| * \tparam RefType The reference type | ||
| * \tparam NodeType The node type | ||
| * \return The corresponding RefType | ||
| */ | ||
| template <typename RefType, typename NodeType> | ||
| RefType GetRef(const NodeType* ptr) { | ||
| static_assert(std::is_same<typename RefType::ContainerType, NodeType>::value, | ||
| "Can only cast to the ref of same container type"); | ||
| return RefType(const_cast<NodeType*>(ptr)->shared_from_this()); | ||
| } | ||
|
|
||
| // TODO(@tqchen, @jroesch): can we move these semantics to HalideIR | ||
| template <typename T> | ||
| inline const T* As(const NodeRef& node) { | ||
| const Node* ptr = static_cast<const Node*>(node.get()); | ||
| if (ptr && (ptr->is_type<T>() || ptr->derived_from<T>())) { | ||
| return static_cast<const T*>(ptr); | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| template <typename SubRef, typename BaseRef> | ||
| SubRef Downcast(BaseRef ref) { | ||
| CHECK(ref->template is_type<typename SubRef::ContainerType>()) | ||
| << "Downcast from " << ref->type_key() << " to " | ||
| << SubRef::ContainerType::_type_key << " failed."; | ||
| return SubRef(ref.node_); | ||
| } | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_RELAY_BASE_H_ | ||
This file contains hidden or 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,121 @@ | ||
| /*! | ||
| * Copyright (c) 2018 by Contributors | ||
| * \file tvm/relay/environment.h | ||
| * \brief The global environment: contains information needed to | ||
| * compile & optimize Relay programs. | ||
| */ | ||
| #ifndef TVM_RELAY_ENVIRONMENT_H_ | ||
| #define TVM_RELAY_ENVIRONMENT_H_ | ||
|
|
||
| #include <tvm/relay/error.h> | ||
| #include <tvm/relay/expr.h> | ||
| #include <tvm/relay/op.h> | ||
| #include <tvm/relay/type.h> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| struct Environment; | ||
|
|
||
| /*! \brief The global environment of Relay programs. | ||
| * | ||
| * The global environment contains the global | ||
| * information needed to compile a Relay program. | ||
| * | ||
| * It contains all global functions, and configuration | ||
| * options. | ||
| * | ||
| * Many operations require access to the global | ||
| * Environment. We pass the Environment by value | ||
| * in a functional style as an explicit argument, | ||
| * but we mutate the Environment while optimizing | ||
| * Relay programs. | ||
| * | ||
| * The functional style allows users to construct custom | ||
| * environments easily, for example each thread can store | ||
| * an Environment while auto-tuning. | ||
| * */ | ||
|
|
||
| class EnvironmentNode : public RelayNode { | ||
| public: | ||
| /*! \brief A map from ids to all global functions. */ | ||
| tvm::Map<GlobalVar, Function> functions; | ||
|
|
||
| EnvironmentNode() {} | ||
|
|
||
| void VisitAttrs(tvm::AttrVisitor* v) final { | ||
| v->Visit("functions", &functions); | ||
| v->Visit("global_map_", &global_map_); | ||
| } | ||
|
|
||
| TVM_DLL static Environment make(tvm::Map<GlobalVar, Function> global_funcs); | ||
|
|
||
| /*! \brief Add a function to the global environment. | ||
| * \param var The name of the global function. | ||
| * \param func The function. | ||
| * \param update Controls whether you can replace a definition in the | ||
| * environment. | ||
| */ | ||
| void Add(const GlobalVar& var, const Function& func, bool update = false); | ||
|
|
||
| /*! \brief Update a function in the global environment. | ||
| * \param var The name of the global function to update. | ||
| * \param func The new function. | ||
| */ | ||
| void Update(const GlobalVar& var, const Function& func); | ||
|
|
||
| /*! \brief Remove a function from the global environment. | ||
| * \param var The name of the global function to update. | ||
| */ | ||
| void Remove(const GlobalVar& var); | ||
|
|
||
| /*! \brief Lookup a global function by its variable. | ||
| * \param str The unique string specifying the global variable. | ||
| * \returns The global variable. | ||
| */ | ||
| GlobalVar GetGlobalVar(const std::string& str); | ||
|
|
||
| /*! \brief Lookup a global function by its variable. | ||
| * \param var The global var to lookup. | ||
| * \returns The function named by the variable argument. | ||
| */ | ||
| Function Lookup(const GlobalVar& var); | ||
|
|
||
| /*! \brief Lookup a global function by its string name | ||
| * \param name The name of the function. | ||
| * \returns The function named by the argument. | ||
| */ | ||
| Function Lookup(const std::string& name); | ||
|
|
||
| /*! \brief Combine with another Environment. | ||
| * \param other The other environment. | ||
| */ | ||
| void Merge(const Environment& other); | ||
|
|
||
| static constexpr const char* _type_key = "relay.Environment"; | ||
| TVM_DECLARE_NODE_TYPE_INFO(EnvironmentNode, Node); | ||
|
|
||
| private: | ||
| /*! \brief A map from string names to global variables that | ||
| * ensures global uniqueness. | ||
| */ | ||
| tvm::Map<std::string, GlobalVar> global_map_; | ||
| }; | ||
|
|
||
| struct Environment : public NodeRef { | ||
| Environment() {} | ||
| explicit Environment(std::shared_ptr<tvm::Node> p) : NodeRef(p) {} | ||
jroesch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| inline EnvironmentNode* operator->() const { | ||
| return static_cast<EnvironmentNode*>(node_.get()); | ||
| } | ||
|
|
||
| using ContainerType = EnvironmentNode; | ||
| }; | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_RELAY_ENVIRONMENT_H_ | ||
This file contains hidden or 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,36 @@ | ||
| /*! | ||
| * Copyright (c) 2018 by Contributors | ||
| * \file error.h | ||
| * \brief The set of errors raised by Relay. | ||
| */ | ||
| #ifndef TVM_RELAY_ERROR_H_ | ||
| #define TVM_RELAY_ERROR_H_ | ||
|
|
||
| #include <string> | ||
| #include "./base.h" | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| struct Error : dmlc::Error { | ||
| explicit Error(const std::string &msg) : dmlc::Error(msg) {} | ||
| }; | ||
|
|
||
| struct InternalError : Error { | ||
| explicit InternalError(const std::string &msg) : Error(msg) {} | ||
| }; | ||
|
|
||
| // TODO(@jroesch): we should change spanned errors to report | ||
| // errors against the Environment, inverting control to error definition. | ||
| struct FatalTypeError : dmlc::Error { | ||
| explicit FatalTypeError(const std::string &s) : dmlc::Error(s) {} | ||
| }; | ||
|
|
||
| struct TypecheckerError : public dmlc::Error { | ||
| explicit TypecheckerError(const std::string &msg) : Error(msg) {} | ||
| }; | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_RELAY_ERROR_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.