arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2604.23807cs.PL
收藏

通过分阶段的形状依赖类型实现编译时张量形状检查

Compile-Time Tensor Shape Checking via Staged Shape-Dependent Types

Kyoto University(京都大学);Imiron Co., Ltd.(Imiron有限公司)

Takashi Suwa, Atsushi Igarashi

AI总结:

本文提出通过分阶段的形状依赖类型实现编译时张量形状检查,通过编译时计算验证张量一致性,减少手动证明的负担,提高开发效率。

摘要:

在编写涉及矩阵或张量的程序时,希望在实际计算前排除张量形状不一致的问题。为此,某些语言提供依赖类型如Mat m n,其他语言则提供细化类型来跟踪形状的谓词。然而,尽管理论成熟,这些方法在持续软件开发中往往不便,因为需要证明类型相等或子类型。为解决此问题,本文提供了一种替代的正式化方法,利用分阶段。基于观察到在许多典型情况下,形状一致性条件可以在实际张量计算前提取,我们通过编译时计算评估的断言来确保一致性,而不是通过证明。在此正式化下,我们可以在编译时计算中虚拟静态地验证一致性,即形状不一致将在编译时计算中立即检测为断言失败。本文的工作提供了数学保证,确保生成的代码始终与张量形状一致。此外,为了大大减少添加形状或阶段相关描述的负担,我们(1)允许与形状相关的参数隐式,并尽力推断它们,(2)提供一种非分阶段的表面语言,其似乎类似于普通的依赖类型语言,并将其程序翻译成分阶段的核心语言。通过原型实现,我们确认我们的语言足够表达力,可以验证多个程序,包括几个由ocaml-torch提供的示例。

英文摘要:

When writing programs involving matrices or tensors in general, it is desirable to rule out the inconsistency of tensor shapes (i.e., the generalization of matrix sizes) before actual computation. For this purpose, some languages provide dependent types such as Mat m n, and others offer refinement types to track predicates for shapes. Despite the theoretical maturity, however, such methods are often unhandy for continuous software development due to the requirement of proofs for judging type equality or subtyping; even automated proving is often unsuitable due to its unforeseeable time consumption. To remedy this, our study provides an alternative formalization by using staging. Based on the observation that conditions for the shape consistency can be extracted before running the actual tensor computations in many typical cases, we ensure such consistency by assertions evaluated as compile-time computations, not by proofs. Under this formalization, we can verify the consistency virtually statically in the sense that inconsistencies as to tensor shapes will be immediately detected as assertion failures during compile-time computation. Our work achieves a mathematical guarantee that successfully generated code is always consistent with respect to tensor shapes. Furthermore, to vastly lessen the burden of adding shape- or stage-related descriptions, we (1) allow shape-related arguments to be implicit and infer them in a best-effort manner, and (2) offer a non-staged surface language that seemingly resembles ordinary dependently-typed languages and translate its programs into the staged core language. By a prototype implementation, we confirm that our language is expressive enough to verify a number of programs, including several examples offered by ocaml-torch.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