Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Onnx Onnx MakeAttribute

From Leeroopedia


Knowledge Sources
Domains Attribute Construction, Protobuf Utilities
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for creating ONNX AttributeProto objects from typed values provided by the ONNX library.

Description

The onnx/defs/attr_proto_util.h header declares factory functions for creating AttributeProto protobuf objects in a type-safe and convenient manner.

The header provides two families of functions:

MakeAttribute is a set of overloaded functions that create AttributeProto objects from an attribute name and a typed value. Scalar overloads accept: float, int64_t, int, std::string, TensorProto, GraphProto, and TypeProto. Vector overloads accept: std::vector<float>, std::vector<int64_t>, std::vector<std::string>, std::vector<TensorProto>, std::vector<GraphProto>, and std::vector<TypeProto>. Each overload is marked ONNX_API for shared library export. The function sets the appropriate attribute type field and value in the returned AttributeProto.

MakeRefAttribute creates reference attributes used in function body node definitions. These reference attributes link a function body node's attribute to the parent function's attribute by name. Two overloads exist:

  • MakeRefAttribute(attr_name, type) - creates a reference where the function body node and the parent function use the same attribute name
  • MakeRefAttribute(attr_name, referred_attr_name, type) - creates a reference where the function body node uses attr_name but refers to the parent function's referred_attr_name

Both MakeRefAttribute overloads take an AttributeProto_AttributeType to specify the attribute's type.

Usage

Use MakeAttribute when programmatically constructing ONNX nodes and need to create typed attributes. Use MakeRefAttribute when defining function body nodes that need to reference attributes from the enclosing function definition. These functions eliminate the boilerplate of manually setting protobuf fields for attribute creation.

Code Reference

Source Location

Signature

namespace ONNX_NAMESPACE {

// Scalar attribute constructors
ONNX_API AttributeProto MakeAttribute(std::string attr_name, float value);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, int64_t value);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, int value);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, std::string value);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, TensorProto value);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, GraphProto value);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, TypeProto value);

// Vector attribute constructors
ONNX_API AttributeProto MakeAttribute(std::string attr_name, std::vector<float> values);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, std::vector<int64_t> values);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, std::vector<std::string> values);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, std::vector<TensorProto> values);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, std::vector<GraphProto> values);
ONNX_API AttributeProto MakeAttribute(std::string attr_name, std::vector<TypeProto> values);

// Reference attribute constructors (for function bodies)
AttributeProto MakeRefAttribute(const std::string& attr_name, AttributeProto_AttributeType type);
AttributeProto MakeRefAttribute(
    const std::string& attr_name,
    const std::string& referred_attr_name,
    AttributeProto_AttributeType type);

} // namespace ONNX_NAMESPACE

Import

#include "onnx/defs/attr_proto_util.h"

I/O Contract

Inputs

Name Type Required Description
attr_name std::string Yes The name of the attribute
value/values float, int64_t, int, std::string, TensorProto, GraphProto, TypeProto, or vector variants Yes (for MakeAttribute) The attribute value(s)
type AttributeProto_AttributeType Yes (for MakeRefAttribute) The type of the referenced attribute
referred_attr_name const std::string& No (for MakeRefAttribute) The name of the attribute in the parent function (when different from attr_name)

Outputs

Name Type Description
AttributeProto AttributeProto A fully constructed attribute protobuf with the name, type, and value set

Usage Examples

#include "onnx/defs/attr_proto_util.h"
using namespace ONNX_NAMESPACE;

// Create scalar attributes
auto float_attr = MakeAttribute("alpha", 0.01f);
auto int_attr = MakeAttribute("axis", int64_t(1));
auto string_attr = MakeAttribute("mode", std::string("constant"));

// Create vector attributes
auto floats_attr = MakeAttribute("scales", std::vector<float>{1.0f, 2.0f, 3.0f});
auto ints_attr = MakeAttribute("kernel_shape", std::vector<int64_t>{3, 3});
auto strings_attr = MakeAttribute("keys", std::vector<std::string>{"a", "b", "c"});

// Create a tensor attribute
TensorProto tensor;
// ... populate tensor ...
auto tensor_attr = MakeAttribute("value", tensor);

// Create a reference attribute for a function body node
// (same name in function and function body)
auto ref_attr = MakeRefAttribute("axis", AttributeProto_AttributeType_INT);

// Create a reference attribute with different names
auto ref_attr2 = MakeRefAttribute("body_axis", "func_axis", AttributeProto_AttributeType_INT);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment