Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions backends/webgpu/runtime/WebGPUShaderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include <executorch/backends/webgpu/runtime/ops/avg_pool2d/avg_pool2d_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/batch_norm/batch_norm_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_div_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_floor_divide_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_minimum_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_mul_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_pow_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_sub_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/bitwise_not/bitwise_not_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/bmm/bmm_tiled_wgsl.h>
Expand Down Expand Up @@ -50,7 +54,6 @@
#include <executorch/backends/webgpu/runtime/ops/extrema/amin_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/fill/fill_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/flip/flip_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/floor_divide/binary_floor_divide_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/fused_ce/fused_ce_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/gather/gather_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/gelu/gelu_wgsl.h>
Expand All @@ -65,20 +68,17 @@
#include <executorch/backends/webgpu/runtime/ops/linear_dq8ca_q4gsw/linear_dq8ca_q4gsw_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/linear_q8ta_q8csw/linear_q8ta_q8csw_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/linear_qcs4w/qcs4w_linear_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/logical_and/logical_and_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/logical_or/logical_or_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/logical_binary/logical_and_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/logical_binary/logical_or_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/max_pool2d/max_pool2d_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/minimum/binary_minimum_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/mm/mm_tiled_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/mm/mm_vec4_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/mm/mm_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/mul/binary_mul_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/mul/silu_mul_fused_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/native_group_norm/group_norm_reduce_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/native_group_norm/group_norm_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/permute/permute_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/pixel_shuffle/pixel_shuffle_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/pow/binary_pow_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/q8ta_add/q8ta_add_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/q8ta_conv2d/q8ta_conv2d_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/q8ta_conv2d_dw/q8ta_conv2d_dw_wgsl.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace executorch::backends::webgpu {

// @generated from binary_floor_divide.wgsl - DO NOT EDIT.
// @generated from binary_op.wgsl - DO NOT EDIT.
// wgsl-sha256: baf71d277da79389315a6b96b439e7f0a55842e8288283f2af121f84536b3af3
inline constexpr const char* kBinaryFloorDivideWGSL = R"(
@group(0) @binding(0) var<storage, read> input1: array<f32>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace executorch::backends::webgpu {

// @generated from binary_minimum.wgsl - DO NOT EDIT.
// @generated from binary_op.wgsl - DO NOT EDIT.
// wgsl-sha256: 929b7ba85936e3652baea9f4e5e7f049d232c7ae7a74814a536b4c2674897972
inline constexpr const char* kBinaryMinimumWGSL = R"(
@group(0) @binding(0) var<storage, read> input1: array<f32>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace executorch::backends::webgpu {

// @generated from binary_mul.wgsl - DO NOT EDIT.
// @generated from binary_op.wgsl - DO NOT EDIT.
// wgsl-sha256: d248c0f1856b57115a5001a47f4936caa564dd3b787c02ceba504a13ab987812
inline constexpr const char* kBinaryMulWGSL = R"(
@group(0) @binding(0) var<storage, read> input1: array<f32>;
Expand Down
25 changes: 19 additions & 6 deletions backends/webgpu/runtime/ops/binary_op/binary_op.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ override wg_size: u32 = 64u;
$if USE_ALPHA:
override alpha: f32 = 1.0;

fn op(a: f32, b: f32) -> f32 {
return ${OP_EXPR};
}
$if INLINE:
@compute @workgroup_size(wg_size, 1, 1)
$else:
fn op(a: f32, b: f32) -> f32 {
return ${OP_EXPR};
}

@compute @workgroup_size(wg_size, 1, 1)
@compute @workgroup_size(wg_size, 1, 1)
fn main(
@builtin(global_invocation_id) gid: vec3<u32>,
@builtin(num_workgroups) num_workgroups: vec3<u32>) {
Expand All @@ -30,6 +33,8 @@ fn main(
return;
}

$if INLINE:
// Fast path: every input dim matches the output dim -> elementwise.
var same = true;
for (var d: u32 = 0u; d < out_meta.ndim; d = d + 1u) {
if (in1_meta.sizes[d >> 2u][d & 3u] != out_meta.sizes[d >> 2u][d & 3u] ||
Expand All @@ -38,10 +43,15 @@ fn main(
}
}
if (same) {
output[idx] = op(input1[idx], input2[idx]);
$if INLINE:
output[idx] = ${SAME_EXPR};
$else:
output[idx] = op(input1[idx], input2[idx]);
return;
}

$if INLINE:
// Broadcast: out idx -> per-input coord (clamp size-1 dims), relinearize.
var rem = idx;
var l1: u32 = 0u;
var l2: u32 = 0u;
Expand All @@ -51,5 +61,8 @@ fn main(
l1 = l1 + min(coord, in1_meta.sizes[d >> 2u][d & 3u] - 1u) * in1_meta.strides[d >> 2u][d & 3u];
l2 = l2 + min(coord, in2_meta.sizes[d >> 2u][d & 3u] - 1u) * in2_meta.strides[d >> 2u][d & 3u];
}
output[idx] = op(input1[l1], input2[l2]);
$if INLINE:
output[idx] = ${BROADCAST_EXPR};
$else:
output[idx] = op(input1[l1], input2[l2]);
}
23 changes: 23 additions & 0 deletions backends/webgpu/runtime/ops/binary_op/binary_op.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@ binary_op:
parameter_names_with_default_values:
OP_EXPR: a + alpha * b
USE_ALPHA: 1
INLINE: 0
SAME_EXPR: input1[idx] + input2[idx]
BROADCAST_EXPR: input1[l1] + input2[l2]
shader_variants:
- NAME: binary_div
OP_EXPR: a / b
USE_ALPHA: 0
- NAME: binary_sub
OP_EXPR: a - alpha * b
USE_ALPHA: 1
- NAME: binary_minimum
USE_ALPHA: 0
INLINE: 1
SAME_EXPR: min(input1[idx], input2[idx])
BROADCAST_EXPR: min(input1[l1], input2[l2])
- NAME: binary_pow
USE_ALPHA: 0
INLINE: 1
SAME_EXPR: pow(input1[idx], input2[idx])
BROADCAST_EXPR: pow(input1[l1], input2[l2])
- NAME: binary_floor_divide
USE_ALPHA: 0
INLINE: 1
SAME_EXPR: floor(input1[idx] / input2[idx])
BROADCAST_EXPR: floor(input1[l1] / input2[l2])
- NAME: binary_mul
USE_ALPHA: 0
INLINE: 1
SAME_EXPR: input1[idx] * input2[idx]
BROADCAST_EXPR: input1[l1] * input2[l2]
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace executorch::backends::webgpu {

// @generated from binary_pow.wgsl - DO NOT EDIT.
// @generated from binary_op.wgsl - DO NOT EDIT.
// wgsl-sha256: a88c161bd3f43d21a72ebd8ca6f8611b6b9b854e3572a8e6b820602091bc464c
inline constexpr const char* kBinaryPowWGSL = R"(
@group(0) @binding(0) var<storage, read> input1: array<f32>;
Expand Down
2 changes: 1 addition & 1 deletion backends/webgpu/runtime/ops/floor_divide/BinaryOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <executorch/backends/webgpu/runtime/WebGPUGraph.h>
#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h>
#include <executorch/backends/webgpu/runtime/ops/binary/BinaryOp.h>
#include <executorch/backends/webgpu/runtime/ops/floor_divide/binary_floor_divide_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_floor_divide_wgsl.h>

#include <stdexcept>
#include <vector>
Expand Down
51 changes: 0 additions & 51 deletions backends/webgpu/runtime/ops/floor_divide/binary_floor_divide.wgsl

This file was deleted.

2 changes: 1 addition & 1 deletion backends/webgpu/runtime/ops/logical_and/LogicalAnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <executorch/backends/webgpu/runtime/WebGPUGraph.h>
#include <executorch/backends/webgpu/runtime/WebGPUUtils.h>
#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h>
#include <executorch/backends/webgpu/runtime/ops/logical_and/logical_and_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/logical_binary/logical_and_wgsl.h>

#include <webgpu/webgpu.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace executorch::backends::webgpu {

// @generated from logical_and.wgsl - DO NOT EDIT.
// @generated from logical_binary.wgsl - DO NOT EDIT.
// wgsl-sha256: cf7c1d1dbba94e429120796c9c25a6717786cca03c08f3bd1e291d5627089c20
inline constexpr const char* kLogicalAndWGSL = R"(
@group(0) @binding(0) var<storage, read_write> t_out: array<u32>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ override wg_size: u32 = 64u;
fn main(
@builtin(global_invocation_id) gid: vec3<u32>,
@builtin(num_workgroups) num_workgroups: vec3<u32>) {
// bool packed 4/word; canonical 0/1 bytes -> word-wise AND == per-byte AND.
$if OP == "&":
// bool packed 4/word; canonical 0/1 bytes -> word-wise AND == per-byte AND.
$else:
// bool packed 4/word; canonical 0/1 bytes -> word-wise OR == per-byte OR.
let w = gid.x + gid.y * (num_workgroups.x * wg_size);
if (w >= params.num_words) {
return;
}
t_out[w] = t_a[w] & t_b[w];
t_out[w] = t_a[w] ${OP} t_b[w];
}
13 changes: 13 additions & 0 deletions backends/webgpu/runtime/ops/logical_binary/logical_binary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

logical_binary:
parameter_names_with_default_values:
OP: "&"
shader_variants:
- NAME: logical_and
- NAME: logical_or
OP: "|"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace executorch::backends::webgpu {

// @generated from logical_or.wgsl - DO NOT EDIT.
// @generated from logical_binary.wgsl - DO NOT EDIT.
// wgsl-sha256: 4ad19ee04e2c7b396b4669cf44f95133d658c3ec2e6f37d7b271bedc0e582ecf
inline constexpr const char* kLogicalOrWGSL = R"(
@group(0) @binding(0) var<storage, read_write> t_out: array<u32>;
Expand Down
2 changes: 1 addition & 1 deletion backends/webgpu/runtime/ops/logical_or/LogicalOr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <executorch/backends/webgpu/runtime/WebGPUGraph.h>
#include <executorch/backends/webgpu/runtime/WebGPUUtils.h>
#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h>
#include <executorch/backends/webgpu/runtime/ops/logical_or/logical_or_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/logical_binary/logical_or_wgsl.h>

#include <webgpu/webgpu.h>

Expand Down
25 changes: 0 additions & 25 deletions backends/webgpu/runtime/ops/logical_or/logical_or.wgsl

This file was deleted.

2 changes: 1 addition & 1 deletion backends/webgpu/runtime/ops/minimum/BinaryOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h>
#include <executorch/backends/webgpu/runtime/ops/binary/BinaryOp.h>
#include <executorch/backends/webgpu/runtime/ops/minimum/binary_minimum_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_minimum_wgsl.h>

#include <vector>

Expand Down
51 changes: 0 additions & 51 deletions backends/webgpu/runtime/ops/minimum/binary_minimum.wgsl

This file was deleted.

2 changes: 1 addition & 1 deletion backends/webgpu/runtime/ops/mul/BinaryOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <executorch/backends/webgpu/runtime/WebGPUUtils.h>
#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h>
#include <executorch/backends/webgpu/runtime/ops/TensorMeta.h>
#include <executorch/backends/webgpu/runtime/ops/mul/binary_mul_wgsl.h>
#include <executorch/backends/webgpu/runtime/ops/binary_op/binary_mul_wgsl.h>

#include <webgpu/webgpu.h>

Expand Down
Loading
Loading