A causal language model predicts the next token from the tokens that precede it. Repeating this step generates a sequence, making the model an autocomplete system for text. section 16.3.1
A high-level CausalLM combines a tokenizer, preprocessing, and a pretrained backbone. KerasHub can load a matching architecture and tokenizer, while generate() handles autoregressive text generation. section 16.3.1
Generating text with Gemma
Instantiate Gemma, choose greedy sampling, and continue a prompt: gemma_lm$generate("A piece of advice", max_length = 40L). Prompts can produce plausible continuations, such as brownie instructions, but unrestricted pretraining mainly produces likely text rather than reliably useful answers.
Instruction fine-tuning adapts the same next-token mechanism to prompts and responses. A template marks [instruction], [end], and [response]; training computes loss mainly on response tokens, excluding the fixed prompt and padding. section 16.3.2
preprocessor <- gemma_lm$preprocessor preprocessor$sequence_length <- 512L batch <- iter_next(as_iterator(train_ds)) .[x, y, sample_weight] <- preprocessor(batch) str(x) List of 2 $ token_ids : <jax.Array shape(2, 512), dtype = int32> $ padding_mask: <jax.Array shape(2, 512), dtype = bool> str(y) <jax.Array shape(2, 512), dtype = int32> str(sample_weight) <jax.Array shape(2, 512), dtype = bool>
Inspect the preprocessor outputs to see token IDs, next-token labels, padding masks, and response-focused sample weights. section 16.3.2


