Views
No views yet
13.5 million parameters which were trainable during the fine-tuning process.1.2 billion parameters.1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_id = 'meta-llama/Llama-3.2-1B'
4base_model = AutoModelForCausalLM.from_pretrained(model_id)1import torch.nn as nn
2
3def get_base_model_parameter(model, rank, alpha):
4 base_model_parameters = []
5 for name, module in model.named_children():
6 if isinstance(module, torch.nn.Linear):
7 base_model_parameters.append(
8 [module.in_features, module.out_features, rank, alpha]
9 )
10 else:
11 base_model_parameters.extend(get_base_model_parameter(module, rank, alpha))
12
13 return base_model_parameters
14
15base_model_parameters = get_base_model_parameter(base_model, rank=16, alpha=16)1import torch
2import math
3from huggingface_hub import PyTorchModelHubMixin
4
5class LoRALayer(torch.nn.Module):
6 def __init__(self, in_dim, out_dim, rank, alpha):
7 super().__init__()
8 self.A = torch.nn.Parameter(torch.empty(in_dim, rank, dtype=torch.bfloat16))
9 torch.nn.init.kaiming_uniform_(self.A, a=math.sqrt(5))
10 self.B = torch.nn.Parameter(torch.zeros(rank, out_dim, dtype=torch.bfloat16))
11 self.alpha = alpha
12 self.rank = rank
13
14 def forward(self, x):
15 x = (self.alpha / self.rank) * (x @ self.A @ self.B)
16 return x
17
18
19class LlamaSummarizationLoRALayers(nn.Module, PyTorchModelHubMixin):
20 def __init__(self):
21 super().__init__()
22 self.lora_layers = nn.ModuleList(
23 [
24 LoRALayer(
25 base_model_parameter[0],
26 base_model_parameter[1],
27 base_model_parameter[2],
28 base_model_parameter[3]
29 )
30 for base_model_parameter in base_model_parameters
31 ]
32 )
33
34model_id = 'SauravP97/llama-3.2-1B-summarization-loraa-layers'
35llama_summarizer_lora_layers = LlamaSummarizationLoRALayers.from_pretrained(model_id)1class LinearWithLoRA(torch.nn.Module):
2 def __init__(self, linear, lora_layer):
3 super().__init__()
4 self.linear = linear
5 self.lora = lora_layer
6
7 def forward(self, x):
8 return self.linear(x) + self.lora(x)
9
10def replace_linear_with_preloaded_lora(model, lora_layers, cur_index):
11 for name, module in model.named_children():
12 if isinstance(module, torch.nn.Linear):
13 print(f'Initialized with Pre-loaded LoRA Layer: {cur_index}')
14 setattr(model, name, LinearWithLoRA(module, lora_layers[cur_index]))
15 cur_index = cur_index + 1
16 else:
17 cur_index = replace_linear_with_preloaded_lora(module, lora_layers, cur_index)
18
19 return cur_index
20
21replace_linear_with_preloaded_lora(base_model, llama_summarizer_lora_layers.lora_layers, 0)1import torch
2
3def generate_content(prompt, model):
4 encoded_ids = tokenizer.encode(prompt)
5 encoded_ids = torch.tensor(encoded_ids, dtype=torch.int64, device=device)
6
7 encoded_ids = encoded_ids.view(1, encoded_ids.shape[-1])
8
9 generated_ids = model.generate(
10 encoded_ids, max_length=500, pad_token_id=tokenizer.eos_token_id
11 )
12
13 return tokenizer.decode(generated_ids)[0]
14
15model_id = 'meta-llama/Llama-3.2-1B'
16tokenizer = AutoTokenizer.from_pretrained(model_id)
17
18tokenizer.pad_token_id = 1131
19device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
20
21prompt2 = '''<|begin_of_text|> Once upon a time, there was a little bird.
22The bird was white and had a pretty song. One day, the bird saw a big pit.
23The pit was deep and dark.
24The bird wanted to see what was inside, so it flew down to take a look.
25As the bird got closer, the pit began to rise up and swallow the bird.
26The bird tried to fly away, but it was too late. The pit had taken the bird down.
27The moral of the story is that we should be careful when we see something dangerous.
28We should not go near it, even if we are curious. It is better to be safe than sorry.
29Summary:
30'''
31
32print(generate_content(prompt2, base_model))<|begin_of_text|><|begin_of_text|> Once upon a time, there was a little bird.
The bird was white and had a pretty song. One day, the bird saw a big pit.
The pit was deep and dark.
The bird wanted to see what was inside, so it flew down to take a look.
As the bird got closer, the pit began to rise up and swallow the bird.
The bird tried to fly away, but it was too late. The pit had taken the bird down.
The moral of the story is that we should be careful when we see something dangerous.
We should not go near it, even if we are curious. It is better to be safe than sorry.
Summary:
A curious little bird flies down to a deep pit and gets swallowed by it,
teaching the moral to be careful when exploring dangerous things. <|end_of_text|>LlamaForCausalLM(
(model): LlamaModel(
(embed_tokens): Embedding(128256, 2048)
(layers): ModuleList(
(0-15): 16 x LlamaDecoderLayer(
(self_attn): LlamaAttention(
(q_proj): Linear(in_features=2048, out_features=2048, bias=False)
(k_proj): Linear(in_features=2048, out_features=512, bias=False)
(v_proj): Linear(in_features=2048, out_features=512, bias=False)
(o_proj): Linear(in_features=2048, out_features=2048, bias=False)
)
(mlp): LlamaMLP(
(gate_proj): Linear(in_features=2048, out_features=8192, bias=False)
(up_proj): Linear(in_features=2048, out_features=8192, bias=False)
(down_proj): Linear(in_features=8192, out_features=2048, bias=False)
(act_fn): SiLUActivation()
)
(input_layernorm): LlamaRMSNorm((2048,), eps=1e-05)
(post_attention_layernorm): LlamaRMSNorm((2048,), eps=1e-05)
)
)
(norm): LlamaRMSNorm((2048,), eps=1e-05)
(rotary_emb): LlamaRotaryEmbedding()
)
(lm_head): Linear(in_features=2048, out_features=128256, bias=False)
)LlamaForCausalLM(
(model): LlamaModel(
(embed_tokens): Embedding(128256, 2048)
(layers): ModuleList(
(0-15): 16 x LlamaDecoderLayer(
(self_attn): LlamaAttention(
(q_proj): LinearWithLoRA(
(linear): Linear(in_features=2048, out_features=2048, bias=False)
(lora): LoRALayer()
)
(k_proj): LinearWithLoRA(
(linear): Linear(in_features=2048, out_features=512, bias=False)
(lora): LoRALayer()
)
(v_proj): LinearWithLoRA(
(linear): Linear(in_features=2048, out_features=512, bias=False)
(lora): LoRALayer()
)
(o_proj): LinearWithLoRA(
(linear): Linear(in_features=2048, out_features=2048, bias=False)
(lora): LoRALayer()
)
)
(mlp): LlamaMLP(
(gate_proj): LinearWithLoRA(
(linear): Linear(in_features=2048, out_features=8192, bias=False)
(lora): LoRALayer()
)
(up_proj): LinearWithLoRA(
(linear): Linear(in_features=2048, out_features=8192, bias=False)
(lora): LoRALayer()
)
(down_proj): LinearWithLoRA(
(linear): Linear(in_features=8192, out_features=2048, bias=False)
(lora): LoRALayer()
)
(act_fn): SiLUActivation()
)
(input_layernorm): LlamaRMSNorm((2048,), eps=1e-05)
(post_attention_layernorm): LlamaRMSNorm((2048,), eps=1e-05)
)
)
(norm): LlamaRMSNorm((2048,), eps=1e-05)
(rotary_emb): LlamaRotaryEmbedding()
)
(lm_head): LinearWithLoRA(
(linear): Linear(in_features=2048, out_features=128256, bias=False)
(lora): LoRALayer()
)
)