Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ASSET_LICENSES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The following paths contain byte-identical copies of an original photograph
provided by the project maintainer who took the photograph and authorized its
inclusion and redistribution in this repository under the Apache License 2.0:

- `families/blip2/tests/data/test_img.jpeg`
- `families/clipseg/tests/data/test_img.jpeg`
- `families/chinese_clip/tests/data/test_img.jpeg`
- `families/idefics3/tests/data/test_img.jpeg`
Expand Down
18 changes: 18 additions & 0 deletions families/blip2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# BLIP-2

The native image processor resizes the complete RGB image directly to the
checkpoint's square input. It preserves Pillow's bicubic coefficient and byte
rounding rules. Resize workspace uses one source row, four filtered rows,
axis coefficient tables and the fixed output; it does not allocate a
source-height by output-width intermediate image. This keeps tall, narrow
images from multiplying worker memory by the output width.

Applications accepting untrusted images should enforce source pixel and prompt
byte admission limits before calling the task. Input processing still takes
work proportional to the admitted input; the fixed inference profile and
generation limit do not replace application admission controls.

Reference preprocessing requires Pillow 13.x. Native resizing follows its
shape-dependent pass order, including vertical-first filtering for strongly
anisotropic downsampling, with byte rounding between passes. The native tests
include independent Pillow 13 pixel goldens and a tall-image allocation bound.
4 changes: 4 additions & 0 deletions families/blip2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""blip2 family package."""
210 changes: 210 additions & 0 deletions families/blip2/builders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""BLIP-2 ViT, query-only Q-Former and learned-position OPT computation graphs."""

import numpy as np
import tensorrt as trt

from . import graph as g


class Graph:
def __init__(self, weights, verbose):
self.logger = trt.Logger(trt.Logger.VERBOSE if verbose else trt.Logger.WARNING)
self.builder = trt.Builder(self.logger)
self.net = self.builder.create_network(
1 << int(trt.NetworkDefinitionCreationFlag.STRONGLY_TYPED)
)
self.options = self.builder.create_builder_config()
self.options.builder_optimization_level = 3
self.options.clear_flag(trt.BuilderFlag.TF32)
self.weights = weights

def const(self, x):
x = np.ascontiguousarray(x, dtype=np.float32)
return self.net.add_constant(x.shape, x).get_output(0)

def reshape(self, x, shape):
layer = self.net.add_shuffle(x)
layer.reshape_dims = shape
return layer.get_output(0)

def add(self, a, b):
return self.net.add_elementwise(a, b, trt.ElementWiseOperation.SUM).get_output(0)

def mul(self, a, b):
return self.net.add_elementwise(a, b, trt.ElementWiseOperation.PROD).get_output(0)

def linear(self, x, name):
return g.add_linear(
self.net, x, self.weights[name + ".weight"], self.weights.get(name + ".bias")
)

def norm(self, x, name, eps):
return g.add_layer_norm(
self.net, x, self.weights[name + ".weight"], self.weights[name + ".bias"], eps
)

def attention(self, q, k, v, heads, mask=None):
qlen, width = q.shape
klen = k.shape[0]
dim = width // heads

def split(x, length):
layer = self.net.add_shuffle(x)
layer.reshape_dims = (1, length, heads, dim)
layer.second_transpose = (0, 2, 1, 3)
return layer.get_output(0)

scores = self.net.add_matrix_multiply(
split(q, qlen), trt.MatrixOperation.NONE, split(k, klen), trt.MatrixOperation.TRANSPOSE
).get_output(0)
scores = self.mul(scores, self.const(np.full((1, 1, 1, 1), dim**-0.5)))
if mask is not None:
scores = self.add(scores, mask)
softmax = self.net.add_softmax(scores)
softmax.axes = 8
context = self.net.add_matrix_multiply(
softmax.get_output(0),
trt.MatrixOperation.NONE,
split(v, klen),
trt.MatrixOperation.NONE,
).get_output(0)
layer = self.net.add_shuffle(context)
layer.first_transpose = (0, 2, 1, 3)
layer.reshape_dims = (qlen, width)
return layer.get_output(0)

def finish(self):
plan = self.builder.build_serialized_network(self.net, self.options)
if plan is None:
raise RuntimeError("BLIP-2 engine build failed")
return bytes(plan)


