Optimize FP8 layer conversion by skipping weight initialization#1295
Closed
Optimize FP8 layer conversion by skipping weight initialization#1295
Conversation
Co-authored-by: yiliu30 <106061964+yiliu30@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Speed up FP8 linear convert method
Optimize FP8 layer conversion by skipping weight initialization
Jan 16, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the FP8-to-Linear layer conversion process by eliminating redundant weight initialization. Since weights are immediately overwritten with dequantized values after layer creation, the initial weight initialization is wasteful and adds unnecessary overhead during model conversion.
Changes:
- Added import of
no_init_weightscontext manager (aliased asskip_weights_initialize) from transformers - Wrapped
torch.nn.Linearinstantiation inskip_weights_initialize()context to bypass initial weight allocation
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -1002,7 +1003,8 @@ def convert_fp8_layer_to_linear(layer, dtype=torch.bfloat16, device: str = "cpu" | |||
| """ """ | |||
There was a problem hiding this comment.
Empty docstring should be removed or replaced with meaningful documentation explaining the function's purpose, parameters, and return value.
Suggested change
| """ """ | |
| """ | |
| Convert an FP8-quantized linear-like layer to a standard torch.nn.Linear layer | |
| in a higher-precision dtype by dequantizing its weights and copying metadata. | |
| This helper is intended for layers produced by AutoRound quantization, such as | |
| regular FP8 linear layers or `CompressedLinear` layers with an attached | |
| compressor. It reconstructs a dense Linear layer with dequantized weights and | |
| preserves relevant attributes from the original layer (e.g. QuantizationScheme | |
| fields, temporary names, and scale dtype). | |
| Args: | |
| layer: The source FP8-quantized layer instance to convert. It is expected | |
| to have `in_features`, `out_features`, an optional `bias`, and either | |
| a `compressor.decompress_module` method (for `CompressedLinear`) or | |
| FP8 weight/scale attributes (`weight`, `weight_scale` or | |
| `weight_scale_inv`, and `block_size`). | |
| dtype: The target floating-point dtype for the new Linear layer weights | |
| and bias. Defaults to torch.bfloat16. | |
| device (str): Device on which to place the source layer before | |
| dequantization (e.g. "cpu", "cuda"). | |
| Returns: | |
| torch.nn.Linear: A new Linear layer with dequantized weights in the given | |
| dtype and copied bias and quantization-related attributes. | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Skip unnecessary weight initialization during FP8-to-Linear layer conversion. Weights are immediately overwritten with dequantized values, making initialization wasteful.
Changes
no_init_weightscontext manager fromtransformers.modeling_utilstorch.nn.Linearinstantiation inskip_weights_initialize()contextReduces overhead in FP8 model conversion pipelines where this function is called repeatedly per layer.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.