Как возможно, что переменная поля становится None сразу после строки присваивания?
В частности, печатается следующий код
CLIPTokenizerFast NoneType CLIPTextModelWithProjection
при запуске _setup_txt_encoder
self.txt_encoder должна быть той же переменной, что и txt_encoder, но при доступе она извлекается как NoneTypeПохоже, что наследование от torch.nn.Module вызывает проблему, потому что если я удалю его из наследования класса, проблем не будет.
from transformers import AutoTokenizer, PreTrainedTokenizer, PreTrainedTokenizerFast, CLIPTextModelWithProjection
from typing import Optional
from torch.nn import Module
class Foo:
def _setup_txt_encoder(self, clip_txt_model_name: str):
print('loading text tokenizer and encoder')
tokenizer = AutoTokenizer.from_pretrained(clip_txt_model_name, clean_up_tokenization_spaces=True)
txt_encoder = CLIPTextModelWithProjection.from_pretrained(clip_txt_model_name).requires_grad_(False)
self.tokenizer, self.txt_encoder = tokenizer, txt_encoder
self.txt_encoder = txt_encoder
print(type(self.tokenizer).__name__, type(self.txt_encoder).__name__, type(txt_encoder).__name__)
return tokenizer, txt_encoder
tokenizer: Optional[PreTrainedTokenizer|PreTrainedTokenizerFast] = None
txt_encoder: Optional[CLIPTextModelWithProjection] = None
class Bar(Module, Foo):
def __init__(self, clip_txt_model_name='laion/CLIP-ViT-bigG-14-laion2B-39B-b160k'):
super().__init__()
if self.tokenizer is None or self.txt_encoder is None:
self._setup_txt_encoder(clip_txt_model_name)
test = Bar()
РЕДАКТИРОВАТЬ: еще немного информации. Это действительно странно: следующее возвращает разные вещи
print(getattr(test,'txt_encoder'))
print(test.__getattr__('txt_encoder'))
выходы
None
CLIPTextModelWithProjection(
(text_model): CLIPTextTransformer(
...
Подробнее здесь: https://stackoverflow.com/questions/791 ... ight-after
Модуль torch приводит к тому, что переменная поля модели становится None по ссылке сразу после назначения ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение