Git Product home page Git Product logo

Comments (12)

patrick-kidger avatar patrick-kidger commented on July 28, 2024 2

Thank you both for your thoughts. :)

Personally I agree with both of you; I'd much rather have Float["foo bar"] or Array[Float, "foo bar"] as well. However the Float[Array, "foo bar"] syntax is the best approach I've been able to find that has any compatibility with static type checkers.

This is necessary since jax.typing will happen at some point. They're happy to work with jaxtyping to ensure we don't bifurcate into incompatible approaches, but static type checking is their goal, so we need to work with them in turn. (I'm all ears to any suggestions that will pass both mypy+pytype.)

[+I'd quite like to support static type checking as well -- it's a pretty useful thing, when it works.]

@thomaspinder - regarding point 4, you mean that you would rather see Float32 over f32? (I don't have a strong preference myself, so I'd be happy to change this.)

@ayaka14732 - indeed, specifying numpy/JAX/etc. backend is also desirable. It's a lesser priority, but supporting this is also a goal of the proposed new syntax. (Note that it's quite difficult to have optional arguments for static type checkers; off the top of my head the only things which support this are typing.{Literal, Annotated, Tuple}, and variadic generics. An Annotated solution is probably the most likely to work.)

from jaxtyping.

thomaspinder avatar thomaspinder commented on July 28, 2024 2

Interesting. Thanks for the clarification. If it enables static type checking, then the Float[Array, "foo bar"] syntax seems like a necessary sacrifice.

@patrick-kidger Yes - I, personally, would prefer Float32 over f32.

from jaxtyping.

ayaka14732 avatar ayaka14732 commented on July 28, 2024 2

jaxtyping and jax.typing

  1. I hope that static type checking is not a main goal, if it would make things more complicated. I also think that compatibility is not necessary, since it would often increase the complexity. And having two separate projects would not be a bad thing, because we are free to choose between the two projects.
  2. As a consequence, the project name would have to be changed. Moreover, considering that we will be supporting not only jnp.array, but also NumPy/PyTorch/TensorFlow as well, the project name may not contain the word JAX.
  3. I suggest tensortyper as the new project name. It is easy to pronounce since both words starts with t and ends with r.

Syntax changes

  1. I think longer names like Float are better than shoter ones like f because 'explicit is better than implicit'. And if we really want to use shorter names, we can always do from jaxtyping import Float as f.
  2. If we are going to use the syntax Float[Array, "foo bar"], I hope that it could be FloatArray[Array, "foo bar"]. This is because the former sounds like a certain type of float instead of an array, similar to that List[int] is a certain type of list.
  3. I prefer IntSign/SignInt to IntS/SInt because 'explicit is better than implicit'. Besides, I think it should be IntSigned/SignedInt because 'signed' is the adjective.

from jaxtyping.

ayaka14732 avatar ayaka14732 commented on July 28, 2024 1

Would Float[jnp.ndarray, "batch length channels"] be a bit confusing? I am thinking that something like jnp.ndarray[Float, "batch length channels"] or FloatArray[jnp.ndarray, "batch length channels"] would look more intuitive

from jaxtyping.

ayaka14732 avatar ayaka14732 commented on July 28, 2024 1

@thomaspinder

Whilst by no means a strong opinion, I prefer Float["foo"] over Float[Array, "foo"]. Everything in GPJax where we use JaxTyping is an Array, so the additional line is somewhat superfluous.

A common use case I can think of is that when the dataset is too large to fit into the TPU memory, we usually load the entire dataset into the CPU memory as a NumPy array first, then slice the dataset into small batches and convert them to JAX arrays on TPU. In this case it would be necessary to distinguish between np.array and jnp.array. (See google/jax#8933 (comment))

Besides, I am also wondering if it is possible to make np.array/jnp.array an optional second argument, so that we can just omit it if we are indifferent to the type.

from jaxtyping.

patrick-kidger avatar patrick-kidger commented on July 28, 2024

CC @leycec for curiosity, as someone who evidently has a lot of strong opinions on type-checking. (Also you might like the trick through which Float[Array, "foo"] is made to work with static type checkers.)

from jaxtyping.

thomaspinder avatar thomaspinder commented on July 28, 2024

Thanks for the heads up @patrick-kidger. I'm OK with the look of both proposed changes, though I do particularly like the explicitness of the former.

To answer your specific questions in order:

  1. Personally, I'm fine with this.
  2. I like the verbosity of IntSign and IntUnsign. It is a few more characters, but it mitigates any confusion i.e., IntS could be misconstrued as multiple Ints.
  3. For my above reasoning, I like Float
  4. As above.
  5. Whilst by no means a strong opinion, I prefer Float["foo"] over Float[Array, "foo"]. Everything in GPJax where we use JaxTyping is an Array, so the additional line is somewhat superfluous.

As an aside, it would be cool to see more mathematically inspired typing systems e.g., ColumnVector['N'] would type a Nx1 vector. Extensions would be RowVector, Scalar, Matrix and Tensor. I have no idea how feasible this is and how you feel about it but I'm happy to open a separate issue where we can discuss further if it is of interest though.

from jaxtyping.

PhilipVinc avatar PhilipVinc commented on July 28, 2024

Alternate project names? Having both jax.typing and jaxtyping is a bit confusing. (But perhaps not too bad, if jaxtyping is a strict superset.)

Taking inspiration from typing-extensions official python package, maybe rename jaxtyping to jaxtyping-extensions?

from jaxtyping.

irhum avatar irhum commented on July 28, 2024

Thanks for this RFC @patrick-kidger! Regarding the naming for types, I'd say "adherence to the NumPy API" to be one axis to evaluate syntax changes on. JAX's own potential stems from "it's just NumPy", and this would help onboard new users as quickly as possible. By that line of reasoning:

  • Verbosity: Types should be more verbose; not just NumPy, but also Python's own "explicit is better than implicit" plays into Float32 feeling more "pythonic" than f32.
  • Specific Cases: Extending this reasoning, I'd prefer to see syntax such as Int and UInt over IntSign and IntUnsign, since they're closer to the established np.int and np.uint.
  • Imports: A question I've been thinking is: should types be direct imports (eg. from typing import Tuple) or module-level imports (e.g. import numpy as np, then use np.float32)? I could easily see either Float or jxt.float style syntax working (where jxt would be import jaxtyping as jxt). After discussion, I personally would lean towards the former (direct imports), since these classes exist to aid type-checking.
  • Open Question: How challenging would it be under Python to have a type such as Array[Float, "b t d"] as opposed to Float[Array, "b t d"] as proposed here? I'm curious if this is a design choice, or reflection of more fundamental limitations in the language, since the former would be closer to how other languages (such as Julia's Array{Float32}) present their types.

I'll add this: I'm enjoying using this library; the multi-argument runtime type-checking over tensors is a brilliant idea, makes me go "how was I not checking this before?". The fact it just works with all the other JAX transforms (jit, pmap, etc.) is equally neat. The syntax changes shouldn't take away from the underlying achievement that, even as the library is right now, it works well! As this RFC is elaborated on, I'm curious to see the fleshing out of the "default, recommended" way to use jaxtyping going forward.

from jaxtyping.

patrick-kidger avatar patrick-kidger commented on July 28, 2024

Thanks all for your feedback. The new version has now been merged: #16.

(In particular I appreciate all the positive feedback to the effect of "jaxtyping is super useful, thanks" -- this warms a library author's heart.)

To respond to the last round of comments raised:

  • I liked the suggestion of @PhilipVinc to rename to jaxtyping-extensions, and came close to doing this. In the end I decided against this just because a shorter name is honestly a little more marketable. (+one fewer backward-incompatible change)
  • The general feedback was a preference for Float32 over Float, so that has now happened.
  • Lots of different ideas on what to call the integer types. In the end I decided to follow NumPy: Int for signed integers, UInt for unsigned integers, and Integer for both.
  • With apologies to @ayaka14732, the goal of these changes was in large part to provide compatibility with static typing, which is important for compatibility with core JAX.
  • @irhum - indeed Array[Float, "foo bar"] would have been a nice syntax, but unfortunately this is incompatible with static typing.

Once again, thankyou everyone for your engagement!

from jaxtyping.

riven314 avatar riven314 commented on July 28, 2024

@patrick-kidger
I am sorry if my question is out of scope, but I have a question on multi-arguments type checking as I see this concept is mentioned in the thread:
in the context of tensor shape, what is something special about multi-arguments type checking (v.s. single argument type checking)? Would you mind briefly explaining how it works differently under multi-arguments? (I couldn't find related references with a few searches) And why it has to be decorated by @jaxtyped and @typechecker?

from jaxtyping.

patrick-kidger avatar patrick-kidger commented on July 28, 2024

The multi-argument checking is in reference to checking that, across multiple arguments to a function, the sizes of multiple arrays should agree.

For example def foo(x: Shaped[Array "bar"], y: Shaped[Array, "bar"]) should be called with two arrays of the same size.

from jaxtyping.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.