| 164 | context = core::reshape_tensor( |
| 165 | ctx, |
| 166 | context, |
| 167 | core::TensorShape::from_dims({hidden_states.shape.dims[0], hidden_states.shape.dims[1], config.hidden_size})); |
| 168 | return modules::LinearModule({config.hidden_size, config.hidden_size, false, GGML_PREC_F32}) |
| 169 | .build(ctx, context, {weights.out_weight, std::nullopt}); |
| 170 | } |
| 171 | |
| 172 | core::TensorValue apply_modulated_rms_norm( |
| 173 | core::ModuleBuildContext & ctx, |
| 174 | const core::TensorValue & input, |
| 175 | const core::TensorValue & norm_weight, |
| 176 | const core::TensorValue & one, |
| 177 | const core::TensorValue & shift, |
| 178 | const core::TensorValue & scale, |
| 179 | float rms_eps) { |
| 180 | auto hidden = modules::RMSNormModule({input.shape.dims[input.shape.rank - 1], rms_eps, true, false}) |
| 181 | .build(ctx, input, {norm_weight, std::nullopt}); |
| 182 | auto scaled = core::wrap_tensor(ggml_add(ctx.ggml, scale.tensor, one.tensor), scale.shape, GGML_TYPE_F32); |
| 183 | hidden = modules::MulModule{}.build(ctx, hidden, scaled); |
| 184 | return modules::AddModule{}.build(ctx, hidden, shift); |
| 185 | } |
| 186 | |
| 187 | core::TensorValue expand_conditioning( |
| 188 | core::ModuleBuildContext & ctx, |
| 189 | const core::TensorValue & conditioning, |
| 190 | int64_t seq_len) { |
| 191 | auto reshaped = core::reshape_tensor( |
| 192 | ctx, |
| 193 | conditioning, |
| 194 | core::TensorShape::from_dims({conditioning.shape.dims[0], 1, conditioning.shape.dims[1]})); |
| 195 | return modules::RepeatModule({core::TensorShape::from_dims({conditioning.shape.dims[0], seq_len, conditioning.shape.dims[1]})}) |
| 196 | .build(ctx, reshaped); |
| 197 | } |
| 198 | |
| 199 | struct TimeEmbeddingOutputs { |
| 200 | core::TensorValue temb; |
| 201 | core::TensorValue timestep_proj; |
| 202 | }; |
| 203 | |
| 204 | core::TensorValue build_time_frequency_tensor( |
| 205 | core::ModuleBuildContext & ctx, |
| 206 | ggml_tensor * freqs_tensor, |
| 207 | const core::TensorValue & timestep) { |
| 208 | const int64_t batch_size = timestep.shape.dims[0]; |
| 209 | auto freqs = core::wrap_tensor(freqs_tensor, core::TensorShape::from_dims({128}), GGML_TYPE_F32); |
| 210 | auto expanded_freqs = core::reshape_tensor(ctx, freqs, core::TensorShape::from_dims({1, 128})); |
| 211 | expanded_freqs = modules::RepeatModule({core::TensorShape::from_dims({batch_size, 128})}).build(ctx, expanded_freqs); |
| 212 | auto expanded_time = modules::RepeatModule({core::TensorShape::from_dims({batch_size, 128})}).build(ctx, timestep); |
| 213 | return modules::MulModule{}.build(ctx, expanded_time, expanded_freqs); |
| 214 | } |
| 215 | |
| 216 | TimeEmbeddingOutputs build_time_embedding( |
| 217 | core::ModuleBuildContext & ctx, |
| 218 | ggml_tensor * freqs_tensor, |
| 219 | const core::TensorValue & timestep, |
| 220 | const AceStepTimeEmbeddingWeights & weights, |
| 221 | int64_t hidden_size) { |
| 222 | auto scaled_t = core::wrap_tensor(ggml_scale(ctx.ggml, timestep.tensor, 1000.0F), timestep.shape, GGML_TYPE_F32); |
| 223 | auto args = build_time_frequency_tensor(ctx, freqs_tensor, scaled_t); |
no test coverage detected