MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / generate_calibration_cache

Function generate_calibration_cache

demo/BERT/builder.py:474–495  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

472 return engine
473
474def 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
497def main():
498 parser = argparse.ArgumentParser(description="TensorRT BERT Sample", formatter_class=argparse.ArgumentDefaultsHelpFormatter)

Callers 1

mainFunction · 0.85

Calls 2

build_engineFunction · 0.70
logMethod · 0.45

Tested by

no test coverage detected