Skip to content

Commit 858a51e

Browse files
authored
Merge pull request #63 from flaviabeo/generate_metrics_layers
Generate metrics from model's layers
2 parents 803e370 + acde4fd commit 858a51e

File tree

11 files changed

+2565
-322
lines changed

11 files changed

+2565
-322
lines changed

aiu_fms_testing_utils/utils/__init__.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# Third Party
1010
from aiu_fms_testing_utils.utils.aiu_setup import dprint
1111
from fms.utils.tokenizers import BaseTokenizer
12+
from fms.utils.generation import pad_input_ids
1213
import torch
1314
import torch.nn as nn
1415

@@ -166,3 +167,43 @@ def sample_squad_v2_qa_requests(
166167
prompt_length_max,
167168
seed,
168169
)
170+
171+
def prepare_inputs(batch_size, seq_length, tokenizer, ds_path, seed=0, ds_type="sharegpt"):
172+
"""
173+
Prepare input IDs and padding kwargs for a batch of questions.
174+
175+
Args:
176+
batch_size (int): The number of questions in the batch.
177+
seq_length (int): The maximum length of the input sequence.
178+
tokenizer (Tokenizer): A tokenizer object to tokenize the questions.
179+
ds_path (str): The path to the dataset file.
180+
seed (int, optional): The random seed for reproducibility. Defaults to 0.
181+
ds_type (str, optional): The type of dataset to use. Can be "sharegpt" or any other supported dataset type. Defaults to "sharegpt".
182+
183+
Returns:
184+
tuple: A tuple containing the input IDs and padding kwargs.
185+
"""
186+
if not "sharegpt" in ds_type:
187+
prompts_and_sizes = sample_squad_v2_qa_requests(
188+
ds_path,
189+
batch_size,
190+
tokenizer,
191+
int(seq_length / 2),
192+
seq_length,
193+
seed,
194+
)
195+
else:
196+
prompts_and_sizes = sample_sharegpt_requests(
197+
ds_path,
198+
batch_size,
199+
tokenizer,
200+
int(seq_length / 2),
201+
seq_length,
202+
seed,
203+
)
204+
prompt_list = []
205+
for prompt, _ in prompts_and_sizes:
206+
prompt_list.append(ids_for_prompt(prompt, tokenizer))
207+
208+
input_ids, padding_kwargs = pad_input_ids(prompt_list, min_pad_length=seq_length)
209+
return input_ids, padding_kwargs
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import numpy as np
2+
import torch
3+
import torch.nn as nn
4+
5+
6+
def abs_diff_linalg_norm(res_vector):
7+
"""
8+
Calculates the Euclidean norm (also known as the L2 norm) of a given array res_vector. This is equivalent to finding the square
9+
root of the sum of the squares of all the elements in the array. It's a fundamental operation in linear algebra and is often used
10+
to measure the "length" or "magnitude" of a vector. More at https://numpy.org/devdocs/reference/generated/numpy.linalg.norm.html
11+
Args:
12+
res_vector (list): The list of abs diff
13+
14+
Returns:
15+
float: "magnitude" of the diff vector.
16+
"""
17+
return np.linalg.norm(res_vector)
18+
19+
def list_mean(val_list):
20+
"""
21+
Calculates the mean for all the values in a given list.
22+
Args:
23+
val_list (list): The list of values
24+
25+
Returns:
26+
float: mean value calculated.
27+
"""
28+
return np.mean(val_list)
29+
30+
def tensor_abs_diff(tensor1, tensor2):
31+
"""
32+
Calculate the absolute difference between two tensors.
33+
34+
Args:
35+
tensor1 (torch.Tensor): The first input tensor.
36+
tensor2 (torch.Tensor): The second input tensor.
37+
38+
Returns:
39+
torch.Tensor: The absolute difference tensor.
40+
41+
Example:
42+
>>> tensor1 = torch.tensor([1, 2, 3])
43+
>>> tensor2 = torch.tensor([4, 5, 6])
44+
>>> abs_diff(tensor1, tensor2)
45+
torch.tensor([3, 3, 3])
46+
"""
47+
abs_diff = torch.abs(tensor1 - tensor2)
48+
return abs_diff
49+
50+
def tensor_cos_sim(tensor1, tensor2):
51+
"""
52+
Computes the cosine similarity between two tensors.
53+
54+
Args:
55+
tensor1 (torch.Tensor): The first input tensor.
56+
tensor2 (torch.Tensor): The second input tensor.
57+
58+
Returns:
59+
torch.Tensor: The cosine similarity between the two input tensors.
60+
61+
Example:
62+
>>> import torch
63+
>>> tensor1 = torch.randn(3, 5)
64+
>>> tensor2 = torch.randn(3, 5)
65+
>>> sim = cos_sim(tensor1, tensor2)
66+
>>> print(sim)
67+
"""
68+
cos = nn.CosineSimilarity(dim=-1)
69+
tensor1[tensor1 == 0.0] = 1e-6
70+
tensor2[tensor2 == 0.0] = 1e-6
71+
cos_sim = cos(tensor1, tensor2)
72+
return cos_sim

0 commit comments

Comments
 (0)