Skip to content

Multidimensional arrays with partial lookups fail #442

@soswow

Description

@soswow

Sorry for poor title. Feel free to modify it if you know how to phrase it.
Here is a simple example:

This will bring trouble:

this.matMult = gpu.createKernel(function (Vw, weights, bias) {
      var a = 0.0;
      var wi = weights[this.thread.x];
      for (var i = 0; i < this.constants.num_inputs; i++) {
        a += Vw[i] * wi[i];
      }
      a += bias[this.thread.x];
      return a;
    }, {
      constants: { num_inputs: this.num_inputs },
      output: [this.out_depth],
    });

But this won't:

this.matMult = gpu.createKernel(function (Vw, weights, bias) {
      var a = 0.0;
      for (var i = 0; i < this.constants.num_inputs; i++) {
        a += Vw[i] * weights[this.thread.x][i];
      }
      a += bias[this.thread.x];

      return a;
    }, {
      constants: { num_inputs: this.num_inputs },
      output: [this.out_depth],
    });

This is what has been generated for the first example:

return function (user_Vw, user_weights, user_bias) {
        const constants_num_inputs = this.constants.num_inputs;
      
      const result = new Float32Array(5);
    
		
    for (let x = 0; x < 5; x++) {
      this.thread.x = x;
      this.thread.y = 0;
      this.thread.z = 0;
      let kernelResult;
      var user_a=0;
var user_wi=user_weights[_this.thread.x];
for (var user_i=0;(user_i<constants_num_inputs);user_i++){
user_a+=(user_Vw[user_i]*user_wi);}

user_a+=user_bias[_this.thread.x];
kernelResult = user_a;
      result[x] = kernelResult;
      
    }

See how user_wi is not indexed.

Activity

robertleeplummerjr

robertleeplummerjr commented on Mar 17, 2019

@robertleeplummerjr
Member

You have to look up arrays directly for compatibility on GPU. This type of behavior isn't supported.

soswow

soswow commented on Mar 17, 2019

@soswow
Author

@robertleeplummerjr considering you allow write JS code it might give an impression that this is normal. The fact it's not supported is not intuitive. It took me some time to understand why I was getting NaN all over the place. Maybe this needs to be mentioned in Docs. Maybe it is already there. Sorry if I missed it.
Thank you for your work.

robertleeplummerjr

robertleeplummerjr commented on Mar 17, 2019

@robertleeplummerjr
Member

We are always looking for ways to make the project better. Do you have a suggestion as to the verbiage that would help ones like yourself in the future? Perhaps we need to throw an error on compilation, as well? Ty for the feedback, I joined the project far after it was initially conceptualized, and loved the core idea which is to accelerate using GPU with simple javascript. Together we can make it far better.

soswow

soswow commented on Mar 17, 2019

@soswow
Author

I am not sure how you do the transformation (compilation). But if you can detect these cases easily - yeah, blowing up with an exception would be perfect, since this result is clearly not what user would expect.
And please, change the title of this issue for something more SEOable so that someone else might find it. I don't know how to describe this =(

changed the title [-]Wrong re-compilation[/-] [+]Multidimensional arrays with partial lookups fail[/+] on Mar 17, 2019
robertleeplummerjr

robertleeplummerjr commented on Mar 17, 2019

@robertleeplummerjr
Member

Lets reopen this and add that to the list then. Ty for the suggestion and viewpoint!

added this to the v2.1.0 milestone on May 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @soswow@robertleeplummerjr

        Issue actions

          Multidimensional arrays with partial lookups fail · Issue #442 · gpujs/gpu.js