Upload 3 files
Browse files- __init__.py +19 -0
- configuration_metalora.py +20 -0
- modeling_metalora.py +7 -7
__init__.py
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
from typing import List
|
| 3 |
+
|
| 4 |
+
class MetaLoRAConfig(PretrainedConfig):
|
| 5 |
+
model_type = "metalora"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
mlora_layers:List[int],
|
| 10 |
+
base_size:int,
|
| 11 |
+
embd_model:str,
|
| 12 |
+
llm_tokenizer:str,
|
| 13 |
+
**kwargs,
|
| 14 |
+
):
|
| 15 |
+
self.mlora_layers = mlora_layers
|
| 16 |
+
self.base_size = base_size
|
| 17 |
+
self.embd_model = embd_model
|
| 18 |
+
self.llm_tokenizer = llm_tokenizer
|
| 19 |
+
super().__init__(**kwargs)
|
configuration_metalora.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
from typing import List
|
| 3 |
+
|
| 4 |
+
class MetaLoRAConfig(PretrainedConfig):
|
| 5 |
+
model_type = "MLoRAModel"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
mlora_layers: List[int]=["init"],
|
| 10 |
+
base_size:int=384,
|
| 11 |
+
embd_model:str="init",
|
| 12 |
+
llm_tokenizer:str="init",
|
| 13 |
+
**kwargs,
|
| 14 |
+
):
|
| 15 |
+
self.auto_map = {"AutoModel": "Arthur-LAGACHERIE/MetaLoRA-code--modeling_metalora.MLoRAModel", "AutoConfig":"Arthur-LAGACHERIE/MetaLoRA-code--configuration_metalora.MetaLoRAConfig"}
|
| 16 |
+
self.mlora_layers = mlora_layers
|
| 17 |
+
self.base_size = base_size
|
| 18 |
+
self.embd_model = embd_model
|
| 19 |
+
self.llm_tokenizer = llm_tokenizer
|
| 20 |
+
super().__init__(**kwargs)
|
modeling_metalora.py
CHANGED
|
@@ -16,6 +16,7 @@ from transformers import Cache, DynamicCache, StaticCache
|
|
| 16 |
from transformers.processing_utils import Unpack
|
| 17 |
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 18 |
|
|
|
|
| 19 |
class MLoRASingleton:
|
| 20 |
_instance = None # Stocke l'instance unique
|
| 21 |
|
|
@@ -197,28 +198,27 @@ class MLoRAModel(nn.Module, GenerationMixin):
|
|
| 197 |
self.singleton.set_tensor(torch.zeros(1, 1).to("cpu"))
|
| 198 |
|
| 199 |
@classmethod
|
| 200 |
-
def from_pretrained(cls, repo, token=None):
|
| 201 |
|
| 202 |
with tempfile.TemporaryDirectory() as tmp:
|
| 203 |
# load the repo and the config
|
| 204 |
snapshot_download(repo_id=repo, local_dir=f"{tmp}/repo/", token=token)
|
| 205 |
-
config =
|
| 206 |
-
config = json.load(config)
|
| 207 |
|
| 208 |
### load model
|
| 209 |
llm_config = AutoConfig.from_pretrained(f"{tmp}/repo/model/config.json")
|
| 210 |
print("load LLM")
|
| 211 |
llm = AutoModelForCausalLM.from_config(llm_config)
|
| 212 |
-
llm = get_mlora_model(llm, config
|
| 213 |
sfts.load_model(llm, f"{tmp}/repo/model/model.safetensors")
|
| 214 |
print("LLM loaded")
|
| 215 |
|
| 216 |
### load tokenizer
|
| 217 |
-
tokenizer = AutoTokenizer.from_pretrained(config
|
| 218 |
|
| 219 |
### load embd_model
|
| 220 |
print("load Embd model")
|
| 221 |
-
embd_model = EmbdModel(AutoTokenizer.from_pretrained(config
|
| 222 |
print("Embd model loaded")
|
| 223 |
|
| 224 |
### create instance
|
|
@@ -261,7 +261,7 @@ class MLoRAModel(nn.Module, GenerationMixin):
|
|
| 261 |
### push config
|
| 262 |
config_path = saved_path / "config.json"
|
| 263 |
with open(config_path, "w") as config_file:
|
| 264 |
-
json.dump(self.config, config_file)
|
| 265 |
|
| 266 |
api.upload_file(
|
| 267 |
path_or_fileobj=config_path,
|
|
|
|
| 16 |
from transformers.processing_utils import Unpack
|
| 17 |
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 18 |
|
| 19 |
+
|
| 20 |
class MLoRASingleton:
|
| 21 |
_instance = None # Stocke l'instance unique
|
| 22 |
|
|
|
|
| 198 |
self.singleton.set_tensor(torch.zeros(1, 1).to("cpu"))
|
| 199 |
|
| 200 |
@classmethod
|
| 201 |
+
def from_pretrained(cls, repo, token=None, print_=False):
|
| 202 |
|
| 203 |
with tempfile.TemporaryDirectory() as tmp:
|
| 204 |
# load the repo and the config
|
| 205 |
snapshot_download(repo_id=repo, local_dir=f"{tmp}/repo/", token=token)
|
| 206 |
+
config = MetaLoRAConfig.from_pretrained(repo, token=token)
|
|
|
|
| 207 |
|
| 208 |
### load model
|
| 209 |
llm_config = AutoConfig.from_pretrained(f"{tmp}/repo/model/config.json")
|
| 210 |
print("load LLM")
|
| 211 |
llm = AutoModelForCausalLM.from_config(llm_config)
|
| 212 |
+
llm = get_mlora_model(llm, config.mlora_layers, config.base_size, lm_head="lm_head.weight" in config.mlora_layers, print_=print_)
|
| 213 |
sfts.load_model(llm, f"{tmp}/repo/model/model.safetensors")
|
| 214 |
print("LLM loaded")
|
| 215 |
|
| 216 |
### load tokenizer
|
| 217 |
+
tokenizer = AutoTokenizer.from_pretrained(config.llm_tokenizer)
|
| 218 |
|
| 219 |
### load embd_model
|
| 220 |
print("load Embd model")
|
| 221 |
+
embd_model = EmbdModel(AutoTokenizer.from_pretrained(config.embd_model), AutoModel.from_pretrained(config.embd_model))
|
| 222 |
print("Embd model loaded")
|
| 223 |
|
| 224 |
### create instance
|
|
|
|
| 261 |
### push config
|
| 262 |
config_path = saved_path / "config.json"
|
| 263 |
with open(config_path, "w") as config_file:
|
| 264 |
+
json.dump(self.config.to_dict(), config_file)
|
| 265 |
|
| 266 |
api.upload_file(
|
| 267 |
path_or_fileobj=config_path,
|