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

Method build

src/framework/modules/linear_module.cpp:73–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

71}
72
73core::TensorValue LinearModule::build(
74 core::ModuleBuildContext & ctx,
75 const core::TensorValue & input,
76 const LinearWeights & weights) const {
77 if (ctx.ggml == nullptr) {
78 throw std::runtime_error("ModuleBuildContext.ggml is null");
79 }
80
81 core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
82 core::validate_last_dim(input, config_.in_features, "input");
83 validate_weight_shape(config_, weights);
84
85 const core::TensorValue contiguous_input = core::ensure_backend_addressable_layout(ctx, input);
86
87 const core::TensorShape matrix_input_shape = flatten_to_matrix_shape(contiguous_input.shape);
88 core::TensorValue matrix_input = core::reshape_tensor(ctx, contiguous_input, matrix_input_shape);
89
90 ggml_tensor * projected_raw = ggml_mul_mat(ctx.ggml, weights.weight.tensor, matrix_input.tensor);
91 if (config_.precision != GGML_PREC_DEFAULT) {
92 ggml_mul_mat_set_prec(projected_raw, config_.precision);
93 }
94 if (config_.cuda_nvfp4_f16_activation_lowering) {
95 ggml_mul_mat_set_lowering(projected_raw, GGML_MUL_MAT_LOWERING_CUDA_NVFP4_F16_ACTIVATION);
96 }
97 core::TensorValue projected = core::wrap_tensor(
98 projected_raw,
99 core::TensorShape::from_dims({matrix_input_shape.at(0), config_.out_features}),
100 GGML_TYPE_F32);
101
102 if (config_.use_bias) {
103 ggml_tensor * biased_raw = ggml_add(ctx.ggml, projected.tensor, weights.bias->tensor);
104 projected = core::wrap_tensor(
105 biased_raw,
106 core::TensorShape::from_dims({matrix_input_shape.at(0), config_.out_features}),
107 GGML_TYPE_F32);
108 }
109
110 return core::reshape_tensor(ctx, projected, input.shape.with_last_dim(config_.out_features));
111}

Callers

nothing calls this directly

Calls 12

validate_rank_betweenFunction · 0.85
validate_last_dimFunction · 0.85
validate_weight_shapeFunction · 0.85
reshape_tensorFunction · 0.85
ggml_mul_matFunction · 0.85
ggml_mul_mat_set_precFunction · 0.85
wrap_tensorFunction · 0.85
ggml_addFunction · 0.85
with_last_dimMethod · 0.80
flatten_to_matrix_shapeFunction · 0.70
atMethod · 0.45

Tested by

no test coverage detected