| import torch |
|
|
| from trident.config import TridentConfig |
| from trident.patching import entropy_budget_patch |
|
|
|
|
| def test_patch_constraints(): |
| cfg = TridentConfig.tiny_test() |
| B, L = 4, 60 |
| torch.manual_seed(0) |
| entropy = torch.rand(B, L) * 4.0 |
| valid = torch.ones(B, L, dtype=torch.bool) |
| |
| out = entropy_budget_patch(entropy, valid, cfg.b_max, cfg.tau_b, max_patches=L + 1) |
| seg = out["seg_ids"] |
|
|
| |
| assert (seg[:, 0] == 0).all() |
| assert (seg[:, 1:] - seg[:, :-1] >= 0).all() |
| assert (seg[:, 1:] - seg[:, :-1] <= 1).all() |
|
|
| |
| assert (out["patch_len"] <= cfg.b_max).all() |
|
|
| |
| assert torch.equal(out["prev_patch"], seg - 1) |
|
|
| |
| out2 = entropy_budget_patch(entropy, valid, cfg.b_max, cfg.tau_b, max_patches=L + 1) |
| assert torch.equal(seg, out2["seg_ids"]) |
|
|
|
|
| def test_bmax_forces_boundary(): |
| cfg = TridentConfig.tiny_test(tau_b=1e9) |
| B, L = 1, 40 |
| entropy = torch.zeros(B, L) |
| valid = torch.ones(B, L, dtype=torch.bool) |
| out = entropy_budget_patch(entropy, valid, cfg.b_max, cfg.tau_b, cfg.max_patches) |
| |
| assert (out["patch_len"][out["patch_valid"]] <= cfg.b_max).all() |
| assert out["patch_len"][0, 0] == cfg.b_max |
|
|
|
|
| def test_low_threshold_many_patches(): |
| cfg = TridentConfig.tiny_test(tau_b=0.1) |
| B, L = 1, 30 |
| entropy = torch.ones(B, L) |
| valid = torch.ones(B, L, dtype=torch.bool) |
| out = entropy_budget_patch(entropy, valid, cfg.b_max, cfg.tau_b, max_patches=L + 1) |
| |
| assert out["num_patches"][0].item() == L |
|
|
|
|
| def test_overflow_merges_into_last_patch(): |
| """When the patch budget is exceeded, the remainder merges into the last |
| slot rather than corrupting indices.""" |
| cfg = TridentConfig.tiny_test(tau_b=0.1) |
| B, L, P = 1, 30, 8 |
| entropy = torch.ones(B, L) |
| valid = torch.ones(B, L, dtype=torch.bool) |
| out = entropy_budget_patch(entropy, valid, cfg.b_max, cfg.tau_b, max_patches=P) |
| assert out["num_patches"][0].item() == P |
| assert out["seg_ids"].max().item() == P - 1 |
| |
| assert out["patch_len"].sum().item() == L |
|
|