huggingface / huggingface/diffusers

Confusion about `FrozenDict` in `configuration_utils.py`

Đang mở
#9,503 3 bình luận 0 reaction 0 người được giao Xem trên GitHub
stale
Ngôn ngữ chính
Python
Star
34.5k
Fork
7.3k
Merge trung bình
3 ngày 3 giờ
Pull request đã merge (30 ngày)
91

Mô tả

I am confused about the design of `FrozenDict` in `configuration_utils.py` and the usage of it.

### 1. Is `FrozenDict` really frozen?
From the code, `FrozenDict` sets `self.__frozen = True` during initialization. It then checks `if hasattr(self, "__frozen") and self.__frozen` in methods like `__setattr__` or `__setitem__` to raise an exception if it's supposed to be frozen. However, in Python, using double underscores (`__`) triggers name mangling, which means `hasattr(self, "__frozen")` couldn't find `__frozen` as it is `_FrozenDict__frozen` actually. This check would always return False, rendering the intended freeze ineffective – `__setattr__` and `__setitem__` can still be used, making the `FrozenDict` not truly frozen.

Is this what we expect?

### 2. If we were to modify `FrozenDict` to be truly immutable, how would we use it as `model.config`?
Typically, we register parameters needed for model initialization as a `FrozenDict` via `register_to_config`. These are often accessed during the forward method with checks like `if self.config.xxx == xxx` to determine execution paths. However, in some models, certain properties of `self.config` might need to be altered after initialization, as seen in [IP-Adapter](https://github.com/huggingface/diffusers/blob/main/src/diffusers/loaders/unet.py#L851)
```py
self.config.encoder_hid_dim_type = "ip_image_proj"
```
Does this contradict the design philosophy of `FrozenDict`?

### 3. If `models.config` could be mutable, how should one go about changing it?

In the example above, should we use `__setattr__` to modify `self.config.encoder_hid_dim_type` or `__setitem__` to add an additional key-value pair to `FrozenDict` which appears more in line with typical dictionary usage, like
```diff
- self.config.encoder_hid_dim_type = "ip_image_proj"
+ self.config['encoder_hid_dim_type'] = "ip_image_proj"
```



I was just wondering if you might have a moment to clarify these points for me?

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Start by reading configuration_utils.py, then compare FrozenDict's behavior with the self.config.encoder_hid_dim_type assignment in loaders/unet.py. Clarify whether the intended outcome is immutable configuration, supported mutation, or documentation of current behavior; the issue is done only once that scope and expected behavior are decided.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
machine-learning
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.