MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / enable_torch_proxy

Function enable_torch_proxy

python/paddle/compat/proxy.py:449–500  ·  view source on GitHub ↗

Enable the PyTorch compat by adding the TorchProxyMetaFinder to sys.meta_path. This allows importing 'torch' modules that are actually proxies to PaddlePaddle. Args: scope (str or Iterable[str], optional): Specific module or modules to enable PyTorch compat for. If

(
    *,
    scope: _ScopeType = None,
    blocked_modules: _ScopeType = None,
    backend: Literal["torch"] = "torch",
    silent: bool = False,
)

Source from the content-addressed store, hash-verified

447
448
449def enable_torch_proxy(
450 *,
451 scope: _ScopeType = None,
452 blocked_modules: _ScopeType = None,
453 backend: Literal["torch"] = "torch",
454 silent: bool = False,
455) -> None:
456 """
457 Enable the PyTorch compat by adding the TorchProxyMetaFinder to sys.meta_path.
458 This allows importing 'torch' modules that are actually proxies to PaddlePaddle.
459
460 Args:
461 scope (str or Iterable[str], optional): Specific module or modules to enable
462 PyTorch compat for. If None, enables PyTorch compat globally. Defaults to None.
463 blocked_modules (str or Iterable[str], optional): Specific module or modules to
464 exclude from PyTorch compat. Defaults to None.
465 backend (str, optional): The backend to enable compat for. Currently only
466 "torch" is supported. Defaults to "torch".
467 silent (bool, optional): If True, suppresses warnings about scope changes.
468 Defaults to False.
469
470 Example:
471 .. code-block:: pycon
472 :name: enable-compat-in-global-scope
473
474 >>> import paddle
475 >>> paddle.enable_compat() # Enable torch compat globally
476 >>> import torch # type: ignore[import-not-found] # This will import paddle as torch
477 >>> assert torch.sin is paddle.sin
478 >>> paddle.disable_compat() # Disable torch compat
479
480 .. code-block:: pycon
481 :name: enable-compat-in-specific-scope
482
483 >>> import paddle
484 >>> paddle.enable_compat(scope={"triton"}) # Enable torch compat for 'triton' module only
485 >>> import triton # type: ignore[import-untyped] # All `import torch` inside `triton` will proxy to paddle
486 >>> try:
487 ... import torch # type: ignore[import-not-found] # This will raise ModuleNotFoundError
488 ... except ModuleNotFoundError:
489 ... print("PyTorch compat is not enabled globally.")
490 >>> paddle.disable_compat() # Disable torch compat
491 """
492 assert backend == "torch", f"Unsupported backend: {backend}"
493 blocked_modules = _parse_scope(blocked_modules)
494 if blocked_modules is not None:
495 extend_torch_proxy_blocked_modules(blocked_modules)
496 scope = _parse_scope(scope)
497 _register_compat_override()
498 _swap_torch_modules_to_cache()
499 _modify_scope_of_torch_proxy(scope, silent=silent)
500 sys.meta_path.insert(0, TORCH_PROXY_FINDER)
501
502
503def disable_torch_proxy() -> None:

Callers 2

use_torch_proxy_guardFunction · 0.85
paddle_triton_funFunction · 0.85

Calls 6

_parse_scopeFunction · 0.85
insertMethod · 0.45

Tested by

no test coverage detected