FP8格式理解解析

官方说明文档

2209.05433v2.pdf (arxiv.org)https://arxiv.org/pdf/2209.05433v2.pdf

与常用浮点数对比

FP8 Binary Interchange Format FP8 consists of two encodings - E4M3 and E5M2, where the name explicitly states the number of exponent (E) and mantissa (M) bits. We use the common term "mantissa" as a synonym for IEEE 754 standard’s trailing significand field (i.e. bits not including the implied leading 1 bit for normal floating point numbers). The recommended use of FP8 encodings is E4M3 for weight and activation tensors, and E5M2 for gradient tensors. While some networks can train with just the E4M3 or the E5M2 type, there are networks that require both types (or must maintain many fewer tensors in FP8). This is consistent with findings in [20, 16], where inference and forward pass of training use a variant of E4M3, gradients in the backward pass of training use a variant of E5M2. FP8 encoding details are specified in Table 1. We use the S.E.M notation to describe binary encodings in the table, where S is the sign bit, E is the exponent field (either 4 or 5 bits containing biased exponent), M is either a 3- or a 2-bit mantissa. Values with a 2 in the subscript are binary, otherwise they are decimal

 Design of these FP8 format followed the principle of staying consistent with IEEE-754 conventions, deviating only if a significant benefit is expected for DL application accuracy. Consequently, the E5M2 format follows the IEEE 754 conventions for exponent and special values and can be viewed as IEEE half precision with fewer mantissa bits (similar to how bfloat16 and TF32 can be viewed as IEEE single precision with fewer bits). This allows for straightforward conversion between E5M2 and IEEE FP16 formats. By contrast, the dynamic range of E4M3 is extended by reclaiming most of the bit patterns used for special values because in this case the greater range achieved is much more useful than supporting multiple encodings for the special values.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言解析HTTP中POST格式form-data需要先理解HTTP协议和POST请求的格式。HTTP协议是一种客户端-服务器协议,用于在Web浏览器和Web服务器之间传输数据。POST请求是HTTP协议中的一种请求方法,用于向服务器提交数据。 在POST请求中,数据可以采用多种格式传输,其中form-data是一种常见的格式。form-data格式将数据分为多个部分,每个部分包含一个名称和一个值,以及可选的其他属性。这些部分之间用boundary分隔符进行分隔。以下是一个示例: ``` POST /example HTTP/1.1 Host: example.com Content-Type: multipart/form-data; boundary=------------------------7d33a816d302b6 --------------------------7d33a816d302b6 Content-Disposition: form-data; name="name" John Doe --------------------------7d33a816d302b6 Content-Disposition: form-data; name="email" john.doe@example.com --------------------------7d33a816d302b6 Content-Disposition: form-data; name="file"; filename="example.txt" Content-Type: text/plain This is an example file. --------------------------7d33a816d302b6-- ``` 在C语言中解析这种格式的POST请求,可以使用以下步骤: 1. 读取HTTP请求头部,获取Content-Type和boundary字段的值。 2. 读取HTTP请求体,按照boundary分隔符将数据分为多个部分。 3. 对于每个部分,解析Content-Disposition头部和数据值。 具体实现可以使用C语言的标准库函数进行字符串处理和文件操作。以下是一个简单的示例代码: ```c #include <stdio.h> #include <string.h> #define MAX_BUF_SIZE 1024 int main() { char boundary[MAX_BUF_SIZE]; char buf[MAX_BUF_SIZE]; char filename[MAX_BUF_SIZE]; char name[MAX_BUF_SIZE]; char value[MAX_BUF_SIZE]; FILE *fp; // 读取HTTP请求头部,获取boundary值 fgets(buf, MAX_BUF_SIZE, stdin); while (fgets(buf, MAX_BUF_SIZE, stdin)) { if (sscanf(buf, "Content-Type: multipart/form-data; boundary=%s", boundary) == 1) { break; } } // 读取HTTP请求体,按照boundary分隔符将数据分为多个部分 while (fgets(buf, MAX_BUF_SIZE, stdin)) { if (strncmp(buf, "--", 2) == 0 && strstr(buf, boundary) != NULL) { // 遇到分隔符,开始解析一个部分 filename[0] = '\0'; name[0] = '\0'; value[0] = '\0'; while (fgets(buf, MAX_BUF_SIZE, stdin)) { if (strncmp(buf, "--", 2) == 0) { // 遇到下一个分隔符,结束解析这个部分 break; } else if (strncmp(buf, "Content-Disposition: ", strlen("Content-Disposition: ")) == 0) { // 解析Content-Disposition头部 if (sscanf(buf, "Content-Disposition: form-data; name=\"%[^\"]\"; filename=\"%[^\"]\"", name, filename) == 2) { // 有文件数据 fp = fopen(filename, "wb"); if (fp == NULL) { perror("fopen"); return 1; } while (fgets(buf, MAX_BUF_SIZE, stdin)) { if (strncmp(buf, "--", 2) == 0) { break; } else { fputs(buf, fp); } } fclose(fp); } else if (sscanf(buf, "Content-Disposition: form-data; name=\"%[^\"]\"", name) == 1) { // 普通表单数据 while (fgets(buf, MAX_BUF_SIZE, stdin)) { if (strncmp(buf, "--", 2) == 0) { break; } else { strcat(value, buf); } } printf("%s: %s\n", name, value); } } } } } return 0; } ``` 这段代码可以解析上传的文件和普通表单数据,并将结果输出到标准输出。在实际应用中,可能需要根据具体的业务逻辑进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值