cpu or cuda
Browse files
app.py
CHANGED
|
@@ -230,6 +230,9 @@ def calculate_iou(box1, box2):
|
|
| 230 |
|
| 231 |
# @spaces.GPU(enable_queue=True, duration=120)
|
| 232 |
def buildmodel(**kwargs):
|
|
|
|
|
|
|
|
|
|
| 233 |
from modeling_crello import CrelloModel, CrelloModelConfig
|
| 234 |
from quantizer import get_quantizer
|
| 235 |
# seed / input model / resume
|
|
@@ -289,7 +292,6 @@ def buildmodel(**kwargs):
|
|
| 289 |
config=model_args,
|
| 290 |
# cache_dir="/openseg_blob/v-yanbin/GradioDemo/cache_dir",
|
| 291 |
).to("cuda")
|
| 292 |
-
# model = CrelloModel(config=model_args)
|
| 293 |
|
| 294 |
tokenizer.add_special_tokens({"mask_token": "<mask>"})
|
| 295 |
quantizer.additional_special_tokens.add("<mask>")
|
|
@@ -298,7 +300,7 @@ def buildmodel(**kwargs):
|
|
| 298 |
for token in added_special_tokens_list:
|
| 299 |
quantizer.additional_special_tokens.add(token)
|
| 300 |
|
| 301 |
-
return model, quantizer, tokenizer
|
| 302 |
|
| 303 |
def construction_layout():
|
| 304 |
params_dict = {
|
|
@@ -318,7 +320,8 @@ def construction_layout():
|
|
| 318 |
}
|
| 319 |
device = "cuda"
|
| 320 |
# Init model
|
| 321 |
-
|
|
|
|
| 322 |
|
| 323 |
# print('resize token embeddings to match the tokenizer', 129423)
|
| 324 |
# model.lm.resize_token_embeddings(129423)
|
|
@@ -332,20 +335,24 @@ def construction_layout():
|
|
| 332 |
model.eval()
|
| 333 |
# quantizer = quantizer.to("cuda")
|
| 334 |
# tokenizer = tokenizer.to("cuda")
|
| 335 |
-
model.lm = model.lm.to("cuda")
|
| 336 |
print(model.lm.device)
|
| 337 |
-
|
|
|
|
|
|
|
| 338 |
|
| 339 |
@torch.no_grad()
|
| 340 |
@spaces.GPU(duration=120)
|
| 341 |
def evaluate_v1(inputs, model, quantizer, tokenizer, width, height, device, do_sample=False, temperature=1.0, top_p=1.0, top_k=50):
|
|
|
|
| 342 |
json_example = inputs
|
| 343 |
input_intension = '{"wholecaption":"' + json_example["wholecaption"] + '","layout":[{"layer":'
|
| 344 |
print("tokenizer1")
|
| 345 |
inputs = tokenizer(
|
| 346 |
input_intension, return_tensors="pt"
|
| 347 |
).to(model.lm.device)
|
| 348 |
-
|
|
|
|
| 349 |
print("tokenizer2")
|
| 350 |
|
| 351 |
stopping_criteria = StoppingCriteriaList()
|
|
@@ -379,12 +386,6 @@ def evaluate_v1(inputs, model, quantizer, tokenizer, width, height, device, do_s
|
|
| 379 |
return pred_json_example
|
| 380 |
|
| 381 |
def inference(generate_method, intention, model, quantizer, tokenizer, width, height, device, do_sample=True, temperature=1.0, top_p=1.0, top_k=50):
|
| 382 |
-
# def FormulateInput(intension: str):
|
| 383 |
-
# resdict = {}
|
| 384 |
-
# resdict["wholecaption"] = intension
|
| 385 |
-
# resdict["layout"] = []
|
| 386 |
-
# return resdict
|
| 387 |
-
# rawdata = FormulateInput(intention)
|
| 388 |
rawdata = {}
|
| 389 |
rawdata["wholecaption"] = intention
|
| 390 |
rawdata["layout"] = []
|
|
@@ -525,7 +526,8 @@ def process_svg(text_input, tuple_input, seed, true_gs, inference_steps):
|
|
| 525 |
return result_images, svg_file_path, svg_editor
|
| 526 |
|
| 527 |
def main():
|
| 528 |
-
model, quantizer, tokenizer, width, height, device = construction_layout()
|
|
|
|
| 529 |
|
| 530 |
inference_partial = partial(
|
| 531 |
inference,
|
|
@@ -540,12 +542,17 @@ def main():
|
|
| 540 |
def process_preddate(intention, temperature, top_p, generate_method='v1'):
|
| 541 |
intention = intention.replace('\n', '').replace('\r', '').replace('\\', '')
|
| 542 |
intention = ensure_space_after_period(intention)
|
|
|
|
|
|
|
|
|
|
| 543 |
if temperature == 0.0:
|
| 544 |
# print("looking for greedy decoding strategies, set `do_sample=False`.")
|
| 545 |
-
preddata = inference_partial(generate_method, intention, do_sample=False)
|
|
|
|
| 546 |
else:
|
| 547 |
-
preddata = inference_partial(generate_method, intention, temperature=temperature, top_p=top_p)
|
| 548 |
-
|
|
|
|
| 549 |
layouts = preddata["layout"]
|
| 550 |
list_box = []
|
| 551 |
for i, layout in enumerate(layouts):
|
|
|
|
| 230 |
|
| 231 |
# @spaces.GPU(enable_queue=True, duration=120)
|
| 232 |
def buildmodel(**kwargs):
|
| 233 |
+
global model
|
| 234 |
+
global quantizer
|
| 235 |
+
global tokenizer
|
| 236 |
from modeling_crello import CrelloModel, CrelloModelConfig
|
| 237 |
from quantizer import get_quantizer
|
| 238 |
# seed / input model / resume
|
|
|
|
| 292 |
config=model_args,
|
| 293 |
# cache_dir="/openseg_blob/v-yanbin/GradioDemo/cache_dir",
|
| 294 |
).to("cuda")
|
|
|
|
| 295 |
|
| 296 |
tokenizer.add_special_tokens({"mask_token": "<mask>"})
|
| 297 |
quantizer.additional_special_tokens.add("<mask>")
|
|
|
|
| 300 |
for token in added_special_tokens_list:
|
| 301 |
quantizer.additional_special_tokens.add(token)
|
| 302 |
|
| 303 |
+
# return model, quantizer, tokenizer
|
| 304 |
|
| 305 |
def construction_layout():
|
| 306 |
params_dict = {
|
|
|
|
| 320 |
}
|
| 321 |
device = "cuda"
|
| 322 |
# Init model
|
| 323 |
+
buildmodel(**params_dict)
|
| 324 |
+
# model, quantizer, tokenizer = buildmodel(**params_dict)
|
| 325 |
|
| 326 |
# print('resize token embeddings to match the tokenizer', 129423)
|
| 327 |
# model.lm.resize_token_embeddings(129423)
|
|
|
|
| 335 |
model.eval()
|
| 336 |
# quantizer = quantizer.to("cuda")
|
| 337 |
# tokenizer = tokenizer.to("cuda")
|
| 338 |
+
# model.lm = model.lm.to("cuda")
|
| 339 |
print(model.lm.device)
|
| 340 |
+
|
| 341 |
+
return params_dict["width"], params_dict["height"], device
|
| 342 |
+
# return model, quantizer, tokenizer, params_dict["width"], params_dict["height"], device
|
| 343 |
|
| 344 |
@torch.no_grad()
|
| 345 |
@spaces.GPU(duration=120)
|
| 346 |
def evaluate_v1(inputs, model, quantizer, tokenizer, width, height, device, do_sample=False, temperature=1.0, top_p=1.0, top_k=50):
|
| 347 |
+
print(model.lm.device)
|
| 348 |
json_example = inputs
|
| 349 |
input_intension = '{"wholecaption":"' + json_example["wholecaption"] + '","layout":[{"layer":'
|
| 350 |
print("tokenizer1")
|
| 351 |
inputs = tokenizer(
|
| 352 |
input_intension, return_tensors="pt"
|
| 353 |
).to(model.lm.device)
|
| 354 |
+
print("Input IDs device:", inputs["input_ids"].device)
|
| 355 |
+
print("Attention Mask device:", inputs["attention_mask"].device)
|
| 356 |
print("tokenizer2")
|
| 357 |
|
| 358 |
stopping_criteria = StoppingCriteriaList()
|
|
|
|
| 386 |
return pred_json_example
|
| 387 |
|
| 388 |
def inference(generate_method, intention, model, quantizer, tokenizer, width, height, device, do_sample=True, temperature=1.0, top_p=1.0, top_k=50):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
rawdata = {}
|
| 390 |
rawdata["wholecaption"] = intention
|
| 391 |
rawdata["layout"] = []
|
|
|
|
| 526 |
return result_images, svg_file_path, svg_editor
|
| 527 |
|
| 528 |
def main():
|
| 529 |
+
# model, quantizer, tokenizer, width, height, device = construction_layout()
|
| 530 |
+
width, height, device = construction_layout()
|
| 531 |
|
| 532 |
inference_partial = partial(
|
| 533 |
inference,
|
|
|
|
| 542 |
def process_preddate(intention, temperature, top_p, generate_method='v1'):
|
| 543 |
intention = intention.replace('\n', '').replace('\r', '').replace('\\', '')
|
| 544 |
intention = ensure_space_after_period(intention)
|
| 545 |
+
print(f"process_preddate: {model.lm.device}")
|
| 546 |
+
model.lm.to("cuda")
|
| 547 |
+
print(f"after process_preddate: {model.lm.device}")
|
| 548 |
if temperature == 0.0:
|
| 549 |
# print("looking for greedy decoding strategies, set `do_sample=False`.")
|
| 550 |
+
# preddata = inference_partial(generate_method, intention, do_sample=False)
|
| 551 |
+
preddata = inference(generate_method, intention, model=model, quantizer=quantizer, tokenizer=tokenizer, width=width, height=height, device=device, do_sample=False)
|
| 552 |
else:
|
| 553 |
+
# preddata = inference_partial(generate_method, intention, temperature=temperature, top_p=top_p)
|
| 554 |
+
preddata = inference(generate_method, intention, model=model, quantizer=quantizer, tokenizer=tokenizer, width=width, height=height, device=device, temperature=temperature, top_p=top_p)
|
| 555 |
+
|
| 556 |
layouts = preddata["layout"]
|
| 557 |
list_box = []
|
| 558 |
for i, layout in enumerate(layouts):
|