Skip to content

Set return_dict=True and remove output_hidden_states in Transformer - #7

Open
arthrod wants to merge 10 commits into
cicero-im:devfrom
mayank-rakesh-mck:main
Open

Set return_dict=True and remove output_hidden_states in Transformer#7
arthrod wants to merge 10 commits into
cicero-im:devfrom
mayank-rakesh-mck:main

Conversation

@arthrod

@arthrod arthrod commented Feb 17, 2026

Copy link
Copy Markdown
Collaborator

Summary by Sourcery

Bug Fixes:

  • Stop passing the output_hidden_states flag into the wrapped model call in the encoder, relying on the model's default behavior instead.

@sourcery-ai

sourcery-ai Bot commented Feb 17, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR modifies the encoder’s forward pass to stop explicitly requesting hidden states from the underlying model (relying on the model’s default behavior instead) and includes minor formatting adjustments in two files.

Class diagram for encoder and its interaction with underlying model

classDiagram
    class Encoder {
        +config
        +model
        +layers_fuser
        +forward(*args, **kwargs)
        +encode_text(input_ids, attention_mask, *args, **kwargs)
        +encode_labels(labels_input_ids, labels_attention_mask, *args, **kwargs)
    }

    class UnderlyingModel {
        +__call__(*args, output_hidden_states, return_dict, **kwargs)
    }

    class LayersFuser {
        +__call__(hidden_states)
    }

    Encoder --> UnderlyingModel : uses
    Encoder --> LayersFuser : uses when config.fuse_layers
Loading

File-Level Changes

Change Details Files
Stop explicitly passing output_hidden_states to the underlying model in the encoder forward method, relying on default model behavior for hidden state outputs.
  • Compute output_hidden_states flag from configuration but no longer pass it as an argument to the underlying model call (the argument is now commented out).
  • Continue to use output.hidden_states for layer fusion and mean pooling, assuming the model still returns them by default.
gliner/modeling/encoder.py
Minor formatting cleanup by ensuring files end with a newline.
  • Ensure encoder module file ends with a newline at EOF.
  • Ensure the custom training script file ends with a newline at EOF.
gliner/modeling/encoder.py
custom_train.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @arthrod, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily adjusts a model call by commenting out the output_hidden_states parameter in the Encoder class, which might be a temporary change for debugging or experimentation related to how hidden states are processed. Additionally, it includes minor code formatting improvements by removing trailing newlines from two files. The overall intent, beyond these specific changes, is not explicitly stated in the pull request title or description.

Highlights

  • Model Output Parameter Adjustment: The output_hidden_states parameter was commented out in a model call within the forward method of the Encoder class, potentially altering how intermediate layer outputs are handled.
  • Code Formatting Cleanup: Trailing newline characters were removed from the end of custom_train.py and gliner/modeling/encoder.py.
Changelog
  • custom_train.py
    • Removed trailing newline.
  • gliner/modeling/encoder.py
    • Modified model call in forward to comment out output_hidden_states parameter.
    • Removed trailing newline.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai

coderabbitai Bot commented Feb 17, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR applies minimal changes across two files. custom_train.py shows no functional differences. gliner/modeling/encoder.py modifies Transformer.forward to set return_dict=True and removes output_hidden_states keyword forwarding, alongside minor formatting adjustments.

Changes

Cohort / File(s) Summary
Training Script
custom_train.py
No functional changes detected; identical lines in diff.
Encoder Updates
gliner/modeling/encoder.py
Modified Transformer.forward to return dictionary format by setting return_dict=True and removed output_hidden_states forwarding. Minor formatting adjustment applied. BiEncoder.forward trailing newline adjusted.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A dictionary now flows so true,
Hidden states returned anew,
No more forwarding, clean and light,
The encoder now feels just right!
Simple tweaks, but oh so bright! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Merge Conflict Detection ⚠️ Warning ❌ Merge conflicts detected (43 files):

