Skip to content

Topological divergence#363

Open
ahdr3w wants to merge 27 commits into
IINemo:mainfrom
ahdr3w:topological_divergence
Open

Topological divergence#363
ahdr3w wants to merge 27 commits into
IINemo:mainfrom
ahdr3w:topological_divergence

Conversation

@ahdr3w

@ahdr3w ahdr3w commented Jul 3, 2025

Copy link
Copy Markdown

Add TopologicalDivergence estimator based on the method proposed in arXiv:2504.10063

@ahdr3w ahdr3w marked this pull request as ready for review July 3, 2025 12:31
@ahdr3w ahdr3w marked this pull request as draft July 3, 2025 12:51
Merge latest changes from main into topological_divergence
@ahdr3w ahdr3w marked this pull request as ready for review July 3, 2025 12:59
@alfekka

alfekka commented Aug 11, 2025

Copy link
Copy Markdown
Collaborator

Please specify the library versions in requirements.txt

Comment thread requirements.txt
fastchat
diskcache>=5.6.3
joblib
ripser

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please specify the library versions in requirements.txt

"model_heads_cache": "model_heads_cache.yaml",
"max_heads": 6,
"n_jobs": -1,
"dataset": ["LM-polygraph/coqa", "continuation"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please specify the dataset parameters in configs

"seed": [1],
"size": None,
},
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please move all these parameters into the configs and also specify this calculator there. Additionally, it might be much better to integrate the initialization of this calculator with TrainingStatisticExtractionCalculator to avoid multiple loadings of the training dataset.

For reference, we combine TrainingStatisticExtractionCalculator with other calculators for supervised methods in another PR. I think this case should be handled in a similar way.

calculator: https://github.com/IINemo/lm-polygraph/blob/supervised_methods/src/lm_polygraph/stat_calculators/statistic_extraction.py

initialization: https://github.com/IINemo/lm-polygraph/blob/supervised_methods/src/lm_polygraph/defaults/stat_calculator_builders/default_TrainingStatisticExtractionCalculator.py

config: https://github.com/IINemo/lm-polygraph/blob/supervised_methods/examples/configs/stat_calculators/default_calculators.yaml

@@ -0,0 +1,63 @@
import os

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

To avoid multiple loadings of the training dataset, it might be much better to integrate the initialization of this calculator with TrainingStatisticExtractionCalculator

labels = []

greedy_calc = GreedyProbsCalculator(False, False)
attn_forward_pass_calc = AttentionForwardPassCalculator()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should be specified in __init__

mtopdivs = np.concatenate(mtopdivs, axis=0)

labels = np.concatenate(labels)
labels = ~(labels > 0.3)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This hyperparameter should be specified in __init__ and in configs

self.x = [self.x[i] for i in indices]
self.y = [self.y[i] for i in indices]
self.x = [self.x[int(i)] for i in indices]
self.y = [self.y[int(i)] for i in indices]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This change is unclear

heads = np.unravel_index(heads, (num_layers, num_heads))
heads = np.stack(heads, axis=1)

save_model_heads(self.cache_path, name_or_path, heads.tolist())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It should be configurable, since saving all statistics is not always required

Comment thread test/test_estimators.py
n_jobs=-1,
)
ue = estimate_uncertainty(model, estimator, INPUT)
get_reusable_executor().shutdown(wait=True)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The usage of get_reusable_executor is unclear

Comment thread test/test_estimators.py
def test_topological_divergence_select_heads(model):
estimator = TopologicalDivergence(n_jobs=-1)
ue = estimate_uncertainty(model, estimator, INPUT)
get_reusable_executor().shutdown(wait=True)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you clarify how this would work without a training dataset?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants