afnio.optim.tgd
afnio.optim.tgd.TGD
Bases: Optimizer
Textual Gradient Descent (TGD) optimizer.
TGD is an optimization algorithm for language-model–based systems where gradients are represented and propagated as natural language feedback rather than numerical tensors. Instead of computing numerical derivatives, TGD relies on a language model to generate textual critiques (gradients) that are used to iteratively refine prompt-based parameters.
This implementation follows the ideas introduced in the TextGrad paper, which proposes treating language-model feedback as a differentiable signal for optimizing textual variables and prompt programs.
TGD operates over Variable objects and consumes textual
gradients produced by the automatic differentiation process. These gradients
are used to update the optimized variables, with optional momentum applied
to recent gradient history to stabilize and accelerate optimization.
Parameters are organized into parameter groups, similar to optimizers in
PyTorch. This allows different optimization settings—such as optimization
meta-prompts (messages), constraints, and momentum—to be applied
consistently across groups.
References:
- TextGrad: Automatic Differentiation via Large Language Models https://arxiv.org/abs/2406.07496
Source code in afnio/optim/tgd.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 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 | |
__init__(params, model_client, messages=TGD_MESSAGES, inputs=None, constraints=None, momentum=0, **completion_args)
Initialize the Textual Gradient Descent (TGD) optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
iterable
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
model_client
|
ChatCompletionModel | None
|
LM model client used for optimization. |
required |
messages
|
MultiTurnMessages
|
Messages for multi-turn interactions. It typically defines the optimizer system prompt and user instruction. In-context examples (shots) can be added as well. |
TGD_MESSAGES
|
inputs
|
dict[str, str | Variable] | None
|
Dynamic values to fill placeholders within message templates |
None
|
constraints
|
list[str | Variable] | None
|
A list of natural language constraints for optimization. |
None
|
momentum
|
int
|
Momentum window size. Tracks the last |
0
|
completion_args
|
dict[str, Any]
|
Additional arguments to pass to the model client when generating text completions. Defaults to an empty dictionary. |
{}
|
Source code in afnio/optim/tgd.py
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 | |
step(closure=None)
Performs a single optimization step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
closure
|
Callable | None
|
A closure that reevaluates the model and returns the loss. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Variable, Variable] | None
|
The loss if |
Source code in afnio/optim/tgd.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
afnio.optim.tgd.tgd(params, grads, momentum_buffer_list, model_client, messages, inputs, constraints, momentum, **completion_args)
Functional API that performs TGD (Textual Gradient Descent) algorithm computation.
See TGD for details.
Source code in afnio/optim/tgd.py
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 | |