mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-09-30 04:03:44 +08:00
0b2c431d3a
version: sunnypilot v2026.003.000 (dev)
date: 2026-09-14T15:43:39
master commit: a5f44653d7
17 lines
613 B
Python
17 lines
613 B
Python
from tinygrad import Tensor
|
|
|
|
def bit_extract(x: Tensor, e: int, s: int) -> Tensor:
|
|
mask = (1 << (e - s + 1)) - 1
|
|
return (x >> s) & mask
|
|
|
|
def u16_to_f16(x: Tensor) -> Tensor:
|
|
sign = bit_extract(x, 15, 15).bool()
|
|
exponent = bit_extract(x, 14, 10).float()
|
|
fraction = bit_extract(x, 9, 0).float()
|
|
return sign.where(-1, 1) * exponent.bool().where((exponent - 15.0).exp2() * (1 + fraction / 1024.0), 6.103515625e-5 * (fraction / 1024.0))
|
|
|
|
def u32_to_f16(oo: Tensor) -> Tensor:
|
|
f1 = u16_to_f16(oo>>16)
|
|
f2 = u16_to_f16(oo&0xFFFF)
|
|
return Tensor.cat(f2.reshape(-1, 1), f1.reshape(-1, 1), dim=1).flatten()
|