如何在 numba 中清除缓存(或强制重新编译)

问题描述 投票:0回答:3

我有一个用 numba 编写的相当大的代码库,我注意到当为调用另一个文件中另一个 numba 编译函数的函数启用缓存时,当被调用函数发生变化时,被调用函数的变化不会被提取。当我有两个文件时会出现这种情况:

测试文件2:

import numba

@numba.njit(cache=True)
def function1(x):
    return x * 10

测试文件:

import numba
from tests import file1

@numba.njit(cache=True)
def function2(x, y):
    return y + file1.function1(x)

如果在 jupyter notebook 中,我运行以下命令:

# INSIDE JUPYTER NOTEBOOK
import sys
sys.path.insert(1, "path/to/files/")
from tests import testfile

testfile.function2(3, 4)
>>> 34   # good value

但是,如果我更改,则将 testfile2 更改为以下内容:

import numba

@numba.njit(cache=True)
def function1(x):
    return x * 1

然后我重启 jupyter notebook 内核并重新运行笔记本,我得到以下内容

import sys
sys.path.insert(1, "path/to/files/")
from tests import testfile

testfile.function2(3, 4)
>>> 34   # bad value, should be 7

将两个文件都导入到笔记本中对不良结果没有影响。此外,仅在

cache=False
上设置
function1
也没有效果。有效的是在所有 njit'd 函数上设置
cache=False
,然后重新启动内核,然后重新运行。

我相信 LLVM 可能会内联一些被调用的函数,然后再也不会检查它们。

我查看了源代码,发现有一个返回缓存对象的方法

numba.caching.NullCache()
,实例化了一个缓存对象并运行了以下命令:

cache = numba.caching.NullCache()
cache.flush()

不幸的是,这似乎没有效果。

是否有 numba 环境设置,或者我可以手动清除 conda env 中所有缓存函数的其他方式?或者我只是做错了什么?

我在 Mac OS X 10.12.3 上使用 Anaconda Python 3.6 运行 numba 0.33。

anaconda numba
3个回答
8
投票

在看到 Josh 的回答后,我通过在项目方法中创建一个实用程序来终止缓存,用黑客解决方案“解决”了这个问题。

可能有更好的方法,但这行得通。我把这个问题悬而未决,以防有人用不那么老套的方式来做这件事。

import os


def kill_files(folder):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as e:
            print("failed on filepath: %s" % file_path)


def kill_numba_cache():

    root_folder = os.path.realpath(__file__ + "/../../")

    for root, dirnames, filenames in os.walk(root_folder):
        for dirname in dirnames:
            if dirname == "__pycache__":
                try:
                    kill_files(root + "/" + dirname)
                except Exception as e:
                    print("failed on %s", root)

6
投票

这有点hack,但这是我以前用过的东西。如果你把这个函数放在你的 numba 函数所在的顶层(对于这个例子,在

testfile
),它应该重新编译一切:

import inspect
import sys

def recompile_nb_code():
    this_module = sys.modules[__name__]
    module_members = inspect.getmembers(this_module)

    for member_name, member in module_members:
        if hasattr(member, 'recompile') and hasattr(member, 'inspect_llvm'):
            member.recompile()

然后在你想要强制重新编译时从你的 jupyter notebook 调用它。需要注意的是,它仅适用于此函数所在模块中的文件及其依赖项。可能有另一种方法来概括它。


0
投票

Numba
的官方文档建议删除缓存目录以清除缓存[链接].

Numba缓存保存在这四个目录[链接]:

  • numba.config.CACHE_DIR
  • __pycache__
  • numba.misc.appdirs.user_cache_dir()
  • IPython.paths.get_ipython_cache_dir()

可以通过搜索

*.nbi
文件[链接].

找到Numba缓存

我个人使用以下两个代码。前者引用自 Python3 project remove __pycache__ folders and .pyc files.

import pathlib
import shutil

_ = [shutil.rmtree(p) for p in pathlib.Path('.').rglob('__pycache__')]
import IPython
import shutil

path_parent = IPython.paths.get_ipython_cache_dir()
path_child = os.path.join(path_parent, 'numba_cache')

if path_parent:
    if os.path.isdir(path_child):
        shutil.rmtree(path_child)
© www.soinside.com 2019 - 2024. All rights reserved.