Returns compiled XLA programs for the given inference runner. Args: inferencer_config: The inference runner config. input_batch_spec: A nested TensorSpec of input batch. topology: A string representing the TPU topology, e.g., "v4-8". Must be a key in USER_FAC
(
inferencer_config: InferenceRunner.Config,
*,
input_batch_spec: Nested[TensorSpec],
topology: str,
topology_num_slices: int = 1,
compiler_options: Optional[Dict[str, Union[str, bool]]] = None,
method: str = "sample_decode",
)
| 412 | |
| 413 | |
| 414 | def compile_inference_programs( |
| 415 | inferencer_config: InferenceRunner.Config, |
| 416 | *, |
| 417 | input_batch_spec: Nested[TensorSpec], |
| 418 | topology: str, |
| 419 | topology_num_slices: int = 1, |
| 420 | compiler_options: Optional[Dict[str, Union[str, bool]]] = None, |
| 421 | method: str = "sample_decode", |
| 422 | ) -> Dict[str, jax.stages.Compiled]: |
| 423 | """Returns compiled XLA programs for the given inference runner. |
| 424 | |
| 425 | Args: |
| 426 | inferencer_config: The inference runner config. |
| 427 | input_batch_spec: A nested TensorSpec of input batch. |
| 428 | topology: A string representing the TPU topology, e.g., "v4-8". Must be a key in |
| 429 | USER_FACING_NAME_TO_SYSTEM_CHARACTERISTICS. |
| 430 | If None, use CPU devices. |
| 431 | topology_num_slices: The number of TPU slices. |
| 432 | compiler_options: Options to pass to XLA. See `compiler_options.py` for examples. |
| 433 | method: The method name to compile. |
| 434 | |
| 435 | Returns: |
| 436 | A dict containing the following programs: |
| 437 | * "sample_decode": a program to run a sample_decode loop. |
| 438 | |
| 439 | Raises: |
| 440 | NotImplementedError: if `topology` is not in USER_FACING_NAME_TO_SYSTEM_CHARACTERISTICS. |
| 441 | """ |
| 442 | topology_devices, devices_per_slice = get_devices_for_topology(topology, topology_num_slices) |
| 443 | |
| 444 | cfg = inferencer_config.clone() |
| 445 | cfg.mesh_axis_names = cfg.mesh_axis_names or ("data", "model") |
| 446 | |
| 447 | # Use a default mesh_shape if None or REQUIRED. |
| 448 | cfg.mesh_shape = cfg.mesh_shape or [len(topology_devices)] + [1] * ( |
| 449 | len(cfg.mesh_axis_names) - 1 |
| 450 | ) |
| 451 | |
| 452 | topology_devices, mesh_shape = reshape_devices( |
| 453 | devices=topology_devices, |
| 454 | mesh_shape=cfg.mesh_shape, |
| 455 | devices_per_slice=devices_per_slice, |
| 456 | num_slices=topology_num_slices, |
| 457 | ) |
| 458 | cfg.mesh_shape = mesh_shape |
| 459 | |
| 460 | inferencer: InferenceRunner = cfg.instantiate( |
| 461 | parent=None, |
| 462 | devices=topology_devices, |
| 463 | inference_runner_state=True, |
| 464 | ) |
| 465 | |
| 466 | method_runner = inferencer.create_method_runner(method=method) |
| 467 | |
| 468 | with inferencer.mesh(): |
| 469 | jitted_fn = cast( |
| 470 | partial, |
| 471 | method_runner._jit_run_on_batch, # pylint: disable=protected-access |
nothing calls this directly
no test coverage detected