def build_vision(weights, config, verbose=False):
graph = Graph(weights, verbose)
net = graph.net
cfg = config["vision_config"]
width = cfg["hidden_size"]
image = cfg["image_size"]
patch = cfg["patch_size"]
patches = (image // patch) ** 2
length = patches + 1
pixels = net.add_input("pixel_values", trt.float32, (1, 3, image, image))
name = "vision_model.embeddings.patch_embedding"
conv = net.add_convolution_nd(
pixels, width, (patch, patch), weights[name + ".weight"], weights[name + ".bias"]
)
conv.stride_nd = (patch, patch)
layer = net.add_shuffle(conv.get_output(0))
layer.reshape_dims = (width, patches)
layer.second_transpose = (1, 0)
cls = graph.const(weights["vision_model.embeddings.class_embedding"].reshape(1, width))
concat = net.add_concatenation([cls, layer.get_output(0)])
concat.axis = 0
hidden = graph.add(
concat.get_output(0),
graph.const(weights["vision_model.embeddings.position_embedding"].reshape(length, width)),
)
for index in range(cfg["num_hidden_layers"]):
prefix = f"vision_model.encoder.layers.{index}"
normalized = graph.norm(hidden, prefix + ".layer_norm1", cfg["layer_norm_eps"])
qkv = graph.linear(normalized, prefix + ".self_attn.qkv")
q, k, v = [
net.add_slice(qkv, (0, j * width), (length, width), (1, 1)).get_output(0)
for j in range(3)
]
attended = graph.attention(q, k, v, cfg["num_attention_heads"])
hidden = graph.add(hidden, graph.linear(attended, prefix + ".self_attn.projection"))
inner = graph.linear(
graph.norm(hidden, prefix + ".layer_norm2", cfg["layer_norm_eps"]), prefix + ".mlp.fc1"
)
inner = net.add_activation(inner, trt.ActivationType.GELU_ERF).get_output(0)
hidden = graph.add(hidden, graph.linear(inner, prefix + ".mlp.fc2"))
memory = graph.norm(hidden, "vision_model.post_layernorm", cfg["layer_norm_eps"])
cfg = config["qformer_config"]
hidden = graph.norm(
graph.const(
weights["query_tokens"].reshape(config["num_query_tokens"], cfg["hidden_size"])
),
"qformer.layernorm",
cfg["layer_norm_eps"],
)
for index in range(cfg["num_hidden_layers"]):
prefix = f"qformer.encoder.layer.{index}"
for kind in ("attention", "crossattention"):
if kind == "crossattention" and index % cfg["cross_attention_frequency"]:
continue
name = prefix + "." + kind
source = memory if kind == "crossattention" else hidden
q = graph.linear(hidden, name + ".attention.query")
k = graph.linear(source, name + ".attention.key")
v = graph.linear(source, name + ".attention.value")
attended = graph.attention(q, k, v, cfg["num_attention_heads"])
hidden = graph.norm(
graph.add(hidden, graph.linear(attended, name + ".output.dense")),
name + ".output.LayerNorm",
cfg["layer_norm_eps"],
)
inner = graph.linear(hidden, prefix + ".intermediate_query.dense")
inner = net.add_activation(inner, trt.ActivationType.GELU_ERF).get_output(0)
hidden = graph.norm(
graph.add(hidden, graph.linear(inner, prefix + ".output_query.dense")),
prefix + ".output_query.LayerNorm",
cfg["layer_norm_eps"],
)
hidden = graph.linear(hidden, "language_projection")
hidden.name = "image_features"
net.mark_output(hidden)
return graph.finish()


def build_decoder(weights, config, length, verbose=False):
graph = Graph(weights, verbose)
net = graph.net
cfg = config["text_config"]
width = cfg["hidden_size"]
prefix = "language_model.model.decoder"
ids = net.add_input("input_ids", trt.int32, (length,))
valid = net.add_input("attention_mask", trt.int32, (length,))
image = net.add_input("image_rows", trt.float32, (length, width))
image_mask = net.add_input("image_mask", trt.float32, (length, 1))
last = net.add_input("last_index", trt.int32, (1,))
hidden = net.add_gather(
graph.const(weights[prefix + ".embed_tokens.weight"]), ids, 0
).get_output(0)
keep = net.add_elementwise(
graph.const(np.ones((length, 1))), image_mask, trt.ElementWiseOperation.SUB
).get_output(0)
hidden = graph.add(graph.mul(hidden, keep), image)
# All real tokens form a contiguous prefix. OPT positions start at learned row 2.
hidden = graph.add(
hidden, graph.const(weights[prefix + ".embed_positions.weight"][2 : length + 2])
)
mask = graph.reshape(net.add_cast(valid, trt.float32).get_output(0), (1, 1, 1, length))
mask = net.add_elementwise(
graph.const(np.ones((1, 1, 1, 1))), mask, trt.ElementWiseOperation.SUB
).get_output(0)
mask = graph.mul(mask, graph.const(np.full((1, 1, 1, 1), -1e9)))
mask = graph.add(
mask, graph.const(np.triu(np.full((1, 1, length, length), -1e9, np.float32), k=1))
)
for index in range(cfg["num_hidden_layers"]):
name = prefix + f".layers.{index}"
normalized = graph.norm(hidden, name + ".self_attn_layer_norm", 1e-5)
q, k, v = [
graph.linear(normalized, name + ".self_attn." + x)
for x in ("q_proj", "k_proj", "v_proj")
]
attended = graph.attention(q, k, v, cfg["num_attention_heads"], mask)
hidden = graph.add(hidden, graph.linear(attended, name + ".self_attn.out_proj"))
inner = graph.linear(graph.norm(hidden, name + ".final_layer_norm", 1e-5), name + ".fc1")
inner = net.add_activation(inner, trt.ActivationType.RELU).get_output(0)
hidden = graph.add(hidden, graph.linear(inner, name + ".fc2"))
hidden = graph.norm(hidden, prefix + ".final_layer_norm", 1e-5)
selected = net.add_gather(hidden, last, 0).get_output(0)
logits = g.add_linear(net, selected, weights[prefix + ".embed_tokens.weight"])
logits.name = "logits"
net.mark_output(logits)
return graph.finish()
78 changes: 78 additions & 0 deletions families/blip2/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""TensorRT graph operations for the BLIP-2 vision, Q-Former and OPT decoder."""

from __future__ import annotations

import numpy as np
import tensorrt as trt


def add_constant(network, shape, values, dtype=np.float32):
data = np.ascontiguousarray(np.asarray(values, dtype=dtype).reshape(shape))
return network.add_constant(shape, data).get_output(0)


def add_sum(network, left, right):
return network.add_elementwise(left, right, trt.ElementWiseOperation.SUM).get_output(0)


def _as_float32(network, tensor):
"""Cast to fp32 for a reduction's statistics, and remember the way back."""
if tensor.dtype == trt.float32:
return tensor, None
return network.add_cast(tensor, trt.float32).get_output(0), tensor.dtype


def _restore_dtype(network, tensor, original):
if original is None:
return tensor
return network.add_cast(tensor, original).get_output(0)


def add_layer_norm(network, inp, gamma, beta, eps):
source, original = _as_float32(network, inp)
axis = len(inp.shape) - 1
mean = network.add_reduce(source, trt.ReduceOperation.AVG, 1 << axis, True).get_output(0)
centred = network.add_elementwise(source, mean, trt.ElementWiseOperation.SUB).get_output(0)
square = network.add_elementwise(centred, centred, trt.ElementWiseOperation.PROD).get_output(0)
variance = network.add_reduce(square, trt.ReduceOperation.AVG, 1 << axis, True).get_output(0)
width = int(inp.shape[-1])
epsilon = add_constant(network, tuple([1] * len(inp.shape)), np.full((1,), eps))
shifted = network.add_elementwise(variance, epsilon, trt.ElementWiseOperation.SUM).get_output(0)
deviation = network.add_unary(shifted, trt.UnaryOperation.SQRT).get_output(0)
normed = network.add_elementwise(centred, deviation, trt.ElementWiseOperation.DIV).get_output(0)
shape = tuple([1] * (len(inp.shape) - 1) + [width])
scaled = network.add_elementwise(
normed, add_constant(network, shape, gamma), trt.ElementWiseOperation.PROD
).get_output(0)
affine = network.add_elementwise(
scaled, add_constant(network, shape, beta), trt.ElementWiseOperation.SUM
).get_output(0)
return _restore_dtype(network, affine, original)


def add_linear(network, inp, weight, bias=None, dtype=np.float32, *, matmul=None):
"""y = x @ W^T + b, matching torch.nn.Linear's weight layout.

``matmul`` (``(network, input) -> output``) replaces the matrix product, which is how
a quantized projection takes its place; the bias is added the same way either way.
"""
rank = len(inp.shape)
if matmul is not None:
out = matmul(network, inp)
else:
weight = np.ascontiguousarray(np.asarray(weight, dtype=dtype))
shape = (1,) * (rank - 2) + tuple(weight.shape)
constant = add_constant(network, shape, weight.reshape(shape), dtype=dtype)
out = network.add_matrix_multiply(
inp, trt.MatrixOperation.NONE, constant, trt.MatrixOperation.TRANSPOSE
).get_output(0)
if bias is None:
return out
bias_shape = (1,) * (rank - 1) + (-1,)
bias = np.asarray(bias, dtype=dtype).reshape(bias_shape)
return network.add_elementwise(
out, add_constant(network, bias.shape, bias, dtype=dtype), trt.ElementWiseOperation.SUM
).get_output(0)
Loading
Loading