Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hessian Trace Estimate Calculation exits prematurely due to negative value of diff_avg #2155

Closed
karam-nus opened this issue Sep 25, 2023 · 1 comment · Fixed by #2159
Closed
Assignees

Comments

@karam-nus
Copy link

karam-nus commented Sep 25, 2023

Trying out automatic mixed precision QAT using mobilenetv2 example.

nncf/torch/quantization/hessian_trace.py#L144C3-L144C3

            diff_avg = abs(mean_avg_total_trace - avg_total_trace) / (avg_total_trace + self._diff_eps)
            if diff_avg < tolerance:
                return mean_avg_traces_per_param

Suppose the diff_avg comes out to be negative in the initial stages of trace estimation, then the traces estimated will not be correct as the algorithm didn't run for expected number of iterations for convergence using Hutchinson Method.

E.g.
Tolerance = 1e-06
diff_avg values w.r.t. iteration number
"diff_avg": {
"1": 26366.833984375,
"2": 0.1541769653558731,
"3": 0.28552359342575073,
"4": 0.783305823802948,
"5": 4.321332931518555,
"6": -0.36715415120124817
},

A correct approach is to use absolute value of avg_total_trace in denominator, as done in PyHessian:

if abs(np.mean(trace_vhv) - trace) / (abs(trace) + 1e-6) < tol:

@ljaljushkin
Copy link
Contributor

Hello, @karamjotsinghmalik! Good catch! Thanks for letting us know!

@ljaljushkin ljaljushkin self-assigned this Sep 25, 2023
@karam-nus karam-nus changed the title Hessian Trace Estimation Calculation exits prematurely due to negative value of diff_avg Hessian Trace Estimate Calculation exits prematurely due to negative value of diff_avg Sep 25, 2023
vshampor pushed a commit that referenced this issue Sep 26, 2023
### Changes

Use absolute value of avg_total_trace in denominator, as done in [PyHessian:](https://github.com/amirgholami/PyHessian/blob/master/pyhessian/hessian.py#L186C22-L186C22)

### Reason for changes

more robust mixed precision algo

### Related tickets

resolves #2155 

### Tests

hawq-related tests
daniil-lyakhov pushed a commit to daniil-lyakhov/nncf that referenced this issue Sep 27, 2023
### Changes

Use absolute value of avg_total_trace in denominator, as done in [PyHessian:](https://github.com/amirgholami/PyHessian/blob/master/pyhessian/hessian.py#L186C22-L186C22)

### Reason for changes

more robust mixed precision algo

### Related tickets

resolves openvinotoolkit#2155 

### Tests

hawq-related tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants