Reference for using, reviewing, or modifying unitorch's @replace decorator in src/unitorch/utils/decorators.py. Use when overriding upstream classes, adding replacement classes under modules/replace, reasoning about process-global monkey patches, or debugging import-time replacement and subclass __bases__ rewriting.
설치
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
Reference for using, reviewing, or modifying unitorch's @replace decorator in src/unitorch/utils/decorators.py. Use when overriding upstream classes, adding replacement classes under modules/replace, reasoning about process-global monkey patches, or debugging import-time replacement and subclass __bases__ rewriting.
@replace Decorator Reference
@replace is defined in src/unitorch/utils/decorators.py.
Signature
from unitorch.utils.decorators import replace
@replace(TargetClass)classReplacementClass(TargetClass):
...
Mechanism
Record TargetClass -> ReplacementClass in the module-level
OPTIMIZED_CLASSES dict.
Set ReplacementClass.__replaced_class__ = TargetClass.
Walk sys.modules and:
Replace every module-level name that equals TargetClass with
ReplacementClass.
Rewrite __bases__ of any class that inherits from TargetClass.
When To Use
Use @replace when you need to override upstream library behavior such as
HuggingFace diffusers or datasets without forking the library or changing
call sites. The replacement class typically:
Inherits from the target to reuse its logic.
Overrides specific methods to fix bugs, skip validation, or add features.
Override HuggingFace datasets iterables for fast skip support.
Replacement classes are named <Original>V2 by convention and are decorated
immediately after their definition.
Important Constraints
The @replace call must happen at module import time. Do not put it inside a
function or conditional block.
The replacement runs once when the module is first imported. Re-importing has
no additional effect, although a warning is logged if the same target is
replaced twice.
The replacement is process-global. It affects every consumer of the patched
module in the same Python process.
Always inherit from the target class so that isinstance checks and
super() calls continue to work correctly.