Skip to content

Commit 76727f2

Browse files
Use psutils for get_available_cpu_count (openvinotoolkit#2003)
### Changes Add option to get logical or physicals cores
1 parent 5179553 commit 76727f2

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

nncf/common/utils/os.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
11-
import multiprocessing
1211
import sys
1312
from contextlib import contextmanager
1413
from pathlib import Path
@@ -42,12 +41,17 @@ def is_linux():
4241
return "linux" in sys.platform
4342

4443

45-
def get_available_cpu_count() -> int:
44+
def get_available_cpu_count(logical: bool = True) -> int:
4645
"""
47-
:return: Logical CPU count
46+
Return the number of CPUs in the system.
47+
48+
:param logical: If False return the number of physical cores only (e.g. hyper thread CPUs are excluded),
49+
otherwise number of logical cores. Defaults, True.
50+
:return: Number of CPU.
4851
"""
4952
try:
50-
return multiprocessing.cpu_count()
53+
num_cpu = psutil.cpu_count(logical=logical)
54+
return num_cpu if num_cpu is not None else 1
5155
except Exception: # pylint: disable=broad-except
5256
return 1
5357

nncf/quantization/algorithms/accuracy_control/algorithm.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ class MetricResults:
141141
142142
:param metric_value: Aggregated metric value.
143143
:param values_for_each_item: Metric values for each data item.
144-
:param preperation_time: Time that it takes to prepare model for validation.
144+
:param preparation_time: Time that it takes to prepare model for validation.
145145
:param validation_time: Time that it takes to validate model.
146146
"""
147147

148148
metric_value: float
149149
values_for_each_item: Union[None, List[float], List[List[TTensor]]]
150-
preperation_time: float
150+
preparation_time: float
151151
validation_time: float
152152

153153

@@ -300,7 +300,7 @@ def _apply(
300300
model_size = algo_backend.get_model_size(quantized_model)
301301
num_ranking_processes = self._calculate_number_ranker_parallel_proc(
302302
model_size,
303-
quantized_metric_results.preperation_time,
303+
quantized_metric_results.preparation_time,
304304
quantized_metric_results.validation_time,
305305
validation_dataset_size,
306306
)
@@ -409,32 +409,32 @@ def _apply(
409409
def _calculate_number_ranker_parallel_proc(
410410
self,
411411
model_size: int,
412-
preperation_time: float,
412+
preparation_time: float,
413413
validation_time: float,
414414
validation_dataset_size: int,
415415
) -> int:
416416
"""
417417
Calculate the number of parallel ranker processes
418418
419419
:param model_size: Target model size.
420-
:param preperation_time: The time it takes to prepare the model.
420+
:param preparation_time: The time it takes to prepare the model.
421421
:param validation_time: The time it takes to validate the model.
422422
:param validation_dataset_size: Validation dataset size.
423423
:return: The number of parallel ranker processes
424424
"""
425-
if preperation_time < PREPARATION_MODEL_THRESHOLD:
425+
if preparation_time < PREPARATION_MODEL_THRESHOLD:
426426
return 1
427427

428428
# Calculate the number of parallel processes needed to override model preparation and
429429
# metric calculation on the ranking subset
430430
ranking_time = validation_time * self.ranking_subset_size / validation_dataset_size
431-
n_proc = max(round((preperation_time / ranking_time + 1) * OVERHEAD_COEFFICIENT), 2)
431+
n_proc = max(round((preparation_time / ranking_time + 1) * OVERHEAD_COEFFICIENT), 2)
432432

433433
# Apply limitation by number of CPU cores
434-
n_cores = get_available_cpu_count()
434+
n_cores = get_available_cpu_count(logical=True)
435435
n_proc = max(min(n_proc, n_cores // 2), 1)
436436

437-
# Apply limitation by memmory
437+
# Apply limitation by memory
438438
ram = get_available_memory_amount()
439439
n_copies = ram // (model_size * MEMORY_INCREASE_COEFFICIENT)
440440
n_proc = max(min(n_proc, n_copies - 1), 1)
@@ -504,9 +504,9 @@ def _collect_metric_and_values(
504504
model: TModel, dataset: Dataset, evaluator: Evaluator, model_name: str
505505
) -> MetricResults:
506506
nncf_logger.info(f"Validation of {model_name} model was started")
507-
with timer() as preperation_time:
507+
with timer() as preparation_time:
508508
model_for_inference = evaluator.prepare_model_for_inference(model)
509509
with timer() as validation_time:
510510
metric, values_for_each_item = evaluator.validate_model_for_inference(model_for_inference, dataset)
511511
nncf_logger.info(f"Metric of {model_name} model: {metric}")
512-
return MetricResults(metric, values_for_each_item, preperation_time(), validation_time())
512+
return MetricResults(metric, values_for_each_item, preparation_time(), validation_time())

0 commit comments

Comments
 (0)