Views
No views yet
pip install -r requirements.txt1import torch
2import torch.nn.functional as F
3import tiktoken
4from huggingface_hub import HfApi, login
5from ChronoGPT_inference import *
6
7# ----------------------------- Setup -----------------------------
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9cache_dir = 'cache' # Update this path as needed
10
11tokenizer = tiktoken.get_encoding("gpt2")
12max_length = 50
13num_return_sequences = 5
14seed = 123
15
16# -------------------------- Load Model --------------------------
17model = ChronoGPT.from_pretrained(
18 "manelalab/chrono-gpt-v1-20241231",
19 trust_remote_code=True,
20 cache_dir=cache_dir
21).to(device)
22
23# ------------------------ Prepare Input -------------------------
24prompt = "Hello, I am a language model,"
25tokens = tokenizer.encode(prompt)
26tokens = torch.tensor(tokens, dtype=torch.long).unsqueeze(0)
27tokens = tokens.repeat(num_return_sequences, 1).to(device)
28
29# -------------------- Sampling Initialization -------------------
30xgen = tokens.clone()
31sample_rng = torch.Generator(device=device)
32sample_rng.manual_seed(seed)
33
34# ------------------------- Text Generation -----------------------
35while xgen.size(1) < max_length:
36 with torch.no_grad():
37 with torch.autocast(device_type='cuda', dtype=torch.bfloat16):
38 logits, _ = model(xgen)
39
40 logits = logits[:, -1, :] # Last token logits
41 probs = F.softmax(logits, dim=-1)
42 topk_probs, topk_indices = torch.topk(probs, 50, dim=-1)
43
44 sampled_idx = torch.multinomial(topk_probs, 1, generator=sample_rng)
45 next_token = torch.gather(topk_indices, -1, sampled_idx)
46
47 xgen = torch.cat([xgen, next_token], dim=1)
48
49# ------------------------- Decode Output -------------------------
50for i in range(num_return_sequences):
51 decoded_tokens = xgen[i, :max_length].tolist()
52 decoded_text = tokenizer.decode(decoded_tokens)
53 print(f"Rank sample {i}:\n{decoded_text}\n")1import torch
2import torch.nn.functional as F
3import tiktoken
4from huggingface_hub import HfApi, login
5from ChronoGPT_inference import *
6
7# ----------------------------- Setup -----------------------------
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9cache_dir = 'cache' # Update this path as needed
10
11tokenizer = tiktoken.get_encoding("gpt2")
12
13# -------------------------- Load Model --------------------------
14model = ChronoGPT.from_pretrained(
15 "manelalab/chrono-gpt-v1-20241231",
16 trust_remote_code=True,
17 cache_dir=cache_dir
18).to(device)
19
20# ----------------------- Embedding Generation ---------------------
21text = "Obviously, the time continuum has been disrupted, creating a new temporal event sequence resulting in this alternate reality."
22
23inputs = torch.tensor(tokenizer.encode(text))[:max_length].reshape(1,-1).to(device)
24logits, emb = model(inputs)
25print('Dimension of embeddings:', emb[0].shape)@article{He2025ChronoBERT,
title={Chronologically Consistent Large Language Models},
author={He, Songrun and Lv, Linying and Manela, Asaf and Wu, Jimmy},
journal={Working Paper},
year={2025}
}