Skip to content

[Fix] Add setter for config property to fix optimum ONNX export#3831

Open
lcheng321 wants to merge 1 commit into
huggingface:mainfrom
lcheng321:patch-2
Open

[Fix] Add setter for config property to fix optimum ONNX export#3831
lcheng321 wants to merge 1 commit into
huggingface:mainfrom
lcheng321:patch-2

Conversation

@lcheng321

@lcheng321 lcheng321 commented Jun 20, 2026

Copy link
Copy Markdown

Problem

SentenceTransformer.config was added in v5.5.0 as a read-only property. optimum's standardize_model_attributes assigns model.config = model[0].auto_model.config during ONNX export, which raises AttributeError. Works fine with sentence-transformers==5.4.1.

Error:

AttributeError: property 'config' of 'SentenceTransformer' object has no setter

Root Cause

sentence_transformers/base/model.py defines config with only a @property getter and no setter. When optimum attempts to assign model.config during export, Python raises AttributeError.

Fix

Add a @config.setter at line 1588 of sentence_transformers/base/model.py that delegates to the underlying transformer model, consistent with the existing tokenizer and max_seq_length setters.

Verification

Verified without internet or GPU (pure Python, no hardware required).

Test 1 — Static AST check (before vs after):

python - << 'EOF'
import subprocess, ast, pathlib, sys
FILE = "sentence_transformers/base/model.py"
def has_setter(src):
    for n in ast.walk(ast.parse(src)):
        if isinstance(n, ast.FunctionDef) and n.name == "config":
            if any(isinstance(d, ast.Attribute) and d.attr == "setter" for d in n.decorator_list):
                return True, n.lineno
    return False, None
r = subprocess.run(["git","show",f"main:{FILE}"], capture_output=True, text=True)
b, _ = has_setter(r.stdout) if r.returncode==0 else (False, None)
a, ln = has_setter(pathlib.Path(FILE).read_text(encoding="utf-8"))
print(f"BEFORE (main):    {'[PASS]' if b else '[FAIL] no @config.setter'}")
print(f"AFTER  (patch-2): {'[PASS] @config.setter at line '+str(ln) if a else '[FAIL] not found'}")
print(f"RESULT: {'Fix confirmed' if not b and a else 'Unexpected'}")
EOF

Output:

BEFORE (main):    [FAIL] no @config.setter
AFTER  (patch-2): [PASS] @config.setter at line 1588
RESULT: Fix confirmed

Test 2 — Behavioral simulation (optimum assignment):

python - << 'EOF'
import ast, pathlib, sys
FILE = "sentence_transformers/base/model.py"
class _B:
    @property
    def config(self): return {}
class _A:
    def __init__(self): self._c = {}
    @property
    def config(self): return self._c
    @config.setter
    def config(self, v): self._c = v
try: _B().config = {}; b = "[?] unexpected"
except AttributeError as e: b = f"[FAIL] AttributeError -> export crashes: {e}"
try: o=_A(); o.config={"x":1}; assert o.config=={"x":1}; a="[PASS] setter works"
except Exception as e: a = f"[FAIL] {e}"
src = pathlib.Path(FILE).read_text(encoding="utf-8")
found, ln = next(((True,n.lineno) for n in ast.walk(ast.parse(src))
    if isinstance(n,ast.FunctionDef) and n.name=="config"
    and any(isinstance(d,ast.Attribute) and d.attr=="setter" for d in n.decorator_list)),(False,None))
print(f"BEFORE: {b}\nAFTER:  {a}")
print(f"SOURCE: {'[PASS] @config.setter at line '+str(ln) if found else '[FAIL] not found'}")
ok = "[FAIL]" in b and "[PASS]" in a and found
print(f"RESULT: {'Fix verified end-to-end' if ok else 'Unexpected'}")
EOF

Output:

BEFORE: [FAIL] AttributeError -> export crashes: property 'config' of '_B' object has no setter
AFTER:  [PASS] setter works
SOURCE: [PASS] @config.setter at line 1588
RESULT: Fix verified end-to-end

Screenshots attached in comments.

Fixes #3830

@lcheng321

Copy link
Copy Markdown
Author

Test output on Windows:
Snipaste_2026-06-21_18-22-36

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.

Optimum ONNX Export with SentenceTransformer results in property 'config' has no setter

1 participant