-
Notifications
You must be signed in to change notification settings - Fork 607
Move compile spec to ArmTester interface #2991
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
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,118 @@ | |
logger.setLevel(logging.INFO) | ||
|
||
|
||
class ArmCompileSpecBuilder: | ||
def __init__(self): | ||
self.compile_spec: List[CompileSpec] = [] | ||
self.compiler_flags = [] | ||
self.output_format = None | ||
self.path_for_intermediates = None | ||
self.permute_nhwc = False | ||
|
||
def ethosu_compile_spec( | ||
self, | ||
config: str, | ||
system_config: Optional[str] = None, | ||
memory_mode: Optional[str] = None, | ||
extra_flags: Optional[str] = None, | ||
config_ini: Optional[str] = "Arm/vela.ini", | ||
): | ||
""" | ||
Generate compile spec for Ethos-U NPU | ||
|
||
Args: | ||
config: Ethos-U accelerator configuration, e.g. ethos-u55-128 | ||
system_config: System configuration to select from the Vel | ||
configuration file | ||
memory_mode: Memory mode to select from the Vela configuration file | ||
extra_flags: Extra flags for the Vela compiler | ||
config_ini: Vela configuration file(s) in Python ConfigParser .ini | ||
file format | ||
""" | ||
assert ( | ||
self.output_format is None | ||
), f"Output format already set to f{self.output_format}" | ||
self.output_format = "vela" | ||
self.compiler_flags = [ | ||
f"--accelerator-config={config}", | ||
f"--config={config_ini}", | ||
] | ||
if system_config is not None: | ||
self.compiler_flags.append(f"--system-config={system_config}") | ||
if memory_mode is not None: | ||
self.compiler_flags.append(f"--memory-mode={memory_mode}") | ||
if extra_flags is not None: | ||
self.compiler_flags.append(extra_flags) | ||
|
||
return self | ||
|
||
def tosa_compile_spec(self): | ||
""" | ||
Generate compile spec for TOSA flatbuffer output | ||
""" | ||
assert ( | ||
self.output_format is None | ||
), f"Output format already set: {self.output_format}" | ||
self.output_format = "tosa" | ||
return self | ||
|
||
def dump_intermediate_tosa(self, output_path: str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can all of these be So you can just do
and
|
||
""" | ||
Output intermediate .tosa file | ||
""" | ||
self.path_for_intermediates = output_path | ||
return self | ||
|
||
def set_permute_memory_format(self, set_nhwc_permutation: bool = True): | ||
self.permute_nhwc = set_nhwc_permutation | ||
return self | ||
|
||
def build(self): | ||
""" | ||
Generate a list of compile spec objects from the builder | ||
""" | ||
if self.output_format == "vela": | ||
self.compile_spec += [ | ||
CompileSpec("output_format", "vela".encode()), | ||
CompileSpec("compile_flags", " ".join(self.compiler_flags).encode()), | ||
] | ||
elif self.output_format == "tosa": | ||
self.compile_spec.append(CompileSpec("output_format", "tosa".encode())) | ||
|
||
if self.path_for_intermediates is not None: | ||
self.compile_spec.append( | ||
CompileSpec("debug_tosa_path", self.path_for_intermediates.encode()) | ||
) | ||
|
||
if self.permute_nhwc: | ||
self.compile_spec.append( | ||
CompileSpec("permute_memory_format", "nhwc".encode()) | ||
) | ||
|
||
return self.compile_spec | ||
|
||
|
||
def is_permute_memory(compile_spec: List[CompileSpec]) -> bool: | ||
for spec in compile_spec: | ||
if spec.key == "permute_memory_format": | ||
return spec.value.decode() == "nhwc" | ||
return False | ||
|
||
|
||
def is_tosa(compile_spec: List[CompileSpec]) -> bool: | ||
for spec in compile_spec: | ||
if spec.key == "output_format": | ||
return spec.value.decode() == "tosa" | ||
return False | ||
|
||
|
||
def get_intermediate_path(compile_spec: List[CompileSpec]) -> str: | ||
for spec in compile_spec: | ||
if spec.key == "debug_tosa_path": | ||
return spec.value.decode() | ||
return None | ||
|
||
|
||
def generate_ethosu_compile_spec( | ||
config: str, | ||
permute_memory_to_nhwc: Optional[bool] = None, | ||
|
@@ -46,45 +158,31 @@ def generate_ethosu_compile_spec( | |
extra_flags: Optional[str] = None, | ||
config_ini: Optional[str] = "Arm/vela.ini", | ||
) -> List[CompileSpec]: | ||
""" | ||
Generate compile spec for Ethos-U NPU | ||
""" | ||
compiler_flags = [f"--accelerator-config={config}", f"--config={config_ini}"] | ||
if system_config is not None: | ||
compiler_flags.append(f"--system-config={system_config}") | ||
if memory_mode is not None: | ||
compiler_flags.append(f"--memory-mode={memory_mode}") | ||
if extra_flags is not None: | ||
compiler_flags.append(extra_flags) | ||
|
||
compile_spec = [ | ||
CompileSpec("output_format", "vela".encode()), | ||
CompileSpec("compile_flags", " ".join(compiler_flags).encode()), | ||
] | ||
|
||
if permute_memory_to_nhwc: | ||
compile_spec.append(CompileSpec("permute_memory_format", "nhwc".encode())) | ||
|
||
return compile_spec | ||
return ( | ||
ArmCompileSpecBuilder() | ||
.ethosu_compile_spec( | ||
config, | ||
system_config=system_config, | ||
memory_mode=memory_mode, | ||
extra_flags=extra_flags, | ||
config_ini=config_ini, | ||
) | ||
.set_permute_memory_format(permute_memory_to_nhwc) | ||
.build() | ||
) | ||
|
||
|
||
def generate_tosa_compile_spec( | ||
permute_memory_to_nhwc: Optional[bool] = None, | ||
output_path: Optional[str] = None, | ||
) -> List[CompileSpec]: | ||
""" | ||
Generate compile spec for TOSA flatbuffer output | ||
""" | ||
|
||
compile_spec = [CompileSpec("output_format", "tosa".encode())] | ||
|
||
if permute_memory_to_nhwc: | ||
compile_spec.append(CompileSpec("permute_memory_format", "nhwc".encode())) | ||
|
||
if output_path is not None: | ||
compile_spec.append(CompileSpec("debug_tosa_path", output_path.encode())) | ||
|
||
return compile_spec | ||
return ( | ||
ArmCompileSpecBuilder() | ||
.tosa_compile_spec() | ||
.set_permute_memory_format(permute_memory_to_nhwc) | ||
.dump_intermediate_tosa(output_path) | ||
.build() | ||
) | ||
|
||
|
||
@final | ||
|
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just do this in
__init__
or you can create a@classmethod
if you want to write another constructor.