Spaces:
Running on Zero
fix: one rearrangement, not two -- drop the latent transpose
Browse files466bbd5 was an over-correction and host-runner has the A/B. With the mel
time-major AND unpacked_latents applied, the latents came back (1, 8, 16,
101) -- transposed back -- and every reconstruction was pinned to 0.61 s
regardless of input, with all four tones lost.
audio_vae.encode does NOT reorder axes. It downsamples both by the
compression ratio and preserves their order, so the MEL's layout decides the
latents' layout. Fed a time-major mel it returns [B, C, L, M] directly.
The measurement that produced the transpose was taken before encoder_layout
existed in the path: with a mel-major mel the latents were mel-major too, and
what was observed was the mel's transposition showing up one stage later. The
symptom's location is not its origin. AUDIO_LATENT_DIMS stays; it was right
throughout, and the only question was whether a transpose was needed to reach
it -- an answer that changed when the input orientation changed.
The invariant that catches this in EITHER direction, and host-runner's
suggestion: the frame count must scale with the input duration. Stronger than
a shape check, which passes on a transposed tensor of the right rank -- the
fixed 0.61 s output was the tell, because the mel axis does not move when the
clip gets longer.
For that to be testable the fake had to stop returning constants. Its mel now
derives its time axis from the waveform and its encode derives the latent
shape from the mel, modelling preserve-order-and-downsample. A fixture that
cannot express duration cannot catch a defect whose signature is that
duration stops mattering; a fake returning a fixed shape agrees with whatever
the code does to it, and would pass a missing transpose and a doubled one
alike.
Also deduplicated test_audio.py: repeated appends had left 8 tests defined up
to SIX times each. Byte-identical copies, so no behaviour was lost -- but a
duplicate def silently shadows the earlier one, which is the
tests-connected-to-nothing pattern arriving in my own file. 80 defs, 44
unique, now 44, confirmed by AST rather than by grep.
- ltx25_studio/audio.py +16 -26
- tests/conftest.py +37 -10
- tests/test_audio.py +41 -15
|
@@ -72,7 +72,6 @@ __all__ = [
|
|
| 72 |
"build_mel_front_end",
|
| 73 |
"encoder_layout",
|
| 74 |
"batched",
|
| 75 |
-
"unpacked_latents",
|
| 76 |
"mel_parameters",
|
| 77 |
"MEL_PARAM_SOURCES",
|
| 78 |
"MEL_CONVENTIONS",
|
|
@@ -424,6 +423,21 @@ def encoder_layout(mel):
|
|
| 424 |
encoder wants, and unbatched. Correcting only the batch axis yields a 4D
|
| 425 |
tensor that satisfies the rank check and is a **transposed spectrogram** --
|
| 426 |
audio out, silently wrong. See :data:`MEL_INPUT_DIMS`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
"""
|
| 428 |
return mel.transpose(-2, -1)
|
| 429 |
|
|
@@ -654,30 +668,6 @@ def batched(waveform):
|
|
| 654 |
return waveform[None]
|
| 655 |
|
| 656 |
|
| 657 |
-
def unpacked_latents(latents):
|
| 658 |
-
"""``audio_vae.encode`` output ``[B, C, M, L]`` -> upstream's ``[B, C, L, M]``.
|
| 659 |
-
|
| 660 |
-
✅ Established by experiment on real weights: halving the input duration
|
| 661 |
-
halves **axis 3** (100 → 50) while axis 2 stays pinned at
|
| 662 |
-
``mel_bins / mel_compression_ratio`` = 16. So the VAE emits mel-major.
|
| 663 |
-
|
| 664 |
-
📄 Upstream wants length-major, and says so in
|
| 665 |
-
``pipeline_ltx2_condition.prepare_audio_latents``: *"latents expected to be
|
| 666 |
-
unpacked (4D) with shape [B, C, L, M]"*, with ``_pack_audio_latents`` doing
|
| 667 |
-
``latents.transpose(1, 2).flatten(2, 3) # [B, C, L, M] --> [B, L, C * M]``.
|
| 668 |
-
|
| 669 |
-
**This is why :data:`AUDIO_LATENT_DIMS` was right and must not be
|
| 670 |
-
"corrected".** Our constant, ``validate_audio_latents_shape`` and
|
| 671 |
-
``audio_frames_from_latents`` all agree with upstream; the missing step was
|
| 672 |
-
here. Swapping the constant instead would have made every reader agree with
|
| 673 |
-
every other reader and all of them wrong -- PLAN §5.7 row 5 exactly.
|
| 674 |
-
"""
|
| 675 |
-
swap = getattr(latents, "transpose", None)
|
| 676 |
-
if swap is None:
|
| 677 |
-
return latents
|
| 678 |
-
return swap(-2, -1)
|
| 679 |
-
|
| 680 |
-
|
| 681 |
def _validate_mel_rank(mel) -> None:
|
| 682 |
"""Refuse a mel the encoder would silently misread.
|
| 683 |
|
|
@@ -787,7 +777,7 @@ def encode_waveform_to_latents(
|
|
| 787 |
# explicitly-passed `to_mel=` from a caller who does not know the VAE dtype.
|
| 788 |
mel = _matched_to_module(mel, audio_vae, name="audio_vae")
|
| 789 |
_validate_mel_rank(mel)
|
| 790 |
-
latents =
|
| 791 |
validate_audio_latents_shape(getattr(latents, "shape", ()))
|
| 792 |
return latents
|
| 793 |
|
|
|
|
| 72 |
"build_mel_front_end",
|
| 73 |
"encoder_layout",
|
| 74 |
"batched",
|
|
|
|
| 75 |
"mel_parameters",
|
| 76 |
"MEL_PARAM_SOURCES",
|
| 77 |
"MEL_CONVENTIONS",
|
|
|
|
| 423 |
encoder wants, and unbatched. Correcting only the batch axis yields a 4D
|
| 424 |
tensor that satisfies the rank check and is a **transposed spectrogram** --
|
| 425 |
audio out, silently wrong. See :data:`MEL_INPUT_DIMS`.
|
| 426 |
+
|
| 427 |
+
**This is the ONLY rearrangement in the path, and that is the point.**
|
| 428 |
+
``audio_vae.encode`` does not reorder axes -- it downsamples both by the
|
| 429 |
+
compression ratio and preserves their order -- so **the mel's layout decides
|
| 430 |
+
the latents' layout.** Fed a time-major mel it returns ``[B, C, L, M]``
|
| 431 |
+
directly, which is what :data:`AUDIO_LATENT_DIMS` declares and what upstream
|
| 432 |
+
packs.
|
| 433 |
+
|
| 434 |
+
⚠️ A second transpose was once applied to the *encode output*, on a
|
| 435 |
+
measurement taken **before this function existed** -- when the mel was still
|
| 436 |
+
mel-major, the latents were too, and the transposition was the mel's showing
|
| 437 |
+
up one stage later. Applying both corrections put the frame axis back where
|
| 438 |
+
it started and pinned every reconstruction to 0.61 s regardless of input.
|
| 439 |
+
**The symptom's location is not its origin**; fixing the mel is the whole
|
| 440 |
+
fix.
|
| 441 |
"""
|
| 442 |
return mel.transpose(-2, -1)
|
| 443 |
|
|
|
|
| 668 |
return waveform[None]
|
| 669 |
|
| 670 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
def _validate_mel_rank(mel) -> None:
|
| 672 |
"""Refuse a mel the encoder would silently misread.
|
| 673 |
|
|
|
|
| 777 |
# explicitly-passed `to_mel=` from a caller who does not know the VAE dtype.
|
| 778 |
mel = _matched_to_module(mel, audio_vae, name="audio_vae")
|
| 779 |
_validate_mel_rank(mel)
|
| 780 |
+
latents = _sampled(audio_vae.encode(mel))
|
| 781 |
validate_audio_latents_shape(getattr(latents, "shape", ()))
|
| 782 |
return latents
|
| 783 |
|
|
@@ -473,6 +473,9 @@ class FakeAudioVae:
|
|
| 473 |
#: hand-off that never moves the tensor.
|
| 474 |
device = "cuda"
|
| 475 |
|
|
|
|
|
|
|
|
|
|
| 476 |
def __init__(self, sampling_rate=16000):
|
| 477 |
# ⚠️ These are the REAL key names, read off audio_vae/config.json at
|
| 478 |
# revision 871165de…, not our semantic ones. The previous fake used our
|
|
@@ -487,25 +490,49 @@ class FakeAudioVae:
|
|
| 487 |
)
|
| 488 |
# Instance attributes on the model, as upstream sets them in __init__.
|
| 489 |
# They are absent from config.json entirely.
|
| 490 |
-
self.mel_compression_ratio = 4
|
| 491 |
self.temporal_compression_ratio = 4
|
| 492 |
self.mels: list = []
|
| 493 |
self.encoded: list = []
|
| 494 |
|
| 495 |
def mel_spectrogram(self, waveform):
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
self.mels.append(waveform)
|
| 500 |
-
|
|
|
|
|
|
|
| 501 |
|
| 502 |
def encode(self, mel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
self.encoded.append(mel)
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
return SimpleNamespace(latent_dist=SimpleNamespace(sample=lambda: latents))
|
| 510 |
|
| 511 |
|
|
|
|
| 473 |
#: hand-off that never moves the tensor.
|
| 474 |
device = "cuda"
|
| 475 |
|
| 476 |
+
#: Downsampling applied to BOTH axes by `encode`, from the real config.
|
| 477 |
+
mel_compression_ratio = 4
|
| 478 |
+
|
| 479 |
def __init__(self, sampling_rate=16000):
|
| 480 |
# ⚠️ These are the REAL key names, read off audio_vae/config.json at
|
| 481 |
# revision 871165de…, not our semantic ones. The previous fake used our
|
|
|
|
| 490 |
)
|
| 491 |
# Instance attributes on the model, as upstream sets them in __init__.
|
| 492 |
# They are absent from config.json entirely.
|
|
|
|
| 493 |
self.temporal_compression_ratio = 4
|
| 494 |
self.mels: list = []
|
| 495 |
self.encoded: list = []
|
| 496 |
|
| 497 |
def mel_spectrogram(self, waveform):
|
| 498 |
+
"""A mel whose TIME axis is derived from the waveform, in encoder layout.
|
| 499 |
+
|
| 500 |
+
float32, as the real transform computes it -- the cast to the VAE's
|
| 501 |
+
dtype happens at the hand-off, which is what this lets us check.
|
| 502 |
+
|
| 503 |
+
⚠️ The time axis is computed, not fixed. A constant-shaped mel makes
|
| 504 |
+
every duration look the same, which is exactly the symptom a doubled
|
| 505 |
+
transpose produced on the card: reconstructions pinned to one length
|
| 506 |
+
regardless of input. A fixture that cannot express duration cannot
|
| 507 |
+
catch that.
|
| 508 |
+
"""
|
| 509 |
self.mels.append(waveform)
|
| 510 |
+
batch, channels, samples = waveform.shape
|
| 511 |
+
frames = samples // self.config.mel_hop_length + 1
|
| 512 |
+
return FakeMel("float32", (batch, channels, frames, self.config.mel_bins))
|
| 513 |
|
| 514 |
def encode(self, mel):
|
| 515 |
+
"""Model what the real encoder does: **preserve axis order, downsample both.**
|
| 516 |
+
|
| 517 |
+
⚠️ The output shape is DERIVED from the input's, not fixed. That is the
|
| 518 |
+
whole point: the encoder does not reorder anything, so the mel's layout
|
| 519 |
+
decides the latents' layout. A fake returning a constant shape agrees
|
| 520 |
+
with whatever the code does to it -- it would pass a missing transpose
|
| 521 |
+
and a doubled one alike, and a doubled one shipped.
|
| 522 |
+
|
| 523 |
+
Matches the card exactly: a (1, 2, 401, 64) mel gives (1, 8, 101, 16).
|
| 524 |
+
"""
|
| 525 |
self.encoded.append(mel)
|
| 526 |
+
batch, _channels, first, second = mel.shape
|
| 527 |
+
ratio = self.mel_compression_ratio
|
| 528 |
+
latents = ShapedTensor(
|
| 529 |
+
(
|
| 530 |
+
batch,
|
| 531 |
+
self.config.latent_channels,
|
| 532 |
+
-(-first // ratio), # ceil, as the strided convolutions give
|
| 533 |
+
second // ratio,
|
| 534 |
+
)
|
| 535 |
+
)
|
| 536 |
return SimpleNamespace(latent_dist=SimpleNamespace(sample=lambda: latents))
|
| 537 |
|
| 538 |
|
|
@@ -702,28 +702,54 @@ def test_a_batched_waveform_is_refused_by_the_channel_check():
|
|
| 702 |
assert "3D" in message and "UNBATCHED" in message, message
|
| 703 |
|
| 704 |
|
| 705 |
-
def
|
| 706 |
-
"""The
|
| 707 |
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
"""
|
| 712 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 717 |
)
|
| 718 |
|
| 719 |
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
"
|
|
|
|
|
|
|
| 727 |
)
|
| 728 |
|
| 729 |
|
|
|
|
| 702 |
assert "3D" in message and "UNBATCHED" in message, message
|
| 703 |
|
| 704 |
|
| 705 |
+
def test_the_latents_come_back_frames_major_from_one_rearrangement():
|
| 706 |
+
"""The mel's layout decides the latents' layout, so fixing the mel is the fix.
|
| 707 |
|
| 708 |
+
``audio_vae.encode`` preserves axis order. Fed a time-major mel it returns
|
| 709 |
+
``[B, C, L, M]`` directly -- no second transpose, and a second transpose put
|
| 710 |
+
the frame axis back where it started.
|
| 711 |
"""
|
| 712 |
+
vae = FakeAudioVae()
|
| 713 |
+
rate = vae.config.sample_rate
|
| 714 |
+
|
| 715 |
+
# A real clip, not the short default fixture: at 4096 samples there are
|
| 716 |
+
# genuinely fewer frames than mel bins, so the invariant would not hold and
|
| 717 |
+
# the test would be measuring the fixture rather than the code.
|
| 718 |
+
latents = audio.encode_waveform_to_latents(
|
| 719 |
+
waveform_of(2, rate * 4), audio_vae=vae, sampling_rate=rate
|
| 720 |
+
)
|
| 721 |
|
| 722 |
+
frames = audio.audio_frames_from_latents(latents.shape)
|
| 723 |
+
mel_axis = latents.shape[-1]
|
| 724 |
+
assert frames > mel_axis, (
|
| 725 |
+
f"the frame count came back as {frames} against {mel_axis} on the mel axis. "
|
| 726 |
+
f"For any real clip there are far more frames than mel bins, so this is the "
|
| 727 |
+
f"signature of a transposed latent -- most likely two rearrangements where "
|
| 728 |
+
f"one was needed."
|
| 729 |
)
|
| 730 |
|
| 731 |
|
| 732 |
+
@pytest.mark.parametrize("seconds,expected", [(1.0, 25), (2.0, 50), (4.0, 100)])
|
| 733 |
+
def test_the_frame_count_tracks_the_input_duration(seconds, expected):
|
| 734 |
+
"""The invariant that catches an axis-order error in EITHER direction.
|
| 735 |
+
|
| 736 |
+
Stronger than a shape check, which passes on a transposed tensor of the
|
| 737 |
+
right rank. A doubled transpose pinned every reconstruction to 0.61 s
|
| 738 |
+
regardless of input, and **the fixed length was the tell** -- the mel axis
|
| 739 |
+
does not move when the clip gets longer.
|
| 740 |
+
"""
|
| 741 |
+
vae = FakeAudioVae()
|
| 742 |
+
rate = vae.config.sample_rate
|
| 743 |
+
|
| 744 |
+
latents = audio.encode_waveform_to_latents(
|
| 745 |
+
waveform_of(2, int(rate * seconds)), audio_vae=vae, sampling_rate=rate
|
| 746 |
+
)
|
| 747 |
|
| 748 |
+
frames = audio.audio_frames_from_latents(latents.shape)
|
| 749 |
+
assert frames == pytest.approx(expected, abs=2), (
|
| 750 |
+
f"a {seconds}s clip gave {frames} frames, expected about {expected}. A frame "
|
| 751 |
+
f"count that does not scale with duration means the axis being read is the mel "
|
| 752 |
+
f"axis, which is fixed at mel_bins / mel_compression_ratio."
|
| 753 |
)
|
| 754 |
|
| 755 |
|