Skip to content

Commit 6a341a3

Browse files
authored
Update launch script for preset env vars and ld_preload fixing (#1603)
* avoid environment variable configuration in the launch script modifies system values * fix preload issue with preset LD_PRELOAD values
1 parent 6e2ffa8 commit 6a341a3

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

intel_extension_for_pytorch/cpu/launch/launcher_base.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, logger=None, lscpu_txt=''):
2222
self.ma_supported = ['auto', 'default', 'tcmalloc', 'jemalloc']
2323
self.omp_supported = ['auto', 'default', 'intel']
2424
self.environ_set = {}
25+
self.ld_preload = os.environ['LD_PRELOAD'].split(':') if 'LD_PRELOAD' in os.environ else []
2526

2627
def add_common_params(self, parser):
2728
group = parser.add_argument_group('Launcher Common Arguments')
@@ -83,12 +84,7 @@ def add_lib_preload(self, lib_type):
8384
'''
8485
lib_found = False
8586
lib_set = False
86-
if not 'LD_PRELOAD' in self.environ_set.keys():
87-
self.environ_set['LD_PRELOAD'] = ''
88-
if self.environ_set['LD_PRELOAD'] == '' and 'LD_PRELOAD' in os.environ:
89-
self.environ_set['LD_PRELOAD'] = os.environ['LD_PRELOAD']
90-
ld_preload = self.environ_set['LD_PRELOAD']
91-
for item in ld_preload.split(':'):
87+
for item in self.ld_preload:
9288
if item.endswith(f'lib{lib_type}.so'):
9389
lib_set = True
9490
break
@@ -99,15 +95,9 @@ def add_lib_preload(self, lib_type):
9995
library_file = f'{lib_path}/lib{lib_type}.so'
10096
matches = glob.glob(library_file)
10197
if len(matches) > 0:
102-
if ld_preload == '':
103-
ld_preload = matches[0]
104-
else:
105-
ld_preload = f'{matches[0]}:{ld_preload}'
106-
self.environ_set['LD_PRELOAD'] = ld_preload
98+
self.ld_preload.append(matches[0])
10799
lib_found = True
108100
break
109-
if self.environ_set['LD_PRELOAD'] == '':
110-
del self.environ_set['LD_PRELOAD']
111101
return lib_set or lib_found
112102

113103
def add_env(self, env_name, env_value):
@@ -174,6 +164,13 @@ def set_lib_bin_from_list(self, name_input, name_map, category, supported, fn, s
174164
self.verbose('info', f'Use \'{name_local}\' {category}.')
175165
else:
176166
self.verbose('info', f'Use \'{name_local}\' {category}.')
167+
if fn == self.add_lib_preload:
168+
for k,v in name_map.items():
169+
if k == name_local:
170+
continue
171+
for item in self.ld_preload:
172+
if item.endswith(f'lib{v[0]}.so'):
173+
self.ld_preload.remove(item)
177174
return name_local
178175

179176
def set_memory_allocator(self, memory_allocator='auto', benchmark=False, skip_list=[]):

intel_extension_for_pytorch/cpu/launch/launcher_distributed.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ def launch(self, args):
185185
self.add_env('CCL_WORKER_COUNT', str(args.ccl_worker_count))
186186
self.add_env('CCL_WORKER_AFFINITY', pin_domain_affinity['affinity'])
187187

188+
ld_preload_marker = 'LD_PRELOAD_UNSET'
189+
ld_preload_bk = os.environ['LD_PRELOAD'] if 'LD_PRELOAD' in os.environ else ld_preload_marker
190+
if len(self.ld_preload) > 0:
191+
os.environ['LD_PRELOAD'] = ':'.join(self.ld_preload)
192+
self.verbose('info', f'LD_PRELOAD={os.environ["LD_PRELOAD"]}')
193+
else:
194+
if 'LD_PRELOAD' in os.environ:
195+
del os.environ['LD_PRELOAD']
188196
for k,v in self.environ_set.items():
189197
self.verbose('info', f'env: {k}={v}')
190198

@@ -211,6 +219,11 @@ def launch(self, args):
211219
self.verbose('info', f'cmd: {cmd_s}')
212220
process = subprocess.Popen(cmd_s, env=os.environ, shell=True)
213221
process.wait()
222+
if ld_preload_bk == ld_preload_marker:
223+
if 'LD_PRELOAD' in os.environ:
224+
del os.environ['LD_PRELOAD']
225+
else:
226+
os.environ['LD_PRELOAD'] = ld_preload_bk
214227
if args.log_dir:
215228
log_fns = []
216229
for i in range(args.nnodes * args.nprocs_per_node):

intel_extension_for_pytorch/cpu/launch/launcher_multi_instances.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,11 @@ def launch(self, args):
180180
args.ninstances = len(self.cpuinfo.pools_ondemand)
181181
args.ncores_per_instance = len(self.cpuinfo.pools_ondemand[0])
182182

183-
preset_ld_preload = os.environ.get('LD_PRELOAD', '')
184-
is_iomp_set = 'libiomp5.so' in preset_ld_preload
183+
is_iomp_set = False
184+
for item in self.ld_preload:
185+
if item.endswith('libiomp5.so'):
186+
is_iomp_set = True
187+
break
185188
is_kmp_affinity_set = True if 'KMP_AFFINITY' in os.environ else False
186189
set_kmp_affinity = True
187190
# When using all cores on all nodes, including logical cores, setting KMP_AFFINITY disables logical cores. Thus, KMP_AFFINITY should not be set.
@@ -199,13 +202,23 @@ def launch(self, args):
199202
task_mgr = self.set_multi_task_manager(args.multi_task_manager, skip_list=skip_list)
200203

201204
# Set environment variables for multi-instance execution
205+
self.verbose('info', 'env: Untouched preset environment variables are not displayed.')
206+
environ_local = {}
207+
for k,v in os.environ.items():
208+
if k == 'LD_PRELOAD':
209+
continue
210+
environ_local[k] = v
211+
if len(self.ld_preload) > 0:
212+
environ_local['LD_PRELOAD'] = ':'.join(self.ld_preload)
213+
self.verbose('info', f'env: LD_PRELOAD={environ_local["LD_PRELOAD"]}')
202214
for k,v in self.environ_set.items():
203215
if task_mgr == self.tm_supported[1]:
204216
if omp_runtime == 'default' and k == 'GOMP_CPU_AFFINITY':
205217
continue
206218
if omp_runtime == 'intel' and k == 'KMP_AFFINITY':
207219
continue
208220
self.verbose('info', f'env: {k}={v}')
221+
environ_local[k] = v
209222

210223
if args.auto_ipex:
211224
args.program = auto_ipex.apply_monkey_patch(args.program, args.dtype, args.auto_ipex_verbose, args.disable_ipex_graph_mode)
@@ -225,7 +238,7 @@ def launch(self, args):
225238
args = args,
226239
omp_runtime = omp_runtime,
227240
task_mgr = task_mgr,
228-
environ = self.environ_set,
241+
environ = environ_local,
229242
cpu_pools = self.cpuinfo.pools_ondemand,
230243
index = i)
231244
processes.append(process)

tests/cpu/test_launcher.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,18 @@ def del_env(self, env_name):
3838
def test_memory_allocator_setup(self):
3939
launcher = Launcher()
4040

41-
# preset ld_preload
42-
ld_preload_bk = os.getenv('LD_PRELOAD', 'UNSET')
43-
os.environ['LD_PRELOAD'] = 'LD_PRELOAD_TEST'
44-
find_tcmalloc = self.find_lib('tcmalloc')
45-
launcher.set_memory_allocator(memory_allocator='tcmalloc')
46-
tcmalloc_enabled = 'libtcmalloc.so' in launcher.environ_set['LD_PRELOAD']
47-
self.assertEqual(find_tcmalloc, tcmalloc_enabled)
48-
self.assertEqual('LD_PRELOAD_TEST' in launcher.environ_set['LD_PRELOAD'], True)
49-
if ld_preload_bk == 'UNSET':
50-
del os.environ['LD_PRELOAD']
51-
else:
52-
os.environ['LD_PRELOAD'] = ld_preload_bk
53-
5441
# tcmalloc
5542
find_tcmalloc = self.find_lib('tcmalloc')
5643
launcher.set_memory_allocator(memory_allocator='tcmalloc')
57-
tcmalloc_enabled = 'libtcmalloc.so' in launcher.environ_set['LD_PRELOAD']
44+
ld_preload = ':'.join(launcher.ld_preload) if len(launcher.ld_preload) > 0 else ''
45+
tcmalloc_enabled = 'libtcmalloc.so' in ld_preload
5846
self.assertEqual(find_tcmalloc, tcmalloc_enabled)
5947

6048
# jemalloc
6149
find_jemalloc = self.find_lib('jemalloc')
6250
launcher.set_memory_allocator(memory_allocator='jemalloc')
63-
jemalloc_enabled = 'libjemalloc.so' in launcher.environ_set['LD_PRELOAD']
51+
ld_preload = ':'.join(launcher.ld_preload) if len(launcher.ld_preload) > 0 else ''
52+
jemalloc_enabled = 'libjemalloc.so' in ld_preload
6453
self.assertEqual(find_jemalloc, jemalloc_enabled)
6554
if jemalloc_enabled:
6655
self.assertTrue('MALLOC_CONF' in launcher.environ_set)

0 commit comments

Comments
 (0)