Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import types
|
|
| 3 |
import os
|
| 4 |
import math
|
| 5 |
import json
|
|
|
|
| 6 |
import torch
|
| 7 |
import torch.nn.functional as F
|
| 8 |
from flask import Flask, request, jsonify, Response
|
|
@@ -97,9 +98,9 @@ def clone_past_key_values(pkv):
|
|
| 97 |
except Exception:
|
| 98 |
pass
|
| 99 |
# Fallback
|
| 100 |
-
import copy
|
| 101 |
return copy.deepcopy(pkv)
|
| 102 |
|
|
|
|
| 103 |
def diffusion_step_block(logits, x_block, mask_block, num_transfer, temperature, remasking):
|
| 104 |
"""Vectorized diffusion step — no per-sample Python loops."""
|
| 105 |
B, L, _ = logits.shape
|
|
@@ -183,10 +184,6 @@ def generate(model, tokenizer, prompt, steps=128, max_new_tokens=128, block_size
|
|
| 183 |
full_attn, full_pos = build_staircase_attention_mask(x, block_size, pad_id)
|
| 184 |
attn_blk = full_attn[:, :, T_prefix:T_total, :]
|
| 185 |
pos_blk = full_pos[:, T_prefix:T_total]
|
| 186 |
-
# Clone prefix caches once per block instead of every step
|
| 187 |
-
cond_past_clone = clone_past_key_values(cond_past)
|
| 188 |
-
uncond_past_clone = clone_past_key_values(uncond_past) if uncond_past is not None else None
|
| 189 |
-
|
| 190 |
for t in range(eff_steps):
|
| 191 |
x_blk = x[:, T_prefix:T_total]
|
| 192 |
m_blk = x_blk == mask_id
|
|
@@ -195,7 +192,6 @@ def generate(model, tokenizer, prompt, steps=128, max_new_tokens=128, block_size
|
|
| 195 |
past_key_values=clone_past_key_values(cond_past), use_cache=False
|
| 196 |
).logits
|
| 197 |
logits = cond_logits
|
| 198 |
-
|
| 199 |
if cfg_scale > 0:
|
| 200 |
un_logits = model(
|
| 201 |
x_blk, attention_mask=attn_blk, position_ids=pos_blk,
|
|
@@ -275,9 +271,6 @@ def generate_stream(model, tokenizer, prompt, steps=128, max_new_tokens=128, blo
|
|
| 275 |
full_attn, full_pos = build_staircase_attention_mask(x, block_size, pad_id)
|
| 276 |
attn_blk = full_attn[:, :, T_prefix:T_total, :]
|
| 277 |
pos_blk = full_pos[:, T_prefix:T_total]
|
| 278 |
-
# Clone prefix caches once per block
|
| 279 |
-
cond_past_clone = clone_past_key_values(cond_past)
|
| 280 |
-
uncond_past_clone = clone_past_key_values(uncond_past) if uncond_past is not None else None
|
| 281 |
for t in range(eff_steps):
|
| 282 |
x_blk = x[:, T_prefix:T_total]
|
| 283 |
m_blk = x_blk == mask_id
|
|
@@ -351,9 +344,6 @@ def load_model():
|
|
| 351 |
print(f"torch.compile skipped: {e}")
|
| 352 |
else:
|
| 353 |
print("Diffusion model loaded without torch.compile (custom FX code incompatible with Dynamo).")
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 358 |
MODEL_NAME,
|
| 359 |
trust_remote_code=IS_DIFFUSION
|
|
|
|
| 3 |
import os
|
| 4 |
import math
|
| 5 |
import json
|
| 6 |
+
import copy
|
| 7 |
import torch
|
| 8 |
import torch.nn.functional as F
|
| 9 |
from flask import Flask, request, jsonify, Response
|
|
|
|
| 98 |
except Exception:
|
| 99 |
pass
|
| 100 |
# Fallback
|
|
|
|
| 101 |
return copy.deepcopy(pkv)
|
| 102 |
|
| 103 |
+
|
| 104 |
def diffusion_step_block(logits, x_block, mask_block, num_transfer, temperature, remasking):
|
| 105 |
"""Vectorized diffusion step — no per-sample Python loops."""
|
| 106 |
B, L, _ = logits.shape
|
|
|
|
| 184 |
full_attn, full_pos = build_staircase_attention_mask(x, block_size, pad_id)
|
| 185 |
attn_blk = full_attn[:, :, T_prefix:T_total, :]
|
| 186 |
pos_blk = full_pos[:, T_prefix:T_total]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
for t in range(eff_steps):
|
| 188 |
x_blk = x[:, T_prefix:T_total]
|
| 189 |
m_blk = x_blk == mask_id
|
|
|
|
| 192 |
past_key_values=clone_past_key_values(cond_past), use_cache=False
|
| 193 |
).logits
|
| 194 |
logits = cond_logits
|
|
|
|
| 195 |
if cfg_scale > 0:
|
| 196 |
un_logits = model(
|
| 197 |
x_blk, attention_mask=attn_blk, position_ids=pos_blk,
|
|
|
|
| 271 |
full_attn, full_pos = build_staircase_attention_mask(x, block_size, pad_id)
|
| 272 |
attn_blk = full_attn[:, :, T_prefix:T_total, :]
|
| 273 |
pos_blk = full_pos[:, T_prefix:T_total]
|
|
|
|
|
|
|
|
|
|
| 274 |
for t in range(eff_steps):
|
| 275 |
x_blk = x[:, T_prefix:T_total]
|
| 276 |
m_blk = x_blk == mask_id
|
|
|
|
| 344 |
print(f"torch.compile skipped: {e}")
|
| 345 |
else:
|
| 346 |
print("Diffusion model loaded without torch.compile (custom FX code incompatible with Dynamo).")
|
|
|
|
|
|
|
|
|
|
| 347 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 348 |
MODEL_NAME,
|
| 349 |
trust_remote_code=IS_DIFFUSION
|