cnuernber / dtype-next

A Clojure library designed to aid in the implementation of high performance algorithms and systems.
Other
319 stars 18 forks source link

`broadcast` --- some special cases and notes #54

Closed harold closed 2 years ago

harold commented 2 years ago

Here are some of my thoughts about broadcast from the last couple days:

1) Docstring typos "Broadcase":

2) Trouble broadcasting up more than one dimension, at least in this case:

Got a tensor:

> (dtt/ensure-tensor [1 2 3])
#<tech.v3.tensor_api.DirectTensor@26a4189e #tech.v3.tensor<object>[3]
[1 2 3]>

Up one dimension, no problem:

> (-> (dtt/ensure-tensor [17 18 19 20])
      (dtt/broadcast [3 4]))
#<tech.v3.tensor_api.Tensor@2685e08f #tech.v3.tensor<object>[3 4]
[[17 18 19 20]
 [17 18 19 20]
 [17 18 19 20]]>

Up two dimensions raises an exception:

> (-> (dtt/ensure-tensor [17 18 19 20])
      (dtt/broadcast [2 3 4]))
Execution error at tech.v3.datatype.index_algebra.IndexAlg/broadcast (index_algebra.clj:197).
Requested length 3 and base length 4 are not commensurate

But one at a time I can get the result I want:

> (-> (dtt/ensure-tensor [17 18 19 20])
      (dtt/broadcast [3 4])
      (dtt/broadcast [2 3 4]))
#<tech.v3.tensor_api.Tensor@37156b1 #tech.v3.tensor<object>[2 3 4]
[[[17 18 19 20]
  [17 18 19 20]
  [17 18 19 20]]
 [[17 18 19 20]
  [17 18 19 20]
  [17 18 19 20]]]>

hth- thanks for this awesome lib, it's definitely helping me down the path I'm on at the moment.

image

cnuernber commented 2 years ago

Right- that is an issue. Theoretically any shape can be extended at any point with a '1' and nothing about the actual data layout changes. In this case the broadcast operator should be able to left-extend the source shape with '1's until it matches the target shape in length.

Great pic!

cnuernber commented 2 years ago

Fixed in 8.064

harold commented 2 years ago

Okay! This is working:

image

Very cool.

https://user-images.githubusercontent.com/7443/151045166-1c81b938-9b81-4a58-95e3-38d3d9642154.mp4

cnuernber commented 2 years ago

Nice retro! Did you literally do the raster yourself?

harold commented 2 years ago

I did. And with some new thoughts, as well.

Clojure is really a fun programming language.

Related: This gif made me realize how uptight might thinking had become:

Pixel_vs_subpixel_precision