torch.utils.rename_privateuse1_backend¶
- torch.utils.rename_privateuse1_backend(backend_name)[source][source]¶
重新命名 privateuse1 後端裝置,使其更方便在 PyTorch API 中用作裝置名稱。
步驟如下:
(在 C++ 中)為各種 torch 操作實現核心,並將它們註冊到 PrivateUse1 分發鍵。
(在 Python 中)呼叫 torch.utils.rename_privateuse1_backend(“foo”)
您現在可以在 Python 中將“foo”用作普通裝置字串。
注意:此 API 每個程序只能呼叫一次。在外部後端已設定後嘗試更改它將導致錯誤。
注意(AMP):如果您的裝置想要支援 AMP,您可以註冊一個自定義後端模組。該後端必須使用
torch._register_device_module("foo", BackendModule)註冊一個自定義後端模組。BackendModule 需要提供以下 API:get_amp_supported_dtype() -> List[torch.dtype]獲取您的“foo”裝置在 AMP 中支援的資料型別,也許“foo”裝置還支援一種額外的資料型別。
注意(隨機數):如果您的裝置想要支援設定種子,BackendModule 需要提供以下 API:
_is_in_bad_fork() -> bool如果當前處於 bad_fork 狀態,則返回True,否則返回False。manual_seed_all(seed int) -> None為您的裝置設定隨機數生成種子。device_count() -> int返回可用“foo”的數量。get_rng_state(device: Union[int, str, torch.device] = 'foo') -> Tensor返回一個 ByteTensor 列表,表示所有裝置的隨機數狀態。set_rng_state(new_state: Tensor, device: Union[int, str, torch.device] = 'foo') -> None設定指定“foo”裝置的隨機數生成器狀態。
還有一些通用函式:
is_available() -> bool返回一個布林值,指示“foo”當前是否可用。current_device() -> int返回當前選定裝置的索引。
有關更多詳細資訊,請參閱 https://pytorch.com.tw/tutorials/advanced/extend_dispatcher.html#get-a-dispatch-key-for-your-backend 有關現有示例,請參閱 https://github.com/bdhirsh/pytorch_open_registration_example
示例
>>> torch.utils.rename_privateuse1_backend("foo") # This will work, assuming that you've implemented the right C++ kernels # to implement torch.ones. >>> a = torch.ones(2, device="foo")