main.es6 4.1 KB
Newer Older
W
wangqun 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* eslint-disable */
/**
 * @file 主函数
 * @author yangmingming
 */
export default `
    // start函数
    void main(void) {
        ivec4 oPos = getOutputTensorPosLIMIT_OUT();
        int x = oPos.a;
        int c = oPos.g;
        int y = oPos.b;
13
        int b = oPos.r;
W
wangqun 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        float res = 0.0;

        // 获取output的坐标
        int oTensorChannel = (c / (channel_out / groups)) * channel_filter;
        int oy = y * stride_v - padTop;
        for (int fy = 0; fy < height_shape_filter; fy++) {
            if (oy >= height_shape_origin) {
                break;
            }
            if (oy < 0) {
                oy += dilation_v;
                continue;
            }
            int ox = x * stride_h - padLeft;
            for (int fx = 0; fx < width_shape_filter; fx++) {
                if (ox >= width_shape_origin) {
                    break;
                }
                if (ox < 0) {
                    ox += dilation_h;
                    continue;
                }
                // channel计算
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                for (int j = 0; j < filter_nearest_vec4; j += 4) {
                    vec4 fValues = vec4(
                        getValueFromTensorPosLIMIT_FILTER_filter(c, j, fy, fx),
                        getValueFromTensorPosLIMIT_FILTER_filter(c, j + 1, fy, fx),
                        getValueFromTensorPosLIMIT_FILTER_filter(c, j + 2, fy, fx),
                        getValueFromTensorPosLIMIT_FILTER_filter(c, j + 3, fy, fx)
                    );   

                    vec4 oValues = vec4(
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + j, oy, ox),
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + j + 1, oy, ox),
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + j + 2, oy, ox),
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + j + 3, oy, ox)
                      );

                    res += dot(fValues, oValues);
                }

                if (filter_remainder_vec4 == 1) {
                    res += dot(
                        getValueFromTensorPosLIMIT_FILTER_filter(c, filter_nearest_vec4, fy, fx), 
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + filter_nearest_vec4, oy, ox));
                } else if (filter_remainder_vec4 == 2) {
                    vec2 fValues = vec2(
                        getValueFromTensorPosLIMIT_FILTER_filter(c, filter_nearest_vec4, fy, fx),
                        getValueFromTensorPosLIMIT_FILTER_filter(c, filter_nearest_vec4 + 1, fy, fx)
                    );   
                    vec2 oValues = vec2(
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + filter_nearest_vec4, oy, ox),
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + filter_nearest_vec4 + 1, oy, ox)
                      );
                    res += dot(fValues, oValues);
                } else if (filter_remainder_vec4 == 3) {
                    vec3 fValues = vec3(
                        getValueFromTensorPosLIMIT_FILTER_filter(c, filter_nearest_vec4, fy, fx),
                        getValueFromTensorPosLIMIT_FILTER_filter(c, filter_nearest_vec4 + 1, fy, fx),
                        getValueFromTensorPosLIMIT_FILTER_filter(c, filter_nearest_vec4 + 2, fy, fx)
                    );   
                    vec3 oValues = vec3(
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + filter_nearest_vec4, oy, ox),
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + filter_nearest_vec4 + 1, oy, ox),
                        getValueFromTensorPosLIMIT_ORIGIN_origin(b, oTensorChannel + filter_nearest_vec4 + 2, oy, ox)
                    );
                    res += dot(fValues, oValues);
W
wangqun 已提交
81 82 83 84 85
                }
                ox += dilation_h;
            }
            oy += dilation_v;
        }
86 87 88 89 90 91

        float bi = getValueFromTensorPosLIMIT_BIAS_bias(0, 0, 0, c);
        res += bi;
        if (fuse_relu) {
            res = max(0.0, res);
        }
W
wangqun 已提交
92 93 94
        setOutput(res);
    }
`;