Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
lm-evaluation-harness
Commits
24b7e2d6
Commit
24b7e2d6
authored
Jul 21, 2025
by
Baber
Browse files
type hints
parent
9f345f33
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
17 deletions
+23
-17
lm_eval/api/registry.py
lm_eval/api/registry.py
+15
-12
lm_eval/config/metric.py
lm_eval/config/metric.py
+8
-5
No files found.
lm_eval/api/registry.py
View file @
24b7e2d6
from
__future__
import
annotations
import
logging
import
logging
from
typing
import
TYPE_CHECKING
,
Callable
,
Dict
,
Optional
,
Union
from
typing
import
TYPE_CHECKING
,
Any
,
Callable
if
TYPE_CHECKING
:
if
TYPE_CHECKING
:
...
@@ -36,13 +38,14 @@ def register_model(*names):
...
@@ -36,13 +38,14 @@ def register_model(*names):
return
decorate
return
decorate
def
get_model
(
model_name
:
str
)
->
type
[
"
LM
"
]:
def
get_model
(
model_name
:
str
)
->
type
[
LM
]:
try
:
try
:
return
MODEL_REGISTRY
[
model_name
]
return
MODEL_REGISTRY
[
model_name
]
except
KeyError
:
except
KeyError
as
err
:
raise
ValueError
(
available_models
=
", "
.
join
(
MODEL_REGISTRY
.
keys
())
f
"Attempted to load model '
{
model_name
}
', but no model for this name found! Supported model names:
{
', '
.
join
(
MODEL_REGISTRY
.
keys
())
}
"
raise
KeyError
(
)
f
"Model '
{
model_name
}
' not found. Available models:
{
available_models
}
"
)
from
err
TASK_REGISTRY
=
{}
TASK_REGISTRY
=
{}
...
@@ -81,7 +84,7 @@ def register_group(name):
...
@@ -81,7 +84,7 @@ def register_group(name):
OUTPUT_TYPE_REGISTRY
=
{}
OUTPUT_TYPE_REGISTRY
=
{}
METRIC_REGISTRY
=
{}
METRIC_REGISTRY
=
{}
METRIC_AGGREGATION_REGISTRY
=
{}
METRIC_AGGREGATION_REGISTRY
=
{}
AGGREGATION_REGISTRY
:
D
ict
[
str
,
Callable
[[],
D
ict
[
str
,
Callable
]]]
=
{}
AGGREGATION_REGISTRY
:
d
ict
[
str
,
Callable
[[],
d
ict
[
str
,
Callable
]]]
=
{}
HIGHER_IS_BETTER_REGISTRY
=
{}
HIGHER_IS_BETTER_REGISTRY
=
{}
FILTER_REGISTRY
=
{}
FILTER_REGISTRY
=
{}
...
@@ -125,7 +128,7 @@ def register_metric(**args):
...
@@ -125,7 +128,7 @@ def register_metric(**args):
return
decorate
return
decorate
def
get_metric
(
name
:
str
,
hf_evaluate_metric
=
False
)
->
Optional
[
Callable
]
:
def
get_metric
(
name
:
str
,
hf_evaluate_metric
=
False
)
->
Callable
[...,
Any
]
|
None
:
if
not
hf_evaluate_metric
:
if
not
hf_evaluate_metric
:
if
name
in
METRIC_REGISTRY
:
if
name
in
METRIC_REGISTRY
:
return
METRIC_REGISTRY
[
name
]
return
METRIC_REGISTRY
[
name
]
...
@@ -157,21 +160,21 @@ def register_aggregation(name: str):
...
@@ -157,21 +160,21 @@ def register_aggregation(name: str):
return
decorate
return
decorate
def
get_aggregation
(
name
:
str
)
->
Optional
[
Callable
[[],
D
ict
[
str
,
Callable
]]
]
:
def
get_aggregation
(
name
:
str
)
->
Callable
[[],
d
ict
[
str
,
Callable
]]
|
None
:
try
:
try
:
return
AGGREGATION_REGISTRY
[
name
]
return
AGGREGATION_REGISTRY
[
name
]
except
KeyError
:
except
KeyError
:
eval_logger
.
warning
(
f
"
{
name
}
not a registered aggregation metric!"
)
eval_logger
.
warning
(
f
"
{
name
}
not a registered aggregation metric!"
)
def
get_metric_aggregation
(
name
:
str
)
->
Optional
[
Callable
[[],
D
ict
[
str
,
Callable
]]
]
:
def
get_metric_aggregation
(
name
:
str
)
->
Callable
[[],
d
ict
[
str
,
Callable
]]
|
None
:
try
:
try
:
return
METRIC_AGGREGATION_REGISTRY
[
name
]
return
METRIC_AGGREGATION_REGISTRY
[
name
]
except
KeyError
:
except
KeyError
:
eval_logger
.
warning
(
f
"
{
name
}
metric is not assigned a default aggregation!"
)
eval_logger
.
warning
(
f
"
{
name
}
metric is not assigned a default aggregation!"
)
def
is_higher_better
(
metric_name
:
str
)
->
Optional
[
bool
]
:
def
is_higher_better
(
metric_name
:
str
)
->
bool
|
None
:
try
:
try
:
return
HIGHER_IS_BETTER_REGISTRY
[
metric_name
]
return
HIGHER_IS_BETTER_REGISTRY
[
metric_name
]
except
KeyError
:
except
KeyError
:
...
@@ -192,7 +195,7 @@ def register_filter(name: str):
...
@@ -192,7 +195,7 @@ def register_filter(name: str):
return
decorate
return
decorate
def
get_filter
(
filter_name
:
Union
[
str
,
Callable
]
)
->
Callable
:
def
get_filter
(
filter_name
:
str
|
Callable
)
->
Callable
:
try
:
try
:
return
FILTER_REGISTRY
[
filter_name
]
return
FILTER_REGISTRY
[
filter_name
]
except
KeyError
as
e
:
except
KeyError
as
e
:
...
...
lm_eval/config/metric.py
View file @
24b7e2d6
from
__future__
import
annotations
from
collections.abc
import
Callable
from
dataclasses
import
dataclass
from
dataclasses
import
dataclass
from
functools
import
cached_property
from
functools
import
cached_property
from
typing
import
Any
,
Callable
,
List
,
Optional
from
typing
import
Any
@
dataclass
@
dataclass
...
@@ -8,9 +11,9 @@ class MetricConfig:
...
@@ -8,9 +11,9 @@ class MetricConfig:
"""Encapsulates information about a single metric."""
"""Encapsulates information about a single metric."""
name
:
str
name
:
str
fn
:
Optional
[
Callable
]
=
None
fn
:
Callable
|
None
=
None
kwargs
:
Optional
[
dict
]
=
None
kwargs
:
dict
|
None
=
None
aggregation_fn
:
Optional
[
Callable
]
=
None
aggregation_fn
:
Callable
|
None
=
None
higher_is_better
:
bool
=
True
higher_is_better
:
bool
=
True
hf_evaluate
:
bool
=
False
hf_evaluate
:
bool
=
False
is_elementwise
:
bool
=
True
is_elementwise
:
bool
=
True
...
@@ -41,7 +44,7 @@ class MetricConfig:
...
@@ -41,7 +44,7 @@ class MetricConfig:
raise
ValueError
(
f
"Metric function for
{
self
.
name
}
is not defined."
)
raise
ValueError
(
f
"Metric function for
{
self
.
name
}
is not defined."
)
return
self
.
fn
(
*
args
,
**
{
**
self
.
kwargs
,
**
kwargs
})
return
self
.
fn
(
*
args
,
**
{
**
self
.
kwargs
,
**
kwargs
})
def
compute_aggregation
(
self
,
values
:
L
ist
[
Any
])
->
Any
:
def
compute_aggregation
(
self
,
values
:
l
ist
[
Any
])
->
Any
:
"""Computes the aggregation of the metric values."""
"""Computes the aggregation of the metric values."""
if
self
.
aggregation_fn
is
None
:
if
self
.
aggregation_fn
is
None
:
raise
ValueError
(
f
"Aggregation function for
{
self
.
name
}
is not defined."
)
raise
ValueError
(
f
"Aggregation function for
{
self
.
name
}
is not defined."
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment