Initialize the DeepSpeed InferenceEngine. Description: all four cases are valid and supported in DS init_inference() API. # Case 1: user provides no config and no kwargs. Default config will be used. .. code-block:: python generator.model = deepspeed.init_inference(generator.
(model: torch.nn.Module,
config: Optional[Union[str, Dict[str, Any]]] = None,
**kwargs: Any)
| 326 | |
| 327 | |
| 328 | def init_inference(model: torch.nn.Module, |
| 329 | config: Optional[Union[str, Dict[str, Any]]] = None, |
| 330 | **kwargs: Any) -> InferenceEngine: |
| 331 | """Initialize the DeepSpeed InferenceEngine. |
| 332 | |
| 333 | Description: all four cases are valid and supported in DS init_inference() API. |
| 334 | |
| 335 | # Case 1: user provides no config and no kwargs. Default config will be used. |
| 336 | |
| 337 | .. code-block:: python |
| 338 | |
| 339 | generator.model = deepspeed.init_inference(generator.model) |
| 340 | string = generator("DeepSpeed is") |
| 341 | print(string) |
| 342 | |
| 343 | # Case 2: user provides a config and no kwargs. User supplied config will be used. |
| 344 | |
| 345 | .. code-block:: python |
| 346 | |
| 347 | generator.model = deepspeed.init_inference(generator.model, config=config) |
| 348 | string = generator("DeepSpeed is") |
| 349 | print(string) |
| 350 | |
| 351 | # Case 3: user provides no config and uses keyword arguments (kwargs) only. |
| 352 | |
| 353 | .. code-block:: python |
| 354 | |
| 355 | generator.model = deepspeed.init_inference(generator.model, |
| 356 | tensor_parallel={"tp_size": world_size}, |
| 357 | dtype=torch.half, |
| 358 | replace_with_kernel_inject=True) |
| 359 | string = generator("DeepSpeed is") |
| 360 | print(string) |
| 361 | |
| 362 | # Case 4: user provides config and keyword arguments (kwargs). Both config and kwargs are merged and kwargs take precedence. |
| 363 | |
| 364 | .. code-block:: python |
| 365 | |
| 366 | generator.model = deepspeed.init_inference(generator.model, config={"dtype": torch.half}, replace_with_kernel_inject=True) |
| 367 | string = generator("DeepSpeed is") |
| 368 | print(string) |
| 369 | |
| 370 | Arguments: |
| 371 | model: Required: original nn.module object without any wrappers |
| 372 | |
| 373 | config: Optional: instead of arguments, you can pass in a DS inference config dict or path to JSON file |
| 374 | |
| 375 | Returns: |
| 376 | A deepspeed.InferenceEngine wrapped model. |
| 377 | """ |
| 378 | log_dist("DeepSpeed info: version={}, git-hash={}, git-branch={}".format(__version__, __git_hash__, |
| 379 | __git_branch__), |
| 380 | ranks=[0]) |
| 381 | |
| 382 | # Load config_dict from config first |
| 383 | if config is None: |
| 384 | config = {} |
| 385 | if isinstance(config, str): |
nothing calls this directly
no test coverage detected