add utility to compute analytical bits per weight - #67
Conversation
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
u-simha
left a comment
There was a problem hiding this comment.
Have left some comments; my main suggestion would be to have a documentation page (even if it is brief) rather than the doc string at the top of the file
| # Use of this source code is governed by a BSD-3-Clause license that can | ||
| # be found in the LICENSE file or at https://opensource.org/licenses/BSD-3-Clause | ||
|
|
||
| """Compute the average bits-per-weight (bpw) of a prepared ``coreai-opt`` model. |
There was a problem hiding this comment.
My suggestion would be to have a doc page explaining the usage of this tool; doc strings in the headings of the file isn't often read, compared to the documentation.
There was a problem hiding this comment.
@u-simha any suggestions on where to add this in the doc? I'm thinking a new page under OTHER WORKFLOWS AND UTILITIES but if there's other similar utils mentioned elsewhere, it can belong there instead of a new page.
|
|
||
| # Sanity envelope for the qparam / LUT / bias overhead a sane config adds on top of the | ||
| # nominal bit width, in bpw. See _assert_bpw_is_plausible for why 2 and not 1. | ||
| _MAX_EXPECTED_OVERHEAD_BPW = 2.0 |
There was a problem hiding this comment.
This is quite a large upper bound; didn't find more information in _assert_bpw_is_plausible which explains this?
There was a problem hiding this comment.
Ah so this is to basically accommodate for cases where the scale/LUTs would add non-trivial overhead. Even per-channel scales and minval add quite a bit of overhead. This gets highlighted here due to the models being relatively small. In real models, these should get amortized and the overhead (ideally) won't be this much. But it does depend on the config chosen. Like for a really small block/group size, the scale/LUT overhead will be non-trivial.
For eg, in this test file, the max overhead is 1.637 for the palettized[n8_per_tensor-pcs-bias] case. So I chose 2 as the bound..
There was a problem hiding this comment.
Lmk if/how we should address this. We could only use configs for this smallish model that don't add such a large overhead, but that won't ensure config coverage for the actual BPW logic..
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Co-authored-by: Utkarsh Simha <135899523+u-simha@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
Signed-off-by: Prathamesh Mandke <46148373+pkmandke@users.noreply.github.com>
|
|
||
| def full_precision_bits(tensor: torch.Tensor) -> int: | ||
| """Return the dense storage cost of a tensor in bits.""" | ||
| return int(tensor.numel() * tensor.element_size() * 8) |
There was a problem hiding this comment.
Nit: Can we replace all usages of "8" with defining a _BITS_PER_BYTE = 8 and using that instead?
|
|
||
| Args: | ||
| weight (torch.Tensor): The dense original weight tensor. | ||
| pal (_FakePalettizeImplBase): The weight fake-palettize parametrization. |
There was a problem hiding this comment.
Nit, argument in the docstring is misnamed
Add utility to compute the average bits-per-weight (bpw) of a prepared
coreai-optmodel in eager mode quantization and palettization.Public API
Imported from
coreai_opt.inspection:Usage:
Supported: eager-mode integer weight quantization (int8/int4/int2 and unsigned variants, symmetric or asymmetric, any granularity, sub-byte payloads packed at
n_bits), and palettization at any spec-supportedn_bitsincluding a quantized LUT.Raises
NotImplementedErrorfor: graph-mode /torch.fx.GraphModulemodels, floating-point (FP8/FP4) weight quantization, and parametrizations that store multiple original tensors (weight_norm,spectral_norm).Note: This is an analytical estimate, not a measurement. It is meant for prepared models, not finalized ones.
Testing
tests/inspection/test_bits_per_weight.py: unit tests against hand-derived golden bit counts across quantization dtypes, qschemes, and granularities, plus palettizationn_bitsx granularity combinations. Also covers persistent and non-persistent buffers, tied weights, per-module attribution, and the unsupported-config errors.tests/export/test_bpw_export_size.py: cross-checkstotal_bits / 8against the measured payload of an actualCore AIexport. The prediction must be a lower bound and land within a 2.5 percent structural-metadata budget.tests/export/export_utils.py: addscoreai_export_size_byteshelper.tests/models/simple.py: addsLinearBatchNormModelfixture (buffer coverage) and optionalbiasto two existing fixtures.TODO: