|
65 | 65 | # model graphs and metadata for linking runtime results to the eager model. |
66 | 66 | # We use ``to_edge_transform_and_lower`` with ``generate_etrecord=True`` to |
67 | 67 | # automatically capture the ETRecord during the lowering process. |
68 | | - |
69 | | -import os |
70 | | -import tempfile |
71 | | - |
72 | | -import torch |
73 | | - |
74 | | -from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
75 | | -from executorch.backends.xnnpack.utils.configs import get_xnnpack_edge_compile_config |
76 | | - |
77 | | -from executorch.exir import ExecutorchProgramManager, to_edge_transform_and_lower |
78 | | -from torch.export import export, ExportedProgram |
79 | | -from torchvision import models # type: ignore[import-untyped] |
80 | | - |
81 | | -# Create Vision Transformer model |
82 | | -vit = models.vision_transformer.vit_b_16(weights="IMAGENET1K_V1") |
83 | | -model = vit.eval() |
84 | | -model_inputs = (torch.randn(1, 3, 224, 224),) |
85 | | - |
86 | | -temp_dir = tempfile.mkdtemp() |
87 | | - |
88 | | -# Export and lower model to XNNPACK delegate |
89 | | -aten_model: ExportedProgram = export(model, model_inputs, strict=True) |
90 | | -edge_program_manager = to_edge_transform_and_lower( |
91 | | - aten_model, |
92 | | - partitioner=[XnnpackPartitioner()], |
93 | | - compile_config=get_xnnpack_edge_compile_config(), |
94 | | - generate_etrecord=True, |
95 | | -) |
96 | | - |
97 | | -et_program_manager: ExecutorchProgramManager = edge_program_manager.to_executorch() |
98 | | - |
99 | | -# Save the .pte file |
100 | | -pte_path = os.path.join(temp_dir, "model.pte") |
101 | | -et_program_manager.save(pte_path) |
102 | | - |
103 | | -# Get and save ETRecord with representative inputs |
104 | | -etrecord = et_program_manager.get_etrecord() |
105 | | -etrecord.update_representative_inputs(model_inputs) |
106 | | -etrecord_path = os.path.join(temp_dir, "etrecord.bin") |
107 | | -etrecord.save(etrecord_path) |
108 | | - |
109 | | -# sphinx_gallery_start_ignore |
110 | | -from unittest.mock import patch |
111 | | - |
112 | | -# sphinx_gallery_end_ignore |
| 68 | +# |
| 69 | +# .. code-block:: python |
| 70 | +# |
| 71 | +# import os |
| 72 | +# import tempfile |
| 73 | +# |
| 74 | +# import torch |
| 75 | +# from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
| 76 | +# from executorch.backends.xnnpack.utils.configs import get_xnnpack_edge_compile_config |
| 77 | +# from executorch.exir import ExecutorchProgramManager, to_edge_transform_and_lower |
| 78 | +# from torch.export import export, ExportedProgram |
| 79 | +# from torchvision import models # type: ignore[import-untyped] |
| 80 | +# |
| 81 | +# # Create Vision Transformer model |
| 82 | +# vit = models.vision_transformer.vit_b_16(weights="IMAGENET1K_V1") |
| 83 | +# model = vit.eval() |
| 84 | +# model_inputs = (torch.randn(1, 3, 224, 224),) |
| 85 | +# |
| 86 | +# temp_dir = tempfile.mkdtemp() |
| 87 | +# |
| 88 | +# # Export and lower model to XNNPACK delegate |
| 89 | +# aten_model: ExportedProgram = export(model, model_inputs, strict=True) |
| 90 | +# edge_program_manager = to_edge_transform_and_lower( |
| 91 | +# aten_model, |
| 92 | +# partitioner=[XnnpackPartitioner()], |
| 93 | +# compile_config=get_xnnpack_edge_compile_config(), |
| 94 | +# generate_etrecord=True, |
| 95 | +# ) |
| 96 | +# |
| 97 | +# et_program_manager: ExecutorchProgramManager = edge_program_manager.to_executorch() |
| 98 | +# |
| 99 | +# # Save the .pte file |
| 100 | +# pte_path = os.path.join(temp_dir, "model.pte") |
| 101 | +# et_program_manager.save(pte_path) |
| 102 | +# |
| 103 | +# # Get and save ETRecord with representative inputs |
| 104 | +# etrecord = et_program_manager.get_etrecord() |
| 105 | +# etrecord.update_representative_inputs(model_inputs) |
| 106 | +# etrecord_path = os.path.join(temp_dir, "etrecord.bin") |
| 107 | +# etrecord.save(etrecord_path) |
| 108 | +# |
113 | 109 |
|
114 | 110 | ###################################################################### |
115 | 111 | # |
|
226 | 222 | # |----|----------------------------------------------------------------|----------------------------------------------------|----------------------------------------------------|----------------------------------------------------| ---------------------------| |
227 | 223 | # | 0 | [conv2d] | [[[tensor([-0.0130, 0.0075, -0.0334, -0.0122,... | [DELEGATE_CALL] | [[[tensor([-0.0130, 0.0075, -0.0334, -0.0122,... | [3.2530690555343034e-15] | |
228 | 224 | # | 1 | [permute, cat, add, dropout] | [[[tensor(-0.0024), tensor(0.0054), tensor(0.0... | [DELEGATE_CALL] | [[[tensor(-0.0024), tensor(0.0054), tensor(0.0... | [3.2488685838924244e-15] | |
| 225 | +# ... |
229 | 226 | # | 4 | [transpose, linear, unflatten, unsqueeze, tran...] | [[[tensor(0.0045), tensor(-0.0084), tensor(0.0... | [DELEGATE_CALL, DELEGATE_CALL, DELEGATE_CALL, ...] | [[tensor(0.0045), tensor(-0.0084), tensor(0.00... | [0.00010033142876115867] | |
| 227 | +# ... |
230 | 228 | # | 59 | [transpose_66, linear_44, unflatten_11, unsque...] | [[[tensor(-0.3346), tensor(0.1540), tensor(-0.... | [DELEGATE_CALL, DELEGATE_CALL, DELEGATE_CALL, ...] | [[tensor(-0.3346), tensor(0.1540), tensor(-0.0... | [0.02629170972698486] | |
231 | 229 | # |
232 | 230 |
|
|
285 | 283 | # Step 1: Export Model and Generate ETRecord |
286 | 284 | # ------------------------------------------ |
287 | 285 | # |
288 | | -# Same as Pipeline 1 - we reuse the model and export artifacts we already created. |
289 | | -# The key artifact needed for the CMake pipeline is: |
| 286 | +# First, we export the model and generate an ``ETRecord``, same as step 1 of pipeline 1: |
| 287 | +# |
| 288 | +# .. code-block:: python |
290 | 289 | # |
291 | | -# - ``bundled_program.bpte``: The BundledProgram file contains the model and |
292 | | -# sample inputs/outputs for testing. |
| 290 | +# import os |
| 291 | +# import tempfile |
293 | 292 | # |
294 | | -# Most of the pipeline were the same as Pipeline 1's Step 1. If you're only using |
295 | | -# the CMake pipeline, use the same export code: |
| 293 | +# import torch |
| 294 | +# from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
| 295 | +# from executorch.backends.xnnpack.utils.configs import get_xnnpack_edge_compile_config |
| 296 | +# from executorch.exir import ExecutorchProgramManager, to_edge_transform_and_lower |
| 297 | +# from torch.export import export, ExportedProgram |
| 298 | +# from torchvision import models # type: ignore[import-untyped] |
296 | 299 | # |
297 | | -# .. code-block:: python |
| 300 | +# # Create Vision Transformer model |
| 301 | +# vit = models.vision_transformer.vit_b_16(weights="IMAGENET1K_V1") |
| 302 | +# model = vit.eval() |
| 303 | +# model_inputs = (torch.randn(1, 3, 224, 224),) |
298 | 304 | # |
299 | | -# # Export and lower model (same as Pipeline 1) |
300 | | -# aten_model = export(model, model_inputs, strict=True) |
| 305 | +# temp_dir = tempfile.mkdtemp() |
| 306 | +# |
| 307 | +# # Export and lower model to XNNPACK delegate |
| 308 | +# aten_model: ExportedProgram = export(model, model_inputs, strict=True) |
301 | 309 | # edge_program_manager = to_edge_transform_and_lower( |
302 | 310 | # aten_model, |
303 | 311 | # partitioner=[XnnpackPartitioner()], |
304 | 312 | # compile_config=get_xnnpack_edge_compile_config(), |
305 | 313 | # generate_etrecord=True, |
306 | 314 | # ) |
307 | | -# et_program_manager = edge_program_manager.to_executorch() |
308 | 315 | # |
309 | | -# # Save artifacts |
| 316 | +# et_program_manager: ExecutorchProgramManager = edge_program_manager.to_executorch() |
| 317 | +# |
| 318 | +# # Save the .pte file |
| 319 | +# pte_path = os.path.join(temp_dir, "model.pte") |
310 | 320 | # et_program_manager.save(pte_path) |
| 321 | +# |
| 322 | +# # Get and save ETRecord with representative inputs |
311 | 323 | # etrecord = et_program_manager.get_etrecord() |
312 | 324 | # etrecord.update_representative_inputs(model_inputs) |
| 325 | +# etrecord_path = os.path.join(temp_dir, "etrecord.bin") |
313 | 326 | # etrecord.save(etrecord_path) |
314 | 327 | # |
315 | 328 |
|
|
329 | 342 | # serialize_from_bundled_program_to_flatbuffer, |
330 | 343 | # ) |
331 | 344 | # |
332 | | -# # Construct Method Test Suites using the same model and inputs from Pipeline 1 |
| 345 | +# # Define the method name and test inputs |
| 346 | +# # IMPORTANT: Use the same inputs as etrecord.update_representative_inputs() |
333 | 347 | # m_name = "forward" |
334 | | -# inputs = [model_inputs for _ in range(2)] |
| 348 | +# test_inputs = [model_inputs] |
335 | 349 | # |
| 350 | +# # Create test cases by running the eager model to get expected outputs |
336 | 351 | # method_test_suites = [ |
337 | 352 | # MethodTestSuite( |
338 | 353 | # method_name=m_name, |
339 | 354 | # test_cases=[ |
340 | | -# MethodTestCase(inputs=inp, expected_outputs=model(*inp)) for inp in inputs |
| 355 | +# MethodTestCase(inputs=inp, expected_outputs=model(*inp)) for inp in test_inputs |
341 | 356 | # ], |
342 | 357 | # ) |
343 | 358 | # ] |
|
355 | 370 | # |
356 | 371 |
|
357 | 372 | ###################################################################### |
358 | | -# Step 3: Run with CMake Example Runner |
| 373 | +# Step 3: Run with Devtool Example Runner |
359 | 374 | # ------------------------------------- |
360 | 375 | # |
361 | | -# Build and run the example runner with output verification and debug output enabled:: |
| 376 | +# This step we will verify the final result and generate etdump for next |
| 377 | +# step usage by using devtool example runner. |
| 378 | +# |
| 379 | +# First, build the example runner with XNNPACK backend support: |
| 380 | +# |
| 381 | +# .. code-block:: bash |
| 382 | +# |
| 383 | +# cd /path/to/executorch |
| 384 | +# ./examples/devtools/build_example_runner.sh --xnnpack |
362 | 385 | # |
363 | | -# cd executorch |
364 | | -# ./examples/devtools/build_example_runner.sh |
365 | | -# cmake-out/examples/devtools/example_runner \ |
366 | | -# --bundled_program_path="bundled_program.bpte" \ |
367 | | -# --output_verification=true \ |
368 | | -# --dump_intermediate_outputs=true |
| 386 | +# where ``--xnnpack`` is a build flag that enables XNNPACK backend support. |
| 387 | +# Then run the example runner with output verification and debug output enabled: |
| 388 | +# |
| 389 | +# .. code-block:: bash |
| 390 | +# |
| 391 | +# cmake-out/examples/devtools/example_runner \ |
| 392 | +# --bundled_program_path=/path/to/bundled_program.bpte \ |
| 393 | +# --output_verification \ |
| 394 | +# --dump_intermediate_outputs \ |
| 395 | +# --debug_buffer_size=1073741824 |
369 | 396 | # |
370 | 397 | # The key flags are: |
371 | 398 | # |
372 | | -# - ``--output_verification=true``: Compare runtime outputs against the expected |
| 399 | +# - ``--output_verification``: Compare runtime outputs against the expected |
373 | 400 | # outputs stored in the BundledProgram (uses rtol=1e-3, atol=1e-5) |
374 | | -# - ``--dump_intermediate_outputs=true``: Capture intermediate outputs for |
| 401 | +# - ``--dump_intermediate_outputs``: Capture intermediate outputs for |
375 | 402 | # operator-level debugging |
376 | | -# - ``--debug_buffer_size=<bytes>``: Size of debug buffer (default: 256KB, increase |
377 | | -# for larger models) |
| 403 | +# - ``--debug_buffer_size=<bytes>``: Size of debug buffer (1GB in this example) |
378 | 404 | # |
379 | 405 | # Example output on success: |
380 | 406 | # |
|
402 | 428 | # |
403 | 429 | # After running the model with the CMake runner, load the generated artifacts |
404 | 430 | # back into Python for analysis using the Inspector. |
405 | | - |
406 | | -from executorch.devtools import Inspector |
407 | | - |
408 | | -# sphinx_gallery_start_ignore |
409 | | -inspector_patch = patch.object(Inspector, "__init__", return_value=None) |
410 | | -inspector_patch.start() |
411 | | -# sphinx_gallery_end_ignore |
412 | | -etrecord_path = "etrecord.bin" |
413 | | -etdump_path = "etdump.etdp" |
414 | | -debug_buffer_path = "debug_output.bin" |
415 | | - |
416 | | -inspector = Inspector( |
417 | | - etdump_path=etdump_path, |
418 | | - etrecord=etrecord_path, |
419 | | - debug_buffer_path=debug_buffer_path, |
420 | | -) |
421 | | - |
422 | | -# sphinx_gallery_start_ignore |
423 | | -inspector_patch.stop() |
424 | | -# sphinx_gallery_end_ignore |
425 | | - |
426 | | -###################################################################### |
| 431 | +# |
| 432 | +# .. code-block:: python |
| 433 | +# |
| 434 | +# from executorch.devtools import Inspector |
| 435 | +# |
| 436 | +# etrecord_path = "/path/to/etrecord.bin" |
| 437 | +# etdump_path = "/path/to/etdump.etdp" |
| 438 | +# debug_buffer_path = "/path/to/debug_output.bin" |
| 439 | +# |
| 440 | +# inspector = Inspector( |
| 441 | +# etdump_path=etdump_path, |
| 442 | +# etrecord=etrecord_path, |
| 443 | +# debug_buffer_path=debug_buffer_path, |
| 444 | +# ) |
| 445 | +# |
427 | 446 | # Then use the same analysis techniques as in Pipeline 1: |
428 | 447 | # |
429 | 448 | # .. code-block:: python |
|
0 commit comments