Main Content

Half Precision Code Generation Support

To assign a half-precision data type to a number or variable, use the half (Fixed-Point Designer) constructor. A half-precision data type occupies 16 bits of memory, but its floating-point representation enables it to handle wider dynamic ranges than integer or fixed-point data types of the same size. For more information, see Floating-Point Numbers (Fixed-Point Designer).

A subset of MATLAB® functions are supported for use with half-precision inputs. Additionally, some functions support code generation with half-precision data types. C and C++ code generation requires MATLAB Coder™. CUDA® code generation for NVIDIA® GPUs requires GPU Coder™. Supported functions appear in alphabetical order in the following table. MATLAB System object™ supports half-precision data type and MATLAB System (Simulink) block supports half-precision data type with real values. For general information regarding code generation with half precision, see half (Fixed-Point Designer).

FunctionMATLAB Simulation SupportC/C++ Code Generation SupportGPU Code Generation Support
abs
acos
acosh
activations (Deep Learning Toolbox)

Half inputs are cast to single precision and computations are performed in single precision.

Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to 'tensorrt' and set the data type to 'FP16' in coder.DeepLearningConfig.

all
allfinite
and, &
Short-Circuit AND
any
anynan
area  
asin
asinh
atan
atan2
atanh
bar  
barh  
cast

Supported syntax:

cast(_,'half')

cast(_,'like',p)

Supported syntax:

cast(_,'half')

cast(_,'like',p)

Supported syntax:

cast(_,'half')

cast(_,'like',p)

cat

  • Dimension argument must be a constant.

  • Dimension argument cannot be half precision.

  • Dimension argument must be a constant.

  • Dimension argument cannot be half precision.

ceil
cell
chol  
circshift
classify (Deep Learning Toolbox)

Half inputs are cast to single precision and computations are performed in single precision.

Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to 'tensorrt' and set the data type to 'FP16' in coder.DeepLearningConfig.

coder.ceval (Simulink) 
colon, :
complex 
conj
conv
conv2
cos
cosh
cospi
ctranspose
cumsum

  
dot  
double
empty  
eps

Supported syntax:

eps('half')

eps(half(1))

eps('like',half(1))

eps(half(1))

eps(half(1))

eq, ==
exp
expm1
eye

Supported syntax:

eye(_,'half')

eye(_,'like',p)

Supported syntax:

eye(_,'half')

eye(_,'like',p) where p is half precision. Other input arguments cannot be half precision.

Supported syntax:

eye(_,'half')

eye(_,'like',p) where p is half precision. Other input arguments cannot be half precision.

fft 
fft2 
fftn 
fftshift
fix
fixed.interp1 (Fixed-Point Designer) 
fixed.interp2 (Fixed-Point Designer) 
fixed.interp3 (Fixed-Point Designer) 
fixed.interpn (Fixed-Point Designer) 
flintmax

Supported syntax:

flintmax('half')

flintmax('like',half(1))

  
flip

Dimension argument cannot be half precision.

Dimension argument cannot be half precision.

fliplr
flipud
floor
fma (Fixed-Point Designer)

Complex half-precision inputs are not supported.

Complex half-precision inputs are not supported.

Complex half-precision inputs are not supported.

fplot  
ge, >=
gt, >
half (Fixed-Point Designer)
horzcat
hypot
ifft 
ifft2 
ifftn 
ifftshift
imag 
Inf

Supported syntax:

Inf(_,'half')

Inf(_,'like',p)

Supported syntax:

Inf(_,'half')

Inf(_,'like',p)

Supported syntax:

Inf(_,'half')

Inf(_,'like',p)

int16
int32
int64
int8
isa
iscolumn
isempty
isequal
isequaln
isfinite
isfloat (Fixed-Point Designer)
isinf
isinteger
islogical
ismatrix
isnan
isnumeric
isobject

Returns true with half-precision input.

Returns false with half-precision input.

Returns false with half-precision input.

isreal
isrow
isscalar
issorted  
isvector
ldivide
le, <=
length
line  
log
log10
log1p
log2

Two output syntax is not supported.

Two output syntax is not supported.

logical
lt, <
lu  
max
mean
min
minus, -
mldivide, \

Left-hand side must be scalar

  
mod
mrdivide, /

Right-hand side must be scalar

Right-hand side must be scalar

Right-hand side must be scalar

mtimes, *

For GPU Code generation, you can perform half-precision matrix multiplication with real inputs.

NaN

Supported syntax:

NaN(_,'half')

NaN(_,'like',p)

Supported syntax:

NaN(_,'half')

NaN(_,'like',p)

Supported syntax:

NaN(_,'half')

NaN(_,'like',p)

ndims
ne, ~=
not
numel
ones

Supported syntax:

ones(_,'half')

ones(_,'like',p)

Supported syntax:

ones(_,'half')

ones(_,'like',p)

Supported syntax:

ones(_,'half')

ones(_,'like',p)

or, ||
Short-Circuit OR
permute
plot  
plot3  
plotmatrix  
plus, +
pow10 (Fixed-Point Designer)
pow2
power, .^
predict (Deep Learning Toolbox)

Half inputs are cast to single precision and computations are performed in single precision.

Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to 'tensorrt' and set the data type to 'FP16' in coder.DeepLearningConfig.

predictAndUpdateState (Deep Learning Toolbox)

Half inputs are cast to single precision and computations are performed in single precision.

Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to 'tensorrt' and set the data type to 'FP16' in coder.DeepLearningConfig.

prod

Half inputs are cast to single precision and computations are performed in single precision. As a result, saturation behavior differs between single and half inputs:

maxhalf = half.realmax;
isequal(prod([maxhalf 2 0.5]), maxhalf)
ans =

  logical

   1
maxsingle = realmax('single');
isequal(prod([maxsingle 2 0.5]), maxsingle)
ans =

  logical

   0
rdivide
real
realmax

Supported syntax:

realmax('half')

realmax('like',half(1))

  
realmin

Supported syntax:

realmin('half')

realmin('like',half(1))

  
rem
repelem
repmat

Dimension argument cannot be half precision.

Dimension argument cannot be half precision.

reshape

Dimension argument cannot be half precision.

Dimension argument cannot be half precision.

rgbplot  
round

Only one input supported

Only one input supported

Only one input supported

rsqrt (Fixed-Point Designer)

Complex half-precision inputs are not supported

  
scatter  
scatter3  
sign
sin
single
sinh
sinpi
size
sort  
sqrt
squeeze
storedInteger (Fixed-Point Designer)  
sum

Half inputs are cast to single precision and computations are performed in single precision. As a result, saturation behavior differs between single and half inputs:

maxhalfint = half.flintmax;
isequal(sum([maxhalfint, 1, -1]), maxhalfint)
ans =

  logical

   1
maxsingleint = flintmax('single');
isequal(sum([maxsingleint, 1, -1]), maxsingleint)
ans =

  logical

   0
tan
tanh
times, .*
transpose
typecast  
uint16
uint32
uint64
uint8
uminus
uplus
vertcat
xlim  
ylim  
zeros

Supported syntax:

zeros(_,'half')

zeros(_,'like',p)

Supported syntax:

zeros(_,'half')

zeros(_,'like',p)

Supported syntax:

zeros(_,'half')

zeros(_,'like',p)

zlim  

See Also

Related Topics