Download tests/infoflow/test_objective_preservation.py from mmmmmcyaaa/ReTime: direct link, hf CLI and curl.
- Browser
- Download file 6.45 kB
-
https://huggingface.co/mmmmmcyaaa/ReTime/resolve/main/tests/infoflow/test_objective_preservation.py
- Command line
-
hf download hf://mmmmmcyaaa/ReTime/tests/infoflow/test_objective_preservation.py
-
curl -L -o test_objective_preservation.py https://huggingface.co/mmmmmcyaaa/ReTime/resolve/main/tests/infoflow/test_objective_preservation.py
6.45 kB
| import pytest | |
| import torch | |
| from starVLA.training.infoflow import ( | |
| aggregate_repeated_losses, | |
| build_safe_mixture, | |
| ordinary_importance_corrected_mean, | |
| reduce_flow_matching_loss, | |
| ) | |
| def test_safe_mixture_normalization_support_and_bound(): | |
| result = build_safe_mixture( | |
| torch.tensor([1.0, 2.0, 0.0, 3.0]), | |
| torch.tensor([4.0, 1.0, 2.0, 1.0]), | |
| alpha=0.5, | |
| ) | |
| torch.testing.assert_close(result.base_probability.sum(), torch.tensor(1.0, dtype=torch.float64)) | |
| torch.testing.assert_close(result.sampling_probability.sum(), torch.tensor(1.0, dtype=torch.float64)) | |
| assert torch.all(result.sampling_probability[result.base_probability > 0] > 0) | |
| assert result.importance_weight.max().item() <= result.max_importance_weight + 1e-12 | |
| assert result.max_importance_weight == 2.0 | |
| assert not result.importance_weight.requires_grad | |
| def test_alpha_zero_is_exact_baseline_identity(): | |
| result = build_safe_mixture( | |
| torch.tensor([0.2, 0.3, 0.5], dtype=torch.float64), | |
| torch.tensor([0.8, 0.1, 0.1], dtype=torch.float64), | |
| alpha=0.0, | |
| ) | |
| torch.testing.assert_close(result.sampling_probability, result.base_probability, rtol=0, atol=0) | |
| torch.testing.assert_close(result.importance_weight, torch.ones(3, dtype=torch.float64), rtol=0, atol=0) | |
| def test_finite_support_objective_and_gradient_are_exactly_preserved(): | |
| theta = torch.tensor(0.7, dtype=torch.float64, requires_grad=True) | |
| targets = torch.tensor([-1.0, 0.5, 2.0], dtype=torch.float64) | |
| result = build_safe_mixture( | |
| torch.tensor([0.2, 0.3, 0.5], dtype=torch.float64), | |
| torch.tensor([0.7, 0.2, 0.1], dtype=torch.float64), | |
| alpha=0.6, | |
| ) | |
| losses = (theta - targets).square() | |
| baseline = (result.base_probability * losses).sum() | |
| proposal_expectation = (result.sampling_probability * result.importance_weight * losses).sum() | |
| baseline_gradient = torch.autograd.grad(baseline, theta, retain_graph=True)[0] | |
| proposal_gradient = torch.autograd.grad(proposal_expectation, theta)[0] | |
| torch.testing.assert_close(proposal_expectation, baseline, rtol=1e-12, atol=1e-12) | |
| torch.testing.assert_close(proposal_gradient, baseline_gradient, rtol=1e-12, atol=1e-12) | |
| def test_ordinary_estimator_is_not_batch_self_normalized_and_stops_weight_gradient(): | |
| losses = torch.tensor([1.0, 4.0], requires_grad=True) | |
| weights = torch.tensor([2.0, 1.0], requires_grad=True) | |
| estimate = ordinary_importance_corrected_mean(losses, weights) | |
| self_normalized = (weights * losses).sum() / weights.sum() | |
| estimate.backward() | |
| torch.testing.assert_close(estimate, torch.tensor(3.0)) | |
| assert not torch.isclose(estimate.detach(), self_normalized.detach()) | |
| assert weights.grad is None | |
| torch.testing.assert_close(losses.grad, torch.tensor([1.0, 0.5])) | |
| def test_fixed_seed_monte_carlo_estimator_matches_baseline(): | |
| result = build_safe_mixture( | |
| torch.tensor([0.1, 0.2, 0.3, 0.4]), | |
| torch.tensor([0.7, 0.1, 0.1, 0.1]), | |
| alpha=0.5, | |
| ) | |
| losses = torch.tensor([0.2, 1.0, 3.0, 5.0], dtype=torch.float64) | |
| generator = torch.Generator().manual_seed(20260723) | |
| indices = torch.multinomial(result.sampling_probability, 50_000, replacement=True, generator=generator) | |
| estimate = ordinary_importance_corrected_mean(losses[indices], result.importance_weight[indices]) | |
| baseline = (result.base_probability * losses).sum() | |
| torch.testing.assert_close(estimate, baseline, rtol=0.015, atol=0.015) | |
| def test_flow_loss_reduction_preserves_legacy_scalar_mean(): | |
| elementwise = torch.arange(24, dtype=torch.float64).reshape(2, 3, 4).square() | |
| per_sample = reduce_flow_matching_loss(elementwise, reduction="none") | |
| scalar = reduce_flow_matching_loss(elementwise, reduction="mean") | |
| assert per_sample.shape == (2,) | |
| torch.testing.assert_close(scalar, elementwise.mean(), rtol=0, atol=0) | |
| torch.testing.assert_close(scalar, per_sample.mean(), rtol=1e-15, atol=1e-15) | |
| def test_repeated_flow_losses_are_aggregated_per_original_sample(): | |
| repeated = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) | |
| aggregated = aggregate_repeated_losses(repeated, repeat_count=2, batch_size=3) | |
| torch.testing.assert_close(aggregated, torch.tensor([2.5, 3.5, 4.5])) | |
| torch.testing.assert_close(aggregated.mean(), repeated.mean()) | |
| def test_repeated_per_sample_path_matches_legacy_scalar_and_gradient(): | |
| repeat_count, batch_size = 3, 2 | |
| prediction = torch.linspace(-1.0, 1.0, repeat_count * batch_size * 4, dtype=torch.float64) | |
| prediction = prediction.reshape(repeat_count * batch_size, 2, 2).requires_grad_() | |
| target = torch.linspace(0.5, -0.5, prediction.numel(), dtype=torch.float64).reshape_as(prediction) | |
| elementwise = (prediction - target).square() | |
| legacy_scalar = reduce_flow_matching_loss(elementwise, reduction="mean") | |
| repeated_per_sample = reduce_flow_matching_loss(elementwise, reduction="none") | |
| original_per_sample = aggregate_repeated_losses( | |
| repeated_per_sample, | |
| repeat_count=repeat_count, | |
| batch_size=batch_size, | |
| ) | |
| reconstructed_scalar = original_per_sample.mean() | |
| legacy_gradient = torch.autograd.grad(legacy_scalar, prediction, retain_graph=True)[0] | |
| reconstructed_gradient = torch.autograd.grad(reconstructed_scalar, prediction)[0] | |
| torch.testing.assert_close(reconstructed_scalar, legacy_scalar, rtol=1e-15, atol=1e-15) | |
| torch.testing.assert_close(reconstructed_gradient, legacy_gradient, rtol=1e-15, atol=1e-15) | |
| def test_invalid_safe_mixture_inputs_fail_fast(base, proposal, alpha, error): | |
| with pytest.raises(error): | |
| build_safe_mixture(base, proposal, alpha=alpha) | |
| def test_invalid_reduction_and_repeat_shapes_fail_fast(): | |
| with pytest.raises(ValueError): | |
| reduce_flow_matching_loss(torch.ones(2, 3), reduction="sum") | |
| with pytest.raises(ValueError): | |
| aggregate_repeated_losses(torch.ones(5), repeat_count=2) | |