Skip to content

Generate metrics from model's layers #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
134c3b8
Initial generate_layers_metrics version
flaviabeo Jun 12, 2025
ce42e32
Initial version of inference with pre and post hooks
flaviabeo Jun 16, 2025
c48e271
Checks output type
flaviabeo Jun 17, 2025
f78909d
Convert tensor method and save files
flaviabeo Jun 17, 2025
9b4853c
Adds Cosine Similarity + prefix files
flaviabeo Jun 18, 2025
f212b8a
Adds dim=1 to Cosine sim
flaviabeo Jun 19, 2025
c9c54a4
Removes extra space
flaviabeo Jun 20, 2025
315ff35
Adds layer IO mode to get_thresholds
flaviabeo Jun 30, 2025
09c76c2
Changes model_id to model_path
flaviabeo Jul 1, 2025
3afcac2
Fixes model_path assignment
flaviabeo Jul 1, 2025
3ce8e9c
Save metrics to json
flaviabeo Jul 1, 2025
55b7811
Fix json results assignment
flaviabeo Jul 1, 2025
54017ac
Adds python logger
flaviabeo Jul 1, 2025
ace8dfe
Fix logs
flaviabeo Jul 1, 2025
9934746
Adds env variable for LOG LEVEL
flaviabeo Jul 2, 2025
5e1f043
unsqueeze cosine similarity
flaviabeo Jul 2, 2025
02a01ce
Fix same device for cosine similarity
flaviabeo Jul 2, 2025
c7d5a40
Convert cos sim to list
flaviabeo Jul 2, 2025
3a96397
Test euclidean dist
flaviabeo Jul 2, 2025
d68c52f
Adds sample json output to layer th
flaviabeo Jul 2, 2025
7639b08
Merge branch 'main' into generate_metrics_layers
flaviabeo Jul 2, 2025
eb0b866
Adds logging to th script
flaviabeo Jul 2, 2025
dee632e
Model forward mode
flaviabeo Jul 3, 2025
4576a3c
Adds docs
flaviabeo Jul 3, 2025
dc31192
Fix typos
flaviabeo Jul 3, 2025
be1b8d8
Small detail changes
flaviabeo Jul 3, 2025
3ea4084
Prefix with sequence lenght on files' names
flaviabeo Jul 3, 2025
ee32a6b
Adds output path to the json th
flaviabeo Jul 7, 2025
45b6514
Catch StopIteration error
flaviabeo Jul 7, 2025
a7732e9
Adds docstring to methods
flaviabeo Jul 7, 2025
d35b521
Fix cosine similarity calculation
flaviabeo Jul 8, 2025
d90b227
Fix print cpu output shape
flaviabeo Jul 8, 2025
a6894ce
Order result JSON for th
flaviabeo Jul 8, 2025
e41bf20
Review fixes required
flaviabeo Jul 8, 2025
86b5fea
Adds layer mode header
flaviabeo Jul 8, 2025
41b849a
Includes head sub-tensors values
flaviabeo Jul 8, 2025
db9b9fe
Metric list shape
flaviabeo Jul 8, 2025
d4aa817
Metric list shape
flaviabeo Jul 8, 2025
b8d900c
First part of review fixes requested
flaviabeo Jul 9, 2025
1c800e3
Help argsparse added
flaviabeo Jul 9, 2025
f74cbbc
Adds docs about the arg parse
flaviabeo Jul 9, 2025
c8aed03
Modifies the th output json to all dicts
flaviabeo Jul 9, 2025
fc249c5
Moves methods to utils
flaviabeo Jul 9, 2025
0f7f697
Small fix
flaviabeo Jul 9, 2025
12d8c9e
Avg and mean for cosine similarity
flaviabeo Jul 9, 2025
de8ee15
Fix avg and mean dict
flaviabeo Jul 9, 2025
fbafe1d
Fix avg and mean dict
flaviabeo Jul 9, 2025
96ed494
Fix find files with cos sim
flaviabeo Jul 9, 2025
5ae39a1
Fix layer names in json
flaviabeo Jul 9, 2025
b10ed9d
Updates sample result JSON
flaviabeo Jul 9, 2025
ced6d31
Changes layer stack structure to dict
flaviabeo Jul 9, 2025
a6f84bc
Adds zero values handling
flaviabeo Jul 10, 2025
2fe9124
Merge branch 'main' into generate_metrics_layers
flaviabeo Jul 10, 2025
8b5a64f
Fix infer method docstring
flaviabeo Jul 10, 2025
b6bf42d
Adds model path and saves all generate iteractions
flaviabeo Jul 10, 2025
cc42d7d
Save iters and read by layers
flaviabeo Jul 10, 2025
40e5924
Removes unused import
flaviabeo Jul 10, 2025
12946a0
Changes metric list to all generate iters
flaviabeo Jul 10, 2025
526c6d5
Improves layers th data structure
flaviabeo Jul 10, 2025
28c44f8
Fix th json
flaviabeo Jul 11, 2025
d2e3d98
Add configurable sample requests to prepare inputs
flaviabeo Jul 11, 2025
acde4fd
Changes 0 values to small values (avoid nan)
flaviabeo Jul 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions aiu_fms_testing_utils/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Third Party
from aiu_fms_testing_utils.utils.aiu_setup import dprint
from fms.utils.tokenizers import BaseTokenizer
from fms.utils.generation import pad_input_ids
import torch
import torch.nn as nn

