Skip to content

Add Sparseness and Sparse Matrices - #177

Open
Soontosh wants to merge 1 commit into
stephane-caron:mainfrom
Soontosh:add_sparseness_only_2
Open

Add Sparseness and Sparse Matrices#177
Soontosh wants to merge 1 commit into
stephane-caron:mainfrom
Soontosh:add_sparseness_only_2

Conversation

@Soontosh

Copy link
Copy Markdown
Contributor

I added an optional use_sparse argument in build_ik / solve_ik that builds SciPy sparse QP matrices for sparse first solvers. I added warnings and fell back to dense matrices when JAX backends can't use them. I also check to see if SciPy is available and for NumPy's copy keywords support so that the feature fails without erroring. I updated the IK tests to cover sparse assembly, solver compatibility (including JAX), and to skip w/o error when SciPy and NumPy behavior isn't available.

If you have any questions or concerns regarding the current implementation, please let me know. I am happy to implement fixes or changes so that the implementation better suits pink's needs.

@Soontosh

Copy link
Copy Markdown
Contributor Author

Note: I haven't updated the changelog yet because I anticipate that the final version of this feature might be different from the current implementation.

@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 20489502116

Details

  • 45 of 63 (71.43%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.8%) to 97.614%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tests/test_solve_ik.py 24 30 80.0%
pink/solve_ik.py 21 33 63.64%
Totals Coverage Status
Change from base Build 19969518045: -0.8%
Covered Lines: 2168
Relevant Lines: 2221

💛 - Coveralls

@stephane-caron stephane-caron left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for proposing this 👍 It makes sense to me that Pink would handle conversion upstream of qpsolvers (even though qpsolvers will do a similar conversion itself, issuing a warning for it).

My feedback below is mainly about keeping changes to a minimum. Maybe I missed something about the numpy-copy check, feel free to let me know if that's the case.

Comment thread pink/solve_ik.py
from .tasks import Task


def _require_sparse_module():

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function could return None rather than re-throw. It could simplify the logic below (see other comment).

Comment thread pink/solve_ik.py
sparse_module = None
if use_sparse:
try:
sparse_module = _require_sparse_module()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _require_sparse_module returns an optional module (i.e. module or None), then we could refactor this block into:

if use_sparse:
    sparse_module = _require_sparse_module()
    if sparse_module is not None:
        P = ...
    else:  # sparse_module is None
        warnings.warn("... (message from above)")

Comment thread pink/solve_ik.py
homogeneous. If it helps we can add a tangent-space scaling to damp the
floating base differently from joint angular velocities.
"""
if use_sparse and solver in _JAX_SOLVERS:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both cases, use_sparse is set to False whenever the solver is a JAX solver. We could then simplify to:

if use_sparse and solver in _JAX_SOLVERS:
    warnings.warn("Falling back to dense matrices as JAX solvers don't support sparsity.")
    use_sparse = False

What do you think?

Let me know if I missed anything. Otherwise I'd suggest we remove the NUMPY_SUPPORTS_COPY_KEYWORD handling entirely, as it will make the logic simpler.

Comment thread tests/test_solve_ik.py

NUMPY_SUPPORTS_COPY_KEYWORD = _numpy_supports_copy_keyword()

SPARSE_SOLVERS = {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those could be imported from qpsolvers.

Comment thread tests/test_solve_ik.py

@unittest.skipIf(
sparse is None,
"SciPy is required for sparse tests. Install SciPy to run them.",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can assume SciPy is installed in test environments.

Comment thread tests/test_solve_ik.py
"NumPy copy keyword unsupported, skipping JAX-based solver."
)
configuration, tasks, dt = self.get_jvrc_problem()
solve_ik(configuration, tasks, dt, solver=solver)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this one as before, since the former test should still run?

If we want to introduce a second variant with use_sparse set to a non-default value, it should go to its own test. This way we grow the test fixture incrementally rather than revising it.

@Soontosh

Soontosh commented Jan 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback Dr. Caron. I think I will make pretty significant changes to this PR to make it more minimal.

I added the variable regarding NumPy since I was running into many errors locally without the check. However, what you pointed out makes it me think the codebase should be able to work with a much simpler approach, as you suggested. I made the commits in around September so I don't have the clearest memory about why I decided to use this roundabout method to address the incompatibility. I will revisit the codebase on my end to see if it is possible to remove the variable without running into issues.

Thank you again for the detailed review!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants