為什麼要有 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 的詳細介紹。

相关推荐
richard_first3 分钟前
Transformer 与大语言模型:第10章 Residual (残差连接)
人工智能·深度学习·机器学习·transformer
denggun123453 分钟前
信号量(DispatchSemaphore vs AsyncSemaphore)、swift协作式线程池 and python信号量
开发语言·python·swift
zhongerzixunshi4 分钟前
深耕绿色建材认证 赋能建筑行业低碳高质量发展
人工智能
Wang's Blog5 分钟前
Vibe Coding一人即团队系列10: 在 Claude 与 Codex 中接入 DeepSeek 模型
人工智能
lisw056 分钟前
计算与科学哲学(Philosophy of Computing and Science)
java·开发语言·人工智能
天天进步20158 分钟前
Pixelle-Video 源码解析 #10:多模型适配:GPT、通义千问、DeepSeek、Ollama 如何统一调用?
人工智能·gpt
@atweiwei9 分钟前
用 Rust 构建 Agent 应用的高性能框架:langchainrust 架构全景
人工智能·架构·rust·langchain·llm·agent·ai编程
欧特克_Glodon13 分钟前
OpenCV计算机视觉开发入门与实践<二十二>:图像平滑之线性滤波
c++·人工智能·opencv·计算机视觉
起光13 分钟前
Python类与对象(一)
python
ShiXZ21315 分钟前
ChatGPT Plus用户注意:5小时限制今日恢复,额度每5小时自动刷新(且额度又又又重置了)
人工智能·chatgpt