Repository navigation
Compute palettizer indices on original device #148
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
Changes from all commits
e4f887b
2607341
7aec572
9c08d4c
63676ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,9 @@ | |
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Max size of the (rows, num_clusters) fp32 distance matrix built at once in _assign_indices. | ||
| _CDIST_CHUNK_BYTES = 512 * 1024**2 | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class _WeightVectorization: | ||
|
|
@@ -576,18 +579,24 @@ def _assign_indices( | |
| distances = torch.cdist(vectors.float(), centroids.detach().cpu().float()) | ||
| return distances.argmin(dim=-1).flatten().to(torch.uint8) | ||
|
|
||
| blocks, axis = self._scale_reshape_and_block(original_weights.detach().cpu()) | ||
| centroids_cpu = centroids.detach().cpu().float() | ||
| blocks, axis = self._scale_reshape_and_block(original_weights.detach()) | ||
| centroids = centroids.detach().float() | ||
|
|
||
| block_indices = [] | ||
| for block_idx, block_weight in enumerate(blocks): | ||
| vec = self._vectorize_block(block_weight) | ||
| dist = torch.cdist(vec.float(), centroids_cpu[block_idx]) | ||
| clusters = dist.argmin(dim=-1) | ||
| block_indices.append(self._build_block_indices(clusters, block_weight).to(torch.uint8)) | ||
| centroid = centroids[block_idx] | ||
|
NehalBhandari marked this conversation as resolved.
Resolved
|
||
| chunk_rows = max(1, _CDIST_CHUNK_BYTES // (centroid.shape[0] * 4)) | ||
| clusters = torch.cat( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe nit: We now have a lot of this pattern of "split some operation across chunks, then re-concatenate the results". Can we create a helper for this that takes in the blockwise operation as a callable, along with the axis to split across and the chunk size to use?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this out and while it does work, I feel like it introduces a level of abstraction that isn't really needed. The chunking operations are already pretty much just a single for loop, inside of which we can take whatever actions we want. Implementing the helper means extracting each set of separate actions into their own functions or lambda function and defining the generic scaffold helper function which itself contains some logic for slicing, zipping, mapping, and concatting. The resulting changes lead to more lines of code than before and additional functions to be unit tested, and don't look as clear as the original code with single for loop in my opinion. |
||
| [ | ||
| torch.cdist(v.float(), centroid).argmin(dim=-1).to(torch.uint8) | ||
| for v in vec.split(chunk_rows) | ||
| ] | ||
| ) | ||
| block_indices.append(self._build_block_indices(clusters, block_weight)) | ||
|
|
||
| # self.indices is always CPU-resident, matching self.lut. | ||
| return self._combine_block_indices(block_indices, axis, original_weights) | ||
| return self._combine_block_indices(block_indices, axis, original_weights).cpu() | ||
|
|
||
| def _combine_block_indices( | ||
| self, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.