Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/twinkle/model/multi_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,18 @@ def has_lora(self, adapter_name: str) -> bool:
return len([_lora for _lora in self.loras if _lora.tenant_adapter_name == adapter_name]) > 0

def find_lora_by_tenant(self, tenant_adapter_name):
return [_lora for _lora in self.loras if _lora.tenant_adapter_name == tenant_adapter_name][0]
_loras = [_lora for _lora in self.loras if _lora.tenant_adapter_name == tenant_adapter_name]
if len(_loras) > 0:
return _loras[0]
else:
raise ValueError(f'No lora found for tenant {tenant_adapter_name}')
Comment on lines +132 to +136
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

While this correctly fixes the potential IndexError, it can be made more efficient. Instead of creating a new list of all matching loras just to get the first one, you can iterate through self.loras and return the first match found. This avoids creating an intermediate list and stops iteration as soon as a match is found.

        for _lora in self.loras:
            if _lora.tenant_adapter_name == tenant_adapter_name:
                return _lora
        raise ValueError(f'No lora found for tenant {tenant_adapter_name}')


def find_lora(self, adapter_name):
return [_lora for _lora in self.loras if _lora.adapter_name == adapter_name][0]
_loras = [_lora for _lora in self.loras if _lora.adapter_name == adapter_name]
if len(_loras) > 0:
return _loras[0]
else:
raise ValueError(f'No lora found for real adapter_name {adapter_name}')
Comment on lines +139 to +143
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Similar to my previous comment, this implementation can be made more efficient. Iterating to find the first match is preferable to building a list of all matches, especially since only the first element is used.

        for _lora in self.loras:
            if _lora.adapter_name == adapter_name:
                return _lora
        raise ValueError(f'No lora found for real adapter_name {adapter_name}')


@staticmethod
def match_target_modules(
Expand Down
Loading