fp16半精度浮点数转成float类型------C/C++

2024-08-30 22:48

本文主要是介绍fp16半精度浮点数转成float类型------C/C++,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在深度学习算法模型推理时,会遇到fp16类型,但是我们的c语言中没有这种类型,直接转成unsigned short又会丧失精度,因此我们首先将FP16转成float类型,再进行计算。

方法1:

typedef unsigned short ushort;//占用2个字节
typedef unsigned int uint;    //占用4个字节uint as_uint(const float x) {return *(uint*)&x;
}
float as_float(const uint x) {return *(float*)&x;
}float half_to_float(const ushort x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digitsconst uint e = (x&0x7C00)>>10; // exponentconst uint m = (x&0x03FF)<<13; // mantissaconst uint v = as_uint((float)m)>>23; // evil log2 bit hack to count leading zeros in denormalized formatreturn as_float((x&0x8000)<<16 | (e!=0)*((e+112)<<23|m) | ((e==0)&(m!=0))*((v-37)<<23|((m<<(150-v))&0x007FE000))); // sign : normalized : denormalized
}
ushort float_to_half(const float x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digitsconst uint b = as_uint(x)+0x00001000; // round-to-nearest-even: add last bit after truncated mantissaconst uint e = (b&0x7F800000)>>23; // exponentconst uint m = b&0x007FFFFF; // mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial roundingreturn (b&0x80000000)>>16 | (e>112)*((((e-112)<<10)&0x7C00)|m>>13) | ((e<113)&(e>101))*((((0x007FF000+m)>>(125-e))+1)>>1) | (e>143)*0x7FFF; // sign : normalized : denormalized : saturate
}

方法2:

float cpu_half2float(unsigned short x)
{unsigned sign = ((x >> 15) & 1);unsigned exponent = ((x >> 10) & 0x1f);unsigned mantissa = ((x & 0x3ff) << 13);if (exponent == 0x1f) {  /* NaN or Inf */mantissa = (mantissa ? (sign = 0, 0x7fffff) : 0);exponent = 0xff;} else if (!exponent) {  /* Denorm or Zero */if (mantissa) {unsigned int msb;exponent = 0x71;do {msb = (mantissa & 0x400000);mantissa <<= 1;  /* normalize */--exponent;} while (!msb);mantissa &= 0x7fffff;  /* 1.mantissa is implicit */}} else {exponent += 0x70;}int temp = ((sign << 31) | (exponent << 23) | mantissa);return *((float*)((void*)&temp));
}

3 demo

 下面的demo中,yolov5_outputs[0].buf是void *类型的,void *类型不能++,因此先转换成ushort*类型。

    ......   float *data0 = (float*)malloc(4 * output_attrs[0].n_elems);float *data1 = (float*)malloc(4 * output_attrs[1].n_elems);float *data2 = (float*)malloc(4 * output_attrs[2].n_elems);unsigned short *temp0 = (ushort*)yolov5_outputs[0].buf;unsigned short *temp1 = (ushort*)yolov5_outputs[1].buf;unsigned short *temp2 = (ushort*)yolov5_outputs[2].buf;for(int i=0; i < output_attrs[0].n_elems;i++){data0[i] = half_to_float(temp0[i]);}for(int i=0; i < output_attrs[1].n_elems;i++){data1[i] = half_to_float(temp1[i]);}for(int i=0; i < output_attrs[2].n_elems;i++){data2[i] = half_to_float(temp2[i]);}......

参考文献:

https://github.com/PrincetonVision/marvin/blob/master/tools/tensorIO_matlab/half2float.cpp

这篇关于fp16半精度浮点数转成float类型------C/C++的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1122156

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

C++读写word文档(.docx)DuckX库的使用详解

《C++读写word文档(.docx)DuckX库的使用详解》DuckX是C++库,用于创建/编辑.docx文件,支持读取文档、添加段落/片段、编辑表格,解决中文乱码需更改编码方案,进阶功能含文本替换... 目录一、基本用法1. 读取文档3. 添加段落4. 添加片段3. 编辑表格二、进阶用法1. 文本替换2

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十