多程序處理¶
用於啟動和管理由函式或二進制檔案指定的 n 個工作程序副本的函式庫。
對於函式,它使用 torch.multiprocessing(因此也使用 Python multiprocessing)來產生/分岔工作程序。對於二進制檔案,它使用 Python subprocessing.Popen 來建立工作程序。
用法 1:將兩個訓練器作為函式啟動
from torch.distributed.elastic.multiprocessing import Std, start_processes
def trainer(a, b, c):
    pass # train
# runs two trainers
# LOCAL_RANK=0 trainer(1,2,3)
# LOCAL_RANK=1 trainer(4,5,6)
ctx = start_processes(
        name="trainer",
        entrypoint=trainer,
        args={0: (1,2,3), 1: (4,5,6)},
        envs={0: {"LOCAL_RANK": 0}, 1: {"LOCAL_RANK": 1}},
        log_dir="/tmp/foobar",
        redirects=Std.ALL, # write all worker stdout/stderr to a log file
        tee={0: Std.ERR}, # tee only local rank 0's stderr to console
      )
# waits for all copies of trainer to finish
ctx.wait()
用法 2:將 2 個回應工作程序作為二進制檔案啟動
# same as invoking
# echo hello
# echo world > stdout.log
ctx = start_processes(
        name="echo"
        entrypoint="echo",
        log_dir="/tmp/foobar",
        args={0: "hello", 1: "world"},
        redirects={1: Std.OUT},
       )
就像 torch.multiprocessing 一樣,函式 start_processes() 的回傳值是一個程序上下文 (api.PContext)。如果啟動的是函式,則會回傳 api.MultiprocessContext,如果啟動的是二進制檔案,則會回傳 api.SubprocessContext。兩者都是父類別 api.PContext 的特定實作。
啟動多個工作程序¶
- torch.distributed.elastic.multiprocessing.start_processes(name, entrypoint, args, envs, logs_specs, log_line_prefixes=None, start_method='spawn')[來源]¶
- 使用提供的選項啟動 - n個- entrypoint程序副本。- entrypoint可以是- Callable(函式)或- str(二進制檔案)。副本的數量由- args和- envs參數的項目數量決定,這些參數需要具有相同的鍵集。- args和- env參數是要傳遞給由副本索引(本地排名)映射的進入點的參數和環境變數。所有本地排名都必須被考慮在內。也就是說,鍵集應該是- {0,1,...,(nprocs-1)}。- 備註 - 當 - entrypoint是二進制檔案 (- str) 時,- args只能是字串。如果給定任何其他類型,則會將其轉換為字串表示形式(例如- str(arg1))。此外,只有在主要函式使用- torch.distributed.elastic.multiprocessing.errors.record標記時,二進制檔案失敗才會寫入- error.json錯誤檔案。對於函式啟動,這是預設行為,不需要使用- @record標記手動標記。- redirects和- tee是位元遮罩,用於指定要將哪些標準串流重定向到- log_dir中的日誌檔案。有效的遮罩值在- Std中定義。若要僅重定向/複製特定本地排名的輸出,請將- redirects作為一個映射傳遞,並將鍵設為本地排名,以指定其重定向行為。任何遺漏的本地排名都將預設為- Std.NONE。- tee的作用類似於 unix 的「tee」命令,它會將輸出重定向並列印到主控台。若要避免工作程序的標準輸出/標準錯誤列印到主控台,請使用- redirects參數。- 對於每個程序, - log_dir將包含- {local_rank}/error.json:如果程序失敗,則為包含錯誤資訊的檔案
- {local_rank}/stdout.json:如果- redirect & STDOUT == STDOUT
- {local_rank}/stderr.json:如果- redirect & STDERR == STDERR
 - 備註 - 預期 - log_dir存在、為空目錄。- 範例 - log_dir = "/tmp/test" # ok; two copies of foo: foo("bar0"), foo("bar1") start_processes( name="trainer", entrypoint=foo, args:{0:("bar0",), 1:("bar1",), envs:{0:{}, 1:{}}, log_dir=log_dir ) # invalid; envs missing for local rank 1 start_processes( name="trainer", entrypoint=foo, args:{0:("bar0",), 1:("bar1",), envs:{0:{}}, log_dir=log_dir ) # ok; two copies of /usr/bin/touch: touch file1, touch file2 start_processes( name="trainer", entrypoint="/usr/bin/touch", args:{0:("file1",), 1:("file2",), envs:{0:{}, 1:{}}, log_dir=log_dir ) # caution; arguments casted to string, runs: # echo "1" "2" "3" and echo "[1, 2, 3]" start_processes( name="trainer", entrypoint="/usr/bin/echo", args:{0:(1,2,3), 1:([1,2,3],), envs:{0:{}, 1:{}}, log_dir=log_dir ) 
處理程序上下文¶
- class torch.distributed.elastic.multiprocessing.api.PContext(name, entrypoint, args, envs, logs_specs, log_line_prefixes=None)[原始碼]¶
- 標準化透過不同機制啟動的一組處理程序操作的基類。 - 名稱 - PContext是為了與- torch.multiprocessing.ProcessContext區分開來。- 警告 - 標準輸出和標準錯誤輸出應該始終分別是 tee_stdouts 和 tee_stderrs 的超集,這是因為 tee 是透過重定向 + tail -f <stdout/stderr.log> 實作的 
- class torch.distributed.elastic.multiprocessing.api.MultiprocessContext(name, entrypoint, args, envs, start_method, logs_specs, log_line_prefixes=None)[原始碼]¶
- 以函數形式調用的工作進程的 - PContext。
- class torch.distributed.elastic.multiprocessing.api.SubprocessContext(name, entrypoint, args, envs, logs_specs, log_line_prefixes=None)[原始碼]¶
- 以二進制文件形式調用的工作進程的 - PContext。
- class torch.distributed.elastic.multiprocessing.api.RunProcsResult(return_values=<factory>, failures=<factory>, stdouts=<factory>, stderrs=<factory>)[原始碼]¶
- 以 - start_processes()啟動的處理程序完成執行的結果。由- PContext返回。- 請注意以下幾點 - 所有字段都按本地等級映射 
- return_values- 僅針對函數(而非二進制文件)填充。
- stdouts- stdout.log 的路徑(如果沒有重定向,則為空字符串)
- stderrs- stderr.log 的路徑(如果沒有重定向,則為空字符串)
 
- class torch.distributed.elastic.multiprocessing.api.DefaultLogsSpecs(log_dir=None, redirects=Std.NONE, tee=Std.NONE, local_ranks_filter=None)[原始碼]¶
- 預設 LogsSpecs 實作 - 如果 log_dir 不存在,將會建立 
- 為每次嘗試和等級生成嵌套文件夾。 
 
- class torch.distributed.elastic.multiprocessing.api.LogsDest(stdouts=<factory>, stderrs=<factory>, tee_stdouts=<factory>, tee_stderrs=<factory>, error_files=<factory>)[原始碼]¶
- 對於每種類型的日誌,保存本地等級 ID 到文件路徑的映射。