implement_for¶
- class torchrl.implement_for(module_name: str | Callable[[], Any], from_version: str | None = None, to_version: str | None = None, *, class_method: bool = False, compilable: bool = False)[原始碼]¶
一個版本裝飾器,用於檢查版本相容性並實現函式。
如果指定的模組丟失或沒有合適的實現,裝飾函式的呼叫將導致顯式錯誤。在範圍交叉的情況下,將使用最後一次合適的實現。
此包裝器還可用於為同一函式實現不同的後端(例如,gym vs gymnasium,numpy vs jax-numpy 等)。
- 引數:
module_name (str 或 callable) – 檢查此名稱模組的版本(例如,“gym”)。如果提供了 callable,它應返回模組。
from_version – 實現相容的版本。可以是開放的(None)。
to_version – 實現不再相容的版本。可以是開放的(None)。
- 關鍵字引數:
class_method (bool, optional) – 如果為
True,則函式將被編寫為類方法。預設為False。compilable (bool, optional) – 如果為
False,則模組匯入僅在第一次呼叫被包裝函式時發生。如果為True,則在被包裝函式初始化時匯入模組。預設為False。
示例
>>> @implement_for("gym", "0.13", "0.14") >>> def fun(self, x): ... # Older gym versions will return x + 1 ... return x + 1 ... >>> @implement_for("gym", "0.14", "0.23") >>> def fun(self, x): ... # More recent gym versions will return x + 2 ... return x + 2 ... >>> @implement_for(lambda: import_module("gym"), "0.23", None) >>> def fun(self, x): ... # More recent gym versions will return x + 2 ... return x + 2 ... >>> @implement_for("gymnasium", None, "1.0.0") >>> def fun(self, x): ... # If gymnasium is to be used instead of gym, x+3 will be returned ... return x + 3 ...
這表明該函式與 gym 0.13+ 相容,但與 gym 0.14+ 不相容。
- classmethod reset(setters_dict: dict[str, implement_for] | None = None) None[原始碼]¶
重置 setter_dict 中的設定器。
- 引數:
setters_dict – 實現的副本。我們遍歷其值併為每個值呼叫
module_set()。