                     continue;
                 }
 
-                const size_t mat_y = x_val*input_size_y*input_size_z + y_val*input_size_z + inp_z;
+                const size_t mat_y = (out_x*stride_x - pad_x + x_shift)*input_size_y*input_size_z + (out_y*stride_y - pad_y + y_shift)*input_size_z + inp_z;
 
                 //if(out_x == 0 && out_y == 0 && out_z == 0)
                 //printf("%lu ", mat_y);

    const long int min_out_x = offset_x + last_x*shift_x;
    const long int min_out_y = offset_y + last_y*shift_y;

    const long int max_out_x = min_out_x + length_x;
    const long int max_out_y = min_out_y + length_y;

    const size_t inp_z = threadIdx.x;
    const size_t y_shift = threadIdx.y;
    const size_t x_shift = threadIdx.z;


                        for(size_t out_z = 0; out_z < output_size_z; out_z++)
                        {
                            const size_t mat_out = (out_x + min_out_x)*output_size_y*output_size_z + (out_y + min_out_y)*output_size_z + out_z;
                            const size_t a = n*output_size_x*output_size_y*output_size_z + mat_out;

                            const float_type prev_inf_coeff = expr_inf_coeff[a];
