question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

One array type to rule them all!

See original GitHub issue

I use Python 3’s typing features as much as possible. Unfortunately jax’s value hierarchy makes this a little bit challenging. Consider the following snippet,

from typing import NamedTuple

import jax.numpy as jp
from jax import lax, random

class Normal(NamedTuple):
  loc: ArrayType
  scale: ArrayType

  def sample(self, rng, sample_shape=()) -> ArrayType:
    batch_shape = lax.broadcast_shapes(self.loc.shape, self.scale.shape)
    return self.loc + self.scale * random.normal(
        rng, shape=sample_shape + batch_shape)

I’d like to be able to fill in the mystery ArrayTypes with something like a made-up “jp.Array”, but AFAICT from the array class hierarchy, there is no such type that really fits. At first glance jp.DeviceArray looks like an eligible candidate, but then there is also ConcreteArray, ShapedArray, and UnshapedArray. I’m not really sure what the differences are between them but some of them derive from jax.core.AbstractValue, while DeviceArray does not… If there are other cases then I’d certainly like to avoid limiting my type signatures to only operating on arrays that live on-device. To make matters more confusing there also seems to be _FilledConstant and DeviceConstant:

In [10]: jp.ones((2, 3))
Out[10]: 
_FilledConstant([[1., 1., 1.],
                 [1., 1., 1.]], dtype=float32)

All in all, it’s not clear to me how each of these types play together and how (if possible) to unify them. What’s the appropriate type to be used here? And if it does not yet exist, could we create such a type?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
samuelacommented, Jul 8, 2019

FWIW my solution here has been to begin a pyi module that is symlinked for both numpy and jax.numpy:

Shape = Tuple[int, ...]

class ndarray:
  @property
  def shape(self) -> Shape:
    ...

  def __getitem__(self, key) -> Any:
    ...

  def __add__(self, other) -> ndarray:
    ...

# and so on...

That certainly doesn’t solve the problem for jax.lax, etc. but it’s a start. I guess it also means that one way to approach this would be to have a jax-types package that contains a bunch of pyi definitions, instead of having to start including type annotations in jax itself if that is not desirable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

array-type - Rule
Config. One of the following arguments must be provided: "array" enforces use of T[] for all types T. "generic" enforces use of Array<T>...
Read more >
One reduce() to rule them all — How to use reduce in JavaScript
The idea behind reduce is a simple, but a very powerful, one. It allows you to take an array of data and transform...
Read more >
array-type | typescript-eslint
TypeScript provides two equivalent ways to define an array type: T[] and Array<T> . ... This rule accepts an options object with the...
Read more >
For of loops in Javascript one loop to rule them all
Then we got some fancy array methods like forEach , map , filter , etc. It starts to get a bit confusing of...
Read more >
One I/O Ring to Rule Them All: A Full Read/Write Exploit ...
If the previous checks pass and the new buffer array is to be used, a new paged pool allocation is made – this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found