Skip to content
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

[microNPU] Add MergeConstants pass #12029

Merged
merged 4 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/tvm/relay/backend/contrib/ethosu/tir/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def lower_ethosu(sch, args, const_dict, name="main"):
mod = tvm.tir.transform.RemoveNoOp()(mod)
mod, const_dict = ethosu_passes.EncodeConstants(const_dict)(mod)
mod = ethosu_passes.HoistAllocates()(mod)
# MergeConstant pass currently does not support striped schedules.
# It requires further investigation.
if not util.is_striping_enabled():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave a comment that MergeConstant pass currently does not support striped schedules and requires further investigation.

mod, const_dict = ethosu_passes.MergeConstants(const_dict)(mod)
mod = ethosu_passes.CopyComputeReordering()(mod)

# When striping is enabled and if storage_rewrite is not run
Expand Down
35 changes: 35 additions & 0 deletions python/tvm/relay/backend/contrib/ethosu/tir/passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,38 @@ def CopyComputeReordering(max_copy_movements: Optional[int] = None) -> tvm.IRMod
The new module with copy and compute nodes reordered.
"""
return _ffi_api.CopyComputeReordering(max_copy_movements)


def MergeConstants(const_dict):
"""
This pass looks for the constants used by each compute operator
and merges them into a single buffer.
Constants written to a buffer with local scope are not merged.
"""

def _merge_constants(mod):
nonlocal const_dict
try:
mod["main"]
except:
raise tvm.TVMError(
"Expected a single primitive function called 'main'. "
"Please run the MergeConstants pass in conjunction with the LowerToTIR() pass."
)

new_const_dict = {}
for param in const_dict.keys():
new_const_dict[tvm.tir.IntImm("int64", param)] = tvm.nd.array(const_dict[param])
mod["main"] = mod["main"].with_attr("ethos-u.const_dict", new_const_dict)

mod = _ffi_api.MergeConstants()(mod)
const_dict = mod["main"].attrs["ethos-u.const_dict"]
mod = _ffi_api.RemoveConstDictAttribute()(mod)

new_const_dict = {}
for param in const_dict.keys():
new_const_dict[int(param)] = const_dict[param].numpy()

return mod, new_const_dict

return _merge_constants
Loading