DeAL
aisteer360.algorithms.output_control.deal
args
control
DeAL
Bases: OutputControl
Implementation of DeAL (Decoding-time Alignment) from Deng et al., 2024.
DeAL performs controlled text generation through iterative lookahead search and reward-guided beam selection. Unlike training-time alignment methods, DeAL operates purely at inference time to steer language model outputs toward desired behaviors.
The algorithm works in three phases:
-
Lookahead Generation: Generate multiple candidate continuations using beam search from the current context.
-
Reward-based Scoring: Evaluate each candidate continuation using a provided reward function that measures alignment with the desired objective (e.g., helpfulness, safety).
-
Iterative Refinement: Select the top-k highest-scoring beams and repeat the process until termination conditions are met (EOS token, max length, or max iterations reached).
This approach allows for flexible alignment with various objectives without requiring model retraining or fine-tuning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reward_func
|
Callable
|
Function that scores generated continuations. Should accept (prompt: str, continuations: list[str], reward_params: dict) and return list[float]. |
required |
lookahead
|
int
|
Number of tokens to generate in each lookahead step. Defaults to 4. |
required |
init_beams
|
int
|
Number of initial beams to generate at each iteration. Defaults to 8. |
required |
topk
|
int
|
Number of top-scoring beams to retain for the next iteration. Defaults to 4. |
required |
max_iterations
|
int
|
Maximum number of search iterations before termination. Defaults to 10. |
required |
Reference:
- "DeAL: Decoding-time Alignment for Large Language Models" James Y. Huang, Sailik Sengupta, Daniele Bonadiman, Yi-an Lai, Arshit Gupta, Nikolaos Pappas, Saab Mansour, Katrin Kirchhoff, Dan Roth https://arxiv.org/abs/2402.06147
Source code in aisteer360/algorithms/output_control/deal/control.py
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 |
|
args = self.Args.validate(*args, **kwargs)
instance-attribute
base_generate = None
class-attribute
instance-attribute
enabled = True
class-attribute
instance-attribute
model = None
class-attribute
instance-attribute
tokenizer = None
class-attribute
instance-attribute
generate(input_ids, attention_mask, runtime_kwargs, model, **gen_kwargs)
Execute guided generation with iterative lookahead search and reward-based selection. Returns the highest-scoring generation.
The generation process is as follows:
- Generate
init_beams
candidate continuations oflookahead
tokens each - Score all candidates using the provided reward function
- Select top-k highest scoring beams
- Check termination conditions (EOS, max length, max iterations)
- If not terminated, continue from the selected beams
- Return the highest-scoring complete generation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
Tensor
|
Input token IDs of shape [1, seq_len]. Currently only supports single prompts (batch size must be 1). |
required |
attention_mask
|
Tensor
|
Attention mask matching input_ids shape. Automatically recomputed during iteration based on padding tokens. |
required |
runtime_kwargs
|
dict | None
|
Runtime parameters including:
|
required |
model
|
PreTrainedModel
|
The language model used for generation. Must match the model provided during steer(). |
required |
**gen_kwargs
|
Generation parameters passed to the underlying model.generate().
Note: |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Generated token IDs of shape [1, output_len] or [output_len]. Contains the highest-scoring complete generation found during search. |
Raises:
Type | Description |
---|---|
ValueError
|
If base_generate is not callable |
NotImplementedError
|
If input has batch size > 1 (multiple prompts not supported) |
RuntimeError
|
If reward function returns incorrect number of scores |
Source code in aisteer360/algorithms/output_control/deal/control.py
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 |
|
steer(model, tokenizer=None, **_)
Lightweight preparation; attaches model, tokenizer, and generate to instance.
Source code in aisteer360/algorithms/output_control/deal/control.py
57 58 59 60 61 62 63 64 65 66 67 |
|