HiTyper:Python 的混合类型推理框架
项目描述
打字机
这是 ICSE 2022 论文“Static Inference Meets Deep Learning: A Hybrid Type InferenceApproach for Python”中发布的工具。
工作流程
HiTyper 是基于类型依赖图(TDG)构建的混合类型推理工具,其典型工作流程如下:
有关详细信息,请参阅论文。
方法论
HiTyper的一般方法是:
-
静态推理是准确的,但由于动态特征而存在覆盖问题
-
深度学习模型与特征无关,但它们很难保持类型正确性并且无法预测看不见的用户定义类型
静态推理与深度学习相结合,相得益彰,在保持准确性的同时提高覆盖率。
安装
要在您自己的计算机上使用 HiTyper,您只需键入:
pip install hityper
或者从源代码构建:(如果需要修改HiTyper的源代码,请使用此方法,pip install .每次修改后重新运行)
git clone https://github.com/JohnnyPeng18/HiTyper.git && cd HiTyper
pip install .
要求:
- 蟒蛇>=3.9
- Linux
HiTyper 需要在 Python >= 3.9 下运行,因为 Python 3.9 在 AST 上引入了很多新节点。然而,由于 Python 的 AST 向后兼容,HiTyper 可以分析大多数在 Python 3 下编写的文件。
建议您使用Anaconda创建干净的 Python 3.9 环境并避免大多数依赖项冲突:
conda create -n hityper python=3.9
用法
目前 HiTyper 有以下命令行选项:(一些重要的设置保存在 file 中config.py,您可能需要在运行 HiTyper 之前对其进行修改)
查找用户类型
hityper findusertype [-h] [-s SOURCE] -p REPO [-v] [-d OUTPUT_DIRECTORY]
optional arguments:
-h, --help show this help message and exit
-s SOURCE, --source SOURCE
Path to a Python source file
-p REPO, --repo REPO Path to a Python project
-v, --validate Validate the imported user-defined types by finding their implementations
-d OUTPUT_DIRECTORY, --output_directory OUTPUT_DIRECTORY
Path to the store the usertypes
例子:
hityper findusertype -s python_project_repo/test.py -p python_project_repo -v -d outputs
该命令生成 HiTyper 收集的自定义类型,并将其保存为.json文件outputs/夹下的文件。
绅士
hityper gentdg [-h] [-s SOURCE] -p REPO [-o] [-l LOCATION] [-a] [-c] [-d OUTPUT_DIRECTORY] [-f {json,pdf}]
optional arguments:
-h, --help show this help message and exit
-s SOURCE, --source SOURCE
Path to a Python source file
-p REPO, --repo REPO Path to a Python project
-o, --optimize Remove redundant nodes in TDG
-l LOCATION, --location LOCATION
Generate TDG for a specific function
-a, --alias_analysis Generate alias graphs along with TDG
-c, --call_analysis Generate call graphs along with TDG
-d OUTPUT_DIRECTORY, --output_directory OUTPUT_DIRECTORY
Path to the generated TDGs
-f {json,pdf}, --output_format {json,pdf}
Formats of output TDGs
例子:
hityper gentdg -s python_project_repo/test.py -p python_project_repo -d outputs -f json -o
此命令为文件中的所有函数生成 TDGpython_project_repo/test.py并将它们保存到outputs文件夹中。
请注意,如果您选择json格式来保存 TDG,它将只有一个json文件包含源文件中的所有 TDG。但是,如果选择pdf格式保存TDG,那么就会有多个pdf文件,每个文件对应源文件中的一个函数。这是因为 pdf 文件几乎不可能包含每个函数的大 TDG。
HiTyper 使用PyCG在调用分析中构建调用图。别名分析和调用分析是暂时内置的,但 HiTyper 不会在推理中使用它们。关于它们的进一步更新将在 HiTyper 中涉及。
推断
hityper infer [-h] [-s SOURCE] -p REPO [-l LOCATION] [-d OUTPUT_DIRECTORY] [-m RECOMMENDATIONS] [-t] [-n TOPN]
optional arguments:
-h, --help show this help message and exit
-s SOURCE, --source SOURCE
Path to a Python source file
-p REPO, --repo REPO Path to a Python project
-l LOCATION, --location LOCATION
Type inference for a specific function
-d OUTPUT_DIRECTORY, --output_directory OUTPUT_DIRECTORY
Path to the generated TDGs
-m RECOMMENDATIONS, --recommendations RECOMMENDATIONS
Path to the recommendations generated by a DL model
-t, --type4py Use Type4Py as the recommendation model
-n TOPN, --topn TOPN Indicate the top n predictions from DL models used by HiTyper
例子:
hityper infer -s python_project_repo/test.py -p python_project_repo -d outputs -n 1 -t
此命令为源文件中的所有变量、参数和返回值生成推断类型并将它们保存到output文件夹中。
如果不指定-mor-t选项,那么 HiTyper 将只使用静态推断部分来推断类型。静态推理通常需要几分钟。
推荐型号:
请注意,HiTyper 本身支持来自 Type4Py 的推荐,如果您使用 option ,它会调用 Type4Py 提供的以下 API 来获取推荐-t:
https://type4py.com/api/predict?tc=0
这会将您的文件上传到 Type4Py 服务器!如果您不想上传文件,可以使用Type4Py提供的 docker 并将 API 更改config.py为:
http://localhost:PORT/api/predict?tc=0
HiTyper 的性能很大程度上取决于推荐模型的最大性能(尤其是预测参数类型的性能)
如果你想使用另一个更强大的模型,你可以编写代码__main__.py来让 HiTyper 适应你的 DL 模型。
评估
hityper eval [-h] -g GROUNDTRUTH -c CLASSIFIED_GROUNDTRUTH -u USERTYPE [-m RECOMMENDATIONS] [-t] [-n TOPN]
optional arguments:
-h, --help show this help message and exit
-g GROUNDTRUTH, --groundtruth GROUNDTRUTH
Path to a ground truth dataset
-c CLASSIFIED_GROUNDTRUTH, --classified_groundtruth CLASSIFIED_GROUNDTRUTH
Path to a classified ground truth dataset
-u USERTYPE, --usertype USERTYPE
Path to a previously collected user-defined type set
-m RECOMMENDATIONS, --recommendations RECOMMENDATIONS
Path to the recommendations generated by a DL model
-t, --type4py Use Type4Py as the recommendation model
-n TOPN, --topn TOPN Indicate the top n predictions from DL models used by HiTyper
例子:
hityper eval -g groundtruth.json -c detailed_groundtruth.json -u usertypes.json -n 1 -t
此命令在预定义的 groundtruth 数据集上评估 HiTyper 的性能。它将输出类似的结果,如Experiment Results部分所述。
在使用此命令评估 Hityper 之前,请使用hityper findusertype命令生成usertypes.json. 这通常需要几个小时,具体取决于文件的数量。
此选项仅用于未来的研究评估。
实验结果
数据集:
以下结果使用ManyTypes4Py数据集进行评估。
由于原始数据集不包含 Python 源文件,为了方便以后的研究,我们这里还附上了HiTyper用于推断类型的 Python 源文件的链接。附加数据集与原始数据集不同,因为原始数据集包含一些不允许开放访问或已被删除的 GitHub 存储库。
请注意,如论文中所述,很少有情况(例如子类型和名称不同的相同类型)HiTyper 应该是正确的,但在评估过程中仍被视为错误。
指标:
有关此处使用的指标的定义,请参阅论文。这些指标可以看作是一种“召回”,它评估了 HiTyper 在特定数据集上的覆盖率。我们在这里不显示“精度”,因为 HiTyper 仅在未观察到任何违反当前打字规则和 TDG 的情况下才输出结果。
仅使用静态推理部分:
| 类别 | 完全符合 | 匹配参数 | 部分匹配 |
|---|---|---|---|
| 简单类型 | 59.00% | 59.47% | 62.15% |
| 泛型类型 | 55.50% | 69.68% | 71.90% |
| 用户定义类型 | 40.40% | 40.40% | 44.30% |
| 论据 | 7.65% | 8.05% | 14.39% |
| 返回值 | 58.71% | 64.61% | 69.06% |
| 局部变量 | 61.56% | 65.66% | 67.05% |
您可以使用以下命令重现上述结果:
hityper eval -g ManyTypes4Py_gts_test_verified.json -c ManyTypes4Py_gts_test_verified_detailed.json -u ManyTypes4Py_test_usertypes.json
使用 Type4Py 的前 1 个预测作为推荐:
| 类别 | 完全符合 | 匹配参数 | 部分匹配 |
|---|---|---|---|
| 简单类型 | 67.20% | 68.00% | 69.80% |
| 泛型类型 | 56.37% | 71.05% | 72.54% |
| 用户定义类型 | 40.42% | 40.42% | 43.82% |
| 论据 | 22.36% | 23.67% | 27.56% |
| 返回值 | 59.11% | 64.98% | 69.30% |
| 局部变量 | 64.47% | 68.78% | 69.72% |
您可以使用以下命令重现上述结果:
hityper eval -g ManyTypes4Py_gts_test_verified.json -c ManyTypes4Py_gts_test_verified_detailed.json -u ManyTypes4Py_test_usertypes.json -t -n 1
使用 Type4Py 的前 10 个预测作为推荐:
| 类别 | 完全符合 | 匹配参数 | 部分匹配 |
|---|---|---|---|
| 简单类型 | 68.94% | 69.64% | 71.23% |
| 泛型类型 | 57.29% | 72.31% | 73.68% |
| 用户定义类型 | 40.43% | 40.43% | 43.67% |
| 论据 | 25.90% | 27.30% | 30.64% |
| 返回值 | 59.35% | 65.25% | 69.54% |
| 局部变量 | 65.21% | 69.53% | 70.37% |
您可以使用以下命令重现上述结果:
hityper eval -g ManyTypes4Py_gts_test_verified.json -c ManyTypes4Py_gts_test_verified_detailed.json -u ManyTypes4Py_test_usertypes.json -t -n 10
目前 Type4Py 带来的改进不是很显着,因为:
-
对于 HiTyper 给出的许多热门类型,Type4Py 无法给出推荐
-
Type4Py 在预测“稀有类型”(主要是用户定义的类型)方面保持低性能
我们目前正在构建一个更适合 HiTyper 的 DL 模型。敬请关注!
其他数据集:
如果您想在其他数据集上评估 HiTyper,请使用 , 生成相同格式的文件ManyTypes4Py_gts_test_verified.json,ManyTypes4Py_gts_test_verified_detailed.json或者您可以修改__main__.py. 要检查类型的类别,您可以使用hityper.typeobject.TypeObject.checkType().
在任何情况下,您还必须准备源文件以进行静态分析。
旧结果:
如果您想要论文中所述的确切实验结果,请在此链接下载。
去做
- 添加对过程间分析的支持
- 添加对来自第三方模块的类型的支持
- 添加对外部函数调用的支持
- 添加对存根文件的支持
引用我们
如果您在研究中使用 HiTyper,请引用我们:
@article{peng2022hityper,
author = {Yun Peng and Cuiyun Gao and Zongjie Li and Bowei Gao and David Lo and Qirun Zhang and Michael R. Lyu},
title = {Static Inference Meets Deep Learning: A Hybrid Type Inference Approach for Python},
journal = {CoRR},
volume = {abs/2105.03595},
year = {2022},
url = {https://arxiv.org/abs/2105.03595}
}
接触
我们积极维护这个项目并欢迎贡献。
如有任何问题,请联系research@yunpeng.work。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。