willow-ahrens / finch-tensor

Sparse Tensors in Python and more! Datastructure-Driven Array Programming Language
MIT License
7 stars 2 forks source link

Scalars in `finch-tensor` #25

Open mtsokol opened 3 months ago

mtsokol commented 3 months ago

Hi @willow-ahrens @hameerabbasi,

This issue is meant to discuss and decide the approach we would like to take in terms of handling scalars.

From existing discussion, when calling a function on a tensor and a scalar (Tensor(...) + 1), the scalar could be wrapped in a Tensor(1) and interpreted as:

  1. Sparse matrix with a background of 1,
  2. Sparse matrix with background value 0, filled with 1,
  3. Dense matrix Tensor(Dense(Element([1]))).
hameerabbasi commented 3 months ago

Paraphrasing my comments from Slack -- We should go with the first option, partially because that's the only backwards-compatible option. The concerns about a new kernel for each background value can be handled via a hybrid approach: We compile a new kernel iff something is an identity or an absorbing value of a registered function; otherwise we resort to treating the background value as a runtime value rather than compile-time constant.

willow-ahrens commented 3 months ago

Oh, that's interesting! So we would compile for 1, 0, inf, -inf. This seems like a very doable compromise, especially if we can add other "special" values to the compiler. We may want to do this in JuliaLand as well when we do broadcasts. A .+ 1 has the same issue in Julia

willow-ahrens commented 3 months ago

We should also figure out syntax to overload/avoid this behavior. If the default is to promote some values to constants, what is the way that we declare constant and what is the way we declare variable?

willow-ahrens commented 1 month ago

I just wanted to make a heads up: Tensor(Element(0.0)) is not very optimized for scalars. we should benchmark it to see what the overhead is before making changes, but I just thought I would give a heads up. This is in response to https://github.com/willow-ahrens/finch-tensor/pull/62