[C++][algorithm] 使用C++实现FP16转FP32算法

1 简介

随着计算资源的日益增长,以及对性能和效率的不断追求,浮点数在科学计算、机器学习和图形处理等领域扮演着越来越重要的角色。在这些应用中,浮点数的精度和性能成为了关键因素。传统的单精度浮点数(FP32)虽然广泛使用,但在某些情况下,其精度可能不足以满足需求,而双精度浮点数(FP64)虽然精度更高,却会显著增加计算成本。
在这样的背景下,半精度浮点数(FP16)因其在保持相对高精度的同时能够显著提高计算速度和降低存储需求,开始受到关注。C++作为一种功能强大的编程语言,提供了对FP16和FP32浮点数的原生支持,使得开发者能够灵活地在这两种精度之间进行选择和转换。
本文将深入探讨如何在C++中实现和优化FP16和FP32的算法。首先,我们将介绍浮点数的基本概念,包括它们的表示方法和运算规则。接着,我们将详细讨论FP16和FP32的特点,以及它们在不同场景下的应用。此外,文章还将提供实际的C++代码示例,展示如何在C++中进行FP16和FP32的运算,以及如何利用C++的特性来提高算法的效率。
通过本文,读者将能够理解FP16和FP32在实际编程中的区别和联系,掌握在C++中实现这两种精度浮点数算法的方法,以及如何根据项目需求选择合适的浮点数精度。无论是在学术研究还是工业应用中,这些知识都将为读者提供宝贵的指导。

2 代码实现

参考了RK提供的类型转换代码

#ifndef _RKNN_APP_TYPE_HALF_H_
#define _RKNN_APP_TYPE_HALF_H_

typedef unsigned short half;
typedef unsigned short ushort;
typedef unsigned int uint;

uint as_uint(const float x) {
    return *(uint*)&x;
}
float as_float(const uint x) {
    return *(float*)&x;
}


float half_to_float(half 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 digits
    // printf("1\n");
    const uint e = (x&0x7C00)>>10; // exponent
    // printf("2\n");
    const uint m = (x&0x03FF)<<13; // mantissa
    // printf("3\n");
    const uint v = as_uint((float)m)>>23; // evil log2 bit hack to count leading zeros in denormalized format
    // printf("4\n");
    return 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
}


typedef union suf32
{
  int      i;
  unsigned u;
  float    f;
} suf32;

half float_to_half(float x)
  {
    suf32 in;
    in.f          = x;
    unsigned sign = in.u & 0x80000000;
    in.u ^= sign;
    ushort w;

    if (in.u >= 0x47800000)
      w = (ushort)(in.u > 0x7f800000 ? 0x7e00 : 0x7c00);
    else {
      if (in.u < 0x38800000) {
        in.f += 0.5f;
        w = (ushort)(in.u - 0x3f000000);
      } else {
        unsigned t = in.u + 0xc8000fff;
        w          = (ushort)((t + ((in.u >> 13) & 1)) >> 13);
      }
    }

    w = (ushort)(w | (sign >> 16));

    return w;
}



void float_to_half_array(float *src, half *dst, int size)
{
    for (int i = 0; i < size; i++)
    {
        dst[i] = float_to_half(src[i]);
    }
}

void half_to_float_array(half *src, float *dst, int size)
{
    for (int i = 0; i < size; i++)
    {
        dst[i] = half_to_float(src[i]);
    }
}

#endif
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FP-growth算法是一种非常常用的关联分析算法,可以用于挖掘数据集中的频繁项集,进而发现数据集中不同项之间的关联关系。C++作为一种高效的编程语言,也可以用于实现FP-growth算法。 下面是一个基于C++类的FP-growth算法实现及案例示例: ```cpp #include <iostream> #include <fstream> #include <algorithm> #include <vector> #include <map> using namespace std; class Node { public: string name; int count; Node* parent; map<string, Node*> children; Node() { name = ""; count = 0; parent = NULL; } Node(string name, int count) { this->name = name; this->count = count; parent = NULL; } void inc(int num) { count += num; } }; class FPTree { public: Node* root; map<string, int> headerTable; FPTree() { root = new Node(); headerTable.clear(); } void insert(vector<string>& transaction) { Node* cur = root; for (int i = 0; i < transaction.size(); i++) { string item = transaction[i]; if (cur->children.count(item) == 0) { cur->children[item] = new Node(item, 1); cur->children[item]->parent = cur; if (headerTable.count(item) == 0) { headerTable[item] = 1; } else { headerTable[item]++; } } else { cur->children[item]->count++; } cur = cur->children[item]; } } }; class FPGrowth { public: FPTree* tree; map<string, int> items; vector<vector<string>> transactions; FPGrowth() { tree = NULL; } void loadTransactions(string filename) { ifstream fin(filename); if (!fin.is_open()) { return; } string line; while (getline(fin, line)) { vector<string> transaction; string item; for (int i = 0; i < line.size(); i++) { if (line[i] == ' ') { if (items.count(item) == 0) { items[item] = 1; } else { items[item]++; } transaction.push_back(item); item = ""; } else { item += line[i]; } } if (!item.empty()) { if (items.count(item) == 0) { items[item] = 1; } else { items[item]++; } transaction.push_back(item); } transactions.push_back(transaction); } fin.close(); } bool cmp(const pair<string, int>& a, const pair<string, int>& b) { return a.second > b.second; } void buildTree() { tree = new FPTree(); for (int i = 0; i < transactions.size(); i++) { vector<string>& transaction = transactions[i]; sort(transaction.begin(), transaction.end(), [&](string a, string b) { return items[a] > items[b]; }); tree->insert(transaction); } } void findPrefixPath(string item, Node* node, vector<Node*>& prefixPath) { while (node != tree->root) { if (node->name == item) { prefixPath.push_back(node); } node = node->parent; } } void mineFrequentItemsets(int minSup) { vector<pair<string, int>> freqItems; for (auto it = items.begin(); it != items.end(); it++) { if (it->second >= minSup) { freqItems.push_back(*it); } } sort(freqItems.begin(), freqItems.end(), cmp); for (int i = 0; i < freqItems.size(); i++) { vector<string> prefix; prefix.push_back(freqItems[i].first); int sup = freqItems[i].second; findPrefixPaths(prefix, tree->headerTable, sup); } } void findPrefixPaths(vector<string>& prefix, map<string, Node*> headerTable, int sup) { string item = prefix[prefix.size() - 1]; Node* node = headerTable[item]->parent; vector<Node*> prefixPath; while (node != tree->root) { prefixPath.clear(); findPrefixPath(item, node, prefixPath); vector<string> subPrefix; for (int i = 0; i < prefix.size() - 1; i++) { subPrefix.push_back(prefix[i]); } subPrefix.push_back(node->name); int count = node->count; for (int i = 0; i < prefixPath.size(); i++) { count = min(count, prefixPath[i]->count); } if (count >= sup) { cout << "{"; for (int i = 0; i < subPrefix.size(); i++) { cout << subPrefix[i] << " "; } cout << item << "} : " << count << endl; findPrefixPaths(subPrefix, node->children, sup); } node = node->parent; } } }; int main() { FPGrowth fpg; fpg.loadTransactions("transactions.txt"); fpg.buildTree(); fpg.mineFrequentItemsets(2); return 0; } ``` 上述代码实现了一个基于类的FP-growth算法,并且支持从文件中加载交易数据,并挖掘出频繁项集。其中,`Node`类表示FP树中的节点,`FPTree`类表示FP树,`FPGrowth`类表示FP-growth算法。具体实现细节可以参考代码注释。 需要注意的是,本示例中的实现仅支持从文件中加载交易数据,并不支持在线实时插入交易数据,如果需要支持在线插入数据,需要对代码进行一定的修改。另外,本示例中的实现也没有进行过多的优化,不适用于大型数据集的挖掘。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值