File size: 976 Bytes
6ec9472 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import io, os
import torch
from huggingface_hub import hf_hub_download
_CACHE = {}
def _wikitext(split):
if split in _CACHE:
return _CACHE[split]
import pandas as pd
fn = hf_hub_download("Salesforce/wikitext", repo_type="dataset",
filename=f"wikitext-2-raw-v1/{split}-00000-of-00001.parquet")
txt = "\n\n".join(pd.read_parquet(fn)["text"].tolist())
_CACHE[split] = txt
return txt
def calib_batches(tok, nsamples, seqlen, seed=0):
ids = tok(_wikitext("train"), return_tensors="pt").input_ids
g = torch.Generator().manual_seed(seed)
out = []
for _ in range(nsamples):
i = torch.randint(0, ids.shape[1] - seqlen - 1, (1,), generator=g).item()
out.append(ids[:, i:i + seqlen])
return out
def test_tokens(tok, seqlen):
ids = tok(_wikitext("test"), return_tensors="pt").input_ids
n = ids.shape[1] // seqlen
return [ids[:, i * seqlen:(i + 1) * seqlen] for i in range(n)]
|