liveBook · Manning

Model architecture

As explained in LLMs in Production

A model architecture is the structural design of a machine-learning model, including its computational components and their connections.

A model architecture is the blueprint defining a model’s layers, component types, and connections. For language models, choices depend on the task, data scale, and available compute. section 5.2.1

Architecture choices shape both capability and training practicality:

  • Feed-forward layers provide a simple starting point; attention and Transformer components support modern language-model designs. section 5.2.1 section 2.5
  • A larger or more sophisticated architecture does not guarantee better results; data quality and size can matter more than architecture or the training loop. section 5.5
  • Architecture decisions interact with memory, speed, model size, tokenization, loss functions, and the training loop. section 5.5 section 9.1.3

A deliberately simple language model

A compact implementation can combine token embeddings with two linear layers and a ReLU activation, then predict vocabulary tokens using cross-entropy loss. This baseline makes later architectural changes visible rather than hiding them inside a large model. section 9.1.3

Listing 9.3 Simple model and training loop
class SimpleFeedForwardNN(nn.Module):
    def __init__(self, config=MASTER_CONFIG):
        super().__init__()
        self.config = config

        self.embedding = nn.Embedding(
            config["vocab_size"], config["d_model"]
        )
        self.linear = nn.Sequential(
            nn.Linear(config["d_model"], config["d_model"]),
            nn.ReLU(),
            nn.Linear(config["d_model"], config["vocab_size"]),
        )

        print(
            f"model params: {sum([m.numel() for m in self.parameters()])}"
        )

    def forward(self, idx, targets=None):
        x = self.embedding(idx)
        logits = self.linear(x)

        if targets is not None:
            loss = F.cross_entropy(
                logits.view(-1, self.config["vocab_size"]),
                targets.view(-1),
                ignore_index=tokenizer.pad_token_id,
                # reduction="sum",
            )
            return logits, loss

        else:
            return logits


model = SimpleFeedForwardNN(MASTER_CONFIG).to(device)
opt_model = torch.compile(model)           #1

Inspect the minimal PyTorch model to see embeddings, linear layers, activation, vocabulary logits, and padding-aware loss in one implementation. section 9.1.3

How LLMs in Production covers it

How deep it goes
Defines layers and connections, demonstrates a small implementation, and stops short of deeply explaining modern architecture changes.
Examples
Uses a two-layer feed-forward network, a GPT-style module, and a small text-generation training run as concrete architecture examples.
What you can do after
Build it from scratch by selecting components, connecting them, defining loss, and integrating the result into a training loop.
How it connects
Connects architecture with Transformers, attention, feed-forward layers, tokenization, loss functions, and training loops.

Also explained in

Compare how 13 books explain model architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.