-
Notifications
You must be signed in to change notification settings - Fork 16
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
JRosenkranz
merged 62 commits into
foundation-model-stack:main
from
flaviabeo:generate_metrics_layers
Jul 14, 2025
Merged
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 ce42e32
Initial version of inference with pre and post hooks
flaviabeo c48e271
Checks output type
flaviabeo f78909d
Convert tensor method and save files
flaviabeo 9b4853c
Adds Cosine Similarity + prefix files
flaviabeo f212b8a
Adds dim=1 to Cosine sim
flaviabeo c9c54a4
Removes extra space
flaviabeo 315ff35
Adds layer IO mode to get_thresholds
flaviabeo 09c76c2
Changes model_id to model_path
flaviabeo 3afcac2
Fixes model_path assignment
flaviabeo 3ce8e9c
Save metrics to json
flaviabeo 55b7811
Fix json results assignment
flaviabeo 54017ac
Adds python logger
flaviabeo ace8dfe
Fix logs
flaviabeo 9934746
Adds env variable for LOG LEVEL
flaviabeo 5e1f043
unsqueeze cosine similarity
flaviabeo 02a01ce
Fix same device for cosine similarity
flaviabeo c7d5a40
Convert cos sim to list
flaviabeo 3a96397
Test euclidean dist
flaviabeo d68c52f
Adds sample json output to layer th
flaviabeo 7639b08
Merge branch 'main' into generate_metrics_layers
flaviabeo eb0b866
Adds logging to th script
flaviabeo dee632e
Model forward mode
flaviabeo 4576a3c
Adds docs
flaviabeo dc31192
Fix typos
flaviabeo be1b8d8
Small detail changes
flaviabeo 3ea4084
Prefix with sequence lenght on files' names
flaviabeo ee32a6b
Adds output path to the json th
flaviabeo 45b6514
Catch StopIteration error
flaviabeo a7732e9
Adds docstring to methods
flaviabeo d35b521
Fix cosine similarity calculation
flaviabeo d90b227
Fix print cpu output shape
flaviabeo a6894ce
Order result JSON for th
flaviabeo e41bf20
Review fixes required
flaviabeo 86b5fea
Adds layer mode header
flaviabeo 41b849a
Includes head sub-tensors values
flaviabeo db9b9fe
Metric list shape
flaviabeo d4aa817
Metric list shape
flaviabeo b8d900c
First part of review fixes requested
flaviabeo 1c800e3
Help argsparse added
flaviabeo f74cbbc
Adds docs about the arg parse
flaviabeo c8aed03
Modifies the th output json to all dicts
flaviabeo fc249c5
Moves methods to utils
flaviabeo 0f7f697
Small fix
flaviabeo 12d8c9e
Avg and mean for cosine similarity
flaviabeo de8ee15
Fix avg and mean dict
flaviabeo fbafe1d
Fix avg and mean dict
flaviabeo 96ed494
Fix find files with cos sim
flaviabeo 5ae39a1
Fix layer names in json
flaviabeo b10ed9d
Updates sample result JSON
flaviabeo ced6d31
Changes layer stack structure to dict
flaviabeo a6f84bc
Adds zero values handling
flaviabeo 2fe9124
Merge branch 'main' into generate_metrics_layers
flaviabeo 8b5a64f
Fix infer method docstring
flaviabeo b6bf42d
Adds model path and saves all generate iteractions
flaviabeo cc42d7d
Save iters and read by layers
flaviabeo 40e5924
Removes unused import
flaviabeo 12946a0
Changes metric list to all generate iters
flaviabeo 526c6d5
Improves layers th data structure
flaviabeo 28c44f8
Fix th json
flaviabeo d2e3d98
Add configurable sample requests to prepare inputs
flaviabeo acde4fd
Changes 0 values to small values (avoid nan)
flaviabeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?