-
Notifications
You must be signed in to change notification settings - Fork 18
Add default benchmark scheduler #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
boomanaiden154
merged 14 commits into
main
from
users/boomanaiden154/add-default-benchmark-scheduler
Jan 17, 2025
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4e063aa
[𝘀𝗽𝗿] changes to main this commit is based on
boomanaiden154 e9b41bb
[𝘀𝗽𝗿] initial version
boomanaiden154 d6ef9cd
fix test
boomanaiden154 d993c86
[𝘀𝗽𝗿] changes introduced through rebase
boomanaiden154 26fb3de
address feedback
boomanaiden154 481365d
[𝘀𝗽𝗿] changes introduced through rebase
boomanaiden154 91f7e40
rebase
boomanaiden154 632dc06
Fix CI
boomanaiden154 62b74e0
skip test in CI
boomanaiden154 21e3132
address feedback
boomanaiden154 15663c1
address feedback
boomanaiden154 798eb4e
address feedback
boomanaiden154 89083cb
[𝘀𝗽𝗿] changes introduced through rebase
boomanaiden154 2710a7a
rebase
boomanaiden154 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # Copyright 2024 Google Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Iterable | ||
| import os | ||
| import re | ||
|
|
||
|
|
||
| class BenchmarkScheduler(ABC): | ||
|
|
||
| @abstractmethod | ||
| def setup_and_get_benchmark_core(self) -> int | None: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def verify(self): | ||
| pass | ||
|
|
||
|
|
||
| class NoSchedulingBenchmarkScheduler(BenchmarkScheduler): | ||
|
|
||
| def setup_and_get_benchmark_core(self) -> int | None: | ||
| return None | ||
|
|
||
| def verify(self): | ||
| pass | ||
|
|
||
|
|
||
| class DefaultBenchmarkScheduler(BenchmarkScheduler): | ||
|
|
||
| def __init__(self): | ||
| self._cpu_mask = [] | ||
|
|
||
| @staticmethod | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def _get_neighboring_threads(cpu_index: int) -> list[int]: | ||
| with open( | ||
| f'/sys/devices/system/cpu/cpu{cpu_index}/topology/thread_siblings_list' | ||
| ) as thread_sibling_list_handle: | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| neighboring_threads_strings = re.split( | ||
| r'[-,]+', thread_sibling_list_handle.read().strip() | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| neighboring_threads = [ | ||
| int(cpu_index_str) for cpu_index_str in neighboring_threads_strings | ||
| ] | ||
| return neighboring_threads | ||
|
|
||
| def _get_aux_core_and_hyperthread_pair( | ||
| self, | ||
| cpu_mask: Iterable[int], | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> tuple[int, list[int]]: | ||
| for cpu_index in cpu_mask: | ||
| neighboring_threads = self._get_neighboring_threads(cpu_index) | ||
| if len(neighboring_threads) != 2: | ||
| raise ValueError('Expected two hyperthreads per CPU.') | ||
|
|
||
| if ( | ||
| neighboring_threads[0] in cpu_mask | ||
| and neighboring_threads[1] in cpu_mask | ||
| ): | ||
| cpus = list(cpu_mask) | ||
| cpus.remove(neighboring_threads[0]) | ||
| cpus.remove(neighboring_threads[1]) | ||
| return (cpus[0], [neighboring_threads[0], neighboring_threads[1]]) | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| raise ValueError( | ||
| 'Expected a pair of neighboring hyperthreads in the CPU mask.' | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| def setup_and_get_benchmark_core(self) -> int | None: | ||
| cpu_mask = os.sched_getaffinity(0) | ||
|
|
||
| if len(cpu_mask) != 3: | ||
| raise ValueError('Expected to have three CPUs.') | ||
|
|
||
| aux_core, hyperthread_pair = self._get_aux_core_and_hyperthread_pair( | ||
| cpu_mask | ||
| ) | ||
| os.sched_setaffinity(0, [aux_core]) | ||
| self._cpu_mask = [aux_core] | ||
|
|
||
| return hyperthread_pair[0] | ||
|
|
||
| def verify(self): | ||
| cpu_mask = list(os.sched_getaffinity(0)) | ||
| if self._cpu_mask != cpu_mask: | ||
| raise ValueError('Expected the CPU mask to not change.') | ||
128 changes: 128 additions & 0 deletions
128
gematria/datasets/pipelines/benchmark_cpu_scheduler_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Copyright 2024 Google Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| from collections.abc import Iterable | ||
|
|
||
| from absl.testing import absltest | ||
|
|
||
| from gematria.datasets.pipelines import benchmark_cpu_scheduler | ||
|
|
||
|
|
||
| class BenchmarkSchedulerTests(absltest.TestCase): | ||
|
|
||
| def test_no_scheduling(self): | ||
| scheduler = benchmark_cpu_scheduler.NoSchedulingBenchmarkScheduler() | ||
| self.assertIsNone(scheduler.setup_and_get_benchmark_core()) | ||
| scheduler.verify() | ||
|
|
||
| def test_default_scheduler_get_neighboring_threads(self): | ||
| scheduler = benchmark_cpu_scheduler.DefaultBenchmarkScheduler() | ||
| neighboring_threads = scheduler._get_neighboring_threads(0) | ||
|
|
||
| # Just check that we get two CPU ids back that are not the same. We cannot | ||
| # do much more without knowing more about the system topology, and this | ||
| # should be a reasonable enough test. | ||
| self.assertLen(neighboring_threads, 2) | ||
| self.assertNotEqual(neighboring_threads[0], neighboring_threads[1]) | ||
|
|
||
| @staticmethod | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def _set_normal_affinity(): | ||
| cpu_mask = os.sched_getaffinity(0) | ||
| cpu_mask_list = list(cpu_mask) | ||
| aux_cpu = cpu_mask.pop() | ||
| hyperthread_pair_part = cpu_mask.pop() | ||
| hyperthread_pair = benchmark_cpu_scheduler.DefaultBenchmarkScheduler._get_neighboring_threads( | ||
| hyperthread_pair_part | ||
| ) | ||
| new_cpu_mask = [aux_cpu, *hyperthread_pair] | ||
|
|
||
| os.sched_setaffinity(0, new_cpu_mask) | ||
| return (aux_cpu, hyperthread_pair, cpu_mask_list) | ||
|
|
||
| @staticmethod | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def _reset_cpu_affinity(cpu_mask: Iterable[int]): | ||
| os.sched_setaffinity(0, cpu_mask) | ||
|
|
||
| def test_default_scheduler_get_cores(self): | ||
| expected_aux_cpu, expected_hyperthread_pair, old_cpu_mask = ( | ||
| self._set_normal_affinity() | ||
| ) | ||
| scheduler = benchmark_cpu_scheduler.DefaultBenchmarkScheduler() | ||
| cpu_mask = os.sched_getaffinity(0) | ||
| aux_cpu, hyperthread_pair = scheduler._get_aux_core_and_hyperthread_pair( | ||
| cpu_mask | ||
| ) | ||
| self.assertEqual(aux_cpu, expected_aux_cpu) | ||
| self.assertContainsSubsequence(hyperthread_pair, expected_hyperthread_pair) | ||
| self._reset_cpu_affinity(old_cpu_mask) | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def test_default_scheduler_get_cores_no_neighboring_threads(self): | ||
| cpu_mask = os.sched_getaffinity(0) | ||
| three_cores = [cpu_mask.pop(), cpu_mask.pop(), cpu_mask.pop()] | ||
|
|
||
| scheduler = benchmark_cpu_scheduler.DefaultBenchmarkScheduler() | ||
| with self.assertRaises(ValueError): | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| scheduler._get_aux_core_and_hyperthread_pair(three_cores) | ||
|
|
||
| def test_default_scheduler_setup(self): | ||
| expected_aux_cpu, expected_hyperthread_pair, old_cpu_mask = ( | ||
| self._set_normal_affinity() | ||
| ) | ||
|
|
||
| scheduler = benchmark_cpu_scheduler.DefaultBenchmarkScheduler() | ||
| benchmark_core = scheduler.setup_and_get_benchmark_core() | ||
| self.assertIn(benchmark_core, expected_hyperthread_pair) | ||
| set_cpu_mask = os.sched_getaffinity(0) | ||
| self.assertLen(set_cpu_mask, 1) | ||
| self.assertEqual(set_cpu_mask.pop(), expected_aux_cpu) | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| self._reset_cpu_affinity(old_cpu_mask) | ||
|
|
||
| def test_default_scheduler_not_three_cpus(self): | ||
| old_cpu_mask = os.sched_getaffinity(0) | ||
| cpu_mask_list = list(old_cpu_mask) | ||
| os.sched_setaffinity(0, cpu_mask_list[0:2]) | ||
|
|
||
| scheduler = benchmark_cpu_scheduler.DefaultBenchmarkScheduler() | ||
| with self.assertRaises(ValueError): | ||
| scheduler.setup_and_get_benchmark_core() | ||
|
|
||
| os.sched_setaffinity(0, old_cpu_mask) | ||
|
|
||
| def test_default_scheduler_verify(self): | ||
| _, _, old_cpu_mask = self._set_normal_affinity() | ||
|
|
||
| scheduler = benchmark_cpu_scheduler.DefaultBenchmarkScheduler() | ||
| scheduler.setup_and_get_benchmark_core() | ||
| scheduler.verify() | ||
|
|
||
| self._reset_cpu_affinity(old_cpu_mask) | ||
|
|
||
| def test_default_scheduler_verify_mask_changed(self): | ||
| _, _, old_cpu_mask = self._set_normal_affinity() | ||
|
|
||
| scheduler = benchmark_cpu_scheduler.DefaultBenchmarkScheduler() | ||
| scheduler.setup_and_get_benchmark_core() | ||
|
|
||
| cpu_mask_list = list(old_cpu_mask) | ||
| os.sched_setaffinity(0, cpu_mask_list[1:3]) | ||
| with self.assertRaises(ValueError): | ||
| scheduler.verify() | ||
|
|
||
| self._reset_cpu_affinity(old_cpu_mask) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| absltest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.