gpt4 book ai didi

c - 将 Python/Matlab 移植到 C 和定点 DSP 处理器——C 也应该是定点的吗?

转载 作者:太空宇宙 更新时间:2023-11-04 01:43:44 25 4
gpt4 key购买 nike

我有一个 Python/Matlab 代码可以进行密集的神经网络计算。此 Python/Matlab 代码最终将移植到多个目标(定点)DSP 处理器平台(ARM、Tensillica 等)。作为中间步骤,Python/Matlab 代码应首先移植到 x86 平台上的 C,从而成为“引用”代码的一部分。

问题是,这个 C 代码应该用普通 float 编写,还是立即在 x86 上用定点数编写它对 future 的 DSP 端口有什么好处

最佳答案

我现在肯定会建议您在定点中实现您的算法。我用来针对 Python 浮点引用实现测试我的定点实现的一个工具是 ctypes直接从 Python 代码调用 C 函数以进行直接比较。

例如,要使用 ctypes,您必须将定点 C 函数编译成共享对象

gcc -shared -std-gnu11 -g -o $(BIN_DIR)/libfm.so src/fxpt_atan2.c

然后在你的 Python 文件中

import scipy as sp
import ctypes as ct

# Bind functions in shared object so they can be called in Python.
fm = ct.CDLL('../build/x86/bin/libfm.so')

# Create floating point reference.
N = 1024
n = sp.arange(N)
x = sp.exp(2*sp.pi*1j*n/N)
phi = sp.arctan2(sp.imag(x), sp.real(x))

# Quantize input and process with fixed-point implementation.
x_q, scale = quantize(x, normalize='pow2')
phi_q = sp.zeros(len(x), dtype=sp.int16)
for n in range(len(x)):
# Call ctypes bound function from shared object.
x_re_q = ct.c_int16(int(sp.real(x_q[n])))
x_im_q = ct.c_int16(int(sp.imag(x_q[n])))
phi_q[n] = fm.fxpt_atan2(x_im_q, x_re_q)

# Compare floating point reference and fixed-point implementation.
print(sp.allclose(phi, phi_q/scale*sp.pi, rtol=1e-3))

关于c - 将 Python/Matlab 移植到 C 和定点 DSP 处理器——C 也应该是定点的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57909044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com