laksjdjf / sd-trainer

GNU Affero General Public License v3.0
76 stars 11 forks source link

TypeError: LoRAModule.forward() takes 2 positional arguments but 3 were given #41

Closed a-ru2016 closed 1 year ago

a-ru2016 commented 1 year ago

学習スクリプトの開発ありがとうございます。いつもお世話になっております。 colab上、T4でSD1.xのlecoを学習しようとしたところ以下のエラーが出ましたので報告します。

Traceback (most recent call last):
  File "/content/sd-trainer/leco.py", line 235, in <module>
    main(config)
  File "/content/sd-trainer/leco.py", line 189, in main
    noise_pred = cfg(unet, latents_input, timestep, torch.cat([target, negative],dim=0), generate_guidance_scale, target_proj, negative_proj, size_condition)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/content/sd-trainer/leco.py", line 51, in cfg
    return unet(latents, timesteps, positive_negative.chunk(2)[0], added_cond_kwargs=added_cond_kwargs).sample
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/diffusers/models/unet_2d_condition.py", line 958, in forward
    sample, res_samples = downsample_block(
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/diffusers/models/unet_2d_blocks.py", line 1076, in forward
    hidden_states = attn(
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/diffusers/models/transformer_2d.py", line 287, in forward
    hidden_states = self.proj_in(hidden_states, lora_scale)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
TypeError: LoRAModule.forward() takes 2 positional arguments but 3 were given

設定ファイルは以下のようになっております。 config.yaml

model:
    input_path: "{model}"
    output_name: "{ploject_name}.safetensors"

leco:
    prompts_file: "leco_utils/prompts.yaml"
    epochs: 500
    save_steps: 100
    resolution: 512
    num_samples: 8 # increasing this value will improve speed but decrease accuracy.
    sampling_step: 30 # sampling_step for generating latents.
    generate_guidance_scale: 1
    noise_scheduler: diffusers.DDIMScheduler

train:
    lr: 1e-3
    batch_size: 2
    lr_scheduler: "cosine"
    amp: true
    gradient_checkpointing: true
    use_xformers: true
    seed: 7
    tome_ratio: null

optimizer:
    module: bitsandbytes.optim.AdamW8bit
    #args:

network:
    module: networks.lora.LoRANetwork
    train: true
    resume: null
    args:
        rank: 32 # "dynamic"で動的に決定
        conv_rank: null # 指定するとloconになる
        module: null # "loha"でlohaになる

prompts.yaml

- target: ""
  positive: "Pictures you don't like,(traditional media,marker,comic book panel,context art,frame,portrait:1.5),lowres, text, error, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, monochrome, greyscale, sketch, many arms,multiple arms,four arms,two arms,(multiple limbs:1.2),4 arms,six arms,2 arms,two arms and to legs,numerous limbs,realististic, many arms,multiple arms,four arms,two arms,multiple limbs,4 arms,six arms,2 arms,two arms and to legs,numerous limbs"
  negative: " "
  neutral: ","
  guidance_scale: -3.0

また、prompts.yamlを以下のようにしたところ別のエラーが出ました。

  File "/content/sd-trainer/leco.py", line 235, in <module>
    main(config)
  File "/content/sd-trainer/leco.py", line 173, in main
    neutral, neutral_proj = batch["neutral"]
TypeError: cannot unpack non-iterable NoneType object

prompts.yaml

- target: ""
  positive: "Pictures you don't like,(traditional media,marker,comic book panel,context art,frame,portrait:1.5),lowres, text, error, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, monochrome, greyscale, sketch, many arms,multiple arms,four arms,two arms,(multiple limbs:1.2),4 arms,six arms,2 arms,two arms and to legs,numerous limbs,realististic, many arms,multiple arms,four arms,two arms,multiple limbs,4 arms,six arms,2 arms,two arms and to legs,numerous limbs"
  negative: ""
  neutral: ""
  guidance_scale: -3.0

お返事いただければ幸いです。また、私は素人なので間違っていることをしたり、やったりしているかもしれませんが野生動物を見ている気分で許してください。

laksjdjf commented 1 year ago

devブランチに変更していただくと直るかもしれません。 git pull origin dev

ついでにgenerate_guidance_scaleは大きめの値にした方がよさそうです。 本家LECOでは3に固定しているようですね。 試したことないので意外と小さいほうがいいということもあるかもしれませんが。。。

a-ru2016 commented 1 year ago

devブランチに変更することで無事LECOを作ることができました! ありがとうございます。 また、アドバイスの通りにgenerate_guidance_scaleを上げたらうまくいきました。