(args)
| 34 | |
| 35 | |
| 36 | def main(args): |
| 37 | qnn_config = QnnConfig.load_config(args.config_file if args.config_file else args) |
| 38 | |
| 39 | os.makedirs(args.artifact, exist_ok=True) |
| 40 | data_size = 100 |
| 41 | model_name = "google-bert/bert-base-uncased" |
| 42 | tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 43 | module = AutoModelForMaskedLM.from_pretrained(model_name).eval() |
| 44 | pte_filename = "bert_qnn_q16" |
| 45 | |
| 46 | if args.ci: |
| 47 | random_ids = torch.randint(low=0, high=100, size=(1, 100), dtype=torch.int32) |
| 48 | attention_mask = create_bidirectional_mask( |
| 49 | config=module.config, |
| 50 | input_embeds=module.bert.embeddings(random_ids), |
| 51 | attention_mask=torch.zeros((1, 100), dtype=torch.float32), |
| 52 | ) |
| 53 | inputs = [ |
| 54 | ( |
| 55 | random_ids, |
| 56 | attention_mask, |
| 57 | ) |
| 58 | ] |
| 59 | logging.warning( |
| 60 | "This option is for CI to verify the export flow. It uses random input and will result in poor accuracy." |
| 61 | ) |
| 62 | else: |
| 63 | inputs, targets = get_masked_language_model_dataset( |
| 64 | args.dataset, tokenizer, data_size |
| 65 | ) |
| 66 | inputs = [ |
| 67 | ( |
| 68 | input_ids, |
| 69 | create_bidirectional_mask( |
| 70 | config=module.config, |
| 71 | input_embeds=module.bert.embeddings(input_ids), |
| 72 | attention_mask=attention_mask, |
| 73 | ), |
| 74 | ) |
| 75 | for input_ids, attention_mask in inputs |
| 76 | ] |
| 77 | |
| 78 | # lower to QNN |
| 79 | quantizer = { |
| 80 | QnnExecuTorchBackendType.kGpuBackend: None, |
| 81 | QnnExecuTorchBackendType.kHtpBackend: make_quantizer( |
| 82 | quant_dtype=QuantDtype.use_16a8w, |
| 83 | eps=2**-20, |
| 84 | backend=qnn_config.backend, |
| 85 | soc_model=qnn_config.soc_model, |
| 86 | ), |
| 87 | }[qnn_config.backend] |
| 88 | build_executorch_binary( |
| 89 | model=module, |
| 90 | qnn_config=qnn_config, |
| 91 | file_name=f"{args.artifact}/{pte_filename}", |
| 92 | dataset=inputs, |
| 93 | custom_quantizer=quantizer, |
no test coverage detected