Repository navigation
fix(nf12): keep tensor.is_inference() out of the compiled forward (#5299) - #5309
Merged
Merged
Conversation
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.
Fixes #5299. Regression from #5230 (which fixed #5211).
Problem
NF12Linear.dequantize_weight()ended with_as_normal_tensor(weight.to(dtype=dtype).contiguous()), and_as_normal_tensorcalls
tensor.is_inference(). Dynamo cannot trace that call ("This graph breakis fundamental"), so every compiled NF12 CPU forward graph-broke between the
decode and the matmul,
fullgraph=TrueraisedUnsupported, andtest_cpu_kernel_reaches_declared_cpu_level[NF12Linear]reported EAGER while thekernel declares
COMPILE_LEVEL.INDUCTOR.Change
_as_normal_tensorwrapper.pack()andload_quant_result()already normalise the buffers they install (that is theactual [BUG] NF12Linear forward uses an @torch.inference_mode() dequantize_weight() as the matmul operand: every grad-mode forward (fp16 CUDA, CPU, in-session after quantize) raises 'Inference tensors cannot be saved for backward' #5211 fix), and a caller running under
inference_modecannot need agradient through the decode.
post_init()now normalises buffers acheckpoint loader installed directly under
InferenceMode, so the guaranteesurvives without touching
forward.Verification
test_cpu_kernel_reaches_declared_cpu_level[NF12Linear]andtest_nf12_cpu_compiles_after_post_init_without_an_eager_call: red onmain(
Unsupported: Encountered tensor.is_inference()), green here.torch.compile(forward, backend="inductor", fullgraph=True)now compiles on a2048x2048 bf16 CPU layer (was
torch._dynamo.exc.Unsupported).test_nf12_codec,test_nf12_escape_bounds,test_gptqmodel_nf12_api,test_nf12_moe_offsets,test_validate_nf12,test_nf12_kernel_manifest,test_checkpoint_resume_nf12_macaq_4792), plus the compile-level file and thegrad-mode contract (60 passed).
New
tests/test_nf12_forward_no_inference_probe_5299.py(3 nodes) monkeypatchestorch.Tensor.is_inferenceto raise: the eager forward anddequantize_weight()must not call it, andpost_init()must normalise buffersinstalled under
InferenceMode. All three fail onmain.Note on timing: at M=4 on this host the compiled-vs-eager ratio is
decode-dominated and within noise, so the evidence here is the graph-level
probe (
INDUCTORreached,fullgraphcompiles) rather than a wall-clock delta.