Daankular commited on
Commit
efc7f8b
·
1 Parent(s): 2bb9311

Skip DiffDMC (CUDA arch mismatch on A10G), use marching cubes with adaptive iso-level and bg_fill

Browse files
patches/triposg/triposg/inference_utils.py CHANGED
@@ -486,36 +486,26 @@ def flash_extract_geometry(
486
  del grid_logits
487
  torch.cuda.empty_cache()
488
  mesh_v_f = []
 
489
  try:
490
- if DiffDMC is not None:
491
- print("[TripoSG] Attempting DiffDMC extraction...")
492
- # Re-upload with NaN intact so DiffDMC ignores masked cells
493
- gl_gpu = torch.from_numpy(grid_logits_cpu).to(device)
494
- dmc = DiffDMC(dtype=torch.float32).to(device)
495
- sdf = -gl_gpu / octree_resolution
496
- sdf = sdf.contiguous()
497
- vertices, faces = dmc(sdf, deform=None, return_quads=False, normalize=False)
498
- vertices = vertices.detach().cpu().numpy()
499
- faces = faces.detach().cpu().numpy()[:, ::-1]
500
- vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
501
- mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
502
- print(f"[TripoSG] DiffDMC: {len(vertices)} vertices, {len(faces)} faces")
503
- if len(vertices) == 0:
504
- raise RuntimeError("DiffDMC returned empty mesh — falling back to marching cubes")
505
- else:
506
- raise RuntimeError("DiffDMC unavailable — using marching cubes")
507
  except Exception as e:
508
- print(f"[TripoSG] DiffDMC path failed ({e}), falling back to marching cubes")
509
- torch.cuda.empty_cache()
510
- try:
511
- # NaN → -1 makes masked/unqueried cells "outside" the isosurface
512
- gl_mc = np.nan_to_num(grid_logits_cpu, nan=-1.0)
513
- vertices, faces, normals, _ = measure.marching_cubes(gl_mc, 0, method="lewiner")
514
- vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
515
- mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
516
- print(f"[TripoSG] marching cubes: {len(vertices)} vertices")
517
- except Exception as e2:
518
- print(f"[TripoSG] marching cubes also failed: {e2}")
519
- mesh_v_f = (None, None)
520
 
521
  return [mesh_v_f]
 
486
  del grid_logits
487
  torch.cuda.empty_cache()
488
  mesh_v_f = []
489
+ # Marching cubes on CPU — skip DiffDMC (CUDA arch mismatch on ZeroGPU A10G)
490
  try:
491
+ # Replace NaN/background with min_real - 1 so they're clearly "outside"
492
+ real_vals = grid_logits_cpu[~np.isnan(grid_logits_cpu)]
493
+ if len(real_vals) == 0:
494
+ raise RuntimeError("grid contains no real values (all NaN)")
495
+ min_real = float(real_vals.min())
496
+ max_real = float(real_vals.max())
497
+ print(f"[TripoSG] grid value range: [{min_real:.4f}, {max_real:.4f}], real cells: {len(real_vals)}")
498
+ bg_fill = min_real - 1.0
499
+ gl_mc = np.where(np.isnan(grid_logits_cpu), bg_fill, grid_logits_cpu)
500
+ # Find a valid iso-level — prefer 0.0 but fall back to midpoint
501
+ iso_level = 0.0 if (min_real < 0.0 < max_real) else (min_real + max_real) * 0.5
502
+ print(f"[TripoSG] using iso_level={iso_level:.4f}")
503
+ vertices, faces, normals, _ = measure.marching_cubes(gl_mc, iso_level, method="lewiner")
504
+ vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
505
+ mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
506
+ print(f"[TripoSG] marching cubes: {len(vertices)} vertices, {len(faces)} faces")
 
507
  except Exception as e:
508
+ print(f"[TripoSG] marching cubes failed: {e}")
509
+ mesh_v_f = (None, None)
 
 
 
 
 
 
 
 
 
 
510
 
511
  return [mesh_v_f]