You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
Vishal V
authored and
Vishal V
committed
fix(sampling): make sample final and add sample_index to majority-vote hook payloads
BaseMBRDSampling._sample fans out number_of_samples concurrent calls to
BaseSamplingStrategy._sample, each receiving the same sampling_id and
starting their own _subsample_iteration loop from subsample_index=0.
This caused all branches to emit the same iteration numbers under one
sampling_id, making (sampling_id, iteration) non-unique for consumers
of SAMPLING_ITERATION and SAMPLING_REPAIR hooks.
Fix by introducing sample_index: int | None = None on both payload
classes and threading it through the emit helpers and _subsample_iteration
so each fan-out branch carries a distinct 0-based ordinal. Non-fan-out
strategies leave sample_index=None; no existing call sites change.
- Renamed _sample_impl to _sample
- Marked sample method as @Final enforces that subclasses override _sample
rather than sample()
- Add sample_index field to SamplingIterationPayload and
SamplingRepairPayload (mellea/plugins/hooks/sampling.py)
- Add sample_index kwarg to _emit_sampling_iteration and
_emit_sampling_repair, forwarded to the payload
(mellea/core/sampling.py)
- Add sample_index param to BaseSamplingStrategy._sample and
_subsample_iteration; forward to both _emit_* calls
(mellea/stdlib/sampling/base.py)
- Pass sample_index=i in the BaseMBRDSampling fan-out loop
(mellea/stdlib/sampling/majority_voting.py)
- Emit mellea.sampling.sample_index span-event attribute in
SamplingTracingPlugin.on_iteration and on_repair when not None
(mellea/telemetry/tracing_plugins.py)
Tests added:
- test_majority_vote_iteration_sample_index_is_unique: e2e regression
proving (sampling_id, sample_index, iteration) is unique across all
branches with number_of_samples=3, loop_budget=2
- test_majority_vote_repair_sample_index_matches_branch: repair events
carry the same sample_index as the failed iteration that triggered them
- test_sample_index_defaults_to_none / test_sample_index_construction
on both payload classes
- test_sampling_iteration_includes/omits_sample_index_when_set/none
- test_sampling_repair_includes/omits_sample_index_when_set/none
Assisted-by: IBM Bob
Signed-off-by: Vishal V <VishalV@ibm.com>
Copy file name to clipboardExpand all lines: docs/docs/community/building-extensions.md
+21-13Lines changed: 21 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,11 +13,11 @@ Components, Sampling Strategies, and Backends without modifying the core library
13
13
14
14
Choose the pathway that fits the scope of your work:
15
15
16
-
| Pathway | When to use |
17
-
| ------- | ----------- |
18
-
|**Core repository**| General-purpose additions that benefit all users — open an issue first to discuss placement |
19
-
|**Your own repo** (`mellea-` prefix) | Application-specific or domain-specific libraries |
20
-
|**[mellea-contribs](https://github.com/generative-computing/mellea-contribs)**| Experimental or specialized components not yet ready for the standard library |
|**Core repository**| General-purpose additions that benefit all users — open an issue first to discuss placement |
19
+
|**Your own repo** (`mellea-` prefix) | Application-specific or domain-specific libraries|
20
+
|**[mellea-contribs](https://github.com/generative-computing/mellea-contribs)**| Experimental or specialized components not yet ready for the standard library |
21
21
22
22
> **Note:** For general-purpose Components, Requirements, or Sampling Strategies,
23
23
> open an issue before submitting a PR. This avoids duplication and ensures
@@ -143,12 +143,14 @@ For a full walkthrough of the Component protocol and templating system, see
143
143
144
144
A [`SamplingStrategy`](../reference/glossary#sampling-strategy) controls how Mellea
145
145
generates and validates outputs — for example, rejection sampling, best-of-n, or
146
-
beam search. Subclass `SamplingStrategy` and implement `sample`:
146
+
beam search. Subclass `SamplingStrategy` and implement `_sample`:
147
+
148
+
> **Note:**`sample()` is `@final` — it owns the sampling lifecycle (hooks, budget
149
+
> management). Override `_sample()` instead; `sample()` calls it automatically.
147
150
148
151
```python
149
-
import asyncio
150
152
from mellea.core.backend import Backend
151
-
from mellea.core.base import Component, Context, ModelOutputThunk, S
153
+
from mellea.core.base importCBlock, Component, Context, ModelOutputThunk, S
152
154
from mellea.core.requirement import Requirement
153
155
from mellea.core.sampling import SamplingResult, SamplingStrategy
154
156
@@ -164,36 +166,42 @@ class BestOfNStrategy(SamplingStrategy):
164
166
"""
165
167
self.n = n
166
168
167
-
asyncdefsample(
169
+
asyncdef_sample(
168
170
self,
169
-
action: Component[S],
171
+
action: Component[S]| CBlock | ModelOutputThunk,
170
172
context: Context,
171
173
backend: Backend,
172
-
requirements: list[Requirement]|None,
174
+
requirements: list[Requirement],
173
175
*,
176
+
effective_loop_budget: int,
174
177
validation_ctx: Context |None=None,
175
178
format: type|None=None,
176
179
model_options: dict|None=None,
177
180
tool_calls: bool=False,
181
+
sampling_id: str,
182
+
**kwargs,
178
183
) -> SamplingResult[S]:
179
184
"""Generate N candidates and return the best one.
180
185
181
186
Args:
182
-
action: The component to generate a response for.
187
+
action: The component, block, or thunk to generate a response for.
183
188
context: The current session context.
184
189
backend: The backend used for generation.
185
190
requirements: Requirements to validate each candidate against.
191
+
effective_loop_budget: Loop budget after hook modification.
186
192
validation_ctx: Optional context override for validation.
187
193
format: Structured output format, if any.
188
194
model_options: Model options to pass to the backend.
189
195
tool_calls: Whether to enable tool calls during generation.
196
+
sampling_id: UUID correlating iteration/repair/end hooks to this loop.
0 commit comments