Manuscript Offprint

arXiv:2610.40533

Homebrewed SLMs: Autoregressive and Masked Diffusion Language Models Built at Home for Portuguese

Pedro Carvalho Brom

ABSTRACT

Building a language model of one’s own no longer requires a data centre. This tutorial trains one from scratch, tokenizer included, on one consumer GPU, and shows how to tell whether it is good for the tasks of a specialist domain. It proposes no new method. It releases a tested Python package and two notebooks in Brazilian Portuguese with autoregressive and masked diffusion generation on one network, a preregistered comparison protocol, legal tasks built from text alone with a Kneser–Ney floor and public yardsticks, reference numbers for models of 11 to 12M parameters, and a catalogue of defects that only measurement revealed. On Brazilian prose and federal laws, at minutes per run, the autoregressive mode peaks at 64 epochs over the same text and then memorises it, while masked diffusion improves up to 256. At their best points on test, the diffusion bound exceeds the autoregressive likelihood by 0.06 bits per byte, within the preregistered minimum effect. On held-out laws and on acts published after training, the model ties a 6-gram floor in likelihood on the same windows, and it loses to the floor at continuing those acts. It beats the floor at detecting tampered text, and fills cross-references more often than the floor and public models of 160M and 630M parameters, only in decrees, a genre absent from training, where those scorers stay near or below chance, and in filling the blank only when each group weighs the same, since per item it ties the 630M model. On laws, the public models, trained with about 5×1025 \times 10^2 to 10510^5 times its compute, lead in tampered text and continuation. The blind human reading is preregistered and not part of this version. Code and notebooks: https://github.com/pcbrom/homebrewed-slms.

Keywords homebrewed language models · small language models · masked diffusion · Portuguese · tutorial · evaluation · specialist domain

1 Introduction

Many people who need a language model for a narrow domain, such as the laws of one country, the records of one hospital or the literature of one period, face the same question. Is it possible to train a model of one's own, from nothing, on a home computer, and obtain something that is actually good for the tasks at hand? Most learning material does not answer it, because it starts from a model someone else pretrained. A checkpoint is downloaded and a library fine-tunes it, and the learner rarely sees where the loss comes from, how the tokenizer was built, or how to know whether a fluent sample reflects learning or recitation of the training text. Adapting a small pretrained model remains a strong road when one exists for the language. Di Oliveira et al. [2024] fine-tuned TeenyTinyLlama [Corrêa et al., 2024a], a small model pretrained on Brazilian Portuguese, to the Mercosur Common Nomenclature, a specialist customs domain, and compared it with GPT-4 on questions held out from training. This tutorial takes the other road, training from nothing,

A PREPRINT

which is the one left when no pretrained model fits the language, the period or the vocabulary of the domain, and the one that keeps every step visible.

This tutorial answers the question with a complete, measured example. It builds a small language model (SLM) by hand, from the raw text to the generated samples, and evaluates it against references that give meaning to its numbers. The reader who follows it ends with three things. The first is a working pipeline, a Python package and two notebooks that run each step with its input and output shown. The second is a way to adapt the pipeline to a corpus of their own by editing a configuration and a domain adapter. The third is an evaluation protocol that says whether the resulting model is good, compared with what, and for which tasks.

The tutorial proposes no new method, and its value lies in putting known pieces together where each can be checked. Masked diffusion in the number-of-masks form follows Ou et al. [2024] and Nie et al. [2025], the floor is interpolated modified Kneser–Ney smoothing [Chen and Goodman, 1996], the statistics are standard, and the protocol borrows preregistration and checks before results from good practice in empirical research [van Miltenburg et al., 2021; Dodge et al., 2019]. What a reader can reuse, and cite, is the assembly, in five parts.

C1, package and notebooks. The s1m package, eleven modules each guarded by tests (Table 2), and two notebooks that run it step by step, one per generation mode, written for the free GPU of Colab. The reproduction on that GPU is not part of this version. Code identifiers and notebooks are in Portuguese, and Table 2 glosses each module.

C2, comparison protocol. Checks with criteria fixed before the measurement, preregistrations frozen by their SHA-256 hash, a minimum effect, a seed count derived from a pilot and dated amendments (Section 7, folder preregistro).

C3, task kit. Four legal tasks built by a script from the text of 1,094 federal acts published in 2025 and 2026, with no human labelling, the item hashes and the scoring rule, which place a home-built model between a Kneser–Ney floor and public models (Section 6.3, folder tarefas).

C4, reference numbers. Bits per byte, task scores and calibration for models of 11 to 12M parameters trained on a few megabytes of text, with seeds, intervals and the floor scored on the same windows, each reproducible on one GPU in minutes (Section 8, folders runs and resultados).

C5, catalogue of defects. The defects that only measurement revealed, each with the check that detected it and the test that now guards it (Section 9).

The model has two generation modes on one network. The autoregressive mode predicts each token from the previous ones with causal attention. The masked diffusion mode [Austin et al., 2021; Sahoo et al., 2024; Shi et al., 2024; Ou et al., 2024; Nie et al., 2025] predicts masked tokens from the visible ones with bidirectional attention. The only differences between the two are the attention mask and the loss, so the tutorial can teach both with the same code, and a measured difference between them comes from training and generation, not from the network.

The worked example uses two Portuguese corpora. The first, the prose of Machado de Assis, has a recognisable style and serves to demonstrate each step. The second, Brazilian federal legislation, is the specialist domain on which usefulness is measured. All runs used one consumer GPU under a 500 W power cap, one run at a time.

The tutorial is organised in the order in which a reader would work. Section 2 places it among comparable resources. Section 3 states what is needed in hardware, data and time. Section 4 gives the two training objectives. Section 5 builds the model step by step, with a note on adapting each step to another domain. Section 6 explains how to evaluate the model, which is the centre of the tutorial. Section 7 explains how to compare configurations so that a difference can be trusted. Section 8 reports the worked example. Section 9 lists the checks that kept defects out of the numbers, and Section 10 gathers the steps to move the pipeline to a new domain.

References (45)