(pretrained=False,
pretrained_name=None,
model_cls=CLIP,
return_transforms=False,
return_tokenizer=False,
tokenizer_padding='eos',
dtype=torch.float32,
device='cpu',
**kwargs)
| 736 | |
| 737 | |
| 738 | def _clip(pretrained=False, |
| 739 | pretrained_name=None, |
| 740 | model_cls=CLIP, |
| 741 | return_transforms=False, |
| 742 | return_tokenizer=False, |
| 743 | tokenizer_padding='eos', |
| 744 | dtype=torch.float32, |
| 745 | device='cpu', |
| 746 | **kwargs): |
| 747 | # init model |
| 748 | if pretrained and pretrained_name: |
| 749 | from sora import BUCKET, DOWNLOAD_TO_CACHE |
| 750 | |
| 751 | # init a meta model |
| 752 | with torch.device('meta'): |
| 753 | model = model_cls(**kwargs) |
| 754 | |
| 755 | # checkpoint path |
| 756 | checkpoint = f'models/clip/{pretrained_name}' |
| 757 | if dtype in (torch.float16, torch.bfloat16): |
| 758 | suffix = '-' + { |
| 759 | torch.float16: 'fp16', |
| 760 | torch.bfloat16: 'bf16' |
| 761 | }[dtype] |
| 762 | if object_exists(BUCKET, f'{checkpoint}{suffix}.pth'): |
| 763 | checkpoint = f'{checkpoint}{suffix}' |
| 764 | checkpoint += '.pth' |
| 765 | |
| 766 | # load |
| 767 | model.load_state_dict( |
| 768 | torch.load(DOWNLOAD_TO_CACHE(checkpoint), map_location=device), |
| 769 | assign=True, |
| 770 | strict=False) |
| 771 | else: |
| 772 | # init a model on device |
| 773 | with torch.device(device): |
| 774 | model = model_cls(**kwargs) |
| 775 | |
| 776 | # set device |
| 777 | output = (model,) |
| 778 | |
| 779 | # init transforms |
| 780 | if return_transforms: |
| 781 | # mean and std |
| 782 | if 'siglip' in pretrained_name.lower(): |
| 783 | mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5] |
| 784 | else: |
| 785 | mean = [0.48145466, 0.4578275, 0.40821073] |
| 786 | std = [0.26862954, 0.26130258, 0.27577711] |
| 787 | |
| 788 | # transforms |
| 789 | transforms = T.Compose([ |
| 790 | T.Resize((model.image_size, model.image_size), |
| 791 | interpolation=T.InterpolationMode.BICUBIC), |
| 792 | T.ToTensor(), |
| 793 | T.Normalize(mean=mean, std=std) |
| 794 | ]) |
| 795 | output += (transforms,) |
no test coverage detected