From 51a8f7ad892b1174d32cba8358804fad09b58f76 Mon Sep 17 00:00:00 2001 From: Denis Samoilov Date: Thu, 6 Jul 2023 12:53:13 -0700 Subject: [PATCH] gtests: add a test case for profiling API --- tests/gtests/ocl/api/test_stream.cpp | 47 +++++++++++++++++++++++++++ tests/gtests/sycl/api/test_stream.cpp | 45 ++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/tests/gtests/ocl/api/test_stream.cpp b/tests/gtests/ocl/api/test_stream.cpp index 39890f5d55b..8a7ad259493 100644 --- a/tests/gtests/ocl/api/test_stream.cpp +++ b/tests/gtests/ocl/api/test_stream.cpp @@ -419,4 +419,51 @@ TEST_F(ocl_stream_test_cpp_t, TestProfilingAPICPU) { #endif +#ifndef DNNL_EXPERIMENTAL_PROFILING +extern "C" dnnl_status_t dnnl_reset_profiling(dnnl_stream_t stream); +#endif + +TEST_F(ocl_stream_test_cpp_t, TestProfilingAPIDisabledAndEnabled) { + SKIP_IF(!find_ocl_device(CL_DEVICE_TYPE_GPU), + "OpenCL GPU devices not found."); + + memory::dims dims = {2, 3, 4, 5}; + memory::desc md(dims, memory::data_type::f32, memory::format_tag::nchw); + + auto eltwise_pd = eltwise_forward::primitive_desc( + eng, prop_kind::forward, algorithm::eltwise_relu, md, md, 0.0f); + auto eltwise = eltwise_forward(eltwise_pd); + auto mem = memory(md, eng); + + cl_int err; +#ifdef CL_VERSION_2_0 + cl_queue_properties properties[] + = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0}; + cl_command_queue ocl_queue = clCreateCommandQueueWithProperties( + ocl_ctx, ocl_dev, properties, &err); +#else + cl_command_queue_properties properties = CL_QUEUE_PROFILING_ENABLE; + cl_command_queue ocl_queue + = clCreateCommandQueue(ocl_ctx, ocl_dev, properties, &err); +#endif + + TEST_OCL_CHECK(err); + + auto stream = ocl_interop::make_stream(eng, ocl_queue); + TEST_OCL_CHECK(clReleaseCommandQueue(ocl_queue)); + + eltwise.execute(stream, {{DNNL_ARG_SRC, mem}, {DNNL_ARG_DST, mem}}); + stream.wait(); + + auto st = dnnl_reset_profiling(stream.get()); + +// If the experimental profiling API is not enabled then the library should not +// enable profiling regardless of the queue's properties. +#ifdef DNNL_EXPERIMENTAL_PROFILING + EXPECT_EQ(st, dnnl_success); +#else + EXPECT_EQ(st, dnnl_invalid_arguments); +#endif +} + } // namespace dnnl diff --git a/tests/gtests/sycl/api/test_stream.cpp b/tests/gtests/sycl/api/test_stream.cpp index aeb5c28bc32..31442d342f2 100644 --- a/tests/gtests/sycl/api/test_stream.cpp +++ b/tests/gtests/sycl/api/test_stream.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright 2019-2022 Intel Corporation +* Copyright 2019-2023 Intel Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -132,6 +132,49 @@ TEST_P(sycl_stream_test, InteropIncompatibleQueue) { true, dnnl_invalid_arguments); } +#ifndef DNNL_EXPERIMENTAL_PROFILING +extern "C" dnnl_status_t dnnl_reset_profiling(dnnl_stream_t stream); +#endif + +TEST_P(sycl_stream_test, TestProfilingAPIDisabledAndEnabled) { + engine::kind kind = GetParam(); + SKIP_IF(!has(kind), "Device not found."); + SKIP_IF(kind == engine::kind::cpu, "Test is GPU specific."); + + auto sycl_queue = sycl::queue(get_context(kind), get_device(kind), + sycl::property_list {sycl::property::queue::in_order {}, + sycl::property::queue::enable_profiling {}}); + stream strm = sycl_interop::make_stream(get_engine(kind), sycl_queue); + + memory::dims tz_dims = {2, 3, 4, 5}; + const size_t N = std::accumulate(tz_dims.begin(), tz_dims.end(), (size_t)1, + std::multiplies()); + auto usm_buffer = (float *)malloc_shared( + N * sizeof(float), get_device(kind), get_context(kind)); + + memory::desc mem_d( + tz_dims, memory::data_type::f32, memory::format_tag::nchw); + + memory mem = sycl_interop::make_memory(mem_d, get_engine(kind), + sycl_interop::memory_kind::usm, usm_buffer); + + auto relu_pd = eltwise_forward::primitive_desc(get_engine(kind), + prop_kind::forward, algorithm::eltwise_relu, mem_d, mem_d, 0.0f); + auto relu = eltwise_forward(relu_pd); + relu.execute(strm, {{DNNL_ARG_SRC, mem}, {DNNL_ARG_DST, mem}}); + strm.wait(); + + auto st = dnnl_reset_profiling(strm.get()); + +// If the experimental profiling API is not enabled then the library should not +// enable profiling regardless of the queue's properties. +#ifdef DNNL_EXPERIMENTAL_PROFILING + EXPECT_EQ(st, dnnl_success); +#else + EXPECT_EQ(st, dnnl_invalid_arguments); +#endif +} + // TODO: Enable the test below after sycl_stream_t is fixed to not reuse the // service stream. Now it ignores the input stream flags and reuses the service // stream which is constructed without any flags.