如何为 Python 模块制作 VScode launch.json

JH *_*H S 16 python visual-studio-code pytorch vscode-debugger

我正在研究自监督机器学习代码

我想用python debuggernot来调试代码pdb.set_trace()。这是 ubuntu 终端的 python 命令。

python -m torch.distributed.launch --nproc_per_node=1 main_swav.py \
--data_path /dataset/imagenet/train \
--epochs 400 \
--base_lr 0.6 \
--final_lr 0.0006 \
--warmup_epochs 0 \
--batch_size 8 \
--size_crops 224 96 \
--nmb_crops 2 6 \
--min_scale_crops 0.14 0.05 \
--max_scale_crops 1. 0.14 \
--use_fp16 true \
--freeze_prototypes_niters 5005 \
--queue_length 380 \
--epoch_queue_starts 15\
--workers 10
Run Code Online (Sandbox Code Playgroud)

为了使用 VScode 调试代码,我尝试修改 launch.json,如下所示,参考stackoverflow 问题

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "module": "torch.distributed.launch --nproc_per_node=1 main_swav.py",
            "request": "launch",
            "console": "integratedTerminal",
            "args": ["--data_path", "/dataset/imagenet/train"]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我知道这行不通...

你能给我一些建议吗?

小智 17

指定您要运行的模块 "module": "torch.distributed.launch"

您可以忽略该-m标志。将其他所有内容都放在密钥下args

注意:确保参数列表中包含--nproc_per_node文件名 ( )main_swav.py

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "module": "torch.distributed.launch",
            "request": "launch",
            "console": "integratedTerminal",
            "args": [
                "--nproc_per_node", "1", 
                "main_swav.py",
                "--data_path", "/dataset/imagenet/train",
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多内容:https ://code.visualstudio.com/docs/python/debugging#_module

  • @aviator 将模块设置为 [`torch.distributed.run`](https://github.com/pytorch/pytorch/blob/8a744c31d3c5194b4850869a112d94912c1e08b4/setup.py#L968) (4认同)
  • 对于较新的“torchrun”有任何更新吗? (2认同)