DeepInfer/csrc/engine/utils.hpp
2026-10-20 12:09:42 +08:00

41 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file utils.hpp
* @brief 实用工具函数和宏定义。
*/
#pragma once
#include <cassert>
#include <cstdlib>
#include <sstream>
#include <string>
#include <torch/torch.h>
#include "../cuda_utils.hpp" // get_default_device()
/**
* @def SPRINT_VARS(prefix, ...)
* @brief 将变量名和值格式化为 string,常用于 TORCH_CHECK 的报错信息。
*
* @par 示例
* @code
* int a = 10;
* double b = 3.14;
* SPRINT_VARS("ctx", a, b); // "ctx a, b: 10 3.14 "
* @endcode
*/
#define SPRINT_VARS(prefix, ...) sprint_vars_impl(prefix, #__VA_ARGS__, __VA_ARGS__)
template <typename... Args> std::string sprint_vars_impl(const char *prefix, const char *names, const Args &...args) {
std::stringstream ss;
ss << prefix << " " << names << ": ";
((ss << args << " "), ...);
return ss.str();
}
inline int get_nextn_from_env() {
const char *env = std::getenv("DEEPINFER_USE_NEXTN_DECODE");
// Keep the unset default consistent with deepinfer/environ.py and script entry points.
return env != nullptr ? atoi(env) : 5;
}