欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 重新编译一个不支持 AVX、AVX2的 TensorFlow 1.15的python3.7的安装包

重新编译一个不支持 AVX、AVX2的 TensorFlow 1.15的python3.7的安装包

2025/6/21 20:05:02 来源:https://blog.csdn.net/babytiger/article/details/148697714  浏览:    关键词:重新编译一个不支持 AVX、AVX2的 TensorFlow 1.15的python3.7的安装包

事情是这样的,以前写过一个图像识别程序,是在python3.7 centos7上实现的,是要求cpu支持avx指令的,但是最近在一台电脑 上部署时发现这台电脑不支持avx的,参才之前的文章

在不支持avx指令集的cpu上部署tensorflow及keras._6133指令集-CSDN博客

但是安装包没有linux下的python版本,得自己编译生成了。这里面问题也不小。

我的系统是ubuntu2404,下载了tensorflow源代码后,配好编译环境,切换到1.5.0分支后各种代码报错,实在是搞不定了,

是的,你完全可以在 Ubuntu 24.04 上编译出一个不带 AVX 的 tensorflow==1.15,然后拷贝到 CentOS 7 上使用,前提是两台机器的 Python 版本、架构一致(通常都是 x86_64)。


✅ 编译逻辑简述

你要做的就是:

  1. 在 Ubuntu 24.04 上编译 TensorFlow 1.15,禁用 AVX、AVX2、FMA 等指令集

  2. 生成 .whl 安装包(比如 tensorflow-1.15.0-cp37-cp37m-linux_x86_64.whl)。

  3. 将该 .whl 拷贝到 CentOS 7 上,用 pip install 安装即可。


✅ 编译准备步骤(Ubuntu 24.04)

1. 安装依赖

sudo apt update
sudo apt install -y python3.7 python3.7-dev python3-pip git zip unzip
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
sudo pip install -U pip setuptools wheel numpy

2. 安装 Bazel(TensorFlow 1.15 兼容 Bazel 0.26.1)

wget https://github.com/bazelbuild/bazel/releases/download/0.26.1/bazel-0.26.1-installer-linux-x86_64.sh
chmod +x bazel-0.26.1-installer-linux-x86_64.sh
./bazel-0.26.1-installer-linux-x86_64.sh

3. 下载 TensorFlow 1.15 源码

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r1.15

4. 配置编译环境(重点是禁用 AVX)

./configure

关键配置回答如下:

Do you wish to build TensorFlow with CUDA support? [y/N]: n
...
Please specify optimization flags to use during compilation: 
→ 输入:`-march=core2 -mno-avx -mno-avx2 -mno-fma`

说明:

  • -march=core2 是一组兼容最基础 x86_64 + SSE2 的优化

  • 关闭 AVX/AVX2/FMA 以保证能运行在老旧 CPU 上

5. 开始构建 .whl 安装包(耗时约 1 小时)

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
./bazel-bin/tensorflow/tools/pip_package/build_pip_package ./output

生成的文件类似:

./output/tensorflow-1.15.0-cp37-cp37m-linux_x86_64.whl

✅ 拷贝并安装到 CentOS 7 上

  1. 拷贝 .whl 到 CentOS(如用 scp):

scp tensorflow-1.15.0-*.whl user@centos7:/tmp/
  1. 在 CentOS 7 上安装:

pip install /tmp/tensorflow-1.15.0-*.whl

🧠 注意事项

  • TensorFlow 的 .whl 与 Python 版本和平台强相关(确保都是 Python 3.7x86_64)。

  • 不要用 -march=native,否则仍会编译进 AVX 指令。

  • 编译机器的系统版本(Ubuntu 24.04)与目标运行环境(CentOS 7)可以不同,但glibc 版本不能太新,否则运行时可能报错(可通过 ldd --version 检查 glibc 版本)。

后来发现,推荐用docker拉取一个低版本的ubuntu2004来实现

FROM ubuntu:20.04ENV DEBIAN_FRONTEND=noninteractive# 使用清华 APT 源(可选)
RUN sed -i 's|http://archive.ubuntu.com/ubuntu/|http://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g' /etc/apt/sources.list && \sed -i 's|http://security.ubuntu.com/ubuntu/|http://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g' /etc/apt/sources.list# 安装构建工具和 GCC 7
RUN apt-get update && \apt-get install -y \build-essential \gcc-7 g++-7 \curl git zip unzip \openjdk-8-jdk \zlib1g-dev \libffi-dev \libssl-dev \libsqlite3-dev \wget \patch \ca-certificates && \ln -sf /usr/bin/gcc-7 /usr/bin/gcc && \ln -sf /usr/bin/g++-7 /usr/bin/g++ && \ln -sf /usr/bin/python3.8 /usr/bin/python && \curl -sS https://bootstrap.pypa.io/get-pip.py | python
# 安装python 我用的是Anaconda3-2019.10-Linux-x86_64.sh# 安装 Bazel(兼容 TensorFlow 1.15)
RUN wget https://github.com/bazelbuild/bazel/releases/download/0.26.1/bazel-0.26.1-installer-linux-x86_64.sh && \chmod +x bazel-0.26.1-installer-linux-x86_64.sh && \./bazel-0.26.1-installer-linux-x86_64.sh && \rm -f bazel-0.26.1-installer-linux-x86_64.sh# 克隆 TensorFlow 源码
WORKDIR /workspace
RUN git clone https://github.com/tensorflow/tensorflow.git && \cd tensorflow && git checkout v1.15.0WORKDIR /workspace/tensorflow# 配置环境
RUN yes "" | python configure.py# 编译 TensorFlow(禁用 AVX)
RUN bazel build --config=opt \--copt=-march=native \--copt=-Wno-sign-compare \--copt=-mno-avx \--copt=-mno-avx2 \--copt=-mfpmath=387 \//tensorflow/tools/pip_package:build_pip_package# 打包为 whl
RUN ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkgCMD ["bash"]

 之后要安装

pip install numpy keras_preprocessing keras_applications wheel

进入容器

docker exec -it ubuntu_rapb-ubuntu_rApb-1 bash

将容器保存在文件

docker commit ubuntu5000 tempimage | xargs -I {} docker save {} | gzip > ubuntu5000.tar.gz

文件迁移到另外的机器上 

## ✅ 步骤一:使用 `docker load` 载入镜像 
docker load -i ubuntu5000.tar 
docker load < ubuntu5000.tar.gz
加载成功后,你会看到类似输出:Loaded image: ubuntu5000-image:latest ## ✅ 步骤二:验证镜像是否导入成功docker images  ## ✅ 步骤三:使用导入的镜像运行容器(如带端口和自动运行脚本)docker run -d \-p 5000:5000 \--name my-restored-container \-w /opt/mobile/src \ubuntu5000-image:latest \/root/anaconda3/bin/python NewMobileNetv2_ImageClassificationService.py

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词