diff --git a/.clang-tidy b/.clang-tidy index bfd57acf944..de1a0f8d396 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -41,6 +41,12 @@ Checks: >- google-default-arguments, google-global-names-in-headers, google-explicit-constructor, + misc-*, + -misc-const-correctness, + -misc-include-cleaner, + -misc-no-recursion, + -misc-non-private-member-variables-in-classes, + -misc-use-anonymous-namespace, modernize-*, -modernize-concat-nested-namespaces, -modernize-raw-string-literal, diff --git a/onnx/checker.cc b/onnx/checker.cc index 4748681e334..7b674d029d0 100644 --- a/onnx/checker.cc +++ b/onnx/checker.cc @@ -310,10 +310,8 @@ void check_map(const MapProto& map, const CheckerContext& ctx) { // Check that the index data stored in a SparseTensorProto is valid. // indices: a 1-dimensional tensor; indices[i] represents the // linearized index value for the i-th nonzero value. -void check_sparse_tensor_indices_1( - const TensorProto& indices, - const SparseTensorProto& sparse_tensor_proto, - size_t nnz) { +static void +check_sparse_tensor_indices_1(const TensorProto& indices, const SparseTensorProto& sparse_tensor_proto, size_t nnz) { int dense_rank = sparse_tensor_proto.dims_size(); int64_t dense_size = 1; for (int i = 0; i < dense_rank; ++i) @@ -350,10 +348,8 @@ void check_sparse_tensor_indices_1( // Check that the index data stored in a SparseTensorProto is valid. // indices: a 2-dimensional tensor; indices[i,j] represents the j-th // index value for the i-th nonzero value. -void check_sparse_tensor_indices_2( - const TensorProto& indices, - const SparseTensorProto& sparse_tensor_proto, - size_t nnz) { +static void +check_sparse_tensor_indices_2(const TensorProto& indices, const SparseTensorProto& sparse_tensor_proto, size_t nnz) { int dense_rank = sparse_tensor_proto.dims_size(); if (static_cast(indices.dims(0)) != nnz) { fail_check("Sparse tensor indices (", indices.name(), ") first dimension size does not equal NNZ."); @@ -523,7 +519,7 @@ void check_attribute(const AttributeProto& attr, const CheckerContext& ctx, cons } } -void print_warning_if_has_experimental(const std::unordered_set& used_experimental_ops) { +static void print_warning_if_has_experimental(const std::unordered_set& used_experimental_ops) { if (!used_experimental_ops.empty()) { std::string all_experimental_ops; for (const auto& op : used_experimental_ops) { @@ -716,7 +712,9 @@ void check_graph(const GraphProto& graph, const CheckerContext& ctx, const Lexic // Utilify function to get the imported version of domain from opset imports // Returns -1 if requested domain is not found in the opset_imports -int get_version_for_domain(const std::string& domain, const std::unordered_map& opset_imports) { +static int get_version_for_domain( + const std::string& domain, + const std::unordered_map& opset_imports) { auto it = opset_imports.find(domain); if (it == opset_imports.end()) { return -1; @@ -890,7 +888,7 @@ void check_function(const FunctionProto& function, const CheckerContext& ctx, co print_warning_if_has_experimental(used_experimental_ops); } -void check_model(const ModelProto& model, CheckerContext& ctx) { +static void check_model(const ModelProto& model, CheckerContext& ctx) { if (!model.ir_version()) { fail_check("The model does not have an ir_version set properly."); } @@ -1062,7 +1060,7 @@ std::string resolve_external_data_location( #endif } -std::unordered_set experimental_ops = { +static std::unordered_set experimental_ops = { "ATen", "Affine", "ConstantFill", diff --git a/onnx/checker.h b/onnx/checker.h index 86ccfabf104..79a2cf7c848 100644 --- a/onnx/checker.h +++ b/onnx/checker.h @@ -89,7 +89,7 @@ class CheckerContext final { check_custom_domain_ = value; } - explicit CheckerContext() {} + explicit CheckerContext() = default; private: int ir_version_{-1}; diff --git a/onnx/common/ir_pb_converter.cc b/onnx/common/ir_pb_converter.cc index 97dc8d01d70..522d92c74d4 100644 --- a/onnx/common/ir_pb_converter.cc +++ b/onnx/common/ir_pb_converter.cc @@ -20,9 +20,9 @@ namespace ONNX_NAMESPACE { // Part 1: convert ONNX Protobuf to IR -std::unique_ptr graphProtoToGraph(const GraphProto& gp, bool nested, const int ir_version = IR_VERSION); +static std::unique_ptr graphProtoToGraph(const GraphProto& gp, bool nested, const int ir_version = IR_VERSION); -Tensor tensorProtoToTensor(const ONNX_NAMESPACE::TensorProto& tp) { +static Tensor tensorProtoToTensor(const ONNX_NAMESPACE::TensorProto& tp) { Tensor ret; ret.sizes().reserve(tp.dims_size()); @@ -108,7 +108,7 @@ Tensor tensorProtoToTensor(const ONNX_NAMESPACE::TensorProto& tp) { return ret; } -void convertAttribute(const ONNX_NAMESPACE::AttributeProto& ap, Node* n, const int ir_version = IR_VERSION) { +static void convertAttribute(const ONNX_NAMESPACE::AttributeProto& ap, Node* n, const int ir_version = IR_VERSION) { Symbol sym = Symbol(ap.name()); switch (ap.type()) { case ONNX_NAMESPACE::AttributeProto_AttributeType_FLOAT: @@ -193,13 +193,13 @@ void convertAttribute(const ONNX_NAMESPACE::AttributeProto& ap, Node* n, const i } } -void convertAttributes(ONNX_NAMESPACE::NodeProto& np, Node* n, const int ir_version = IR_VERSION) { +static void convertAttributes(ONNX_NAMESPACE::NodeProto& np, Node* n, const int ir_version = IR_VERSION) { for (int i = 0; i < np.attribute_size(); i++) { convertAttribute(np.attribute(i), n, ir_version); } } -std::vector tensorShapeProtoToDimensions(const ONNX_NAMESPACE::TensorShapeProto& tsp) { +static std::vector tensorShapeProtoToDimensions(const ONNX_NAMESPACE::TensorShapeProto& tsp) { std::vector dims; dims.reserve(tsp.dim_size()); for (int i = 0; i < tsp.dim_size(); i++) { @@ -216,7 +216,7 @@ std::vector tensorShapeProtoToDimensions(const ONNX_NAMESPACE::Tensor return dims; } -void createDummyValue( +static void createDummyValue( std::unique_ptr& g, const std::string& name, std::unordered_map& value_by_name_of) { @@ -406,13 +406,13 @@ std::unique_ptr ImportModelProto(const ModelProto& mp) { } // Part 2: convert IR to ONNX Protobuf -std::string value_name(Value* n) { +static std::string value_name(Value* n) { return n->uniqueName(); } -void encodeGraph(GraphProto* p_g, const std::shared_ptr& g); +static void encodeGraph(GraphProto* p_g, const std::shared_ptr& g); -void encodeTensor(ONNX_NAMESPACE::TensorProto* p, const Tensor& tensor) { +static void encodeTensor(ONNX_NAMESPACE::TensorProto* p, const Tensor& tensor) { if (tensor.hasName()) { p->set_name(tensor.name()); } @@ -481,7 +481,7 @@ void encodeTensor(ONNX_NAMESPACE::TensorProto* p, const Tensor& tensor) { } } -void addAttribute(ONNX_NAMESPACE::NodeProto* n_p, Node* n, Symbol name) { +static void addAttribute(ONNX_NAMESPACE::NodeProto* n_p, Node* n, Symbol name) { auto attr = n_p->add_attribute(); attr->set_name(name.toString()); switch (n->kindOf(name)) { @@ -551,7 +551,7 @@ void addAttribute(ONNX_NAMESPACE::NodeProto* n_p, Node* n, Symbol name) { } } -void encodeTypeProtoTensorType(ONNX_NAMESPACE::TypeProto_Tensor* tensor_type, Value* n) { +static void encodeTypeProtoTensorType(ONNX_NAMESPACE::TypeProto_Tensor* tensor_type, Value* n) { if (n->elemType() != 0) { tensor_type->set_elem_type(n->elemType()); } @@ -570,7 +570,7 @@ void encodeTypeProtoTensorType(ONNX_NAMESPACE::TypeProto_Tensor* tensor_type, Va } } -void encodeValueInfo(ONNX_NAMESPACE::ValueInfoProto* v, Value* n) { +static void encodeValueInfo(ONNX_NAMESPACE::ValueInfoProto* v, Value* n) { v->set_name(value_name(n)); if (n->elemType() != 0 || n->has_sizes()) { ONNX_NAMESPACE::TypeProto* t = v->mutable_type(); diff --git a/onnx/cpp2py_export.cc b/onnx/cpp2py_export.cc index 5f75c25f38a..77047dc25ac 100644 --- a/onnx/cpp2py_export.cc +++ b/onnx/cpp2py_export.cc @@ -9,9 +9,10 @@ #include #include #include +#include #include "onnx/checker.h" -#include "onnx/defs/function.h" +#include "onnx/common/ir_pb_converter.h" #include "onnx/defs/parser.h" #include "onnx/defs/printer.h" #include "onnx/defs/schema.h" @@ -45,13 +46,13 @@ static std::string ProtoBytesToText(const py::bytes& bytes) { return ProtoToString(proto); } -template ::type> -std::pair, std::unordered_map> ParseProtoFromBytesMap( - std::unordered_map bytesMap) { +template > +static std::pair, std::unordered_map> ParseProtoFromBytesMap( + const std::unordered_map& bytesMap) { std::unique_ptr values(new Ts[bytesMap.size()]); std::unordered_map result; size_t i = 0; - for (auto kv : bytesMap) { + for (const auto& kv : bytesMap) { ParseProtoFromPyBytes(&values[i], kv.second); result[kv.first] = &values[i]; i++; @@ -59,12 +60,12 @@ std::pair, std::unordered_map> ParseProto return std::make_pair(std::move(values), result); } -std::unordered_map CallNodeInferenceFunction( +static std::unordered_map CallNodeInferenceFunction( OpSchema* schema, const py::bytes& nodeBytes, - std::unordered_map valueTypesByNameBytes, - std::unordered_map inputDataByNameBytes, - std::unordered_map inputSparseDataByNameBytes, + const std::unordered_map& valueTypesByNameBytes, + const std::unordered_map& inputDataByNameBytes, + const std::unordered_map& inputSparseDataByNameBytes, std::unordered_map opsetImports, const int irVersion) { NodeProto node{}; @@ -244,7 +245,7 @@ PYBIND11_MODULE(onnx_cpp2py_export, onnx_cpp2py_export) { py::init([](std::string name, std::string domain, int since_version, - std::string doc, + const std::string& doc, std::vector inputs, std::vector outputs, std::vector, std::string>> type_constraints, @@ -586,7 +587,7 @@ PYBIND11_MODULE(onnx_cpp2py_export, onnx_cpp2py_export) { version_converter.doc() = "VersionConverter submodule"; py::register_exception(version_converter, "ConvertError"); - version_converter.def("convert_version", [](const py::bytes& bytes, py::int_ target) { + version_converter.def("convert_version", [](const py::bytes& bytes, const py::int_& target) { ModelProto proto{}; ParseProtoFromPyBytes(&proto, bytes); shape_inference::InferShapes(proto); @@ -659,8 +660,8 @@ PYBIND11_MODULE(onnx_cpp2py_export, onnx_cpp2py_export) { shape_inference.def( "infer_function_output_types", [](const py::bytes& function_proto_bytes, - const std::vector input_types_bytes, - const std::vector attributes_bytes) -> std::vector { + const std::vector& input_types_bytes, + const std::vector& attributes_bytes) -> std::vector { FunctionProto proto{}; ParseProtoFromPyBytes(&proto, function_proto_bytes); @@ -686,7 +687,7 @@ PYBIND11_MODULE(onnx_cpp2py_export, onnx_cpp2py_export) { for (auto& type_proto : output_types) { std::string out; type_proto.SerializeToString(&out); - result.push_back(py::bytes(out)); + result.emplace_back(out); } return result; }); diff --git a/onnx/defs/function.cc b/onnx/defs/function.cc index 4a41d70028a..3938c8a7aa0 100644 --- a/onnx/defs/function.cc +++ b/onnx/defs/function.cc @@ -4,16 +4,14 @@ #include "onnx/defs/function.h" -#include - #include "onnx/defs/schema.h" -namespace ONNX_NAMESPACE { -std::string InteralTensorNameGenerator(const std::string& node_name, const std::string& internal_name) { +static std::string InteralTensorNameGenerator(const std::string& node_name, const std::string& internal_name) { std::string new_name = "Func_" + node_name + internal_name; return new_name; } +namespace ONNX_NAMESPACE { void FunctionExpandHelper( const NodeProto& node, const FunctionProto& func, diff --git a/onnx/defs/generator/defs.cc b/onnx/defs/generator/defs.cc index 3124c1eff52..a5ae8b64f15 100644 --- a/onnx/defs/generator/defs.cc +++ b/onnx/defs/generator/defs.cc @@ -417,7 +417,7 @@ Output: [10, 8, 6] )DOC"; template -inline int64_t +static int64_t compute_output_dim_for_range(const TensorProto* start, const TensorProto* limit, const TensorProto* delta) { if (!start->dims().empty() || !limit->dims().empty() || !delta->dims().empty()) { fail_shape_inference("Input to 'Range' op should be scalars (Tensor with only one element and shape empty)"); diff --git a/onnx/defs/math/defs.cc b/onnx/defs/math/defs.cc index f5206bade41..07c668bc577 100644 --- a/onnx/defs/math/defs.cc +++ b/onnx/defs/math/defs.cc @@ -13,7 +13,7 @@ namespace ONNX_NAMESPACE { -inline void MathOpDataPropagator(DataPropagationContext& ctx, const std::string& op_type) { +static void MathOpDataPropagator(DataPropagationContext& ctx, const std::string& op_type) { const auto input_0 = ctx.getInputData(0); const auto input_1 = ctx.getInputData(1); if (input_0 == nullptr || input_1 == nullptr) { @@ -41,7 +41,7 @@ inline void MathOpDataPropagator(DataPropagationContext& ctx, const std::string& ctx.addOutputData(0, std::move(tsp)); } -std::function MathDocGenerator(const char* name) { +static std::function MathDocGenerator(const char* name) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -481,31 +481,7 @@ max(0,x) + min(0,alpha*(exp(x/alpha)-1)) static float celu_default_alpha = 1.0; -TensorProto ToDimensionOneFloatTensor(float value) { - auto t = ToTensor(std::vector({value})); - t.add_dims(1); - return t; -} - -TensorProto ToDimensionOneTensor(int32_t value) { - auto t = ToTensor(std::vector({value})); - t.add_dims(1); - return t; -} - -TensorProto ToDimensionOneInt64Tensor(int64_t value) { - auto t = ToTensor(std::vector({value})); - t.add_dims(1); - return t; -} - -TensorProto ToDimensionOneInt64Tensor(const std::vector& value) { - auto t = ToTensor(value); - t.add_dims(value.size()); - return t; -} - -bool BuildContextDependentFunctionBodyCelu( +static bool BuildContextDependentFunctionBodyCelu( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { @@ -549,7 +525,7 @@ to the tensor elementwise. static std::string gelu_default_approx = "none"; -bool BuildContextDependentFunctionBodyGelu( +static bool BuildContextDependentFunctionBodyGelu( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { @@ -873,7 +849,7 @@ ONNX_OPERATOR_SET_SCHEMA( // Generate opschema for element-wise ops. Leaves type constraint "T" // unspecified. -std::function ElementwiseMultiOpDocGenerator(const char* name) { +static std::function ElementwiseMultiOpDocGenerator(const char* name) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -960,7 +936,7 @@ When 'min' is greater than 'max', the clip operator sets all the 'input' values the value of 'max'. Thus, this is equivalent to 'Min(max, Max(input, min))'. )DOC"; -bool BuildContextDependentFunctionBodyClip( +static bool BuildContextDependentFunctionBodyClip( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { @@ -1037,7 +1013,7 @@ ONNX_OPERATOR_SET_SCHEMA( .SetContextDependentFunctionBodyBuilder(BuildContextDependentFunctionBodyClip) .TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput)); -std::function +static std::function SoftmaxFamilyDocGenerator(const char* name, const char* description, const char* equation) { return [=](OpSchema& schema) { std::string doc; @@ -2160,7 +2136,7 @@ ONNX_OPERATOR_SET_SCHEMA( } })); -bool BuildContextDependentFunctionBody( +static bool BuildContextDependentFunctionBody( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { @@ -2493,7 +2469,7 @@ ONNX_OPERATOR_SET_SCHEMA( } })); -void einsumShapeInference(ONNX_NAMESPACE::InferenceContext& ctx, std::string const& equation) { +static void einsumShapeInference(ONNX_NAMESPACE::InferenceContext& ctx, std::string const& equation) { // Only accept letters for indices auto is_letter = [](char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }; @@ -2699,7 +2675,7 @@ ONNX_OPERATOR_SET_SCHEMA( einsumShapeInference(ctx, equation); })); -const char* reduction_doc_sce = +static const char* reduction_doc_sce = "Type of reduction to apply to loss: none, sum, mean(default). " "'none': no reduction will be applied, " "'sum': the output will be summed. " @@ -2749,7 +2725,7 @@ Finally, L is optionally reduced: where tensor W is of shape `(N, D1, D2, ..., Dk)` and `W[n][d1][d2]...[dk] = weights[labels[i][d1][d2]...[dk]]`. )DOC"; -bool BuildContextDependentFunctionBodySCE( +static bool BuildContextDependentFunctionBodySCE( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { @@ -3084,7 +3060,7 @@ ONNX_OPERATOR_SET_SCHEMA( updateOutputShape(ctx, output_index, result_shape_proto); })); -std::function CosineSumWindowOpDocGenerator(const char* name) { +static std::function CosineSumWindowOpDocGenerator(const char* name) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( diff --git a/onnx/defs/nn/defs.cc b/onnx/defs/nn/defs.cc index 442f9dff699..e90c8b84dca 100644 --- a/onnx/defs/nn/defs.cc +++ b/onnx/defs/nn/defs.cc @@ -10,7 +10,7 @@ #include "onnx/defs/schema.h" namespace ONNX_NAMESPACE { -const char* pads_doc = +static const char* pads_doc = "Padding for the beginning and ending along each spatial axis, it can take any value greater " "than or equal to 0. The value represent the number of pixels added to the beginning " "and end part of the corresponding axis. `pads` format should be as follow " @@ -18,7 +18,7 @@ const char* pads_doc = "added at the beginning of axis `i` and xi_end, the number of pixels added at " "the end of axis `i`. This attribute cannot be used simultaneously with " "auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis."; -const char* conv_auto_pad_doc = +static const char* conv_auto_pad_doc = "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where " "default value is NOTSET, which means explicit padding is used. " "SAME_UPPER or SAME_LOWER mean pad the input so that " @@ -26,7 +26,7 @@ const char* conv_auto_pad_doc = "The padding is split between the two sides equally or almost equally (depending " "on whether it is even or odd). In case the padding is an odd number, the extra " "padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER."; -const char* conv_transpose_auto_pad_doc = +static const char* conv_transpose_auto_pad_doc = "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where " "default value is NOTSET, which means explicit padding is used. " "SAME_UPPER or SAME_LOWER mean pad the input so that " @@ -35,7 +35,7 @@ const char* conv_transpose_auto_pad_doc = "on whether it is even or odd). In case the padding is an odd number, the extra " "padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER."; -void convPoolShapeInference( +static void convPoolShapeInference( InferenceContext& ctx, bool use_dilation, bool require_kernel_shape, @@ -193,14 +193,14 @@ void convPoolShapeInference( } } -std::vector GetSupportedDataTypesForPoolingOps(bool supports8bit) { +static std::vector GetSupportedDataTypesForPoolingOps(bool supports8bit) { if (supports8bit) { return OpSchema::all_float_types_plus_Xint8_ir4(); } return OpSchema::all_float_types_ir4(); } -std::function PoolOpSchemaGenerator( +static std::function PoolOpSchemaGenerator( const char* name, const char* opName, const char* additionalDescription, @@ -373,7 +373,7 @@ ONNX_OPERATOR_SET_SCHEMA( OpSchema::NonDifferentiable) .TypeConstraint("I", {"tensor(int64)"}, "Constrain index tensor to int64")); -void maxUnpoolShapeInference(InferenceContext& ctx) { +static void maxUnpoolShapeInference(InferenceContext& ctx) { // we need at least two inputs to have a shape for this inference. if (ctx.getNumInputs() != 2 && ctx.getNumInputs() != 3) { fail_type_inference("MaxUnpool op must have either two or three inputs."); @@ -548,7 +548,7 @@ ONNX_OPERATOR_SET_SCHEMA( .TypeConstraint("T2", {"tensor(int64)"}, "Constrain index tensor to int64") .TypeAndShapeInferenceFunction([](InferenceContext& ctx) { maxUnpoolShapeInference(ctx); })); -std::function LpPoolOpSchemaGenerator(const char* name) { +static std::function LpPoolOpSchemaGenerator(const char* name) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -635,7 +635,7 @@ std::function LpPoolOpSchemaGenerator(const char* name) { ONNX_OPERATOR_SET_SCHEMA(LpPool, 22, OpSchema().FillUsing(LpPoolOpSchemaGenerator("LpPool"))); // For ROI pool operations. -void roiPoolTypeShapeInference(InferenceContext& ctx) { +static void roiPoolTypeShapeInference(InferenceContext& ctx) { propagateElemTypeFromInputToOutput(ctx, 0, 0); // rois is the second input. @@ -674,7 +674,7 @@ void roiPoolTypeShapeInference(InferenceContext& ctx) { output_shape->add_dim()->set_dim_value(pooled_shape[1]); } -std::function RoiPoolOpSchemaGenerator(const char* name) { +static std::function RoiPoolOpSchemaGenerator(const char* name) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -729,7 +729,7 @@ std::function RoiPoolOpSchemaGenerator(const char* name) { ONNX_OPERATOR_SET_SCHEMA(MaxRoiPool, 22, OpSchema().FillUsing(RoiPoolOpSchemaGenerator("max"))); -std::function ConvOpSchemaGenerator(const char* filter_desc) { +static std::function ConvOpSchemaGenerator(const char* filter_desc) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -1101,7 +1101,7 @@ ONNX_OPERATOR_SET_SCHEMA( convPoolShapeInference(ctx, true, false, 0, 1); })); -void convTransposeShapeInference(InferenceContext& ctx) { +static void convTransposeShapeInference(InferenceContext& ctx) { propagateElemTypeFromInputToOutput(ctx, 0, 0); // we need at least two inputs to have a shape for this inference. @@ -1244,7 +1244,7 @@ void convTransposeShapeInference(InferenceContext& ctx) { } } -std::function ConvTransposeOpSchemaGenerator(const char* filter_desc) { +static std::function ConvTransposeOpSchemaGenerator(const char* filter_desc) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -1461,7 +1461,7 @@ ONNX_OPERATOR_SET_SCHEMA( })); // For GlobalPool operations. -void globalPoolTypeShapeInference(InferenceContext& ctx) { +static void globalPoolTypeShapeInference(InferenceContext& ctx) { propagateElemTypeFromInputToOutput(ctx, 0, 0); // needs at least one input with shape. @@ -1487,7 +1487,7 @@ void globalPoolTypeShapeInference(InferenceContext& ctx) { } } -std::function GlobalPoolingOpSchemaGenerator(const char* op_type, const char* op) { +static std::function GlobalPoolingOpSchemaGenerator(const char* op_type, const char* op) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -1534,7 +1534,7 @@ ONNX_OPERATOR_SET_SCHEMA( OpSchema().FillUsing(GlobalPoolingOpSchemaGenerator("AveragePool", "average"))); ONNX_OPERATOR_SET_SCHEMA(GlobalMaxPool, 22, OpSchema().FillUsing(GlobalPoolingOpSchemaGenerator("MaxPool", "max"))); -std::function GlobalLpPoolingOpSchemaGenerator(const char* op_type, const char* op) { +static std::function GlobalLpPoolingOpSchemaGenerator(const char* op_type, const char* op) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( @@ -2265,7 +2265,7 @@ ONNX_OPERATOR_SET_SCHEMA( )ONNX", 18)); -void col2imShapeInference(InferenceContext& ctx) { +static void col2imShapeInference(InferenceContext& ctx) { propagateElemTypeFromInputToOutput(ctx, 0, 0); // All inputs shapes are required @@ -2494,7 +2494,7 @@ static const char* LayerNormalization_ver17_doc = R"DOC( for more details please check [the doc](Broadcasting.md). )DOC"; -bool BuildContextDependentFunctionBodyLayerNormalization( +static bool BuildContextDependentFunctionBodyLayerNormalization( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto, @@ -2584,14 +2584,14 @@ bool BuildContextDependentFunctionBodyLayerNormalization( return true; } -bool BuildContextDependentFunctionBodyLayerNormalizationVer17( +static bool BuildContextDependentFunctionBodyLayerNormalizationVer17( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { return BuildContextDependentFunctionBodyLayerNormalization(ctx, schema, functionProto, 17); } -bool BuildContextDependentFunctionBodyLayerNormalizationVer18( +static bool BuildContextDependentFunctionBodyLayerNormalizationVer18( const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { diff --git a/onnx/defs/parser.cc b/onnx/defs/parser.cc index 8121228e576..eb36d3ddf43 100644 --- a/onnx/defs/parser.cc +++ b/onnx/defs/parser.cc @@ -623,7 +623,7 @@ Status OnnxParser::Parse(AttributeProto& attr) { return Parse(attr, name); } -bool IsSingletonAttribute(AttributeProto_AttributeType type) { +static bool IsSingletonAttribute(AttributeProto_AttributeType type) { switch (type) { case AttributeProto_AttributeType_FLOAT: case AttributeProto_AttributeType_INT: @@ -638,7 +638,7 @@ bool IsSingletonAttribute(AttributeProto_AttributeType type) { } } -AttributeProto_AttributeType ToSingletonType(AttributeProto_AttributeType type) { +static AttributeProto_AttributeType ToSingletonType(AttributeProto_AttributeType type) { switch (type) { case AttributeProto_AttributeType_FLOATS: return AttributeProto_AttributeType_FLOAT; diff --git a/onnx/defs/printer.cc b/onnx/defs/printer.cc index 28b1021001b..7204d12570f 100644 --- a/onnx/defs/printer.cc +++ b/onnx/defs/printer.cc @@ -12,7 +12,7 @@ namespace ONNX_NAMESPACE { using StringStringEntryProtos = google::protobuf::RepeatedPtrField; -bool IsValidIdentifier(const std::string& str) { +static bool IsValidIdentifier(const std::string& str) { // Check if str is a valid identifier const char* next_ = str.c_str(); const char* end_ = next_ + str.size(); diff --git a/onnx/defs/reduction/defs.cc b/onnx/defs/reduction/defs.cc index e7d06a7f1aa..5349ac9953b 100644 --- a/onnx/defs/reduction/defs.cc +++ b/onnx/defs/reduction/defs.cc @@ -24,7 +24,7 @@ ONNX_OPERATOR_SET_SCHEMA( ONNX_OPERATOR_SET_SCHEMA(ReduceSum, 13, OpSchema().FillUsing(ReduceOpDynamicAxes("sum", EMPTY_ZERO))); -const char* reduce_sum_square_func_body = R"ONNX( +static const char* reduce_sum_square_func_body = R"ONNX( { data_square = Mul(data, data) reduced = ReduceSum(data_square, axes) @@ -40,7 +40,7 @@ ONNX_OPERATOR_SET_SCHEMA(ReduceMean, 18, OpSchema().FillUsing(ReduceOpDynamicAxe ONNX_OPERATOR_SET_SCHEMA(ReduceProd, 18, OpSchema().FillUsing(ReduceOpDynamicAxes("product", EMPTY_ONE))); -const char* reduce_log_sum_func_body = R"ONNX( +static const char* reduce_log_sum_func_body = R"ONNX( { reduced_sum = ReduceSum(data, axes) reduced = Log (reduced_sum) @@ -52,7 +52,7 @@ ONNX_OPERATOR_SET_SCHEMA( 18, OpSchema().FillUsing(ReduceFunctionOp("log sum", EMPTY_MINUS_INF, reduce_log_sum_func_body))); -const char* reduce_log_sum_exp_func_body = R"ONNX( +static const char* reduce_log_sum_exp_func_body = R"ONNX( { data_double = Cast(data) data_exp = Exp (data_double) @@ -67,7 +67,7 @@ ONNX_OPERATOR_SET_SCHEMA( 18, OpSchema().FillUsing(ReduceFunctionOp("log sum exponent", EMPTY_MINUS_INF, reduce_log_sum_exp_func_body))); -const char* reduce_l1_func_body = R"ONNX( +static const char* reduce_l1_func_body = R"ONNX( { data_abs = Abs(data) reduced = ReduceSum(data_abs, axes) @@ -79,7 +79,7 @@ ONNX_OPERATOR_SET_SCHEMA( 18, OpSchema().FillUsing(ReduceFunctionOp("L1 norm", EMPTY_ZERO, reduce_l1_func_body))); -const char* reduce_l2_func_body = R"ONNX( +static const char* reduce_l2_func_body = R"ONNX( { data_square = Mul(data, data) sum_square = ReduceSum(data_square, axes) @@ -94,7 +94,7 @@ ONNX_OPERATOR_SET_SCHEMA( 18, OpSchema().FillUsing(ReduceFunctionOp("L2 norm", EMPTY_ZERO, reduce_l2_func_body))); -std::function ArgReduceDocGenerator(const char* name) { +static std::function ArgReduceDocGenerator(const char* name) { return [=](OpSchema& schema) { std::string doc; POPULATE_OP_DOC_STR(doc = R"DOC( diff --git a/onnx/defs/reduction/utils.cc b/onnx/defs/reduction/utils.cc index 822feb498c8..051977a995d 100644 --- a/onnx/defs/reduction/utils.cc +++ b/onnx/defs/reduction/utils.cc @@ -10,7 +10,7 @@ namespace ONNX_NAMESPACE { -std::vector GetSupportedDataTypesForReductionOps(bool supports8bit, bool supports_bool) { +static std::vector GetSupportedDataTypesForReductionOps(bool supports8bit, bool supports_bool) { auto data_types = OpSchema::numeric_types_for_math_reduction_ir4(); if (supports8bit) { data_types.emplace_back("tensor(uint8)"); diff --git a/onnx/defs/rnn/defs.cc b/onnx/defs/rnn/defs.cc index a28f2354b02..c0ed3a39381 100644 --- a/onnx/defs/rnn/defs.cc +++ b/onnx/defs/rnn/defs.cc @@ -5,7 +5,7 @@ #include "onnx/defs/schema.h" namespace ONNX_NAMESPACE { -void RNNShapeInference(InferenceContext& ctx) { +static void RNNShapeInference(InferenceContext& ctx) { TensorShapeProto::Dimension num_directions, seq_length, batch_size, hidden_size; auto direction = getAttribute(ctx, "direction", "forward"); @@ -72,7 +72,7 @@ void RNNShapeInference(InferenceContext& ctx) { } } -std::function RNNDocGenerator(const char* /*name*/) { +static std::function RNNDocGenerator(const char* /*name*/) { return [=](OpSchema& schema) { schema.Attr( "direction", diff --git a/onnx/defs/schema.h b/onnx/defs/schema.h index c106f519f36..a8a7ff5a646 100644 --- a/onnx/defs/schema.h +++ b/onnx/defs/schema.h @@ -271,7 +271,7 @@ class OpSchema final { std::string description_; // Formal parameter option. - FormalParameterOption param_option_; + FormalParameterOption param_option_{}; // For variadic parameters, a flag indicating if all parameters must be of // same type @@ -283,7 +283,7 @@ class OpSchema final { // True if this parameter can be an differentiable inputs of Gradient. // Otherwise, using this parameter as an differentiable inputs of Gradient // is prohibited. - DifferentiationCategory differentiation_category_; + DifferentiationCategory differentiation_category_{}; }; enum class SupportType : uint8_t { diff --git a/onnx/defs/sequence/defs.cc b/onnx/defs/sequence/defs.cc index d199d6c9c55..b56499d8124 100644 --- a/onnx/defs/sequence/defs.cc +++ b/onnx/defs/sequence/defs.cc @@ -526,7 +526,7 @@ the input. This operator assumes that processing each sample is independent and could executed in parallel or in any order. Users cannot expect any specific ordering in which each subgraph is computed.)DOC"; -void SequenceMapInferenceFunction(InferenceContext& ctx) { +static void SequenceMapInferenceFunction(InferenceContext& ctx) { auto num_inputs = ctx.getNumInputs(); assert(num_inputs > 0); @@ -576,10 +576,8 @@ void SequenceMapInferenceFunction(InferenceContext& ctx) { } } -bool BuildSequenceMapBodyFunc( - const FunctionBodyBuildContext& ctx, - const OpSchema& schema, - FunctionProto& functionProto) { +static bool +BuildSequenceMapBodyFunc(const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) { schema.BuildFunction(functionProto); // variadic input/outputs will be expanded diff --git a/onnx/defs/shape_inference.cc b/onnx/defs/shape_inference.cc index fc26db0f385..43d4c088536 100644 --- a/onnx/defs/shape_inference.cc +++ b/onnx/defs/shape_inference.cc @@ -44,7 +44,7 @@ void propagateElemTypeFromTensorInputToOutput(InferenceContext& ctx, size_t inpu } } -void propagateElemTypeFromSequenceInputToOutput(InferenceContext& ctx, size_t inputIndex, size_t outputIndex) { +static void propagateElemTypeFromSequenceInputToOutput(InferenceContext& ctx, size_t inputIndex, size_t outputIndex) { auto input_type = ctx.getInputType(inputIndex); if (nullptr == input_type || input_type->value_case() != TypeProto::kSequenceType) { fail_type_inference("Input ", inputIndex, " expected to have sequence type"); @@ -58,7 +58,7 @@ void propagateElemTypeFromSequenceInputToOutput(InferenceContext& ctx, size_t in output_type->mutable_sequence_type()->mutable_elem_type()->CopyFrom(input_seq_type.elem_type()); } -void propagateElemTypeFromOptionalInputToOutput(InferenceContext& ctx, size_t inputIndex, size_t outputIndex) { +static void propagateElemTypeFromOptionalInputToOutput(InferenceContext& ctx, size_t inputIndex, size_t outputIndex) { auto input_type = ctx.getInputType(inputIndex); if (nullptr == input_type || input_type->value_case() != TypeProto::kOptionalType) { fail_type_inference("Input ", inputIndex, " expected to have optional type"); @@ -72,7 +72,7 @@ void propagateElemTypeFromOptionalInputToOutput(InferenceContext& ctx, size_t in output_type->mutable_optional_type()->mutable_elem_type()->CopyFrom(input_opt_type.elem_type()); } -void propagateElemTypeFromMapInputToOutput(InferenceContext& ctx, size_t inputIndex, size_t outputIndex) { +static void propagateElemTypeFromMapInputToOutput(InferenceContext& ctx, size_t inputIndex, size_t outputIndex) { auto input_type = ctx.getInputType(inputIndex); if (nullptr == input_type || input_type->value_case() != TypeProto::kMapType) { fail_type_inference("Input ", inputIndex, " expected to have map type"); @@ -115,7 +115,7 @@ Merge shape information from a source shape into a target shape. * prefer target param over source param if mismatched. * Fail if there are mismatches in number of dimensions or dimension values. */ -void mergeInShapeInfo(const TensorShapeProto& source, TensorShapeProto& target) { +static void mergeInShapeInfo(const TensorShapeProto& source, TensorShapeProto& target) { auto num_source_dims = source.dim_size(); auto num_target_dims = target.dim_size(); if (num_source_dims != num_target_dims) { @@ -183,7 +183,7 @@ void mergeInShapeInfo(const TypeProto_SparseTensor& source, TypeProto_SparseTens /// /// /// destination shape -void UnionShapeInfo(const TensorShapeProto& source_shape, TensorShapeProto& target_shape) { +static void UnionShapeInfo(const TensorShapeProto& source_shape, TensorShapeProto& target_shape) { auto source_rank = source_shape.dim_size(); for (int i = 0; i < source_rank; ++i) { const auto& source_dim = source_shape.dim(i); @@ -214,7 +214,7 @@ void UnionShapeInfo(const TensorShapeProto& source_shape, TensorShapeProto& targ } template -void UnionShapeInfoForTensor(const TensorShapeProto& source_shape, TENSOR_TYPE& target_type) { +static void UnionShapeInfoForTensor(const TensorShapeProto& source_shape, TENSOR_TYPE& target_type) { if (target_type.has_shape()) { TensorShapeProto* target_shape = target_type.mutable_shape(); @@ -233,7 +233,7 @@ void UnionShapeInfo(const TensorShapeProto& source_shape, TypeProto_Tensor& targ UnionShapeInfoForTensor(source_shape, target_type); } -void UnionShapeInfo(const TypeProto_Tensor& source_type, TypeProto_Tensor& target_type) { +static void UnionShapeInfo(const TypeProto_Tensor& source_type, TypeProto_Tensor& target_type) { // The union of a tensor of unknown rank and a tensor of known rank is a tensor of unknown rank. // Hence, if the source_type had unknown rank, we clear the shape of the target_type. // Otherwise, UnionShapeInfoForTensor handles the rest. @@ -244,7 +244,7 @@ void UnionShapeInfo(const TypeProto_Tensor& source_type, TypeProto_Tensor& targe } } -void UnionShapeInfo(const TypeProto_SparseTensor& source_type, TypeProto_SparseTensor& target_type) { +static void UnionShapeInfo(const TypeProto_SparseTensor& source_type, TypeProto_SparseTensor& target_type) { // The union of a tensor of unknown rank and a tensor of known rank is a tensor of unknown rank. // Hence, if the source_type had unknown rank, we clear the shape of the target_type. // Otherwise, UnionShapeInfoForTensor handles the rest. @@ -342,7 +342,7 @@ void UnionTypeInfo(const TypeProto& source_type, TypeProto& target_type) { // sparse input and outputs dense or vice-versa. // If the output value_case is not set, then // the input value_case is propagated. -void propagateTensorElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { +static void propagateTensorElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { if (nullptr == input_type) { fail_type_inference("Input type was null"); } @@ -377,7 +377,7 @@ void propagateTensorElemTypeWithValidation(const TypeProto* input_type, TypeProt } } -void propagateSequenceElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { +static void propagateSequenceElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { if (nullptr == input_type) { fail_type_inference("Input type was null"); } @@ -396,7 +396,7 @@ void propagateSequenceElemTypeWithValidation(const TypeProto* input_type, TypePr } } -void propagateOptionalElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { +static void propagateOptionalElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { if (nullptr == input_type) { fail_type_inference("Input type was null"); } @@ -415,7 +415,7 @@ void propagateOptionalElemTypeWithValidation(const TypeProto* input_type, TypePr } } -void propagateMapElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { +static void propagateMapElemTypeWithValidation(const TypeProto* input_type, TypeProto* output_type) { if (nullptr == input_type) { fail_type_inference("Input type was null"); } diff --git a/onnx/defs/tensor/defs.cc b/onnx/defs/tensor/defs.cc index 242b83bdcaa..929bdc83d3c 100644 --- a/onnx/defs/tensor/defs.cc +++ b/onnx/defs/tensor/defs.cc @@ -790,7 +790,7 @@ result = [ ``` )DOC"; -inline void processSliceInputs(const int64_t input_rank, int64_t& start, int64_t& end, int64_t step) { +static void processSliceInputs(const int64_t input_rank, int64_t& start, int64_t& end, int64_t step) { auto clamp = [](int64_t val, int64_t min, int64_t max) -> int64_t { return (val < min) ? min : (val > max) ? max : val; }; diff --git a/onnx/defs/tensor/utils.cc b/onnx/defs/tensor/utils.cc index a423fa6a24d..8c265417bd6 100644 --- a/onnx/defs/tensor/utils.cc +++ b/onnx/defs/tensor/utils.cc @@ -155,7 +155,7 @@ void resizeShapeInferenceHelper( } } -void resizeShapeInferenceVersioned(InferenceContext& ctx, int opset_version) { +static void resizeShapeInferenceVersioned(InferenceContext& ctx, int opset_version) { propagateElemTypeFromInputToOutput(ctx, 0, 0); if (!hasNInputShapes(ctx, 1)) { return; diff --git a/onnx/shape_inference/implementation.cc b/onnx/shape_inference/implementation.cc index 6d26ff16657..5149ac8a246 100644 --- a/onnx/shape_inference/implementation.cc +++ b/onnx/shape_inference/implementation.cc @@ -72,7 +72,7 @@ inline bool IsOnnxDomainOp(const NodeProto& node, const std::string& op_type) { } // namespace template -void CheckTensorShapesAndTypes(const T& inferred_type, const T& existing_type) { +static void CheckTensorShapesAndTypes(const T& inferred_type, const T& existing_type) { if (inferred_type.elem_type() != TensorProto::UNDEFINED && existing_type.elem_type() != TensorProto::UNDEFINED && existing_type.elem_type() != inferred_type.elem_type()) { std::stringstream ss; @@ -249,7 +249,7 @@ void MaterializeSymbolicShape(TypeProto* inferred_type, SymbolTable& symbol_tabl } } -std::string GetFunctionIdentifier(const FunctionProto& function) { +static std::string GetFunctionIdentifier(const FunctionProto& function) { // Note: Models with IR version < 10 do not have the overload attribute. // However, that will be mapped to an empty identifier. const std::string& overload = function.overload(); @@ -259,7 +259,7 @@ std::string GetFunctionIdentifier(const FunctionProto& function) { return function.domain() + ":" + function.name() + ":" + overload; } -std::string GetFunctionIdentifier(const NodeProto& node) { +static std::string GetFunctionIdentifier(const NodeProto& node) { // Note: Models with IR version < 10 do not have the overload attribute. // However, that will be mapped to an empty identifier. const std::string& overload = node.overload(); @@ -303,7 +303,7 @@ class InferredTypes { }; // Initialize a DataValueMap for a called function from the DataValueMap of the caller -void BindValuesOnCall( +static void BindValuesOnCall( const DataValueMap& caller_map, const NodeProto& caller, DataValueMap& callee_map, @@ -322,7 +322,7 @@ void BindValuesOnCall( } // Update a DataValueMap for a calling function from the DataValueMap of the callee -void BindValuesOnReturn( +static void BindValuesOnReturn( const DataValueMap& callee_map, const FunctionProto& callee, DataValueMap& caller_map, @@ -786,7 +786,7 @@ static void InferShapesImpl( // Either ModelProto or FunctionProto template -std::unordered_map GetOpsetImportsFromProto(const T& proto) { +static std::unordered_map GetOpsetImportsFromProto(const T& proto) { std::unordered_map opset_imports; for (const auto& opset_import : proto.opset_import()) { opset_imports[opset_import.domain()] = static_cast(opset_import.version()); diff --git a/onnx/shape_inference/implementation.h b/onnx/shape_inference/implementation.h index bc410dcb915..4721089bb7c 100644 --- a/onnx/shape_inference/implementation.h +++ b/onnx/shape_inference/implementation.h @@ -29,7 +29,7 @@ using DataValueMap = std::unordered_map; class SymbolTableImpl : public SymbolTable { public: - SymbolTableImpl() {} + SymbolTableImpl() = default; void addFromGraph(const GraphProto& g) override { AddExistingSymbolicDims(g.input()); diff --git a/onnx/version_converter/BaseConverter.h b/onnx/version_converter/BaseConverter.h index a85806a09a7..f503172567a 100644 --- a/onnx/version_converter/BaseConverter.h +++ b/onnx/version_converter/BaseConverter.h @@ -8,19 +8,14 @@ #pragma once -#include - -#include -#include +#include #include #include #include #include #include "onnx/common/ir.h" -#include "onnx/common/ir_pb_converter.h" #include "onnx/defs/schema.h" -#include "onnx/proto_utils.h" #include "onnx/version_converter/adapters/adapter.h" namespace ONNX_NAMESPACE { diff --git a/onnx/version_converter/convert.cc b/onnx/version_converter/convert.cc index 5049b9bb69e..e958d525b2c 100644 --- a/onnx/version_converter/convert.cc +++ b/onnx/version_converter/convert.cc @@ -9,6 +9,8 @@ #include #include +#include "onnx/common/ir_pb_converter.h" + namespace ONNX_NAMESPACE { namespace version_conversion {