Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <type_traits>
#include "core/framework/tensor_shape.h"
 
namespace onnxruntime {
 
template <typename... Args>
TensorShape make_shape(Args... args) {
  std::initializer_list<int64_t> dims = {args...};
  return TensorShape(dims);
}
 
// This assumes the tensor is optional, and check wether its shape is expected.
#define ASSERT_TENSOR_DIMS(tensor, ...)                                                                          \
  if (tensor != nullptr) {                                                                                       \
    static_assert(std::is_same<decltype(tensor), const Tensor*>::value, "tensor must be a pointer to a Tensor"); \
    const TensorShape& tensor_shape = tensor->Shape();                                                           \
    const TensorShape& expected_shape = make_shape(__VA_ARGS__);                                                 \
    if (tensor_shape != expected_shape) {                                                                        \
      return ORT_MAKE_STATUS(                                                                                    \
          ONNXRUNTIME, INVALID_ARGUMENT, "Input '" #tensor "' is expected to have shape ", expected_shape,       \
          ", got ", tensor_shape);                                                                               \
    }                                                                                                            \
  }
 
#define CHECK_TENSOR_SHAPE ASSERT_TENSOR_DIMS
 
// This assumes the tensor is optional, and check wether its shape is expected.
#define ASSERT_TENSOR_SHAPE(tensor, shape)                                                                       \
  if (tensor != nullptr) {                                                                                       \
    static_assert(std::is_same<decltype(tensor), const Tensor*>::value, "tensor must be a pointer to a Tensor"); \
    static_assert(std::is_same<std::remove_cv_t<std::remove_reference_t<decltype(shape)>>,                       \
                               TensorShape>::value,                                                              \
                  "shape must be or refer to a TensorShape");                                                    \
    const TensorShape& tensor_shape = tensor->Shape();                                                           \
    if (tensor_shape != shape) {                                                                                 \
      return ORT_MAKE_STATUS(                                                                                    \
          ONNXRUNTIME, INVALID_ARGUMENT, "Input '" #tensor "' is expected to have shape ", shape,                \
          ", got ", tensor_shape);                                                                               \
    }                                                                                                            \
  }
 
// This assumes the tensor is optional, and check wether its shape is shape_1 or shape_2 when it is not null.
#define ASSERT_TENSOR_SHAPE_2(tensor, shape_1, shape_2)                                                          \
  if (tensor != nullptr) {                                                                                       \
    static_assert(std::is_same<decltype(tensor), const Tensor*>::value, "tensor must be a pointer to a Tensor"); \
    static_assert(std::is_same<std::remove_cv_t<std::remove_reference_t<decltype(shape_1)>>,                     \
                               TensorShape>::value,                                                              \
                  "shape_1 must be or refer to a TensorShape");                                                  \
    static_assert(std::is_same<std::remove_cv_t<std::remove_reference_t<decltype(shape_2)>>,                     \
                               TensorShape>::value,                                                              \
                  "shape_2 must be or refer to a TensorShape");                                                  \
    const TensorShape& tensor_shape = tensor->Shape();                                                           \
    if (tensor_shape != shape_1 && tensor_shape != shape_2) {                                                    \
      return ORT_MAKE_STATUS(                                                                                    \
          ONNXRUNTIME, INVALID_ARGUMENT, "Input '" #tensor "' is expected to have shape ", shape_1,              \
          " or ", shape_2, ", got ", tensor_shape);                                                              \
    }                                                                                                            \
  }
 
#define ASSERT_TENSOR_DIMENSION(tensor, dim)                                                                     \
  if (tensor != nullptr) {                                                                                       \
    static_assert(std::is_same<decltype(tensor), const Tensor*>::value, "tensor must be a pointer to a Tensor"); \
    const auto tensor_dimensions = tensor->Shape().NumDimensions();                                              \
    if (tensor_dimensions != dim) {                                                                              \
      return ORT_MAKE_STATUS(                                                                                    \
          ONNXRUNTIME, INVALID_ARGUMENT, "Input '" #tensor "' is expected to have " #dim " dimensions, got ",    \
          tensor_dimensions);                                                                                    \
    }                                                                                                            \
  }
 
#define ASSERT_TENSOR_DIMENSION_2_CHOICES(tensor, choice1, choice2)                                              \
  if ((tensor) != nullptr) {                                                                                     \
    static_assert(std::is_same<decltype(tensor), const Tensor*>::value, "tensor must be a pointer to a Tensor"); \
    const auto tensor_dimensions = tensor->Shape().NumDimensions();                                              \
    if (tensor_dimensions != choice1 && tensor_dimensions != choice2) {                                          \
      return ORT_MAKE_STATUS(                                                                                    \
          ONNXRUNTIME, INVALID_ARGUMENT,                                                                         \
          "Input '" #tensor "' is expected to have " #choice1 " or ", #choice2, " dimensions, got ",             \
          tensor_dimensions);                                                                                    \
    }                                                                                                            \
  }
 
#define ASSERT_TENSOR_2D(tensor) ASSERT_TENSOR_DIMENSION(tensor, 2)
#define ASSERT_TENSOR_3D(tensor) ASSERT_TENSOR_DIMENSION(tensor, 3)
#define ASSERT_TENSOR_2D_OR_3D(tensor) ASSERT_TENSOR_DIMENSION_2_CHOICES(tensor, 2, 3)
 
}  // namespace onnxruntime