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

NamedTuples not being documented properly #669

Open
fabianp opened this issue Dec 10, 2023 · 15 comments
Open

NamedTuples not being documented properly #669

fabianp opened this issue Dec 10, 2023 · 15 comments
Labels
documentation Improvements or additions to documentation work-in-progress

Comments

@fabianp
Copy link
Member

fabianp commented Dec 10, 2023

Note the redundancy and all the "Alias for field number X" in classes such as optax.MultiStepsState: https://optax.readthedocs.io/en/latest/api/optimizer_wrappers.html#optax.MultiStepsState

image

@fabianp fabianp added the documentation Improvements or additions to documentation label Dec 10, 2023
@amosyou
Copy link
Contributor

amosyou commented Jan 21, 2024

I took a look at using sphinx-toolbox to autodoc the namedtuples, but I wasn't able to resolve the duplicate fields showing up. It seems that this is a known issue according to here, not too sure how else to go about fixing this.

I think it's possible to remove the duplicate 'Alias for field ___' but the issue is combining the type with the docstring

@fabianp
Copy link
Member Author

fabianp commented Jan 22, 2024

Thanks for looking into it @amosyou . Eliminating the "Alias for field ___" would already be better, even if we loose the type information IMO. Could you post a screenshot of how that would look like?

@amosyou
Copy link
Contributor

amosyou commented Feb 3, 2024

Screenshot 2024-02-03 at 12 48 46 AM

this is what it looks like if you remember the :members: line in each of the autoclass directives

@fabianp
Copy link
Member Author

fabianp commented Feb 3, 2024

I think that looks good! (module some formatting issues that I thin are orthogonal to this issue). Could you submit a pull request for this?

You're on 🔥 @amosyou !

@amosyou
Copy link
Contributor

amosyou commented Feb 15, 2024

revisiting this! so removing :members: from the autoclass directives isn't a viable solution since it also removes fields that are accurately documented like for LookaheadState

Screenshot 2024-02-14 at 11 19 07 PM

i've tried using the autodoc-skip-member event as suggested here but it doesn't seem to be removing the duplicate alias fields. i've also tried modifying the _monkey_patch_doc_strings in conf.py to replace lines with "Alias for field number" with empty string, also didn't work. not sure where to go from here 😅

@vroulet
Copy link
Collaborator

vroulet commented Feb 15, 2024

Thank you @amosyou for looking into this!
I've just tried removing :members: from the LookAheadState and the fast_state and steps_since_sync were still in the doc. One can even still have types if we are brave enough to make a pass on the whole package.

class LookaheadState(NamedTuple):
  """State of the `GradientTransformation` returned by `lookahead`.

  Attributes:
    fast_state (:class:`optax.OptState`): Optimizer state of the fast optimizer.
    steps_since_sync (``jnp.ndarray``): Number of fast optimizer steps taken since
      slow and fast parameters were synchronized.
  """
  fast_state: base.OptState
  steps_since_sync: jnp.ndarray
Screenshot 2024-02-15 at 08 42 34

@fabianp
Copy link
Member Author

fabianp commented Feb 15, 2024

@vroulet taht solutions looks great to me!

@vroulet
Copy link
Collaborator

vroulet commented Feb 15, 2024

Ok, but I may have missed something, @amosyou can you try again on your side and see if that works? Your idea of removing the :members: sounded great :). I let you see if you want to open a PR to remove the :members: and (if you are extra brave) adding the types.

@amosyou
Copy link
Contributor

amosyou commented Feb 15, 2024

oh that looks good! hmm I just tried commenting out :members: again and the entire docstring for LookaheadState is gone from the docs (this has never happened before).

Screenshot 2024-02-15 at 12 05 16 AM

@vroulet
Copy link
Collaborator

vroulet commented Feb 15, 2024

Strange. All states are missing in that screenshot (they're in pink not in blue). There may be another issue?
Here is the modification of the code on my side:

Flatten
~~~~~~~~
.. autofunction:: flatten

Lookahead
~~~~~~~~~~~~~~~~~
.. autofunction::  lookahead
.. autoclass::  LookaheadParams
   :members:
.. autoclass::  LookaheadState

Masked update
~~~~~~~~~~~~~~
.. autofunction::  masked
.. autoclass::  MaskedState
   :members:

So really just removing the line :members: below LookaheadState

@amosyou
Copy link
Contributor

amosyou commented Feb 15, 2024

my bad! commenting in rst is not with # that was why they were missing 🤥

on a side note, I think to have proper rendering of the docstring we need to change Fields to Attributes (even though we're working with namedtuples here since we're using autoclass). hope that's fine?

i'll create a PR for this. thanks @vroulet!

@vroulet
Copy link
Collaborator

vroulet commented Feb 15, 2024

Yes, typically, MultiStepsState should be reformatted with attributes instead of fields. Thanks again for looking into that!

@amosyou
Copy link
Contributor

amosyou commented Feb 15, 2024

also for ApplyIfFiniteState, I noticed that all the fields are typed as Any. Did we figure out a solution for the typing jnp.array?

@vroulet
Copy link
Collaborator

vroulet commented Feb 15, 2024

also for ApplyIfFiniteState, I noticed that all the fields are typed as Any. Did we figure out a solution for the typing jnp.array?

They should be Union[jax.Array, int] for notfinite_count, last_finite, and total_notfinite, I believe.
For inner_state it should be the usual base.Optstate

@vroulet
Copy link
Collaborator

vroulet commented Feb 15, 2024

my bad! commenting in rst is not with # that was why they were missing 🤥

on a side note, I think to have proper rendering of the docstring we need to change Fields to Attributes (even though we're working with namedtuples here since we're using autoclass). hope that's fine?

i'll create a PR for this. thanks @vroulet!

PS: no need to add types for all attributes (it's possible but it would take forever). Simply remove the :members: in the doc. If people want to know the types, they should be able to click on [source] in the doc.
That said, if you find some issues (like the fields in MultiStepsState (which is badly formatted anyway) or like in the ApplyFiniteState you pointed out), would be super cool if you could fix them by the way. We can review that in your PR together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation work-in-progress
Projects
None yet
Development

No branches or pull requests

3 participants