Skip to content

Add GPU implementation for median filter - #1917

Merged
jordimas merged 9 commits into
OpenNMT:masterfrom
a2d8a4v:master
Dec 18, 2025
Merged

jordimas merged 9 commits into
OpenNMT:masterfrom
a2d8a4v:master

Conversation

@a2d8a4v

@a2d8a4v a2d8a4v commented Sep 25, 2025

Copy link
Copy Markdown
Contributor

The current median filter operator has a CPU version alone. Adding a GPU implementation to satisfy the support for GPU computing.

@a2d8a4v a2d8a4v changed the title Add GPU implementation for medain filter Add GPU implementation for median filter Sep 25, 2025
@a2d8a4v

a2d8a4v commented Sep 25, 2025 •

Copy link
Copy Markdown
Contributor Author

The code for testing the GPU implementation is here


void test_median_filter() {

    StorageView input({1, 2, 10}, DataType::FLOAT32);
    auto* in_data = input.data<float>();
    for (size_t i = 0; i < 20; ++i) in_data[i] = float(i + 1);
    std::cout << "Input:\n";
    for (size_t i = 0; i < 20; ++i) {
        std::cout << in_data[i] << ' ';
        if ((i + 1) % 10 == 0) std::cout << '\n';
    }

    StorageView output;
    ops::MedianFilter median(7);
    median(input, output);

    std::cout << "Output:\n";
    auto* out_data = output.data<float>();
    for (size_t i = 0; i < 20; ++i) {
        std::cout << out_data[i] << ' ';
        if ((i + 1) % 10 == 0) std::cout << '\n';
    }

    StorageView input_gpu = input.to(Device::CUDA);
    StorageView output_gpu(input_gpu.shape(), input.dtype(), Device::CUDA);
    output_gpu.zero();
    ops::MedianFilter(7)(input_gpu, output_gpu);
    std::cout << "Output (GPU -> CPU copy):\n";
    StorageView output_gpu_host = output_gpu.to(Device::CPU);
    out_data = output_gpu_host.data<float>();
    for (size_t i = 0; i < 20; ++i) {
        std::cout << out_data[i] << ' ';
        if ((i + 1) % 10 == 0) std::cout << '\n';
    }
    
    std::cout << "Input GPU size: " << input_gpu.size() << '\n';
    std::cout << "Output GPU size: " << output_gpu.size() << '\n';
}

The result is like:

Input:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
Output:
3 3 3 4 5 6 7 8 8 8
13 13 13 14 15 16 17 18 18 18
Output (GPU -> CPU copy):
3 3 3 4 5 6 7 8 8 8
13 13 13 14 15 16 17 18 18 18
Input GPU size: 20
Output GPU size: 20  

I hope this tiny experiment proves that it can work.

@a2d8a4v

a2d8a4v commented Oct 1, 2025

Copy link
Copy Markdown
Contributor Author

Hi, @minhthuc2502
Can you help me review this pull req?

Sincerely,

@jordimas

Copy link
Copy Markdown
Collaborator

Thanks for your work

Will be possible to update the current MedianFilter then executes also in CUDA and test that the test pass?

@a2d8a4v

a2d8a4v commented Nov 26, 2025

Copy link
Copy Markdown
Contributor Author

Sure!

@a2d8a4v

a2d8a4v commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

Wow, thank you for helping me
Sorry for my delay...

@a2d8a4v

a2d8a4v commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

I think the implementation in src/models/whisper.cc can be updated as well, cause the GPU implementation is now provided.

@jordimas

Copy link
Copy Markdown
Collaborator

Thanks for your work, it is really good.
In order for Whisper to use in GPU the median filter it will be great to complete this first:

#1954

If you can help it will be great.

@jordimas
jordimas merged commit 3aa101f into OpenNMT:master Dec 18, 2025
16 checks passed
@a2d8a4v

a2d8a4v commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

Gotcha, let me see this issue~

Lanurence666 pushed a commit to Lanurence666/CTranslate2_backport_py38 that referenced this pull request May 23, 2026
The current median filter operator has a CPU version alone. Adding a GPU implementation to satisfy the support for GPU computing.
veilnetwork pushed a commit to veilnetwork/CTranslate2 that referenced this pull request Aug 27, 2026
The current median filter operator has a CPU version alone. Adding a GPU implementation to satisfy the support for GPU computing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants