techfreakworm commited on
Commit
02fe943
Β·
unverified Β·
1 Parent(s): b56acd4

feat: quantise the text encoder too, against a now-measured baseline

Browse files

components_to_quantize widens from ["transformer"] to ["transformer",
"text_encoder"].

The old rationale is KEPT, not deleted, because it was right at the time:
transformer-only avoided confounding an int8-vs-bf16 comparison, since a
quality change could not then be attributed to the denoiser rather than to
prompt encoding. What changed is the ground under it. The live Space at sha
a1791c9e supplied a MEASURED transformer-only baseline -- 55.89 GiB peak RSS,
60.0 GB steady -- and a measured baseline turns exactly that confound into a
control.

It is also load-bearing now rather than optional. Disk was never the binding
constraint: the run died with the disk line reporting OK at 82.2 GB against
the cap, killed at ~60.0 GB of RAM against a ceiling measurably above that
(ours) and ~68 GB (inherited).

The arithmetic in the docstring is revised with it rather than left stale
under a changed argument -- packed 53.197 -> 41.237 GB, peak 133.4-137.4 ->
121.4-125.4, ~46.6 GB expected RSS if the 1.13x transient factor holds, marked
as an extrapolation from one data point.

AND THE COMPONENT NAMES ARE NOW GUARDED, because upstream does not guard them.
pipe_quant_config.py:171 is a bare `module_name in components_to_quantize`
membership test with no validation anywhere, so an unrecognised name never
matches and that component quietly loads at full precision. That is the same
shape as the LTX25_TEXT_ENCODER_PRECISION variable wired to nothing, one rung
further out. QUANTIZED_COMPONENTS is checked against model_files.COMPONENTS --
the registry the download allow-list is built from -- and the check runs
BEFORE the diffusers import, because validating our own constant should not
need a heavy optional dependency and a config error should surface as one.

"text_encoder" verified as the real key in model_index.json at revision
871165de, mapping to Gemma4UnifiedForConditionalGeneration, rather than taken
on trust.

Both mutations fail for their own reasons: reverting the list names the value
that arrived against the value expected, and a typo'd name is refused with the
known components listed.

The value test's failure message was also fixed: it interpolated
QUANTIZED_COMPONENTS, so under mutation it printed "reached as ['transformer'],
not the declared ['transformer']" -- a message derived from the thing under
test tracks any change to it and can never contradict it. The expectation is
now written out.

ltx25_studio/pipelines.py CHANGED
@@ -77,6 +77,7 @@ __all__ = [
77
  "startup_report",
78
  "disk_footprint",
79
  "ZEROGPU_DISK_CAP_GB",
 
80
  "peak_rss_gib",
81
  ]
82
 
@@ -250,19 +251,76 @@ def peak_rss_gib() -> float | None:
250
  return peak / 1024**2
251
 
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  def _quantization_config(quantization: str):
254
- """The transformer's quantisation config, or ``None`` for the bf16 default.
255
-
256
- **Transformer only, deliberately.** It is the largest single component
257
- (37.98 GB of 72.19) and quantising it alone takes the ZeroGPU packing peak
258
- from 152.4-156.4 GB to 133.4-137.4 against :data:`ZEROGPU_DISK_CAP_GB` -- enough, with
259
- 12.6 GB to spare. Widening to the text encoder buys headroom we do not need
260
- and costs something we do: **it would make an int8-vs-bf16 comparison
261
- unable to say whether a quality change came from the denoiser or from
262
- prompt encoding.**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  """
264
  if quantization != "int8":
265
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  try:
267
  from diffusers import PipelineQuantizationConfig
268
  except ImportError as exc:
@@ -290,7 +348,7 @@ def _quantization_config(quantization: str):
290
  # Bound B, now expressed as an ARGUMENT rather than as an intent a
291
  # source-inspection test had to assert. Widening is one list entry and
292
  # the kwargs test sees it.
293
- components_to_quantize=["transformer"],
294
  )
295
 
296
 
 
77
  "startup_report",
78
  "disk_footprint",
79
  "ZEROGPU_DISK_CAP_GB",
80
+ "QUANTIZED_COMPONENTS",
81
  "peak_rss_gib",
82
  ]
83
 
 
251
  return peak / 1024**2
252
 
253
 
