|
| 1 | +""" |
| 2 | +tests/functional/test_functional_e2e.py — eosllm functional E2E tests |
| 3 | +SPDX-License-Identifier: MIT Copyright (c) 2026 EmbeddedOS Foundation |
| 4 | +""" |
1 | 5 | import unittest |
| 6 | + |
| 7 | + |
| 8 | +class Tokenizer: |
| 9 | + VOCAB = { |
| 10 | + "<bos>":1,"<eos>":2,"<pad>":0, |
| 11 | + "the":3,"a":4,"is":5,"in":6,"of":7,"and":8,"to":9,"it":10, |
| 12 | + "hello":11,"world":12,"embedded":13,"os":14,"kernel":15, |
| 13 | + "task":16,"scheduler":17,"memory":18,"gps":19,"location":20, |
| 14 | + } |
| 15 | + def __init__(self): |
| 16 | + self.vocab = self.VOCAB |
| 17 | + self.id2tok = {v:k for k,v in self.VOCAB.items()} |
| 18 | + def encode(self, text): |
| 19 | + tokens = [1] |
| 20 | + for w in text.lower().split(): |
| 21 | + tokens.append(self.vocab.get(w, 0)) |
| 22 | + tokens.append(2) |
| 23 | + return tokens |
| 24 | + def decode(self, ids): |
| 25 | + return " ".join(self.id2tok.get(i,"<unk>") for i in ids if i not in (0,1,2)) |
| 26 | + def vocab_size(self): |
| 27 | + return len(self.vocab) |
| 28 | + |
| 29 | + |
2 | 30 | class TestEosLLMFunctional(unittest.TestCase): |
3 | | - def test_llm_inference_pipeline(self): |
4 | | - pipeline = ["tokenize", "forward", "sample", "detokenize"] |
5 | | - self.assertEqual(pipeline[-1], "detokenize") |
| 31 | + def setUp(self): |
| 32 | + self.tok = Tokenizer() |
| 33 | + |
| 34 | + def test_encode_decode_roundtrip(self): |
| 35 | + text = "hello world" |
| 36 | + ids = self.tok.encode(text) |
| 37 | + decoded = self.tok.decode(ids) |
| 38 | + self.assertEqual(decoded, text) |
| 39 | + |
| 40 | + def test_encode_adds_bos_eos(self): |
| 41 | + ids = self.tok.encode("hello") |
| 42 | + self.assertEqual(ids[0], 1) # BOS |
| 43 | + self.assertEqual(ids[-1], 2) # EOS |
| 44 | + |
| 45 | + def test_encode_unknown_token(self): |
| 46 | + ids = self.tok.encode("unknownxyz") |
| 47 | + # Unknown token maps to 0 (pad) |
| 48 | + self.assertIn(0, ids) |
| 49 | + |
| 50 | + def test_decode_skips_special_tokens(self): |
| 51 | + ids = [1, 11, 12, 2] # BOS hello world EOS |
| 52 | + decoded = self.tok.decode(ids) |
| 53 | + self.assertEqual(decoded, "hello world") |
| 54 | + |
| 55 | + def test_vocab_size(self): |
| 56 | + self.assertGreater(self.tok.vocab_size(), 10) |
| 57 | + |
| 58 | + def test_inference_pipeline(self): |
| 59 | + pipeline = ["tokenize", "embed", "attention", "ffn", "decode"] |
| 60 | + self.assertEqual(pipeline[0], "tokenize") |
| 61 | + self.assertEqual(pipeline[-1], "decode") |
| 62 | + |
| 63 | + def test_context_window_limit(self): |
| 64 | + max_ctx = 512 |
| 65 | + tokens = list(range(max_ctx + 10)) |
| 66 | + truncated = tokens[:max_ctx] |
| 67 | + self.assertEqual(len(truncated), max_ctx) |
| 68 | + |
| 69 | + def test_temperature_sampling(self): |
| 70 | + import math |
| 71 | + logits = [1.0, 2.0, 3.0] |
| 72 | + temp = 0.5 |
| 73 | + scaled = [l / temp for l in logits] |
| 74 | + max_l = max(scaled) |
| 75 | + exp_l = [math.exp(l - max_l) for l in scaled] |
| 76 | + probs = [e / sum(exp_l) for e in exp_l] |
| 77 | + self.assertAlmostEqual(sum(probs), 1.0, places=5) |
| 78 | + self.assertGreater(probs[2], probs[0]) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + unittest.main() |
0 commit comments