Skip to content

Allow printf in shaders #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions python/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ namespace py = pybind11;
// used in Core.hpp
py::object kp_trace, kp_debug, kp_info, kp_warning, kp_error;

static void kp_log(int level, const std::string& msg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it is already understandable, I would put an enum for the level (just to have a meaningful level name from "trace" to "error")

py::gil_scoped_acquire gil;
switch (level) {
case 0:
kp_trace(msg); break;
case 1:
kp_debug(msg); break;
case 2:
kp_info(msg); break;
case 3:
kp_warning(msg); break;
case 4:
kp_error(msg); break;
default:
break;
}
}

void py_log_trace(const std::string& msg) {kp_log(0, msg);}
void py_log_debug(const std::string& msg) {kp_log(1, msg);}
void py_log_info(const std::string& msg) {kp_log(2, msg);}
void py_log_warning(const std::string& msg) {kp_log(3, msg);}
void py_log_error(const std::string& msg) {kp_log(4, msg);}

std::unique_ptr<kp::OpAlgoDispatch>
opAlgoDispatchPyInit(std::shared_ptr<kp::Algorithm>& algorithm,
const py::array& push_consts)
Expand Down
73 changes: 60 additions & 13 deletions src/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@
#include <string>
#include <unordered_map>

template <> struct fmt::formatter<vk::ValidationFeatureEnableEXT>: public fmt::formatter<std::string> {

// parse is inherited from formatter<string>.

template <typename Context>
fmt::format_context::iterator format (vk::ValidationFeatureEnableEXT const& flag, Context& ctx) const {
std::string name {"unknown"};
switch(flag) {
case vk::ValidationFeatureEnableEXT::eDebugPrintf:
name = "debugPrintF";
break;
case vk::ValidationFeatureEnableEXT::eGpuAssisted:
name = "gpuAssisted";
break;
case vk::ValidationFeatureEnableEXT::eGpuAssistedReserveBindingSlot:
name = "gpuAssistedReserveBindingSlot";
break;
case vk::ValidationFeatureEnableEXT::eBestPractices:
name = "bestPractices";
break;
case vk::ValidationFeatureEnableEXT::eSynchronizationValidation:
name = "synchronizationValidation";
break;
}
return fmt::formatter<std::string>::format(name, ctx);
}
};

namespace kp {

#ifndef KOMPUTE_DISABLE_VK_DEBUG_LAYERS
Expand Down Expand Up @@ -185,10 +213,11 @@ Manager::createInstance()
#endif

if (!applicationExtensions.empty()) {
computeInstanceCreateInfo.enabledExtensionCount =
(uint32_t)applicationExtensions.size();
computeInstanceCreateInfo.ppEnabledExtensionNames =
applicationExtensions.data();
computeInstanceCreateInfo.setEnabledExtensionCount(
(uint32_t)applicationExtensions.size());
computeInstanceCreateInfo.setPpEnabledExtensionNames(
applicationExtensions.data());
KP_LOG_DEBUG("Desired extensions: {}", fmt::join(applicationExtensions, ", "));
}

#ifndef KOMPUTE_DISABLE_VK_DEBUG_LAYERS
Expand All @@ -212,7 +241,21 @@ Manager::createInstance()
for (const std::string& layerName : envLayerNames) {
desiredLayerNames.push_back(layerName.c_str());
}
KP_LOG_DEBUG("Desired layers: {}", fmt::join(desiredLayerNames, ", "));
}
KP_LOG_DEBUG("Desired layers: {}", fmt::join(desiredLayerNames, ", "));

vk::ValidationFeaturesEXT validationFeatures;
std::vector<vk::ValidationFeatureEnableEXT> validationFeatureEnablesExt;
const char* envPrintfVal = std::getenv("KOMPUTE_ENV_PRINTF");
if (envPrintfVal != nullptr && *envPrintfVal != '\0') {
KP_LOG_DEBUG("Kompute Manager adding debug printf");
validationFeatureEnablesExt.push_back(vk::ValidationFeatureEnableEXT::eDebugPrintf);
}

if (!validationFeatureEnablesExt.empty()) {
validationFeatures.setEnabledValidationFeatures(validationFeatureEnablesExt);
computeInstanceCreateInfo.setPNext(&validationFeatures);
KP_LOG_DEBUG("Validation features: {}", fmt::join(validationFeatureEnablesExt, ", "));
}

// Identify the valid layer names based on the desiredLayerNames
Expand All @@ -236,9 +279,9 @@ Manager::createInstance()
KP_LOG_DEBUG(
"Kompute Manager Initializing instance with valid layers: {}",
fmt::join(validLayerNames, ", "));
computeInstanceCreateInfo.enabledLayerCount =
static_cast<uint32_t>(validLayerNames.size());
computeInstanceCreateInfo.ppEnabledLayerNames = validLayerNames.data();
computeInstanceCreateInfo.setEnabledLayerCount(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for next ones but I like the usage of the VKCPP capabilities

static_cast<uint32_t>(validLayerNames.size()));
computeInstanceCreateInfo.setPpEnabledLayerNames(validLayerNames.data());
} else {
KP_LOG_WARN("Kompute Manager no valid layer names found from desired "
"layer names");
Expand Down Expand Up @@ -268,15 +311,14 @@ Manager::createInstance()
KP_LOG_DEBUG("Kompute Manager Instance Created");

#ifndef KOMPUTE_DISABLE_VK_DEBUG_LAYERS
KP_LOG_DEBUG("Kompute Manager adding debug callbacks");
if (validLayerNames.size() > 0) {
KP_LOG_DEBUG("Kompute Manager adding debug callbacks");
vk::DebugReportFlagsEXT debugFlags =
vk::DebugReportFlagBitsEXT::eError |
vk::DebugReportFlagBitsEXT::eWarning;
vk::DebugReportCallbackCreateInfoEXT debugCreateInfo = {};
debugCreateInfo.pfnCallback =
(PFN_vkDebugReportCallbackEXT)debugMessageCallback;
debugCreateInfo.flags = debugFlags;
auto debugCreateInfo = vk::DebugReportCallbackCreateInfoEXT()
.setFlags(debugFlags)
.setPfnCallback((PFN_vkDebugReportCallbackEXT)debugMessageCallback);

this->mDebugDispatcher.init(*this->mInstance, &vkGetInstanceProcAddr);
this->mDebugReportCallback =
Expand Down Expand Up @@ -384,6 +426,7 @@ Manager::createDevice(const std::vector<uint32_t>& familyQueueIndices,
} else {
this->mComputeQueueFamilyIndices = familyQueueIndices;
}
KP_LOG_DEBUG("compute queue family indices: {}", fmt::join(this->mComputeQueueFamilyIndices, ", "));

std::unordered_map<uint32_t, uint32_t> familyQueueCounts;
std::unordered_map<uint32_t, std::vector<float>> familyQueuePriorities;
Expand Down Expand Up @@ -425,11 +468,15 @@ Manager::createDevice(const std::vector<uint32_t>& familyQueueIndices,
validExtensions.push_back(ext.c_str());
}
}

if (desiredExtensions.size() != validExtensions.size()) {
KP_LOG_ERROR("Kompute Manager not all extensions were added: {}",
fmt::join(validExtensions, ", "));
}

KP_LOG_DEBUG("Kompute Manager used extensions {}",
fmt::join(validExtensions, ", "));

vk::DeviceCreateInfo deviceCreateInfo(vk::DeviceCreateFlags(),
deviceQueueCreateInfos.size(),
deviceQueueCreateInfos.data(),
Expand Down
9 changes: 6 additions & 3 deletions src/include/kompute/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ typedef std::vector<float> Constants;
#endif // KOMPUTE_VK_API_VERSION

#if defined(KOMPUTE_BUILD_PYTHON)
#include <pybind11/pybind11.h>
namespace py = pybind11;
// from python/src/main.cpp
extern py::object kp_trace, kp_debug, kp_info, kp_warning, kp_error;

extern void py_log_trace(const std::string& msg);
extern void py_log_debug(const std::string& msg);
extern void py_log_info(const std::string& msg);
extern void py_log_warning(const std::string& msg);
extern void py_log_error(const std::string& msg);
#endif
20 changes: 11 additions & 9 deletions src/include/kompute/logger/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ static const char* KOMPUTE_LOG_TAG = "KomputeLog";
#else
#if KOMPUTE_BUILD_PYTHON
#include <fmt/core.h>
#include <pybind11/pybind11.h>
namespace py = pybind11;
// from python/src/main.cpp
extern py::object kp_trace, kp_debug, kp_info, kp_warning, kp_error;
extern void py_log_trace(const std::string& msg);
extern void py_log_debug(const std::string& msg);
extern void py_log_info(const std::string& msg);
extern void py_log_warning(const std::string& msg);
extern void py_log_error(const std::string& msg);
#else
#include <fmt/core.h>
#endif // KOMPUTE_BUILD_PYTHON
Expand Down Expand Up @@ -57,7 +59,7 @@ setupLogger();
ANDROID_LOG_VERBOSE, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
#else
#if KOMPUTE_BUILD_PYTHON
#define KP_LOG_TRACE(...) kp_trace(fmt::format(__VA_ARGS__))
#define KP_LOG_TRACE(...) py_log_trace(fmt::format(__VA_ARGS__))
#else
#define KP_LOG_TRACE(...) \
fmt::print("[{} {}] [trace] [{}:{}] {}\n", \
Expand All @@ -81,7 +83,7 @@ setupLogger();
ANDROID_LOG_DEBUG, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
#else
#if KOMPUTE_BUILD_PYTHON
#define KP_LOG_DEBUG(...) kp_debug(fmt::format(__VA_ARGS__))
#define KP_LOG_DEBUG(...) py_log_debug(fmt::format(__VA_ARGS__))
#else
#ifdef __FILE_NAME__ // gcc 12 provides only file name without path
#define KP_LOG_DEBUG(...) \
Expand All @@ -98,7 +100,7 @@ setupLogger();
__TIME__, \
__FILE__, \
__LINE__, \
fmt::format(__VA_ARGS__))
fmt::format(__VA_ARGS__));
#endif // __FILE__NAME__
#endif // KOMPUTE_BUILD_PYTHON
#endif // VK_USE_PLATFORM_ANDROID_KHR
Expand All @@ -115,7 +117,7 @@ setupLogger();
ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
#else
#if KOMPUTE_BUILD_PYTHON
#define KP_LOG_INFO(...) kp_info(fmt::format(__VA_ARGS__))
#define KP_LOG_INFO(...) py_log_info(fmt::format(__VA_ARGS__))
#else
#define KP_LOG_INFO(...) \
fmt::print("[{} {}] [info] [{}:{}] {}\n", \
Expand All @@ -139,7 +141,7 @@ setupLogger();
ANDROID_LOG_WARN, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
#else
#if KOMPUTE_BUILD_PYTHON
#define KP_LOG_WARN(...) kp_warning(fmt::format(__VA_ARGS__))
#define KP_LOG_WARN(...) py_log_warning(fmt::format(__VA_ARGS__))
#else
#define KP_LOG_WARN(...) \
fmt::print("[{} {}] [warn] [{}:{}] {}\n", \
Expand All @@ -163,7 +165,7 @@ setupLogger();
ANDROID_LOG_ERROR, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
#else
#if KOMPUTE_BUILD_PYTHON
#define KP_LOG_ERROR(...) kp_error(fmt::format(__VA_ARGS__))
#define KP_LOG_ERROR(...) py_log_error(fmt::format(__VA_ARGS__))
#else
#define KP_LOG_ERROR(...) \
fmt::print("[{} {}] [error] [{}:{}] {}\n", \
Expand Down
Loading