Skip to content

Add Support for NDArrays with Zero elements#8621

Merged
danking merged 10 commits intohail-is:masterfrom
johnc1231:johnc-matmul-shape-zero
Apr 28, 2020
Merged

Add Support for NDArrays with Zero elements#8621
danking merged 10 commits intohail-is:masterfrom
johnc1231:johnc-matmul-shape-zero

Conversation

@johnc1231
Copy link
Contributor

This PR:

  • Fixes it so that we can pass numpy ndarray literals with no elements.
  • Makes sure we don't call BLAS when shape contains a 0.
  • Allows reshapes that contain a 0.

@johnc1231 johnc1231 force-pushed the johnc-matmul-shape-zero branch from 1fe3d69 to b6abd99 Compare April 24, 2020 19:22
@johnc1231
Copy link
Contributor Author

Alright, adjusted this to also make sure we don't pass a 0 into dgeqrf as the LDA argument and also to make sure that we test multiplying when only the inner dimension is a 0. Should be good for review now.

Copy link
Member

@patrick-schultz patrick-schultz left a comment

Choose a reason for hiding this comment

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

Nice! Just a couple small things.

(hypercube.reshape((5, 7, 9, 3)).reshape((7, 9, 3, 5)), np_hypercube.reshape((7, 9, 3, 5))),
(hypercube.reshape(hl.tuple([5, 7, 9, 3])), np_hypercube.reshape((5, 7, 9, 3)))
(hypercube.reshape(hl.tuple([5, 7, 9, 3])), np_hypercube.reshape((5, 7, 9, 3))),
(shape_zero.reshape((0, 5)), np_shape_zero.reshape((0, 5)))
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a case shape_zero.reshape((-1, 5)), and also assert an error is thrown in shape_zero.reshape((0, -1))?

Code(newShapeVars.zip(requestedShape).map { case (variable, shapeElement) =>
variable := (shapeElement ceq -1L).mux(quotient, shapeElement)
})
(runningProduct ceq 0L).mux(
Copy link
Member

Choose a reason for hiding this comment

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

I think the logic here could be cleaned up a bit. How about

Code(
  hasNegativeOne.mux(
    (runningProduct ceq 0) || (numElements % runningProduct) > 0L,
    numElements cne runningProduct
  ).orEmpty(Code._fatal[Unit]("Can't reshape since requested shape is incompatible with number of elements")),
  quotient := (runningProduct ceq 0).mux(0, numElements / runningProduct),
  Code(newShapeVars.zip(requestedShape).map { case (variable, shapeElement) =>
    variable := (shapeElement ceq -1L).mux(quotient, shapeElement)
  })

@danking danking merged commit 38dc9dd into hail-is:master Apr 28, 2020
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