Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

checktag's permissive fallback silently disables tag checking (e.g. for derivative(f!, y, x, cfg))

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

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
50/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
领域
tooling

调研方向

先从 checktag 方法和 derivative/derivative! 的调用点开始,然后将它们的行为与 gradient 的 InvalidTagException 路径进行比较。当针对错误函数的 DerivativeConfig 在标量 derivative 调用中被拒绝,并且已考虑文档中 nothing 的行为和自定义标签的行为时,即视为完成。

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

描述

A DerivativeConfig built for one function is silently accepted by another, so derivative(f!, y, x, cfg) gets no perturbation confusion protection at all:

julia> a!(y, x) = (y .= sin(x)); b!(y, x) = (y .= cos(x));

julia> y = Vector{Float64}(undef, 2);

julia> cfg = ForwardDiff.DerivativeConfig(a!, y, 0.0);

julia> ForwardDiff.derivative(b!, y, 0.0, cfg)   # cfg belongs to a!, no complaint
2-element Vector{Float64}:
 -0.0
 -0.0

gradient does raise InvalidTagException in the analogous situation. This reproduces with a real y on master, so it is unrelated to complex support.

The immediate cause is that every non-fallback checktag method requires x::AbstractArray:

checktag(::Type{Tag{FT,VT}}, f::F, x::AbstractArray{V}) where {FT,VT,F,V} = throw(InvalidTagException{...}())
checktag(::Type{Tag{F,V}}, f::F, x::AbstractArray{V}) where {F,V} = true

while derivative/derivative! call checktag(T, f!, x) with a scalar x. Dispatch lands on the catch-all checktag(z, f, x) = true, so the check argument of derivative(f!, y, x, cfg, Val{true}()) has no effect.

Adding scalar counterparts of those two methods fixes this particular case, but the more interesting question is whether the permissive checktag(z, f, x) = true default should stay. As it stands, any signature that does not match a specific method silently opts out of checking, which is how this gap went unnoticed -- the failure mode is "no error", not "no method". Defaulting to rejection and explicitly accepting only validated tags would make such gaps loud instead of silent.

Two things to note for anyone attempting that:

  • The return value is currently discarded -- every call site is CHK && checktag(...), and checktag works purely by throwing. So flipping the default to false on its own changes nothing; the call sites would have to throw on a false result.
  • Nothing needs to be accepted explicitly, since a nothing tag is documented as usable with any target function. Custom tag types currently pass via the same fallback ("custom tag: you're on your own"), so rejecting by default would be breaking for them unless they opt in by defining checktag for their tag type -- arguably the point, but worth a deprecation path.
主要语言
Julia
星标
1k
派生
162
PR 合并指标
30 天内没有已合并 PR

环境准备

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

从这里开始

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

JuliaDiff/ForwardDiff.jl 的其他 Issue

查看 JuliaDiff/ForwardDiff.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 发到你的邮箱

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