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