Skip to content

Commit

Permalink
Add tvmllvm backend for shared_object builds with cpp runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippvK authored and Philipp v. K committed Jun 15, 2022
1 parent 0b4adb5 commit 176a494
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
7 changes: 4 additions & 3 deletions mlonmcu/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ArtifactFormat(Enum): # TODO: ArtifactType, ArtifactKind?
PATH = 10 # NOT A DIRECTORY?
RAW = 11
BIN = 11
SHARED_OBJECT = 12 # Here: the parent tar archive


def lookup_artifacts(artifacts, name=None, fmt=None, flags=None, first_only=False):
Expand Down Expand Up @@ -112,7 +113,7 @@ def validate(self):
assert self.content is not None
elif self.fmt in [ArtifactFormat.RAW, ArtifactFormat.BIN]:
assert self.raw is not None
elif self.fmt in [ArtifactFormat.MLF]:
elif self.fmt in [ArtifactFormat.MLF, ArtifactFormat.SHARED_OBJECT]:
assert self.raw is not None
elif self.fmt in [ArtifactFormat.PATH]:
assert self.path is not None
Expand All @@ -139,7 +140,7 @@ def export(self, dest, extract=False):
assert not extract, "extract option is only available for ArtifactFormat.MLF"
with open(filename, "wb") as handle:
handle.write(self.raw)
elif self.fmt in [ArtifactFormat.MLF]:
elif self.fmt in [ArtifactFormat.MLF, ArtifactFormat.SHARED_OBJECT]:
with open(filename, "wb") as handle:
handle.write(self.raw)
if extract:
Expand All @@ -161,7 +162,7 @@ def print_summary(self):
print(self.content)
elif self.fmt in [ArtifactFormat.RAW, ArtifactFormat.BIN]:
print(f"Data Size: {len(self.raw)}B")
elif self.fmt in [ArtifactFormat.MLF]:
elif self.fmt in [ArtifactFormat.MLF, ArtifactFormat.SHARED_OBJECT]:
print(f"Archive Size: {len(self.raw)}B")
elif self.fmt in [ArtifactFormat.PATH]:
print(f"File Location: {self.path}")
Expand Down
2 changes: 2 additions & 0 deletions mlonmcu/flow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from mlonmcu.flow.tflm.framework import TFLMFramework
from mlonmcu.flow.tvm.backend.tvmaot import TVMAOTBackend
from mlonmcu.flow.tvm.backend.tvmcg import TVMCGBackend
from mlonmcu.flow.tvm.backend.tvmllvm import TVMLLVMBackend
from mlonmcu.flow.tvm.backend.tvmrt import TVMRTBackend
from mlonmcu.flow.tvm.framework import TVMFramework

Expand All @@ -41,6 +42,7 @@
"tvmaot": TVMAOTBackend,
"tvmrt": TVMRTBackend,
"tvmcg": TVMCGBackend,
"tvmllvm": TVMLLVMBackend,
}

SUPPORTED_FRAMEWORK_BACKENDS = {
Expand Down
4 changes: 2 additions & 2 deletions mlonmcu/flow/tvm/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
from .tvmaot import TVMAOTBackend
from .tvmcg import TVMCGBackend
from .tvmrt import TVMRTBackend
from .tvmllvm import TVMLLVMBackend


__all__ = ["TVMBackend", "TVMAOTBackend", "TVMCGBackend", "TVMRTBackend"]
__all__ = ["TVMBackend", "TVMAOTBackend", "TVMCGBackend", "TVMRTBackend", "TVMLLVMBackend"]
84 changes: 84 additions & 0 deletions mlonmcu/flow/tvm/backend/tvmllvm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# Copyright (c) 2022 TUM Department of Electrical and Computer Engineering.
#
# This file is part of MLonMCU.
# See https://github.com/tum-ei-eda/mlonmcu.git for further info.
#
# Licensed 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.
#
import sys
import tempfile
import json
import tarfile
from pathlib import Path

from .backend import TVMBackend
from mlonmcu.flow.backend import main
from mlonmcu.artifact import Artifact, ArtifactFormat
from .tvmc_utils import get_tvmaot_tvmc_args


# Warning: This is only ment to be used with the TvmPlatform!

class TVMLLVMBackend(TVMBackend):

FEATURES = [
*TVMBackend.FEATURES,
]

DEFAULTS = {
**TVMBackend.DEFAULTS,
}

name = "tvmllvm"


def get_tvmc_compile_args(self, out):
return super().get_tvmc_compile_args(out, executor="graph", target="llvm", runtime="cpp", fmt="so")

def generate_code(self, verbose=False):
artifacts = []
assert self.model is not None

dump = []
with tempfile.TemporaryDirectory() as temp_dir:
out_path = Path(temp_dir) / f"{self.prefix}.tar"
out = self.invoke_tvmc_compile(out_path, dump=dump, verbose=verbose)
tar_dir = Path(temp_dir) / self.prefix
tarfile.open(out_path).extractall(tar_dir)

with open(out_path, "rb") as handle:
mlf_data = handle.read()
artifacts.append(
Artifact(
f"{self.prefix}.tar",
raw=mlf_data,
fmt=ArtifactFormat.SHARED_OBJECT,
archive=True,
)
)

stdout_artifact = Artifact(
"tvmc_compile_out.log", content=out, fmt=ArtifactFormat.TEXT
) # TODO: rename to tvmllvm_out.log?
artifacts.append(stdout_artifact)
self.artifacts = artifacts


if __name__ == "__main__":
sys.exit(
main(
TVMLLVMBackend,
args=sys.argv[1:],
)
) # pragma: no cover

0 comments on commit 176a494

Please sign in to comment.