laya-ledgar / code /tests /test_fit.py
VibeHash's picture
Release laya-ledgar, run ce-seed0
779ff0b verified
Raw History Blame Contribute Delete
1.67 kB
"""The time estimate's replay of Laya's batching, checked by hand and against Laya's own split."""
from __future__ import annotations
import importlib.util
import random
import unittest
from layatasks import fit
class PaddedTokenTests(unittest.TestCase):
def test_pads_each_micro_batch_to_its_longest_row(self):
# Of ten rows Laya holds back int(10 * 0.1) = 1 for calibration and trains on nine.
lengths = [5] * 9 + [50]
keep = [lengths[i] for i in fit.train_rows(len(lengths))]
self.assertEqual(len(keep), 9)
order = list(keep)
random.Random(0).shuffle(order)
expected = sum(max(order[s:s + 4]) * len(order[s:s + 4]) for s in range(0, len(order), 4))
self.assertEqual(fit.padded_tokens(lengths, epochs=1, micro_batch=4), expected)
def test_equal_rows_need_no_padding(self):
self.assertEqual(fit.padded_tokens([7] * 100, epochs=3), 7 * 90 * 3)
class SplitTests(unittest.TestCase):
@unittest.skipUnless(importlib.util.find_spec("laya"), "laya is not installed")
def test_hold_out_matches_laya(self):
from laya.train import split_calibration
for n in (10, 999, 5000):
items = list(range(n))
train, _calib = split_calibration(items, fit.CALIB_MAX, fit.CALIB_FRAC, fit.CALIB_SEED)
self.assertEqual(fit.train_rows(n), train)
class DescribeTests(unittest.TestCase):
def test_counts_rows_longer_than_the_room(self):
d = fit.describe([1, 2, 3, 10, 20], room=5)
self.assertEqual((d["truncated"], d["truncated_share"], d["state_tokens"]["max"]), (2, 0.4, 20))
if __name__ == "__main__":
unittest.main()