254
+ #: Which pipeline components load in int8. **Names must match
255
+ #: ``model_index.json``'s own keys** -- verified at revision ``871165de...``,
256
+ #: where ``text_encoder`` maps to ``Gemma4UnifiedForConditionalGeneration``.
257
+ #:
258
+ #: WARNING: a typo here fails SILENTLY. ``PipelineQuantizationConfig`` does not
259
+ #: validate these names -- ``pipe_quant_config.py:171`` is a bare
260
+ #: ``module_name in components_to_quantize`` membership test, so an unrecognised
261
+ #: entry never matches and that component quietly loads at full precision. That
262
+ #: is the advertised-lever-attached-to-nothing shape again, which is why
263
+ #: :func:`_quantization_config` checks them against ``model_files.COMPONENTS``.
264
+ QUANTIZED_COMPONENTS = ("transformer", "text_encoder")
265
+
266
+
267
  def _quantization_config(quantization: str):
268
+ """The quantisation config, or ``None`` for the bf16 default.
269
+
270
+ **Transformer only, originally β€” and that reasoning was right at the time.**
271
+ The transformer is the largest single component (37.98 GB of 72.19), and
272
+ quantising it alone took the packing peak from 152.4-156.4 GB to
273
+ 133.4-137.4 against :data:`ZEROGPU_DISK_CAP_GB`. Widening to the text
274
+ encoder bought headroom we did not need and cost something we did: **it
275
+ would have made an int8-vs-bf16 comparison unable to say whether a quality
276
+ change came from the denoiser or from prompt encoding.**
277
+
278
+ βœ… **The ground moved and that argument no longer binds.** The live Space at
279
+ sha ``a1791c9e`` gave a *measured* transformer-only baseline β€” **55.89 GiB
280
+ peak RSS, 60.0 GB steady state.** Adding the text encoder is now
281
+ attributable **against that baseline** rather than confounded with it:
282
+ the very thing that made widening a confound is what a measured baseline
283
+ turns into a control.
284
+
285
+ **And it is load-bearing now rather than optional.** Disk was never the
286
+ binding constraint β€” the run died with the disk line reporting **[OK]** at
287
+ 82.2 GB against :data:`ZEROGPU_DISK_CAP_GB`, killed at ~60.0 GB of RAM
288
+ against a ceiling measurably above that (ours)
289
+ and ~68 GB (⚠️ inherited). See ``docs/SPACE_GENERATION_FINDING.md``.
290
+
291
+ βœ… **Revised arithmetic**, derived from ``model_files`` component sizes:
292
+
293
+ * transformer only ....... packed 53.197 GB, peak 133.4 - 137.4
294
+ * **+ text encoder** ..... packed **41.237 GB**, peak **121.4 - 125.4**
295
+
296
+ ⚠️ ~11.96 GB less packed, and ~46.6 GB expected RSS **if** the 1.13x
297
+ transient factor the measured run implies holds β€” **one data point, so an
298
+ extrapolation rather than a rate.**
299
  """
300
  if quantization != "int8":
301
  return None
302
+
303
+ # Checked BEFORE reaching for diffusers: validating our own constant does not
304
+ # need a heavy optional dependency, and a config error should surface as a
305
+ # config error rather than behind an ImportError on a machine without it.
306
+ #
307
+ # Guarded because upstream will not guard it: an unknown component name is
308
+ # silently ignored, so a typo would deploy that component at full precision
309
+ # and raise nothing. `model_files.COMPONENTS` is the registry the download
310
+ # allow-list is built from, so a name that survives this check is one the
311
+ # checkpoint really has.
312
+ unknown = [c for c in QUANTIZED_COMPONENTS if c not in model_files.COMPONENTS]
313
+ if unknown:
314
+ raise ModelLoadError(
315
+ component="transformer",
316
+ reason=(
317
+ f"QUANTIZED_COMPONENTS names {unknown}, which the checkpoint does not "
318
+ f"have. PipelineQuantizationConfig ignores unknown names silently and "
319
+ f"would load those components at full precision -- a lever attached to "
320
+ f"nothing. Known: {sorted(model_files.COMPONENTS)}."
321
+ ),
322
+ )
323
+
324
  try:
325
  from diffusers import PipelineQuantizationConfig
326
  except ImportError as exc:
 
348
  # Bound B, now expressed as an ARGUMENT rather than as an intent a
349
  # source-inspection test had to assert. Widening is one list entry and
350
  # the kwargs test sees it.
351
+ components_to_quantize=list(QUANTIZED_COMPONENTS),
352
  )
353
 
354
 
