Skip to content

Add 4D transpose for weights #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2020
Merged
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
64 changes: 51 additions & 13 deletions ShapedWeights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,43 +95,81 @@ ShapedWeights::operator nvinfer1::Weights() const
}

template <typename DType>
void transpose2DWeights(ShapedWeights const& weights, nvinfer1::Dims const& new_shape, ShapedWeights* result)
void transpose4DWeights(ShapedWeights const& weights, nvinfer1::Permutation const perm, ShapedWeights* result)
{
nvinfer1::Dims original_shape = weights.shape;
nvinfer1::Dims new_shape = result->shape;
int nbDims = new_shape.nbDims;
DType const* src = reinterpret_cast<DType*>(weights.values);
DType* dst = reinterpret_cast<DType*>(result->values);
int src_stride = weights.shape.d[1];
int dst_stride = result->shape.d[1];
for (int i = 0; i < new_shape.d[0]; ++i)

nvinfer1::Dims expanded_original_shape{4, {1, 1, 1, 1}};
nvinfer1::Dims expanded_new_shape{4, {1, 1, 1, 1}};
nvinfer1::Permutation expanded_perm{0, 1, 2, 3};

int pad = 4 - nbDims;
for (int i = 0; i < nbDims; ++i)
{
expanded_original_shape.d[pad + i] = original_shape.d[i];
expanded_new_shape.d[pad + i] = new_shape.d[i];
expanded_perm.order[pad + i] = perm.order[i] + pad;
}

int src_strides[4] = {1, 1, 1, 1};
int dst_strides[4] = {1, 1, 1, 1};

for (int i = 2; i >= 0; --i)
{
src_strides[i] = expanded_original_shape.d[i + 1] * src_strides[i + 1];
dst_strides[i] = expanded_new_shape.d[i + 1] * dst_strides[i + 1];
}

for (int n = 0; n < expanded_original_shape.d[0]; ++n)
{
for (int j = 0; j < new_shape.d[1]; ++j)
for (int c = 0; c < expanded_original_shape.d[1]; ++c)
{
dst[i * dst_stride + j] = src[j * src_stride + i];
for (int h = 0; h < expanded_original_shape.d[2]; ++h)
{
for (int w = 0; w < expanded_original_shape.d[3]; ++w)
{
int src_index = 0;
int dst_index = 0;
int src_coord[4] = {n, c, h, w};
int dst_coord[4];
for (int i = 0 ; i < 4; ++i)
{
dst_coord[i] = src_coord[expanded_perm.order[i]];
src_index += src_coord[i] * src_strides[i];
dst_index += dst_coord[i] * dst_strides[i];
}
dst[dst_index] = src[src_index];
}
}
}
}
}

bool transposeWeights(ShapedWeights const& weights, nvinfer1::Permutation const& perm, ShapedWeights* result)
{
nvinfer1::Dims shape = weights.shape;
int nbDims = shape.nbDims;
nvinfer1::Dims new_shape;
new_shape.nbDims = shape.nbDims;
for (int d = 0; d < shape.nbDims; ++d)
new_shape.nbDims = nbDims;
for (int d = 0; d < nbDims; ++d)
{
new_shape.d[d] = shape.d[perm.order[d]];
result->shape.d[d] = new_shape.d[d];
}
// TODO: Need to generalize this transpose implementation
assert(perm.order[0] == 1 && perm.order[1] == 0);

if (shape.nbDims == 2)
if (shape.nbDims <= 4)
{
if (weights.type == ::ONNX_NAMESPACE::TensorProto::FLOAT)
{
transpose2DWeights<float>(weights, new_shape, result);
transpose4DWeights<float>(weights, perm, result);
}
else if (weights.type == ::ONNX_NAMESPACE::TensorProto::FLOAT16)
{
transpose2DWeights<uint16_t>(weights, new_shape, result);
transpose4DWeights<uint16_t>(weights, perm, result);
}
else
{
Expand Down