Skip to content

Commit 0697196

Browse files
committed
itertools.pairwise substitute for Python<3.10
1 parent 1552b06 commit 0697196

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

exllamav2/compat.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
from __future__ import annotations
22
import torch
3+
import itertools
4+
5+
# Emulate pairwise on Python <3.10
6+
7+
try:
8+
pairwise = itertools.pairwise
9+
except AttributeError:
10+
def pairwise(iterable):
11+
a, b = itertools.tee(iterable)
12+
next(b, None)
13+
return zip(a, b)
314

415
# On some setups Torch will attempt to use GPU peer-to-peer copies even when they are not supported. This is either
516
# a driver issue, a bug in Torch, or both. Either way, the result is that .to() will create an empty tensor on the

exllamav2/generator/dynamic.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from exllamav2.ext import exllamav2_ext as ext_c, none_tensor
99
from concurrent.futures import ThreadPoolExecutor
1010

11+
from exllamav2.compat import pairwise
1112
import torch
1213
import random
1314
import numpy as np
@@ -1331,7 +1332,7 @@ def __eq__(self, other):
13311332
rotation = [r * self.page_size for r in rotation]
13321333
for cache, buffer in zip(cache_tensors, defrag_buffers):
13331334
buffer[:, :, :, :].copy_(cache[:, rotation[0] : rotation[0] + self.page_size, :, :])
1334-
for a, b in itertools.pairwise(rotation):
1335+
for a, b in pairwise(rotation):
13351336
cache[:, a : a + self.page_size, :, :].copy_(cache[:, b : b + self.page_size, :, :])
13361337
cache[:, rotation[-1] : rotation[-1] + self.page_size, :, :].copy_(buffer[:, :, :, :])
13371338

@@ -2392,7 +2393,7 @@ def allocate_pages(self):
23922393
# Metrics
23932394

23942395
self.total_pages += len(seq.allocated_pages)
2395-
for page_a, page_b in itertools.pairwise(seq.allocated_pages):
2396+
for page_a, page_b in pairwise(seq.allocated_pages):
23962397
if page_b.page_index != page_a.page_index + 1:
23972398
self.non_sequential_pages += 1
23982399

0 commit comments

Comments
 (0)