[Feature] Add LoRA support for embedding layers (#14177)
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com> Co-authored-by: Beichen-Ma <bm685@cornell.edu>
This commit is contained in:
co-authored by
Baizhou Zhang
Beichen-Ma
parent
9ad02b799d
commit
0c63fb9420
@@ -27,13 +27,14 @@ class LoRAConfig:
|
||||
self.hf_config = self.get_lora_config()
|
||||
self.target_modules = self.hf_config["target_modules"]
|
||||
|
||||
# TODO: Support more modules
|
||||
if any(module in self.target_modules for module in ["embed_tokens", "lm_head"]):
|
||||
raise ValueError("Not supported yet")
|
||||
|
||||
self.r = self.hf_config["r"]
|
||||
self.lora_alpha = self.hf_config["lora_alpha"]
|
||||
|
||||
self.added_tokens_config = self.get_added_tokens_config()
|
||||
self.lora_added_tokens_size = (
|
||||
len(self.added_tokens_config) if self.added_tokens_config is not None else 0
|
||||
)
|
||||
|
||||
def get_lora_config(self, dummy=False):
|
||||
if dummy:
|
||||
raise NotImplementedError()
|
||||
@@ -45,3 +46,30 @@ class LoRAConfig:
|
||||
config_name = "adapter_config.json"
|
||||
with open(os.path.join(weights_dir, config_name), "r") as f:
|
||||
return json.load(f)
|
||||
|
||||
def get_added_tokens_config(self):
|
||||
"""Load added tokens from the LoRA adapter if the file exists."""
|
||||
# Determine the weights directory
|
||||
if not os.path.isdir(self.path):
|
||||
weights_dir = snapshot_download(self.path, allow_patterns=["*.json"])
|
||||
else:
|
||||
weights_dir = self.path
|
||||
|
||||
# Construct the path to added_tokens.json
|
||||
added_tokens_path = os.path.join(weights_dir, "added_tokens.json")
|
||||
|
||||
# Return None if the file doesn't exist (optional for standard LoRA adapters)
|
||||
if not os.path.exists(added_tokens_path):
|
||||
return None
|
||||
|
||||
# Load and return the added tokens
|
||||
try:
|
||||
with open(added_tokens_path, "r") as f:
|
||||
return json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
# Log warning but don't crash if JSON is malformed
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning(f"Failed to parse added_tokens.json: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user