⚔️ .gitignore (content)
⚔️ README.md (content)
⚔️ configs/config.yaml (content)
⚔️ configs/config_biencoder.yaml (content)
⚔️ configs/config_span.yaml (content)
⚔️ configs/config_token.yaml (content)
⚔️ examples/convert_to_onnx.ipynb (content)
⚔️ examples/finetune.ipynb (content)
⚔️ examples/quickstart.ipynb (content)
⚔️ gliner/__init__.py (content)
⚔️ gliner/config.py (content)
⚔️ gliner/data_processing/__init__.py (content)
⚔️ gliner/data_processing/collator.py (content)
⚔️ gliner/data_processing/processor.py (content)
⚔️ gliner/data_processing/tokenizer.py (content)
⚔️ gliner/data_processing/utils.py (content)
⚔️ gliner/decoding/__init__.py (content)
⚔️ gliner/decoding/decoder.py (content)
⚔️ gliner/decoding/utils.py (content)
⚔️ gliner/evaluation/__init__.py (content)
⚔️ gliner/evaluation/evaluator.py (content)
⚔️ gliner/model.py (content)
⚔️ gliner/modeling/base.py (content)
⚔️ gliner/modeling/encoder.py (content)
⚔️ gliner/modeling/layers.py (content)
⚔️ gliner/modeling/loss_functions.py (content)
⚔️ gliner/modeling/scorers.py (content)
⚔️ gliner/modeling/span_rep.py (content)
⚔️ gliner/multitask/__init__.py (content)
⚔️ gliner/multitask/base.py (content)
⚔️ gliner/multitask/classification.py (content)
⚔️ gliner/multitask/open_extraction.py (content)
⚔️ gliner/multitask/question_answering.py (content)
⚔️ gliner/multitask/relation_extraction.py (content)
⚔️ gliner/multitask/summarization.py (content)
⚔️ gliner/onnx/model.py (content)
⚔️ gliner/training/__init__.py (content)
⚔️ gliner/training/trainer.py (content)
⚔️ gliner/utils.py (content)
⚔️ pyproject.toml (content)
⚔️ requirements.txt (content)
⚔️ tests/test_features_selection.py (content)
⚔️ train.py (content)

These conflicts must be resolved before merging into main.
Resolve conflicts locally and push changes to this branch.
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The pull request description clearly relates to the changeset, specifically addressing the removal of output_hidden_states parameter passing in the encoder model.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title accurately describes the main functional change: setting return_dict=True and removing output_hidden_states in the Transformer component, which matches the primary modification in encoder.py.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
⚔️ Resolve merge conflicts (beta)
  • Auto-commit resolved conflicts to branch main
  • Post resolved changes as copyable diffs in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Commenting out output_hidden_states in the call to self.model while still computing output_hidden_states above leaves an unused variable and may break fuse_layers logic that depends on hidden_states; either remove the variable and any dependent code or keep passing the flag explicitly.
  • The inline comment #output_hidden_states = output_hidden_states inside the argument list makes the call harder to read; if this is intentionally disabled, prefer removing the argument entirely or adding a clearer comment above the call explaining why hidden states are no longer requested.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Commenting out `output_hidden_states` in the call to `self.model` while still computing `output_hidden_states` above leaves an unused variable and may break `fuse_layers` logic that depends on `hidden_states`; either remove the variable and any dependent code or keep passing the flag explicitly.
- The inline comment `#output_hidden_states = output_hidden_states` inside the argument list makes the call harder to read; if this is intentionally disabled, prefer removing the argument entirely or adding a clearer comment above the call explaining why hidden states are no longer requested.

## Individual Comments

### Comment 1
<location> `gliner/modeling/encoder.py:100-101` </location>
<code_context>
         else:
             output_hidden_states = False
