Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Vllm project Vllm CPU Float Convert

From Leeroopedia


Knowledge Sources
Domains CPU_Inference, Numeric_Conversion
Last Updated 2026-02-08 00:00 GMT

Overview

Provides low-level scalar conversion functions between FP32, BF16, and FP16 formats using bit manipulation for CPU platforms without native hardware support.

Description

This header implements four static conversion functions: bf16_to_float and float_to_bf16 use simple bit-shift operations for BF16 conversion, while float_to_fp16 and fp16_to_float implement full IEEE 754 half-precision conversion with proper handling of NaN, Inf, denormalized numbers, and round-to-nearest-even semantics. The FP16 conversion code is derived from the Princeton Vision Group's Marvin project.

Usage

These functions serve as software fallback converters included by other CPU kernel files when hardware FP16/BF16 conversion instructions are unavailable. They are used in the vectorized type abstraction layer across all CPU inference kernels.

Code Reference

Source Location

Signature

static float bf16_to_float(uint16_t bf16);
static uint16_t float_to_bf16(float fp32);
static uint16_t float_to_fp16(float fp32);
static float fp16_to_float(uint16_t fp16);

Import

#include "csrc/cpu/float_convert.hpp"

I/O Contract

Inputs

Name Type Required Description
bf16 uint16_t Yes BF16 value as raw 16-bit unsigned integer (for bf16_to_float)
fp32 float Yes FP32 value for conversion to BF16 or FP16 (for float_to_bf16, float_to_fp16)
fp16 uint16_t Yes FP16 value as raw 16-bit unsigned integer (for fp16_to_float)

Outputs

Name Type Description
return (bf16_to_float) float FP32 representation of the input BF16 value
return (float_to_bf16) uint16_t BF16 representation via truncation of upper 16 bits
return (float_to_fp16) uint16_t FP16 representation with round-to-nearest-even
return (fp16_to_float) float FP32 representation of the input FP16 value

Usage Examples

// Convert BF16 to FP32
uint16_t bf16_val = 0x4049;  // ~3.14 in BF16
float fp32_val = bf16_to_float(bf16_val);

// Convert FP32 to FP16
float x = 1.5f;
uint16_t fp16_val = float_to_fp16(x);

// Round-trip FP32 -> BF16 -> FP32
float original = 2.718f;
uint16_t bf16 = float_to_bf16(original);
float recovered = bf16_to_float(bf16);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment