Olivia commited on
Commit
4aca758
·
1 Parent(s): 0122045

info endpoint

Browse files
Files changed (2) hide show
  1. app.py +16 -3
  2. kernels/__init__.py +37 -4
app.py CHANGED
@@ -73,9 +73,10 @@ if SPACES_AVAILABLE:
73
 
74
  # Check CUDA kernels availability
75
  try:
76
- from kernels import check_cuda_kernels, get_fused_instance_norm
 
77
  CUDA_KERNELS_AVAILABLE = check_cuda_kernels()
78
- print(f"CUDA Kernels: {'Available' if CUDA_KERNELS_AVAILABLE else 'Not Available'}")
79
  except Exception:
80
  CUDA_KERNELS_AVAILABLE = False
81
  print("CUDA Kernels: Not Available (using PyTorch fallback)")
@@ -528,7 +529,7 @@ print("=" * 50)
528
  print("StyleForge - Initializing...")
529
  print("=" * 50)
530
  print(f"Device: {DEVICE.type.upper()}")
531
- print(f"CUDA Kernels: {'Available' if CUDA_KERNELS_AVAILABLE else 'Not Available'}")
532
  print("Preloading models...")
533
  for style in STYLES.keys():
534
  try:
@@ -1307,10 +1308,22 @@ def stylize_image_impl(
1307
  add_watermark: bool
1308
  ) -> Tuple[Optional[Image.Image], str, Optional[str]]:
1309
  """Main stylization function for Gradio."""
 
 
1310
  if input_image is None:
1311
  return None, "Please upload an image first.", None
1312
 
1313
  try:
 
 
 
 
 
 
 
 
 
 
1314
  # Convert to RGB if needed
1315
  if input_image.mode != 'RGB':
1316
  input_image = input_image.convert('RGB')
 
73
 
74
  # Check CUDA kernels availability
75
  try:
76
+ from kernels import check_cuda_kernels, get_fused_instance_norm, compile_kernels
77
+ # On ZeroGPU, kernels will be compiled on-demand within GPU tasks
78
  CUDA_KERNELS_AVAILABLE = check_cuda_kernels()
79
+ print(f"CUDA Kernels: {'Available (lazy-loaded)' if not CUDA_KERNELS_AVAILABLE and SPACES_AVAILABLE else 'Available' if CUDA_KERNELS_AVAILABLE else 'Not Available'}")
80
  except Exception:
81
  CUDA_KERNELS_AVAILABLE = False
82
  print("CUDA Kernels: Not Available (using PyTorch fallback)")
 
529
  print("StyleForge - Initializing...")
530
  print("=" * 50)
531
  print(f"Device: {DEVICE.type.upper()}")
532
+ print(f"CUDA Kernels: {'Available' if CUDA_KERNELS_AVAILABLE else 'Not Available (will compile on first GPU task)'}")
533
  print("Preloading models...")
534
  for style in STYLES.keys():
535
  try:
 
1308
  add_watermark: bool
1309
  ) -> Tuple[Optional[Image.Image], str, Optional[str]]:
1310
  """Main stylization function for Gradio."""
1311
+ global CUDA_KERNELS_AVAILABLE
1312
+
1313
  if input_image is None:
1314
  return None, "Please upload an image first.", None
1315
 
1316
  try:
1317
+ # On ZeroGPU, compile CUDA kernels within the GPU task on first use
1318
+ if SPACES_AVAILABLE and not CUDA_KERNELS_AVAILABLE:
1319
+ try:
1320
+ from kernels import compile_kernels
1321
+ CUDA_KERNELS_AVAILABLE = compile_kernels()
1322
+ if CUDA_KERNELS_AVAILABLE:
1323
+ print("CUDA kernels compiled successfully within GPU task!")
1324
+ except Exception as e:
1325
+ print(f"Failed to compile CUDA kernels: {e}")
1326
+
1327
  # Convert to RGB if needed
