Skip to content

Commit a0bfa5d

Browse files
Add shared weights flow to llama export script
1 parent 5350547 commit a0bfa5d

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

backends/mediatek/preprocess.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ def preprocess_multimethod(
146146
infos_dict = collections.defaultdict(list)
147147
models_dict = collections.defaultdict(list)
148148
result_dict = collections.defaultdict(list)
149-
preprocess_result_list = []
150149
for method_name, method_results in preprocess_results.items():
151150
for idx, result in enumerate(method_results):
152151
shared_blob_key = None
@@ -168,12 +167,7 @@ def preprocess_multimethod(
168167
data_store_output_dict = dict()
169168
for key, models in models_dict.items():
170169
ndm = NamedDataStore()
171-
print('------------------')
172-
print(key)
173-
print('Original DLA sizes: {}'.format([len(model) for model in models]))
174170
blob, new_models = mtk_neuron.extract_shared_data(models, options='-e union')
175-
print('Extracted data size: {}'.format(len(blob)))
176-
print('New DLA sizes: {}'.format([len(model) for model in new_models]))
177171
ndm.add_named_data(key, bytes(blob))
178172
data_store_output_dict[key] = ndm.get_named_data_store_output()
179173
models.clear()

examples/mediatek/aot_utils/llm_utils/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ def generate_mask(
336336
return combined_mask.copy()
337337

338338

339-
def get_dest_path(output_folder, exp_name, shape, chunk_idx):
340-
dest_folder_root = output_folder + f"_{shape}"
339+
def get_dest_path(output_folder, exp_name, shape=None, chunk_idx=0):
340+
dest_folder_root = output_folder + f"{f'_{shape}' if shape is not None else ''}"
341341
os.makedirs(dest_folder_root, exist_ok=True)
342-
fname = f"{exp_name}_{shape}_{chunk_idx}.pte"
342+
fname = f"{exp_name}{f'_{shape}' if shape is not None else ''}_{chunk_idx}.pte"
343343
dest_path = os.path.join(dest_folder_root, fname)
344344

345345
return dest_path

examples/mediatek/model_export_scripts/llama.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
NeuropilotQuantizer,
4343
Precision,
4444
)
45+
from executorch.exir.backend.backend_api import to_backend, MethodProgramsPartitionerSpec
4546
from executorch.exir.backend.backend_details import CompileSpec
4647
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
4748
from tqdm import tqdm
@@ -331,51 +332,65 @@ def export_to_et_ir(
331332
prepared_graph(*example_inputs) # dummy calibration
332333
converted_graph = convert_pt2e(prepared_graph, fold_quantize=False)
333334

334-
print("Getting ATen Dialect Graph")
335+
method_to_edge_program = {}
336+
method_to_partitioner = {}
337+
edge_compile_config=exir.EdgeCompileConfig(_check_ir_validity=False)
338+
339+
model_shared_key_name = f'{exp_name}_{chunk_idx}'
340+
335341
# Fixed Shape Export Here
336342
for shape, ntok_and_cache in export_shapes.items():
337-
dest_path = get_dest_path(output_folder, exp_name, shape, chunk_idx)
338-
print(f"Exporting Shape {shape} to:\n{dest_path}")
343+
model_fname = f'{exp_name}_{shape}_{chunk_idx}'
339344
example_inputs = model.get_example_inputs(*ntok_and_cache)
345+
print(f"Getting ATen Dialect Graph for {exp_name} {shape} chunk {chunk_idx}")
340346
aten_dialect: exir.ExportedProgram = torch.export.export(
341347
converted_graph, example_inputs, strict=True
342348
)
343349

344-
print("Lowering to Edge Dialect Graph")
345-
edge_program: exir.EdgeProgramManager = exir.to_edge(
346-
aten_dialect,
347-
compile_config=exir.EdgeCompileConfig(_check_ir_validity=False),
348-
)
350+
method_to_edge_program[f'{model_fname}'] = exir.to_edge(aten_dialect).exported_program()
349351
del aten_dialect
350352

351-
print("Delegating Edge Program to Neuropilot Backend")
352353
compile_spec = [
353354
CompileSpec("gno", struct.pack("3s", b"LTS")),
354355
CompileSpec("gno-exp", struct.pack("0s", b"")),
355356
CompileSpec("gno-non-4d-tiling", struct.pack("0s", b"")),
356357
CompileSpec("ImportForever", struct.pack("?", True)),
358+
CompileSpec("ExtractSharedBlobKey", model_shared_key_name.encode()),
357359
]
358-
partitioner = NeuropilotPartitioner(compile_spec)
359-
delegated_program = edge_program.to_backend(partitioner)
360-
print("Exported Delegated Program:")
361-
print(delegated_program.exported_program())
362-
del edge_program
363-
364-
print("Transforming delegated program to executorch backend")
365-
executorch_program = delegated_program.to_executorch(
366-
config=exir.ExecutorchBackendConfig(
367-
memory_planning_pass=exir.passes.MemoryPlanningPass(
368-
alloc_graph_input=False,
369-
alloc_graph_output=False,
370-
),
371-
extract_delegate_segments=True,
372-
)
360+
method_to_partitioner[f'{model_fname}'] = NeuropilotPartitioner(compile_spec)
361+
362+
print("Delegating Edge Program to Neuropilot Backend")
363+
delegated_program = to_backend(
364+
MethodProgramsPartitionerSpec(
365+
method_to_edge_program,
366+
method_to_partitioner
373367
)
368+
)
374369

375-
print(f"ET Model Dest: {dest_path}\n")
376-
os.makedirs(dest_path.rsplit("/", 1)[0], exist_ok=True)
377-
with open(dest_path, "wb") as file:
378-
file.write(executorch_program.buffer)
370+
edge_manager = exir.EdgeProgramManager(
371+
delegated_program,
372+
compile_config=edge_compile_config
373+
)
374+
del delegated_program
375+
376+
print("Transforming delegated program to executorch backend")
377+
executorch_program = edge_manager.to_executorch(
378+
config=exir.ExecutorchBackendConfig(
379+
memory_planning_pass=exir.passes.MemoryPlanningPass(
380+
alloc_graph_input=False,
381+
alloc_graph_output=False,
382+
),
383+
extract_delegate_segments=True,
384+
)
385+
)
386+
del edge_manager
387+
print(f'\n Model Size: {len(executorch_program.buffer)}')
388+
389+
dest_path = get_dest_path(output_folder, exp_name, None, chunk_idx)
390+
print(f"{exp_name} ET Model chunk {chunk_idx} Dest: {dest_path}\n")
391+
os.makedirs(dest_path.rsplit("/", 1)[0], exist_ok=True)
392+
with open(dest_path, "wb") as file:
393+
file.write(executorch_program.buffer)
379394

380395

381396
def main():

0 commit comments

Comments
 (0)