modeling
Model
Base class for all LLaMPPL models.
Your models should subclass this class. Minimally, you should provide an __init__
method
that calls super().__init__(self)
, and a step
method.
Source code in hfppl/modeling.py
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 |
|
condition(b)
Constrain a given Boolean expression to be True
.
If the condition is False, the particle's weight is set to zero and self.finish()
is called, so that no further step
calls are made.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b
|
the Boolean expression whose value is constrained to be True. |
required |
Source code in hfppl/modeling.py
immutable_properties()
Return a set[str]
of properties that LLaMPPL may assume do not change during execution of step
.
This set is empty by default but can be overridden by subclasses to speed up inference.
Returns:
Name | Type | Description |
---|---|---|
properties |
set[str]
|
a set of immutable property names |
Source code in hfppl/modeling.py
intervene(dist, x)
async
Force the distribution to take on the value x
, but do not condition on this result.
This is useful primarily with distributions that have side effects (e.g., modifying some state). For example, a model with the code
token_1 = await self.sample(self.stateful_lm.next_token())
await self.observe(self.stateful_lm.next_token(), token_2)
encodes a posterior inference problem, to find token_1
values that likely preceded token_2
. By contrast,
token_1 = await self.sample(stateful_lm.next_token())
await self.intervene(self.stateful_lm.next_token(), token_2)
encodes a much easier task: freely generate token_1
and then force-feed token_2
as the following token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dist
|
Distribution
|
the distribution on which to intervene. |
required |
x
|
the value to intervene with. |
required |
Source code in hfppl/modeling.py
observe(dist, x)
async
Condition the model on the value x
being sampled from the distribution dist
.
For discrete distributions dist
, await self.observe(dist, x)
specifies the same constraint as
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dist
|
a |
required | |
x
|
the value observed from |
required |
Source code in hfppl/modeling.py
sample(dist, proposal=None)
async
Extend the model with a sample from a given Distribution
, with support for autobatching.
If specified, the Distribution proposal
is used during inference to generate informed hypotheses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dist
|
the |
required | |
proposal
|
if provided, inference algorithms will use this |
None
|
Returns:
Name | Type | Description |
---|---|---|
value |
the value sampled from the distribution. |
Source code in hfppl/modeling.py
score(score)
Multiply this particle's weight by exp(score)
.
The score
method is a low-level way to change the target distribution.
For many use cases, it is sufficient to use sample
, observe
, condition
,
and twist
, all of which are implemented in terms of score
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
score
|
logarithm of the amount by which the particle's weight should be multiplied. |
required |
Source code in hfppl/modeling.py
step()
async
Defines the computation performed in each step of the model.
All subclasses should override this method.
string_for_serialization()
Return a string representation of the particle for serialization purposes.
Returns:
Name | Type | Description |
---|---|---|
str |
a string representation of the particle. |
twist(amt)
Multiply this particle's weight by exp(amt)
, but divide it back out before the next step
.
Use this method to provide heuristic guidance about whether a particle is "on the right track" without changing the ultimate target distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
amt
|
the logarithm of the amount by which to (temporarily) multiply this particle's weight. |
required |
Source code in hfppl/modeling.py
submodel(f)
Decorator to create a SubModel implementation from an async function.
For example:
@submodel
async def sample_two_tokens(self, context):
token1 = await self.sample(context.next_token())
token2 = await self.sample(context.next_token())
return token1, token2
This SubModel can then be used from another model or submodel, using the syntax await self.call(sample_two_tokens(context))
.