蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测版本、设备能力与均匀随机张量

PYTHONWindows CPU、CPython 3.12.14、PyTorch 2.10.0+cpu。verify-torch-installation.py
verify-torch-installation.py62 行
"""Verify an explicit backend with a small native tensor calculation."""
import argparse
import json
import platform
import sys

import torch


def probe(device_name):
    if device_name == "cuda" and not torch.cuda.is_available():
        raise SystemExit("This build and host cannot initialize CUDA.")
    if device_name == "mps" and not torch.backends.mps.is_available():
        raise SystemExit("This build and host cannot initialize MPS.")
    device = torch.device(device_name)
    rng = torch.Generator(device="cpu").manual_seed(105)
    values = torch.rand((5, 3), dtype=torch.float32, generator=rng)
    transferred = values.to(device)
    roundtrip = transferred.cpu()
    return dict(version=platform.python_version(), torch=torch.__version__, system=platform.system(),
                platform_release=platform.release(), architecture=platform.machine(),
                venv=(sys.prefix != sys.base_prefix), cuda_compiled=torch.version.cuda,
                cuda_available=torch.cuda.is_available(), mps_available=torch.backends.mps.is_available(),
                backend=transferred.device.type, tensor_shape=list(transferred.shape),
                values=values.tolist(), minimum=values.min().item(), maximum=values.max().item(),
                same_values=bool(torch.equal(values, roundtrip)))


def record(name, data, action, expected, actual):
    assert expected == actual, (name, expected, actual)
    return dict(name=name, inputs=data, prerequisites="Installed matching PyTorch wheel in a separate CPython environment",
                action=action, expected=expected, actual=actual, passed=True)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--device", default="cpu", choices=("cpu","cuda","mps"))
    parser.add_argument("--assert-project-install", action="store_true")
    args = parser.parse_args()
    result = probe(args.device)
    if args.assert_project_install:
        result["cases"] = [
            record("windows_isolated_environment", "explicit current interpreter",
                   "Read system, architecture, Python version and virtual-environment identity",
                   ["Windows", "AMD64", "3.12.14", True],
                   [result["system"], result["architecture"], result["version"], result["venv"]]),
            record("cpu_install_probe", "torch==2.10.0 CPU",
                   "Import torch and inspect the build/runtime capability independently",
                   ["2.10.0+cpu", None, False, False],
                   [result["torch"], result["cuda_compiled"], result["cuda_available"], result["mps_available"]]),
            record("random_uniform_shape", {"seed":105, "shape":[5,3], "device":args.device},
                   "Allocate a uniform float32 tensor and send it through the selected backend",
                   [[5,3], args.device, True, True],
                   [result["tensor_shape"], result["backend"],
                    0 <= result["minimum"] <= result["maximum"] < 1, result["same_values"]]),
        ]
    print(json.dumps(result))


if __name__ == "__main__":
    main()
本例需在 Windows CPU、CPython 3.12.14、PyTorch 2.10.0+cpu。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows CPU、CPython 3.12.14、PyTorch 2.10.0+cpu。

入口文件:projects/pytorch-install-lab/verify-torch-installation.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 verify-torch-installation.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows CPU、CPython 3.12.14、PyTorch 2.10.0+cpu。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PyTorch

课程关联工程包(登记关联39章) · 含当前章节或实例 · mbskycom202612a409.zip

下载课程关联工程包:PyTorch

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a424.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习