Skip to content

Commit

Permalink
Make TensorShapeVector to use InlinedVector<Int64_t> to reduce on tem…
Browse files Browse the repository at this point in the history
…plate instantiations (#18519)

### Description
Use InlinedVector<int64> instead of <int64_t,5> to reduce on the number
of template instantiations.

### Motivation and Context
The reported size reduction is small, just a few Ks. Just trying it out.
  • Loading branch information
yuslepukhin authored Nov 21, 2023
1 parent 680a526 commit 81a763a
Showing 1 changed file with 11 additions and 38 deletions.
49 changes: 11 additions & 38 deletions include/onnxruntime/core/framework/tensor_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,17 @@
// Licensed under the MIT License.

#pragma once
#include <iosfwd>
#include <vector>

#include <algorithm>
#include <string>
#include <cstring>
#include "core/common/gsl.h"
#include "onnxruntime_config.h"

#ifndef DISABLE_ABSEIL
// Need to include abseil inlined_vector.h header directly here
// as hash tables cause CUDA 10.2 compilers to fail. inlined_vector.h is fine.
#ifdef _MSC_VER
#pragma warning(push)
// C4127: conditional expression is constant
#pragma warning(disable : 4127)
// C4324: structure was padded due to alignment specifier
// Usage of alignas causes some internal padding in places.
#pragma warning(disable : 4324)
#endif

#include <absl/container/inlined_vector.h>

#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif // DISABLE_ABSEIL
#include <iosfwd>
#include <string>
#include <vector>

#include "core/common/gsl.h"
#include "core/common/inlined_containers_fwd.h"
#include "core/common/span_utils.h"
#include "onnxruntime_config.h"

namespace onnxruntime {
#ifdef __GNUC__
Expand All @@ -41,18 +24,10 @@ namespace onnxruntime {

constexpr size_t kTensorShapeSmallBufferElementsSize = 5;

#ifndef DISABLE_ABSEIL
// Use this type to build a shape and then create TensorShape.
using TensorShapeVector = absl::InlinedVector<int64_t, kTensorShapeSmallBufferElementsSize>;
#else
class TensorShapeVector : public std::vector<int64_t> {
using Base = std::vector<int64_t>;

public:
using Base::Base;
};

#endif // DISABLE_ABSEIL
// We opt to re-use a common instantiation instead of a typedef with kTensorShapeSmallBufferElementsSize
// To reduce on binary size.
using TensorShapeVector = InlinedVector<int64_t>;

inline TensorShapeVector ToShapeVector(const gsl::span<const int64_t>& span) {
TensorShapeVector out;
Expand Down Expand Up @@ -194,9 +169,7 @@ class TensorShape {

friend struct ProviderHostImpl; // So that the shared provider interface can access Allocate
};
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

// operator<< to nicely output to a stream
std::ostream& operator<<(std::ostream& out, const TensorShape& shape);

Expand Down

0 comments on commit 81a763a

Please sign in to comment.