File size: 1,300 Bytes
43e1177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Lossless integer words stored in the normal finite FP16 encodings."""
import numpy as np


SIGN_BIT = 0x8000
FIRST_NORMAL = 0x0400
NORMAL_PER_SIGN = 0x7800
CAPACITY = 2 * NORMAL_PER_SIGN


def encode_ranks(ranks):
    ranks = np.asarray(ranks, dtype=np.int32)
    if np.any((ranks < 0) | (ranks >= CAPACITY)):
        raise ValueError("Radix word is outside the finite FP16 code space")
    bits = FIRST_NORMAL + ranks + np.where(ranks < NORMAL_PER_SIGN, 0, SIGN_BIT - NORMAL_PER_SIGN)
    return bits.astype(np.uint16).view(np.float16)


def decode_ranks(words):
    halves = words
    if words.dtype != np.float16:
        if not np.isfinite(words).all() or np.any(np.abs(words) > 65504):
            raise RuntimeError("Radix head returned an invalid FP16 word")
        halves = words.astype(np.float16)
        if not np.array_equal(halves, words):
            raise RuntimeError("Radix head returned an inexact FP16 word")
    bits = halves.view(np.uint16).astype(np.int32)
    magnitude = bits & (SIGN_BIT - 1)
    if np.any((magnitude < FIRST_NORMAL) | (magnitude >= FIRST_NORMAL + NORMAL_PER_SIGN)):
        raise RuntimeError("Radix head returned a non-normal FP16 word")
    return magnitude - FIRST_NORMAL + np.where(bits & SIGN_BIT, NORMAL_PER_SIGN, 0)