Search Streams
Summary
delphyne.core.streams
The Search Stream Protocol.
In order to allow the composition of heterogeneous search policies and prompting policies, a standard protocol is defined for defining resource-aware search iterators.
A search stream consists in an iterator that yields three kinds of messages:
Solution
messages indicating that a solution has been found.Barrier
messages asking authorization to spend a given amount of resources (for which an over-estimate is provided).Spent
messages reporting actual resource spending.
The following invariants and guarantees must be offered and preserved by stream combinators:
Invariants
Barrier
andSpent
messages come in pairs and are associated using shared identifiers. Because search streams can spawn mulitple threads internally, multipleBarrier
messages can be simultaneously pending (i.e., in waiting of a matchingSpent
message).- A stream must eventually terminate if all spending requests are denied
(this is why
loop
has astop_on_reject
argument). - A stream can be interrupted before exhaustion (and be later garbage
collected), provided that no
Barrier
messages are pending (i.e., an identical number ofBarrier
andSpent
messages has been seen so far). If this condition does not hold, some actual resource spending might be unreported.
Warning
Manually implementing the search stream protocol by yielding
Barrier
and Spent
messages is error-prone. Standard stream
combinators should usually be used instead (see Stream
class from
the standard library).
Definitions
Budget
dataclass
An immutable datastructure for tracking spent budget as an infinite vector with finite support. Each dimension corresponds to a different metric (e.g., number of requests, price in dollars...)
Attributes:
Name | Type | Description |
---|---|---|
values |
Mapping[str, float | int]
|
a mapping from metrics to spent budget. Metrics outside of this field are associated a spending of 0. |
Note
We explicitly use type float | int
for metrics, despite int
being treated as a subtype of float
by most Python type
checkers. This is to avoid pydantic mistakenly converting
integers into floats when parsing serialized budget data.
Source code in src/delphyne/core/streams.py
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 |
|
BudgetLimit
dataclass
An immutable datastructure for representing a budget limit as an infinite vector with finite support.
Elements outside of the finite support are associated an
infinite limit. Hence the separation of Budget
and
BudgetLimit
.
Source code in src/delphyne/core/streams.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
AbstractStream
Bases: ABC
Base class for search streams.
A search stream must be capable of producing a search stream
generator. The standard library contains a subclass with more
features (Stream
).
Source code in src/delphyne/core/streams.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
gen
abstractmethod
gen() -> StreamGen[T]
Produce a search stream generator, i.e. an iterator that yields
Barrier
and Spent
messages along with solutions.
Source code in src/delphyne/core/streams.py
255 256 257 258 259 260 261 |
|
StreamGen
A search stream generator.
See delphyne.core.streams for more explanations about the search stream protocol.
Solution
dataclass
A solution yielded by a search stream, which combines a tracked value with optional metadata.
Attributes:
Name | Type | Description |
---|---|---|
tracked |
Tracked[T]
|
A tracked value. |
meta |
SearchMeta | None
|
Optional metadata. |
Source code in src/delphyne/core/streams.py
149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
Barrier
dataclass
Ask authorization for spending a given budget amount.
Attributes:
Name | Type | Description |
---|---|---|
budget |
Budget
|
an over-estimate of how much budget will be spent if the request is granted. An inaccurate estimate can be provided by a policy, although more budget could be actually be spent than is intended in this case. |
allow |
bool
|
a boolean flag that can be set to |
id |
BarrierId
|
a unique identifier, which is shared by a unique associated
|
Warning
Manually yielding Spent
and Barrier
messages is error-prone
and usually not recommended. Use stream combinators instead (see
the Stream
class from the standard library).
Source code in src/delphyne/core/streams.py
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 |
|
Spent
dataclass
Indicate that an actual amount of resources has been spent.
Each Spent
message is associated with a unique prior Barrier
message that shares the same identifier.
Attributes:
Name | Type | Description |
---|---|---|
budget |
Budget
|
Amount of budget that was actually spent. |
barrier_id |
BarrierId
|
Identifier of the prior associated |
Warning
Manually yielding Spent
and Barrier
messages is error-prone
and usually not recommended. Use stream combinators instead (see
the Stream
class from the standard library).
Source code in src/delphyne/core/streams.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
BarrierId
BarrierId = int
A unique identifier associated with a Barrier
message, used to identify
a matching Spent
message. Overlapping barrier messages must not share
the same identifier (barrier messages are considered to overlap if the
second one occurs before the Spent
message associated with the first).
SearchMeta
Base class for valid search metadata.
Search metadata can be attached to all solutions yielded by a search
stream. See ProbInfo
for an example.
Source code in src/delphyne/core/streams.py
129 130 131 132 133 134 135 136 137 |
|