-        output = self.model(*args, output_hidden_states = output_hidden_states, 
+        output = self.model(*args, #output_hidden_states = output_hidden_states, 
                                             return_dict = True,  **kwargs)
         if self.config.fuse_layers:
             encoder_layer = self.layers_fuser(output.hidden_states)
</code_context>

<issue_to_address>
**issue (bug_risk):** Passing `output_hidden_states` is commented out but related logic is still active, which may break `fuse_layers` behavior.

Since `output_hidden_states` is still derived from `self.config.fuse_layers` but no longer passed to `self.model`, `output.hidden_states` may be absent if the model’s default is `output_hidden_states=False`, breaking or degrading `self.layers_fuser`. Either remove the now-dead `output_hidden_states` logic and explicitly handle the `fuse_layers` path, or restore/gate the argument instead of commenting it out in place.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +100 to 101
output = self.model(*args, #output_hidden_states = output_hidden_states,
return_dict = True, **kwargs)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Passing output_hidden_states is commented out but related logic is still active, which may break fuse_layers behavior.

Since output_hidden_states is still derived from self.config.fuse_layers but no longer passed to self.model, output.hidden_states may be absent if the model’s default is output_hidden_states=False, breaking or degrading self.layers_fuser. Either remove the now-dead output_hidden_states logic and explicitly handle the fuse_layers path, or restore/gate the argument instead of commenting it out in place.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to adjust how output_hidden_states is handled in the encoder. However, the current change of commenting out the output_hidden_states parameter is likely to introduce a bug. When layer fusion is enabled, the model needs to output hidden states, but with this change, it will probably not, leading to a runtime error. I've provided a critical review comment with a suggested fix that not only corrects the issue but also simplifies the related code. The other changes are minor stylistic improvements, like adding newlines at the end of files, which are good practices.

else:
output_hidden_states = False
output = self.model(*args, output_hidden_states = output_hidden_states,
output = self.model(*args, #output_hidden_states = output_hidden_states,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This change introduces a potential bug. By commenting out output_hidden_states, the model will rely on its default configuration. If self.config.fuse_layers is True (checked on line 96), line 103 accesses output.hidden_states, which will likely not be present in the model's output, causing an AttributeError at runtime. The output_hidden_states parameter must be explicitly set to True when layer fusion is enabled.

Instead of commenting out this line, the logic should be corrected and simplified. The entire block from line 96 to 101 can be replaced with:

        output = self.model(
            *args,
            output_hidden_states=self.config.fuse_layers,
            return_dict=True,
            **kwargs
        )

This is more concise and ensures output_hidden_states is correctly passed when needed.

@coderabbitai coderabbitai Bot changed the title cr Set return_dict=True and remove output_hidden_states in Transformer Feb 17, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e017e04117

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +100 to 101
output = self.model(*args, #output_hidden_states = output_hidden_states,
return_dict = True, **kwargs)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Pass hidden states when layer fusion is enabled

When fuse_layers is enabled, this forward path still computes output_hidden_states = True but no longer forwards it to the transformer call, so output.hidden_states is typically None for standard HF configs and self.layers_fuser(output.hidden_states) will fail at runtime (or produce invalid fusion input) for any run using layer fusion. This change effectively breaks the fuse_layers feature introduced in this module.

Useful? React with 👍 / 👎.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
gliner/modeling/encoder.py (1)

100-105: ⚠️ Potential issue | 🔴 Critical

Critical bug: output_hidden_states is computed but never passed to the model.

When fuse_layers is enabled, the code sets output_hidden_states = True (line 97) but the parameter is commented out in the model call (line 100). This means output.hidden_states on line 103 will be None, causing the fuse_layers feature to fail at runtime.

The commented-out argument should be restored.

🐛 Proposed fix
-        output = self.model(*args, `#output_hidden_states` = output_hidden_states, 
-                                            return_dict = True,  **kwargs)
+        output = self.model(*args, output_hidden_states=output_hidden_states, 
+                            return_dict=True, **kwargs)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@gliner/modeling/encoder.py` around lines 100 - 105, The model call is not
receiving the computed output_hidden_states flag so output.hidden_states is None
when self.config.fuse_layers is true; restore the argument by passing
output_hidden_states=output_hidden_states into the self.model(...) call
(alongside return_dict=True and **kwargs) so that output.hidden_states is
populated for layers_fuser; update the call in encoder where self.model is
invoked and remove the commented-out parameter to ensure
layers_fuser(output.hidden_states) can run.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@gliner/modeling/encoder.py`:
- Around line 100-105: The model call is not receiving the computed
output_hidden_states flag so output.hidden_states is None when
self.config.fuse_layers is true; restore the argument by passing
output_hidden_states=output_hidden_states into the self.model(...) call
(alongside return_dict=True and **kwargs) so that output.hidden_states is
populated for layers_fuser; update the call in encoder where self.model is
invoked and remove the commented-out parameter to ensure
layers_fuser(output.hidden_states) can run.

@arthrod
arthrod changed the base branch from main to dev February 17, 2026 16:56
@arthrod

arthrod commented Feb 17, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review carefully again after these edits

@coderabbitai

coderabbitai Bot commented Feb 17, 2026

Copy link
Copy Markdown

@arthrod: I'll perform a careful review of the changes in this PR right away.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants