Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 69a99e1

Browse files
authored
* Add a context manager to setup fresh session within, and refresh on context exit (#2169)
* Refresh session b/w evaluate calls using the context manager
1 parent 1baadd0 commit 69a99e1

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# flake8: noqa
15+
from .session_helpers import *
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from contextlib import contextmanager
16+
17+
import sparseml.core.session as session_manager
18+
19+
20+
@contextmanager
21+
def session_context_manager():
22+
"""
23+
A context manager to setup a fresh session and reset it after the context
24+
is exited.
25+
"""
26+
27+
active_session = session_manager.active_session()
28+
active_session.reset()
29+
yield
30+
# reset the session after each context
31+
active_session.reset()

src/sparseml/evaluation/evaluator.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from typing import Optional
1717

18+
from sparseml.core.utils import session_context_manager
1819
from sparseml.evaluation.registry import SparseMLEvaluationRegistry
1920
from sparsezoo.evaluation.results import Result
2021

@@ -43,15 +44,17 @@ def evaluate(
4344
:param batch_size: The batch size to use for evals, defaults to 1
4445
:return: The evaluation result as a Result object
4546
"""
46-
47-
eval_integration = SparseMLEvaluationRegistry.resolve(
48-
name=integration, datasets=datasets
49-
)
50-
51-
if datasets is None:
52-
# let the integration handle the default dataset
53-
return eval_integration(model_path=model_path, batch_size=batch_size, **kwargs)
54-
55-
return eval_integration(
56-
model_path=model_path, datasets=datasets, batch_size=batch_size, **kwargs
57-
)
47+
with session_context_manager():
48+
eval_integration = SparseMLEvaluationRegistry.resolve(
49+
name=integration, datasets=datasets
50+
)
51+
52+
if datasets is None:
53+
# let the integration handle the default dataset
54+
return eval_integration(
55+
model_path=model_path, batch_size=batch_size, **kwargs
56+
)
57+
58+
return eval_integration(
59+
model_path=model_path, datasets=datasets, batch_size=batch_size, **kwargs
60+
)

0 commit comments

Comments
 (0)