mirror of
https://github.com/deepseek-ai/DeepInfer.git
synced 2026-10-28 23:07:09 +08:00
41 lines
1.0 KiB
C++
41 lines
1.0 KiB
C++
/**
|
||
* @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;
|
||
}
|