Commit 140a27987 for llama.cpp

commit 140a27987441a4d78804fff1183b6365b0ec8197
Author: Nikhil Jain <nikhil.jain0987@gmail.com>
Date:   Tue Nov 17 00:41:37 2026 -0800

    webgpu : Implement timestep embedding (#31695)

    * Only run webgpu CI on my fork

    * Add webgpu only workflow

    * resolve merge conflicts

    * save state

    * save kernel

    * add kernel back in

    * fix merge issues with kernels

    * updates

    * update tests

    * change invocation count and workgroup to be u32

    * clang format

    * clang format

    * update docs

    * format

    * remove extra file

diff --git a/docs/ops.md b/docs/ops.md
index 9a73a097f..7cd9bc2f8 100644
--- a/docs/ops.md
+++ b/docs/ops.md
@@ -127,7 +127,7 @@ Legend:
 |                     SWIGLU_CLAMP | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
 |                       SWIGLU_OAI | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
 |                             TANH | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
-|               TIMESTEP_EMBEDDING | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
+|               TIMESTEP_EMBEDDING | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
 |                            TOP_K | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
 |                       TOP_K_MASK | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
 |                              TRI | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
diff --git a/docs/ops/WebGPU.csv b/docs/ops/WebGPU.csv
index 3b874fd68..2ae3a1d36 100644
--- a/docs/ops/WebGPU.csv
+++ b/docs/ops/WebGPU.csv
@@ -12351,7 +12351,7 @@
 "WebGPU: WebGPU","ARANGE","type=f32,start=-1.000000,stop=255.000000,step=0.250000","support","1","yes","WebGPU"
 "WebGPU: WebGPU","ARANGE","type=f32,start=-1.000000,stop=255.250000,step=0.250000","support","1","yes","WebGPU"
 "WebGPU: WebGPU","ARANGE","type=f32,start=0.000000,stop=1048576.000000,step=1.000000","support","1","yes","WebGPU"
-"WebGPU: WebGPU","TIMESTEP_EMBEDDING","type=f32,ne_a=[2,1,1,1],dim=320,max_period=10000","support","0","no","WebGPU"
+"WebGPU: WebGPU","TIMESTEP_EMBEDDING","type=f32,ne_a=[2,1,1,1],dim=320,max_period=10000","support","1","yes","WebGPU"
 "WebGPU: WebGPU","LEAKY_RELU","type=f32,ne_a=[10,5,4,3],negative_slope=0.100000","support","0","no","WebGPU"
 "WebGPU: WebGPU","CUMSUM","type=f32,ne=[10,5,4,3]","support","1","yes","WebGPU"
 "WebGPU: WebGPU","CUMSUM","type=f32,ne=[127,5,4,3]","support","1","yes","WebGPU"
diff --git a/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp b/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp
index db612da23..6a57e9f3d 100644
--- a/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp
+++ b/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp
@@ -1264,15 +1264,16 @@ class ggml_webgpu_shader_lib {
     wgpu::Device           device;
     pre_wgsl::Preprocessor preprocessor;

-    std::unordered_map<int, webgpu_pipeline> sum_rows_pipelines;       // key is whether to compute the mean
-    std::unordered_map<int, webgpu_pipeline> argmax_pipelines;         // key is vec4
-    std::unordered_map<int, webgpu_pipeline> argsort_pipelines;        // key is order
-    std::unordered_map<int, webgpu_pipeline> argsort_merge_pipelines;  // key is order
-    std::unordered_map<int, webgpu_pipeline> cumsum_pipelines;         // key is fixed, no variants yet
-    std::unordered_map<int, webgpu_pipeline> arange_pipelines;         // key is fixed, no variants yet
-    std::unordered_map<int, webgpu_pipeline> count_equal_pipelines;    // key is src_overlap
+    std::unordered_map<int, webgpu_pipeline> sum_rows_pipelines;            // key is whether to compute the mean
+    std::unordered_map<int, webgpu_pipeline> argmax_pipelines;              // key is vec4
+    std::unordered_map<int, webgpu_pipeline> argsort_pipelines;             // key is order
+    std::unordered_map<int, webgpu_pipeline> argsort_merge_pipelines;       // key is order
+    std::unordered_map<int, webgpu_pipeline> cumsum_pipelines;              // key is fixed, no variants yet
+    std::unordered_map<int, webgpu_pipeline> arange_pipelines;              // key is fixed, no variants yet
+    std::unordered_map<int, webgpu_pipeline> count_equal_pipelines;         // key is src_overlap
+    std::unordered_map<int, webgpu_pipeline> timestep_embedding_pipelines;  // key is fixed, no variants yet
     std::unordered_map<ggml_webgpu_row_norm_pipeline_key, webgpu_pipeline, ggml_webgpu_row_norm_pipeline_key_hash>
-        row_norm_pipelines;                                            // op/inplace
+        row_norm_pipelines;                                                 // op/inplace

     std::unordered_map<ggml_webgpu_get_rows_pipeline_key, webgpu_pipeline, ggml_webgpu_get_rows_pipeline_key_hash>
         get_rows_pipelines;   // src_type, vectorized
@@ -1633,6 +1634,20 @@ class ggml_webgpu_shader_lib {
         return cumsum_pipelines[1];
     }

+    webgpu_pipeline get_timestep_embedding_pipeline(const ggml_webgpu_shader_lib_context & context) {
+        auto it = timestep_embedding_pipelines.find(1);
+        if (it != timestep_embedding_pipelines.end()) {
+            return it->second;
+        }
+
+        std::vector<std::string> defines;
+        defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size));
+
+        auto processed                  = preprocessor.preprocess(wgsl_timestep_embedding, defines);
+        timestep_embedding_pipelines[1] = ggml_webgpu_create_pipeline(device, processed, "timestep_embedding");
+        return timestep_embedding_pipelines[1];
+    }
+
     webgpu_pipeline get_argsort_pipeline(const ggml_webgpu_shader_lib_context & context) {
         bool          is_top_k = context.dst->op == GGML_OP_TOP_K;
         // ascending order is 0, descending order is 1
diff --git a/ggml/src/ggml-webgpu/ggml-webgpu.cpp b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
index a09b8a5e1..898c89399 100644
--- a/ggml/src/ggml-webgpu/ggml-webgpu.cpp
+++ b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
@@ -3363,6 +3363,44 @@ static webgpu_encoded_op ggml_webgpu_cumsum(webgpu_context & ctx, ggml_tensor *
     return ggml_backend_webgpu_build(ctx, pipeline, params, entries, wg_x, wg_y);
 }

+static webgpu_encoded_op ggml_webgpu_timestep_embedding(webgpu_context & ctx, ggml_tensor * src, ggml_tensor * dst) {
+    const int dim        = ggml_get_op_params_i32(dst, 0);
+    const int max_period = ggml_get_op_params_i32(dst, 1);
+    GGML_ASSERT(dim >= 1);
+    GGML_ASSERT(max_period > 0);
+
+    std::vector<uint32_t> params = {
+        (uint32_t) dim,
+        ggml_webgpu_u32_from_f32((float) max_period),
+        (uint32_t) src->ne[0],
+        (uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src) / ggml_type_size(src->type)),
+        (uint32_t) (ggml_webgpu_tensor_misalignment(ctx, dst) / ggml_type_size(dst->type)),
+    };
+
+    std::vector<wgpu::BindGroupEntry> entries = {
+        ggml_webgpu_make_tensor_bind_group_entry(ctx, 0, src),
+        ggml_webgpu_make_tensor_bind_group_entry(ctx, 1, dst),
+    };
+
+    ggml_webgpu_shader_lib_context shader_lib_ctx = {};
+    shader_lib_ctx.src0                           = src;
+    shader_lib_ctx.dst                            = dst;
+    shader_lib_ctx.max_wg_size = ctx->global_ctx->capabilities.limits.maxComputeInvocationsPerWorkgroup;
+
+    webgpu_pipeline pipeline = ctx->shader_lib->get_timestep_embedding_pipeline(shader_lib_ctx);
+
+    // Each thread calculates both sin and cos for a given frequency,
+    // so divide params.dim by 2.
+    const uint32_t invocation_count = (uint32_t) src->ne[0] * (dim == 1 ? 1 : dim / 2);
+    const uint32_t workgroups       = CEIL_DIV(invocation_count, shader_lib_ctx.max_wg_size);
+
+    uint32_t wg_x, wg_y;
+    compute_2d_workgroups(workgroups, ctx->global_ctx->capabilities.limits.maxComputeWorkgroupsPerDimension, wg_x,
+                          wg_y);
+
+    return ggml_backend_webgpu_build(ctx, pipeline, params, entries, wg_x, wg_y);
+}
+
 static webgpu_encoded_op ggml_webgpu_sum_rows(webgpu_context & ctx, ggml_tensor * src, ggml_tensor * dst) {
     bool                  total_sum = dst->op == GGML_OP_SUM;
     std::vector<uint32_t> params = { (uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src) / ggml_type_size(src->type)),
@@ -3656,6 +3694,8 @@ static std::optional<webgpu_encoded_op> ggml_webgpu_encode(webgpu_context ctx,
             return ggml_webgpu_im2col(ctx, src0, src1, node);
         case GGML_OP_UPSCALE:
             return ggml_webgpu_upscale(ctx, src0, node);
+        case GGML_OP_TIMESTEP_EMBEDDING:
+            return ggml_webgpu_timestep_embedding(ctx, src0, node);
         default:
             return std::nullopt;
     }
@@ -5198,6 +5238,9 @@ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const
         case GGML_OP_CUMSUM:
             supports_op = op->type == GGML_TYPE_F32 && src0->type == op->type;
             break;
+        case GGML_OP_TIMESTEP_EMBEDDING:
+            supports_op = src0->type == GGML_TYPE_F32 && ggml_is_contiguous(src0);
+            break;
         case GGML_OP_SUM:
             supports_op = op->type == GGML_TYPE_F32 && src0->type == op->type && ggml_is_contiguously_allocated(src0);
             break;
diff --git a/ggml/src/ggml-webgpu/wgsl-shaders/timestep_embedding.wgsl b/ggml/src/ggml-webgpu/wgsl-shaders/timestep_embedding.wgsl
new file mode 100644
index 000000000..d9a4b3b2e
--- /dev/null
+++ b/ggml/src/ggml-webgpu/wgsl-shaders/timestep_embedding.wgsl
@@ -0,0 +1,48 @@
+struct Params {
+    dim: u32,
+    max_period: f32,
+    num_timesteps: u32,
+    input_offset: u32,
+    output_offset: u32,
+};
+
+@group(0) @binding(0)
+var<storage, read_write> timesteps: array<f32>;
+
+@group(0) @binding(2)
+var<uniform> params: Params;
+
+@group(0) @binding(1)
+var<storage, read_write> output: array<f32>;
+
+@compute @workgroup_size(WG_SIZE)
+fn main(
+    @builtin(global_invocation_id) gid: vec3<u32>,
+    @builtin(num_workgroups) num_wg: vec3<u32>
+) {
+    let global_thread_index = gid.x + gid.y * num_wg.x * u32(WG_SIZE);
+
+    let H: u32 = params.dim / 2;
+
+    if (params.dim == 1 && global_thread_index < params.num_timesteps) {
+        output[params.output_offset + global_thread_index] = 0.0;
+    }
+
+    if (global_thread_index >= params.num_timesteps * H) { return; }
+
+    let timestep_index: u32 = global_thread_index / H;
+    let frequency_index: u32 = global_thread_index % H;
+
+    let frequency: f32 = exp(-log(params.max_period) * f32(frequency_index) / f32(H));
+    let angle: f32 = timesteps[params.input_offset + timestep_index] * frequency;
+
+    output[params.output_offset + timestep_index * params.dim + frequency_index] = cos(angle);
+    output[params.output_offset + timestep_index * params.dim + frequency_index + H] = sin(angle);
+
+    // For odd dims, we zero out the last output via the first thread of each timestep block,
+    // since we expect cos(x) sin(x) pairs for each frequency.
+    if (params.dim % 2 == 1 && frequency_index == 0) {
+        output[params.output_offset + timestep_index * params.dim + (params.dim - 1)] = 0.0;
+    }
+
+}
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 74aac916e..5df6d14e8 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -12880,9 +12880,12 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
     }
     test_cases.emplace_back(new test_arange(GGML_TYPE_F32, 0.0f, 1048576.0f, 1.0f));
     test_cases.emplace_back(new test_timestep_embedding());
-    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, { 1, 1, 1, 1 }, 256, 10000));
-    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, { 21, 1, 1, 1 }, 256, 10000));
-    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, { 31, 1, 1, 1 }, 256, 10000));
+    // Exceeds 65535 workgroups even with 1024 invocations per workgroup.
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {32769, 1, 1, 1}, 4096, 10000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 1, 10000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 2, 10000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 7, 1000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 256, 10000));
     test_cases.emplace_back(new test_leaky_relu());

     test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 10, 5, 4, 3 }));
@@ -13528,6 +13531,12 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
 static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
     std::vector<std::unique_ptr<test_case>> test_cases;

+    test_cases.emplace_back(new test_timestep_embedding());
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {32769, 1, 1, 1}, 4096, 10000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 1, 10000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 2, 10000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 7, 1000));
+    test_cases.emplace_back(new test_timestep_embedding(GGML_TYPE_F32, {65, 1, 1, 1}, 256, 10000));
     for (float n : {128.0f, 4096.0f, 32768.0f, 1048576.0f}) {
         test_cases.emplace_back(new test_arange(GGML_TYPE_F32, 0.0f, n, 1.0f));
     }