-
Notifications
You must be signed in to change notification settings - Fork 112
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
set cpu affinity and membind for better oob performance #853
Changes from 11 commits
54bc512
5d19b46
37e1022
9387717
191f772
1a20674
2773fe6
7b37c39
fa5526a
09ac1ce
8b476f8
85ccfc4
5407359
95b65fe
7fb3cb5
3613f69
c825876
4535927
e658ffc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
|
||||
import os | ||||
import re | ||||
from pathlib import Path | ||||
from typing import List, Optional, Union | ||||
|
@@ -110,3 +111,85 @@ def _find_files_matching_pattern( | |||
files = [Path(p) for p in repo_files if re.match(pattern, str(p)) and str(p.parent) == subfolder] | ||||
|
||||
return files | ||||
|
||||
|
||||
def get_int_from_env(env_keys, default): | ||||
"""Returns the first positive env value found in the `env_keys` list or the default.""" | ||||
for e in env_keys: | ||||
val = int(os.environ.get(e, -1)) | ||||
if val >= 0: | ||||
return val | ||||
return default | ||||
|
||||
|
||||
def bind_cores_for_best_perf(): | ||||
""" | ||||
Set number of threads per rank, numa cpu affinity and numa memory binding if not already set for better OOB performance. | ||||
Works for wold_size >= 1 and rank >= 1 | ||||
|
||||
Example: | ||||
.. code-block:: python | ||||
|
||||
from optimum.intel.ipex import IPEXModelForCausalLM | ||||
from optimum.intel.utils.modeling_utils import bind_cores_for_best_perf | ||||
|
||||
bind_cores_for_best_perf() | ||||
model = IPEXModelForCausalLM.from_pretrained("gpt2", torch_dtype=torch.bfloat16, export=True) | ||||
tokenizer = AutoTokenizer.from_pretrained("gpt2") | ||||
input_sentence = ["tell me a story about a trip to the moon"] | ||||
model_inputs = tokenizer(input_sentence, return_tensors="pt") | ||||
generation_kwargs = dict(max_new_tokens=500) | ||||
generated_ids = model.generate(**model_inputs, **generation_kwargs) | ||||
|
||||
Returns: | ||||
None | ||||
|
||||
""" | ||||
|
||||
import importlib.util | ||||
import platform | ||||
|
||||
system = platform.system() | ||||
if system == "Linux": | ||||
if importlib.util.find_spec("numa") is not None: | ||||
import math | ||||
|
||||
import numa | ||||
import psutil | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. math could be at the top here, numa is checked but not psutil
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added is_numa_available to the import_utils.py |
||||
|
||||
local_size = get_int_from_env( | ||||
["MPI_LOCALNRANKS", "OMPI_COMM_WORLD_LOCAL_SIZE", "MV2_COMM_WORLD_LOCAL_SIZE"], 1 | ||||
) | ||||
rank_id = get_int_from_env( | ||||
["LOCAL_RANK", "MPI_LOCALRANKID", "OMPI_COMM_WORLD_LOCAL_RANK", "MV2_COMM_WORLD_LOCAL_RANK"], 0 | ||||
) | ||||
nodes = numa.get_max_node() + 1 | ||||
rank_per_node = math.ceil(local_size / nodes) | ||||
num_cpus_per_nodes = int(psutil.cpu_count(logical=False) / nodes) | ||||
node_id = int(rank_id / rank_per_node) | ||||
rank_offset_per_node = rank_id % rank_per_node | ||||
if os.getenv("OMP_NUM_THREADS") is None: | ||||
# set OMP_NUM_THREADS to num of physical cores per socket | ||||
num_cpus_per_rank = max(int(num_cpus_per_nodes / rank_per_node), 1) | ||||
print("setting OMP_NUM_THREADS to", num_cpus_per_rank) | ||||
IlyasMoutawwakil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
else: | ||||
num_cpus_per_rank = int(os.getenv("OMP_NUM_THREADS")) | ||||
print("OMP_NUM_THREADS already set to ", num_cpus_per_rank) | ||||
if len(numa.get_membind()) == nodes: | ||||
# if numa memory binding is not set, set it to the node where the rank is running | ||||
numa.set_membind([node_id]) | ||||
|
||||
torch.set_num_threads(num_cpus_per_rank) | ||||
|
||||
if len(numa.get_affinity(0)) == psutil.cpu_count(logical=True): | ||||
# if numa affinity is unset (default value is set to all logical cores) set it to the physical cores assigned to the rank | ||||
cpu_start = num_cpus_per_rank * rank_offset_per_node | ||||
numa.set_affinity( | ||||
0, | ||||
list(numa.node_to_cpus(node_id))[cpu_start : cpu_start + num_cpus_per_rank], | ||||
) | ||||
print(f"affinity={numa.get_affinity(0)}, membind = {numa.get_membind()}") | ||||
else: | ||||
print("numa module not found, skipping binding cores") | ||||
else: | ||||
print("OS not supported, skipping binding cores") | ||||
IlyasMoutawwakil marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not at the top ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved