华为晟腾设备910B进行pytorch初始化时的设置 —— torch_npu的初始化设置

参考:

小模型在昇腾NPU上的推理部署:【 使用torch_npu进行模型迁移】




初始化昇腾设备


def init_npu_device(device_id=0):
    """初始化NPU设备并设置优化选项"""
    # 关闭JIT编译,避免初期调试时的复杂问题
    torch_npu.npu.set_compile_mode(jit_compile=False)
    # 关闭内部格式转换,确保精度与CPU/GPU一致
    torch_npu.npu.config.allow_internal_format = False
    # 指定设备并设置为当前设备
    device = torch.device(f'npu:{device_id}')
    torch.npu.set_device(device)
    print(f"Using device: {device}")
    return device







posted on 2026-02-26 14:14  Angry_Panda  阅读(67)  评论(0)    收藏  举报

导航