Skip to content
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

Update parserUtils.h #3797

Open
wants to merge 1 commit into
base: release/10.0
Choose a base branch
from
Open
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
53 changes: 20 additions & 33 deletions parsers/common/parserUtils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Copyright (c) 1993-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,13 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TRT_PARSER_UTILS_H
#define TRT_PARSER_UTILS_H

#ifndef PARSER_HELPER_H
#define PARSER_HELPER_H

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <memory>

Expand All @@ -33,8 +32,8 @@

#include "NvInfer.h"

namespace parserutils
{
namespace parserhelper {

#define RETURN_AND_LOG_ERROR_IMPL(ret, message, parserName) \
do \
{ \
Expand All @@ -45,17 +44,15 @@ namespace parserutils
} while (0)

// Helper function to compute unpadded volume of a Dims (1 if 0 dimensional)
inline int64_t volume(const nvinfer1::Dims& d)
{
inline int64_t volume(const nvinfer1::Dims& d) {
int64_t v = 1;
for (int64_t i = 0; i < d.nbDims; i++)
v *= d.d[i];
return v;
}

// Show some debugging output about how much memory is free
inline void printMem(const char* where)
{
inline void printMem(const char* where) {
#if !defined _MSC_VER && !defined __QNX__
const unsigned mb = 1024 * 1024;
auto pages = static_cast<uint64_t>(sysconf(_SC_PHYS_PAGES));
Expand All @@ -72,10 +69,8 @@ inline void printMem(const char* where)
}

// Compute size of datatypes
inline unsigned int elementSize(nvinfer1::DataType t)
{
switch (t)
{
inline unsigned int elementSize(nvinfer1::DataType t) {
switch (t) {
case nvinfer1::DataType::kINT32: return 4;
case nvinfer1::DataType::kFLOAT: return 4;
case nvinfer1::DataType::kHALF: return 2;
Expand All @@ -85,19 +80,16 @@ inline unsigned int elementSize(nvinfer1::DataType t)
return 0;
}

inline std::ostream& operator<<(std::ostream& o, const nvinfer1::Dims& dims)
{
inline std::ostream& operator<<(std::ostream& o, const nvinfer1::Dims& dims) {
o << "[";
for (int i = 0; i < dims.nbDims; i++)
o << (i ? "," : "") << dims.d[i];
o << "]";
return o;
}

inline std::ostream& operator<<(std::ostream& o, nvinfer1::DataType dt)
{
switch (dt)
{
inline std::ostream& operator<<(std::ostream& o, nvinfer1::DataType dt) {
switch (dt) {
case nvinfer1::DataType::kINT32: o << "Int32"; break;
case nvinfer1::DataType::kFLOAT: o << "Float"; break;
case nvinfer1::DataType::kHALF: o << "Half"; break;
Expand All @@ -107,36 +99,31 @@ inline std::ostream& operator<<(std::ostream& o, nvinfer1::DataType dt)
return o;
}

inline nvinfer1::Dims3 getCHW(const nvinfer1::Dims& d)
{
inline nvinfer1::Dims3 getCHW(const nvinfer1::Dims& d) {
assert(d.nbDims >= 3);
return nvinfer1::Dims3(d.d[d.nbDims - 3], d.d[d.nbDims - 2], d.d[d.nbDims - 1]);
}

inline int32_t getC(const nvinfer1::Dims& d)
{
inline int32_t getC(const nvinfer1::Dims& d) {
return getCHW(d).d[0];
}

inline nvinfer1::Dims toDims(int32_t w, int32_t h) noexcept
{
inline nvinfer1::Dims toDims(int32_t w, int32_t h) noexcept {
return nvinfer1::Dims{2, {w, h}};
}

inline int combineIndexDimensions(int batchSize, const nvinfer1::Dims& d)
{
inline int combineIndexDimensions(int batchSize, const nvinfer1::Dims& d) {
int x = batchSize;
for (int i = 0; i < d.nbDims - 3; i++)
x *= d.d[i];
return x;
}

template <typename A, typename B>
inline A divUp(A m, B n)
{
inline A divUp(A m, B n) {
return (m + n - 1) / n;
}

} // namespace parserhelper
} // namespace parserhelper

#endif // PARSER_HELPER_H
#endif // PARSER_HELPER_H