Fix PEFT
This commit is contained in:
parent
6e30a7a006
commit
08d3ef4bb3
1 changed files with 29 additions and 25 deletions
|
|
@ -404,32 +404,36 @@ pass
|
|||
# =============================================
|
||||
# Weirdly LoraLayer.update_layer downcasts PEFT layers to float16??
|
||||
# For mixed precision, we need it to be in float32 not float16.
|
||||
from peft.tuners.lora.layer import LoraLayer
|
||||
import inspect, re
|
||||
try:
|
||||
source = inspect.getsource(LoraLayer.update_layer)
|
||||
text = "if weight is not None:\n"
|
||||
start = source.find(text) + len(text)
|
||||
end = source.find("self.to(weight.device)", start)
|
||||
spaces = re.findall(r"^([ ]{1,})break", source, flags = re.MULTILINE)[0]
|
||||
source = source.replace(source[start : end], spaces)
|
||||
spaces = len(re.match(r"[\s]{1,}", source).group(0))
|
||||
lines = source.split("\n")
|
||||
source = "\n".join(x[spaces:] for x in lines)
|
||||
source = re.sub("([^\.])nn\.", r"\1torch.nn.", source)
|
||||
source = source.replace("def update_layer", "def LoraLayer_update_layer")
|
||||
exec(source, globals())
|
||||
|
||||
# Fix up incorrect downcasting of LoRA weights
|
||||
from packaging import Version
|
||||
from peft import __version__
|
||||
if Version(__version__) < Version("0.12.0"):
|
||||
from peft.tuners.lora.layer import LoraLayer
|
||||
LoraLayer.update_layer = LoraLayer_update_layer
|
||||
from peft.tuners.lora import LoraLayer
|
||||
LoraLayer.update_layer = LoraLayer_update_layer
|
||||
except:
|
||||
logger.warning_once(
|
||||
"Unsloth unsuccessfully patched LoraLayer.update_layer. Please file a bug report.\n"\
|
||||
"Luckily, your training run will still work in the meantime!"
|
||||
)
|
||||
import inspect, re
|
||||
try:
|
||||
source = inspect.getsource(LoraLayer.update_layer)
|
||||
text = "if weight is not None:\n"
|
||||
start = source.find(text) + len(text)
|
||||
end = source.find("self.to(weight.device)", start)
|
||||
spaces = re.findall(r"^([ ]{1,})break", source, flags = re.MULTILINE)[0]
|
||||
source = source.replace(source[start : end], spaces)
|
||||
spaces = len(re.match(r"[\s]{1,}", source).group(0))
|
||||
lines = source.split("\n")
|
||||
source = "\n".join(x[spaces:] for x in lines)
|
||||
source = re.sub("([^\.])nn\.", r"\1torch.nn.", source)
|
||||
source = source.replace("def update_layer", "def LoraLayer_update_layer")
|
||||
exec(source, globals())
|
||||
|
||||
# Fix up incorrect downcasting of LoRA weights
|
||||
from peft.tuners.lora.layer import LoraLayer
|
||||
LoraLayer.update_layer = LoraLayer_update_layer
|
||||
from peft.tuners.lora import LoraLayer
|
||||
LoraLayer.update_layer = LoraLayer_update_layer
|
||||
except:
|
||||
logger.warning_once(
|
||||
"Unsloth unsuccessfully patched LoraLayer.update_layer. Please file a bug report.\n"\
|
||||
"Luckily, your training run will still work in the meantime!"
|
||||
)
|
||||
pass
|
||||
pass
|
||||
# =============================================
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue