def test_ane_layer_norm_intermediate_var_reuse(self, with_affine, reused_name):
        """
        Avoid false positive detection of ml-ane-transformers layer norm pattern.

        In cases where an intermediate value is used after the layer norm
        is computed, the pattern should not be fused.
        """
        shape = (3, 5, 1, 6)

        @mb.program(input_specs=[mb.TensorSpec(shape=shape)])
        def prog(x):
            x1 = mb.add(x=x, y=np.random.rand(*shape))
            x2 = mb.reduce_mean(x=x1, axes=[1], keep_dims=True) # mean
            x3 = mb.sub(x=x1, y=x2) # x - mean
            x4 = mb.mul(x=x3, y=x3) # (x - mean)^2
            x5 = mb.reduce_mean(x=x4, axes=[1], keep_dims=True) # variance
            x6 = mb.add(x=x5, y=1e-5) # variance + eps
            x7 = mb.rsqrt(x=x6) # rsqrt(variance + eps)
            x8 = mb.mul(x=x3, y=x7) # (x - mean) * rsqrt(variance + eps)
            y = x8

            if with_affine:
                x9 = mb.add(x=y, y=np.random.rand(1, shape[1], 1, 1))
                y = x9
                y = mb.mul(x=y, y=np.random.rand(1, shape[1], 1, 1))

            # All the same shape (3,5,1,6)
            reused = None
            if reused_name == "x2":
                reused = x2
            elif reused_name == "x3":
                reused = x3
            elif reused_name == "x4":
                reused = x4
            elif reused_name == "x8":
                reused = x8
            elif reused_name == "x9":
                reused = x9

            if reused:
                y = mb.sub(x=reused, y=y) # reuse an intermediate variable

            return y

        prev_prog, prev_block, block = apply_pass_and_basic_check(
            prog, "common::fuse_layernorm_or_instancenorm"
        )
        if reused_name == "x8" and not with_affine:
            # This can be fused since without affine, x8 is the final op in the layer norm.
            assert get_op_types_in_program(prog) == ["add", "layer_norm", "sub"]
        elif reused_name:
            assert "layer_norm" not in get_op_types_in_program(prog)
            assert get_op_types_in_program(prog)[-1] ==  "sub"
        else:
            # Fusion should still work when nothing is reused.
            assert get_op_types_in_program(prog) == ["add", "layer_norm"]

        assert_model_is_valid(
            prog, {"x": shape}, expected_output_shapes={block.outputs[0].name: shape}
        )

        Avoid false positive detection of ml-ane-transformers layer norm pattern.

        In cases where an intermediate value is used after the layer norm
        is computed, the pattern should not be fused.
        
coremltools/converters/mil/mil/passes/tests/test_passes.py