Skip to content

Commit b654852

Browse files
authored
[Bugfix] Allow import of TVM when current directory is read-only (#17142)
* [Bugfix] Allow import of TVM when current directory is read-only Prior to this commit, TVM could only be imported if the current directory had write privileges. This was due to the use of `tvm.contrib.pickle_memoize` to cache the winograd transformation matrices. This commit makes multiple related fixes, to ensure that (1) TVM can be imported regardless of directory permissions, (2) the working directory is not left in a cluttered state, and (3) cache files are generated in an expected location to be reused later. * The cache directory is only generated when required, just prior to saving a cache. * The cache directory defaults to `$HOME/.cache/tvm/pkl_memoize`, rather than `.pkl_memorize_py3` in the working directory. * The cache directory respects `XDG_CACHE_HOME`, using `$XDG_CACHE_HOME/tvm/pkl_memoize` if set. * lint fix
1 parent 9a9386d commit b654852

File tree

3 files changed

+214
-18
lines changed

3 files changed

+214
-18
lines changed

python/tvm/contrib/pickle_memoize.py

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
"""Memoize result of function via pickle, used for cache testcases."""
18+
1819
# pylint: disable=broad-except,superfluous-parens
20+
import atexit
1921
import os
22+
import pathlib
2023
import sys
21-
import atexit
24+
2225
from decorator import decorate
2326
from .._ffi.base import string_types
2427

@@ -28,6 +31,17 @@
2831
import pickle
2932

3033

34+
def _get_global_cache_dir() -> pathlib.Path:
35+
if "XDG_CACHE_HOME" in os.environ:
36+
cache_home = pathlib.Path(os.environ.get("XDG_CACHE_HOME"))
37+
else:
38+
cache_home = pathlib.Path.home().joinpath(".cache")
39+
return cache_home.joinpath("tvm", f"pkl_memoize_py{sys.version_info[0]}")
40+
41+
42+
GLOBAL_CACHE_DIR = _get_global_cache_dir()
43+
44+
3145
class Cache(object):
3246
"""A cache object for result cache.
3347
@@ -42,28 +56,36 @@ class Cache(object):
4256
cache_by_key = {}
4357

4458
def __init__(self, key, save_at_exit):
45-
cache_dir = f".pkl_memoize_py{sys.version_info[0]}"
46-
try:
47-
os.mkdir(cache_dir)
48-
except FileExistsError:
49-
pass
50-
else:
51-
self.cache = {}
52-
self.path = os.path.join(cache_dir, key)
53-
if os.path.exists(self.path):
54-
try:
55-
self.cache = pickle.load(open(self.path, "rb"))
56-
except Exception:
57-
self.cache = {}
58-
else:
59-
self.cache = {}
59+
self._cache = None
60+
61+
self.path = GLOBAL_CACHE_DIR.joinpath(key)
6062
self.dirty = False
6163
self.save_at_exit = save_at_exit
6264

65+
@property
66+
def cache(self):
67+
"""Return the cache, initializing on first use."""
68+
69+
if self._cache is not None:
70+
return self._cache
71+
72+
if self.path.exists():
73+
with self.path.open("rb") as cache_file:
74+
try:
75+
cache = pickle.load(cache_file)
76+
except pickle.UnpicklingError:
77+
cache = {}
78+
else:
79+
cache = {}
80+
81+
self._cache = cache
82+
return self._cache
83+
6384
def save(self):
6485
if self.dirty:
65-
print(f"Save memoize result to {self.path}")
66-
with open(self.path, "wb") as out_file:
86+
self.path.parent.mkdir(parents=True, exist_ok=True)
87+
88+
with self.path.open("wb") as out_file:
6789
pickle.dump(self.cache, out_file, pickle.HIGHEST_PROTOCOL)
6890

6991

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
import sys
21+
22+
import tvm
23+
24+
25+
@tvm.contrib.pickle_memoize.memoize("test_memoize_save_data", save_at_exit=True)
26+
def get_data_saved():
27+
return 42
28+
29+
30+
@tvm.contrib.pickle_memoize.memoize("test_memoize_transient_data", save_at_exit=False)
31+
def get_data_transient():
32+
return 42
33+
34+
35+
def main():
36+
assert len(sys.argv) == 3, "Expect arguments SCRIPT NUM_SAVED NUM_TRANSIENT"
37+
38+
num_iter_saved = int(sys.argv[1])
39+
num_iter_transient = int(sys.argv[2])
40+
41+
for _ in range(num_iter_saved):
42+
get_data_saved()
43+
for _ in range(num_iter_transient):
44+
get_data_transient()
45+
46+
47+
if __name__ == "__main__":
48+
main()
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
"""Tests for tvm.contrib.pickle_memoize"""
19+
20+
import os
21+
import pathlib
22+
import tempfile
23+
import subprocess
24+
import sys
25+
26+
import tvm.testing
27+
28+
TEST_SCRIPT_FILE = pathlib.Path(__file__).with_name("pickle_memoize_script.py").resolve()
29+
30+
31+
def test_cache_dir_not_in_current_working_dir():
32+
with tempfile.TemporaryDirectory(prefix="tvm_") as temp_dir:
33+
temp_dir = pathlib.Path(temp_dir)
34+
subprocess.check_call([TEST_SCRIPT_FILE, "1", "1"], cwd=temp_dir)
35+
36+
new_files = list(temp_dir.iterdir())
37+
assert (
38+
not new_files
39+
), "Use of tvm.contrib.pickle_memorize may not write to current directory."
40+
41+
42+
def test_current_directory_is_not_required_to_be_writable():
43+
"""TVM may be imported without directory permissions
44+
45+
This is a regression test. In previous implementations, the
46+
`tvm.contrib.pickle_memoize.memoize` function would write to the
47+
current directory when importing TVM. Import of a Python module
48+
should not write to any directory.
49+
50+
"""
51+
52+
with tempfile.TemporaryDirectory(prefix="tvm_") as temp_dir:
53+
temp_dir = pathlib.Path(temp_dir)
54+
55+
# User may read/cd into the temp dir, nobody may write to temp
56+
# dir.
57+
temp_dir.chmod(0o500)
58+
subprocess.check_call([sys.executable, "-c", "import tvm"], cwd=temp_dir)
59+
60+
61+
def test_cache_dir_defaults_to_home_config_cache():
62+
with tempfile.TemporaryDirectory(prefix="tvm_") as temp_dir:
63+
temp_dir = pathlib.Path(temp_dir)
64+
65+
subprocess.check_call([TEST_SCRIPT_FILE, "1", "0"], cwd=temp_dir)
66+
67+
new_files = list(temp_dir.iterdir())
68+
assert (
69+
not new_files
70+
), "Use of tvm.contrib.pickle_memorize may not write to current directory."
71+
72+
cache_dir = pathlib.Path.home().joinpath(".cache", "tvm", "pkl_memoize_py3")
73+
assert cache_dir.exists()
74+
cache_files = list(cache_dir.iterdir())
75+
assert len(cache_files) >= 1
76+
77+
78+
def test_cache_dir_respects_xdg_cache_home():
79+
with tempfile.TemporaryDirectory(
80+
prefix="tvm_"
81+
) as temp_working_dir, tempfile.TemporaryDirectory(prefix="tvm_") as temp_cache_dir:
82+
temp_cache_dir = pathlib.Path(temp_cache_dir)
83+
temp_working_dir = pathlib.Path(temp_working_dir)
84+
85+
subprocess.check_call(
86+
[TEST_SCRIPT_FILE, "1", "0"],
87+
cwd=temp_working_dir,
88+
env={
89+
**os.environ,
90+
"XDG_CACHE_HOME": temp_cache_dir.as_posix(),
91+
},
92+
)
93+
94+
new_files = list(temp_working_dir.iterdir())
95+
assert (
96+
not new_files
97+
), "Use of tvm.contrib.pickle_memorize may not write to current directory."
98+
99+
cache_dir = temp_cache_dir.joinpath("tvm", "pkl_memoize_py3")
100+
assert cache_dir.exists()
101+
cache_files = list(cache_dir.iterdir())
102+
assert len(cache_files) == 1
103+
104+
105+
def test_cache_dir_only_created_when_used():
106+
with tempfile.TemporaryDirectory(
107+
prefix="tvm_"
108+
) as temp_working_dir, tempfile.TemporaryDirectory(prefix="tvm_") as temp_cache_dir:
109+
temp_cache_dir = pathlib.Path(temp_cache_dir)
110+
temp_working_dir = pathlib.Path(temp_working_dir)
111+
112+
subprocess.check_call(
113+
[TEST_SCRIPT_FILE, "0", "1"],
114+
cwd=temp_working_dir,
115+
env={
116+
**os.environ,
117+
"XDG_CACHE_HOME": temp_cache_dir.as_posix(),
118+
},
119+
)
120+
121+
cache_dir = temp_cache_dir.joinpath("tvm", "pkl_memoize_py3")
122+
assert not cache_dir.exists()
123+
124+
125+
if __name__ == "__main__":
126+
tvm.testing.main()

0 commit comments

Comments
 (0)