| 209 | } |
| 210 | |
| 211 | core::TensorValue graph_instance_norm_prelu( |
| 212 | core::ModuleBuildContext & ctx, |
| 213 | const core::TensorValue & input, |
| 214 | const std::unordered_map<std::string, Param> & params, |
| 215 | const std::string & norm_weight_name, |
| 216 | const std::string & norm_bias_name, |
| 217 | const std::string & prelu_weight_name) { |
| 218 | const auto & norm_weight = require_param(params, norm_weight_name); |
| 219 | const auto & norm_bias = require_param(params, norm_bias_name); |
| 220 | const auto & prelu_weight = require_param(params, prelu_weight_name); |
| 221 | core::validate_shape(norm_weight.tensor, core::TensorShape::from_dims({input.shape.dims[1]}), "ZipEnhancer instance norm weight"); |
| 222 | core::validate_shape(norm_bias.tensor, core::TensorShape::from_dims({input.shape.dims[1]}), "ZipEnhancer instance norm bias"); |
| 223 | core::validate_shape(prelu_weight.tensor, core::TensorShape::from_dims({input.shape.dims[1]}), "ZipEnhancer PReLU weight"); |
| 224 | |
| 225 | auto mean_f = modules::ReduceMeanModule({3}).build(ctx, input); |
| 226 | auto mean = modules::ReduceMeanModule({2}).build(ctx, mean_f); |
| 227 | auto mean_rep = core::wrap_tensor(ggml_repeat(ctx.ggml, mean.tensor, input.tensor), input.shape, GGML_TYPE_F32); |
| 228 | auto centered = core::wrap_tensor(ggml_sub(ctx.ggml, input.tensor, mean_rep.tensor), input.shape, GGML_TYPE_F32); |
| 229 | auto squared = core::wrap_tensor(ggml_sqr(ctx.ggml, centered.tensor), centered.shape, GGML_TYPE_F32); |
| 230 | auto var_f = modules::ReduceMeanModule({3}).build(ctx, squared); |
| 231 | auto var = modules::ReduceMeanModule({2}).build(ctx, var_f); |
| 232 | auto stddev = core::wrap_tensor( |
| 233 | ggml_sqrt(ctx.ggml, ggml_scale_bias(ctx.ggml, var.tensor, 1.0f, 1.0e-5f)), |
| 234 | var.shape, |
| 235 | GGML_TYPE_F32); |
| 236 | auto stddev_rep = core::wrap_tensor(ggml_repeat(ctx.ggml, stddev.tensor, input.tensor), input.shape, GGML_TYPE_F32); |
| 237 | auto normed = core::wrap_tensor(ggml_div(ctx.ggml, centered.tensor, stddev_rep.tensor), input.shape, GGML_TYPE_F32); |
| 238 | auto weight = core::reshape_tensor(ctx, norm_weight.tensor, core::TensorShape::from_dims({1, input.shape.dims[1], 1, 1})); |
| 239 | auto bias = core::reshape_tensor(ctx, norm_bias.tensor, core::TensorShape::from_dims({1, input.shape.dims[1], 1, 1})); |
| 240 | auto weight_rep = core::wrap_tensor(ggml_repeat(ctx.ggml, weight.tensor, normed.tensor), normed.shape, GGML_TYPE_F32); |
| 241 | auto bias_rep = core::wrap_tensor(ggml_repeat(ctx.ggml, bias.tensor, normed.tensor), normed.shape, GGML_TYPE_F32); |
| 242 | auto affine = core::wrap_tensor(ggml_add(ctx.ggml, ggml_mul(ctx.ggml, normed.tensor, weight_rep.tensor), bias_rep.tensor), normed.shape, GGML_TYPE_F32); |
| 243 | |
| 244 | auto positive = modules::ReluModule().build(ctx, affine); |
| 245 | auto negative = modules::ReluModule().build(ctx, core::wrap_tensor(ggml_scale(ctx.ggml, affine.tensor, -1.0f), affine.shape, GGML_TYPE_F32)); |
| 246 | auto slope = core::reshape_tensor(ctx, prelu_weight.tensor, core::TensorShape::from_dims({1, input.shape.dims[1], 1, 1})); |
| 247 | auto slope_rep = core::wrap_tensor(ggml_repeat(ctx.ggml, slope.tensor, affine.tensor), affine.shape, GGML_TYPE_F32); |
| 248 | auto scaled_negative = core::wrap_tensor(ggml_mul(ctx.ggml, slope_rep.tensor, negative.tensor), affine.shape, GGML_TYPE_F32); |
| 249 | return core::wrap_tensor(ggml_sub(ctx.ggml, positive.tensor, scaled_negative.tensor), affine.shape, GGML_TYPE_F32); |
| 250 | } |
| 251 | |
| 252 | core::TensorValue graph_dense_block( |
| 253 | core::ModuleBuildContext & ctx, |
no test coverage detected