# Nd4j.scatterUpdates slower than simple CPU implementation

**URL:** <https://community.konduit.ai/t/nd4j-scatterupdates-slower-than-simple-cpu-implementation/2801>\
**Category:** ND4J\
**Created:** [August 31, 2023, 12:15pm UTC](https://community.konduit.ai/t/nd4j-scatterupdates-slower-than-simple-cpu-implementation/2801 "2023-08-31T12:15:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebeaufay](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/ebeaufay/32/1191_2.png) [@ebeaufay](https://community.konduit.ai/u/ebeaufay)\
**Post date:** [August 31, 2023, 12:15pm UTC](https://community.konduit.ai/t/nd4j-scatterupdates-slower-than-simple-cpu-implementation/2801/1 "2023-08-31T12:15:14Z")

</div>

I’m using the EmbeddingLayer and SequenceEmbeddingLayer but I noticed it was slow.

I tracked it down to the call to Nd4j.scatterUpdate

It felt unreasonably slow so I tried to do it in java.

from this:

```auto
        INDArray weightGradients = this.gradientViews.get("W");
        weightGradients.assign(0);
        INDArray indices = Nd4j.createFromArray(this.indexes);
        Nd4j.scatterUpdate(ScatterUpdate.UpdateOp.ASSIGN, weightGradients, indices, epsilon, WEIGHT_DIM);

```

to this:

```auto
        INDArray weightGradients = this.gradientViews.get("W");
        weightGradients.assign(0);

        float[][] weightGradientUpdates = new float[(int) this.layerConf().getDictionarySize()][(int) nOut];
        float[][] eps = new float[(int)nOut][(int)epsilon.size(0)];
        for (int j = 0; j < nOut; j++) {
            INDArray column = epsilon.getColumn(j);
            eps[j] = column.data().asFloat();
        }

        for (int i = 0; i < indexes.length; i++) {
            for (int j = 0; j < nOut; j++) {
                weightGradientUpdates[indexes[i]][j]+=eps[j][i];
            }
        }

        INDArray reshape = Nd4j.create(weightGradientUpdates).reshape(this.layerConf().getDictionarySize(), 1);
        weightGradients.addi(reshape);

```

makes the code 100x faster

The size of the weightsGradient does matter in the CPU work-around and if it gets too large (\>1M), the RAM grows too much and the call to Nd4j.create is slow. So, this CPU workaround doesn’t scale.

these are my backend dependencies and I have cuDnn set up:

```auto
<dependency>
            <groupId>org.nd4j</groupId>
            <artifactId>nd4j-cuda-11.6</artifactId>
            <version>1.0.0-M2.1</version>
        </dependency>
        <dependency>
            <groupId>org.nd4j</groupId>
            <artifactId>nd4j-cuda-11.6</artifactId>
            <version>1.0.0-M2.1</version>
            <classifier>windows-x86_64-cudnn</classifier>
        </dependency>

```

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 1, 2023, 7:38am UTC](https://community.konduit.ai/t/nd4j-scatterupdates-slower-than-simple-cpu-implementation/2801/2 "2023-09-01T07:38:33Z")

</div>

@ebeaufay can you setup a reproducer for me and file a github issue? [Sign in to GitHub · GitHub](https://github.com/deeplearning4j/deeplearning4j/issues/new)

---

<div class="post-metadata">

**Author:** ![ebeaufay](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/ebeaufay/32/1191_2.png) [@ebeaufay](https://community.konduit.ai/u/ebeaufay)\
**Post date:** [September 1, 2023, 5:51pm UTC](https://community.konduit.ai/t/nd4j-scatterupdates-slower-than-simple-cpu-implementation/2801/3 "2023-09-01T17:51:27Z")

</div>

yes, I added an issue: [Nd4j.ScatterUpdates has a large overhead · Issue #10029 · deeplearning4j/deeplearning4j · GitHub](https://github.com/deeplearning4j/deeplearning4j/issues/10029)

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 2, 2023, 2:14am UTC](https://community.konduit.ai/t/nd4j-scatterupdates-slower-than-simple-cpu-implementation/2801/4 "2023-09-02T02:14:44Z")

</div>

@ebeaufay thanks I’ll take a look!
