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

Fix linter and add pre-commit CI #193

Closed
wants to merge 12 commits into from
Closed

Fix linter and add pre-commit CI #193

wants to merge 12 commits into from

Conversation

younik
Copy link
Collaborator

@younik younik commented Oct 11, 2024

Apologies for the big PR; I can split it in two if it is too hassle to review.

This PR adds the pre-commit check on CI and fixes all the problems with linters.

To fix the problems with pyright:

  • I removed torchtyping, in favor of torch.Tensor only. In this way, we lost shape typing (which is a nice feature), but we can have static typing checks. I suggest documenting well the functions with the expected shape and adding assert on shape when necessary.
  • Fixing a likely bug in ReplayBuffer.add. Please check the fix as it is different from Small fixes #192
  • ReplayBuffer itself is a bit problematic because it can accept Trajectories, Transitions, and tuples of States. While Trajectories and Transitions inherit from Container, States doesn't. Also, the code has a bunch of if-else to handle the cases differently. Should States (or a new class StatesTuple) inherit from Container, and require for Container objects a complete interface so ReplayBuffer can be agnostic to the underline object? Or should we have different subclasses of ReplayBuffer? For simplicity, atm, I proposed to make States inherit from Container, but I suggest we think about it (e.g. an object tuple of states may be more appropriate to inherit from Container). I can address this in a future PR.
  • Trajectory should inherit from Generic and differentiate between DiscreteStates and States, since they have different methods and some codes expect one over the else. For simplicity, I didn't do it in this PR.

@josephdviviano josephdviviano self-requested a review October 11, 2024 15:02
@younik younik marked this pull request as ready for review October 12, 2024 17:04
Comment on lines +222 to +224
assert isinstance(
self.training_objects, (Trajectories, Transitions)
) # TODO: becasue we use last_states... is it correct?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

check this: look like self.cutoff_distance>=0 only when we are working with Trajectories and Transitions, but there is no real check for it during init. Should this branch work also with tuple of states?

Comment on lines +261 to +262
estimator_outputs = self.estimator_outputs
other_estimator_outputs = other.estimator_outputs
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[these things are required for narrowing types by the checker, which cannot be done on self attributes]

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a comment explaining this (and other similar decisions which might confuse a future developer if they don't know the underlying reason)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, make sense


class States(ABC):

class States(Container, ABC):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

check if this is okay

Copy link
Collaborator

Choose a reason for hiding this comment

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

Having states inherit from Container does not change it's function at all, correct, but adds some handy save/load features and makes it automatically compatible with the buffers, correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, exactly. For example, Container have load/save which is used in ReplayBuffer.

@josephdviviano
Copy link
Collaborator

Hi Omar thanks this is great.

Perhaps can you split this into two PRs - one for typing and one for the replay buffer?

I think the typing requires more investigation before we undo all of that work, but the replay buffer changes seem very important to resolve soon.

Thanks so much!

@younik
Copy link
Collaborator Author

younik commented Oct 18, 2024

Sure, I did it here: #202

@saleml
Copy link
Collaborator

saleml commented Oct 19, 2024

Thanks Omar for initiating this important work.

If we were to indeed remove torchtyping in favor of torch.Tensor (which I am leaning towards, following the discussion we had ~10 days ago), I highly suggest we follow Joseph's recommendation of forcing the shapes of inputs, whenever a function takes tensors as inputs.

One way to do that is to create a repo-wide subclass of Exception that takes as input the tensor to check, the desiderata (ndim, shape, type... ?) and throws an error when the conditions are not met?

@saleml saleml assigned saleml and unassigned saleml Oct 19, 2024
@saleml saleml self-requested a review October 19, 2024 08:25
@younik
Copy link
Collaborator Author

younik commented Oct 19, 2024

One way to do that is to create a repo-wide subclass of Exception that takes as input the tensor to check, the desiderata (ndim, shape, type... ?) and throws an error when the conditions are not met?

This is a very good idea, indeed.

I will do the work in other smaller PRs (drop torch typing, fix other typing errors, ...) for the sake of review.

@josephdviviano
Copy link
Collaborator

Hey Omar just a heads up - doing the review now but looks like you might have some merge conflicts to resolve as well. I like the plan stated above by you two RE: removing torchtyping.

@younik
Copy link
Collaborator Author

younik commented Oct 24, 2024

Hey Omar just a heads up - doing the review now but looks like you might have some merge conflicts to resolve as well. I like the plan stated above by you two RE: removing torchtyping.

Hey @josephdviviano, yes, please check #204. The plan is to break this PR into pieces, so I will close it.

@younik younik closed this Oct 24, 2024
Copy link
Collaborator

@josephdviviano josephdviviano left a comment

Choose a reason for hiding this comment

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

OK awesome PR - thanks - I have a few requests (sorry one of them i s super annoying -- the nature of a PR like this).

Really appreciate your attention to detail.

.pre-commit-config.yaml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
src/gfn/containers/replay_buffer.py Outdated Show resolved Hide resolved

from gfn.containers import Trajectories
from gfn.env import Env
from gfn.gflownet.base import TrajectoryBasedGFlowNet
from gfn.modules import GFNModule, ScalarEstimator

ContributionsTensor = TT["max_len * (1 + max_len) / 2", "n_trajectories"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case, I think the notes will be particularly important.

src/gfn/gym/helpers/test_box_utils.py Show resolved Hide resolved

class States(ABC):

class States(Container, ABC):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Having states inherit from Container does not change it's function at all, correct, but adds some handy save/load features and makes it automatically compatible with the buffers, correct?

testing/test_gflownet.py Outdated Show resolved Hide resolved
tutorials/examples/train_line.py Show resolved Hide resolved
@josephdviviano
Copy link
Collaborator

@younik please use my notes from this review in any future PR, I don't want to have to re-do it once more.

Copy link
Collaborator Author

@younik younik left a comment

Choose a reason for hiding this comment

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

Apologies for making you review this big one.
If this PR looks good to you, we can move this forward and add the asserts (and docs) on shape immediately after. Otherwise, we can move the smaller one forward (I will integrate the comments here that are related to that one).

.pre-commit-config.yaml Outdated Show resolved Hide resolved
src/gfn/containers/replay_buffer.py Outdated Show resolved Hide resolved
Comment on lines +261 to +262
estimator_outputs = self.estimator_outputs
other_estimator_outputs = other.estimator_outputs
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, make sense

@@ -124,8 +131,8 @@ def get_pfs_and_pbs(
fill_value: float = 0.0,
recalculate_all_logprobs: bool = False,
) -> Tuple[
TT["max_length", "n_trajectories", torch.float],
TT["max_length", "n_trajectories", torch.float],
Tensor,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, make sense. There is #204 which does it with asserts. As we briefly discussed last time, I also believe it makes sense to add the information to the documentation. I will do it in #204


class States(ABC):

class States(Container, ABC):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, exactly. For example, Container have load/save which is used in ReplayBuffer.

testing/test_gflownet.py Outdated Show resolved Hide resolved
@younik
Copy link
Collaborator Author

younik commented Oct 25, 2024

I open it again and make it a draft, otherwise, it doesn't get updated.

@younik younik reopened this Oct 25, 2024
@younik younik marked this pull request as draft October 25, 2024 16:41
@josephdviviano
Copy link
Collaborator

Hey @younik - I assume this is now safe to close?

@younik
Copy link
Collaborator Author

younik commented Oct 30, 2024

Hey @younik - I assume this is now safe to close?

Yes, I close it.

@younik younik closed this Oct 30, 2024
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