BERT demo needs a separate engine building path to generate calibration cache. This is because we need to configure SLN and MHA plugins in FP32 mode when generating calibration cache, and INT8 mode when building the actual engine. This cache could be generated by examining certain t
(sequence_lengths, workspace_size, config, weights_dict, squad_json, vocab_file, calibrationCacheFile, calib_num)
| 472 | return engine |
| 473 | |
| 474 | def generate_calibration_cache(sequence_lengths, workspace_size, config, weights_dict, squad_json, vocab_file, calibrationCacheFile, calib_num): |
| 475 | """ |
| 476 | BERT demo needs a separate engine building path to generate calibration cache. |
| 477 | This is because we need to configure SLN and MHA plugins in FP32 mode when |
| 478 | generating calibration cache, and INT8 mode when building the actual engine. |
| 479 | This cache could be generated by examining certain training data and can be |
| 480 | reused across different configurations. |
| 481 | """ |
| 482 | # dynamic shape not working with calibration, so we need generate a calibration cache first using fulldims network |
| 483 | if not config.use_int8 or os.path.exists(calibrationCacheFile): |
| 484 | return calibrationCacheFile |
| 485 | |
| 486 | # generate calibration cache |
| 487 | saved_use_fp16 = config.use_fp16 |
| 488 | config.use_fp16 = False |
| 489 | config.is_calib_mode = True |
| 490 | |
| 491 | with build_engine([1], workspace_size, sequence_lengths, config, weights_dict, squad_json, vocab_file, calibrationCacheFile, calib_num, False) as engine: |
| 492 | TRT_LOGGER.log(TRT_LOGGER.INFO, "calibration cache generated in {:}".format(calibrationCacheFile)) |
| 493 | |
| 494 | config.use_fp16 = saved_use_fp16 |
| 495 | config.is_calib_mode = False |
| 496 | |
| 497 | def main(): |
| 498 | parser = argparse.ArgumentParser(description="TensorRT BERT Sample", formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
no test coverage detected