|
| 1 | +/** |
| 2 | + * @file slice |
| 3 | + * @description slice op,目前 case 很单一,暂时仅支持遇到的 case:slice final dim |
| 4 | + * @example x = [[1,2,3,4],[5,6,7,8]] axes=[1] starts=[2] ends=[3] => out [3,7] |
| 5 | + */ |
| 6 | + |
| 7 | +import { env } from '@paddlejs/paddlejs-core'; |
| 8 | +import { initializeGLSLArr, ArrTypeEnum } from '../atom/common_utils'; |
| 9 | + |
| 10 | + |
| 11 | +function mainFunc( |
| 12 | + { |
| 13 | + out, origin |
| 14 | + }, |
| 15 | + { |
| 16 | + axes, |
| 17 | + starts, |
| 18 | + ends, |
| 19 | + decrease_axis |
| 20 | + } |
| 21 | +) { |
| 22 | + |
| 23 | + if ( |
| 24 | + axes.length > 1 |
| 25 | + || starts.length > 1 |
| 26 | + || ends.length > 1 |
| 27 | + || (decrease_axis && decrease_axis.length === 0) |
| 28 | + ) { |
| 29 | + throw Error('[slice op feature]: current support one dim, support decrease_axis'); |
| 30 | + } |
| 31 | + const { |
| 32 | + width_shape, |
| 33 | + height_shape, |
| 34 | + channel, |
| 35 | + total_shape, |
| 36 | + length_unformatted_shape |
| 37 | + } = origin; |
| 38 | + const batch = total_shape / (width_shape * height_shape * channel); |
| 39 | + const tensor_shape = [batch, channel, height_shape, width_shape]; |
| 40 | + |
| 41 | + let axis = axes[0]; |
| 42 | + |
| 43 | + if (axis < 0) { |
| 44 | + axis = axis + length_unformatted_shape + 1; |
| 45 | + } |
| 46 | + axis = 4 - length_unformatted_shape + axis; |
| 47 | + |
| 48 | + if (axis !== 4) { |
| 49 | + throw Error('[slice op feature]: unsupport axis value'); |
| 50 | + } |
| 51 | + |
| 52 | + const start = starts[0]; |
| 53 | + const end = ends[0]; |
| 54 | + |
| 55 | + const [ |
| 56 | + batch_num, |
| 57 | + channel_num, |
| 58 | + height_num, |
| 59 | + width_num |
| 60 | + ] = tensor_shape; |
| 61 | + |
| 62 | + // 计算 output tensor value 对应的 origin index |
| 63 | + const res_pos = []; |
| 64 | + for (let index = start; index < end; index++) { |
| 65 | + for (let batch = 0; batch < batch_num; batch++) { |
| 66 | + for (let channel = 0; channel < channel_num; channel++) { |
| 67 | + for (let height = 0; height < height_num; height++) { |
| 68 | + res_pos.push( |
| 69 | + batch * channel_num * height_num * width_num |
| 70 | + + channel * height_num * width_num |
| 71 | + + height * width_num + index |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const glslIndexArr = initializeGLSLArr(res_pos, ArrTypeEnum.INT_TYPE); |
| 79 | + |
| 80 | + const ifConditions = res_pos.reduce((acc, _, idx) => { |
| 81 | + const ifCondition = idx === 0 |
| 82 | + ? ` |
| 83 | + int index = 0; |
| 84 | + if (sumVal == ${idx}) { |
| 85 | + index = arr[${idx}]; |
| 86 | + }` |
| 87 | + : ` |
| 88 | + else if (sumVal == ${idx}) { |
| 89 | + index = arr[${idx}]; |
| 90 | + } |
| 91 | + `; |
| 92 | + return acc + ifCondition; |
| 93 | + }, ''); |
| 94 | + |
| 95 | + const getValueFromArrIndex = env.get('webglVersion') === 2 |
| 96 | + ? 'int index = arr[sumVal];' |
| 97 | + : ifConditions; |
| 98 | + return ` |
| 99 | + void main(void) { |
| 100 | + ivec4 oPos = getOutputTensorPos(); |
| 101 | + ${glslIndexArr} |
| 102 | +
|
| 103 | + // 输出坐标转换为输入坐标 |
| 104 | + int sumVal = oPos.a |
| 105 | + + oPos.b * ${out.width_shape} |
| 106 | + + oPos.g * ${out.height_shape} * ${out.width_shape} |
| 107 | + + oPos.r * ${out.channel} * ${out.width_shape} * ${out.height_shape}; |
| 108 | + |
| 109 | + ${getValueFromArrIndex} |
| 110 | +
|
| 111 | + float res = 0.0; |
| 112 | + ivec4 co = getTensorPosFromArrayIndex_origin(index); |
| 113 | + res = getValueFromTensorPos_origin(co.r, co.g, co.b, co.a); |
| 114 | + setOutput(float(res)); |
| 115 | + } |
| 116 | + `; |
| 117 | +} |
| 118 | +export default { |
| 119 | + mainFunc, |
| 120 | + textureFuncConf: { |
| 121 | + origin: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex'] |
| 122 | + } |
| 123 | +}; |
0 commit comments