From cb8b14857bf3142cd645d6b3440aed7131d57034 Mon Sep 17 00:00:00 2001 From: ArchitAnant Date: Thu, 25 Dec 2025 13:24:12 +0530 Subject: [PATCH 1/7] rem: cuda dependency : : device agnostic --- diffusion_pen/diff_unet.py | 2 +- model_inference.py | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/diffusion_pen/diff_unet.py b/diffusion_pen/diff_unet.py index ae6a322..22ed896 100644 --- a/diffusion_pen/diff_unet.py +++ b/diffusion_pen/diff_unet.py @@ -203,7 +203,7 @@ def forward(self, x, context=None, mask=None): k = self.to_k(context) v = self.to_v(context) - mask = None #torch.ones(1, 8192).bool().cuda('cuda:6') + mask = None q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v)) diff --git a/model_inference.py b/model_inference.py index c6e68ad..eb860f0 100644 --- a/model_inference.py +++ b/model_inference.py @@ -119,13 +119,9 @@ def main_sample( text_list : List[str], style_refs : List[str], uname : str, + device : str ): - device = 'cuda' - if not torch.cuda.is_available(): - print("CUDA is needed.") - return - # tokenizer and text encoder tokenizer = model_pipeline.text_tokenizer#CanineTokenizer.from_pretrained("google/canine-c") text_encoder = model_pipeline.text_encoder#CanineModel.from_pretrained("google/canine-c").to(device) From 2a2bc5eb104679f35a8acc8ed36ca294f9608990 Mon Sep 17 00:00:00 2001 From: ArchitAnant Date: Thu, 25 Dec 2025 13:38:21 +0530 Subject: [PATCH 2/7] fix: missing argument --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index e116905..442042d 100644 --- a/main.py +++ b/main.py @@ -83,7 +83,8 @@ async def generate_handwriting(payload: HandwritingRequest): model_pipeline=pipeline, text_list=texts, style_refs=style_image_paths, - uname=uname + uname=uname, + device=torch.device("cuda" if torch.cuda.is_available() else "cpu") ) return { From 7e4c42765e29743012ac7fc7ff632fb08f73caf4 Mon Sep 17 00:00:00 2001 From: ArchitAnant Date: Thu, 25 Dec 2025 14:02:29 +0530 Subject: [PATCH 3/7] add: quantization and steps redunction for cpu' --- model_inference.py | 2 +- model_pipeline.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/model_inference.py b/model_inference.py index eb860f0..a2df4cd 100644 --- a/model_inference.py +++ b/model_inference.py @@ -171,7 +171,7 @@ def main_sample( device=device, img_h=64, img_w=256, - steps=100, + steps=100 if device=='cuda' else 30, ) image = image_tensor[0].cpu() if image.size(0) == 4: # latent decoded returns 3 channels diff --git a/model_pipeline.py b/model_pipeline.py index a559c01..7fa4b2f 100644 --- a/model_pipeline.py +++ b/model_pipeline.py @@ -35,6 +35,7 @@ def __init__(self, vocab_size=95, # unused internally; placeholder text_encoder=self.text_encoder, args=SimpleArgs(interpolation=False, mix_rate=None), + use_fp16 = True if device=='cpu' else False ) self.unet = UNetModel(**unet_cfg).to(device) From c052c6404126ddec2958870f3c1c5310d6a0eb4f Mon Sep 17 00:00:00 2001 From: ArchitAnant Date: Thu, 25 Dec 2025 14:15:33 +0530 Subject: [PATCH 4/7] fix regulariation and step count --- model_inference.py | 2 +- model_pipeline.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/model_inference.py b/model_inference.py index a2df4cd..4b8d63f 100644 --- a/model_inference.py +++ b/model_inference.py @@ -171,7 +171,7 @@ def main_sample( device=device, img_h=64, img_w=256, - steps=100 if device=='cuda' else 30, + steps= 30, ) image = image_tensor[0].cpu() if image.size(0) == 4: # latent decoded returns 3 channels diff --git a/model_pipeline.py b/model_pipeline.py index 7fa4b2f..f678e23 100644 --- a/model_pipeline.py +++ b/model_pipeline.py @@ -35,7 +35,7 @@ def __init__(self, vocab_size=95, # unused internally; placeholder text_encoder=self.text_encoder, args=SimpleArgs(interpolation=False, mix_rate=None), - use_fp16 = True if device=='cpu' else False + use_fp16 = True ) self.unet = UNetModel(**unet_cfg).to(device) From de060a5acd33188eb1fc4b86e441f7a4981d4323 Mon Sep 17 00:00:00 2001 From: ArchitAnant Date: Thu, 25 Dec 2025 14:25:50 +0530 Subject: [PATCH 5/7] fix: dtype --- model_pipeline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model_pipeline.py b/model_pipeline.py index f678e23..db2d4a9 100644 --- a/model_pipeline.py +++ b/model_pipeline.py @@ -35,7 +35,7 @@ def __init__(self, vocab_size=95, # unused internally; placeholder text_encoder=self.text_encoder, args=SimpleArgs(interpolation=False, mix_rate=None), - use_fp16 = True + ) self.unet = UNetModel(**unet_cfg).to(device) From b82a5496477bfaa62f7625d0bdaa55ea63432549 Mon Sep 17 00:00:00 2001 From: ArchitAnant Date: Thu, 25 Dec 2025 14:35:09 +0530 Subject: [PATCH 6/7] add: folder cleanup --- main.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main.py b/main.py index 442042d..314979a 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import os +import shutil import torch from typing import List from model_inference import main_sample @@ -86,6 +87,12 @@ async def generate_handwriting(payload: HandwritingRequest): uname=uname, device=torch.device("cuda" if torch.cuda.is_available() else "cpu") ) + # check and remove the tmp/uname folder + folder_path = f"/tmp/{uname}" + + if os.path.isdir(folder_path): + shutil.rmtree(folder_path) + return { "message": "Handwriting generation completed.", From 67c3ef3b63dc412822a8da142cf5e41eb3858aea Mon Sep 17 00:00:00 2001 From: ArchitAnant Date: Thu, 25 Dec 2025 14:50:40 +0530 Subject: [PATCH 7/7] mod: cleanup --- main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 314979a..99e4a79 100644 --- a/main.py +++ b/main.py @@ -88,11 +88,13 @@ async def generate_handwriting(payload: HandwritingRequest): device=torch.device("cuda" if torch.cuda.is_available() else "cpu") ) # check and remove the tmp/uname folder - folder_path = f"/tmp/{uname}" + base_tmp = "/tmp" + user_tmp = os.path.join(base_tmp, uname) - if os.path.isdir(folder_path): - shutil.rmtree(folder_path) + if os.path.isdir(user_tmp): + shutil.rmtree(user_tmp) + os.makedirs(user_tmp, exist_ok=True) return { "message": "Handwriting generation completed.",