@@ -639,6 +639,21 @@ def build_empty_features(dtype: ir.DataType, feature_size: int) -> PolicyCompone
639639 return _component ("mobius.policy.auxiliary@1" , graph , {})
640640
641641
642+ def build_empty_batched_features (
643+ dtype : ir .DataType ,
644+ feature_size : int ,
645+ ) -> PolicyComponent :
646+ """Build ``(1, 0, hidden)`` features for an optional single-image path."""
647+ graph , builder = _make_graph ("empty_batched_features" )
648+ features = builder .op .ConstantOfShape (
649+ builder .op .Constant (value_ints = [1 , 0 , feature_size ]),
650+ value = ir .tensor ([0.0 ], dtype = dtype ),
651+ )
652+ features .shape = ir .Shape ([1 , 0 , feature_size ])
653+ builder .add_output (features , "features" )
654+ return _component ("mobius.policy.auxiliary@1" , graph , {})
655+
656+
642657def rotary_axis_count (position_value : ir .Value ) -> int | None :
643658 """Number of rotary axes a decoder's ``position_ids`` input carries.
644659
@@ -1670,6 +1685,106 @@ def build_guidance_combine(dtype: ir.DataType = ir.DataType.FLOAT) -> PolicyComp
16701685 )
16711686
16721687
1688+ def build_tensor_cast (
1689+ source_dtype : ir .DataType ,
1690+ target_dtype : ir .DataType ,
1691+ dims : Sequence [str ] = ("batch" , "heads" , "sequence" , "head_dim" ),
1692+ ) -> PolicyComponent :
1693+ """Cast a typed tensor at an explicit component precision boundary."""
1694+ graph , builder = _make_graph ("tensor_cast" )
1695+ value = builder .input ("value" , source_dtype , list (dims ))
1696+ cast = builder .op .Cast (value , to = target_dtype )
1697+ cast .type = ir .TensorType (target_dtype )
1698+ _set_public_shape (cast , list (dims ))
1699+ builder .add_output (cast , "cast" )
1700+ return _component ("mobius.policy.auxiliary@1" , graph , {})
1701+
1702+
1703+ def build_x0_flow_velocity (
1704+ dtype : ir .DataType = ir .DataType .FLOAT ,
1705+ * ,
1706+ t_eps : float = 0.02 ,
1707+ ) -> PolicyComponent :
1708+ """Convert a clean-sample prediction into flow velocity.
1709+
1710+ Unified pixel-space generators commonly predict ``x0`` rather than
1711+ velocity. Their flow-matching derivative is
1712+ ``(x0 - sample) / max(1 - timestep, t_eps)``.
1713+ """
1714+ graph , builder = _make_graph ("x0_flow_velocity" )
1715+ op = builder .op
1716+ sample = builder .input ("sample" , dtype , ["batch" , "channels" , "height" , "width" ])
1717+ x0 = builder .input ("x0" , dtype , ["batch" , "channels" , "height" , "width" ])
1718+ timestep = builder .input ("timestep" , dtype , ["batch" ])
1719+ one = op .CastLike (op .Constant (value_float = 1.0 ), timestep )
1720+ epsilon = op .CastLike (op .Constant (value_float = float (t_eps )), timestep )
1721+ denominator = op .Max (op .Sub (one , timestep ), epsilon )
1722+ denominator = op .Unsqueeze (denominator , op .Constant (value_ints = [1 , 2 , 3 ]))
1723+ velocity = op .Div (op .Sub (x0 , sample ), denominator )
1724+ velocity .type = ir .TensorType (dtype )
1725+ _set_public_shape (velocity , ["batch" , "channels" , "height" , "width" ])
1726+ builder .add_output (velocity , "velocity" )
1727+ return _component ("onnx-genai.flow-velocity@1" , graph , {})
1728+
1729+
1730+ def build_image_grid_positions (
1731+ dtype : ir .DataType = ir .DataType .FLOAT ,
1732+ * ,
1733+ pixels_per_token : int ,
1734+ ) -> PolicyComponent :
1735+ """Derive a three-axis image position grid from a pixel-space latent."""
1736+ graph , builder = _make_graph ("image_grid_positions" )
1737+ op = builder .op
1738+ latent = builder .input ("latent" , dtype , ["batch" , "channels" , "height" , "width" ])
1739+ prompt_tokens = builder .input (
1740+ "prompt_tokens" , ir .DataType .INT64 , ["batch" , "prompt_sequence" ]
1741+ )
1742+ batch = op .Shape (latent , start = 0 , end = 1 )
1743+ height = op .Div (
1744+ op .Shape (latent , start = 2 , end = 3 ),
1745+ op .Constant (value_ints = [pixels_per_token ]),
1746+ )
1747+ width = op .Div (
1748+ op .Shape (latent , start = 3 , end = 4 ),
1749+ op .Constant (value_ints = [pixels_per_token ]),
1750+ )
1751+ count = op .Mul (height , width )
1752+ flat = op .Range (
1753+ op .Constant (value_int = 0 ),
1754+ op .Squeeze (count , op .Constant (value_ints = [0 ])),
1755+ op .Constant (value_int = 1 ),
1756+ )
1757+ token_shape = op .Concat (batch , count , axis = 0 )
1758+ height_positions = op .Expand (
1759+ op .Unsqueeze (
1760+ op .Div (flat , op .Squeeze (width , op .Constant (value_ints = [0 ]))),
1761+ op .Constant (value_ints = [0 ]),
1762+ ),
1763+ token_shape ,
1764+ )
1765+ width_positions = op .Expand (
1766+ op .Unsqueeze (
1767+ op .Mod (flat , op .Squeeze (width , op .Constant (value_ints = [0 ])), fmod = 0 ),
1768+ op .Constant (value_ints = [0 ]),
1769+ ),
1770+ token_shape ,
1771+ )
1772+ prompt_length = op .Shape (prompt_tokens , start = 1 , end = 2 )
1773+ temporal = op .Expand (prompt_length , token_shape )
1774+ positions = op .Concat (
1775+ op .Unsqueeze (temporal , op .Constant (value_ints = [0 ])),
1776+ op .Unsqueeze (height_positions , op .Constant (value_ints = [0 ])),
1777+ op .Unsqueeze (width_positions , op .Constant (value_ints = [0 ])),
1778+ axis = 0 ,
1779+ )
1780+ positions .shape = ir .Shape ([3 , "batch" , "image_tokens" ])
1781+ token_grid = op .Concat (height , width , axis = 0 )
1782+ token_grid .shape = ir .Shape ([2 ])
1783+ builder .add_output (positions , "position_ids" )
1784+ builder .add_output (token_grid , "token_grid" )
1785+ return _component ("mobius.policy.auxiliary@1" , graph , {})
1786+
1787+
16731788_RNG_MODULUS = 2147483647
16741789_RNG_MULTIPLIER = 48271
16751790_RNG_STRIDE = 2654435761
0 commit comments