(args)
| 34 | |
| 35 | |
| 36 | def main(args): |
| 37 | qnn_config = QnnConfig.load_config(args.config_file if args.config_file else args) |
| 38 | |
| 39 | # ensure the working directory exist. |
| 40 | os.makedirs(args.artifact, exist_ok=True) |
| 41 | |
| 42 | data_num = 100 |
| 43 | if args.ci: |
| 44 | inputs = [(torch.rand(1, 3, 224, 224),)] |
| 45 | logging.warning( |
| 46 | "This option is for CI to verify the export flow. It uses random input and will result in poor accuracy." |
| 47 | ) |
| 48 | else: |
| 49 | inputs, targets = get_imagenet_dataset( |
| 50 | dataset_path=f"{args.dataset}", |
| 51 | data_size=data_num, |
| 52 | image_shape=(256, 256), |
| 53 | crop_size=224, |
| 54 | ) |
| 55 | |
| 56 | pte_filename = "convnext_small_qnn" |
| 57 | instance = torchvision.models.convnext_small(weights="IMAGENET1K_V1").eval() |
| 58 | qnn_quantizer = { |
| 59 | QnnExecuTorchBackendType.kGpuBackend: None, |
| 60 | QnnExecuTorchBackendType.kHtpBackend: make_quantizer( |
| 61 | quant_dtype=QuantDtype.use_8a8w, |
| 62 | per_channel_linear=True, |
| 63 | backend=qnn_config.backend, |
| 64 | soc_model=qnn_config.soc_model, |
| 65 | ), |
| 66 | }[qnn_config.backend] |
| 67 | build_executorch_binary( |
| 68 | model=instance, |
| 69 | qnn_config=qnn_config, |
| 70 | file_name=f"{args.artifact}/{pte_filename}", |
| 71 | dataset=inputs, |
| 72 | custom_quantizer=qnn_quantizer, |
| 73 | ) |
| 74 | |
| 75 | adb = SimpleADB( |
| 76 | qnn_config=qnn_config, |
| 77 | pte_path=f"{args.artifact}/{pte_filename}.pte", |
| 78 | workspace=f"/data/local/tmp/executorch/{pte_filename}", |
| 79 | ) |
| 80 | adb.push(inputs=inputs) |
| 81 | adb.execute() |
| 82 | |
| 83 | # collect output data |
| 84 | output_data_folder = f"{args.artifact}/outputs" |
| 85 | make_output_dir(output_data_folder) |
| 86 | |
| 87 | adb.pull(host_output_path=args.artifact) |
| 88 | |
| 89 | # top-k analysis |
| 90 | predictions = [] |
| 91 | for i in range(data_num): |
| 92 | predictions.append( |
| 93 | np.fromfile( |
no test coverage detected