Skip to content

Commit

Permalink
[inductor] Copy RedisRemoteCacheBackend into pytorch (pytorch#127480)
Browse files Browse the repository at this point in the history
Summary: We need an implementation of RedisRemoteCacheBackend with the same API that we're using for FbMemcacheRemoteFxGraphCacheBackend. So we'll stop using the Triton implementation and adapt a version for use by inductor. I also renamed parameters and cache entries to match our cache terminology.

Test Plan: Ran this command twice and inspected log output to ensure I got cache hits:
```
TORCH_LOGS=+torch._inductor.codecache TORCHINDUCTOR_FX_GRAPH_REMOTE_CACHE=1 python benchmarks/dynamo/torchbench.py --performance --inductor --device cuda --training --amp --print-compilation-time --only dcgan
```

Pull Request resolved: pytorch#127480
Approved by: https://github.com/oulgen
  • Loading branch information
masnesral authored and pytorchmergebot committed May 30, 2024
1 parent cdeb242 commit 3f5d863
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
7 changes: 7 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,10 @@ ignore_missing_imports = True

[mypy-torch_xla.*]
ignore_missing_imports = True

#
# Third party dependencies that are optional.
#

[mypy-redis]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion test/inductor/test_codecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def put(self, filename, data):
cache_module = (
"triton.runtime.fb_memcache.FbMemcacheRemoteFxGraphCacheBackend"
if config.is_fbcode()
else "triton.runtime.cache.RedisRemoteCacheBackend"
else "torch._inductor.remote_cache.RedisRemoteCacheBackend"
)

with config.patch(
Expand Down
2 changes: 1 addition & 1 deletion test/inductor/test_max_autotune.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def put(self, filename, data):
cache_module = (
"triton.runtime.fb_memcache.FbMemcacheRemoteAutotuneCacheBackend"
if config.is_fbcode()
else "triton.runtime.cache.RedisRemoteCacheBackend"
else "torch._inductor.remote_cache.RedisRemoteCacheBackend"
)

with config.patch(
Expand Down
14 changes: 7 additions & 7 deletions torch/_inductor/codecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,16 +976,16 @@ def load(
if remote:
cache_id = "fx-graph-v1"
try:
import triton

if config.is_fbcode():
remote_cache = triton.runtime.fb_memcache.FbMemcacheRemoteFxGraphCacheBackend(
cache_id
from triton.runtime.fb_memcache import (
FbMemcacheRemoteFxGraphCacheBackend,
)

remote_cache = FbMemcacheRemoteFxGraphCacheBackend(cache_id)
else:
remote_cache = triton.runtime.cache.RedisRemoteCacheBackend(
cache_id
)
from torch._inductor.remote_cache import RedisRemoteCacheBackend

remote_cache = RedisRemoteCacheBackend(cache_id)
except Exception:
remote_cache = None
log.warning("Unable to create a remote cache", exc_info=True)
Expand Down
46 changes: 46 additions & 0 deletions torch/_inductor/remote_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
from abc import abstractmethod


class RemoteCacheBackend:
"""
A backend implementation for accessing a remote/distributed cache.
"""

def __init__(self, cache_id: str):
pass

@abstractmethod
def get(self, key: str):
pass

@abstractmethod
def put(self, key: str, data: bytes):
pass


class RedisRemoteCacheBackend(RemoteCacheBackend):
"""
A Redis implementation of a remote/distributed cache.
"""

def __init__(self, cache_id: str):
import redis

self._cache_id = cache_id
self._key_fmt = os.environ.get(
"TORCHINDUCTOR_REDIS_KEY_FORMAT", "pt2:{cache_id}:{key}"
)
self._redis = redis.Redis(
host=os.environ.get("TRITON_REDIS_HOST", "localhost"),
port=int(os.environ.get("TRITON_REDIS_PORT", 6379)),
)

def _get_key(self, key: str) -> str:
return self._key_fmt.format(cache_id=self._cache_id, key=key)

def get(self, key: str):
return self._redis.get(self._get_key(key))

def put(self, key: str, data: bytes):
return self._redis.set(self._get_key(key), data)
4 changes: 3 additions & 1 deletion torch/_inductor/runtime/triton_heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,9 @@ def cached_autotune(
key
)
else:
remote_cache = triton.runtime.cache.RedisRemoteCacheBackend(key)
from torch._inductor.remote_cache import RedisRemoteCacheBackend

remote_cache = RedisRemoteCacheBackend(key)
except Exception:
remote_cache = None
log.warning("Unable to create a remote cache", exc_info=True)
Expand Down

0 comments on commit 3f5d863

Please sign in to comment.