Repository navigation
Integrate Breeze into shared components and Qwen3.8-Flash-Next for SM110 #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -282,8 +282,16 @@ def __init__( | |
|
|
||
| from b12x.gemm.blockscaled._linear import MXFP8LinearWeight | ||
|
|
||
| breeze = getattr(session, "breeze", None) | ||
| self._breeze_mode = breeze is not None | ||
| self._breeze_conv = None | ||
| self._breeze_gdn_prepared = None | ||
| self._breeze_gdn_key = None | ||
| self._breeze_gdn_buffers = None | ||
| self._breeze_use_fused_conv = False | ||
| self._breeze_use_accepted = False | ||
| self.output_encoding = None | ||
| if (prepare_projections and mode == "prefill" and partitions is None | ||
| if (prepare_projections and mode == "prefill" and partitions is None and not self._breeze_mode | ||
| and not sm103.is_sm103(self.device) and isinstance(projections["out_proj"], MXFP8LinearWeight)): | ||
| self.output_encoding = (scratch.mxfp8_output if gdn_buffers is None | ||
| else scratch.allocate_mxfp8_output(buffers=gdn_buffers)) | ||
|
|
@@ -314,7 +322,7 @@ def projection(key, inputs, output): | |
| self.projections = {key: projection(key, source, output) for key, output in inputs} | ||
| self.projections["out_proj"] = projection("out_proj", scratch.gated, scratch.output) | ||
| self.parallel_projections = None | ||
| if (self.fused_qkvz and mode == "decode" and self.tokens <= 16 | ||
| if (not self._breeze_mode and self.fused_qkvz and mode == "decode" and self.tokens <= 16 | ||
| and isinstance(projection_workspace, ScratchArena) and projection_workspace.overlap | ||
| and all(isinstance(self.projections[key], QuantizedProjection) | ||
| for key in ("in_proj_qkvz", "in_proj_ba"))): | ||
|
|
@@ -332,6 +340,10 @@ def projection(key, inputs, output): | |
| in_place=in_place if part.mode == "decode" else False, prepare_projections=False) | ||
| for part in partitions.parts) | ||
| return | ||
| if self._breeze_mode: | ||
| self._init_breeze(module, scratch, state, metadata, mode=mode, session=session, | ||
| records=records, in_place=in_place, breeze=breeze, gdn_buffers=gdn_buffers) | ||
| return | ||
| self.conv_weight = module.conv1d.weight.squeeze(1) | ||
| conv_args = { | ||
| "x": scratch.projected_qkv, | ||
|
|
@@ -487,6 +499,92 @@ def set_recurrent_binding(binding): | |
| eps=module.norm.variance_epsilon, round_normalized=True, activation=module.norm.activation, | ||
| encoded=self.output_encoding) | ||
|
|
||
| def _init_breeze(self, module, scratch, state, metadata, *, mode, session, records, in_place, breeze, gdn_buffers): | ||
| from breeze import gdn | ||
| from hyperflare_recipes.components.recurrent import PreparedShortConv | ||
| from ignition.preparation.breeze import BreezeBuffers, breeze_op_key | ||
| from ignition.preparation.tensor_metadata import record_numeric_tensor | ||
|
|
||
| for param_tensor in (module.conv1d.weight, module.A_log, module.dt_bias, module.norm.weight): | ||
| record_numeric_tensor(param_tensor) | ||
| conv_weight = module.conv1d.weight.squeeze(1).contiguous() | ||
| gate = gdn.Gate.SIGMOID if module.norm.activation == "sigmoid" else gdn.Gate.SILU | ||
| params = gdn.Params( | ||
| hk=self.key_heads, | ||
| hv=self.value_heads, | ||
| dk=128, | ||
| dv=128, | ||
| A_log=module.A_log.detach(), | ||
| dt_bias=module.dt_bias.detach(), | ||
| norm_weight=module.norm.weight.detach(), | ||
| eps=float(module.norm.variance_epsilon), | ||
| gate=gate, | ||
| ) | ||
| self.recovered = None | ||
| self.chunked = None | ||
| self.conv_plan = None | ||
| self.conv_binding = None | ||
| self.recurrent_binding = None | ||
| if mode == "prefill": | ||
| # Prefill must never enter the speculative conv path even when conv_state has speculative columns. | ||
| self._breeze_conv = PreparedShortConv( | ||
| conv_weight, state_shape=tuple(state.convolution.shape), rows=self.tokens, | ||
| session=session, dilation=1, index_dtype=torch.int64, | ||
| has_state_dtype=torch.bool, accepted_dtype=None, residual=False, null_slot=0, | ||
| ) | ||
| prefill_key = ("gdn.prefill", module.A_log.data_ptr(), self.key_heads, self.value_heads, gate) | ||
| calls = gdn.Calls( | ||
| dtype=torch.bfloat16, state_dtype=torch.float32, gate_dtype=torch.bfloat16, | ||
| index_dtype=torch.int64, has_state_dtype=torch.bool, | ||
| ) | ||
| self._breeze_gdn_prepared = breeze.get_or_prepare( | ||
| prefill_key, lambda: gdn.prepare_prefill(params, calls), keep=(module.A_log,) | ||
| ) | ||
| self._breeze_gdn_key = breeze_op_key(self._breeze_gdn_prepared) | ||
| elif records is not None: | ||
| self._breeze_conv = PreparedShortConv( | ||
| conv_weight, state_shape=tuple(state.convolution.shape), rows=self.tokens, | ||
| session=session, dilation=1, index_dtype=torch.int64, | ||
| has_state_dtype=None, accepted_dtype=torch.int32, residual=False, null_slot=0, | ||
| ) | ||
| self.recovered = PreparedRecoveredRecurrence( | ||
| mixed_qkv=scratch.convolved_qkv, a=scratch.a, b=scratch.b, A_log=module.A_log, | ||
| dt_bias=module.dt_bias, state=state.recurrent, metadata=metadata, output=scratch.recurrence, | ||
| records=records, key_heads=self.key_heads, value_heads=self.value_heads, | ||
| convolution=None, buffers=gdn_buffers, round_beta=False, | ||
| ) | ||
| self.output_norm = PreparedGatedRMSNorm( | ||
| scratch.recurrence.view(self.tokens, self.value_heads, 128), | ||
| scratch.z.view(self.tokens, self.value_heads, 128), | ||
| module.norm.weight, | ||
| scratch.gated.view(self.tokens, self.value_heads, 128), | ||
| metadata.num_tokens, | ||
| eps=module.norm.variance_epsilon, round_normalized=False, activation=module.norm.activation, | ||
| ) | ||
| else: | ||
| self._breeze_use_fused_conv = True | ||
| self._breeze_use_accepted = bool(in_place) | ||
| decode_key = ( | ||
| "gdn.decode", module.A_log.data_ptr(), self.key_heads, self.value_heads, gate, | ||
| (int(state.convolution.shape[1]), int(state.convolution.shape[2])), self._breeze_use_accepted, | ||
| ) | ||
| calls = gdn.Calls( | ||
| dtype=torch.bfloat16, state_dtype=torch.float32, gate_dtype=torch.bfloat16, | ||
| index_dtype=torch.int64, accepted_dtype=torch.int32 if self._breeze_use_accepted else None, | ||
| ) | ||
| conv_params = gdn.ConvParams(weight=conv_weight, state_shape=tuple(state.convolution.shape)) | ||
| self._breeze_gdn_prepared = breeze.get_or_prepare( | ||
| decode_key, lambda: gdn.prepare_decode(params, calls, conv=conv_params), keep=(module.A_log, conv_weight) | ||
| ) | ||
| self._breeze_gdn_key = breeze_op_key(self._breeze_gdn_prepared) | ||
| # Size counters for the full virtual state-row extent Helios can address at runtime. | ||
| conv_slots = int(state.convolution.shape[0]) | ||
| self._breeze_gdn_buffers = BreezeBuffers( | ||
| breeze, | ||
| session, | ||
| lambda p=self._breeze_gdn_prepared, n=conv_slots: (0, int(gdn.sizes(p, conv_slots=n).state)), | ||
| ) | ||
|
|
||
| def functional(self, *, session, dynamic_metadata=False): | ||
| """Keep selected kernels and persistent state while tracing intermediate allocations.""" | ||
| if getattr(self, "_functional_requested", False): | ||
|
|
@@ -507,6 +605,8 @@ def functional(self, *, session, dynamic_metadata=False): | |
| projection.functional(session=session) | ||
| if self.parallel_projections is not None: | ||
| self.parallel_projections.functional(session=session) | ||
| if self._breeze_conv is not None: | ||
| self._breeze_conv.functional(session=session) | ||
| if self.partitions is not None: | ||
| if not dynamic_metadata: | ||
| self.partitions = self.partitions.functional() | ||
|
|
@@ -548,6 +648,48 @@ def _functional_sequence(self, projected, z, a, b, *, records=None, metadata=Non | |
| if metadata is None: | ||
| raise ValueError("functional GDN requires metadata at invocation") | ||
| state, module = self.state, self.module | ||
| if self._breeze_mode: | ||
| if self.mode == "prefill": | ||
| has_state = ~metadata.fresh | ||
| convolved = self._breeze_conv( | ||
| projected, state.convolution, | ||
| cu_seqlens=metadata.query_start_loc, slots=metadata.convolution_slots, has_state=has_state, | ||
| ) | ||
| # Zero-init defines padding / null-slot rows that Breeze leaves untouched. | ||
| gated = torch.zeros((self.tokens, self.value_heads * 128), dtype=torch.bfloat16, device=projected.device) | ||
| torch.ops.breeze.gdn_prefill( | ||
| convolved, self._breeze_gdn_key, a, b, z, state.recurrent, gated, | ||
| metadata.query_start_loc, metadata.final_state_indices, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Breeze prefill drops
Some current producers emit equal rows for continuations, including The Breeze prefill path must read the initial row for a continued sequence. Choose one correction:
The operator signature is not in the inspected source. The correction is therefore left open.
|
||
| has_state=has_state, null_slot=0, | ||
| ) | ||
| return gated, None | ||
| if self.recovered is not None: | ||
| convolved = self._breeze_conv( | ||
| projected, state.convolution, | ||
| cu_seqlens=metadata.query_start_loc, slots=metadata.convolution_slots, | ||
| num_accepted=metadata.accepted_tokens, | ||
| ) | ||
| recurrence = self.recovered(convolved, a, b, records=records, metadata=runtime_metadata) | ||
| recurrence = recurrence.view(self.tokens, self.value_heads, 128) | ||
| return self.output_norm(recurrence, z.view_as(recurrence), live=metadata.num_tokens).view(self.tokens, -1), None | ||
| gated = torch.zeros((self.tokens, self.value_heads * 128), dtype=torch.bfloat16, device=projected.device) | ||
| if self._breeze_use_accepted: | ||
| # Width-1 speculative decode: replicate each request's single live slot across columns so Breeze | ||
| # reads from slots[n, num_accepted - 1] and updates in place with conv history at num_accepted - 1. | ||
| slots = metadata.state_indices[:, :1].expand(-1, metadata.state_indices.shape[1]).contiguous() | ||
| accepted = metadata.accepted_tokens | ||
| else: | ||
| slots = metadata.state_indices[:, 0].contiguous() | ||
| accepted = None | ||
| _, op_state = self._breeze_gdn_buffers.get() | ||
| torch.ops.breeze.gdn_decode( | ||
| projected, self._breeze_gdn_key, a, b, z, state.recurrent, gated, | ||
| metadata.query_start_loc, slots, | ||
| num_accepted=accepted, null_slot=0, | ||
| conv_state=state.convolution if self._breeze_use_fused_conv else None, | ||
|
Comment on lines
+687
to
+689
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fused Breeze decode path can read and update convolution history from the recurrence row. On the Breeze decode path, When the two mappings differ, the fused operation can use the recurrence row for convolution history. This writes the wrong convolution state, and later tokens read that state. The The fused decode path must either receive the convolution slot mapping, or it must not run when the two mappings differ.
|
||
| state=op_state, | ||
| ) | ||
| return gated, None | ||
| if records is not None and self.recovered is None: | ||
| raise ValueError("GDN correction records require a prepared verification recurrence") | ||
| convolved = torch.empty((self.tokens, module.conv_dim), dtype=torch.bfloat16, device=projected.device) | ||
|
|
@@ -661,6 +803,22 @@ def __call__(self, source: torch.Tensor, *, records=None, metadata=None, partiti | |
|
|
||
| def run_sequence(self): | ||
| scratch = self.scratch | ||
| if self._breeze_mode: | ||
| if self.recovered is not None: | ||
| convolved = self._breeze_conv( | ||
| scratch.projected_qkv, self.state.convolution, | ||
| cu_seqlens=self.metadata.query_start_loc, slots=self.metadata.convolution_slots, | ||
| num_accepted=self.metadata.accepted_tokens, | ||
| ) | ||
| scratch.convolved_qkv.copy_(convolved) | ||
| self.recovered() | ||
| self.output_norm() | ||
| return | ||
| gated, _ = self._functional_sequence( | ||
| scratch.projected_qkv, scratch.z, scratch.a, scratch.b, metadata=self.metadata | ||
| ) | ||
| scratch.gated.copy_(gated) | ||
| return | ||
| if self.conv_binding is not None: | ||
| short_conv.run(self.conv_binding) | ||
| if self.recovered is not None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve the prepared output layout in functional embedding.
functional()saves the output shape, strides, and dtype. The Breeze branch returns the gather result without using them. For a[4, 32]output with stride(64, 1), the non-Breeze path allocates that stride, but the Breeze path does not request it. Allocate fromself.output_layoutand copy the masked rows into it. Keep returning the caller’s buffer in bound mode. (docs.pytorch.org)As per coding guidelines, “Preserve checkpoint precision, declared layout contracts, and frontend asset contracts when updating recipes or shared components.”
Source: Coding guidelines