VectorBlox VNNX Conversion Issues: Removing Clip and ScatterND Operators
Published:
When converting AI models to VectorBlox’s .vnnx format, some operators1 are not supported in VNNX, causing conversion errors. In such cases, these operators must be replaced with VNNX-supported operators.
1. Clip
Problem
The Clip operator generated during PyTorch to ONNX conversion is incompatible with VNNX.
Solution
- The
Clipoperator clips each element of the input tensor between a specified minimum (min) and maximum (max) value. The formula is:
- When converting from PyTorch to ONNX, find the part that creates the
Clipoperator, i.e., findtorch.clampin PyTorch. If found, replace it by combiningminandmaxoperations instead oftorch.clampin PyTorch.
Code Before/After Comparison
# Before (problem occurs)
bbox[:,:,0] = torch.clamp(x - w/2, 0, image_size[0] - 1)
# After
bbox[:,:,0] = torch.minimum(torch.maximum(x - w/2, zero), x_max)
2. ScatterND
Problem
The ScatterND operator generated during ONNX conversion is incompatible with VNNX.
Solution
ScatterNDcopies a givendatatensor and overwrites values at positions specified byindiceswithupdatesto create a new tensor.- However, ONNX does not have a general assignment operator like
x[:,0] = 1to modify existing values. - Instead, it must create a new tensor by copying the original
xand changing only the 0th column to 1. This is why theScatterNDoperator appears. - Instead of modifying specific positions, use
torch.stackto stack and store operation results. - The
torch.stackoperation is later converted toConcatorUnsqueeze + Concatoperations.
Code Before/After Comparison
# Before (problem occurs)
bbox[:,:,0] = ...
bbox[:,:,1] = ...
# After
x1 = operation result of bbox[:,:,0]
y1 = operation result of bbox[:,:,1]
...
bbox = torch.stack((x1,y1,x2,y2), dim=-1)
Language: 한국어 (Korean)
https://github.com/Microchip-Vectorblox/VectorBlox-SDK/blob/master/docs/OPS.md ↩
