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

Function effective_weight_norm_dim0

src/models/heartmula/codec.cpp:67–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

65 int64_t hidden_dim = 4 * dim;
66 hidden_dim = 2 * hidden_dim / 3;
67 return multiple_of * ((hidden_dim + multiple_of - 1) / multiple_of);
68}
69
70std::vector<float> effective_weight_norm_dim0(
71 const assets::TensorSource & source,
72 const std::string & prefix,
73 const std::vector<int64_t> & shape) {
74 if (shape.empty()) {
75 throw std::runtime_error("HeartCodec weight-norm tensor shape is empty");
76 }
77 const auto g = source.require_f32(prefix + ".parametrizations.weight.original0", {shape[0], 1, 1});
78 const auto v = source.require_f32(prefix + ".parametrizations.weight.original1", shape);
79 std::vector<float> out(v.size(), 0.0F);
80 int64_t inner = 1;
81 for (size_t axis = 1; axis < shape.size(); ++axis) {
82 inner *= shape[axis];
83 }
84 for (int64_t row = 0; row < shape[0]; ++row) {
85 double sum = 0.0;
86 const size_t base = static_cast<size_t>(row * inner);
87 for (int64_t index = 0; index < inner; ++index) {
88 const float value = v[base + static_cast<size_t>(index)];
89 sum += static_cast<double>(value) * static_cast<double>(value);
90 }
91 const double norm = std::sqrt(sum);
92 if (norm == 0.0) {
93 throw std::runtime_error("HeartCodec weight-norm tensor has zero norm: " + prefix);
94 }
95 const float scale = static_cast<float>(static_cast<double>(g[static_cast<size_t>(row)]) / norm);
96 for (int64_t index = 0; index < inner; ++index) {
97 out[base + static_cast<size_t>(index)] = v[base + static_cast<size_t>(index)] * scale;
98 }
99 }
100 return out;
101}

Callers 2

load_weight_norm_conv1dFunction · 0.85

Calls 3

emptyMethod · 0.45
require_f32Method · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected