• <xmp id="om0om">
  • <table id="om0om"><noscript id="om0om"></noscript></table>
  • NVIDIA TensorRT? ?? ??? ?? ??? ???? INT8 ??? ?? FP32 ??? ??

    Reading Time: 10 minutes

    ? ??? ??? ?? ? ???? ???? ??? ???? ????. ??? ????? ??? ??? ?? ?? ??, ?? ? ???, ?? ?? ??????? ??? ??, ?? ? ??? ?????. ??? ??????? ????? ????? ???.

    ???? ??? ? ?? ?? ??? ???? ?? ?? ??? 32?? ??? ?????. ??? ???? ??? ??? ??? ?? ??? ?? ??? ???? ? ? ? ??? ???? ??? ??? ?? ???? ??? ??? ??? ?? ? ????.

    ?? ?? ??? ??? ??? ?? 8?? ?? ??? ? ????. ??? ?? ? ???? ?????? ??, ?? ???? ?? ??? ?? ?? ???? ??? ? ??? ????. ? ???? QAT(??? ?? ??)? ?? ??? ??? ?? ?? ?? ???? ???? NVIDIA TensorRT 8.0?? ??? ???? ??? ?????.

    ??

    ?? ???? ????? 8?? ??? ???? ???? ????? ??? ??? ?? ???? ?? ??? ???? ???? ???? ???? ?? ?? ? ?? ??? ?????. ???? ??? ?? ?? ?? ??? ????.

    • 8?? ?? ???? ??? ? NVIDIA GPU? ? ??? ??? 8?? Tensor ??? ???? ??? ? ?? ?? ?? ??? ?????. ??? ?? ? ?? ??? ???? ???? ?? ??? ?? ???? ?? ??????.
    • ???? ????? ??? ??(NVIDIA GPU? ?????? ????)? ????? ??? ???? ???? ?? ?????.??? ? ???? ???? ???? 32?? ???? 8?? ??? ??? ???? 4? ???? ??? ???? ???? ?? ?????.
    • ?? ??? ??? ??????(??? ??). ?, ? ??? ???? ?? ???? ? ???? ??? ????? ?? ??? ??? ?? ???? ??? ????. ??? ??? ???? ??? ??? ?? ???? ?? ? ??? ????.
    • ??? ???? ???? ??? ???? ??? ??, ???? ????? ? ??, ?? ???? ? ???? ?? ??? ????.

    ??? ??

    ???? ?? ??? ????? ????? ???? ???? ???? ??? ?? ???? ?? ?? ? ????. 32?? ?? ???? [-3.4e38, 3.40e38] ???? ? 40? ?? ??? ????? ?? ??????. ? ?? ??? ?? ??? ?? ????? ???. ??? ? ?? ?? ?? ?? ??? ??? ??? ??????.

    ?? ??? ??? ?? ???? ????? ????, ?? ??? ?? ??? ??? ? ??? [-1,1] ??? ????. ?, [-1, 1] ??? ?? ??? ??? [1, 2]? ???? ? ?? ???? ????. [-1, 1]? ?? ??? 32?? ?? ??? ??? ???? ????? ???? ?? ??? ???? 0 ??? ?? ? ?? ??? ?????.

    ??? 8?? ?? ??? ???? 28?? ?? ??? ? ????. ??? 256?? ?? ?? ?? 0? ???? ? ?? ???? ?? ????? ????? ??? ? ????. ?? ?? ? ?? ???? ? ?????? ???? ?? ?? ???? ?? ?? ?????? ??? ???? ????? ??? ??? ????? ??????.

    ?? ??? ??(x_{f})? ??? 8?? ??(x_{q})?? ???? ??, ??? ??? ?? ??? Tensor? ?? ??? [-128, 127]? ???? ? ?????.

    ?? ??? ??? ????? ?? ?? ??????. Round? ???? ??? ????? ??? ??? ???? ???? Clip? [-128, 127] ?? ?? ???? ???? ?????. TensorRT? ??? ???? ?? ???? ?? ???? ?? ???? ?????.

    ?? 1? ???? ?? ??? ??????? ??? ?? ?? ??? ?? x_{f}? ?????? ????. NVIDIA? ???? ??(-amax, amax])? ??? ??? ?? ??? ??????. ??? amax? ???? ? ?? ? ?? ??? ?? ?????. ??? ??? ????? ?? ??? ?? ??? 256?? ??? ???? ????.

    amax = max(abs(x_{f}))
    scale = (2 * amax) / 256

    ??? ???? ?? ??? ??? ??? ??? ?? 8 ?? ??? ??? ? ?? ?? ?? [-128, 127]? ?????. TensorRT ??? ???(Q/DQ) ????? ???? ???? ???? ? ? ??? ?????.

    ?? 8??? ???? ????? ??? ?? ??? ??? ???? ??? ?? ???? ??? ????. ?? ??? ?? ?? ?? ??? ???? ? ?? ?? ???? ??? ?????, ?? ? ?? ???? ???? ? ? ??? ??? ????? ????? ???.

    ? ?? ?? ??? ???? ??? ??? ????? ??? ??? ?????. ?? ?? ?? ?? ?? ??? ?? ?? ??? ??/?? ??? ??????.

    ?? 1. ?? ??? ?? x_{f}? ??? ?? 8?? ?? ???. x_{f}? [-amax, amax] ?? ?? ??? ???? ?? [-128, 127]? ?????.

    ??? ??? ?? ???? ??? ??? ???? ?? ??? ??? ??? ??????. ??? ??? ? ?? ?? ? ??? ?? ? ???(PTQ) ?? ??? ?? ??(QAT) ? ??? ??? ??? ??? ? ????.

    ???? ? ? ??? ??? ??? ???? ? PTQ? ?????. PTQ? ???? ???? ?? ???? ? ????. ??? ??? ???? ? ?? ? ??? ??? ? ????. ??? ??? ?? ?? ???? ???? ???? ?? ??? ???? ???? ? ?????.

    ?? ?? ??? ?? ??? ??? ??? ?? ?? ???? ???? ?? ?????? ???? ???? ????? ??? ??? ?? ??? ?????. ??? ????? ??? ??? ??? ??? ???? ?? ??? ?? ? ??? ???? ?????. ? ????? ???? ???? ???? ?????? ?? ????????.

    PTQ? ???? ?? ???? ???? ??? ??? ????. ?? QAT ??? ??? ? ????. QAT? ????? ?????. ?? ??? ??? ??? ??? ?? ???? ??? ???? ??? ? ????. ????? ???? ??? ? ???? ??? ? ??? ????.

    ???? ?? ??? ???? ??? ?? ??? ??? ???? ??? QAT? ???? ??? ???? ????. ?? ???? ??? ? ????? ???? ??????? ?? ?? ???? ?? ??? ??? ???? ?? ??? ??? ??? ????? ?? ??? ?????. ??? ??? ???? ????? ??? ‘??’?? ????, ?? ???? ??? ???? ??? ???? ?? ??? ???? ????? ???. ? ??? ? ?? ??????? ?? ?? ??? ?? ??? ???? ????.

    ??? ????? ?? ??? ???? ???? ??? ????? ??? ?? ???? ???? ???? ???? ??? ??? ?????.??? ????? ???? ?????? ???? ?? ??? ???? ???????. ???? ?? ??? ???? ?? ?? ??? 0? ??? ?????? ???? ??, ?? ??? ???? ?? ?????? ?? ??? ???? STE(straight-through estimator)? ?????. QAT ????? ???? ?? ??? ???? ??? ??? ???? ???? ???? ????? ? ???? ??? ???? ?????.

    ?? 2. ?? ??? ??(??) ? ??? ??? QAT ?? ??? ???(???)

    PTQ? ???? ?? ?????? ???? ???? ?? ?? ??????. ??? QAT? ?? ?? ? ?? ???? ???? ??? ?? ???? ???? ??????.

    TensorRT? ???

    TensorRT 8.0? 2?? ?? ?? ?? ??? ???? INT8 ??? ?????. ? ?? ?? ??? TensorRT ?? ?? ?? API? ???? INT8 ???(??? ?? 8?? ??) ???? ???? ????? ???? ?? ?? ??? ??????.

    ?? 3. TensorRT PTQ ?????(??)? ??? ?? ?? ???? ??? ??? ???? ???? TensorRT INT8 ???(???)

    ? ??? TensorRT? ?? PTQ ?? ???? ???? TensorRT? ?? ??? ?? ?? ??? ??? ? ?????(?? 3). ?? TensorRT INT8 ?? ??? QuantizeLayer/DequantizeLayer ??? ???? ?? ??? ONNX ????? ??? ? ???? ??? ??? ??? ????. ???? ?? ??? ??? TensorRT ??? ????? ??? ??? ? PTQ ??? ??????.

    TensorRT ??? ??

    PyTorch? ?? TensorRT ??? ??? ??? ??? QAT ??? ???? ? ??? ?? ??? PyTorch ?????? ???? TensorRT? ?????. ? ??? QAT ?? PTQ? ??? ???? ?? ???? ???? API? ?????.

    API? ??? TensorQuantizer ???, ???? ??????, ?? ??????, ??? ??? ? ????. ??? ????? ??? ???? QuantDescriptor? ?? ?????. TensorQuantizer ?? ???? ??? ?? PyTorch? ???? ??? ??? ????? ??? ???? ?????. TensorQuantizer? ???? ??? ??? ??? ?? ??? ?? ?????? ???? ??? ?????.

    ? API? PyTorch ??? ???? ???? ?? ????? ?????. ?? ??? ????? ?? ?? ?? ???? ???? ??? API? ???? ???? ??? ?? ????. ?? ?? ?? ??? ???? ? ??? ? ??? ????? ?? ??? ?? ?? ???? ?????.

    4. TensorRT ??? ?? ?? ??

    QAT? TensorRT ???? NVIDIA ??? ??? ??? ???? ???, ???? ??? ?? ??? ?? QAT ? PTQ? ??? ?? ??? ??? ??? ?? ?? ??? ??? ?????.

    ?? ?? ??

    ? ????? ??? ??? ?? ?? ??? ??? ?????.

    QAT? ?? ?? ???? ?? ??? ??? ?? ?? ???? ???? ?? ??? ? ?? ???? ???? ?? ?? ??? ????? ??? ??t?? ??? ?? ??? ???? ??? ?? ?????. ? ?? ?? ??? ResNet50 ??? ?????. bash ??? ??? ???? ?? ??? ??:

    python3 classification_flow.py --data-dir [path to ImageNet DS] --out-dir . --num-finetune-epochs 1 --evaluate-onnx --pretrained --calibrator=histogram --model resnet50_res

    –data-dir ??? ??? ?????? ?? ImageNet(ILSVRC2012) ?????? ?????. –calibrator=histogram ??? ??? ?? ???? ?? ????? ???? ???? ??? ???? ??? ?????. ??? ?? ? ?? ?? ?? ??? ????? ????.

    ResNet50 ??? ?? Facebook? Torchvision ????? ?????? ? ?? ??? ?? ??(Skip-Connection ???)? ???? ???? ???? ??? ??(resnet50_res)? ???? ????. ??? ??? Q/DQ ??? ?? ?? ??? ??????.

    ??? ??? ?? ??? ?????. ??? ??? ResNet50 ???? ??????.

    # Prepare the pretrained model and data loaders
     model, data_loader_train, data_loader_test, data_loader_onnx = prepare_model(
             args.model_name,
             args.data_dir,
             not args.disable_pcq,
             args.batch_size_train,
             args.batch_size_test,
             args.batch_size_onnx,
             args.calibrator,
             args.pretrained,
             args.ckpt_path,
             args.ckpt_url) 

    ?? prepare_model? ??? ??? ??? ??? ?? ???????? ??? ???? ?????. ?? ?? ??? ????.

    # Initialize quantization
     if per_channel_quantization:
             quant_desc_input = QuantDescriptor(calib_method=calibrator)
     else:
             quant_desc_input = QuantDescriptor(calib_method=calibrator, axis=None)
     quant_nn.QuantConv2d.set_default_quant_desc_input(quant_desc_input)
     quant_nn.QuantConvTranspose2d.set_default_quant_desc_input(quant_desc_input)
     quant_nn.QuantLinear.set_default_quant_desc_input(quant_desc_input)
     quant_desc_weight = QuantDescriptor(calib_method=calibrator, axis=None)
     quant_nn.QuantConv2d.set_default_quant_desc_weight(quant_desc_weight)
     quant_nn.QuantConvTranspose2d.set_default_quant_desc_weight(quant_desc_weight)
     quant_nn.QuantLinear.set_default_quant_desc_weight(quant_desc_weight) 

    QuantDescriptor ????? ???? ?? ??? ? ?? ?????? ?? ?? ? ??? ??? ?????. ? ???? ??(?: quant_nn.QuantConv2d)?? ?? ?? ?? ??? ??? ???? ??? QuantDescriptor?? ??? ??? ? ???? ?????.

    ?? ?? ?? ???? ?? ??? ??? ?????. ?? ??(quant_modules.initialize)? ?? ??? PyTorch ??? ???? ???? ?? torch.nn.module ?? ???? ???? ????? ???? ??? ??? ?????? ?? ?? ??(quant_modules.deactivate)? ???? ???. ?? ?? torch.nn.conv2d? 2D ???? ???? ?? ?? ???? ???? pytorch_quantization.nn.QuantConv2d? ?????. ?? ????? ?? ??? quant_modules.initialize? ???? ???.

    quant_modules.initialize()
    model = torchvision.models.__dict__[model_name](pretrained=pretrained)
    quant_modules.deactivate()

    ????, ?? ???? ?? ??(collect_stats)? ?????. ?, ??? ?? ?? ?? ???? ???? ? ??? ???? ? ??? ????? ??? ??? ?? ??? ?????. ????? ???? ??? ? ?? ??? ?? ????(compute_amax)? ???? ???(calibrate_model)? ?????.

    ?? ??? ?? ???? ?? ?? ??? ?????? ? ??? ??? ??? ??????. ?? ? ?? ??? ???? ????.

    • pytorch_quantization.calib.histogram – ???? ???(KLD), ?? ?? ?? ???(MSE) ?? ???? ??? ??? ?????(??? ??? ??? ???? ?? ?? ??? ?????).
    • pytorch_quantization.calib.max – ?? ??? ?? ???? ?????(?? ??? ???? ?? ?? ??? ?????).

    ? ?? ?? ??? ??? ????? ?????? ?? ???? ??????. ? ??? ???? 4??? ?? ?? ?? ??? ??? ?? ???? ?? ??? ?? ??? ??? ??? ? ????. ? ??? ?? ?? ????? ?? ??? ? ????. ??? ??? ResNet50 ?? ??? ??????.

    ??? ???? ????? ?? QAT? ?? ???? ??? ???. ONNX? ???? ??? ? ????. ??? PTQ ???? ? ????. TensorRT?? ??? ???? ?? Q/DQ ???? ?? ONNX ??? ???? ??? ?? ??? ??????. Q/DQ ONNX ??? ???? PTQ ????????.

    QAT ??? ?? ????? ??? ?? ???? ??? ??????. QAT? ???? ?? ?? ??? ? 10%? ??? ?? ?? ???? ?? ????, ????? ONNX? ??? ? ????. ??? ??? ? ?? ??? ?? ?? ???: ?? ? ??? ?? ??? ??????.

    ONNX? ??? ? ??? ??? ? ? ?? ??? ????.

    • ONNX opset 13? ??? ???(PCQ)? ??????? ?? PCQ? ???? ?? ?? ?? opset ??? ??????.
    • do_constant_folding ??? ?? ?? ? ?? ??? ???? ?? True? ????? ???.
    torch.onnx.export(model, dummy_input, onnx_filename, verbose=False, opset_version=opset_version, do_constant_folding=True)

    ??? ??? ONNX? ????? ?? ??? ??? ONNX? ???? ? ?? ??? ONNX ???? QuantizeLinear ? DequantizeLinear(?? 5 Q ? DQ? ??)? ?????.

    ?? 5. PyTorch ??? ONNX? ??? ? ?? ??? ???? Q/DQ ONNX ???? ????

    QAT ?? ??

    ????? TensorRT? Q/DQ ???? ?? ONNX ??? TensorRT? ?? ONNX ??? ???? ??? ???? ?????.

    1. TensorRT? Q/DQ ??? ??? ONNX ??? ?????.
    2. Q/DQ ?? ?? ??? ??? ?????.
    3. ???? ?? ??? ??? ?????.
    4. ?? ??? ?? ???? ?? ?? ??? ?????. ? ?? ???? ???? ?? ? ???? ???? ????.

    TensorRT?? Q/DQ ????? ????? INT8? ????? ? ?? ??? ?? ??? ???? ????. ?????? Q/DQ ??? ??? ? ???? ????? ?????. TensorRT ?? ?????? trtexec? ???? Q/DQ ????? ???? ?? ??? ??? ????.

    $ trtexec -int8 <onnx ??>

    TensorRT? ??? ????? ?? ?? ??? ???? Q/DQ ????? ?????, ?? ???? ??? ???? ?? ???? ?? ???? ?? ?? ??? ? ??? ?? ??? ?? ??? ???. ??-???? ?? ??? ?? ???? ??? ?? ?????. ????? Q/DQ ??? ??? ??? ???? ?? ??? ???? ?? ???? ?? ONNX ??? ?? ???? ???? ??? ????.

    TensorRT Q/DQ ??? ?? TensorRT INT8 ??? ?? ? ? ???? ? ??? ???. ?? TensorRT?? INT8 ???? Tensor? ?? ?? API ?? ?? ????? ?? ??? ???? ?????. TensorRT? ??? ???? ??? ? ??? ?? ??? ??? ???? INT8? ? ?? ??? ???? ?? ?? ??? ??????. ??? INT8?? ? ??? ???? ?? INT8? ????? ?????. ??? ??? FP32 ?? FP16 ? ? ?? ?? ?????. ? ???? TensorRT? ?? ???? ?? ????? ??? ?? ??? ?????? ??? ? ????.

    ??? ??? ????? Q/DQ ??? ??? ??? ???? ?? ??? ?????. ? ??? ??? ?????? ???? ?? ?? ??? ??? ? ????. ??? ??? ?? ???? ????? ??(?: INT8 ??? ?? FP16 ?? ??), ??? ??? ? ??? ???? ?? ??? ???? ??(?: INT8? Tensor ??? ?? ????? ?? V100?? FP16?? INT8? ???? ??)?? ???????.

    ??? ?????? ??? ??? ??? ??? ? ??? ???? ??? ? ????. TensorRT? ??? ??? ?? ?????? ?? ??? ?? ???? ???? ?? ?? ????. Q/DQ ?????? ?? ?? API? ???? ?? ???? ????.

    ??? ??? ??? ??? ?? ? ??? ?????.

    • ??, ??? ??? ??? INT8 ???? ????? Q/DQ ?? ??? ???? ???? ?????. Q/DQ ??? Q/DQ ??? ?????? ???????? ??? ???? ??? ?????. ?? ?? QuantizeLayer? ??? ReLU ??? ???? ?? ???? ???? ??????? ? ????. ??? ?? ReLU ??? ?? ? ?? ???? INT8 ???? ???? ??? ?? ??? 4? ?????.
    • ?? ?? ??? ??? ???? ???? INT8 ???? ???? INT8 ?? ?????? ???? ???? ??? ?????. ?? ?? QuantizeLayer? ConvolutionLayer? ??? ? ????.
    • ????? TensorRT ?? ??? ??? ??? ???? ??? ?? ? ?? ???? ???? ? ??? ?? ?? ??? ?????.

    TensorRT? ???? ?? ??? ??? ???? ?? ??? ??? TensorRT ??? ???? ??????.

    TensorRT Q/DQ ???? ??? ?? ???? ?? ???? ???? ??? ? ??? ???? ??? ??? ??? ?? ????. EfficientNet? ???? ???? ?? QAT? ??? ???? ? ?????. ?? ??? PTQ? QAT? ?????.

    ?? 6. PTQ ? QAT??? EfficientNet-B0? ?? ??? ??

    ??? ??? NVIDIA DeepLearningExamples? EfficientNet ??? ??? ??????.

    ??

    ? ?????? ?? ??? ??? TensorRT? ??? ??? ???? ??? ?? TensorRT 8.0? Q/DQ ????? ???? ??? ??????. ??? ??? ?? ???? ResNet50 QAT ??? ??? ??????.

    ResNet50? PTQ? ???? ???? ? ??? QAT? ???? ????. ??? EfficientNet? ???? ???? ?? QAT? ?????. EfficientNet B0 ????? ?? ??? Top1 ???? 77.4??? PTQ Top1 ???? 33.9?? QAT Top1 ???? 76.8???.

    ??? ??? GTC 2021 ??, TensorRT 8.0? ??? PyTorch? ??? ?? ??.? ??????.

    Discuss (0)
    +1

    Tags

    ?? ???

    人人超碰97caoporen国产