Repository: opencv/opencv
File: modules/dnn/src/layers/onehot_layer.cpp (C++)

Code before:

        Y.setTo(Scalar::all(offVal));

        Mat idx64;
        if (indices.depth() == CV_64S) idx64 = indices;
        else indices.convertTo(idx64, CV_64S);
        const int64_t* idxPtr64 = idx64.ptr<int64_t>();

        std::vector<size_t> strides(outShape.size());
        size_t stride = 1;
        for (int k = (int)outShape.size() - 1; k >= 0; --k) {
            strides[k] = stride;
            stride *= (size_t)outShape[k];
        }

        auto rankIdx = shape(indices);
        size_t totalIdx = indices.total();
        Tout* outPtr = Y.ptr<Tout>();

        std::vector<size_t> mappedStrides(rankIdx.size());
        for (int d = 0; d < (int)rankIdx.size(); ++d) {
            int outDim = d < insAxis ? d : d + 1;
            mappedStrides[d] = strides[outDim];
        }

        std::atomic<int64_t> numSet(0), numNegWrapped(0), numOOR(0);

        parallel_for_(Range(0, (int)totalIdx), [&](const Range& r){
            int64_t localSet = 0, localNeg = 0, localOOR = 0;
            for (int i = r.start; i < r.end; ++i) {
                int rem = i;
                size_t base = 0;
                for (int d = (int)rankIdx.size() - 1; d >= 0; --d) {
                    int dim = rankIdx[d];
                    int coord = rem % dim;
                    rem /= dim;
                    base += (size_t)coord * mappedStrides[d];
                }
                int64_t idxVal = idxPtr64[i];
                bool wasOutOfRange = (idxVal < 0 || idxVal >= depth);
                if (idxVal < 0) { localNeg++; }
                if (wasOutOfRange) { localOOR++; }
                int64_t wrapped = idxVal % depth;
                if (wrapped < 0) wrapped += depth;
                size_t pos = base + strides[insAxis] * (size_t)wrapped;
                outPtr[pos] = onVal;
                localSet++;
            }
            numSet += localSet;
            numNegWrapped += localNeg;
            numOOR += localOOR;

Review comment (suggestion):
it's not clear why you need atomic.

Basically, the loop should look like:

```
size_t inTotal = in.total();
size_t outTotal = depth*inTotal;
size_t outStep = <product_of_all_dims_after_axis_if_any>; // if axis==-1, outStep = 1.
// if input is N_0 ... N_{r-1}, the output will be N_0 ... N_{axis-1} depth N_{axis+1} ... N_{r-1}
// e.g. 1d array: [x0, ..., xK] => [0 0 0 1 */at x0 position */ 0 0 0; 0 0 ..1 /* at x1 */.. 0 0; ....]

parallel_for_(Range(0, (int)inTotal), [&](const Range& r){
       const T* inptr = in.ptr<T>();
       const V* vptr = values.ptr<V>(); // { _0, _1 };
       const V offval = vptr[0], onval = vptr[1];
       V* outptr = out.ptr<V>();
       for (int pos = r.start; pos < r.end; pos++) {
              T v = inptr[pos];
              int onpos = int(v);
              size_t outOffset = ... // outOffset = pos*depth*outStep (???)
              for (int i = 0; i < depth; i++)
                     outptr[outOffset + i*outStep] = offval;
              outptr[outOffset + onpos*outStep] = onval;
        }
});
```


Code after:
        Tout offVal = (Tout)0;
        Tout onVal  = (Tout)1;
        if (!values.empty())
        {
            CV_Assert(values.total() == 2);
            Mat flat = values.reshape(1, (int)values.total());
            offVal = tensorToScalar<Tout>(flat.row(0));
            onVal  = tensorToScalar<Tout>(flat.row(1));
        }

        Y.setTo(Scalar::all(offVal));

        Mat idx64;
        if (indices.depth() == CV_64S) idx64 = indices;
        else indices.convertTo(idx64, CV_64S);
        const int64_t* idxPtr64 = idx64.ptr<int64_t>();
        size_t inTotal = indices.total();
        size_t outStep = 1;
        for (int k = insAxis + 1; k < (int)outShape.size(); ++k)
            outStep *= (size_t)outShape[k];

        Tout* outPtr = Y.ptr<Tout>();

        parallel_for_(Range(0, (int)inTotal), [&](const Range& r){
            const int64_t* localIdxPtr64 = idxPtr64;
            Tout* localOutPtr = outPtr;
            const size_t localOutStep = outStep;
            const int localDepth = depth;
            const Tout localOnVal = onVal;
            for (int pos = r.start; pos < r.end; ++pos) {
                int64_t idxVal = localIdxPtr64[pos];
                int64_t wrapped = idxVal % localDepth;
                if (wrapped < 0) wrapped += localDepth;
                size_t hi = (size_t)pos / localOutStep;
                size_t lo = (size_t)pos % localOutStep;
                size_t base = hi * ((size_t)localDepth * localOutStep) + lo;
                localOutPtr[base + (size_t)wrapped * localOutStep] = localOnVal;
            }
        });
    }

public:
    void forward(InputArrayOfArrays in, OutputArrayOfArrays out, OutputArrayOfArrays) CV_OVERRIDE
    {
        int inKind = in.kind();
        int outKind = out.kind();
        CV_Assert(in.size().area() == 3);

        Mat indices, depthMat, values;
        if (inKind == _InputArray::STD_VECTOR_MAT)
        {