為什麼要有 eval_is_non_overlapping_and_dense?PyTorch 包裝層與調用端解析

前言

前篇 為什麼這個 Tensor 算 dense?PyTorch _eval_is_non_overlapping_and_dense 深入解析 介紹了 _eval_is_non_overlapping_and_dense 函數,該函數位於 torch/fx/experimental/symbolic_shapes.py,實作了判斷張量的記憶體佈局是否「非重疊且稠密」(non-overlapping and dense)的邏輯,是個純 Python 函數。

本篇介紹的 eval_is_non_overlapping_and_dense 函數則是對 _eval_is_non_overlapping_and_dense 函數的包裝,與它同樣是一個模組層級的函數,並開始跟 PyTorch 的編譯系統產生關聯。

注:從本節開始,會牽涉到 PyTorch 中的動態形狀系統,建議先閱讀 PyTorch 的官方文檔:Dynamic Shapes Core Concepts

eval_is_non_overlapping_and_dense

eval_is_non_overlapping_and_dense 位於 torch/fx/experimental/symbolic_shapes.py,實作如下:

python 复制代码
# TODO: Deduplicate this with torch/_prims_common/__init__.py
def eval_is_non_overlapping_and_dense(sizes, strides):
    return int(guard_bool(_eval_is_non_overlapping_and_dense(sizes, strides)))

可以看到它的參數跟 _eval_is_non_overlapping_and_dense 一樣是 sizesstrides,呼叫 _eval_is_non_overlapping_and_dense 後對其回傳值作了兩層包裝,第一層是 guard_bool,第二層則是 int

為何要做這兩層包裝呢?回顧前篇 為什麼這個 Tensor 算 dense?PyTorch _eval_is_non_overlapping_and_dense 深入解析,當中提到了 _eval_is_non_overlapping_and_dense 函數回傳的型別在一般情況下是 bool,但也有回傳 SymBool 的情況,所以此處才在它回傳後多包一層 guard_bool 做保險,即使 _eval_is_non_overlapping_and_dense 真的回傳了 SymBoolguard_bool 也能將它轉成 bool 型別。

第二層包裝則是將 bool 轉為 int 給後續函數做使用。

調用端

IsNonOverlappingAndDenseIndicator

torch/fx/experimental/symbolic_shapes.py

eval_is_non_overlapping_and_dense 會在 IsNonOverlappingAndDenseIndicator.eval 方法中被調用:

python 复制代码
class IsNonOverlappingAndDenseIndicator(sympy.Function):
    # ...
    @classmethod
    def eval(cls, *args):
        # ...
            return eval_is_non_overlapping_and_dense(
                [int(a) for a in size_args],
                [int(a) for a in stride_args]
            )
        return None

IsNonOverlappingAndDenseIndicator 正是我們下一篇的主題。

SYMPY_INTERP

torch/fx/experimental/symbolic_shapes.py

python 复制代码
SYMPY_INTERP = {
    'Eq': operator.eq,
    'Ne': operator.ne,
    'Gt': operator.gt,
    'Lt': operator.lt,
    'Le': operator.le,
    'Ge': operator.ge,
    'Min': min,
    'Max': max,
    'Mod': operator.mod,
    'FloorDiv': operator.floordiv,
    'TrueDiv': operator.truediv,
    'IsNonOverlappingAndDenseIndicator': eval_is_non_overlapping_and_dense,
    'floor': math.floor,
    'ceiling': math.ceil,
}

SYMPY_INTERP 字典中,作為 'IsNonOverlappingAndDenseIndicator' 鍵的值,在執行期驗證時會用到。

sizes_strides_user

_make_node_sizes_strides 函數同樣位於 torch/fx/experimental/symbolic_shapes.py,是一個模組層級的函數,當中調用了 eval_is_non_overlapping_and_dense

python 复制代码
def _make_node_sizes_strides(method, func):
    # ...
    # TODO: This is technically hotpath, but in the ideal end state
    # guards on this will resolve at a higher level so you never
    # spend time in this code
    def sizes_strides_user(sizes, strides):
        for a in itertools.chain(sizes, strides):
            if isinstance(a, SymInt):
                return wrap_node(getattr(a.node, method)(
                    [to_node(a.node, b) for b in sizes],
                    [to_node(a.node, b) for b in strides],
                ))
        if method == "is_non_overlapping_and_dense_indicator":
            return eval_is_non_overlapping_and_dense(sizes, strides)
        else:
            # TODO: this is an awful implementation
            return bool(func(
                [sympy.sympify(a) for a in sizes],
                [sympy.sympify(a) for a in strides],
            ))

PyTorch SymNode 為何找不到方法實作?──sizes_strides_methods 動態安裝機制解析 中有對 sizes_strides_methods 的詳細介紹。

相关推荐
weixin_6686 分钟前
Cursor-superpowers插件用法
数据库·人工智能
adinnet20267 分钟前
深度拆解企业级 Agent 架构:LangGraph + 知识图谱 + 向量检索的协同设计
人工智能·架构·知识图谱
糖果店的幽灵9 分钟前
langgraph分支之 - 动态分支(Dynamic Branch)
java·前端·javascript·人工智能·langgraph
小小代码狗11 分钟前
SQLi-Labs 基础注入实战教程(Less-1 ~ Less-5and Less-9)
服务器·python·php
这张生成的图像能检测吗12 分钟前
(论文速读)SCNN:用于交通场景理解的空间CNN
人工智能·深度学习·目标检测·计算机视觉·道路线检测
@insist12318 分钟前
系统规划与管理师-流程评价与持续改进核心知识-终章
大数据·人工智能·软考·系统规划与管理师·软件水平考试·系统规划与管理工程师
meilindehuzi_a20 分钟前
Workflow 与 Agent 有什么区别:从 LangChain 流水线到智能体决策
前端·人工智能
SLD_Allen22 分钟前
大规模分布式AI训练基础设施
人工智能·分布式·模型训练
码农学院37 分钟前
AI优化AIO技术演进史:从模板生成到多智能体协同创作
人工智能
科技发布41 分钟前
拓氪科技 AI 内容引擎软文投放效果详解
人工智能·科技