Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jerrrrry
infinicore
Commits
1a576d41
Commit
1a576d41
authored
Jan 22, 2026
by
Ceng23333
Browse files
issue/961: fix metax init with preload
Signed-off-by:
Ceng23333
<
441651826@qq.com
>
parent
148b475b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
125 additions
and
0 deletions
+125
-0
python/infinicore/__init__.py
python/infinicore/__init__.py
+4
-0
python/infinicore/_preload.py
python/infinicore/_preload.py
+121
-0
No files found.
python/infinicore/__init__.py
View file @
1a576d41
import
contextlib
with
contextlib
.
suppress
(
ImportError
):
from
._preload
import
preload
preload
()
import
infinicore.context
as
context
import
infinicore.nn
as
nn
...
...
python/infinicore/_preload.py
0 → 100644
View file @
1a576d41
import
ctypes
import
os
from
typing
import
Iterable
,
List
def
_candidate_prefixes
(
path
:
str
)
->
List
[
str
]:
"""
Return HPCC install prefixes to search for libs.
Prefer HPCC_PATH; if absent and explicitly opted-in, fall back to /opt/hpcc.
"""
prefixes
:
List
[
str
]
=
[]
if
path
:
prefixes
.
append
(
path
)
seen
=
set
()
unique
:
List
[
str
]
=
[]
for
p
in
prefixes
:
if
p
and
p
not
in
seen
:
seen
.
add
(
p
)
unique
.
append
(
p
)
return
unique
def
_try_load
(
paths
:
Iterable
[
str
],
name
:
str
)
->
bool
:
"""Try to load a shared library from given paths or system search path."""
for
path
in
paths
:
full
=
os
.
path
.
join
(
path
,
"lib"
,
name
)
if
os
.
path
.
exists
(
full
):
try
:
ctypes
.
CDLL
(
full
,
mode
=
ctypes
.
RTLD_GLOBAL
)
return
True
except
OSError
:
# Try next candidate
continue
# Last resort: rely on loader search path
try
:
ctypes
.
CDLL
(
name
,
mode
=
ctypes
.
RTLD_GLOBAL
)
return
True
except
OSError
:
return
False
def
preload_hpcc
()
->
None
:
"""
Best-effort preload of key HPCC runtime libs with RTLD_GLOBAL.
This mirrors the behavior of torch's HPCC build that loads libtorch_global_deps.so,
but avoids introducing a hard torch dependency. All failures are swallowed.
"""
hpcc_path
=
os
.
getenv
(
"HPCC_PATH"
)
if
not
hpcc_path
:
return
prefixes
=
_candidate_prefixes
(
hpcc_path
)
libs
=
[
"libhcruntime.so"
,
"libhcToolsExt.so"
,
"libruntime_cu.so"
,
"libhccompiler.so"
,
]
for
lib
in
libs
:
_try_load
(
prefixes
,
lib
)
def
_should_preload_device
(
device_type
:
str
)
->
bool
:
"""
Check if preload is needed for a specific device type.
"""
device_env_map
=
{
"METAX"
:
[
"HPCC_PATH"
,
"INFINICORE_PRELOAD_HPCC"
],
# HPCC/METAX
# Add other device types here as needed:
# "ASCEND": ["ASCEND_PATH"],
# "CAMBRICON": ["NEUWARE_HOME"],
}
env_vars
=
device_env_map
.
get
(
device_type
,
[])
for
env_var
in
env_vars
:
if
os
.
getenv
(
env_var
):
return
True
return
False
def
preload_device
(
device_type
:
str
)
->
None
:
"""
Preload runtime libraries for a specific device type if needed.
Args:
device_type: Device type name (e.g., "METAX", "ASCEND", etc.)
"""
if
device_type
==
"METAX"
:
preload_hpcc
()
# Add other device preload functions here as needed:
# elif device_type == "ASCEND":
# preload_ascend()
# etc.
def
preload
()
->
None
:
"""
Universal preload function that loops through device types and preloads when required.
This function detects available device types and preloads their runtime libraries
if the environment indicates they are needed.
"""
# Device types that may require preload
device_types
=
[
"METAX"
,
# HPCC/METAX
# Add other device types here as they are implemented:
# "ASCEND",
# "CAMBRICON",
# etc.
]
for
device_type
in
device_types
:
if
_should_preload_device
(
device_type
):
try
:
preload_device
(
device_type
)
except
Exception
:
# Swallow all errors - preload is best-effort
pass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment