99#
1010
1111import logging
12+ import os
1213import struct
13- from typing import final , List , Optional
14+ from typing import final
1415
1516import numpy as np
1617import torch
@@ -45,10 +46,11 @@ class NeutronCompileSpecBuilder:
4546 config : NeutronTargetSpec
4647
4748 def __init__ (self ):
48- self .compile_spec : List [CompileSpec ] = []
49+ self .compile_spec : list [CompileSpec ] = []
4950 self .compiler_flags = []
5051 self .output_format = None
51- self .operators_not_to_delegate : List [str ] = []
52+ self .intermediates_dir = None
53+ self .operators_not_to_delegate : list [str ] = []
5254 self .use_neutron_for_format_conversion = True
5355 self .fetch_constants_to_sram = False
5456 self .dump_kernel_selection_code = False
@@ -62,15 +64,17 @@ def _replace_colons(self, operator: str) -> str:
6264 def neutron_compile_spec (
6365 self ,
6466 config : str ,
65- extra_flags : Optional [str ] = None ,
66- operators_not_to_delegate : Optional [List [str ]] = None ,
67+ intermediates_dir : str | None = None ,
68+ extra_flags : str | None = None ,
69+ operators_not_to_delegate : list [str ] | None = None ,
6770 use_neutron_for_format_conversion : bool = True ,
6871 fetch_constants_to_sram : bool = False ,
6972 dump_kernel_selection_code : bool = False ,
7073 ) -> "NeutronCompileSpecBuilder" :
7174 """Generate compile spec for Neutron NPU
7275
7376 :param config: Neutron accelerator configuration, e.g. "imxrt700"
77+ :param intermediates_dir: Directory to store intermediate artifact files.
7478 :param extra_flags: Extra flags for the Neutron compiler
7579 :param operators_not_to_delegate: List of operators that should not be delegated
7680 :param use_neutron_for_format_conversion: If True, the EdgeProgramToIRConverter will insert `Transpose` ops to
@@ -83,6 +87,7 @@ def neutron_compile_spec(
8387 """
8488
8589 self .config = NeutronTargetSpec (config )
90+ self .intermediates_dir = intermediates_dir
8691
8792 assert (
8893 self .output_format is None
@@ -113,6 +118,7 @@ def build(self):
113118 CompileSpec ("output_format" , "tflite" .encode ()),
114119 CompileSpec ("compile_flags" , " " .join (self .compiler_flags ).encode ()),
115120 CompileSpec ("target" , self .config .get_name ().encode ()),
121+ CompileSpec ("intermediates_dir" , f"{ self .intermediates_dir } " .encode ()),
116122 CompileSpec (
117123 "operators_not_to_delegate" ,
118124 "," .join (self .operators_not_to_delegate ).encode (),
@@ -136,17 +142,19 @@ def build(self):
136142
137143def generate_neutron_compile_spec (
138144 config : str , # The target platform. For example "imxrt700".
139- system_config : Optional [str ] = None ,
140- extra_flags : Optional [str ] = None ,
141- operators_not_to_delegate : Optional [List [str ]] = None ,
145+ system_config : str | None = None ,
146+ extra_flags : str | None = None ,
147+ intermediates_dir : str | None = None ,
148+ operators_not_to_delegate : list [str ] | None = None ,
142149 use_neutron_for_format_conversion : bool = True ,
143150 fetch_constants_to_sram : bool = False ,
144151 dump_kernel_selection_code : bool = False ,
145- ) -> List [CompileSpec ]:
152+ ) -> list [CompileSpec ]:
146153 return (
147154 NeutronCompileSpecBuilder ()
148155 .neutron_compile_spec (
149156 config ,
157+ intermediates_dir = intermediates_dir ,
150158 extra_flags = extra_flags ,
151159 operators_not_to_delegate = operators_not_to_delegate ,
152160 use_neutron_for_format_conversion = use_neutron_for_format_conversion ,
@@ -163,7 +171,7 @@ class NeutronBackend(BackendDetails):
163171 @staticmethod
164172 def preprocess ( # noqa C901
165173 edge_program : ExportedProgram ,
166- compile_spec : List [CompileSpec ],
174+ compile_spec : list [CompileSpec ],
167175 ) -> PreprocessResult :
168176 logging .info ("NeutronBackend::preprocess" )
169177
@@ -173,6 +181,7 @@ def preprocess( # noqa C901
173181 compile_flags = []
174182 binary = bytes ()
175183 target = ""
184+ intermediates_dir = "None"
176185 use_neutron_for_format_conversion = None
177186 fetch_constants_to_sram = False
178187 dump_kernel_selection_code = None
@@ -181,6 +190,8 @@ def preprocess( # noqa C901
181190 output_format = spec .value .decode ()
182191 if spec .key == "target" :
183192 target = spec .value .decode ()
193+ if spec .key == "intermediates_dir" :
194+ intermediates_dir = spec .value .decode ()
184195 if spec .key == "compile_flags" :
185196 compile_flags .append (spec .value .decode ())
186197 if spec .key == "use_neutron_for_format_conversion" :
@@ -194,6 +205,10 @@ def preprocess( # noqa C901
194205 if not output_format :
195206 raise RuntimeError ("output format is required" )
196207
208+ # Check if provided intermediates_dir is a correct path (None is decoded to str)
209+ if intermediates_dir != "None" and not os .path .isdir (intermediates_dir ):
210+ raise ValueError ("intermediates_dir is not a directory path." )
211+
197212 for node in edge_program .graph .nodes :
198213 if node .op == "call_function" :
199214 logging .debug (f"Operator to be processed: { node .target } " )
@@ -228,16 +243,22 @@ def preprocess( # noqa C901
228243 fetch_constants_to_sram ,
229244 )
230245
231- # Dump the tflite file if logging level is enabled
232- if logging .root .isEnabledFor (logging .DEBUG ):
233- import os
234-
246+ # Dump the tflite file if intermediates_dir is set
247+ if intermediates_dir != "None" :
235248 logging .debug (
236- f"Serializing converted graph with tag { delegation_tag } to { os . getcwd () } "
249+ f"Serializing converted graph with tag { delegation_tag } to { intermediates_dir } "
237250 )
238- with open (f"{ delegation_tag } _pure.et.tflite" , "wb" ) as f :
251+ with open (
252+ os .path .join (intermediates_dir , f"{ delegation_tag } _pure.et.tflite" ),
253+ "wb" ,
254+ ) as f :
239255 f .write (bytes (tflite_model ))
240- with open (f"{ delegation_tag } _neutron.et.tflite" , "wb" ) as f :
256+ with open (
257+ os .path .join (
258+ intermediates_dir , f"{ delegation_tag } _neutron.et.tflite"
259+ ),
260+ "wb" ,
261+ ) as f :
241262 f .write (bytes (neutron_model ))
242263
243264 binary = PayloadComposer ().get_binary_payload (io_formats , neutron_model )
0 commit comments