【问题标题】:Is there another any alternative to torch.Generator() to support MPS device type? (M1 Mac, PyTorch)是否有其他替代 torch.Generator() 来支持 MPS 设备类型? (M1 Mac,PyTorch)
【发布时间】:2022-11-06 10:29:37
【问题描述】:

我正在运行 python 代码以在我的 m1 mac 上实现稳定的扩散,并在我的 text2img 函数中出现此错误。我了解到 Pytorch 最近开始支持 m1 GPU。我收到此错误(RuntimeError:torch.Generator() api 不支持设备类型 MPS。) 我把代码放在下面并突出显示给我错误的行。将不胜感激任何帮助,让这个工作,谢谢!

def txt2img(prompt, width, height, guidance_scale, steps, seed): 
  global pipe, pipe_type 

  if pipe_type != 'txt2img': 
    pipe = None 
    clear_memory() 

    pipe_type = 'txt2img' 
    pipe = StableDiffusionPipeline.from_pretrained( 
      "CompVis/stable-diffusion-v1-4", 
      revision="fp16", 
      torch_dtype=torch.float16,
      use_auth_token=YOUR_TOKEN # use huggingface token for private model
    ).to("mps") 

  seed = random.randint(0, 2**32) if seed == -1 else seed 
  generator = torch.Generator(device='mps').manual_seed(int(seed)) 
 
 
  pipe.enable_attention_slicing() 
  with autocast("mps"): 
    image = pipe(prompt=prompt,
                 height=height, width=width,
                 num_inference_steps=steps, guidance_scale=guidance_scale, 
                 generator=generator).images[0] 

  return [[image], seed] 

错误引用的主要代码行如下:generator = torch.Generator(device='mps').manual_seed(int(seed))

【问题讨论】:

    标签: python pytorch apple-m1


    【解决方案1】:

    由于您只需要一个随机数,因此只需在 CPU 中生成它即可:

    generator = torch.Generator().manual_seed(int(seed))
    

    【讨论】:

      【解决方案2】:

      我也发现了这个问题。如果我使用 torch.has_mps.manual_seed(SEED),它会显示 AttributeError: 'bool' object has no attribute 'manual_seed'

      【讨论】:

        猜你喜欢
        • 2022-09-28
        • 1970-01-01
        • 1970-01-01
        • 2023-02-21
        • 1970-01-01
        • 2013-03-23
        • 1970-01-01
        • 1970-01-01
        • 2015-03-13
        相关资源
        最近更新 更多