Skip to main content

TensorSpec.h File

TensorConstraint (alias TensorSpec) — declarative shape/dtype/device contract for tensors. More...

Included Headers

#include "pipeline/TensorCore.h" #include <optional> #include <string> #include <vector>

Namespaces Index

namespacesimaai
namespaceneat

Classes Index

structTensorConstraint

Declarative tensor contract — describes the shape/dtype/device/format a tensor must satisfy. More...

Description

TensorConstraint (alias TensorSpec) — declarative shape/dtype/device contract for tensors.

TensorConstraint (typedef'd as TensorSpec from Model.h) is the declarative description of what kind of tensor a function expects or produces. Used by Model::input_spec() / Model::output_spec() for introspection and by validation code to assert that a tensor meets the model's contract. Supports flexible matching: dtype lists, dynamic shape dims (-1 = any), device requirements, image-format hints, and segment requirements.

See Also

Tensor (the value type) in TensorCore.h

See Also

Model::input_spec, Model::output_spec

File Listing

The file content with the documentation metadata removed is:

1
16#pragma once
17
19
20#include <optional>
21#include <string>
22#include <vector>
23
24namespace simaai::neat {
25
45 std::vector<simaai::neat::TensorDType> dtypes;
46 int rank = -1;
47 std::vector<int64_t> shape;
48 std::optional<Device> device;
49 std::vector<Device> allowed_devices;
50 std::optional<Device> preferred_device;
51
52 std::optional<ImageSpec::PixelFormat>
54 std::vector<Segment> required_segments;
55 std::vector<std::string>
57 bool allow_composite = true;
58
65 bool matches(const Tensor& t) const {
66 if (rank >= 0 && static_cast<int>(t.shape.size()) != rank)
67 return false;
68 if (!shape.empty() && shape.size() == t.shape.size()) {
69 for (size_t i = 0; i < shape.size(); ++i) {
70 if (shape[i] >= 0 && t.shape[i] != shape[i])
71 return false;
72 }
73 }
74 if (!dtypes.empty()) {
75 bool ok = false;
76 for (auto dt : dtypes) {
77 if (dt == t.dtype) {
78 ok = true;
79 break;
80 }
81 }
82 if (!ok)
83 return false;
84 }
85 if (device.has_value()) {
86 if (t.device.type != device->type || t.device.id != device->id)
87 return false;
88 }
89 if (!allowed_devices.empty()) {
90 bool ok = false;
91 for (const auto& allowed : allowed_devices) {
92 if (t.device.type == allowed.type && t.device.id == allowed.id) {
93 ok = true;
94 break;
95 }
96 }
97 if (!ok)
98 return false;
99 }
100 if (image_format.has_value()) {
101 if (!t.semantic.image.has_value())
102 return false;
103 if (t.semantic.image->format != *image_format)
104 return false;
105 }
106 if (!required_segments.empty()) {
107 if (!t.storage || t.storage->sima_segments.empty())
108 return false;
109 if (t.storage->sima_segments.size() != required_segments.size())
110 return false;
111 for (size_t i = 0; i < required_segments.size(); ++i) {
112 if (t.storage->sima_segments[i].name != required_segments[i].name)
113 return false;
114 if (t.storage->sima_segments[i].size_bytes != required_segments[i].size_bytes) {
115 return false;
116 }
117 }
118 }
119 if (!required_segment_names.empty()) {
120 if (!t.storage || t.storage->sima_segments.empty())
121 return false;
122 for (const auto& name : required_segment_names) {
123 bool found = false;
124 for (const auto& seg : t.storage->sima_segments) {
125 if (seg.name == name) {
126 found = true;
127 break;
128 }
129 }
130 if (!found)
131 return false;
132 }
133 }
134 if (!allow_composite && t.is_composite())
135 return false;
136 return true;
137 }
138};
139
140} // namespace simaai::neat

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.