Light AI tools developed by Gang Zhang
Project description
light_deep_ai
: Deep AI modules developed by MOGO RTX team, aims to accelerate the distributed training, int8-aware distributed training, distributed evaluation and inference, model tracing and optimization, and TensorRT deployment.
1 Dependency
torch>=1.10.0
tensorrt>=7.0
graphviz
2 Installation
pip3 install graphviz
apt-get install graphviz
python3 setup.py install
3 Examples
3.1 Graph Tracing and Model Optimization
import torch
import torch.nn as nn
import torch.nn.functional as F
import light_deep_ai
class conv3x3_bn_relu(nn.Module):
def __init__(self, in_planes, out_planes, stride=1, dilation=1, groups=1):
super(conv3x3_bn_relu, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, dilation=dilation, groups=groups, bias=False),
nn.BatchNorm2d(out_planes),
nn.ReLU(inplace=True)
)
def forward(self, x):
x1 = self.net(x)
return x1
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.net = nn.Sequential(
conv3x3_bn_relu(64, 64),
conv3x3_bn_relu(64, 64)
)
def forward(self, x):
x1 = self.net(x)
return x1
model = Model()
model.eval()
model.cuda()
input_data = torch.randn(1, 64, 1024, 1024).cuda()
# graph tracing
model_fx = light_deep_ai.graph_tracer.ad_trace.graph_trace(model, function_name=None)
# Model Optimization
# conduct graph tracing in graph_optim_from_module automatically
model_fx_optim = light_deep_ai.graph_tracer.graph_utils.graph_optim_from_module(model, function_name=None, sample_inputs=(input_data,))
3.2 Quantization-Aware Training
import torch
import torch.nn as nn
import torch.nn.functional as F
import light_deep_ai
class conv3x3_bn_relu(nn.Module):
def __init__(self, in_planes, out_planes, stride=1, dilation=1, groups=1):
super(conv3x3_bn_relu, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, dilation=dilation, groups=groups, bias=False),
nn.BatchNorm2d(out_planes),
nn.ReLU(inplace=True)
)
def forward(self, x):
x1 = self.net(x)
return x1
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.net = nn.Sequential(
conv3x3_bn_relu(64, 64),
conv3x3_bn_relu(64, 64)
)
def forward(self, x):
x1 = self.net(x)
return x1
model = Model()
model.eval()
model.cuda()
input_data = torch.randn(1, 64, 1024, 1024).cuda()
# Model Optimization
# conduct graph tracing in graph_optim_from_module automatically
model_fx_optim = light_deep_ai.graph_tracer.graph_utils.graph_optim_from_module(model, function_name=None)
# qat
model_qat = light_deep_ai.quant_lib.quant_utils.prepare_qat(model_fx_optim,
sample_inputs=[input_data],
observe_config_dic=dict(averaging_constant=0.05),
quant_config_dic=dict(quant_min=-127, quant_max=127, is_symmetric=True, is_quant=True),
disable_prefix=[])
# vis model network
light_deep_ai.graph_tracer.vis_model.vis(model_fx_optim, './model_fx_optim.png')
light_deep_ai.graph_tracer.vis_model.vis(model_qat, './model_qat.png')
# qat training
...
3.3 TensorRT Deployment
import torch
import torch.nn as nn
import torch.nn.functional as F
import light_deep_ai
import light_deep_ai_plugin
from light_deep_ai.deploy_lib.convert_trt import InputTensor, torch2trt
class conv3x3_bn_relu(nn.Module):
def __init__(self, in_planes, out_planes, stride=1, dilation=1, groups=1):
super(conv3x3_bn_relu, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, dilation=dilation, groups=groups, bias=False),
nn.BatchNorm2d(out_planes),
nn.ReLU(inplace=True)
)
def forward(self, x):
x1 = self.net(x)
return x1
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.net = nn.Sequential(
conv3x3_bn_relu(64, 64),
conv3x3_bn_relu(64, 64)
)
def forward(self, x):
x1 = self.net(x)
x2 = light_deep_ai_plugin.max_op(x1, dim=1)
return x2
model = Model()
model.eval()
model.cuda()
input_data = torch.randn(1, 64, 1024, 1024).cuda()
# Model Optimization
# conduct graph tracing in graph_optim_from_module automatically
model_fx_optim = light_deep_ai.graph_tracer.graph_utils.graph_optim_from_module(model, function_name=None, sample_inputs=(input_data,))
# TensorRT Deployment
model_trt = torch2trt(
model=model_fx_optim,
input_specs=[InputTensor(input_data, 'input_data')],
output_names=['max_value', 'max_index'],
fp16_mode=True,
#dla_core=0,
strict_type_constraints=True,
explicit_precision=True
)
# vis tensorrt network
light_deep_ai.deploy_lib.tools.vis_trt.vis(model_trt.network, 'test.png')
error = model(input_data)[0] - model_trt(input_data)[0]
print(error.abs().max())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distributions
Close
Hashes for light_deep_ai-1.5.2-py311-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f63faad246282be69585139b893283b3d1c4dc9edc0c3840167850454e34e1ba |
|
MD5 | acb37388b6af5c62393bbfcfc6d78299 |
|
BLAKE2b-256 | 47ed97afc44d2d443cb7fd858814d315d620d13ae1e5444b4d39f981b6277d42 |
Close
Hashes for light_deep_ai-1.5.2-py310-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78431f4a944be92310205017a072519a9f76b3edc7bde09d57d9a34b6833bf68 |
|
MD5 | 68e4f5ccf69c4690204b63a2911e3f30 |
|
BLAKE2b-256 | 5ba166945fd48ebfb95b8b452d21719691539a7d4fc6e9441c792c071e217162 |
Close
Hashes for light_deep_ai-1.5.2-py39-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a011b74729741ada43d73f3cb4a0dbcd507eca4e8f5a03f136bc588d2c8e3632 |
|
MD5 | 4d0f8ecb3bd9da576b0a9fa30f95e4c4 |
|
BLAKE2b-256 | b47a80e6497c3a12942112f86889e276648177fd5014df6db20a6c31450b1fc6 |
Close
Hashes for light_deep_ai-1.5.2-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dd72d77d9e0a80a84ed8fa683795dec9a256f867ff0bb07aac267009b7b9bf0 |
|
MD5 | f497d85559207323a9a8700aa6969742 |
|
BLAKE2b-256 | f6a7ff41da4d7f50e4f8c892b4163cc2a9a177c552b7c3fba63fe08830ee6e9d |
Close
Hashes for light_deep_ai-1.5.2-py37-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a542f16a110bf5cb8939b9a1e185220dc7abbc16e79c5920cbb69dcf95390ca |
|
MD5 | eb573264dc72ed6e52cb5dc3d38f9e33 |
|
BLAKE2b-256 | b6e196a5ebecbb9644810204396c634d57823f992a175d0349201e90504e1f66 |
Close
Hashes for light_deep_ai-1.5.2-py36-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b32cb15ee2b831a9aadd320512f7c6f5658ec030be97803e4baba67f270164cf |
|
MD5 | c868f72a18a1a27988f6818cdfcced6c |
|
BLAKE2b-256 | d354a85c4522ddb2f49dd48eb206a0cc2a36e7c044e4442629d26155b0da6358 |