Skip to content
Merged
Changes from 7 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
307 changes: 307 additions & 0 deletions scripts/generate_layers_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
import os
import time

import itertools
import torch
import torch.nn as nn

from fms.utils import tokenizers
from fms.models import get_model
from fms.utils.generation import pad_input_ids, generate

from aiu_fms_testing_utils.testing.validation import get_default_validation_prefix

from aiu_fms_testing_utils.utils import (
sample_sharegpt_requests,
ids_for_prompt,
)
from aiu_fms_testing_utils.utils.aiu_setup import dprint



ORIGINAL_HF_HOME = os.environ.get("HF_HOME", None)
Copy link
Contributor

@JRosenkranz JRosenkranz Jul 9, 2025

Choose a reason for hiding this comment

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

We don't use HF_HOME anymore as we go through the default hf cache hub, this can be removed


SHARE_GPT_DATASET_PATH = os.environ.get(
"SHARE_GPT_DATASET_PATH", os.path.expanduser("~/share_gpt.json")
)

common_model_paths = common_model_paths = os.environ.get(
"MODEL_PATHS",
["ibm-granite/granite-3.2-8b-instruct"],
Copy link
Contributor

Choose a reason for hiding this comment

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

update to ibm-granite/granite-3.3-8b-instruct

)
common_batch_sizes = os.environ.get("BATCH_SIZES", [1, 2, 4, 8])
common_seq_lengths = os.environ.get("SEQ_LENGTHS", [64, 2048])
common_max_new_tokens = os.environ.get("MAX_NEW_TOKENS", [128])

output_dir = os.environ.get("OUTPUT_PATH", "/tmp/output")

# pass custom model path list for eg: EXPORT FMS_TESTING_COMMON_MODEL_PATHS="/tmp/models/granite-3-8b-base,/tmp/models/granite-7b-base"
if isinstance(common_model_paths, str):
common_model_paths = common_model_paths.split(",")

# pass custom common batch sizes as a comma separated str of ints
if isinstance(common_batch_sizes, str):
common_batch_sizes = [int(bs) for bs in common_batch_sizes.split(",")]

# pass custom common seq lengths as a comma separated str of ints
if isinstance(common_seq_lengths, str):
common_seq_lengths = [int(sl) for sl in common_seq_lengths.split(",")]

# pass custom common max new tokens as a comma separated str of ints
if isinstance(common_max_new_tokens, str):
common_max_new_tokens = [int(mnt) for mnt in common_max_new_tokens.split(",")]

common_shapes = list(
itertools.product(
common_model_paths,
common_batch_sizes,
common_seq_lengths,
common_max_new_tokens,
)
)

def __prepare_inputs(batch_size, seq_length, tokenizer, 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.

I think we re-use this in a couple of areas, might make sense to add this to the utils

prompts_and_sizes = sample_sharegpt_requests(
SHARE_GPT_DATASET_PATH,
batch_size,
tokenizer,
int(seq_length / 2),
seq_length,
seed,
)
print(prompts_and_sizes)
## TODO: for each prompt
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

def __infer_layer(warmup, model, max_len, device,

Choose a reason for hiding this comment

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

Add with torch.no_grad(): inside __infer_layer to prevent unnecessary autograd graph construction.

max_new_tokens, batch_size, tokenizer):


do_sample = False
use_cache = True

prompts = __prepare_inputs(batch_size, max_len, tokenizer)
ids, pad_input_ids = prompts

if "cuda" in device:
print("cuda prompts")
print(len(prompts))
ids = ids.to("cuda")

if hasattr(model.config, "ntk_scaling") and model.config.ntk_scaling:
max_seq_len = max(max_len, model.config.max_expected_seq_len)
else:
# without ntk scaling, extending the seq length too far gives bogus results.
max_seq_len = model.config.max_expected_seq_len

result = generate(

Choose a reason for hiding this comment

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

I think it is better to use model.forward() instead of generate to:

  • Trigger one full forward pass without sampling or token iteration.
  • See all intermediate activations, since hooks will fire exactly once per layer.
  • Avoid introducing noise from sampling, past key caching, etc.

If we use generate only may mask individual issues inside specific layers because It’s a high-level API that wraps many operations: (forward pass, KV cache logic, sampling or greeting decoding, post-processing). It may skip certain branches inside model.forward() depending on decoding logic (e.g., only decoder path, or only first token). It may use optimized inference paths (e.g., with contiguous_cache=True) that bypass certain logic like residual addition, attention masking, or past key handling.

@jjhursey is this the way that you typically compare between the models at a macro level. Only relying on generate?
Let us know what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! Added the model.forward mode

model,
ids,
max_new_tokens=max_new_tokens,
use_cache=use_cache,
do_sample=do_sample,
max_seq_len=max_seq_len,
timing="e2e",
eos_token_id=None,
contiguous_cache=True,
extra_kwargs={},
)
result, timings = result
dprint(f"E2E timing information: {timings[0]:.3f}s")
if len(result.shape) == 1:
result = result.unsqueeze(0)

if not warmup:
for i in range(result.shape[0]):
print(result[i])

Choose a reason for hiding this comment

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

In the script, we have several print() statements with no control over log levels, formatting or file redirection. It would be better to have a structured logging interface using dprint() or Python’s built-in logging
What is the logging mechanisms that FMS uses? @jjhursey @ani300
example:

import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

logger.info("Saving file...")
logger.debug(f"Layer: {layer}, Output: {output}")
logger.warning("Some layers were skipped due to missing output")


def __register_call_layers(model, batch_size, device, seq_length, max_new_tokens, tokenizer):
layer_stack = []
pt_compile_model_time = time.time()

module_depth = {}
module_name = {}

def register_depths(module, current_depth=0, name='model'):
module_depth[module] = current_depth
module_name[module] = name
parent=name
# if we are dealing with array of layers
array_layers = all(key.isdigit() for key in module._modules.keys())
for name, child in module._modules.items():
if array_layers:
register_depths(child, current_depth + 1, parent+'['+name+']')
else:
register_depths(child, current_depth + 1, parent+'.'+name)

register_depths(model)

def wrap_forward(layer):
original_forward = layer.forward

def safe_forward(*args, **kwargs):
#print("In safe forward")
try:
#print("No Error")
return original_forward(*args, **kwargs)
except (RuntimeError,TypeError) as e:
print(f"Error in {layer.__class__.__name__}: {e}")
return torch.zeros_like(args[0]) if args else None
layer.forward = safe_forward


hooks = []
def pre_hook_fn(module, input):
depth = module_depth.get(module, 0)
layer_name = module_name.get(module, 0)
prefix = '│ ' * depth
if len(input) == 0: return
input_shape_str = f"[{', '.join(map(str, input[0].shape))}]"
input_type = str(input[0].dtype)
if module.parameters() == None: return
param_size = sum(p.numel() for p in module.parameters() if p.requires_grad)
param_size_str = f"{param_size:,}" if param_size > 0 else "--"
print(f"DEBUG TOOL {prefix}├─{layer_name}() -> {module.__class__.__name__} : | Input(arg): {input_shape_str} | {input_type} | Params: {param_size_str}")
wrap_forward(module)
# save input for later use with outputs
module._debug_input = input

def post_hook_fn(module, input, output):
print("post_hook_fn")
layer_name = module_name.get(module, 0)
# Save inputs and outputs
if hasattr(module, '_debug_input'):
print(output)
layer_stack.append((layer_name, output))
# Clean up
delattr(module, '_debug_input')

for name, layer in model.named_modules():
hooks.append(layer.register_forward_pre_hook(pre_hook_fn))
hooks.append(layer.register_forward_hook(post_hook_fn))


__infer_layer(warmup=True,
model= model, max_len=seq_length,
device=device, max_new_tokens=max_new_tokens,
batch_size=batch_size, tokenizer=tokenizer)

for hook in hooks:
hook.remove()

pt_compile_model_time = time.time() - pt_compile_model_time
dprint(f"PT compile complete, took {pt_compile_model_time:.3f}s")

return layer_stack

def write_csv(l, path, metric):
print("saving file")
with open(path, 'w') as f:
f.write(f'{metric}\n')
if not type(l) is float:

Choose a reason for hiding this comment

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

better to use if not isinstance(l, float): instead of if not type(l) is float:

print("saving list")
for t in l:
f.write(f"{t}\n")
else:
print("saving float")
f.write(f"{l}\n")
f.close()

def convert_tensor(output):

Choose a reason for hiding this comment

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

convert_tensor() just creates a zero tensor of shape (len(output), len(keys)). Is this the real intention. Either document clearly that this is a placeholder or consider using torch.nn.utils.rnn.pad_sequence() or torch.cat() with real tensors if possible.

out_unique = set(list(itertools.chain.from_iterable(output)))
keys = {key: value for key, value in zip(out_unique, range(len(out_unique)))}
return torch.zeros(size=(len(output), len(keys)))

def generate_layers_metrics(model_path, batch_size, seq_length, max_new_tokens):
torch.manual_seed(42)
os.environ["COMPILATION_MODE"] = "offline_decoder"

if "HF_HOME" not in os.environ:
os.environ["HF_HOME"] = "/tmp/models/hf_cache"

model_path_kwargs = {"variant": model_path}
micro_model_kwargs = {"architecture": "hf_pretrained"}

get_model_kwargs = {
**model_path_kwargs,
**micro_model_kwargs,
}

tokenizer = tokenizers.get_tokenizer(model_path)

# prepare the cpu model
validation_model = get_model(
device_type="cpu",
data_type=torch.float32,
fused_weights=False,
**get_model_kwargs,
)

# prepare the cuda model
validation_model_cuda = get_model(
device_type="cuda",
data_type=torch.float16,
fused_weights=False,
**get_model_kwargs,
)

layer_stack_cpu = __register_call_layers(model=validation_model,
batch_size=batch_size,
device="cpu",
seq_length=seq_length, max_new_tokens=max_new_tokens,
tokenizer=tokenizer)

layer_stack_cuda = __register_call_layers(model=validation_model_cuda,
batch_size=batch_size,
device="cuda",
seq_length=seq_length, max_new_tokens=max_new_tokens,
tokenizer=tokenizer)

absolute_differences = []

assert len(layer_stack_cuda) == len(layer_stack_cpu)

for layer, cuda_output in layer_stack_cuda:
tensor_cuda_out = None
tensor_cpu_out = None
abs_diff = None
for cpu_layer, cpu_output in layer_stack_cpu:
if cpu_layer == layer:
print("CPU Layer {} GPU Layer {}".format(cpu_layer, layer))

if not type(cuda_output) is tuple:
tensor_cuda_out = cuda_output
else:
tensor_cuda_out = convert_tensor(cuda_output)
if type(cpu_output) is tuple:
tensor_cpu_out = convert_tensor(cpu_output)
else:
tensor_cpu_out = cpu_output.to('cuda')
print("tensor converted... get torch abs diff")
abs_diff = torch.abs(tensor_cpu_out - tensor_cuda_out).flatten().tolist()
print("abs_diff calculated")
cos = nn.CosineSimilarity(dim=1)
cos_sim = cos(tensor_cpu_out, tensor_cuda_out)
print(cos_sim)

absolute_differences.append(abs_diff)
print("abs_diff list appended")
print(len(absolute_differences))

prefix = get_default_validation_prefix(model_id, max_new_token, batch_size, 0, 'float16')

Choose a reason for hiding this comment

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

model_id is not defined in the function. This looks like it might be a bug.
I proposed to change model_id to model_path since this is what you use throughout the code.

prefix = get_default_validation_prefix(model_path, max_new_token, batch_size, 0, 'float16')

layer_name = str(layer).replace('[','').replace(']', '')

print("saving files")
write_csv(abs_diff, os.path.join(output_dir, f"{prefix}--{layer_name}.abs_diff.csv"), "abs_diff")
write_csv(cos_sim, os.path.join(output_dir, f"{prefix}--{layer_name}.cos_sim.csv"), "cos_sim")

print(f"Completed {model_id} layers' metrics generation")

for model_id, batch_size, sequence_length, max_new_token in common_shapes:
print("testing ", "model_id-", model_id, ", max_new_tokens-", max_new_token, ", batch_size-",batch_size, ", seq_length-",sequence_length)
generate_layers_metrics(model_path=model_id, batch_size=batch_size, seq_length=sequence_length, max_new_tokens=max_new_token)