-
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.
- Loading branch information
1 parent
fc419df
commit a2fc6db
Showing
41 changed files
with
567 additions
and
343 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
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,61 @@ | ||
#ifndef TVM_RELAY_BACKEND_GLOBAL_VAR_SUPPLY_H | ||
#define TVM_RELAY_BACKEND_GLOBAL_VAR_SUPPLY_H | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include "tvm/ir/expr.h" | ||
#include "tvm/ir/name_supply.h" | ||
|
||
namespace tvm { | ||
|
||
class GlobalVarSupplyNode : public Object { | ||
public: | ||
GlobalVarSupplyNode() : GlobalVarSupplyNode(NameSupply("")) {} | ||
|
||
explicit GlobalVarSupplyNode(NameSupply name_supply); | ||
|
||
GlobalVar FreshGlobal(String name, bool add_prefix = true); | ||
|
||
GlobalVar UniqueGlobalFor(const String& name, bool add_prefix = true); | ||
|
||
void VisitAttrs(AttrVisitor* v) { | ||
v->Visit("name_supply", &name_supply_); | ||
} | ||
|
||
NameSupply name_supply_; | ||
|
||
static constexpr const char* _type_key = "GlobalVarSupply"; | ||
static constexpr const bool _type_has_method_sequal_reduce = false; | ||
static constexpr const bool _type_has_method_shash_reduce = false; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(GlobalVarSupplyNode, Object); | ||
|
||
private: | ||
std::unordered_map<std::string, GlobalVar> name_to_var_map_; | ||
|
||
friend class GlobalVarSupply; | ||
}; | ||
|
||
class GlobalVarSupply : public ObjectRef { | ||
public: | ||
TVM_DLL explicit GlobalVarSupply(const NameSupply& name_supply = NameSupply::NameSupplyWithPrefix(""), | ||
std::unordered_map<std::string, GlobalVar> name_to_var_map = {}); | ||
|
||
TVM_DLL static GlobalVarSupply GlobalVarSupplyFromNameSupply(const NameSupply& name_supply); | ||
|
||
TVM_DLL static GlobalVarSupply EmptySupply(); | ||
|
||
explicit GlobalVarSupply(ObjectPtr<Object> n) : ObjectRef(n) {} | ||
/*! \return mutable pointers to the node. */ | ||
GlobalVarSupplyNode* operator->() const { | ||
auto* ptr = get_mutable(); | ||
ICHECK(ptr != nullptr); | ||
return static_cast<GlobalVarSupplyNode*>(ptr); | ||
} | ||
|
||
TVM_DEFINE_OBJECT_REF_COW_METHOD(GlobalVarSupplyNode); | ||
}; | ||
|
||
} | ||
|
||
#endif // TVM_RELAY_BACKEND_GLOBAL_VAR_SUPPLY_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,70 @@ | ||
#ifndef TVM_RELAY_BACKEND_NAME_SUPPLY_H | ||
#define TVM_RELAY_BACKEND_NAME_SUPPLY_H | ||
|
||
#include "tvm/ir/expr.h" | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace tvm { | ||
|
||
class NameSupplyNode : public Object { | ||
public: | ||
NameSupplyNode() : NameSupplyNode("") {} | ||
|
||
explicit NameSupplyNode(const String& prefix); | ||
|
||
String FreshName(const String& name, bool add_prefix = true); | ||
|
||
String ReserveName(const String& name, bool add_prefix = true); | ||
|
||
bool ContainsName(const String& name, bool add_prefix = true); | ||
|
||
void Clear(); | ||
|
||
void VisitAttrs(AttrVisitor* v) { | ||
v->Visit("prefix", &prefix_); | ||
} | ||
|
||
// Prefix for all GlobalVar names. It can be empty. | ||
std::string prefix_; | ||
|
||
static constexpr const char* _type_key = "NameSupply"; | ||
static constexpr const bool _type_has_method_sequal_reduce = false; | ||
static constexpr const bool _type_has_method_shash_reduce = false; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(NameSupplyNode, Object); | ||
|
||
private: | ||
String prefix_module_name(const String& name); | ||
|
||
std::string GetUniqueName(std::string name); | ||
|
||
// Key is function_name. Value is a counter. | ||
std::unordered_map<std::string, int> name_map; | ||
|
||
friend class NameSupply; | ||
}; | ||
|
||
class NameSupply : public ObjectRef { | ||
public: | ||
TVM_DLL NameSupply(); | ||
|
||
TVM_DLL explicit NameSupply(const String& prefix, std::unordered_map<std::string, int> name_map = {}); | ||
|
||
TVM_DLL static NameSupply NameSupplyWithPrefix(const String& prefix = ""); | ||
|
||
TVM_DLL static NameSupply EmptySupply(); | ||
|
||
explicit NameSupply(ObjectPtr<Object> n) : ObjectRef(n) {} | ||
/*! \return mutable pointers to the node. */ | ||
NameSupplyNode* operator->() const { | ||
auto* ptr = get_mutable(); | ||
ICHECK(ptr != nullptr); | ||
return static_cast<NameSupplyNode*>(ptr); | ||
} | ||
|
||
TVM_DEFINE_OBJECT_REF_COW_METHOD(NameSupplyNode); | ||
}; | ||
|
||
} | ||
|
||
#endif // TVM_NAME_SUPPLY_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
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,30 @@ | ||
import tvm | ||
from tvm import Object | ||
from . import _ffi_api | ||
|
||
|
||
@tvm._ffi.register_object("NameSupply") | ||
class NameSupply(Object): | ||
|
||
def __init__(self, prefix=""): | ||
self.__init_handle_by_constructor__(_ffi_api.NameSupply, prefix) | ||
|
||
def fresh_name(self, name, add_prefix=True): | ||
return _ffi_api.NameSupply_FreshName(self, name, add_prefix) | ||
|
||
def reserve_name(self, name, add_prefix=True): | ||
return _ffi_api.NameSupply_ReserveName(self, name, add_prefix) | ||
|
||
|
||
@tvm._ffi.register_object("GlobalVarSupply") | ||
class GlobalVarSupply(Object): | ||
|
||
def __init__(self, name_supply=None): | ||
name_supply = name_supply if name_supply is not None else NameSupply("") | ||
self.__init_handle_by_constructor__(_ffi_api.GlobalVarSupply, name_supply) | ||
|
||
def fresh_global(self, name, add_prefix=True): | ||
return _ffi_api.GlobalVarSupply_FreshGlobal(self, name, add_prefix) | ||
|
||
def unique_global_for(self, name, add_prefix=True): | ||
return _ffi_api.GlobalVarSupply_UniqueGlobalFor(self, name, add_prefix) |
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
Oops, something went wrong.