Skip to content

Conversation

@rydeveraumn
Copy link
Collaborator

@rydeveraumn rydeveraumn commented Sep 7, 2025

PyGranso 2.0 Prep 🚀 — Memory Leak Fix, CUDA OSQP Backend, Config Flag, & E2E Tests

Summary 📌

This PR prepares PyGranso 2.0 by:

  • 🧼 Fixing a memory leak that limited model size
  • ⚡ Adding an optional CUDA-accelerated OSQP backend
  • 🔧 Introducing a config parameter to select the QP solver backend
  • ✅ Validating across example notebooks and large-scale problems (e.g., topology optimization)

Motivation 💡

For 2.0 we need better scalability and GPU efficiency. This patch removes a long-standing memory retention issue and enables a CUDA OSQP pathway for QP subproblems throughout the algorithm.

What’s Changed 🛠️

  • Memory leak fix 🧠🩹

    • Eliminated autograd graph / tensor retention in solver loops while creating historical snap shots
    • Removed in-place ops on saved tensors along differentiable paths
    • Verified steady memory usage in long runs
  • CUDA OSQP backend 🧩⚙️

    • New integration layer for OSQP built with CUDA
    • Graceful fallback to CPU OSQP when CUDA build isn’t available
  • Configuration 🧭

    • Added qp_backend with values: "osqp" (default) or "osqp-cuda"

      • Optional bool alias use_cuda_osqp: bool = False
    • Example:

      # choose one style depending on your API
      solver = PyGranso(..., qp_backend="osqp-cuda")
      # or
      solver = PyGranso(..., use_cuda_osqp=True)
  • Testing & examples 🧪📓

    • Ran all example notebooks end-to-end
    • Exercised large problems (topology optimization) to verify stability, correctness, and memory behavior
    • Added/updated regression tests where applicable

Usage Notes 📝

  • Optional CUDA OSQP: install the wheel matching your CUDA/Python/OS, then set qp_backend="osqp-cuda". If not present, PyGranso auto-falls back to CPU OSQP.
  • No action needed for CPU-only users 👍

Backward Compatibility 🔄

  • No breaking changes—default remains "osqp".

Documentation 📚

  • Updated README/config docs for qp_backend / use_cuda_osqp and install notes for the CUDA OSQP wheel
  • Added troubleshooting tips (e.g., selecting the correct wheel)

QA Checklist ✅

  • Stable memory over long training runs 🧮
  • Example notebooks pass 🟢
  • Large-scale topology optimization completes with expected results 🏗️
  • CPU OSQP path unchanged 🖥️
  • CUDA OSQP path enabled + clean fallback 🧷
  • Docs updated 🖊️

@Wenjie-Zhang08 Wenjie-Zhang08 self-assigned this Sep 7, 2025
@rydeveraumn rydeveraumn marked this pull request as draft September 7, 2025 21:53
@rydeveraumn rydeveraumn added the enhancement New feature or request label Sep 7, 2025
@rydeveraumn
Copy link
Collaborator Author

rydeveraumn commented Sep 7, 2025

PyGranso 2.0 Prep 🚀 — Memory Leak Fix, CUDA OSQP Backend, Config Flag, & E2E Tests

Summary 📌

This PR prepares PyGranso 2.0 by:

  • 🧼 Fixing a memory leak that limited model size
  • ⚡ Adding an optional CUDA-accelerated OSQP backend
  • 🔧 Introducing a config parameter to select the QP solver backend
  • ✅ Validating across example notebooks and large-scale problems (e.g., topology optimization)

Motivation 💡

For 2.0 we need better scalability and GPU efficiency. This patch removes a long-standing memory retention issue and enables a CUDA OSQP pathway for QP subproblems throughout the algorithm.

What’s Changed 🛠️

  • Memory leak fix 🧠🩹

    • Eliminated autograd graph / tensor retention in solver loops while creating historical snap shots
    • Removed in-place ops on saved tensors along differentiable paths
    • Verified steady memory usage in long runs
  • CUDA OSQP backend 🧩⚙️

    • New integration layer for OSQP built with CUDA
    • Graceful fallback to CPU OSQP when CUDA build isn’t available
  • Configuration 🧭

    • Added opts.cuda_osqp_enabled with values: True or False`
  • Testing & examples 🧪📓

    • Ran all example notebooks end-to-end
    • Exercised large problems (topology optimization) to verify stability, correctness, and memory behavior
    • Added / updated regression tests where applicable

Usage Notes 📝

  • Optional CUDA OSQP: install the wheel matching your CUDA/Python/OS, then set opts.cuda_osqp_enabled=True. If not present, PyGranso auto-falls back to CPU OSQP.
  • No action needed for CPU-only users 👍

Backward Compatibility 🔄

  • No breaking changes—default remains "osqp".

Documentation 📚

  • Updated README/config docs for qp_backend / use_cuda_osqp and install notes for the CUDA OSQP wheel
  • Added troubleshooting tips (e.g., selecting the correct wheel)

QA Checklist ✅

  • Stable memory over long training runs 🧮
  • Example notebooks pass 🟢
  • Large-scale topology optimization completes with expected results 🏗️
  • CPU OSQP path unchanged 🖥️
  • CUDA OSQP path enabled + clean fallback 🧷
  • Docs updated 🖊️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants