// Copyright 2026 Tencent
// SPDX-License-Identifier: BSD-3-Clause

#include "groupnorm_vulkan.h"

#include "layer_shader_type.h"

namespace ncnn {

GroupNorm_vulkan::GroupNorm_vulkan()
{
    support_vulkan = true;
    support_vulkan_packing = true;

    pipeline_groupnorm_reduce_sum4_fp16_to_fp32 = 0;
    pipeline_groupnorm_reduce_sum4_fp32[0] = 0;
    pipeline_groupnorm_reduce_sum4_fp32[1] = 0;
    pipeline_groupnorm_reduce_mean = 0;
    pipeline_groupnorm_sub_mean_square = 0;
    pipeline_groupnorm_coeffs = 0;
    pipeline_groupnorm_norm = 0;

    pipeline_groupnorm_reduce_sum4_fp16_to_fp32_pack4 = 0;
    pipeline_groupnorm_reduce_sum4_fp32_pack4[0] = 0;
    pipeline_groupnorm_reduce_sum4_fp32_pack4[1] = 0;
    pipeline_groupnorm_reduce_mean_pack4 = 0;
    pipeline_groupnorm_sub_mean_square_pack4 = 0;
    pipeline_groupnorm_coeffs_pack4 = 0;
    pipeline_groupnorm_norm_pack4 = 0;
}

int GroupNorm_vulkan::create_pipeline(const Option& opt)
{
    const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];

    // resolve total actual channels
    int _channels = channels;
    if (shape.dims == 1) _channels = shape.w * shape.elempack;
    if (shape.dims == 2) _channels = shape.h * shape.elempack;
    if (shape.dims == 3 || shape.dims == 4) _channels = shape.c * shape.elempack;

    int elempack = 1;
    if (_channels != 0) elempack = _channels % 4 == 0 ? 4 : 1;

    size_t elemsize;
    if (opt.use_fp16_storage || opt.use_fp16_packed || opt.use_bf16_storage || opt.use_bf16_packed)
    {
        elemsize = elempack * 2u;
    }
    else
    {
        elemsize = elempack * 4u;
    }

    int channels_g = _channels / group;

    // workspace for per-channel partial sums (after reduce_sum4 chain)
    Mat workspace_shape;
    if (_channels != 0) workspace_shape = Mat(1, 1, _channels / elempack, (void*)0, elemsize, elempack);

    // reduce_sum4 pipelines
    {
        Mat local_size_xyz;
        {
            local_size_xyz = Mat(16, 1, _channels ? std::min(4, _channels / elempack) : 4, (void*)0);
            if (workspace_shape.dims != 0)
            {
                local_size_xyz.w = 16;
                local_size_xyz.h = 1;
                local_size_xyz.c = std::min(4, workspace_shape.c);
            }
        }

        // pack1
        if (elempack == 1 || _channels == 0)
        {
            pipeline_groupnorm_reduce_sum4_fp16_to_fp32 = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_sum4_fp16_to_fp32->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_sum4_fp16_to_fp32->create(LayerShaderType::groupnorm_reduce_sum4_fp16_to_fp32, opt, std::vector<vk_specialization_type>());

            pipeline_groupnorm_reduce_sum4_fp32[0] = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_sum4_fp32[0]->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_sum4_fp32[0]->create(LayerShaderType::groupnorm_reduce_sum4_fp32, opt, std::vector<vk_specialization_type>());
            pipeline_groupnorm_reduce_sum4_fp32[1] = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_sum4_fp32[1]->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_sum4_fp32[1]->create(LayerShaderType::groupnorm_reduce_sum4_fp32, opt, std::vector<vk_specialization_type>());
        }

        // pack4
        if (elempack == 4 || _channels == 0)
        {
            pipeline_groupnorm_reduce_sum4_fp16_to_fp32_pack4 = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_sum4_fp16_to_fp32_pack4->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_sum4_fp16_to_fp32_pack4->create(LayerShaderType::groupnorm_reduce_sum4_fp16_to_fp32_pack4, opt, std::vector<vk_specialization_type>());

            pipeline_groupnorm_reduce_sum4_fp32_pack4[0] = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_sum4_fp32_pack4[0]->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_sum4_fp32_pack4[0]->create(LayerShaderType::groupnorm_reduce_sum4_fp32_pack4, opt, std::vector<vk_specialization_type>());
            pipeline_groupnorm_reduce_sum4_fp32_pack4[1] = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_sum4_fp32_pack4[1]->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_sum4_fp32_pack4[1]->create(LayerShaderType::groupnorm_reduce_sum4_fp32_pack4, opt, std::vector<vk_specialization_type>());
        }
    }

    // reduce_mean pipeline
    {
        std::vector<vk_specialization_type> specializations(0 + 4);
        specializations[0].i = 0; // w - unknown at pipeline creation
        specializations[1].i = 0; // h
        specializations[2].i = workspace_shape.c;
        specializations[3].i = 0; // cstep - unknown at pipeline creation

        Mat local_size_xyz(group ? std::min(64, group) : 64, 1, 1, (void*)0);

        if (elempack == 1 || _channels == 0)
        {
            pipeline_groupnorm_reduce_mean = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_mean->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_mean->create(LayerShaderType::groupnorm_reduce_mean, opt, specializations);
        }

        if (elempack == 4 || _channels == 0)
        {
            pipeline_groupnorm_reduce_mean_pack4 = new Pipeline(vkdev);
            pipeline_groupnorm_reduce_mean_pack4->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_reduce_mean_pack4->create(LayerShaderType::groupnorm_reduce_mean_pack4, opt, specializations);
        }
    }

    // sub_mean_square pipeline - use original shape like InstanceNorm, with h*d flattening for dims=4
    {
        // shape hints must match what forward_inplace sends as push constants
        int shape_dims = std::min(3, shape.dims);
        int shape_w = shape.w;
        int shape_h = shape.h * shape.d;
        int shape_c = 0;
        int shape_cstep = shape.cstep;
        if (shape.dims == 1 || shape.dims == 2)
        {
            // dims=1/2: VkMat c=1, cannot use shape directly
            // we will not set shape hints for dims=1/2 (leave as 0 to use push constants)
            shape_dims = 0;
            shape_w = 0;
            shape_h = 0;
            shape_cstep = 0;
        }
        else if (shape.dims == 3 || shape.dims == 4)
        {
            shape_c = shape.c * shape.elempack / elempack;
        }

        Mat square_workspace;
        if (shape_c != 0)
        {
            square_workspace = Mat(shape_w, shape_h, shape_c, (void*)0, elempack * 4u, elempack);
        }

        std::vector<vk_specialization_type> specializations(0 + 10);
        specializations[0 + 0].i = shape_dims;
        specializations[0 + 1].i = shape_w;
        specializations[0 + 2].i = shape_h;
        specializations[0 + 3].i = shape_c;
        specializations[0 + 4].i = shape_cstep;
        specializations[0 + 5].i = square_workspace.dims;
        specializations[0 + 6].i = square_workspace.w;
        specializations[0 + 7].i = square_workspace.h;
        specializations[0 + 8].i = square_workspace.c;
        specializations[0 + 9].i = square_workspace.cstep;

        Mat local_size_xyz(4, 4, _channels ? std::min(4, _channels / elempack) : 4, (void*)0);
        if (square_workspace.dims != 0)
        {
            local_size_xyz.w = std::min(4, square_workspace.w);
            local_size_xyz.h = std::min(4, square_workspace.h);
            local_size_xyz.c = std::min(4, square_workspace.c);
        }

        if (elempack == 1 || _channels == 0)
        {
            pipeline_groupnorm_sub_mean_square = new Pipeline(vkdev);
            pipeline_groupnorm_sub_mean_square->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_sub_mean_square->create(LayerShaderType::groupnorm_sub_mean_square, opt, specializations);
        }

        if (elempack == 4 || _channels == 0)
        {
            pipeline_groupnorm_sub_mean_square_pack4 = new Pipeline(vkdev);
            pipeline_groupnorm_sub_mean_square_pack4->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_sub_mean_square_pack4->create(LayerShaderType::groupnorm_sub_mean_square_pack4, opt, specializations);
        }
    }

    // coeffs pipeline
    {
        Mat local_size_xyz(_channels ? std::min(64, _channels / elempack) : 64, 1, 1, (void*)0);
        if (workspace_shape.dims != 0)
        {
            local_size_xyz.w = std::min(64, workspace_shape.c);
            local_size_xyz.h = 1;
            local_size_xyz.c = 1;
        }

        if (elempack == 1 || _channels == 0)
        {
            std::vector<vk_specialization_type> specializations(4);
            specializations[0].f = eps;
            specializations[1].i = affine;
            specializations[2].i = _channels;
            specializations[3].i = channels_g;

            pipeline_groupnorm_coeffs = new Pipeline(vkdev);
            pipeline_groupnorm_coeffs->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_coeffs->create(LayerShaderType::groupnorm_coeffs, opt, specializations);
        }

        if (elempack == 4 || _channels == 0)
        {
            std::vector<vk_specialization_type> specializations(4);
            specializations[0].f = eps;
            specializations[1].i = affine;
            specializations[2].i = _channels / 4;
            specializations[3].i = channels_g; // actual channels_g, not packed

            pipeline_groupnorm_coeffs_pack4 = new Pipeline(vkdev);
            pipeline_groupnorm_coeffs_pack4->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_coeffs_pack4->create(LayerShaderType::groupnorm_coeffs_pack4, opt, specializations);
        }
    }

    // norm pipeline - same shape hint approach as sub_mean_square
    {
        int shape_dims = std::min(3, shape.dims);
        int shape_w = shape.w;
        int shape_h = shape.h * shape.d;
        int shape_c = 0;
        int shape_cstep = shape.cstep;
        if (shape.dims == 1 || shape.dims == 2)
        {
            shape_dims = 0;
            shape_w = 0;
            shape_h = 0;
            shape_cstep = 0;
        }
        else if (shape.dims == 3 || shape.dims == 4)
        {
            shape_c = shape.c * shape.elempack / elempack;
        }

        std::vector<vk_specialization_type> specializations(0 + 5);
        specializations[0 + 0].i = shape_dims;
        specializations[0 + 1].i = shape_w;
        specializations[0 + 2].i = shape_h;
        specializations[0 + 3].i = shape_c;
        specializations[0 + 4].i = shape_cstep;

        Mat local_size_xyz(4, 4, _channels ? std::min(4, _channels / elempack) : 4, (void*)0);
        if (shape_dims != 0)
        {
            local_size_xyz.w = std::min(4, shape_w);
            local_size_xyz.h = std::min(4, shape_h);
            local_size_xyz.c = std::min(4, shape_c);
        }

        if (elempack == 1 || _channels == 0)
        {
            pipeline_groupnorm_norm = new Pipeline(vkdev);
            pipeline_groupnorm_norm->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_norm->create(LayerShaderType::groupnorm_norm, opt, specializations);
        }

        if (elempack == 4 || _channels == 0)
        {
            pipeline_groupnorm_norm_pack4 = new Pipeline(vkdev);
            pipeline_groupnorm_norm_pack4->set_optimal_local_size_xyz(local_size_xyz);
            pipeline_groupnorm_norm_pack4->create(LayerShaderType::groupnorm_norm_pack4, opt, specializations);
        }
    }

    return 0;
}

int GroupNorm_vulkan::destroy_pipeline(const Option& /*opt*/)
{
    delete pipeline_groupnorm_reduce_sum4_fp16_to_fp32;
    pipeline_groupnorm_reduce_sum4_fp16_to_fp32 = 0;

    delete pipeline_groupnorm_reduce_sum4_fp32[0];
    delete pipeline_groupnorm_reduce_sum4_fp32[1];
    pipeline_groupnorm_reduce_sum4_fp32[0] = 0;
    pipeline_groupnorm_reduce_sum4_fp32[1] = 0;

    delete pipeline_groupnorm_reduce_mean;
    pipeline_groupnorm_reduce_mean = 0;

    delete pipeline_groupnorm_sub_mean_square;
    pipeline_groupnorm_sub_mean_square = 0;

    delete pipeline_groupnorm_coeffs;
    pipeline_groupnorm_coeffs = 0;

    delete pipeline_groupnorm_norm;
    pipeline_groupnorm_norm = 0;

    delete pipeline_groupnorm_reduce_sum4_fp16_to_fp32_pack4;
    pipeline_groupnorm_reduce_sum4_fp16_to_fp32_pack4 = 0;

    delete pipeline_groupnorm_reduce_sum4_fp32_pack4[0];
    delete pipeline_groupnorm_reduce_sum4_fp32_pack4[1];
    pipeline_groupnorm_reduce_sum4_fp32_pack4[0] = 0;
    pipeline_groupnorm_reduce_sum4_fp32_pack4[1] = 0;

    delete pipeline_groupnorm_reduce_mean_pack4;
    pipeline_groupnorm_reduce_mean_pack4 = 0;

    delete pipeline_groupnorm_sub_mean_square_pack4;
    pipeline_groupnorm_sub_mean_square_pack4 = 0;

    delete pipeline_groupnorm_coeffs_pack4;
    pipeline_groupnorm_coeffs_pack4 = 0;

    delete pipeline_groupnorm_norm_pack4;
    pipeline_groupnorm_norm_pack4 = 0;

    return 0;
}

int GroupNorm_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
{
    if (affine == 0)
        return 0;

    cmd.record_upload(gamma_data, gamma_data_gpu, opt);

    cmd.record_upload(beta_data, beta_data_gpu, opt);

    return 0;
}

int GroupNorm_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
{
    int dims = bottom_top_blob.dims;
    int w = bottom_top_blob.w;
    int h = bottom_top_blob.h * bottom_top_blob.d;
    int c = bottom_top_blob.c;
    int size = w * h;
    size_t elemsize = bottom_top_blob.elemsize;
    int elempack = bottom_top_blob.elempack;
    int cstep = bottom_top_blob.cstep;

    // For dims=1/2, VkMat has c=1. Channels are stored in w (dims=1) or h (dims=2).
    // Remap to a virtual 3D view where channels are along c axis.
    int c_virtual = c;       // packed channel count for shaders
    int size_virtual = size; // spatial size per channel for area computation
    int cstep_virtual = cstep;
    if (dims == 1)
    {
        // VkMat: w=channels/elempack, h=1, c=1
        c_virtual = w;
        size_virtual = 1;
        cstep_virtual = 1;
    }
    else if (dims == 2)
    {
        // VkMat: w=spatial, h=channels/elempack, c=1
        c_virtual = h;
        size_virtual = w;
        cstep_virtual = w;
    }

    int channels_g = channels / group;

    // mean - one float per group
    VkMat mean_workspace(group, 4u, 1, opt.workspace_vkallocator);
    {
        // reduce sum per channel first
        VkMat sum_workspace;
        {
            int reduced_w = (size_virtual + 3) / 4;
            int reduced_c = c_virtual;

            sum_workspace.create(reduced_w, 1, reduced_c, 4u * elempack, elempack, opt.workspace_vkallocator);
            {
                std::vector<VkMat> bindings(2);
                bindings[0] = bottom_top_blob;
                bindings[1] = sum_workspace;

                std::vector<vk_constant_type> constants(8);
                constants[0].i = size_virtual;
                constants[1].i = 1;
                constants[2].i = c_virtual;
                constants[3].i = cstep_virtual;
                constants[4].i = sum_workspace.w;
                constants[5].i = 1;
                constants[6].i = sum_workspace.c;
                constants[7].i = sum_workspace.cstep;

                const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_reduce_sum4_fp16_to_fp32_pack4 : pipeline_groupnorm_reduce_sum4_fp16_to_fp32;

                cmd.record_pipeline(pipeline, bindings, constants, sum_workspace);
            }
        }

        int pb = 0;
        while (sum_workspace.w > 4)
        {
            int reduced_w = (sum_workspace.w + 3) / 4;
            int reduced_c = sum_workspace.c;

            VkMat sum_workspace_reduced;
            sum_workspace_reduced.create(reduced_w, 1, reduced_c, 4u * elempack, elempack, opt.workspace_vkallocator);

            {
                std::vector<VkMat> bindings(2);
                bindings[0] = sum_workspace;
                bindings[1] = sum_workspace_reduced;

                std::vector<vk_constant_type> constants(8);
                constants[0].i = sum_workspace.w;
                constants[1].i = 1;
                constants[2].i = sum_workspace.c;
                constants[3].i = sum_workspace.cstep;
                constants[4].i = sum_workspace_reduced.w;
                constants[5].i = 1;
                constants[6].i = sum_workspace_reduced.c;
                constants[7].i = sum_workspace_reduced.cstep;

                const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_reduce_sum4_fp32_pack4[pb % 2] : pipeline_groupnorm_reduce_sum4_fp32[pb % 2];

                cmd.record_pipeline(pipeline, bindings, constants, sum_workspace_reduced);

                pb++;
            }

            sum_workspace = sum_workspace_reduced;
        }

        // reduce mean across channels within each group
        {
            std::vector<VkMat> bindings(2);
            bindings[0] = sum_workspace;
            bindings[1] = mean_workspace;

            std::vector<vk_constant_type> constants(7);
            constants[0].i = sum_workspace.w;
            constants[1].i = 1;
            constants[2].i = sum_workspace.c;
            constants[3].i = sum_workspace.cstep;
            constants[4].f = (float)(channels_g * size_virtual);
            constants[5].i = group;
            constants[6].i = channels_g;

            const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_reduce_mean_pack4 : pipeline_groupnorm_reduce_mean;

            cmd.record_pipeline(pipeline, bindings, constants, mean_workspace);
        }
    }

    // var - one float per group
    VkMat var_workspace(group, 4u, 1, opt.workspace_vkallocator);
    {
        // sub mean and square
        VkMat square_workspace;
        if (dims == 1 || dims == 2)
        {
            square_workspace.create(size_virtual, 1, c_virtual, 4u * elempack, elempack, opt.workspace_vkallocator);
        }
        else
        {
            square_workspace.create(w, h, c, 4u * elempack, elempack, opt.workspace_vkallocator);
        }
        {
            std::vector<VkMat> bindings(3);
            bindings[0] = bottom_top_blob;
            bindings[1] = mean_workspace;
            bindings[2] = square_workspace;

            std::vector<vk_constant_type> constants(11);
            if (dims == 1 || dims == 2)
            {
                constants[0].i = 3;
                constants[1].i = size_virtual;
                constants[2].i = 1;
                constants[3].i = c_virtual;
                constants[4].i = cstep_virtual;
            }
            else
            {
                constants[0].i = std::min(3, dims);
                constants[1].i = w;
                constants[2].i = h;
                constants[3].i = c;
                constants[4].i = cstep;
            }
            constants[5].i = square_workspace.dims;
            constants[6].i = square_workspace.w;
            constants[7].i = square_workspace.h;
            constants[8].i = square_workspace.c;
            constants[9].i = square_workspace.cstep;
            constants[10].i = channels_g;

            const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_sub_mean_square_pack4 : pipeline_groupnorm_sub_mean_square;

            cmd.record_pipeline(pipeline, bindings, constants, square_workspace);
        }

        // reduce square
        VkMat sqsum_workspace = square_workspace;
        sqsum_workspace.w = sqsum_workspace.w * sqsum_workspace.h;
        sqsum_workspace.h = 1;

        int pb = 0;
        while (sqsum_workspace.w > 4)
        {
            int reduced_w = (sqsum_workspace.w + 3) / 4;
            int reduced_c = sqsum_workspace.c;

            VkMat sqsum_workspace_reduced;
            sqsum_workspace_reduced.create(reduced_w, 1, reduced_c, 4u * elempack, elempack, opt.workspace_vkallocator);

            {
                std::vector<VkMat> bindings(2);
                bindings[0] = sqsum_workspace;
                bindings[1] = sqsum_workspace_reduced;

                std::vector<vk_constant_type> constants(8);
                constants[0].i = sqsum_workspace.w;
                constants[1].i = 1;
                constants[2].i = sqsum_workspace.c;
                constants[3].i = sqsum_workspace.cstep;
                constants[4].i = sqsum_workspace_reduced.w;
                constants[5].i = 1;
                constants[6].i = sqsum_workspace_reduced.c;
                constants[7].i = sqsum_workspace_reduced.cstep;

                const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_reduce_sum4_fp32_pack4[pb % 2] : pipeline_groupnorm_reduce_sum4_fp32[pb % 2];

                cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace_reduced);

                pb++;
            }

            sqsum_workspace = sqsum_workspace_reduced;
        }

        // reduce var across channels within each group
        {
            std::vector<VkMat> bindings(2);
            bindings[0] = sqsum_workspace;
            bindings[1] = var_workspace;

            std::vector<vk_constant_type> constants(7);
            constants[0].i = sqsum_workspace.w;
            constants[1].i = 1;
            constants[2].i = sqsum_workspace.c;
            constants[3].i = sqsum_workspace.cstep;
            constants[4].f = (float)(channels_g * size_virtual);
            constants[5].i = group;
            constants[6].i = channels_g;

            const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_reduce_mean_pack4 : pipeline_groupnorm_reduce_mean;

            cmd.record_pipeline(pipeline, bindings, constants, var_workspace);
        }
    }

    // coeffs - per channel
    VkMat coeffs_workspace;
    coeffs_workspace.create(c_virtual, elemsize * 2, elempack * 2, opt.workspace_vkallocator);
    {
        std::vector<VkMat> bindings(5);
        bindings[0] = coeffs_workspace;
        bindings[1] = mean_workspace;
        bindings[2] = var_workspace;
        bindings[3] = gamma_data_gpu;
        bindings[4] = beta_data_gpu;

        std::vector<vk_constant_type> constants(1);
        constants[0].i = c_virtual;

        const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_coeffs_pack4 : pipeline_groupnorm_coeffs;

        cmd.record_pipeline(pipeline, bindings, constants, coeffs_workspace);
    }

    // norm
    {
        std::vector<VkMat> bindings(2);
        bindings[0] = bottom_top_blob;
        bindings[1] = coeffs_workspace;

        std::vector<vk_constant_type> constants(5);
        if (dims == 1 || dims == 2)
        {
            constants[0].i = 3;
            constants[1].i = size_virtual;
            constants[2].i = 1;
            constants[3].i = c_virtual;
            constants[4].i = cstep_virtual;
        }
        else
        {
            constants[0].i = std::min(3, dims);
            constants[1].i = w;
            constants[2].i = h;
            constants[3].i = c;
            constants[4].i = cstep;
        }

        const Pipeline* pipeline = elempack == 4 ? pipeline_groupnorm_norm_pack4 : pipeline_groupnorm_norm;

        VkMat dispatcher = bottom_top_blob;
        if (dims == 1)
        {
            dispatcher.w = size_virtual;
            dispatcher.h = 1;
            dispatcher.c = c_virtual;
        }
        else if (dims == 2)
        {
            dispatcher.w = size_virtual;
            dispatcher.h = 1;
            dispatcher.c = c_virtual;
        }
        cmd.record_pipeline(pipeline, bindings, constants, dispatcher);
    }

    return 0;
}

} // namespace ncnn
