{"record":{"id":"196622926b058dbd","repo":"tracel-ai/burn","slug":"burn-flex-layer-norm-unsupported-dtype","errorCode":null,"errorMessage":"burn_flex::layer_norm: unsupported dtype {:?}","messagePattern":"burn_flex::layer_norm: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/activation.rs","lineNumber":603,"sourceCode":"        assert!(\n            beta_shape.len() == 1 && beta_shape[0] == d_model,\n            \"layer_norm: beta must be a 1-D tensor of length equal to last dim of input \\\n             (got shape {:?}, expected [{}])\",\n            beta_shape,\n            d_model,\n        );\n    }\n\n    match input.dtype() {\n        DType::F32 => layer_norm_f32(input, gamma, beta, epsilon as f32),\n        DType::F64 => layer_norm_f64(input, gamma, beta, epsilon),\n        DType::F16 => {\n            layer_norm_via_f32::<f16>(input, gamma, beta, epsilon, f16::to_f32, f16::from_f32)\n        }\n        DType::BF16 => {\n            layer_norm_via_f32::<bf16>(input, gamma, beta, epsilon, bf16::to_f32, bf16::from_f32)\n        }\n        dtype => panic!(\"burn_flex::layer_norm: unsupported dtype {:?}\", dtype),\n    }\n}\n\nfn layer_norm_via_f32<E: burn_backend::Element + bytemuck::Pod + Copy>(\n    input: FlexTensor,\n    gamma: FlexTensor,\n    beta: Option<FlexTensor>,\n    epsilon: f64,\n    to_f32: fn(E) -> f32,\n    from_f32: fn(f32) -> E,\n) -> FlexTensor {\n    let input_f32 = crate::ops::module::cast_to_f32::<E>(input, to_f32);\n    let gamma_f32 = crate::ops::module::cast_to_f32::<E>(gamma, to_f32);\n    let beta_f32 = beta.map(|b| crate::ops::module::cast_to_f32::<E>(b, to_f32));\n    let out = layer_norm_f32(input_f32, gamma_f32, beta_f32, epsilon as f32);\n    crate::ops::module::cast_from_f32::<E>(out, from_f32)\n}\n","sourceCodeStart":585,"sourceCodeEnd":621,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/activation.rs#L585-L621","documentation":"layer_norm in burn-flex supports F32, F64 natively and F16/BF16 via an upcast-to-f32 path (layer_norm_via_f32). Any other dtype reaches the catch-all panic arm. The library requires floating-point input because layer norm computes mean/variance with floating-point math and takes float gamma/beta parameters.","triggerScenarios":"Calling burn_flex::ops::layer_norm (public) with input of an integer or bool dtype; also if gamma/beta and input dtypes are mismatched in a way that resolves to an unhandled dtype branch. Commonly after a quantize step that left activations in I8.","commonSituations":"Quantized transformer inference feeding int8 activations into LayerNorm; porting a model where the previous backend auto-cast; constructing normalization params as integers in tests.","solutions":["Cast the input (and gamma/beta) to a float dtype, e.g. input.cast(DType::F32), before calling layer_norm.","Audit the preceding op so activations stay in F16/BF16/F32/F64 through the normalization layer.","Wrap layer_norm in a helper that upcasts unsupported dtypes to F32 and downcasts after."],"exampleFix":"// before\nlet out = layer_norm(x_i8, &gamma, &beta, 1e-5);\n// after\nlet out = layer_norm(x_i8.cast(DType::F32), &gamma.cast(DType::F32), &beta.cast(DType::F32), 1e-5).cast(x_i8.dtype());","handlingStrategy":"validation","validationCode":"if !matches!(input.dtype(), DType::F32 | DType::F64 | DType::F16 | DType::BF16) {\n    input = input.cast(DType::F32);\n}\nlet out = layer_norm(input, &gamma, &beta, epsilon);","typeGuard":"fn supports_layer_norm(d: DType) -> bool {\n    matches!(d, DType::F32 | DType::F64 | DType::F16 | DType::BF16)\n}","tryCatchPattern":"let out = std::panic::catch_unwind(|| layer_norm(input.clone(), &gamma, &beta, eps))\n    .unwrap_or_else(|_| layer_norm(input.cast(DType::F32), &gamma, &beta, eps));","preventionTips":["Keep activations in float dtypes through normalization layers.","Insert explicit dequantization right after int8 quantized ops.","Assert input.dtype().is_float() in model forward helpers.","Keep gamma/beta dtype aligned with the input dtype."],"tags":["panic","dtype","layer-norm","unsupported-dtype"],"backgroundTag":"unsupported-dtype","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}