Skip to main content

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


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

If you're not sure about the file name format, learn more about wheel file names.

light_deep_ai-1.5.9-py311-none-any.whl (138.7 kB view details)

Uploaded Python 3.11

light_deep_ai-1.5.9-py310-none-any.whl (89.5 kB view details)

Uploaded Python 3.10

light_deep_ai-1.5.9-py39-none-any.whl (88.8 kB view details)

Uploaded Python 3.9

light_deep_ai-1.5.9-py38-none-any.whl (89.1 kB view details)

Uploaded Python 3.8

light_deep_ai-1.5.9-py37-none-any.whl (89.1 kB view details)

Uploaded Python 3.7

light_deep_ai-1.5.9-py36-none-any.whl (88.7 kB view details)

Uploaded Python 3.6

File details

Details for the file light_deep_ai-1.5.9-py311-none-any.whl.

File metadata

  • Download URL: light_deep_ai-1.5.9-py311-none-any.whl
  • Upload date:
  • Size: 138.7 kB
  • Tags: Python 3.11
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.5

File hashes

Hashes for light_deep_ai-1.5.9-py311-none-any.whl
Algorithm Hash digest
SHA256 d979759ff6eefa47c97223e7ecdcee3f54393990b862eca945c7dc0f239c64b6
MD5 f604361168c30bef70487050f2ead39f
BLAKE2b-256 d77bd425d77069b017360c9c73cbc35ac9f501f78c7062ba9c78cfddb315756d

See more details on using hashes here.

File details

Details for the file light_deep_ai-1.5.9-py310-none-any.whl.

File metadata

  • Download URL: light_deep_ai-1.5.9-py310-none-any.whl
  • Upload date:
  • Size: 89.5 kB
  • Tags: Python 3.10
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.5

File hashes

Hashes for light_deep_ai-1.5.9-py310-none-any.whl
Algorithm Hash digest
SHA256 3f789df2571a9ba0b79299db3556907753a3ac82f133e562384f21764cefc02d
MD5 68fd6b7b5c211168bf7563c28060f016
BLAKE2b-256 3cac4cc6c87d9786cd0ca269961f2932278157fd91c51961134c3b9adae66289

See more details on using hashes here.

File details

Details for the file light_deep_ai-1.5.9-py39-none-any.whl.

File metadata

  • Download URL: light_deep_ai-1.5.9-py39-none-any.whl
  • Upload date:
  • Size: 88.8 kB
  • Tags: Python 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.5

File hashes

Hashes for light_deep_ai-1.5.9-py39-none-any.whl
Algorithm Hash digest
SHA256 b840ebffcf9caf233508603dfaab087e762a6d651b5965107110187e986b5ccc
MD5 eb80e93f6a2a8327c9d395ea157e33e2
BLAKE2b-256 43a4aaf3c6eddf6e4be08c86d1e62ef08adca551766d66ef04d4e0cd3e3badef

See more details on using hashes here.

File details

Details for the file light_deep_ai-1.5.9-py38-none-any.whl.

File metadata

  • Download URL: light_deep_ai-1.5.9-py38-none-any.whl
  • Upload date:
  • Size: 89.1 kB
  • Tags: Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.5

File hashes

Hashes for light_deep_ai-1.5.9-py38-none-any.whl
Algorithm Hash digest
SHA256 e5536ba154374321e4452b3dd9e088126d11010d77635e9c1b9756cb3617ae19
MD5 2925b952b0e1e7c8e3465a3884fe9fbb
BLAKE2b-256 b755b330e67453323869c37ad9b3180799813147d899ce7cf1d88d39e784ba11

See more details on using hashes here.

File details

Details for the file light_deep_ai-1.5.9-py37-none-any.whl.

File metadata

  • Download URL: light_deep_ai-1.5.9-py37-none-any.whl
  • Upload date:
  • Size: 89.1 kB
  • Tags: Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.5

File hashes

Hashes for light_deep_ai-1.5.9-py37-none-any.whl
Algorithm Hash digest
SHA256 5cd4573ae29678be1b6fec9edd94c4485051b7c94180c50f62ee42a7f601f3c1
MD5 8e37171cb771039215eada7f98864215
BLAKE2b-256 fe9fab7ed0859c0880a11e8ed54fa1cd36c36b1e41fa5aa68d305cd7df007836

See more details on using hashes here.

File details

Details for the file light_deep_ai-1.5.9-py36-none-any.whl.

File metadata

  • Download URL: light_deep_ai-1.5.9-py36-none-any.whl
  • Upload date:
  • Size: 88.7 kB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.5

File hashes

Hashes for light_deep_ai-1.5.9-py36-none-any.whl
Algorithm Hash digest
SHA256 04375809095994028b96b06c5fa3aeab80e19324753bfefaa1430f5e97e2d7fe
MD5 b83251ee492f00262c16e5afaba0c80f
BLAKE2b-256 e76dfeb2aa630b9da38a0a6f054bafe7bce2156d0e0844825d2579d7a088b6c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page