From e8e4d8841edfe5a19a0cbad0b3a2a33f1fb4f091 Mon Sep 17 00:00:00 2001 From: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Date: Mon, 4 Apr 2022 16:28:52 -0700 Subject: [PATCH] other f32 unary operators --- .../backends/webgpu/op-resolve-rules.ts | 13 +++--- .../onnxjs/backends/webgpu/ops/unary-op.ts | 40 ++++++++++++------- js/web/test/suite-test-list.jsonc | 32 +++++++-------- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/js/web/lib/onnxjs/backends/webgpu/op-resolve-rules.ts b/js/web/lib/onnxjs/backends/webgpu/op-resolve-rules.ts index 4717b4944c019..67db9375b101d 100644 --- a/js/web/lib/onnxjs/backends/webgpu/op-resolve-rules.ts +++ b/js/web/lib/onnxjs/backends/webgpu/op-resolve-rules.ts @@ -40,7 +40,7 @@ export const WEBGPU_OP_RESOLVE_RULES: readonly OpSet.ResolveRule[] = [ // ['InstanceNormalization', '', '6+', instanceNormalization, parseInstanceNormalizationAttributes], ['LeakyRelu', '', '6+', unaryOps.leakyRelu, unaryOps.parseLeakyReluAttributes], // ['Less', '', '7+', binaryOps.less], - // ['Log', '', '6+', unaryOps.log], + ['Log', '', '6+', unaryOps.log], // ['MatMul', '', '1+', matMul, parseMatMulAttributes], // // TODO: support new attributes for MaxPool-8 and MaxPool-10 // ['MaxPool', '', '1+', maxPool, parseMaxPoolAttributes], @@ -59,13 +59,11 @@ export const WEBGPU_OP_RESOLVE_RULES: readonly OpSet.ResolveRule[] = [ // ['ReduceProd', '', '1+', reduceProd, parseReduceAttributes], // ['ReduceSum', '', '1-12', reduceSum, parseReduceAttributes], // ['ReduceSumSquare', '', '1+', reduceLogSumSquare, parseReduceAttributes], - // ['Relu', '', '6+', unaryOps.relu], - ['Reshape', '', '5+', reshape], + ['Relu', '', '6+', unaryOps.relu], ['Reshape', '', '5+', reshape], // ['Resize', '', '10', resize, parseResizeAttributesV10], // ['Resize', '', '11+', resize, parseResizeAttributesV11], // ['Shape', '', '1+', shape], - // ['Sigmoid', '', '6+', unaryOps.sigmoid], - // ['Sin', '', '7+', unaryOps.sin], + ['Sigmoid', '', '6+', unaryOps.sigmoid], ['Sin', '', '7+', unaryOps.sin], // ['Slice', '', '10+', sliceV10], // TODO: support 'steps' for Slice-10 // ['Slice', '', '1-9', slice, parseSliceAttributes], // // The "semantic" meaning of axis has changed in opset-13. @@ -76,13 +74,12 @@ export const WEBGPU_OP_RESOLVE_RULES: readonly OpSet.ResolveRule[] = [ // // When the attribute is missing, we need the count of number of outputs // // so that we can determine the 'split' attribute from the runtime input to the Operator // ['Split', '', '2-12', split, parseSplitAttributes], - // ['Sqrt', '', '6+', unaryOps.sqrt], + ['Sqrt', '', '6+', unaryOps.sqrt], // ['Squeeze', '', '1-12', squeeze, parseSqueezeAttributes], // ['Squeeze', '', '13+', squeezeV13], // ['Sub', '', '7+', binaryOps.sub], // ['Sum', '', '6+', sum], - // ['Tan', '', '7+', unaryOps.tan], - // ['Tanh', '', '6+', unaryOps.tanh], + ['Tan', '', '7+', unaryOps.tan], ['Tanh', '', '6+', unaryOps.tanh], // ['Tile', '', '6+', tile], // ['Transpose', '', '1+', transpose, parseTransposeAttributes], // ['Upsample', '', '7-8', upsample, parseUpsampleAttributesV7], diff --git a/js/web/lib/onnxjs/backends/webgpu/ops/unary-op.ts b/js/web/lib/onnxjs/backends/webgpu/ops/unary-op.ts index 1a8b738125554..fd053bc80be89 100644 --- a/js/web/lib/onnxjs/backends/webgpu/ops/unary-op.ts +++ b/js/web/lib/onnxjs/backends/webgpu/ops/unary-op.ts @@ -157,8 +157,8 @@ export const leakyRelu = async(handler: WebGpuInferenceHandler, inputs: Tensor[] export const parseLeakyReluAttributes = (node: Graph.Node): LeakyReluAttributes => createAttributeWithCacheKey({alpha: node.attributes.getFloat('alpha', 0.01)}); -// export const log = (handler: WebGLInferenceHandler, inputs: Tensor[]): -// Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslLog()), inputs)]; +export const log = async(handler: WebGpuInferenceHandler, inputs: Tensor[]): Promise => + handler.run(createElementwiseProgramInfoLoader(inputs[0], 'log'), inputs); // export const neg = (handler: WebGLInferenceHandler, inputs: Tensor[]): // Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslNeg()), inputs)]; @@ -166,20 +166,32 @@ export const parseLeakyReluAttributes = (node: Graph.Node): LeakyReluAttributes // export const not = (handler: WebGLInferenceHandler, inputs: Tensor[]): // Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslNot()), inputs)]; -// export const relu = (handler: WebGLInferenceHandler, inputs: Tensor[]): -// Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslRelu()), inputs)]; +export const relu = async(handler: WebGpuInferenceHandler, inputs: Tensor[]): Promise=>handler.run( + createElementwiseProgramInfoLoader(inputs[0], 'relu', ` + let relu_zero_: vec4 = vec4(0.0, 0.0, 0.0, 0.0); -// export const sigmoid = (handler: WebGLInferenceHandler, inputs: Tensor[]): -// Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslSigmoid()), inputs)]; + fn relu(v: vec4) -> vec4 { + return max( v, relu_zero_ ); + }`), + inputs); -// export const sin = (handler: WebGLInferenceHandler, inputs: Tensor[]): -// Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslSin()), inputs)]; +export const sigmoid = async(handler: WebGpuInferenceHandler, inputs: Tensor[]): Promise=>handler.run( + createElementwiseProgramInfoLoader(inputs[0], 'sigmoid', ` + let sigmoid_one_: vec4 = vec4(1.0, 1.0, 1.0, 1.0); -// export const sqrt = (handler: WebGLInferenceHandler, inputs: Tensor[]): -// Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslSqrt()), inputs)]; + fn sigmoid(v: vec4) -> vec4 { + return sigmoid_one_ / (sigmoid_one_ + exp(-v)); + }`), + inputs); -// export const tan = (handler: WebGLInferenceHandler, inputs: Tensor[]): -// Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslTan()), inputs)]; +export const sin = async(handler: WebGpuInferenceHandler, inputs: Tensor[]): Promise => + handler.run(createElementwiseProgramInfoLoader(inputs[0], 'sin'), inputs); -// export const tanh = (handler: WebGLInferenceHandler, inputs: Tensor[]): -// Tensor[] => [handler.run(createElementwiseProgramInfoLoader(handler, inputs[0], glslTanh()), inputs)]; +export const sqrt = async(handler: WebGpuInferenceHandler, inputs: Tensor[]): Promise => + handler.run(createElementwiseProgramInfoLoader(inputs[0], 'sqrt'), inputs); + +export const tan = async(handler: WebGpuInferenceHandler, inputs: Tensor[]): Promise => + handler.run(createElementwiseProgramInfoLoader(inputs[0], 'tan'), inputs); + +export const tanh = async(handler: WebGpuInferenceHandler, inputs: Tensor[]): Promise => + handler.run(createElementwiseProgramInfoLoader(inputs[0], 'tanh'), inputs); diff --git a/js/web/test/suite-test-list.jsonc b/js/web/test/suite-test-list.jsonc index efd6b00d6f300..b512e4a4b052d 100644 --- a/js/web/test/suite-test-list.jsonc +++ b/js/web/test/suite-test-list.jsonc @@ -374,7 +374,7 @@ // "test_identity", "test_leakyrelu_default", "test_leakyrelu_example", - "test_leakyrelu" + "test_leakyrelu", // "test_lrn_default", <-- failing due to low precison. If absolute CPU error threshold is increased from 1e-4 to 1e-2 (100x increase), it passes the test. // "test_lrn", <-- failing due to low precison. If absolute CPU error threshold is increased from 1e-4 to 1e-3 (10x increase), it passes the test. // "test_matmul_2d", @@ -405,16 +405,16 @@ // "test_or_bcast4v4d", // "test_prelu_broadcast", // "test_prelu_example", - // "test_relu", + "test_relu", // "test_reshape_extended_dims", // "test_reshape_negative_dim", // "test_reshape_one_dim", // "test_reshape_reduced_dims", // "test_reshape_reordered_dims", - // "test_sigmoid", - // "test_sigmoid_example", - // "test_sin_example", - // "test_sin", + "test_sigmoid", + "test_sigmoid_example", + "test_sin_example", + "test_sin", // "test_softmax_axis_0", // "test_softmax_axis_1", // "test_softmax_axis_2", @@ -484,10 +484,10 @@ // "v{7,8,9}/test_slice_neg", // "test_slice_start_out_of_bounds", // tensor shape of 0 // "test_squeeze", - // "test_tan_example", - // "test_tan", - // "test_tanh_example", - // "test_tanh", + "test_tan_example", + "test_tan", + "test_tanh_example", + "test_tanh" // "test_tile", // "test_tile_precomputed", // "test_transpose_all_permutations_0", @@ -528,26 +528,26 @@ ////"identity.jsonc", //"image-scaler.jsonc", //"less.jsonc", - //"log.jsonc", + "log.jsonc", //"matmul.jsonc", //"mul.jsonc", //"neg.jsonc", //"not.jsonc", //"or.jsonc", - "leaky-relu.jsonc" + "leaky-relu.jsonc", //"reduce-min.jsonc", - //"relu.jsonc", + "relu.jsonc", //"pad.jsonc", //"pad-big.jsonc", //"pow.jsonc", //"pow-big-number.jsonc", //"reshape.jsonc", //"softmax.jsonc", - //"sin.jsonc", + "sin.jsonc", //"split.jsonc", - //"sqrt.jsonc", + "sqrt.jsonc", //"sub.jsonc", - //"tan.jsonc", + "tan.jsonc" //"transpose.jsonc", //"xor.jsonc" ]