Expand Down Expand Up @@ -166,3 +167,19 @@ def sample_squad_v2_qa_requests(
prompt_length_max,
seed,
)

def prepare_inputs(batch_size, seq_length, tokenizer, sharegpt_path, seed=0):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add documentation for this? Also, is it possible to make the sample_requests as configurable (as we have other sample requests methods)?

Copy link
Contributor Author

@flaviabeo flaviabeo Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I made the changes to the other files to use the utils' method in this other PR #77. I thought it would be better to not mix these changes in here, then once it's merged I can rebase the other PR. Is this ok?

prompts_and_sizes = sample_sharegpt_requests(
sharegpt_path,
batch_size,
tokenizer,
int(seq_length / 2),
seq_length,
seed,
)
prompt_list = []
for prompt, _ in prompts_and_sizes:
prompt_list.append(ids_for_prompt(prompt, tokenizer))

input_ids, padding_kwargs = pad_input_ids(prompt_list, min_pad_length=seq_length)
return input_ids, padding_kwargs
72 changes: 72 additions & 0 deletions aiu_fms_testing_utils/utils/metrics_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import numpy as np
import torch
import torch.nn as nn


def abs_diff_linalg_norm(res_vector):
"""
Calculates the Euclidean norm (also known as the L2 norm) of a given array res_vector. This is equivalent to finding the square
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
to measure the "length" or "magnitude" of a vector. More at https://numpy.org/devdocs/reference/generated/numpy.linalg.norm.html
Args:
res_vector (list): The list of abs diff

Returns:
float: "magnitude" of the diff vector.
"""
return np.linalg.norm(res_vector)

def list_mean(val_list):
"""
Calculates the mean for all the values in a given list.
Args:
val_list (list): The list of values

Returns:
float: mean value calculated.
"""
return np.mean(val_list)

def tensor_abs_diff(tensor1, tensor2):
"""
Calculate the absolute difference between two tensors.

Args:
tensor1 (torch.Tensor): The first input tensor.
tensor2 (torch.Tensor): The second input tensor.

Returns:
torch.Tensor: The absolute difference tensor.

Example:
>>> tensor1 = torch.tensor([1, 2, 3])
>>> tensor2 = torch.tensor([4, 5, 6])
>>> abs_diff(tensor1, tensor2)
torch.tensor([3, 3, 3])
"""
abs_diff = torch.abs(tensor1 - tensor2)
abs_diff[abs_diff == 0.0] = 1e-6
return abs_diff

def tensor_cos_sim(tensor1, tensor2):
"""
Computes the cosine similarity between two tensors.

Args:
tensor1 (torch.Tensor): The first input tensor.
tensor2 (torch.Tensor): The second input tensor.

Returns:
torch.Tensor: The cosine similarity between the two input tensors.

Example:
>>> import torch
>>> tensor1 = torch.randn(3, 5)
>>> tensor2 = torch.randn(3, 5)
>>> sim = cos_sim(tensor1, tensor2)
>>> print(sim)
"""
cos = nn.CosineSimilarity(dim=-1)
cos_sim = cos(tensor1, tensor2)
cos_sim[cos_sim == 0.0] = 1e-6
return cos_sim
Loading