MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / reduce_axis

Function reduce_axis

src/framework/modules/primitive_modules.cpp:139–163  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

137 if (rank == 0) {
138 throw std::runtime_error("Reduce input rank must be positive");
139 }
140 const int normalized = axis < 0 ? static_cast<int>(rank) + axis : axis;
141 if (normalized < 0 || normalized >= static_cast<int>(rank)) {
142 throw std::runtime_error("Reduce axis out of range");
143 }
144 return normalized;
145}
146
147core::TensorShape reduced_shape(const core::TensorShape & shape, int axis) {
148 core::TensorShape out = shape;
149 out.dims[static_cast<size_t>(axis)] = 1;
150 return out;
151}
152
153template <typename Fn>
154core::TensorValue reduce_axis(
155 core::ModuleBuildContext & ctx,
156 const core::TensorValue & input,
157 const ReduceConfig & config,
158 Fn fn) {
159 if (ctx.ggml == nullptr) {
160 throw std::runtime_error("ModuleBuildContext.ggml is null");
161 }
162 core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
163 const int axis = normalize_reduce_axis(config.axis, input.shape.rank);
164 if (axis == static_cast<int>(input.shape.rank - 1)) {
165 const auto contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, input);
166 return core::wrap_tensor(fn(ctx.ggml, contiguous.tensor), reduced_shape(input.shape, axis), GGML_TYPE_F32);

Callers 1

buildMethod · 0.85

Calls 8

validate_rank_betweenFunction · 0.85
normalize_reduce_axisFunction · 0.85
wrap_tensorFunction · 0.85
reduced_shapeFunction · 0.85
TransposeModuleClass · 0.85
swapFunction · 0.50
buildMethod · 0.45

Tested by

no test coverage detected