variable scope of `@thunk`

未关闭
#684 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@chengchingwen 已经在做这个了。

开始于 2025年2月22日。

  • #686 来自 @chengchingwen —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
32/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
julia

调研方向

首先检查 @thunk 宏、_usethunks() 路径和 issue #683,以了解 second-order AD 的预期行为。复现 caller-scope 和 Core.Box 类型不稳定性问题,然后在 NeuralAttentionlib.jl 中验证相关的 rrule 类型稳定性检查,同时确保两个分支中的 thunk 行为正确。

由索引模型根据 Issue 内容生成。

描述

#683 introduces a separate path when creating the thunk (_usethunks() ? Thunk($(esc(func))) : $(esc(body))). However, this would introduce new variables into the caller's scope and create Core.Box thus causing type instability, which breaks some rrule type stability checks in NeuralAttentionlib.jl.

Currently, I have two ways to solve this:

  1. replace $(esc(body)) with $(esc(func))() which turns the else path into a function call, thus no variable introduction.
  2. mimic how @async and @spawn using $ to interpolate values. so @thunk would be defined as:
macro thunk(body)
    letargs = Base._lift_one_interp!(body)
    func = Base.replace_linenums!(:(()->($(esc(body)))), __source__)
    return quote
        if _usethunks()
            let $(letargs...)
                Thunk($func)
            end
        else
            let $(letargs...)
                $(esc(body))
            end
        end
    end
end

However, I don't understand #683 enough to see what would be better for 2nd order AD. Maybe @pxl-th or @mcabbott would have some thoughts on this.

主要语言
Julia
星标
267
派生
66
平均合并
1 天 1 小时
30 天内合并 PR
9

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JuliaDiff/ChainRulesCore.jl 的其他 Issue

查看 JuliaDiff/ChainRulesCore.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。