Loading...
Searching...
No Matches
network_internal.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
6#include <algorithm>
7#include <cmath>
8#include <cstdint>
9
11
12// Private implementation shared by network setup and persistence. Not installed.
14{
15
16static inline void validate_source_indices(const vector<Index>& sources, Index layer_index, Index layers_count)
17{
18 for (const Index src : sources | views::filter([](Index source) { return source >= 0; }))
19 throw_if(src >= layers_count || src >= layer_index,
20 "Network: source index {} is not a previous layer for layer {}.", src, layer_index);
21}
22
23static inline void validate_source_arity(const Layer& layer,
24 const vector<Index>& sources,
25 Index layer_index)
26{
27 const Index expected_sources = layer.get_sources_number();
28
29 throw_if(ssize(sources) != expected_sources,
30 "Network: {} layer {} expects {} sources, got {}.",
31 layer.get_name(), layer_index, expected_sources, sources.size());
32}
33
34#ifdef OPENNN_HAS_CUDA
35static inline Index quantization_channel(const Index element_index,
36 const Index row_length,
37 const Index channels,
38 const int axis)
39{
40 return axis == 0 ? element_index / row_length : element_index % channels;
41}
42
43static inline void finalize_int8_scales(vector<float>& absolute_maxima)
44{
45 for (float& scale : absolute_maxima)
46 scale = scale > 0.0f ? scale / 127.0f : 1.0f;
47}
48
49static inline void quantize_int8_host(const float* values, const Index count,
50 const Index base_index, const Index row_length,
51 const Index channels, const int axis,
52 const float* scales, int8_t* out)
53{
54 #pragma omp parallel for if(count > 4096)
55 for (Index i = 0; i < count; ++i)
56 {
57 const Index channel =
58 quantization_channel(base_index + i, row_length, channels, axis);
59 out[i] = int8_t(clamp<long>(lroundf(values[i] / scales[channel]), -127, 127));
60 }
61}
62#endif
63
64}
Definition layer.h:38
virtual Index get_sources_number() const noexcept
Definition layer.h:114
const string & get_name() const
Definition network_internal.h:14
static Index quantization_channel(const Index element_index, const Index row_length, const Index channels, const int axis)
Definition network_internal.h:35
static void validate_source_arity(const Layer &layer, const vector< Index > &sources, Index layer_index)
Definition network_internal.h:23
static void quantize_int8_host(const float *values, const Index count, const Index base_index, const Index row_length, const Index channels, const int axis, const float *scales, int8_t *out)
Definition network_internal.h:49
static void validate_source_indices(const vector< Index > &sources, Index layer_index, Index layers_count)
Definition network_internal.h:16
static void finalize_int8_scales(vector< float > &absolute_maxima)
Definition network_internal.h:43
void scale(const TensorView &, const TensorView &, const TensorView &, const TensorView &, const TensorView &, const TensorView &, float, float, TensorView &)
#define throw_if(condition,...)
Definition opennn_types.h:243