Repository Public MIT Research preview
1"""Les valeurs numériques du quiz et de ses explications sont recalculées par le code du laboratoire."""2from __future__ import annotations34import math5import unittest67from tests import LABS # noqa: F40189import experiences as X10from ce01 import cache_kv, couts, position11from ce01.selection import Candidat, objectif12from chargeur import charger131415class TestQuiz(unittest.TestCase):16 def test_q03_cache(self):17 c = cache_kv.ConfigAttention("quiz", 16, 16, 2, 32)18 self.assertEqual(cache_kv.memoire_cache_kv(c, 4096, "fp16"), 16 * cache_kv.MIO)19 self.assertEqual(cache_kv.memoire_cache_kv(c, 4096, "fp32"), 32 * cache_kv.MIO)2021 def test_q05_itl(self):22 self.assertAlmostEqual(couts.latence_entre_jetons(2.5, 0.5, 101), 0.020)23 self.assertAlmostEqual(2.5 / 101, 0.0248, places=4)2425 def test_q06_rapport_prefill_decodage(self):26 l = next(x for x in X.couts_jouet()["lignes"] if x["longueur"] == 128)27 self.assertEqual(round(l["preremplissage"] / l["etape_avec_cache"]), 90)2829 def test_q08_marge_requise(self):30 self.assertAlmostEqual(position.marge_requise(1025), math.log(1024))31 self.assertAlmostEqual(position.marge_requise(1025), 6.93, places=2)32 self.assertAlmostEqual(math.log(32), 3.47, places=2)33 self.assertEqual(round(100 * position.masse_sur_le_fait(65, 32, 4.0)), 46)3435 def test_q09_budget(self):36 b = X.budget_exemple()37 self.assertEqual((b["fenetre"] - b["reserve"], b["allocation"].entree, b["marge"]), (7168, 5734, 1434))38 self.assertLessEqual(5734 * 1.25, 7168)39 self.assertGreater(5735 * 1.25, 7168)4041 def test_q10_estimation(self):42 ligne = next(l for l in X.comptage()["lignes"] if l["fusions"] == 200)43 self.assertEqual((round(100 * ligne["erreur_moyenne"]), round(100 * ligne["part_sous_estimee"]), round(100 * ligne["taux_marge_95"])), (53, 19, 25))4445 def test_q11_glouton(self):46 P = charger("solution").politiques47 c = [Candidat("A", 3, 3.3, 1, 0, True), Candidat("B", 5, 5.0, 1, 0, True), Candidat("C", 5, 5.0, 1, 0, True)]48 par_id = {x.id: x for x in c}49 self.assertEqual(P.glouton_densite(c, 10), ["A", "B"])50 self.assertAlmostEqual(objectif(P.glouton_densite(c, 10), par_id), 8.3)51 self.assertEqual(objectif(P.sac_a_dos_exact(c, 10), par_id), 10.0)5253 def test_exemples_du_corrige(self):54 P = charger("solution").politiques55 c = [Candidat("p", 2, 2.2, 1, 0, True), Candidat("a", 9, 9.0, 1, 0, True), Candidat("b", 9, 9.0, 1, 0, True)]56 par_id = {x.id: x for x in c}57 g, e = objectif(P.glouton_densite(c, 18), par_id), objectif(P.sac_a_dos_exact(c, 18), par_id)58 self.assertEqual((round(g, 1), e, round(100 * (e - g) / e)), (11.2, 18.0, 38))59 lin = X.ecarts_lineaires()60 self.assertEqual(sum(l["instances"] for l in lin), 990)61 self.assertTrue(all(l["glouton"] == l["corrige"] for l in lin))6263 def test_q14_compaction(self):64 c = {l["strategie"]: round(100 * l["part_importance"]) for l in X.compaction()}65 self.assertEqual((c["résumés hiérarchiques"], c["résumé glissant"]), (60, 27))666768if __name__ == "__main__":69 unittest.main()70