microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.68k stars 2.93k forks source link

Calibration ranges for Histogram-based approaches gives very bad performances on the task (BERT / SQuAD). #10571

Open mfuntowicz opened 2 years ago

mfuntowicz commented 2 years ago

Hi ORT folks πŸ‘‹πŸ».

I continue my walk over all the possible calibration techniques provided by ORT, especially target BERT-like models, and spot something strange w.r.t the calibration ranges generated by different Calibrater.

I'm using the following schema:

Everything is highly inspired by the ORT example on BERT-SQuAD

Every attempt with a calibration method different than minmax leads to very poor exact match and F1 score (like EM < 0.01 and F1 < 0.1)

The baseline I'm using is from HuggingFace's Hub: https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad

when looking at the calibration ranges for percentiles I'm confused by the results (see JSON below).

Am I missing something? I would have expected to have a negative side on each node (as I get with entropy).

Also, histogram-based values are symmetric, the left-side values are always the negative of the right-side. Is it expected? Even in asymmetric mode?

Thanks a lot for your inputs on this πŸ™πŸ»

{
  "916": [
    6.055913925170898,
    6.055913925170898
  ],
  "367": [
    1.7712894678115845,
    1.7712894678115845
  ],
  "438": [
    6.504486560821533,
    6.504486560821533
  ],
  "356": [
    3.519847869873047,
    3.519847869873047
  ],
  "898": [
    9.999778782798785e-13,
    9.999778782798785e-13
  ],
  "226": [
    43.03803253173828,
    43.03803253173828
  ],
  "127": [
    0.011049754917621613,
    0.011049754917621613
  ],
mfuntowicz commented 2 years ago

For the record, it seems manually "enforcing" the symmetry in the calibration ranges restore performances:

calibration_ranges = calibrator.compute_range()

# here v[0] == v[1], let's chnge it to -v[0]
calibration_ranges = {k: (-v[0], v[1]) for k, v in calibrations_ranges.items()}
yufenglee commented 2 years ago

Yes, TRT only supports symmetric quantization.

mfuntowicz commented 2 years ago

@yufenglee I'm not using TRT, I'm using ORT CPU EP

yufenglee commented 2 years ago

OK, I see. Then you are running on machine without VNNI.

mfuntowicz commented 2 years ago

I though about it too, but I'm running on a VNNI machine, Intel i9-10980XE 🀐

>  lscpu                                                                                                                                                                                                                                 ξ‚² βœ” ξ‚² 10070 ξ‚² 00:13:26
Architecture:            x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Address sizes:         46 bits physical, 48 bits virtual
  Byte Order:            Little Endian
CPU(s):                  36
  On-line CPU(s) list:   0-35
Vendor ID:               GenuineIntel
  Model name:            Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz
    CPU family:          6
    Model:               85
    Thread(s) per core:  2
    Core(s) per socket:  18
    Socket(s):           1
    Stepping:            7
    CPU max MHz:         4800.0000
    CPU min MHz:         1200.0000
    BogoMIPS:            6000.00
    Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq
                         dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single ssbd mba ibrs ibpb stibp ibrs_en
                         hanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_ll
                         c cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization features:
  Virtualization:        VT-x
Caches (sum of all):
  L1d:                   576 KiB (18 instances)
  L1i:                   576 KiB (18 instances)
  L2:                    18 MiB (18 instances)
  L3:                    24.8 MiB (1 instance)
NUMA:
  NUMA node(s):          1
  NUMA node0 CPU(s):     0-35
Vulnerabilities:
  Itlb multihit:         KVM: Mitigation: VMX disabled
  L1tf:                  Not affected
  Mds:                   Not affected
  Meltdown:              Not affected
  Spec store bypass:     Mitigation; Speculative Store Bypass disabled via prctl
  Spectre v1:            Mitigation; usercopy/swapgs barriers and __user pointer sanitization
  Spectre v2:            Mitigation; Enhanced IBRS, IBPB conditional, RSB filling
  Srbds:                 Not affected
  Tsx async abort:       Mitigation; TSX disabled
yufenglee commented 2 years ago

If on machine with VNNI, the perf is expected to be similar.

yufenglee commented 2 years ago

I thought the performance to be latency. @chilo-ms.

mfuntowicz commented 2 years ago

Also, I want to point out the calibration ranges generated by ORT seems wrong min = max for entropy/percentiles schema, see attached JSON file in first message.

yufenglee commented 2 years ago

Thanks @mfuntowicz! Yes, it is a bug for non-symmetric activation: https://github.com/microsoft/onnxruntime/blob/be9cc40aa51679daed51c88f9ce8ceecd5e6a800/onnxruntime/python/tools/quantization/calibrate.py#L557.

chilo-ms commented 2 years ago

Thanks @mfuntowicz for bringing out this problem. The range of tensor in percentiles calibration method in ORT only considers symmetric and will fix the bug.