1328
  if input_image.mode != 'RGB':
1329
  input_image = input_image.convert('RGB')
kernels/__init__.py CHANGED
@@ -1,13 +1,20 @@
1
  """
2
  StyleForge CUDA Kernels Package
3
  Custom CUDA kernels for accelerated neural style transfer.
 
 
4
  """
5
 
6
  import torch
 
7
 
8
  # Try to import CUDA kernels, fall back gracefully
9
  _CUDA_KERNELS_AVAILABLE = False
10
  _FusedInstanceNorm2d = None
 
 
 
 
11
 
12
 
13
  def check_cuda_kernels():
@@ -26,18 +33,44 @@ def get_fused_instance_norm(num_features, **kwargs):
26
  return torch.nn.InstanceNorm2d(num_features, affine=kwargs.get('affine', True))
27
 
28
 
29
- # Try to import CUDA kernels on load
30
- if torch.cuda.is_available():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  try:
32
  from .instance_norm_wrapper import FusedInstanceNorm2d
33
  _FusedInstanceNorm2d = FusedInstanceNorm2d
34
  _CUDA_KERNELS_AVAILABLE = True
35
- except Exception:
36
- _CUDA_KERNELS_AVAILABLE = False
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  __all__ = [
40
  'check_cuda_kernels',
41
  'get_fused_instance_norm',
42
  'FusedInstanceNorm2d',
 
43
  ]
 
1
  """
2
  StyleForge CUDA Kernels Package
3
  Custom CUDA kernels for accelerated neural style transfer.
4
+
5
+ For ZeroGPU: Kernels are compiled on-demand within GPU task context.
6
  """
7
 
8
  import torch
9
+ import os
10
 
11
  # Try to import CUDA kernels, fall back gracefully
12
  _CUDA_KERNELS_AVAILABLE = False
13
  _FusedInstanceNorm2d = None
14
+ _KERNELS_COMPILED = False
15
+
16
+ # Check if running on ZeroGPU
17
+ _ZERO_GPU = os.environ.get('SPACE_ID', '').startswith('hf.co') or os.environ.get('ZERO_GPU') == '1'
18
 
19
 
20
  def check_cuda_kernels():
 
33
  return torch.nn.InstanceNorm2d(num_features, affine=kwargs.get('affine', True))
34
 
35
 
36
+ def compile_kernels():
37
+ """
38
+ Compile CUDA kernels on-demand.
39
+
40
+ This function is called within a GPU task on ZeroGPU to ensure
41
+ compilation happens within the task's timeout budget.
42
+ """
43
+ global _CUDA_KERNELS_AVAILABLE, _FusedInstanceNorm2d, _KERNELS_COMPILED
44
+
45
+ if _KERNELS_COMPILED:
46
+ return _CUDA_KERNELS_AVAILABLE
47
+
48
+ if not torch.cuda.is_available():
49
+ _KERNELS_COMPILED = True
50
+ return False
51
+
52
  try:
53
  from .instance_norm_wrapper import FusedInstanceNorm2d
54
  _FusedInstanceNorm2d = FusedInstanceNorm2d
55
  _CUDA_KERNELS_AVAILABLE = True
56
+ _KERNELS_COMPILED = True
57
+ print("CUDA kernels compiled successfully!")
58
+ return True
59
+ except Exception as e:
60
+ print(f"Failed to compile CUDA kernels: {e}")
61
+ print("Using PyTorch InstanceNorm2d fallback")
62
+ _KERNELS_COMPILED = True
63
+ return False
64
+
65
+
66
+ # Auto-compile on import for non-ZeroGPU environments
67
+ if torch.cuda.is_available() and not _ZERO_GPU:
68
+ compile_kernels()
69
 
70
 
71
  __all__ = [
72
  'check_cuda_kernels',
73
  'get_fused_instance_norm',
74
  'FusedInstanceNorm2d',
75
+ 'compile_kernels',
76
  ]