lmcontext
LMContext
Represents a generation-in-progress from a language model.
The state tracks two pieces of information:
- A sequence of tokens — the ever-growing context for the language model.
- A current mask — a set of tokens that have not yet been ruled out as the next token.
Storing a mask enables sub-token generation: models can use LMContext
to sample
the next token in stages, first deciding, e.g., whether to use an upper-case or lower-case
first letter, and only later deciding which upper-case or lower-case token to generate.
The state of a LMContext
can be advanced in two ways:
- Sampling, observing, or intervening the
next_token()
distribution. This causes a token to be added to the growing sequence of tokens. Supports auto-batching. - Sampling, observing, or intervening the
mask_dist(mask)
distribution for a given mask (set of token ids). This changes the current mask.
Attributes:
Name | Type | Description |
---|---|---|
lm |
CachedCausalLM
|
the language model for which this is a context |
tokens |
list[int]
|
the underlying sequence of tokens, including prompt, in this context |
next_token_logprobs |
array
|
numpy array holding the log probabilities for the next token. Unlike the log probabilities reported by |
temp |
float
|
temeprature for next-token distribution (0 < temp < float('inf')) |
model_mask |
set[int]
|
set of tokens that have not been ruled out as the next token. This mask is managed by the |
show_prompt |
bool
|
controls whether the string representation of this |
Source code in hfppl/distributions/lmcontext.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
__init__(lm, prompt, temp=1.0, show_prompt=False, show_eos=True)
Create a new LMContext
with a given prompt and temperature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lm
|
CachedCausalLM
|
the language model for which this is a context. |
required |
prompt
|
str
|
a string with which to initialize the context. Will be tokenized using |
required |
temp
|
float
|
temeprature for next-token distribution (0 < temp < float('inf')) |
1.0
|
Source code in hfppl/distributions/lmcontext.py
mask_dist(mask)
Bernoulli distribution, with probability of True equal to the probability that the next token of this LMContext
belongs
to the given mask.
Sampling or observing from this distribution modifies the state of this LMContext
instance, so that
the next_token()
distribution either will (if True) or will not (if False) generate a token from
the given mask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
a |
required |
Source code in hfppl/distributions/lmcontext.py
next_token()
Distribution over the next token.
Sampling or observing from this distribution advances the state of this LMContext
instance.