Repository navigation
Add GS1 decision model support #50921
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
Open
jacklanda
wants to merge
18
commits into
huggingface:main
Choose a base branch
from
jacklanda:add-gs1-decision-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,431
−2
Open
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1ed24a5
docs(gs1): add architecture proposal
jacklanda 944bf27
docs(gs1): sort model table of contents entry
jacklanda 82f68ad
Merge branch 'main' into add-gs1-decision-model
jacklanda 61c0970
Merge branch 'main' into add-gs1-decision-model
jacklanda 5135f78
Fix GS1 docs repository consistency check
jacklanda b1f4288
Add GS1 decision model support
jacklanda 05aacd2
Merge branch 'main' into add-gs1-decision-model
jacklanda 1103ce2
Fix GS1 CI and repository consistency
jacklanda 075bb5d
Merge branch 'main' into add-gs1-decision-model
jacklanda e46b1c7
Merge branch 'main' into add-gs1-decision-model
jacklanda 50f18d9
docs: refresh GS1 contribution date
jacklanda f336d3f
Merge branch 'main' into add-gs1-decision-model
jacklanda 0deab8c
Merge branch 'main' into add-gs1-decision-model
jacklanda 8b4ed35
Simplify GS1 modeling and reuse activation checkpointing
jacklanda ae35052
Fix published GS1 checkpoint loading and document decision usage
jacklanda c7ec566
Merge branch 'main' into add-gs1-decision-model
jacklanda 9e4b0e2
Merge branch 'main' into add-gs1-decision-model
jacklanda e8a8745
Merge branch 'main' into add-gs1-decision-model
jacklanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <!--Copyright 2026 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| ⚠️ Note that this file is in Markdown but contains specific syntax for the doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
|
|
||
| # GS1 | ||
|
|
||
| GS1 is a family of pretrained models from BIGAI for structured decision tasks. Given a state and a question, GS1 | ||
| scores the available candidates for Choice, Noul, and Score questions instead of generating a text answer. | ||
|
jacklanda marked this conversation as resolved.
Resolved
|
||
|
|
||
| The models use a Qwen3.5 hybrid text/vision backbone with GS1-specific bidirectional decision-context processing, | ||
|
jacklanda marked this conversation as resolved.
Resolved
|
||
| attention-residual routing, and a cross-attention decision head. The head reads the question and candidate spans from | ||
| the compiled request and returns one score per candidate. | ||
|
jacklanda marked this conversation as resolved.
Resolved
|
||
|
|
||
| | Model | Checkpoint | | ||
| |---|---| | ||
| | GS1-0.8B | [bigai/GS1-0.8B](https://huggingface.co/bigai/GS1-0.8B) | | ||
| | GS1-2B | [bigai/GS1-2B](https://huggingface.co/bigai/GS1-2B) | | ||
| | GS1-4B | [bigai/GS1-4B](https://huggingface.co/bigai/GS1-4B) | | ||
| | GS1-9B | [bigai/GS1-9B](https://huggingface.co/bigai/GS1-9B) | | ||
|
|
||
| GS1 is developed by [BIGAI](https://gs1.mybigai.ac.cn). See the [open-source implementation](https://github.com/bigai-nlco/GS1) | ||
|
jacklanda marked this conversation as resolved.
Resolved
|
||
| for the model and inference reference. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| def GS1ForDecisionLoss( | ||
| logits: torch.Tensor, | ||
| labels: torch.Tensor, | ||
| num_options: torch.Tensor, | ||
| loss_weights: torch.Tensor | None = None, | ||
| brier_weight: float = 1.0, | ||
| num_items_in_batch: torch.Tensor | int | None = None, | ||
| **kwargs, | ||
| ) -> torch.Tensor: | ||
| """Mean distribution NLL plus expected multiclass Brier, one unit per question.""" | ||
| logits = logits.float() | ||
| labels = labels.to(device=logits.device).reshape(-1) | ||
| num_options = num_options.to(device=logits.device, dtype=torch.long).reshape(-1) | ||
| if num_options.numel() == 0 or (num_options < 1).any() or num_options.sum() != logits.numel(): | ||
| raise ValueError("num_options must partition the flat candidate logits") | ||
| if not labels.is_floating_point() or labels.numel() != logits.numel(): | ||
| raise ValueError("GS1 targets must provide a floating-point distribution per active candidate") | ||
| labels = labels.float() | ||
| if not torch.isfinite(labels).all() or (labels < 0).any(): | ||
| raise ValueError("Decision targets must be finite and nonnegative") | ||
|
|
||
| losses, offset = [], 0 | ||
| for index, count in enumerate(num_options.tolist()): | ||
| scores = logits[offset : offset + count] | ||
| target = labels[offset : offset + count] | ||
| if not torch.isclose(target.sum(), target.new_tensor(1.0), atol=1e-6, rtol=0): | ||
| raise ValueError("Each target distribution must sum to one") | ||
| log_probability = scores.log_softmax(-1) | ||
| probability = log_probability.exp() | ||
| nll = -(target * log_probability).sum() | ||
| brier = (probability - target).square().sum() + 1 - target.square().sum() | ||
| weight = 1.0 if loss_weights is None else loss_weights.reshape(-1)[index].to(logits.device) | ||
| losses.append((nll + brier_weight * brier) * weight) | ||
| offset += count | ||
|
|
||
| loss = torch.stack(losses).sum() | ||
| if num_items_in_batch is not None: | ||
| denominator = torch.as_tensor(num_items_in_batch, device=logits.device).clamp_min(1) | ||
| return loss / denominator | ||
| return loss / len(losses) | ||
|
|
||
|
|
||
| __all__ = ["GS1ForDecisionLoss"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # This file was automatically generated from the modules in `src/transformers/models/gs1`. | ||
| # Do NOT edit this file manually as any edits will be overwritten by auto-generation of the file. | ||
| # A module is picked up once it exports a name through `__all__`. | ||
| # Regenerate the file with: `python utils/check_inits.py --fix_and_overwrite` | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils import _LazyModule | ||
| from ...utils.import_utils import define_import_structure | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration_gs1 import * | ||
| from .modeling_gs1 import * | ||
| else: | ||
| import sys | ||
|
|
||
| _file = globals()["__file__"] | ||
| sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # This file was automatically generated from src/transformers/models/gs1/modular_gs1.py. | ||
| # Do NOT edit this file manually as any edits will be overwritten by the generation of | ||
| # the file from the modular. If any change should be done, please apply the change to the | ||
| # modular_gs1.py file directly. One of our CI enforces this. | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| import math | ||
| from dataclasses import field | ||
|
|
||
| from huggingface_hub.dataclasses import strict | ||
|
|
||
| from ...configuration_utils import PreTrainedConfig, SubConfigSpec | ||
| from ...utils import auto_docstring | ||
|
|
||
| GS1_DEFAULTS = { | ||
| "attnres": True, | ||
| "attnres_layers": None, | ||
| "attnres_eps": 1e-6, | ||
| "bidirectional": True, | ||
| "bidirectional_layers": None, | ||
| "cross_attention_heads": 8, | ||
| "cross_attention_layers": 2, | ||
| "projection_size": 256, | ||
| "norm_eps": 1e-5, | ||
| } | ||
|
|
||
|
|
||
| @auto_docstring(checkpoint="bigai/GS1-9B") | ||
| @strict | ||
| class GS1Config(PreTrainedConfig): | ||
| r"""Configuration for the GS1 structured decision model. | ||
|
|
||
| Args: | ||
| decision (`dict`, *optional*): GS1 routing and decision-head dimensions. | ||
| calibration (`dict`, *optional*): Calibration mode used by GS1 inference. | ||
| initialization_seed (`int`, *optional*, defaults to 12): Initialization seed stored with the checkpoint. | ||
| head_compute_dtype (`str`, *optional*, defaults to `"float32"`): Decision-head compute dtype. | ||
| structural_token_ids (`dict`, *optional*): IDs of the seven GS1 structural tokens. | ||
| inference_limits (`dict`, *optional*): Maximum context, request, question, and branch sizes. | ||
| decision_brier_weight (`float`, *optional*, defaults to 1.0): Expected multiclass Brier coefficient. | ||
| """ | ||
|
|
||
| model_type = "gs1" | ||
| sub_configs_defaults = { | ||
| "text_config": SubConfigSpec(model_type="qwen3_5_text"), | ||
| "vision_config": SubConfigSpec(model_type="qwen3_5_vision"), | ||
| } | ||
|
|
||
| text_config: dict | PreTrainedConfig | None = None | ||
| vision_config: dict | PreTrainedConfig | None = None | ||
| image_token_id: int = 248056 | ||
| video_token_id: int = 248057 | ||
| vision_start_token_id: int = 248053 | ||
| vision_end_token_id: int = 248054 | ||
| tie_word_embeddings: bool = False | ||
| decision: dict = field(default_factory=dict) | ||
| calibration: dict = field(default_factory=dict) | ||
| initialization_seed: int = 12 | ||
| head_compute_dtype: str = "float32" | ||
| structural_token_ids: dict | None = None | ||
| inference_limits: dict = field(default_factory=dict) | ||
| decision_brier_weight: float = 1.0 | ||
|
|
||
| def __post_init__(self, **kwargs): | ||
| super().__post_init__(**kwargs) | ||
| self.decision = {**GS1_DEFAULTS, **(self.decision or {})} | ||
| self.calibration = {"mode": "identity", "log_temperature_bound": math.log(20), **self.calibration} | ||
| if self.head_compute_dtype not in {"float32", "bfloat16"}: | ||
| raise ValueError("head_compute_dtype must be float32 or bfloat16") | ||
| if self.text_config.hidden_size % self.decision["cross_attention_heads"]: | ||
| raise ValueError("cross_attention_heads must divide the text hidden size") | ||
| self.tie_word_embeddings = False | ||
| self.text_config.use_cache = False | ||
|
|
||
|
|
||
| __all__ = ["GS1Config"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.