首页 > 解决方案 > 半浮点数的 FLT_MAX

问题描述

我正在使用带有半浮点数的 CUDA,或者__half在 CUDA 中调用它们。

半浮点数相当于FLT_MAX多少?

cuda_fp16.h头似乎没有类似的宏。

$ grep MAX /usr/local/cuda-11.1/targets/x86_64-linux/include/cuda_fp16.h
$

标签: cudamath.hhalf-precision-float

解决方案


我以前需要一次类似的宏(虽然不是在 CUDA 中),并在这个 C++ fp16 提案中找到了一些用于short floats的常量。

“S”前缀来自建议的短浮点数“short”。

// Smallest positive short float
#define SFLT_MIN 5.96046448e-08
// Smallest positive
// normalized short float
#define SFLT_NRM_MIN 6.10351562e-05
// Largest positive short float
#define SFLT_MAX 65504.0
// Smallest positive e
// for which (1.0 + e) != (1.0)
#define SFLT_EPSILON 0.00097656
// Number of digits in mantissa
// (significand + hidden leading 1)
#define SFLT_MANT_DIG 11
// Number of base 10 digits that
// can be represented without change
#define SFLT_DIG 2
// Base of the exponent
#define SFLT_RADIX 2
// Minimum negative integer such that
// HALF_RADIX raised to the power of
// one less than that integer is a
// normalized short float
#define SFLT_MIN_EXP -13
// Maximum positive integer such that
// HALF_RADIX raised to the power of
// one less than that integer is a
// normalized short float
#define SFLT_MAX_EXP 16
// Minimum positive integer such
// that 10 raised to that power is
// a normalized short float
#define SFLT_MIN_10_EXP -4
// Maximum positive integer such
// that 10 raised to that power is
// a normalized short float
#define SFLT_MAX_10_EXP 4

您还可以从half.hpp 库中找到类似的常量。

注意:我不确定 CUDA 编译器在 fp16 文字方面支持什么。因此,您可能需要将这些转换为十六进制,将这些位重新解释为 __half(注意:注意转换/转换)。

这些都不是理想的,如果有人可以将您指向某个cuda_fp16_limits.h文件,那么请选择这个答案而不是这个答案。


推荐阅读