ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

cl_convolution example - how to free memory

Open hemantbits opened this issue 8 years ago • 5 comments

Output of 'strings libarm_compute.so | grep arm_compute_version': arm_compute_version=v18.08 Build options: {'arch': 'armv7a', 'opencl': '1', 'neon': '1', 'benchmark_tests': '0', 'examples': '0', 'debug': '0', 'os': 'linux', 'Werror': '1'} Git hash=unknown

Platform: gpu mali g-51

Operating System: Linux

Problem description: I'm using compute library kernels, e.g. CLDepthwiseConvolutionLayer, CLDirectConvolutionLayer etc. to configure and run my scenario. I'm not using Graph API. I'm seeing that memory allocated after configure and allocate are not being freed and due to my huge model, it runs out of OOM soon.

To confirm this, I modified cl_convolution example in the following manner and confirmed that even after allocator()->free(), there is no release of memory (See output below). Please can you clarify if I'm doing something wrong here or missing something or this is a bug?

dump_gpu_memory prints process name, pid, current GPU memory usage, maximum GPU memory usage and a custom message.

output:

sh-3.2# ./cl_convolution input.ppm

./cl_convolution

tsai: my constructor in proc cl_convolution (init=b446b559) cl_convolution 15480 2228 2228 Before conv3x3 configure cl_convolution 15480 12424 12424 after allocate cl_convolution 15480 12424 12424 after run cl_convolution 15480 12424 12424 before free cl_convolution 15480 12424 12424 after teardown

Test passed cl_convolution 15480 12424 12424 after running example tsai: my destructor in proc cl_convolution sh-3.2# ./cl_convolution

./cl_convolution

tsai: my constructor in proc cl_convolution (init=b43ed559) Usage: ./build/cl_convolution [input_image.ppm]

No input_image provided, creating a dummy 640x480 image cl_convolution 15542 2228 2228 Before conv3x3 configure cl_convolution 15542 3764 3764 after allocate cl_convolution 15542 3764 3764 after run cl_convolution 15542 3764 3764 before free cl_convolution 15542 3764 3764 after teardown

Test passed cl_convolution 15542 3764 3764 after running example

code:

void dump_gpu_memory(std::string msg)
{
    char line[128];

    FILE* file = fopen("/sys/kernel/debug/gpu/gpu_memory", "r");
    if(file)
    {
        while(fgets(line, 128, file) != NULL){
            //printf("%s\n", line);
            if (strncmp(line, "cl_convo", strlen("cl_convo")) == 0)
            {
                std::string str(line);
                str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
                printf("%s %s\n", str.c_str(), msg.c_str());
            }
        }
        fclose(file);
    }
    return;
}

class CLConvolutionExample : public Example
{
public:
    bool do_setup(int argc, char **argv) override
    {
        PPMLoader ppm;

        CLScheduler::get().default_init();

        if(argc < 2)
        {
            // Print help
            std::cout << "Usage: ./build/cl_convolution [input_image.ppm]\n\n";
            std::cout << "No input_image provided, creating a dummy 640x480 image\n";
            // Create an empty grayscale 640x480 image
            src.allocator()->init(TensorInfo(640, 480, Format::U8));
        }
        else
        {
            ppm.open(argv[1]);
            ppm.init_image(src, Format::U8);
        }

        // Configure the temporary and destination images
        tmp.allocator()->init(*src.info());
        dst.allocator()->init(*src.info());

        dump_gpu_memory("Before conv3x3 configure");
        // Apply a Gaussian 3x3 filter to the source image followed by a Gaussian 5x5:
        conv3x3.configure(&src, &tmp, gaussian3x3, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
        conv5x5.configure(&tmp, &dst, gaussian5x5, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);

        // Allocate all the images
        src.allocator()->allocate();
        tmp.allocator()->allocate();
        dst.allocator()->allocate();

        dump_gpu_memory("after allocate");

        // Fill the input image with the content of the PPM image if a filename was provided:
        if(ppm.is_open())
        {
            ppm.fill_image(src);
            output_filename = std::string(argv[1]) + "_out.ppm";
        }

        return true;
    }
    void do_run() override
    {
        // Execute the functions:
        conv3x3.run();
        conv5x5.run();

        // Make sure all the OpenCL jobs are done executing:
        CLScheduler::get().sync();

        dump_gpu_memory("after run");
    }
    void do_teardown() override
    {
        // Save the result to file:
        if(!output_filename.empty())
        {
            save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
        }

        dump_gpu_memory("before free");
        src.allocator()->free();
        tmp.allocator()->free();
        dst.allocator()->free();

        dump_gpu_memory("after teardown");
    }
    CLImage          src{}, tmp{}, dst{};
    CLConvolution3x3 conv3x3{};
    CLConvolution5x5 conv5x5{};
    std::string      output_filename{};
};

void run(int argc, char **argv)
{
    utils::run_example<CLConvolutionExample>(argc, argv);
}

/** Main program for convolution test
 *
 * @param[in] argc Number of arguments
 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
 */
int main(int argc, char **argv)
{
    run(argc, argv);
    dump_gpu_memory("after running example");
}

hemantbits avatar Oct 30 '18 15:10 hemantbits

Hi,

It might be because the OpenCL driver keeps some memory cached until the CLContext gets destroyed. What we do in our unit tests is we reset the Context every once in a while: https://github.com/ARM-software/ComputeLibrary/blob/master/tests/framework/Framework.cpp#L535

This should be enough to force the OpenCL driver to release the memory it's got cached.

Hope this helps,

AnthonyBarbier avatar Oct 30 '18 16:10 AnthonyBarbier

Thanks for quick reply. However, if I add the code to reset queue with clearing cache, it makes cl_covolution use more memory. And if I use only clearning code, there is no change in behaviour.

output with reset and clearning

sh-3.2# ./cl_convolution

./cl_convolution

tsai: my constructor in proc cl_convolution (init=b4493559) Usage: ./build/cl_convolution [input_image.ppm]

No input_image provided, creating a dummy 640x480 image cl_convolution 20047 2228 2228 Before conv3x3 configure cl_convolution 20047 3764 3764 after allocate cl_convolution 20047 3764 3764 after run cl_convolution 20047 3764 3764 before free cl_convolution 20047 3764 3764 after free cl_convolution 20047 5820 5820 After clearing programs cache

Test passed cl_convolution 20047 5820 5820 after running example tsai: my destructor in proc cl_convolution sh-3.2# ./cl_convolution misc/input.ppm

./cl_convolution

tsai: my constructor in proc cl_convolution (init=b43c5559) cl_convolution 20210 2228 2228 Before conv3x3 configure cl_convolution 20210 12424 12424 after allocate cl_convolution 20210 12424 12424 after run cl_convolution 20210 12424 12424 before free cl_convolution 20210 12424 12424 after free cl_convolution 20210 14480 14480 After clearing programs cache

Test passed cl_convolution 20210 14480 14480 after running example tsai: my destructor in proc cl_convolution

code with reset and clearning

