<jupyter_start><jupyter_text>Manually Constructing a TensorRT EngineThe Python API provides a path for Python-based frameworks, which might be unsupported by the UFF converter, if they use NumPy compatible layer weights. For this example, we will use PyTorch. First, we import TensorRT.<jupyter_code>import tensorrt as trt<jupyter_output><empty_output><jupyter_text>We use PyCUDA to transfer data to/from the GPU and NumPy to store data.<jupyter_code>import pycuda.driver as cuda
import pycuda.autoinit
import numpy as np
from matplotlib.pyplot import imshow # to show test case<jupyter_output><empty_output><jupyter_text>Then, we import PyTorch and its various packages.<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable<jupyter_output><empty_output><jupyter_text>Training a Model in PyTorchFor more detailed information about training models in PyTorch, see http://pytorch.org/tutorials/ First, we define hyper-parameters, then create a dataloader, define our network, set our optimizer and define our training and testing steps.<jupyter_code>BATCH_SIZE = 64
TEST_BATCH_SIZE = 1000
EPOCHS = 3
LEARNING_RATE = 0.001
SGD_MOMENTUM = 0.5  
SEED = 1
LOG_INTERVAL = 10
# Enable Cuda
torch.cuda.manual_seed(SEED)
# Dataloader
kwargs = {'num_workers': 1, 'pin_memory': True}
train_loader  = torch.utils.data.DataLoader(
    datasets.MNIST('/tmp/mnist/data', train=True, download=True, 
                    transform=transforms.Compose([
                    transforms.ToTensor(),
                    transforms.Normalize((0.1307,), (0.3081,))
        ])),
    batch_size=BATCH_SIZE,
    shuffle=True,
    **kwargs)

test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('/tmp/mnist/data', train=False, 
                   transform=transforms.Compose([
                   transforms.ToTensor(),
                    transforms.Normalize((0.1307,), (0.3081,))
        ])),
    batch_size=TEST_BATCH_SIZE,
    shuffle=True,
    **kwargs)
# Network
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, kernel_size=5)
        self.conv2 = nn.Conv2d(20, 50, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(800, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        x = F.max_pool2d(self.conv1(x), kernel_size=2, stride=2)
        x = F.max_pool2d(self.conv2(x), kernel_size=2, stride=2)
        x = x.view(-1, 800)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

model = Net()
model.cuda()
optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=SGD_MOMENTUM)
def train(epoch):
    model.train()
    for batch, (data, target) in enumerate(train_loader):
        data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        if batch % LOG_INTERVAL == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'
                  .format(epoch, 
                          batch * len(data), 
                          len(train_loader.dataset), 
                          100. * batch / len(train_loader), 
                          loss.data.item()))

def test(epoch):
    model.eval()
    test_loss = 0
    correct = 0
    for data, target in test_loader:
        data, target = data.cuda(), target.cuda()
        with torch.no_grad():
            data, target = Variable(data), Variable(target)
        output = model(data)
        test_loss += F.nll_loss(output, target).data.item()
        pred = output.data.max(1)[1]
        correct += pred.eq(target.data).cpu().sum()
    test_loss /= len(test_loader)
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'
          .format(test_loss, 
                  correct, 
                  len(test_loader.dataset), 
                  100. * correct / len(test_loader.dataset)))<jupyter_output><empty_output><jupyter_text>Now we train the model.<jupyter_code>for e in range(EPOCHS):
    train(e + 1)
    test(e + 1)<jupyter_output><empty_output><jupyter_text>Converting the Model into a TensorRT EngineNow that we have a "trained" model, we extract the layer wieghts by getting the `state_dict`<jupyter_code>weights = model.state_dict()<jupyter_output><empty_output><jupyter_text>Next, we create a builder and a logger for the build process.<jupyter_code>G_LOGGER = trt.infer.ConsoleLogger(trt.infer.LogSeverity.ERROR)
builder = trt.infer.create_infer_builder(G_LOGGER)<jupyter_output><empty_output><jupyter_text>Then, we replicate the network structure above in TensorRT and extract the weights from PyTorch in the form of numpy arrays. The numpy arrays from PyTorch reflect the dimensionality of the layers, so we flatten the arrays<jupyter_code>network = builder.create_network()

# Name for the input layer, data type, tuple for dimension 
data = network.add_input("data", trt.infer.DataType.FLOAT, (1, 28, 28))
assert(data)

#-------------
conv1_w = weights['conv1.weight'].cpu().numpy().reshape(-1)
conv1_b = weights['conv1.bias'].cpu().numpy().reshape(-1)
conv1 = network.add_convolution(data, 20, (5,5),  conv1_w, conv1_b)
assert(conv1)
conv1.set_stride((1,1))

#-------------
pool1 = network.add_pooling(conv1.get_output(0), trt.infer.PoolingType.MAX, (2,2))
assert(pool1)
pool1.set_stride((2,2))

#-------------
conv2_w = weights['conv2.weight'].cpu().numpy().reshape(-1)
conv2_b = weights['conv2.bias'].cpu().numpy().reshape(-1)
conv2 = network.add_convolution(pool1.get_output(0), 50, (5,5), conv2_w, conv2_b)
assert(conv2)
conv2.set_stride((1,1))

#-------------
pool2 = network.add_pooling(conv2.get_output(0), trt.infer.PoolingType.MAX, (2,2))
assert(pool2)
pool2.set_stride((2,2))

#-------------
fc1_w = weights['fc1.weight'].cpu().numpy().reshape(-1)
fc1_b = weights['fc1.bias'].cpu().numpy().reshape(-1)
fc1 = network.add_fully_connected(pool2.get_output(0), 500, fc1_w, fc1_b)
assert(fc1)

#-------------
relu1 = network.add_activation(fc1.get_output(0), trt.infer.ActivationType.RELU)
assert(relu1)

#-------------
fc2_w = weights['fc2.weight'].cpu().numpy().reshape(-1)
fc2_b = weights['fc2.bias'].cpu().numpy().reshape(-1)
fc2 = network.add_fully_connected(relu1.get_output(0), 10, fc2_w, fc2_b)
assert(fc2)<jupyter_output><empty_output><jupyter_text>Now, we need to mark our output layer.<jupyter_code>fc2.get_output(0).set_name("prob")
network.mark_output(fc2.get_output(0))<jupyter_output><empty_output><jupyter_text>We set the rest of the parameters for the network (max batch size and max workspace) and build the engine.<jupyter_code>builder.set_max_batch_size(1)
builder.set_max_workspace_size(1 << 20)

engine = builder.build_cuda_engine(network)
network.destroy()
builder.destroy()<jupyter_output><empty_output><jupyter_text>Now we create the engine runtime and generate a test case from the torch dataloader.<jupyter_code>runtime = trt.infer.create_infer_runtime(G_LOGGER)
img, target = next(iter(test_loader))
img = img.numpy()[0]
target = target.numpy()[0]
%matplotlib inline
img.shape
imshow(img[0])
print("Test Case: " + str(target))
img = img.ravel()<jupyter_output><empty_output><jupyter_text>Then, we create an execution context for the engine.<jupyter_code>context = engine.create_execution_context()<jupyter_output><empty_output><jupyter_text>Next, we allocate memory on the GPU, as well as on the host to hold results after inference. The size of these allocations is the size of the input/expected output * the batch size.<jupyter_code>output = np.empty(10, dtype = np.float32)

# Allocate device memory
d_input = cuda.mem_alloc(1 * img.nbytes)
d_output = cuda.mem_alloc(1 * output.nbytes)<jupyter_output><empty_output><jupyter_text>The engine requires bindings (pointers to GPU memory). PyCUDA lets us do this by casting the results of memory allocations to ints.<jupyter_code>bindings = [int(d_input), int(d_output)]<jupyter_output><empty_output><jupyter_text>We create a cuda stream to run inference.<jupyter_code>stream = cuda.Stream()<jupyter_output><empty_output><jupyter_text>Now, we transfer the data to the GPU, run inference, then transfer the results to the host.<jupyter_code># Transfer input data to device
cuda.memcpy_htod_async(d_input, img, stream)
#execute model 
context.enqueue(1, bindings, stream.handle, None)
# Transfer predictions back
cuda.memcpy_dtoh_async(output, d_output, stream)
# Synchronize threads
stream.synchronize()<jupyter_output><empty_output><jupyter_text>We can use `np.argmax` to get a prediction.<jupyter_code>print("Test Case: " + str(target))
print ("Prediction: " + str(np.argmax(output)))<jupyter_output><empty_output><jupyter_text>We can also save our engine to a file to use later.<jupyter_code>trt.utils.write_engine_to_file("./pyt_mnist.engine", engine.serialize())<jupyter_output><empty_output><jupyter_text>You can load this engine later by using `tensorrt.utils.load_engine`.<jupyter_code>new_engine = trt.utils.load_engine(G_LOGGER, "./pyt_mnist.engine")<jupyter_output><empty_output><jupyter_text>Finally, we clean up our context, engine and runtime.<jupyter_code>context.destroy()
engine.destroy()
new_engine.destroy()
runtime.destroy()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1341012363943781, 'fraction_numerical': 0.02187466976645884, 'mean_word_length': 4.165938864628821, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '443848', 'license': 'MIT', 'n_tokens_mistral': 3354, 'n_tokens_neox': 3234, 'n_words': 712, 'path': 'src/tensorrt/tensorrt-4.0.1.6/manually_construct_tensorrt_engine.ipynb', 'repo_name': 'aimuch/AIplay', 'size': '9463'}