Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow constant inputs to cost functions to be passed as floats #150

Merged
merged 10 commits into from
Apr 27, 2022

Conversation

jeffin07
Copy link
Contributor

Motivation and Context

To close #38

How Has This Been Tested

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have completed my CLA (see CONTRIBUTING)
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 10, 2022
@jeffin07
Copy link
Contributor Author

@luisenp @mhmukadam I have added some changes to collisions.py and double_integrator.py. Tests not included. I will add test once the feature is finalized.Also i do see lot of variables in other cost functions,Could you please tell me which one are elligible for this change ?

@mhmukadam mhmukadam added the refactor Refactor library components label Apr 10, 2022
Copy link
Contributor

@luisenp luisenp left a comment

Choose a reason for hiding this comment

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

This is looking good! Some tweaks needed but mostly easy fixes and unit tests. The only other variable I've seen so far where this applies is eff_radius in EffectorObjectContactPlanar. Poses and sdf variables (origin, cell size, data) are multidimensional, so no need to change those.

theseus/embodied/collision/collision.py Show resolved Hide resolved
dt = torch.tensor(dt)
self.dt = Variable(dt)
else:
self.dt = dt
Copy link
Contributor

Choose a reason for hiding this comment

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

After the ndim check below, we can add dt.data = self.dt.data.view(-1, 1), to make sure we have a batch dimension as well, as discussed above.

name: Optional[str] = None,
):
super().__init__(name=name)
if not isinstance(dt, Variable):
if not isinstance(dt, torch.Tensor):
dt = torch.tensor(dt)
Copy link
Contributor

Choose a reason for hiding this comment

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

I have added a fix for this class in #157, make sure you merge those changes as well.

dt = torch.tensor(dt)
self.dt = Variable(dt)
else:
self.dt = dt
super().__init__(pose1, vel1, pose2, vel2, dt, cost_weight, name=name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar comment about maintaining a batch shape here.

@jeffin07
Copy link
Contributor Author

@luisenp Added the changes. Please review.Also do you have any suggestions for unit test ?

Copy link
Contributor

@luisenp luisenp left a comment

Choose a reason for hiding this comment

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

LGTM! For unit tests, just constructing all of these classes with the different types of inputs, and checking that the corresponding member is a variable of the expected type and shape should be sufficient. Thanks!!

@jeffin07
Copy link
Contributor Author

@luisenp Added unit test. Can you please take a look ?

Copy link
Contributor

@luisenp luisenp left a comment

Choose a reason for hiding this comment

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

Looks good, almost there! Once the code is finished, I'll also run some tests on my side before confirming that we can merge. Thanks!

obj, eff, cost_weight, origin, sdf_data, cell_size, eff_radius
)

assert isinstance(cost_function.eff_radius, Variable)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we can check that cost_function.eff_radius is eff_radius.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can delete the line above that has the isinstance check now.

)

assert isinstance(cost_function.eff_radius, Variable)

Copy link
Contributor

Choose a reason for hiding this comment

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

Here we can add another check that cost_function.eff_radius.data is eff_radius_t.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@luisenp While assert cost_function.eff_radius.data is eff_radius_t gives error as one is tensor and other is th.Variable .Is it better to use assert torch.allclose(cost_function.eff_radius.data,eff_radius_t) ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused. Shouldn't both cost_function.eff_radius.data (notice, .data) and eff_radius_t be both torch tensors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@luisenp when i try to do assert cost_function.eff_radius.data is eff_radius_t i get this error which expained above

>           assert cost_function.eff_radius.data is eff_radius_t
E           assert tensor([[0.4160]], dtype=torch.float64) is tensor([[0.4160]], dtype=torch.float64)
E            +  where tensor([[0.4160]], dtype=torch.float64) = Variable(data=tensor([[0.4160]], dtype=torch.float64), name=Variable__680).data
E            +    where Variable(data=tensor([[0.4160]], dtype=torch.float64), name=Variable__680) = <theseus.embodied.collision.eff_obj_contact.EffectorObjectContactPlanar object at 0x7f813704edd0>.eff_radius

theseus/embodied/collision/tests/test_eff_obj_contact.py:180: AssertionError


Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right, it's because of the view(-1, 1) we added. In this case, let's stick to compare that they are close in value as you had initially.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@luisenp Then can you take a look at PR. I've already committed the changes

obj, eff, cost_weight, origin, sdf_data, cell_size, eff_radius_f
)

assert isinstance(cost_function.eff_radius, Variable)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we can first set up some random value for eff_radius_f and then add an additional check that np.isclose(cost_function.eff_radius.data.item(), eff_radius_f).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@luisenp
we're already giving a random value to eff_radius_f=1.0
So is this enough assert np.isclose(cost_function.eff_radius.data.item(), eff_radius_f)

Copy link
Contributor

Choose a reason for hiding this comment

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

By random I mean sampling a new value every iteration rather than hardcoding to 1.0.

@@ -60,6 +61,30 @@ def test_gp_motion_model_cost_weight_copy():
)


def test_gp_motion_model_variable_type():
Copy link
Contributor

Choose a reason for hiding this comment

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

Add similar modifications to the test here, as mentioned above.

assert isinstance(cost_weight.Qc_inv, Variable)
assert isinstance(cost_weight.dt, Variable)

q_inv_v = Variable(q_inv)
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need the second check for q_inv_v.

@jeffin07
Copy link
Contributor Author

@luisenp Added changes please review

@jeffin07
Copy link
Contributor Author

jeffin07 commented Apr 20, 2022

@luisenp @mhmukadam I'm unable to run tests after fetching latest changes from upstream/main. When running pytest theseus/ gives the following output. Any idea what I've done wrong ?

ImportError while importing test module '/theseus/theseus/optimizer/tests/test_sparse_linearization.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
theseus/optimizer/tests/test_sparse_linearization.py:9: in <module>
    import theseus as th
theseus/__init__.py:71: in <module>
    import theseus.embodied as eb
theseus/embodied/__init__.py:6: in <module>
    from .collision import Collision2D, EffectorObjectContactPlanar, SignedDistanceField2D
theseus/embodied/collision/__init__.py:6: in <module>
    from .collision import Collision2D
theseus/embodied/collision/collision.py:11: in <module>
    from theseus.embodied.kinematics import IdentityModel, KinematicsModel
theseus/embodied/kinematics/__init__.py:6: in <module>
    from .kinematics_model import IdentityModel, KinematicsModel, UrdfRobotModel
theseus/embodied/kinematics/kinematics_model.py:9: in <module>
    import differentiable_robot_model as drm
E   ModuleNotFoundError: No module named 'differentiable_robot_model'





This keeps repating for every file

@luisenp
Copy link
Contributor

luisenp commented Apr 20, 2022

We merged a new component that has an additional requirement. Can you try pip installing this?

cost_weight = th.eb.GPCostWeight(q_inv_v, dt_f)
assert isinstance(cost_weight.Qc_inv, Variable)
assert isinstance(cost_weight.dt, Variable)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, didn't think of this before, but we should also check that cost_weight.dt has a batch dimension. And similar for eff_radius_t in the other cost function. We are also missing the check np.isclose(cost_function.eff_radius.data.item(), eff_radius_f) (and similar for dt).

Copy link
Contributor

Choose a reason for hiding this comment

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

@jeffin07 Missing the np.allclose() check for eff_radius_f and also checking that the batch dimension is present.

Copy link
Contributor

@luisenp luisenp left a comment

Choose a reason for hiding this comment

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

Left two more comments.

@jeffin07
Copy link
Contributor Author

@luisenp Updated unit tests

@jeffin07
Copy link
Contributor Author

@luisenp Updated the changes.Please review

Copy link
Contributor

@luisenp luisenp left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, got busy with other PRs and forgot this was waiting for review. LGTM!

@luisenp
Copy link
Contributor

luisenp commented Apr 26, 2022

Thanks a lot for another great contribution, @jeffin07 :)

Copy link
Member

@mhmukadam mhmukadam left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks.

@jeffin07
Copy link
Contributor Author

@luisenp @mhmukadam Updated the changes. Please review

@mhmukadam mhmukadam merged commit 3498bbd into facebookresearch:main Apr 27, 2022
suddhu pushed a commit to suddhu/theseus that referenced this pull request Jan 21, 2023
…ookresearch#150)

* collision and double_integrator variable change

* unit test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. refactor Refactor library components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow constant inputs to cost functions to be passed as floats
4 participants