    void do_teardown() override
    {
        // Save the result to file:
        if(!output_filename.empty())
        {
            save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
        }

        dump_gpu_memory("before free");
        src.allocator()->free();
        tmp.allocator()->free();
        dst.allocator()->free();
        dump_gpu_memory("after free");

        auto ctx_properties = CLScheduler::get().context().getInfo<CL_CONTEXT_PROPERTIES>(nullptr);
        auto queue_properties = CLScheduler::get().queue().getInfo<CL_QUEUE_PROPERTIES>(nullptr);
        cl::Context new_ctx = cl::Context(CL_DEVICE_TYPE_DEFAULT, ctx_properties.data());
        cl::CommandQueue new_queue = cl::CommandQueue(new_ctx, CLKernelLibrary::get().get_device(), queue_properties);

        CLKernelLibrary::get().clear_programs_cache();

        CLScheduler::get().set_context(new_ctx);
        CLScheduler::get().set_queue(new_queue);

        dump_gpu_memory("After clearing programs cache");
    }

output with only clearning cache

sh-3.2# ./cl_convolution misc/input.ppm

./cl_convolution

tsai: my constructor in proc cl_convolution (init=b4460559) cl_convolution 21995 2228 2228 Before conv3x3 configure cl_convolution 21995 12424 12424 after allocate cl_convolution 21995 12424 12424 after run cl_convolution 21995 12424 12424 before free cl_convolution 21995 12424 12424 after free cl_convolution 21995 12424 12424 After clearing programs cache

Test passed cl_convolution 21995 12424 12424 after running example tsai: my destructor in proc cl_convolution sh-3.2#

code with only clearning cache

    void do_teardown() override
    {
        // Save the result to file:
        if(!output_filename.empty())
        {
            save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
        }

        dump_gpu_memory("before free");
        src.allocator()->free();
        tmp.allocator()->free();
        dst.allocator()->free();
        dump_gpu_memory("after free");

/*
        auto ctx_properties = CLScheduler::get().context().getInfo<CL_CONTEXT_PROPERTIES>(nullptr);
        auto queue_properties = CLScheduler::get().queue().getInfo<CL_QUEUE_PROPERTIES>(nullptr);
        cl::Context new_ctx = cl::Context(CL_DEVICE_TYPE_DEFAULT, ctx_properties.data());
        cl::CommandQueue new_queue = cl::CommandQueue(new_ctx, CLKernelLibrary::get().get_device(), queue_properties);
*/
        CLKernelLibrary::get().clear_programs_cache();
/*
        CLScheduler::get().set_context(new_ctx);
        CLScheduler::get().set_queue(new_queue);
*/
        dump_gpu_memory("After clearing programs cache");
    }

hemantbits avatar Oct 30 '18 16:10 hemantbits

Hello @AnthonyARM Do you think the above was correct understanding of what you said? Do you have any other idea on how this memory could be freed? Regards, H

hemantbits avatar Nov 01 '18 08:11 hemantbits

I don't know what gpu_memory reports so I'm afraid I can't give you much more details. The Context itself probably needs some memory so it's probably why it doesn't go back quite to 0.

Either way, In our internal testing we've got some nightly runs with OCLGrind and no memory leaks are detected, so we're fairly confident that it's not a problem with the ComputeLibrary.

AnthonyBarbier avatar Nov 01 '18 13:11 AnthonyBarbier

Closing for inactivity, feel free to reopen otherwise

ManubARM avatar Feb 01 '19 11:02 ManubARM