-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·206 lines (168 loc) · 5.81 KB
/
Copy pathgenerate.py
File metadata and controls
executable file
·206 lines (168 loc) · 5.81 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import numpy as np
import json
import torch
import argparse
import sys
from omegaconf import ListConfig
from hnet.models.mixer_seq import HNetForCausalLM
from hnet.models.config_hnet import (
AttnConfig,
SSMConfig,
HNetConfig,
)
from hnet.utils.tokenizers import ByteTokenizer
def load_from_pretrained(model_path: str, model_config_path: str):
"""Load model from pretrained checkpoint.
Args:
model_path: Path to the model checkpoint (.pt file)
model_config_path: Path to the model configuration (.json file)
Returns:
Loaded HNetForCausalLM model
"""
# Load configuration
with open(model_config_path, "r") as f:
config = json.load(f)
# Create config objects
attn_cfg = AttnConfig(**config.pop("attn_cfg"))
ssm_cfg = SSMConfig(**config.pop("ssm_cfg"))
hnet_cfg = HNetConfig(**config, attn_cfg=attn_cfg, ssm_cfg=ssm_cfg)
# Create model
device = "cuda" if torch.cuda.is_available() else "cpu"
model = HNetForCausalLM(hnet_cfg, device=device, dtype=torch.bfloat16)
model.eval()
# Load checkpoint
major, minor = map(int, torch.__version__.split('.')[:2])
if (major, minor) >= (2, 6):
with torch.serialization.safe_globals([ListConfig]):
state_dict = torch.load(model_path, map_location=device, weights_only=False)
else:
state_dict = torch.load(model_path, map_location=device)
model.load_state_dict(state_dict)
return model
def generate(
model,
prompt: str,
max_tokens: int = 1024,
temperature: float = 1.0,
top_p: float = 0.9,
):
"""Generate text from the model, yielding tokens as they're generated.
Args:
model: HNetForCausalLM model
prompt: Input text prompt
max_tokens: Maximum number of tokens to generate
temperature: Sampling temperature (higher = more random)
top_p: Top-p sampling parameter
Yields:
Generated text token by token as strings
"""
device = next(model.parameters()).device
tokenizer = ByteTokenizer()
# Tokenize prompt
encoded = tokenizer.encode([prompt], add_bos=True)[0]
input_ids = torch.tensor(
encoded["input_ids"], dtype=torch.long, device=device
).unsqueeze(0)
inference_cache = model.allocate_inference_cache(
1, input_ids.shape[1] + max_tokens, dtype=torch.bfloat16
)
with torch.inference_mode():
mask = torch.ones(input_ids.shape, device=device, dtype=torch.bool)
output = model.forward(input_ids, mask=mask, inference_params=inference_cache)
logits = output.logits[0, -1, :] / temperature
for _ in range(max_tokens):
# Apply top-p sampling
if top_p < 1.0:
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(
torch.softmax(sorted_logits, dim=-1), dim=-1
)
# Remove tokens with cumulative probability above the threshold
sorted_indices_to_remove = cumulative_probs > top_p
sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].clone()
sorted_indices_to_remove[0] = 0
indices_to_remove = sorted_indices[sorted_indices_to_remove]
logits[indices_to_remove] = -float("inf")
probs = torch.softmax(logits, dim=-1)
next_token = torch.multinomial(probs, 1)
if next_token.item() == tokenizer.eos_idx:
break
current_token = next_token.unsqueeze(0)
yield current_token
with torch.inference_mode():
output = model.step(current_token, inference_cache)
# Get logits and apply temperature
logits = output.logits[0, -1, :] / temperature
def main():
parser = argparse.ArgumentParser(description="Generate text from an H-Net model")
parser.add_argument(
"--model-path",
type=str,
required=True,
help="Path to the model checkpoint (.pt file)",
)
parser.add_argument(
"--config-path",
type=str,
required=True,
help="Path to the model configuration (.json file)",
)
parser.add_argument(
"--max-tokens",
type=int,
default=1024,
help="Maximum number of tokens to generate (default: 1024)",
)
parser.add_argument(
"--temperature",
type=float,
default=1.0,
help="Sampling temperature (default: 1.0)",
)
parser.add_argument(
"--top-p",
type=float,
default=1.0,
help="Top-p sampling parameter (default: 1.0)",
)
args = parser.parse_args()
print("Loading model...")
try:
model = load_from_pretrained(args.model_path, args.config_path)
print("Model loaded successfully!")
except Exception as e:
print(f"Error loading model: {e}")
sys.exit(1)
tokenizer = ByteTokenizer()
while True:
prompt = input("\nPrompt: ").strip()
if not prompt:
continue
print(
f"\nGenerating (max_tokens={args.max_tokens}, temperature={args.temperature}, top_p={args.top_p})"
)
print(f"\033[92m{prompt}\033[0m", end="")
token_count = 0
buf = []
for token in generate(
model,
prompt,
max_tokens=args.max_tokens,
temperature=args.temperature,
top_p=args.top_p,
):
buf.append(token)
token_count += 1
decoded = None
res = None
for j in range(1, min(len(buf), 4)):
try:
res = tokenizer.decode(buf[:j])
decoded = j
except:
pass
if res is not None:
print(res, end="", flush=True)
buf = buf[decoded:]
if __name__ == "__main__":
main()