base
Potential
Bases: ABC
, PotentialOps
, PotentialTests
Abstract base class for potentials.
A Potential is a function that maps sequences of tokens in a vocabulary to non-negative real numbers (weights).
Potentials assign weights to sequences of tokens based on whether they are complete sequences or prefixes of complete sequences.
complete
: Assess the log weight of a sequence of tokens in the vocabulary as a complete sequence.prefix
: Assess the log weight of a sequence of tokens in the vocabulary as a prefix.
Potentials additionally implement a logw_next
method:
logw_next
: Compute the next-token log weights of each token in the vocabulary and a special EOS (end-of-sequence) token given a context.
Subclasses must minimally implement complete
and prefix
. logw_next
and batched versions of the above methods
come with default implementations, but may be overridden by subclasses for improved performance.
All Potentials must satisfy a set of properties which can be tested using PotentialTests.
Attributes:
Name | Type | Description |
---|---|---|
token_type |
TokenType
|
The type of tokens in the vocabulary. |
vocab |
list
|
List of tokens making up the vocabulary. |
eos |
EndOfSequence
|
Special token to use as end-of-sequence. |
vocab_eos |
list
|
List of tokens in |
lookup |
dict
|
Mapping from tokens and |
Source code in genlm/control/potential/base.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
__init__(vocabulary, token_type=None, eos=None)
Initialize the potential.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocabulary
|
list
|
List of tokens that make up the vocabulary. |
required |
token_type
|
TokenType
|
Optional TokenType of all elements of the vocabulary. If None, will be inferred from vocabulary. |
None
|
eos
|
EndOfSequence
|
Special token to use as end-of-sequence. Defaults to |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If vocabulary is empty. |
TypeError
|
If vocabulary contains tokens which are not of |
Source code in genlm/control/potential/base.py
complete(context)
abstractmethod
async
Assess the weight of context
as a complete sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
Type | Description |
---|---|
float
|
Log weight of the context under the language. |
Source code in genlm/control/potential/base.py
prefix(context)
abstractmethod
async
Assess the weight of context
as a prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
Type | Description |
---|---|
float
|
Log weight of the context as a prefix. |
Source code in genlm/control/potential/base.py
score(context)
async
Assess the weight of context
based on EOS-termination.
This is a convenience method which dispatches to complete
if context
ends with self.eos
, otherwise to prefix
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
Type | Description |
---|---|
float
|
Log weight of the context, either as a prefix or complete sequence. |
Source code in genlm/control/potential/base.py
logw_next(context)
async
Compute the next-token weights of each token in self.vocab_eos
given context
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
Type | Description |
---|---|
LazyWeights
|
Weights of each token in the vocabulary and EOS. |
Source code in genlm/control/potential/base.py
batch_complete(contexts)
async
Batched equivalent to complete
.
Assess the weight of each context as a complete sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
Type | Description |
---|---|
array
|
Array of log weights for each context. |
Source code in genlm/control/potential/base.py
batch_prefix(contexts)
async
Batched equivalent to prefix
.
Assess the weight of each context as a prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
Type | Description |
---|---|
array
|
Array of log weights for each context. |
Source code in genlm/control/potential/base.py
batch_score(contexts)
async
Batched equivalent to score
.
Assess the weight of each context based on EOS-termination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
Type | Description |
---|---|
array
|
Array of log weights for each context. |
Source code in genlm/control/potential/base.py
batch_logw_next(contexts)
async
Batched equivalent to logw_next
.
Computes the next-token weights of each token in self.vocab_eos
given each context in the batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
Type | Description |
---|---|
list
|
List of LazyWeights objects, one for each context. |
Raises:
Type | Description |
---|---|
ValueError
|
If any context has zero weight (log weight of -inf) under |
Source code in genlm/control/potential/base.py
make_lazy_weights(weights, log=True)
Helper method to create a LazyWeights object over the potential's vocabulary and EOS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weights
|
array
|
Array of weights. |
required |
log
|
bool
|
Whether the weights are in log space. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
LazyWeights
|
LazyWeights object defined over |
Source code in genlm/control/potential/base.py
alloc_logws(default=float('-inf'))
Allocate a new array of log weights for the potential's vocabulary and EOS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default
|
float
|
Default log weight. Defaults to -inf. |
float('-inf')
|
Returns:
Type | Description |
---|---|
array
|
Array of length |
Source code in genlm/control/potential/base.py
spawn()
Spawn a fresh instance of the potential.
This method is not required by default, but may be implemented by subclasses
to support CPU-parallelism using (MultiProcPotential
)[genlm.control.potential.multi_proc.MultiProcPotential].