Add Seed-OSS architecture support (SeedOssForCausalLM)#2116
Conversation
ByteDance's Seed-OSS architecture (`SeedOssForCausalLM`) is structurally
a Llama-family decoder with two extra per-layer normalisations injected
into the residual branches:
Input → input_layernorm → Attention → attn_post_norm ─→ + ─→ (residual)
→ post_attention_layernorm → MLP → ffn_post_norm ─→ + ─→ (residual)
It also sets `attention_bias=True` (QKV projection biases present),
`attention_out_bias=False` (o_proj clean), uses GQA, and a high
`rope_theta=1e7`. NousResearch's Hermes-4.3-36B is the most prominent
Seed-OSS-based open-weight model and was the motivating use case here.
This patch adds:
* `builders/seed_oss.py`: `SeedOssModel` inheriting from `LlamaModel`
with a `make_layer` override that wires the two extra layernorms
inside the residual path. Auto-detects post-norm tensor location
(some HF weight layouts expose it as `self_attn.post_attn_layernorm`,
others as `layer.attn_post_norm` — both supported). `layer_attrs`
is initialised defensively since LlamaModel base does not always
populate it depending on the version path.
* `builders/__init__.py`: export `SeedOssModel`.
* `builder.py`: route `SeedOssForCausalLM` to `SeedOssModel` in
`create_model` and add the import.
* `models/model_type.h`: register `seed_oss` in the LLM type array
(size 21 → 22) so the C++ runtime selects the standard
`DecoderOnly_Model` pipeline at load. No graph or KV-cache changes
in C++ are required — Seed-OSS's structural similarity to Llama
means the existing decoder-only runtime handles the resulting graph
transparently.
Validated on a CPU build target and on `-e dml` against
`NousResearch/Hermes-4.3-36B` (36B, FP16 BF16 source, INT4 output via
the `int4_block_size=32` path); the resulting graph loads cleanly
through `Microsoft.ML.OnnxRuntimeGenAI.DirectML 0.13.2` and produces
coherent generation on AMD DirectML hardware (RX 9060 XT, 16 GB VRAM).
Total delta is ~135 lines of Python plus a single one-line C++ array
update, with no kernel or runtime changes.
|
@microsoft-github-policy-service agree |
|
For transparency: the Seed-OSS architectural mapping in this PR — the |
| if not hasattr(self, "layer_attrs") or self.layer_attrs is None: | ||
| self.layer_attrs = {} | ||
|
|
||
| # SeedOss has per-layer post-norms after attention and MLP |
There was a problem hiding this comment.
Some of the Gemma models use a similar architecture for a post-normalization after the attention and FFN blocks. Would switching to inherit from one of those classes instead simplify the implementation of this class?
| onnx_model = GraniteModel(config, io_dtype, onnx_dtype, execution_provider, cache_dir, extra_options) | ||
| elif config.architectures[0] == "InternLM2ForCausalLM": | ||
| onnx_model = InternLM2Model(config, io_dtype, onnx_dtype, execution_provider, cache_dir, extra_options) | ||
| elif config.architectures[0] == "SeedOssForCausalLM": |
There was a problem hiding this comment.
Can we insert this in a way such that we maintain alphabetical order?
| @@ -1,3 +1,4 @@ | |||
| from .seed_oss import SeedOssModel | |||
There was a problem hiding this comment.
Can we insert this in a way such that we maintain alphabetical order?
There was a problem hiding this comment.
Pull request overview
Adds initial support for ByteDance Seed-OSS (SeedOssForCausalLM) to the Python model builder and updates the C++ model-type registry so the runtime can recognize the resulting genai_config.json as an LLM.
Changes:
- Introduces a new
SeedOssModelbuilder class (subclassingLlamaModel) intended to inject Seed-OSS post-norms in each decoder layer. - Routes
SeedOssForCausalLMto the new builder insrc/python/py/models/builder.pyand exposes it frombuilders. - Adds
seed_ossto the C++ModelType::IsLLMallowlist.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/python/py/models/builders/seed_oss.py | New Seed-OSS builder implementation overriding make_layer to add per-layer post-norms. |
| src/python/py/models/builders/init.py | Exposes the new builder from the builders package. |
| src/python/py/models/builder.py | Routes SeedOssForCausalLM to SeedOssModel and imports it. |
| src/models/model_type.h | Adds seed_oss to the runtime’s LLM type registry. |
Comments suppressed due to low confidence (4)
src/python/py/models/builders/seed_oss.py:101
attn_post_normis created usinglocation="post_attention", which collides with the existingpost_attention_layernormthat also useslocation="post_attention". Sincelocationis used to form node/initializer names, this will create duplicate initializer names and node names for the same layer.
self.make_layernorm(
layer_id,
attn_post_norm,
skip=True,
simple=self.layernorm_attrs["simple"],
location="post_attention",
)
# base.py accumulates into layernorm_attrs["output_0"]
# the attention output is still in the residual path
# ---- post_attention_layernorm (standard) ----
self.make_layernorm(
layer_id,
layer.post_attention_layernorm,
skip=True,
simple=self.layernorm_attrs["simple"],
location="post_attention",
)
src/python/py/models/builders/seed_oss.py:93
- The
attn_post_normpath callsmake_layernorm(..., skip=True, location=...), which builds a SkipLayerNorm (i.e., performs a residual add using the currentlayernorm_attrs[root_input]andlayernorm_attrs[skip_input]). Seed-OSS’attn_post_normis a post-norm on the attention output before the residual add, so this wiring will produce incorrect residual semantics (and can double-add the attention output). Please implementattn_post_normas a standalone norm on the attention output, then feed its output as theskip_inputto the subsequentpost_attention_layernormresidual op.
# ---- attn_post_norm (SeedOss specific) ----
if self.layer_attrs.get("has_attn_post_norm", False):
# Some HF weights flatten this into self_attn.post_attn_layernorm,
# others expose it as layer.attn_post_norm. Try both.
attn_post_norm = None
if hasattr(layer.self_attn, "post_attn_layernorm"):
attn_post_norm = layer.self_attn.post_attn_layernorm
elif hasattr(layer, "attn_post_norm"):
attn_post_norm = layer.attn_post_norm
if attn_post_norm is not None:
self.make_layernorm(
layer_id,
attn_post_norm,
skip=True,
simple=self.layernorm_attrs["simple"],
location="post_attention",
)
# base.py accumulates into layernorm_attrs["output_0"]
# the attention output is still in the residual path
src/python/py/models/builders/seed_oss.py:127
- Similarly,
ffn_post_normis currently built viamake_layernorm(..., skip=True, location="post_mlp"), which performs a residual add+norm using the current residual root and the MLP output. Seed-OSS needsffn_post_normapplied to the MLP output before it is added to the residual, so this will mis-wire the block and can cause double-adds on the next layer’s input SkipLayerNorm. Please applyffn_post_normas a standalone norm on the MLP output and use that as the subsequent residualskip_input.
# ---- ffn_post_norm (SeedOss specific) ----
if self.layer_attrs.get("has_ffn_post_norm", False):
ffn_post_norm = None
if hasattr(layer.mlp, "post_mlp_layernorm"):
ffn_post_norm = layer.mlp.post_mlp_layernorm
elif hasattr(layer, "ffn_post_norm"):
ffn_post_norm = layer.ffn_post_norm
if ffn_post_norm is not None:
self.make_layernorm(
layer_id,
ffn_post_norm,
skip=True,
simple=self.layernorm_attrs["simple"],
location="post_mlp",
)
src/python/py/models/builders/init.py:6
- This import was inserted before the module’s copyright/license header, which breaks the convention used across the repo (headers appear at the top of the file). Please move the import below the header block.
from .seed_oss import SeedOssModel
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------
| """ | ||
| SeedOssModel ONNX builder for ONNX Runtime GenAI. | ||
|
|
||
| SeedOss architecture (ByteDance Seed) is structurally similar to Llama with: | ||
| - attention_bias=True (QKV bias) | ||
| - attention_out_bias=False | ||
| - attn_post_norm per decoder layer (extra vs Llama) | ||
| - ffn_post_norm per decoder layer (extra vs Llama) | ||
| - GQA (num_key_value_heads != num_attention_heads) | ||
| - RoPE with rope_theta=1e7 | ||
|
|
||
| This builder inherits from LlamaModel and extends make_layer() to | ||
| inject the extra post-norms in the residual path. | ||
| """ |
| class SeedOssModel(LlamaModel): | ||
| def __init__(self, config, io_dtype, onnx_dtype, ep, cache_dir, extra_options): | ||
| super().__init__(config, io_dtype, onnx_dtype, ep, cache_dir, extra_options) | ||
|
|
||
| # LlamaModel base does not always populate layer_attrs (varies by | ||
| # ORT-GenAI version); ensure the dict exists before we set keys on it. | ||
| if not hasattr(self, "layer_attrs") or self.layer_attrs is None: | ||
| self.layer_attrs = {} | ||
|
|
||
| # SeedOss has per-layer post-norms after attention and MLP | ||
| # These bools inform the downstream graph builder to wire extra | ||
| # LayerNorm/RMSNorm nodes into the decoder block. | ||
| self.layer_attrs["has_attn_post_norm"] = getattr(config, "has_attn_post_norm", True) | ||
| self.layer_attrs["has_ffn_post_norm"] = getattr(config, "has_ffn_post_norm", True) | ||
|
|
||
| # attention_bias is auto-detected in base.py via hasattr on attn.q_proj.bias | ||
| # attention_out_bias is auto-detected similarly on attn.o_proj.bias | ||
| # We only need to set flags if the config explicitly overrides detection | ||
| self.attention_bias = getattr(config, "attention_bias", True) | ||
| self.attention_out_bias = getattr(config, "attention_out_bias", False) | ||
|
|
| # attention_bias is auto-detected in base.py via hasattr on attn.q_proj.bias | ||
| # attention_out_bias is auto-detected similarly on attn.o_proj.bias | ||
| # We only need to set flags if the config explicitly overrides detection | ||
| self.attention_bias = getattr(config, "attention_bias", True) | ||
| self.attention_out_bias = getattr(config, "attention_out_bias", False) |
| inline static bool IsLLM(const std::string& model_type) { | ||
| // Large-language model (LLM) | ||
| static constexpr std::array<std::string_view, 21> LLM = {"chatglm", "decoder", "ernie4_5", "gemma", "gemma2", "gemma3_text", "gpt2", "gptoss", "granite", "internlm2", "llama", "mistral", "nemotron", "olmo", "phi", "phimoe", "phi3", "phi3small", "qwen2", "qwen3", "smollm3"}; | ||
| static constexpr std::array<std::string_view, 22> LLM = {"chatglm", "decoder", "ernie4_5", "gemma", "gemma2", "gemma3_text", "gpt2", "gptoss", "granite", "internlm2", "llama", "mistral", "nemotron", "olmo", "phi", "phimoe", "phi3", "phi3small", "qwen2", "qwen3", "seed_oss", "smollm3"}; |
Thanks for your contribution! The proposed approach looks good to me. To answer your questions:
|
| inline static bool IsLLM(const std::string& model_type) { | ||
| // Large-language model (LLM) | ||
| static constexpr std::array<std::string_view, 21> LLM = {"chatglm", "decoder", "ernie4_5", "gemma", "gemma2", "gemma3_text", "gpt2", "gptoss", "granite", "internlm2", "llama", "mistral", "nemotron", "olmo", "phi", "phimoe", "phi3", "phi3small", "qwen2", "qwen3", "smollm3"}; | ||
| static constexpr std::array<std::string_view, 22> LLM = {"chatglm", "decoder", "ernie4_5", "gemma", "gemma2", "gemma3_text", "gpt2", "gptoss", "granite", "internlm2", "llama", "mistral", "nemotron", "olmo", "phi", "phimoe", "phi3", "phi3small", "qwen2", "qwen3", "seed_oss", "smollm3"}; |
There was a problem hiding this comment.
I believe the model builder will auto-create the model type as seedoss in the GenAI config.
| static constexpr std::array<std::string_view, 22> LLM = {"chatglm", "decoder", "ernie4_5", "gemma", "gemma2", "gemma3_text", "gpt2", "gptoss", "granite", "internlm2", "llama", "mistral", "nemotron", "olmo", "phi", "phimoe", "phi3", "phi3small", "qwen2", "qwen3", "seed_oss", "smollm3"}; | |
| static constexpr std::array<std::string_view, 22> LLM = {"chatglm", "decoder", "ernie4_5", "gemma", "gemma2", "gemma3_text", "gpt2", "gptoss", "granite", "internlm2", "llama", "mistral", "nemotron", "olmo", "phi", "phimoe", "phi3", "phi3small", "qwen2", "qwen3", "seedoss", "smollm3"}; |
What
Adds support for ByteDance's Seed-OSS architecture (
SeedOssForCausalLM) toonnxruntime_genai.models.builder. The motivating model isNousResearch/Hermes-4.3-36B(Hermes fine-tune of Seed-OSS-36B), which is currently rejected by the builder withNotImplementedError: model is not currently supported.Why
Seed-OSS is structurally a Llama-family decoder with two extra per-layer normalisations injected into the residual branches. Because the C++ runtime (
DecoderOnly_Model) is architecture-agnostic for decoder-only models — it loads and runs whatever ONNX graph the builder produces — the only blocker is the Python-side builder router and a one-line addition to the C++ LLM-type registry. No kernel changes, no KV cache changes, no graph executor changes.Architecture spec
Verified against the public Seed-OSS-36B config + the Hermes-4.3-36B fine-tune. Key invariants:
attention_biasTrue(QKV projection biases present)attention_out_biasFalse(o_projis clean)num_key_value_heads != num_attention_heads(8 KV / 80 Q on the 36B)rope_theta = 1e7(higher than Llama-3's5e5)attn_post_normafter attention,ffn_post_normafter MLP — both inside the residual pathsilu(same as Llama)The two post-norms are the only structural delta from Llama.
attention_biasis auto-detected by the existingbase.pylogic viahasattr(attn.q_proj, 'bias'), so no manual override is required at runtime — butSeedOssModel.__init__sets the explicit flag for clarity in case the heuristic is bypassed.Patch shape
src/python/py/models/builders/seed_oss.pySeedOssModel(LlamaModel)withmake_layeroverride that injectsattn_post_norm+ffn_post_normbetween attention/MLP and the residual add. Auto-detects post-norm tensor location (HF layouts vary:self_attn.post_attn_layernormvslayer.attn_post_norm— both supported).src/python/py/models/builder.pyfrom builders import (...)block, +1elifincreate_modelroutingSeedOssForCausalLMtoSeedOssModel.src/python/py/models/builders/__init__.pyfrom .seed_oss import SeedOssModel.src/models/model_type.h\"seed_oss\"to the LLM string array, alphabetically (between\"qwen3\"and\"smollm3\"); bumpstd::array<...,21>→<...,22>.Total: ~135 lines net, no C++ runtime changes beyond the type-registry string.
Validation
End-to-end build verified against
NousResearch/Hermes-4.3-36B:model.onnx(704 KB) +model.onnx.data(21 GB at INT4 + biases at f16)Microsoft.ML.OnnxRuntimeGenAI.DirectML 0.13.2C# bindings on AMD DirectML hardware (RX 9060 XT, 16 GB VRAM)DmlFusedNodeerrors, no shape mismatchesThe same patch produces a CUDA build cleanly (
-e cuda), and the 2-layer stub smoke-tests in our internal harness pass.A note on
genai_config.jsonmodel.typeAfter the builder writes
genai_config.json, themodel.typefield is set toseed_oss(matchingconfig.json'smodel_type). The C++ runtime recognises this once\"seed_oss\"is inIsLLM's array, so no manual config-rewrite is needed at inference time.(Operators on a release that doesn't yet include this PR can work around the C++ side by setting
model.type = \"llama\"ingenai_config.jsonafter the build — Seed-OSS is sufficiently Llama-shaped that the runtime accepts it. With this PR merged, that hack is unnecessary.)Things I'd appreciate maintainer guidance on
layer_attrsdefensive-init pattern.SeedOssModel.__init__includesif not hasattr(self, 'layer_attrs') or self.layer_attrs is None: self.layer_attrs = {}— needed because LlamaModel base initialisation paths vary across recent commits. Could be removed once base.py guarantees the attribute. Open to either approach.Out of scope (deliberately)
Spec material in this PR description was derived from internal architectural notes assembled while validating the patch against the 36B production model. Happy to expand any section, tighten the language, or restructure to match your contribution-guide template.