@@ -23,15 +23,15 @@ def __init__(
2323 ** kwargs : Any ,
2424 ):
2525 """
26- Prepares a dataset and compiles metrics to assess transferability.
26+ Prepares huggingface text dataset (downsamples and finds the task category).
27+ Sets up transferability metrics.
2728
2829 :param dataset: a dataset from huggingface with texts and labels.
29- :param dataset_downsample: a fraction to which the dataset should be reduced .
30+ :param dataset_downsample: a fraction to downsample the dataset to .
3031 :param text_column: the name of the column containing texts.
3132 :param label_column: the name of the column containing labels.
32- :param kwargs: additional dataset-specific parameters for data cleaning .
33+ :param kwargs: additional parameters for dataset preprocessing .
3334 """
34- # Preprocess and down-sample a dataset
3535 datacleaner = DatasetCleaner (
3636 dataset_downsample = dataset_downsample ,
3737 text_column = text_column ,
@@ -46,6 +46,7 @@ def __init__(
4646 "logme" : LogME ,
4747 "hscore" : HScore ,
4848 "knn" : NearestNeighbors ,
49+ # add more
4950 }
5051
5152 def run (
@@ -57,23 +58,22 @@ def run(
5758 ** kwargs : Any ,
5859 ):
5960 """
60- Loads models, collects embeddings , and scores them.
61+ Loads language models, collects embedding from each , and scores them.
6162
6263 :param models: A list of model names
6364 :param estimator: Transferability metric ('hscore', 'logme', 'knn').
64- :param layer_aggregator: Method to aggregate layers ('lastlayer ', 'layermean ', 'bestlayer').
65+ :param layer_aggregator: Method to aggregate layers ('layermean ', 'lastlayer ', 'bestlayer').
6566 :param batch_size: Number of samples per batch, defaults to 32.
6667 :param device: Device for embedding ('cpu', 'cuda', 'cuda:2').
67- :param gpu_estimation: Store and score embeddings on GPU for speedup .
68+ :param gpu_estimation: Boolean if to compute transferability estimation using gpu .
6869 :param kwargs: Additional parameters for embedder class.
6970 :return: Returns sorted dictionary of model names and their scores
7071 """
7172 self ._confirm_ranker_setup (estimator = estimator , layer_aggregator = layer_aggregator )
7273
73- # Set device for models and the metric
7474 device = kwargs .pop ("device" , None )
75- gpu_estimation = kwargs .get ("gpu_estimation" , True )
76- if gpu_estimation :
75+ estimation_using_gpu = kwargs .get ("gpu_estimation" , True )
76+ if estimation_using_gpu :
7777 self .labels = self .labels .to (device )
7878
7979 # Download models to hf cache
@@ -91,7 +91,6 @@ def run(
9191 else kwargs .get ("sentence_pooling" , "mean" )
9292 )
9393
94- # Setup the embedder
9594 embedder = Embedder (
9695 model = model ,
9796 layer_ids = "0" if layer_aggregator == "lastlayer" else "all" ,
@@ -101,26 +100,27 @@ def run(
101100 ** kwargs ,
102101 )
103102
104- # Collect embeddings
103+ # Collect language model embeddings
105104 embeddings = embedder .embed (
106- self .texts , batch_size = batch_size , unpack_to_cpu = not gpu_estimation , show_progress = True ,
105+ self .texts , batch_size = batch_size , unpack_to_cpu = not estimation_using_gpu , show_progress = True ,
107106 ) # fmt: skip
108107
109- # Flatten them for ner tasks
110108 if self .task_category == TaskCategory .TOKEN_CLASSIFICATION :
111109 embeddings = [word for sentence in embeddings for word in sentence ]
112110
113111 model_name = embedder .name
114- del embedder # remove from memory
112+ del embedder # remove language model from memory
115113 torch .cuda .empty_cache ()
116114
117115 # Compute transferability
118116 score = self ._transferability_score (embeddings , metric , layer_aggregator )
119117
120- # Store and log results
121118 result .add_score (model_name , score )
122119 logger .info (f"{ model_name } { result .metric } : { score :.4f} " )
123120
121+ logger .info (f"Results ▲\n { result } " )
122+ logger .info (f"Done!" )
123+
124124 return result
125125
126126 def _transferability_score (self , embeddings , metric , layer_aggregator , show_progress = True ) -> float :
@@ -132,7 +132,7 @@ def _transferability_score(self, embeddings, metric, layer_aggregator, show_prog
132132 range (num_layers ), desc = "Transferability score" , bar_format = tqdm_bar_format , disable = not show_progress
133133 )
134134
135- # Score each layer separately
135+ # Score each hidden layer separately
136136 for layer_id in transferability_progress :
137137 layer_embeddings = torch .stack ([emb [layer_id ] for emb in embeddings ])
138138 score = metric .fit (embeddings = layer_embeddings , labels = self .labels )
0 commit comments