当前位置: 首页 > news >正文

AI Edge Torch - PyTorch 模型转换为 TensorFlow Lite 模型 (.tflite)

AI Edge Torch - PyTorch 模型转换为 TensorFlow Lite 模型 {.tflite}

  • 1. AI Edge Torch
    • 1.1. PyTorch Converter
    • 1.2. Generative API
  • 2. Installation
    • 2.1. Update `LD_LIBRARY_PATH` if necessary
  • References

google-ai-edge
https://github.com/google-ai-edge

1. AI Edge Torch

ai-edge-torch
https://github.com/google-ai-edge/ai-edge-torch

AI Edge Torch is a python library that supports converting PyTorch models into a .tflite format, which can then be run with TensorFlow Lite and MediaPipe. This enables applications for Android, iOS and IOT that can run models completely on-device.

AI Edge Torch offers broad CPU coverage, with initial GPU and NPU support. AI Edge Torch seeks to closely integrate with PyTorch, building on top of torch.export() and providing good coverage of Core ATen operators.

AI Edge Torch 可以将 PyTorch 编写的模型直接转换成 TFLite 格式 (.tflite),且有着优异的模型覆盖率和 CPU 性能。

1.1. PyTorch Converter

Here are the steps needed to convert a PyTorch model to a TFLite flatbuffer:

import torch
import torchvision
import ai_edge_torch# Use resnet18 with pre-trained weights.
resnet18 = torchvision.models.resnet18(torchvision.models.ResNet18_Weights.IMAGENET1K_V1)
sample_inputs = (torch.randn(1, 3, 224, 224),)# Convert and serialize PyTorch model to a tflite flatbuffer. Note that we
# are setting the model to evaluation mode prior to conversion.
edge_model = ai_edge_torch.convert(resnet18.eval(), sample_inputs)
edge_model.export("resnet18.tflite")

docs/pytorch_converter/getting_started.ipynb
https://github.com/google-ai-edge/ai-edge-torch/blob/main/docs/pytorch_converter/getting_started.ipynb

docs/pytorch_converter/README.md
https://github.com/google-ai-edge/ai-edge-torch/blob/main/docs/pytorch_converter/README.md

1.2. Generative API

The AI Edge Torch Generative API is a Torch native library for authoring mobile-optimized PyTorch Transformer models, which can be converted to TFLite, allowing users to easily deploy Large Language Models (LLMs) on mobile devices. Users can convert the models using the AI Edge Torch PyTorch Converter, and run them via the TensorFlow Lite runtime.

AI Edge Torch Generative API 可以将开发者用 PyTorch 编写的高性能大语言模型 (LLM) 部署至 TensorFlow Lite (TFLite) 运行时,从而无缝地将新的设备端生成式 AI 模型部署到边缘设备上。

Example transformer models (decoder-only LLMs)
https://github.com/google-ai-edge/ai-edge-torch/tree/main/ai_edge_torch/generative/examples

Mobile app developers can also use the Edge Generative API to integrate PyTorch LLMs directly with the MediaPipe LLM Inference API for easy integration within their application code.

LLM Inference guide - LLM 推断指南
https://ai.google.dev/edge/mediapipe/solutions/genai/llm_inference

AI Edge Torch Generative API
https://github.com/google-ai-edge/ai-edge-torch/tree/main/ai_edge_torch/generative

2. Installation

2.1. Update LD_LIBRARY_PATH if necessary

Torch XLA builds a shared library, _XLAC.so that needs to link to the version of Python it was built with (currently 3.10 or 3.11). In order to ensure that import _XLAC can succeed, update the LD_LIBRARY_PATH to the lib directory of your Python environment:

export LD_LIBRARY_PATH=<path to Python installation>/lib:$LD_LIBRARY_PATH

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] AI Edge Torch, https://github.com/google-ai-edge/ai-edge-torch

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 让一切发生皆有利于我,在人生的长河中,我们常常面临诸多的不确定性和变化
  • AI模型常见的压缩技术分类
  • Prometheus+Grafana保姆笔记(1)——Prometheus+Grafana的安装
  • 【STM32系统】基于STM32设计的按键PWM控制舵机窗帘柜子门禁家居等控制系统——文末资料下载
  • docker上传镜像至阿里云
  • PHP Web服务全攻略:构建与消费的精粹指南
  • easyExcel2.1.6自动trim()的问题
  • 嵌入式八股-面试30题(20240812)
  • RocketMQ 是什么?它的架构是怎样的?和 Kafka 有什么区别?
  • 从分散到集中:TSINGSEE青犀EasyCVR视频汇聚网关在视频整体监控解决方案中的整合作用
  • 抖店飞鸽客服自动回复软件开发教程与下载体验(.NET版)
  • KCTF 闯关游戏:1 ~ 7 关
  • 取消Edge浏览器自带的JSON格式化插件
  • 斯坦福UE4 C++课学习补充19:黑洞技能
  • Vue项目-Table添加Form表单校验
  • JavaScript 如何正确处理 Unicode 编码问题!
  • 《剑指offer》分解让复杂问题更简单
  • 【EOS】Cleos基础
  • ES6 学习笔记(一)let,const和解构赋值
  • Git 使用集
  • GraphQL学习过程应该是这样的
  • javascript 哈希表
  • Java知识点总结(JavaIO-打印流)
  • Mysql5.6主从复制
  • React as a UI Runtime(五、列表)
  • React 快速上手 - 07 前端路由 react-router
  • React-redux的原理以及使用
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • swift基础之_对象 实例方法 对象方法。
  • vue总结
  • 测试开发系类之接口自动化测试
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 聊聊flink的BlobWriter
  • 智能网联汽车信息安全
  • - 转 Ext2.0 form使用实例
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • Linux权限管理(week1_day5)--技术流ken
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • ​【数据结构与算法】冒泡排序:简单易懂的排序算法解析
  • #传输# #传输数据判断#
  • (02)Hive SQL编译成MapReduce任务的过程
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (补)B+树一些思想
  • (每日一问)设计模式:设计模式的原则与分类——如何提升代码质量?
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (一)RocketMQ初步认识
  • (转)Mysql的优化设置
  • *算法训练(leetcode)第四十天 | 647. 回文子串、516. 最长回文子序列
  • .libPaths()设置包加载目录
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .NET 读取 JSON格式的数据
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .NET设计模式(8):适配器模式(Adapter Pattern)
  • .py文件应该怎样打开?