Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/paddlefleet_ops/src/paddlefleet_ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,65 @@

import paddle


# ---------------------------------------------------------------------------
# Patch CUTLASS DSL scalar types to cache __c_pointers__ results.
# Prevents pymalloc arena fragmentation from short-lived ctypes objects
# created on every kernel launch. Must run before any CUTLASS kernel is called.
# ---------------------------------------------------------------------------
def _patch_cutlass_cptr_cache(maxsize=4096):
try:
from cutlass.base_dsl import typing as _typing
except Exception:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 建议 这里会把 cutlass.base_dsl.typing 导入过程中的任意异常静默吞掉。

当前补丁是否生效完全依赖这个导入;如果安装了 nvidia-cutlass-dsl 但其依赖或初始化异常,函数会直接返回 0,后面也不会打印失败原因,训练进程仍然带着未缓存的 __c_pointers__ 继续跑。按 checklist §C,except Exception 吞异常且无日志属于错误静默。

建议只把 ImportError / ModuleNotFoundError 作为 CUTLASS 不存在的正常路径;对其它异常至少记录 warning 后返回,或让异常暴露出来。

return 0
_logger = logging.getLogger(__name__)
_warned = set()
count = 0

def _make_cached(orig_fn, cache, cls_name):
def _cached(self):
v = self.value
r = cache.get(v)
if r is None:
r = orig_fn(self)
if len(cache) < maxsize:
cache[v] = r
elif cls_name not in _warned:
_warned.add(cls_name)
_logger.warning(
f"[cptr_cache] {cls_name}.__c_pointers__ cache exceeded "
f"{maxsize} entries. Dynamic scalar values may cause "
f"pymalloc arena fragmentation."
)
return r

return _cached

_Numeric = getattr(_typing, "Numeric", None)
for name in dir(_typing):
cls = getattr(_typing, name)
if (
isinstance(cls, type)
and "__c_pointers__" in cls.__dict__
and _Numeric
and issubclass(cls, _Numeric)
and cls is not _Numeric
and not name.startswith("_")
):
cache = {}
cls.__c_pointers__ = _make_cached(cls.__c_pointers__, cache, name)
count += 1
return count


_patched_count = _patch_cutlass_cptr_cache()
if _patched_count:
logging.getLogger(__name__).warning(
f"[cptr_cache] Patched {_patched_count} CUTLASS DSL types"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 建议 这里的成功日志和 PR 描述要求的验证信号不一致。

PR 描述让使用者搜索 [cptr_cache] Result: patched 18 types,但代码实际输出 [cptr_cache] Patched {N} CUTLASS DSL types。按现状验证步骤会误判补丁未生效。

建议统一为描述中的固定格式,例如 "[cptr_cache] Result: patched {_patched_count} types",或同步更新 PR 描述中的搜索串。

)
del _patch_cutlass_cptr_cache, _patched_count
# ---------------------------------------------------------------------------

from .utils import (
HardwareIncompatibleBlocker,
ModuleContext,
Expand Down
Loading