Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug of dual module (setattr, and compatible with DualModule input) #613

Merged
merged 29 commits into from
Feb 5, 2024

Conversation

marigoold
Copy link
Collaborator

@marigoold marigoold commented Feb 1, 2024

适配 PEFT 中暴露出了一些问题:
PEFT 创建 LoRA model 时候,会把对应的 Linear(也包含 Conv2d 等其他 module,这里以 Linear 为例)拿出来,然后创建一个 LoRALinear,把这个 Linear 包一层,放在原来这个 Linear 的位置。
当使用 DeployableModule 的时候,拿出来的 Linear 是个 DualModule,用 LoRALinear 包完一层之后,就变成了 torch module 包 DualModule 的情况。
在后续 torch2oflow 以及创建 DualModule 的时候,会报错。

这里是拿 Linear 时候的现场:

ipdb> l .
    300             self.targeted_module_names.append(key)
    301             is_target_modules_in_base_model = True
    302             if key == "down_blocks.0.resnets.0.time_emb_proj":
    303                 import ipdb; ipdb.set_trace()
    304             parent, target, target_name = _get_submodules(model, key)
--> 305             self._create_and_replace(peft_config, adapter_name, target, target_name, parent, current_key=key)
    306
    307         if not is_target_modules_in_base_model:
    308             raise ValueError(
    309                 f"Target modules {peft_config.target_modules} not found in the base model. "
    310                 f"Please check the target modules and try again."

ipdb> p target
MixedDualModule(of Linear)(in_features=1280, out_features=320, bias=True)
ipdb> p type(model)
<class 'onediff.infer_compiler.with_oneflow_compile.get_mixed_deployable_module.<locals>.MixedDeployableModule'>

这里是用 LoRALinear 包起来的现场:

> /data/home/wangyi/workspace/peft/src/peft/tuners/lora/layer.py(721)dispatch_default()
    720         import ipdb; ipdb.set_trace()
--> 721         new_module = Linear(target, adapter_name, **kwargs)
    722     elif isinstance(target_base_layer, Conv1D):

ipdb> p target
MixedDualModule(of Linear)(in_features=1280, out_features=320, bias=True)
ipdb> n
> /data/home/wangyi/workspace/peft/src/peft/tuners/lora/layer.py(731)dispatch_default()
    729         new_module = Linear(target, adapter_name, is_target_conv_1d_layer=True, **kwargs)
    730
--> 731     return new_module

ipdb> p new_module
lora.Linear(
  (base_layer): MixedDualModule(of Linear)(in_features=1280, out_features=320, bias=True)
  (lora_dropout): ModuleDict(
    (default_0): Identity()
  )
  (lora_A): ModuleDict(
    (default_0): Linear(in_features=1280, out_features=8, bias=False)
  )
  (lora_B): ModuleDict(
    (default_0): Linear(in_features=8, out_features=320, bias=False)
  )
  (lora_embedding_A): ParameterDict()
  (lora_embedding_B): ParameterDict()
)

[None] * len(torch_attr) if oneflow_attr is None else oneflow_attr
)
if oneflow_attr is None:
oneflow_attr = flow.nn.ModuleList([None] * len(torch_attr))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里原来的方法创建的 oneflow_attr 是个 list,在下面 DualModule 的 setattr 中,setattr(self._oneflow_module, key, value) 对于 key='0' 会挂掉。这里本意应该是也创建一个 flow.nn.ModuleList

Comment on lines +171 to +174
@torch2oflow.register
def _(mod: DualModule, verbose=False):
return torch2oflow(mod._torch_module, verbose)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

转换 torch model 在那种情况会出现 DualModule ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

转换 torch model 在那种情况会出现 DualModule ?

PEFT 这里有一处逻辑是,找到 LoRA 对应的 Linear,用自己定义的 LoRALinear 包一下。
“找到 LoRA 对应的 Linear” 这里会导致 DualModule getattr 返回一个 DualModule
“用自己定义的 LoRALinear 包一下” 这里会生成一个 torch module,但它的 submodule 包含 DualModule,再创建 DualModule 的时候,会导致这里 torch2oflow 接受一个 DualModule

@marigoold marigoold changed the base branch from main to dev_wy_lora_support_textencoder February 2, 2024 06:19
Base automatically changed from dev_wy_lora_support_textencoder to main February 3, 2024 14:49
@strint strint merged commit 2bd12a5 into main Feb 5, 2024
1 check passed
@strint strint deleted the fix_wy_dualmodulelist_setattr branch February 5, 2024 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants