vmap
Vmap expression for data-parallel operations.
This module provides symbolic support for JAX's vmap (vectorized map) operation, enabling efficient data-parallel computations over batched data within the symbolic expression framework.
Vmap supports multiple modes based on the type of batch:
- Constant/array: Values baked into the compiled function at trace time, equivalent to closure-captured values in BYOF. Use for static data.
- Parameter: Values looked up from params dict at runtime, allowing updates between SCP iterations. Use for data that may change.
- State: Values extracted from the unified state vector at runtime, enabling vectorized operations over state elements (e.g., multi-agent).
- Control: Values extracted from the unified control vector at runtime, enabling vectorized operations over control elements.
Vmap also supports batching over multiple arguments by passing a list of batch sources. Each batch source is mapped to a corresponding lambda argument.
See the :class:Vmap class documentation for usage examples.
Vmap
¶
Bases: Expr
Vectorized map over batched data in symbolic expressions.
Vmap enables data-parallel operations by applying a symbolic expression to each element of a batched array (or multiple arrays). This is the symbolic equivalent of JAX's jax.vmap, allowing efficient vectorized computation without explicit loops.
The expression is defined via a lambda that receives one or more Placeholder arguments, each representing a single element from the corresponding batch. During lowering, this becomes a jax.vmap call.
The behavior depends on the type of each batch element:
- numpy array or Constant: Data is baked into the compiled function at trace time, equivalent to closure-captured values in BYOF.
- Parameter: Data is looked up from the params dict at runtime, allowing the same compiled code to be reused with different values.
- State: Data is extracted from the unified state vector at runtime, enabling vectorized operations over state elements (e.g., multi-agent).
- Control: Data is extracted from the unified control vector at runtime, enabling vectorized operations over control elements.
Attributes:
| Name | Type | Description |
|---|---|---|
_batches |
tuple
|
Tuple of data sources (Constant, Parameter, State, or Control) |
_axis |
int
|
The axis to vmap over (default: 0) |
_placeholders |
tuple
|
Tuple of placeholders used in the expression |
_child |
Expr
|
The expression tree built from the user's lambda |
_is_parameter |
tuple
|
Tuple of bools indicating which batches are Parameters |
_is_state |
tuple
|
Tuple of bools indicating which batches are States |
_is_control |
tuple
|
Tuple of bools indicating which batches are Controls |
Example
Compute distances to multiple reference points (baked-in)::
position = ox.State("position", shape=(3,))
init_poses = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
distances = ox.Vmap(
lambda pose: ox.linalg.Norm(position - pose),
batch=init_poses
)
# distances has shape (3,)
With runtime-updateable Parameter::
refs = ox.Parameter("refs", shape=(10, 3), value=init_poses)
dist_state = ox.State("dist_state", shape=(10,))
dynamics["dist_state"] = ox.Vmap(
lambda pose: ox.linalg.Norm(position - pose),
batch=refs
)
# Later, change the parameter value without recompiling:
problem.parameters["refs"] = new_poses
With multiple batch arguments::
obs_centers = ox.Parameter("obs_centers", shape=(100, 3))
obs_radii = ox.Parameter("obs_radii", shape=(100,))
constraints = ox.Vmap(
lambda center, radius: radius <= ox.linalg.Norm(position - center),
batch=[obs_centers, obs_radii]
)
# constraints has shape (100,)
With State batching (multi-agent)::
# State representing n_agents positions, each 3D
agent_positions = ox.State("agent_positions", shape=(n_agents, 3))
# Apply constraint to each agent's position
constraints = ox.Vmap(
lambda pos: ox.linalg.Norm(pos) <= max_distance,
batch=agent_positions
)
# constraints has shape (n_agents,)
With Control batching::
# Control representing n_thrusters, each scalar
thrusters = ox.Control("thrusters", shape=(n_thrusters,))
# Apply constraint to each thruster
constraints = ox.Vmap(
lambda t: t <= max_thrust,
batch=thrusters
)
# constraints has shape (n_thrusters,)
Batching over a non-default axis::
# Data shaped (features, n_samples) - batch over axis 1
samples = ox.Parameter("samples", shape=(3, n_samples), value=data)
results = ox.Vmap(
lambda sample: ox.linalg.Norm(sample),
batch=samples,
axis=1 # batch over samples, not features
)
# results has shape (n_samples,)
Note
- For static data that won't change, pass a numpy array or Constant to get closure-equivalent behavior (numerically identical to BYOF).
- For data that needs to be updated between iterations, use Parameter.
- For vectorized operations over state/control elements, pass State/Control.
- When using multiple batches, all must have the same size along the vmap axis.
Prefer Constants over Parameters
Use a raw numpy array or Constant unless you specifically need to update the vmap data between solves without recompiling.
Using a Parameter (runtime lookup) may produce different numerical results compared to using a Constant (baked-in), even when the underlying data is identical. This can manifest as:
- Different SCP iteration counts
- Different convergence behavior
- In unlucky cases, convergence to a different local solution
This is likely due to JAX/XLA trace and compilation differences between the two code paths. When data is baked in, JAX sees concrete values at trace time. When data is looked up from a params dict at runtime, JAX traces through the dictionary access, potentially producing different XLA compilation or floating-point operation ordering.
Source code in openscvx/symbolic/expr/vmap.py
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | |
axis: int
property
¶
The axis being vmapped over.
batch: Union[Constant, Parameter, State, Control]
property
¶
The first batched data source (for single-batch backward compatibility).
batches: Tuple[Union[Constant, Parameter, State, Control], ...]
property
¶
Tuple of batched data sources being vmapped over.
is_control: Tuple[bool, ...]
property
¶
Tuple of bools indicating which batches are Controls (control vector lookup).
is_parameter: Tuple[bool, ...]
property
¶
Tuple of bools indicating which batches are Parameters (runtime lookup).
is_state: Tuple[bool, ...]
property
¶
Tuple of bools indicating which batches are States (state vector lookup).
num_batches: int
property
¶
Number of batch arguments.
placeholder: _Placeholder
property
¶
The first placeholder (for single-batch backward compatibility).
placeholders: Tuple[_Placeholder, ...]
property
¶
Tuple of placeholders used in the inner expression.
__init__(fn: Callable[..., Expr], batch: Union[BatchSource, Sequence[BatchSource]], axis: int = 0)
¶
Initialize a Vmap expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Expr]
|
A callable (typically a lambda) that takes one or more Placeholder arguments and returns a symbolic expression. Each Placeholder represents a single element from the corresponding batched data. |
required |
batch
|
Union[BatchSource, Sequence[BatchSource]]
|
The batched data to vmap over. Can be: - A single batch source (numpy array, Constant, Parameter, State, or Control) - A list/tuple of batch sources for multi-argument vmapping Each batch source can be: - numpy array: baked into compiled function (closure-equivalent) - Constant: baked into compiled function (closure-equivalent) - Parameter: looked up from params dict at runtime - State: extracted from unified state vector at runtime - Control: extracted from unified control vector at runtime |
required |
axis
|
int
|
The axis to vmap over. Default is 0 (first axis). Applied to all batch sources. |
0
|
Example
Single batch (baked-in data)::
ox.Vmap(lambda x: ox.linalg.Norm(x), batch=points)
Single batch with Parameter::
refs = ox.Parameter("refs", shape=(10, 3), value=points)
ox.Vmap(lambda ref: ox.linalg.Norm(position - ref), batch=refs)
Multiple batches::
centers = ox.Parameter("centers", shape=(100, 3))
radii = ox.Parameter("radii", shape=(100,))
ox.Vmap(
lambda c, r: r <= ox.linalg.Norm(position - c),
batch=[centers, radii]
)
State batching (multi-agent)::
agent_positions = ox.State("positions", shape=(n_agents, 3))
ox.Vmap(lambda pos: g(pos), batch=agent_positions)
Non-default axis::
# Batch over axis 1 instead of axis 0
data = ox.Parameter("data", shape=(3, n_samples))
ox.Vmap(lambda x: f(x), batch=data, axis=1)
Source code in openscvx/symbolic/expr/vmap.py
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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
canonicalize() -> Expr
¶
Canonicalize by canonicalizing the child expression.
Returns:
| Name | Type | Description |
|---|---|---|
Vmap |
Expr
|
A new Vmap with canonicalized child expression |
Source code in openscvx/symbolic/expr/vmap.py
check_shape() -> Tuple[int, ...]
¶
Compute the output shape of the vmapped expression.
The output shape is (batch_size,) + inner_shape, where batch_size is the size of the vmap axis and inner_shape is the shape of the child expression.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
Output shape after vmapping |
Example
If data has shape (10, 3) and the inner expression produces a scalar (shape ()), the output shape is (10,).
Source code in openscvx/symbolic/expr/vmap.py
children() -> List[Expr]
¶
Return child expressions.
Returns:
| Name | Type | Description |
|---|---|---|
list |
List[Expr]
|
The vmapped expression and any Parameter/State/Control data sources. These are included so traverse() finds them for parameter/variable collection in preprocessing. |