teragron/TinyStories
3
1#define TESTING2#include "run.c"3 4void assert_eq(int a, int b) {5 if (a != b) {6 printf("Assertion failed: %d != %d\n", a, b);7 exit(EXIT_FAILURE);8 }9}10 11void test_prompt_encoding(Tokenizer* tokenizer, char* prompt, int* expected_tokens, int num_expected_tokens) {12 // encode13 int* prompt_tokens = (int*)malloc((strlen(prompt)+3) * sizeof(int));14 int num_prompt_tokens = 0; // the total number of prompt tokens15 encode(tokenizer, prompt, 1, 0, prompt_tokens, &num_prompt_tokens);16 17 #if VERBOSITY == 118 // print maybe19 printf("expected tokens:\n");20 for (int i = 0; i < num_expected_tokens; i++) printf("%d ", expected_tokens[i]);21 printf("\n");22 printf("actual tokens:\n");23 for (int i = 0; i < num_prompt_tokens; i++) printf("%d ", prompt_tokens[i]);24 printf("\n");25 #endif26 27 // verify28 assert_eq(num_prompt_tokens, num_expected_tokens);29 for (int i = 0; i < num_prompt_tokens; i++) {30 assert_eq(prompt_tokens[i], expected_tokens[i]);31 }32 33 #if VERBOSITY == 134 printf("OK\n");35 printf("---\n");36 #endif37 free(prompt_tokens);38}39 40void test_prompt_encodings() {41 // let's verify that the Tokenizer works as expected42 43 char *tokenizer_path = "tokenizer.bin";44 int vocab_size = 32000;45 Tokenizer tokenizer;46 build_tokenizer(&tokenizer, tokenizer_path, vocab_size);47 48 // test 0 (test the empty string) (I added this as a simple case)49 char *prompt0 = "";50 int expected_tokens0[] = {1};51 test_prompt_encoding(&tokenizer, prompt0, expected_tokens0, sizeof(expected_tokens0) / sizeof(int));52 53 // the tests below are taken from the Meta Llama 2 repo example code54 // https://github.com/facebookresearch/llama/blob/main/example_text_completion.py55 // and the expected tokens come from me breaking in the debugger in Python56 57 // test 158 char *prompt = "I believe the meaning of life is";59 int expected_tokens[] = {1, 306, 4658, 278, 6593, 310, 2834, 338};60 test_prompt_encoding(&tokenizer, prompt, expected_tokens, sizeof(expected_tokens) / sizeof(int));61 62 // test 263 char* prompt2 = "Simply put, the theory of relativity states that ";64 int expected_tokens2[] = {1, 3439, 17632, 1925, 29892, 278, 6368, 310, 14215, 537, 5922, 393, 29871};65 test_prompt_encoding(&tokenizer, prompt2, expected_tokens2, sizeof(expected_tokens2) / sizeof(int));66 67 // test 368 char* prompt3 = "A brief message congratulating the team on the launch:\n\n Hi everyone,\n\n I just ";69 int expected_tokens3[] = {1, 319, 11473, 2643, 378, 629, 271, 18099, 278, 3815, 373, 278, 6826, 29901, 13, 13, 4706, 6324, 14332, 29892, 13, 13, 4706, 306, 925, 29871};70 test_prompt_encoding(&tokenizer, prompt3, expected_tokens3, sizeof(expected_tokens3) / sizeof(int));71 72 // test 473 char* prompt4 = "Translate English to French:\n\n sea otter => loutre de mer\n peppermint => menthe poivrée\n plush girafe => girafe peluche\n cheese =>";74 int expected_tokens4[] = {1, 4103, 9632, 4223, 304, 5176, 29901, 13, 13, 4706, 7205, 4932, 357, 1149, 301, 449, 276, 316, 2778, 13, 4706, 1236, 407, 837, 524, 1149, 6042, 354, 772, 440, 29878, 1318, 13, 4706, 715, 1878, 330, 3055, 1725, 1149, 330, 3055, 1725, 4639, 28754, 13, 4706, 923, 968, 1149};75 test_prompt_encoding(&tokenizer, prompt4, expected_tokens4, sizeof(expected_tokens4) / sizeof(int));76 77 // memory and file handles cleanup78 free_tokenizer(&tokenizer);79}80 81int main(int argc, char *argv[]) {82 test_prompt_encodings();83 printf("ALL OK\n");84}85 