tests/test_stage_graph_contract.py CHANGED
@@ -1184,23 +1184,53 @@ def test_the_config_is_the_pipeline_level_class_with_the_right_backend(monkeypat
1184
  assert built[-1]["quant_backend"] == "bitsandbytes_8bit", built[-1]
1185
 
1186
 
1187
- def test_bound_b_is_expressed_as_an_argument(monkeypatch, tmp_path):
1188
- """Transformer-only used to be an intent a source scan had to assert.
1189
-
1190
- It is now `components_to_quantize`, so widening it is a visible change to a
1191
- value this test reads β€” not a change to what a call happens to produce.
 
 
 
 
 
 
 
 
 
1192
  """
1193
  _sent, recorders = _loaded(monkeypatch, tmp_path, "int8")
1194
 
1195
  built = recorders["PipelineQuantizationConfig"].calls
1196
  assert built, "the pipeline-level class was not constructed at all"
1197
- assert built[-1]["components_to_quantize"] == ["transformer"], (
1198
- "quantisation is no longer transformer-only. Bound B was chosen over A "
1199
- "deliberately: widening buys headroom we do not need and costs the ability "
1200
- "to attribute a quality change to the denoiser rather than prompt encoding."
 
 
 
 
 
1201
  )
1202
 
1203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1204
  # --------------------------------------------------------------------------
1205
  # the disk line reports our footprint, not the host's free space
1206
  #
 
1184
  assert built[-1]["quant_backend"] == "bitsandbytes_8bit", built[-1]
1185
 
1186
 
1187
+ def test_the_quantised_components_are_exactly_the_declared_set(monkeypatch, tmp_path):
1188
+ """The value that reaches the pipeline, not merely that a value does.
1189
+
1190
+ ⚠️ **A wrong name here would not raise.** `PipelineQuantizationConfig` does
1191
+ no validation β€” `pipe_quant_config.py:171` is a bare membership test β€” so a
1192
+ typo'd component silently loads at full precision. `_quantization_config`
1193
+ guards the names; this asserts the ones that actually arrive.
1194
+
1195
+ **Widened from `["transformer"]` deliberately.** Transformer-only was right
1196
+ while there was no measured baseline: adding the text encoder would have
1197
+ confounded an int8-vs-bf16 comparison. The live Space then supplied one β€”
1198
+ 55.89 GiB peak RSS, 60.0 GB steady β€” and a measured baseline turns that
1199
+ confound into a control. If this test goes red, someone has changed the
1200
+ deployed precision, and that needs to be on purpose.
1201
  """
1202
  _sent, recorders = _loaded(monkeypatch, tmp_path, "int8")
1203
 
1204
  built = recorders["PipelineQuantizationConfig"].calls
1205
  assert built, "the pipeline-level class was not constructed at all"
1206
+ # The expected value is written out, NOT read from QUANTIZED_COMPONENTS. A
1207
+ # message that interpolates the constant under test tracks any mutation of
1208
+ # it and can never contradict it -- it printed "reached as ['transformer'],
1209
+ # not the declared ['transformer']" before this was fixed.
1210
+ assert built[-1]["components_to_quantize"] == ["transformer", "text_encoder"], (
1211
+ f"components_to_quantize reached from_pretrained as "
1212
+ f"{built[-1]['components_to_quantize']}, expected "
1213
+ f"['transformer', 'text_encoder']. Changing the deployed precision must "
1214
+ f"be deliberate."
1215
  )
1216
 
1217
 
1218
+ def test_an_unknown_component_name_is_refused_rather_than_ignored(monkeypatch):
1219
+ """Upstream ignores unknown names silently; we must not.
1220
+
1221
+ A component that is never matched loads at full precision, the deploy uses
1222
+ more memory than intended, and nothing says so β€” the same shape as the
1223
+ `LTX25_TEXT_ENCODER_PRECISION` variable that was wired to nothing.
1224
+ """
1225
+ monkeypatch.setattr(pipelines, "QUANTIZED_COMPONENTS", ("transformer", "text_encodr"))
1226
+
1227
+ with pytest.raises(ModelLoadError) as excinfo:
1228
+ pipelines._quantization_config("int8")
1229
+
1230
+ message = str(excinfo.value)
1231
+ assert "text_encodr" in message and "silently" in message, message
1232
+
1233
+
1234
  # --------------------------------------------------------------------------
1235
  # the disk line reports our footprint, not the host's free space
1236
  #