taichi-dev / taichi

Productive, portable, and performant GPU programming in Python.
https://taichi-lang.org
Apache License 2.0
25.47k stars 2.28k forks source link

Possible Taichi language improvement regarding to computing #3008

Open GargantuaOmni opened 3 years ago

GargantuaOmni commented 3 years ago

When we are developing some image processing programs, we have faced several problems which we might walk around but still noticed. We made those proposes here, though maybe not all of them are reasonable. We will be appreciate that if Taichi could make some improvement about it.

  1. The Slicing of Ndarray In some cases we will need different channels of a Ndarray. In some functions we have presumed that they have input Ndarrays with a certain dimension, for example, 1 or 4 in image processing programs. Sometimes we are expecting that those functions could be applied to Ndarrays with higher dimensions by SELECTING certain dimensions of those Ndarrays, by doing which we also expects there is no extra space cost.

For example, we want something like: Image -> VectorNdarray with 4 channels Image.slice([3], ......) returns a ScalarNdarray with 1 channel without spending more space on GPU

Data -> VectorNdarray with 8 channels Data.slice([2, 3, 5], ......) returns a VectorNdarray with 3 channels (2, 3, 5) without spending more space on GPU

  1. "Indexing a Scalar" In some cases we are using a ScalarNdarray(ScalarField) filled with a uniform value, for example. a full black image(a ScalarField with all entries zero). We want to reuse functions with ScalarNdarray input for this sometimes.

For example, we are using something like for P in ti.grouped(src): dst[P] = some_computation(src[P], ....) Sometimes when src is a uniform ScalarField we want we input a single scalar rather than a field to save space. We want that src[P] is equivalent to src if src is a scalar, thus we could reuse those functions in some way.

Discussion above also applies to VectorField.

  1. One-dimension Vector Sometimes we want one-dimension vector be equivalent with scalar. That is, src is a ScalarNdarray, 'a = ti.Vector([1.0])' and then src[P] += a should make sense.

  2. Automatically passing vector Sometimes we are constructing numpy array in python scope and pass it into Taichi kernel using ti.any_arr() and get a Ndarray inside the kernel. However, sometimes we are expecting a vector be automatically got through this passing procedure.

We understand that we need to ensure that the vector length should be fixed during compiling. Thus we propose that there be a way that allows us to pass a vector with an arbitrary but fixed length into kernel.

k-ye commented 3 years ago

Thanks for proposing all these!

Sometimes when src is a uniform ScalarField we want we input a single scalar rather than a field to save space.

I wonder if you can just create a 0-D ScalarField in this case :-) ?

GargantuaOmni commented 3 years ago

I am not sure. Does src[2, 3] make sense when src is a 0-D ScalarNdarray? Will compiler make it equivalent to src[0, 0]?

strongoier commented 3 years ago

I am not sure. Does src[2, 3] make sense when src is a 0-D ScalarNdarray? Will compiler make it equivalent to src[0, 0]?

Do you mean that your src is 0-D but dst is 2-D, and you want to set all values of dst to the value of src?

GargantuaOmni commented 3 years ago

Yes, and maybe with some computation. In script we expect to make the code available for src in both 2-D and 0-D, which will apply to different cases.

k-ye commented 3 years ago

I am not sure. Does src[2, 3] make sense when src is a 0-D ScalarNdarray? Will compiler make it equivalent to src[0, 0]?

I think it's better to have some kind of adaptor rather than asking a Taichi field to support a coordinate with mismatching dimensions

GargantuaOmni commented 3 years ago

Thanks. Let me think about it.