text
stringlengths
7
1.24M
id
stringlengths
14
166
metadata
dict
__index_level_0__
int64
0
519
# ResNeSt A **ResNeSt** is a variant on a [ResNet](https://paperswithcode.com/method/resnet), which instead stacks [Split-Attention blocks](https://paperswithcode.com/method/split-attention). The cardinal group representations are then concatenated along the channel dimension: \\( V = \text{Concat} \\){\\( V^{1},V^{2},\cdots{V}^{K} \\)}. As in standard residual blocks, the final output \\( Y \\) of otheur Split-Attention block is produced using a shortcut connection: \\( Y=V+X \\), if the input and output feature-map share the same shape. For blocks with a stride, an appropriate transformation \\( \mathcal{T} \\) is applied to the shortcut connection to align the output shapes: \\( Y=V+\mathcal{T}(X) \\). For example, \\( \mathcal{T} \\) can be strided convolution or combined convolution-with-pooling. ## How do I use this model on an image? To load a pretrained model: ```py >>> import timm >>> model = timm.create_model('resnest101e', pretrained=True) >>> model.eval() ``` To load and preprocess the image: ```py >>> import urllib >>> from PIL import Image >>> from timm.data import resolve_data_config >>> from timm.data.transforms_factory import create_transform >>> config = resolve_data_config({}, model=model) >>> transform = create_transform(**config) >>> url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") >>> urllib.request.urlretrieve(url, filename) >>> img = Image.open(filename).convert('RGB') >>> tensor = transform(img).unsqueeze(0) # transform and add batch dimension ``` To get the model predictions: ```py >>> import torch >>> with torch.no_grad(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) >>> # prints: torch.Size([1000]) ``` To get the top-5 predictions class names: ```py >>> # Get imagenet class mappings >>> url, filename = ("https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt", "imagenet_classes.txt") >>> urllib.request.urlretrieve(url, filename) >>> with open("imagenet_classes.txt", "r") as f: ... categories = [s.strip() for s in f.readlines()] >>> # Print top categories per image >>> top5_prob, top5_catid = torch.topk(probabilities, 5) >>> for i in range(top5_prob.size(0)): ... print(categories[top5_catid[i]], top5_prob[i].item()) >>> # prints class names and probabilities like: >>> # [('Samoyed', 0.6425196528434753), ('Pomeranian', 0.04062102362513542), ('keeshond', 0.03186424449086189), ('white wolf', 0.01739676296710968), ('Eskimo dog', 0.011717947199940681)] ``` Replace the model name with the variant you want to use, e.g. `resnest101e`. You can find the IDs in the model summaries at the top of this page. To extract image features with this model, follow the [timm feature extraction examples](../feature_extraction), just change the name of the model you want to use. ## How do I finetune this model? You can finetune any of the pre-trained models just by changing the classifier (the last layer). ```py >>> model = timm.create_model('resnest101e', pretrained=True, num_classes=NUM_FINETUNE_CLASSES) ``` To finetune on your own dataset, you have to write a training loop or adapt [timm's training script](https://github.com/rwightman/pytorch-image-models/blob/master/train.py) to use your dataset. ## How do I train this model? You can follow the [timm recipe scripts](../scripts) for training a new model afresh. ## Citation ```BibTeX @misc{zhang2020resnest, title={ResNeSt: Split-Attention Networks}, author={Hang Zhang and Chongruo Wu and Zhongyue Zhang and Yi Zhu and Haibin Lin and Zhi Zhang and Yue Sun and Tong He and Jonas Mueller and R. Manmatha and Mu Li and Alexander Smola}, year={2020}, eprint={2004.08955}, archivePrefix={arXiv}, primaryClass={cs.CV} } ``` <!-- Type: model-index Collections: - Name: ResNeSt Paper: Title: 'ResNeSt: Split-Attention Networks' URL: https://paperswithcode.com/paper/resnest-split-attention-networks Models: - Name: resnest101e In Collection: ResNeSt Metadata: FLOPs: 17423183648 Parameters: 48280000 File Size: 193782911 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest101e LR: 0.1 Epochs: 270 Layers: 101 Dropout: 0.2 Crop Pct: '0.875' Momentum: 0.9 Batch Size: 4096 Image Size: '256' Weight Decay: 0.0001 Interpolation: bilinear Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L182 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-resnest/resnest101-22405ba7.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 82.88% Top 5 Accuracy: 96.31% - Name: resnest14d In Collection: ResNeSt Metadata: FLOPs: 3548594464 Parameters: 10610000 File Size: 42562639 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest14d LR: 0.1 Epochs: 270 Layers: 14 Dropout: 0.2 Crop Pct: '0.875' Momentum: 0.9 Batch Size: 8192 Image Size: '224' Weight Decay: 0.0001 Interpolation: bilinear Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L148 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/gluon_resnest14-9c8fe254.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 75.51% Top 5 Accuracy: 92.52% - Name: resnest200e In Collection: ResNeSt Metadata: FLOPs: 45954387872 Parameters: 70200000 File Size: 193782911 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest200e LR: 0.1 Epochs: 270 Layers: 200 Dropout: 0.2 Crop Pct: '0.909' Momentum: 0.9 Batch Size: 2048 Image Size: '320' Weight Decay: 0.0001 Interpolation: bicubic Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L194 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-resnest/resnest101-22405ba7.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 83.85% Top 5 Accuracy: 96.89% - Name: resnest269e In Collection: ResNeSt Metadata: FLOPs: 100830307104 Parameters: 110930000 File Size: 445402691 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest269e LR: 0.1 Epochs: 270 Layers: 269 Dropout: 0.2 Crop Pct: '0.928' Momentum: 0.9 Batch Size: 2048 Image Size: '416' Weight Decay: 0.0001 Interpolation: bicubic Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L206 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-resnest/resnest269-0cc87c48.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 84.53% Top 5 Accuracy: 96.99% - Name: resnest26d In Collection: ResNeSt Metadata: FLOPs: 4678918720 Parameters: 17070000 File Size: 68470242 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest26d LR: 0.1 Epochs: 270 Layers: 26 Dropout: 0.2 Crop Pct: '0.875' Momentum: 0.9 Batch Size: 8192 Image Size: '224' Weight Decay: 0.0001 Interpolation: bilinear Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L159 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/gluon_resnest26-50eb607c.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 78.48% Top 5 Accuracy: 94.3% - Name: resnest50d In Collection: ResNeSt Metadata: FLOPs: 6937106336 Parameters: 27480000 File Size: 110273258 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest50d LR: 0.1 Epochs: 270 Layers: 50 Dropout: 0.2 Crop Pct: '0.875' Momentum: 0.9 Batch Size: 8192 Image Size: '224' Weight Decay: 0.0001 Interpolation: bilinear Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L170 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-resnest/resnest50-528c19ca.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 80.96% Top 5 Accuracy: 95.38% - Name: resnest50d_1s4x24d In Collection: ResNeSt Metadata: FLOPs: 5686764544 Parameters: 25680000 File Size: 103045531 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest50d_1s4x24d LR: 0.1 Epochs: 270 Layers: 50 Dropout: 0.2 Crop Pct: '0.875' Momentum: 0.9 Batch Size: 8192 Image Size: '224' Weight Decay: 0.0001 Interpolation: bicubic Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L229 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-resnest/resnest50_fast_1s4x24d-d4a4f76f.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 81.0% Top 5 Accuracy: 95.33% - Name: resnest50d_4s2x40d In Collection: ResNeSt Metadata: FLOPs: 5657064720 Parameters: 30420000 File Size: 122133282 Architecture: - 1x1 Convolution - Convolution - Dense Connections - Global Average Pooling - Max Pooling - ReLU - Residual Connection - Softmax - Split Attention Tasks: - Image Classification Training Techniques: - AutoAugment - DropBlock - Label Smoothing - Mixup - SGD with Momentum - Weight Decay Training Data: - ImageNet Training Resources: 64x NVIDIA V100 GPUs ID: resnest50d_4s2x40d LR: 0.1 Epochs: 270 Layers: 50 Dropout: 0.2 Crop Pct: '0.875' Momentum: 0.9 Batch Size: 8192 Image Size: '224' Weight Decay: 0.0001 Interpolation: bicubic Code: https://github.com/rwightman/pytorch-image-models/blob/d8e69206be253892b2956341fea09fdebfaae4e3/timm/models/resnest.py#L218 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-resnest/resnest50_fast_4s2x40d-41d14ed0.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 81.11% Top 5 Accuracy: 95.55% -->
pytorch-image-models/hfdocs/source/models/resnest.mdx/0
{ "file_path": "pytorch-image-models/hfdocs/source/models/resnest.mdx", "repo_id": "pytorch-image-models", "token_count": 5466 }
200
# (Tensorflow) EfficientNet **EfficientNet** is a convolutional neural network architecture and scaling method that uniformly scales all dimensions of depth/width/resolution using a *compound coefficient*. Unlike conventional practice that arbitrary scales these factors, the EfficientNet scaling method uniformly scales network width, depth, and resolution with a set of fixed scaling coefficients. For example, if we want to use \\( 2^N \\) times more computational resources, then we can simply increase the network depth by \\( \alpha ^ N \\), width by \\( \beta ^ N \\), and image size by \\( \gamma ^ N \\), where \\( \alpha, \beta, \gamma \\) are constant coefficients determined by a small grid search on the original small model. EfficientNet uses a compound coefficient \\( \phi \\) to uniformly scales network width, depth, and resolution in a principled way. The compound scaling method is justified by the intuition that if the input image is bigger, then the network needs more layers to increase the receptive field and more channels to capture more fine-grained patterns on the bigger image. The base EfficientNet-B0 network is based on the inverted bottleneck residual blocks of [MobileNetV2](https://paperswithcode.com/method/mobilenetv2), in addition to [squeeze-and-excitation blocks](https://paperswithcode.com/method/squeeze-and-excitation-block). The weights from this model were ported from [Tensorflow/TPU](https://github.com/tensorflow/tpu). ## How do I use this model on an image? To load a pretrained model: ```py >>> import timm >>> model = timm.create_model('tf_efficientnet_b0', pretrained=True) >>> model.eval() ``` To load and preprocess the image: ```py >>> import urllib >>> from PIL import Image >>> from timm.data import resolve_data_config >>> from timm.data.transforms_factory import create_transform >>> config = resolve_data_config({}, model=model) >>> transform = create_transform(**config) >>> url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") >>> urllib.request.urlretrieve(url, filename) >>> img = Image.open(filename).convert('RGB') >>> tensor = transform(img).unsqueeze(0) # transform and add batch dimension ``` To get the model predictions: ```py >>> import torch >>> with torch.no_grad(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) >>> # prints: torch.Size([1000]) ``` To get the top-5 predictions class names: ```py >>> # Get imagenet class mappings >>> url, filename = ("https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt", "imagenet_classes.txt") >>> urllib.request.urlretrieve(url, filename) >>> with open("imagenet_classes.txt", "r") as f: ... categories = [s.strip() for s in f.readlines()] >>> # Print top categories per image >>> top5_prob, top5_catid = torch.topk(probabilities, 5) >>> for i in range(top5_prob.size(0)): ... print(categories[top5_catid[i]], top5_prob[i].item()) >>> # prints class names and probabilities like: >>> # [('Samoyed', 0.6425196528434753), ('Pomeranian', 0.04062102362513542), ('keeshond', 0.03186424449086189), ('white wolf', 0.01739676296710968), ('Eskimo dog', 0.011717947199940681)] ``` Replace the model name with the variant you want to use, e.g. `tf_efficientnet_b0`. You can find the IDs in the model summaries at the top of this page. To extract image features with this model, follow the [timm feature extraction examples](../feature_extraction), just change the name of the model you want to use. ## How do I finetune this model? You can finetune any of the pre-trained models just by changing the classifier (the last layer). ```py >>> model = timm.create_model('tf_efficientnet_b0', pretrained=True, num_classes=NUM_FINETUNE_CLASSES) ``` To finetune on your own dataset, you have to write a training loop or adapt [timm's training script](https://github.com/rwightman/pytorch-image-models/blob/master/train.py) to use your dataset. ## How do I train this model? You can follow the [timm recipe scripts](../scripts) for training a new model afresh. ## Citation ```BibTeX @misc{tan2020efficientnet, title={EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks}, author={Mingxing Tan and Quoc V. Le}, year={2020}, eprint={1905.11946}, archivePrefix={arXiv}, primaryClass={cs.LG} } ``` <!-- Type: model-index Collections: - Name: TF EfficientNet Paper: Title: 'EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks' URL: https://paperswithcode.com/paper/efficientnet-rethinking-model-scaling-for Models: - Name: tf_efficientnet_b0 In Collection: TF EfficientNet Metadata: FLOPs: 488688572 Parameters: 5290000 File Size: 21383997 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet Training Resources: TPUv3 Cloud TPU ID: tf_efficientnet_b0 LR: 0.256 Epochs: 350 Crop Pct: '0.875' Momentum: 0.9 Batch Size: 2048 Image Size: '224' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1241 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b0_aa-827b6e33.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 76.85% Top 5 Accuracy: 93.23% - Name: tf_efficientnet_b1 In Collection: TF EfficientNet Metadata: FLOPs: 883633200 Parameters: 7790000 File Size: 31512534 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet ID: tf_efficientnet_b1 LR: 0.256 Epochs: 350 Crop Pct: '0.882' Momentum: 0.9 Batch Size: 2048 Image Size: '240' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1251 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b1_aa-ea7a6ee0.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 78.84% Top 5 Accuracy: 94.2% - Name: tf_efficientnet_b2 In Collection: TF EfficientNet Metadata: FLOPs: 1234321170 Parameters: 9110000 File Size: 36797929 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet ID: tf_efficientnet_b2 LR: 0.256 Epochs: 350 Crop Pct: '0.89' Momentum: 0.9 Batch Size: 2048 Image Size: '260' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1261 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b2_aa-60c94f97.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 80.07% Top 5 Accuracy: 94.9% - Name: tf_efficientnet_b3 In Collection: TF EfficientNet Metadata: FLOPs: 2275247568 Parameters: 12230000 File Size: 49381362 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet ID: tf_efficientnet_b3 LR: 0.256 Epochs: 350 Crop Pct: '0.904' Momentum: 0.9 Batch Size: 2048 Image Size: '300' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1271 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b3_aa-84b4657e.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 81.65% Top 5 Accuracy: 95.72% - Name: tf_efficientnet_b4 In Collection: TF EfficientNet Metadata: FLOPs: 5749638672 Parameters: 19340000 File Size: 77989689 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet Training Resources: TPUv3 Cloud TPU ID: tf_efficientnet_b4 LR: 0.256 Epochs: 350 Crop Pct: '0.922' Momentum: 0.9 Batch Size: 2048 Image Size: '380' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1281 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b4_aa-818f208c.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 83.03% Top 5 Accuracy: 96.3% - Name: tf_efficientnet_b5 In Collection: TF EfficientNet Metadata: FLOPs: 13176501888 Parameters: 30390000 File Size: 122403150 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet ID: tf_efficientnet_b5 LR: 0.256 Epochs: 350 Crop Pct: '0.934' Momentum: 0.9 Batch Size: 2048 Image Size: '456' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1291 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b5_ra-9a3e5369.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 83.81% Top 5 Accuracy: 96.75% - Name: tf_efficientnet_b6 In Collection: TF EfficientNet Metadata: FLOPs: 24180518488 Parameters: 43040000 File Size: 173232007 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet ID: tf_efficientnet_b6 LR: 0.256 Epochs: 350 Crop Pct: '0.942' Momentum: 0.9 Batch Size: 2048 Image Size: '528' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1301 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b6_aa-80ba17e4.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 84.11% Top 5 Accuracy: 96.89% - Name: tf_efficientnet_b7 In Collection: TF EfficientNet Metadata: FLOPs: 48205304880 Parameters: 66349999 File Size: 266850607 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet ID: tf_efficientnet_b7 LR: 0.256 Epochs: 350 Crop Pct: '0.949' Momentum: 0.9 Batch Size: 2048 Image Size: '600' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1312 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b7_ra-6c08e654.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 84.93% Top 5 Accuracy: 97.2% - Name: tf_efficientnet_b8 In Collection: TF EfficientNet Metadata: FLOPs: 80962956270 Parameters: 87410000 File Size: 351379853 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - Label Smoothing - RMSProp - Stochastic Depth - Weight Decay Training Data: - ImageNet ID: tf_efficientnet_b8 LR: 0.256 Epochs: 350 Crop Pct: '0.954' Momentum: 0.9 Batch Size: 2048 Image Size: '672' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1323 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b8_ra-572d5dd9.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 85.35% Top 5 Accuracy: 97.39% - Name: tf_efficientnet_el In Collection: TF EfficientNet Metadata: FLOPs: 9356616096 Parameters: 10590000 File Size: 42800271 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Data: - ImageNet ID: tf_efficientnet_el Crop Pct: '0.904' Image Size: '300' Interpolation: bicubic Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1551 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_el-5143854e.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 80.45% Top 5 Accuracy: 95.17% - Name: tf_efficientnet_em In Collection: TF EfficientNet Metadata: FLOPs: 3636607040 Parameters: 6900000 File Size: 27933644 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Data: - ImageNet ID: tf_efficientnet_em Crop Pct: '0.882' Image Size: '240' Interpolation: bicubic Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1541 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_em-e78cfe58.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 78.71% Top 5 Accuracy: 94.33% - Name: tf_efficientnet_es In Collection: TF EfficientNet Metadata: FLOPs: 2057577472 Parameters: 5440000 File Size: 22008479 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Data: - ImageNet ID: tf_efficientnet_es Crop Pct: '0.875' Image Size: '224' Interpolation: bicubic Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1531 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_es-ca1afbfe.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 77.28% Top 5 Accuracy: 93.6% - Name: tf_efficientnet_l2_ns_475 In Collection: TF EfficientNet Metadata: FLOPs: 217795669644 Parameters: 480310000 File Size: 1925950424 Architecture: - 1x1 Convolution - Average Pooling - Batch Normalization - Convolution - Dense Connections - Dropout - Inverted Residual Block - Squeeze-and-Excitation Block - Swish Tasks: - Image Classification Training Techniques: - AutoAugment - FixRes - Label Smoothing - Noisy Student - RMSProp - RandAugment - Weight Decay Training Data: - ImageNet - JFT-300M Training Resources: TPUv3 Cloud TPU ID: tf_efficientnet_l2_ns_475 LR: 0.128 Epochs: 350 Dropout: 0.5 Crop Pct: '0.936' Momentum: 0.9 Batch Size: 2048 Image Size: '475' Weight Decay: 1.0e-05 Interpolation: bicubic RMSProp Decay: 0.9 Label Smoothing: 0.1 BatchNorm Momentum: 0.99 Stochastic Depth Survival: 0.8 Code: https://github.com/rwightman/pytorch-image-models/blob/9a25fdf3ad0414b4d66da443fe60ae0aa14edc84/timm/models/efficientnet.py#L1509 Weights: https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_l2_ns_475-bebbd00a.pth Results: - Task: Image Classification Dataset: ImageNet Metrics: Top 1 Accuracy: 88.24% Top 5 Accuracy: 98.55% -->
pytorch-image-models/hfdocs/source/models/tf-efficientnet.mdx/0
{ "file_path": "pytorch-image-models/hfdocs/source/models/tf-efficientnet.mdx", "repo_id": "pytorch-image-models", "token_count": 8002 }
201
""" ONNX export script Export PyTorch models as ONNX graphs. This export script originally started as an adaptation of code snippets found at https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html The default parameters work with PyTorch 1.6 and ONNX 1.7 and produce an optimal ONNX graph for hosting in the ONNX runtime (see onnx_validate.py). To export an ONNX model compatible with caffe2 (see caffe2_benchmark.py and caffe2_validate.py), the --keep-init and --aten-fallback flags are currently required. Older versions of PyTorch/ONNX (tested PyTorch 1.4, ONNX 1.5) do not need extra flags for caffe2 compatibility, but they produce a model that isn't as fast running on ONNX runtime. Most new release of PyTorch and ONNX cause some sort of breakage in the export / usage of ONNX models. Please do your research and search ONNX and PyTorch issue tracker before asking me. Thanks. Copyright 2020 Ross Wightman """ import argparse import timm from timm.utils.model import reparameterize_model from timm.utils.onnx import onnx_export parser = argparse.ArgumentParser(description='PyTorch ImageNet Validation') parser.add_argument('output', metavar='ONNX_FILE', help='output model filename') parser.add_argument('--model', '-m', metavar='MODEL', default='mobilenetv3_large_100', help='model architecture (default: mobilenetv3_large_100)') parser.add_argument('--opset', type=int, default=None, help='ONNX opset to use (default: 10)') parser.add_argument('--keep-init', action='store_true', default=False, help='Keep initializers as input. Needed for Caffe2 compatible export in newer PyTorch/ONNX.') parser.add_argument('--aten-fallback', action='store_true', default=False, help='Fallback to ATEN ops. Helps fix AdaptiveAvgPool issue with Caffe2 in newer PyTorch/ONNX.') parser.add_argument('--dynamic-size', action='store_true', default=False, help='Export model width dynamic width/height. Not recommended for "tf" models with SAME padding.') parser.add_argument('--check-forward', action='store_true', default=False, help='Do a full check of torch vs onnx forward after export.') parser.add_argument('-b', '--batch-size', default=1, type=int, metavar='N', help='mini-batch size (default: 1)') parser.add_argument('--img-size', default=None, type=int, metavar='N', help='Input image dimension, uses model default if empty') parser.add_argument('--mean', type=float, nargs='+', default=None, metavar='MEAN', help='Override mean pixel value of dataset') parser.add_argument('--std', type=float, nargs='+', default=None, metavar='STD', help='Override std deviation of of dataset') parser.add_argument('--num-classes', type=int, default=1000, help='Number classes in dataset') parser.add_argument('--checkpoint', default='', type=str, metavar='PATH', help='path to checkpoint (default: none)') parser.add_argument('--reparam', default=False, action='store_true', help='Reparameterize model') parser.add_argument('--training', default=False, action='store_true', help='Export in training mode (default is eval)') parser.add_argument('--verbose', default=False, action='store_true', help='Extra stdout output') parser.add_argument('--dynamo', default=False, action='store_true', help='Use torch dynamo export.') def main(): args = parser.parse_args() args.pretrained = True if args.checkpoint: args.pretrained = False print("==> Creating PyTorch {} model".format(args.model)) # NOTE exportable=True flag disables autofn/jit scripted activations and uses Conv2dSameExport layers # for models using SAME padding model = timm.create_model( args.model, num_classes=args.num_classes, in_chans=3, pretrained=args.pretrained, checkpoint_path=args.checkpoint, exportable=True, ) if args.reparam: model = reparameterize_model(model) onnx_export( model, args.output, opset=args.opset, dynamic_size=args.dynamic_size, aten_fallback=args.aten_fallback, keep_initializers=args.keep_init, check_forward=args.check_forward, training=args.training, verbose=args.verbose, use_dynamo=args.dynamo, input_size=(3, args.img_size, args.img_size), batch_size=args.batch_size, ) if __name__ == '__main__': main()
pytorch-image-models/onnx_export.py/0
{ "file_path": "pytorch-image-models/onnx_export.py", "repo_id": "pytorch-image-models", "token_count": 1811 }
202
import torch import torch.nn as nn from timm.layers import create_act_layer, set_layer_config import importlib import os torch_backend = os.environ.get('TORCH_BACKEND') if torch_backend is not None: importlib.import_module(torch_backend) torch_device = os.environ.get('TORCH_DEVICE', 'cpu') class MLP(nn.Module): def __init__(self, act_layer="relu", inplace=True): super(MLP, self).__init__() self.fc1 = nn.Linear(1000, 100) self.act = create_act_layer(act_layer, inplace=inplace) self.fc2 = nn.Linear(100, 10) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.fc2(x) return x def _run_act_layer_grad(act_type, inplace=True): x = torch.rand(10, 1000) * 10 m = MLP(act_layer=act_type, inplace=inplace) def _run(x, act_layer=''): if act_layer: # replace act layer if set m.act = create_act_layer(act_layer, inplace=inplace) out = m(x) l = (out - 0).pow(2).sum() return l x = x.to(device=torch_device) m.to(device=torch_device) out_me = _run(x) with set_layer_config(scriptable=True): out_jit = _run(x, act_type) assert torch.isclose(out_jit, out_me) with set_layer_config(no_jit=True): out_basic = _run(x, act_type) assert torch.isclose(out_basic, out_jit) def test_swish_grad(): for _ in range(100): _run_act_layer_grad('swish') def test_mish_grad(): for _ in range(100): _run_act_layer_grad('mish') def test_hard_sigmoid_grad(): for _ in range(100): _run_act_layer_grad('hard_sigmoid', inplace=None) def test_hard_swish_grad(): for _ in range(100): _run_act_layer_grad('hard_swish') def test_hard_mish_grad(): for _ in range(100): _run_act_layer_grad('hard_mish')
pytorch-image-models/tests/test_layers.py/0
{ "file_path": "pytorch-image-models/tests/test_layers.py", "repo_id": "pytorch-image-models", "token_count": 871 }
203
import csv import os import pkgutil import re from typing import Dict, List, Optional, Union from .dataset_info import DatasetInfo # NOTE no ambiguity wrt to mapping from # classes to ImageNet subset so far, but likely to change _NUM_CLASSES_TO_SUBSET = { 1000: 'imagenet-1k', 11221: 'imagenet-21k-miil', # miil subset of fall11 11821: 'imagenet-12k', # timm specific 12k subset of fall11 21841: 'imagenet-22k', # as in fall11.tar 21842: 'imagenet-22k-ms', # a Microsoft (for FocalNet) remapping of 22k w/ moves ImageNet-1k classes to first 1000 21843: 'imagenet-21k-goog', # Google's ImageNet full has two classes not in fall11 } _SUBSETS = { 'imagenet1k': 'imagenet_synsets.txt', 'imagenet12k': 'imagenet12k_synsets.txt', 'imagenet22k': 'imagenet22k_synsets.txt', 'imagenet21k': 'imagenet21k_goog_synsets.txt', 'imagenet21kgoog': 'imagenet21k_goog_synsets.txt', 'imagenet21kmiil': 'imagenet21k_miil_synsets.txt', 'imagenet22kms': 'imagenet22k_ms_synsets.txt', } _LEMMA_FILE = 'imagenet_synset_to_lemma.txt' _DEFINITION_FILE = 'imagenet_synset_to_definition.txt' def infer_imagenet_subset(model_or_cfg) -> Optional[str]: if isinstance(model_or_cfg, dict): num_classes = model_or_cfg.get('num_classes', None) else: num_classes = getattr(model_or_cfg, 'num_classes', None) if not num_classes: pretrained_cfg = getattr(model_or_cfg, 'pretrained_cfg', {}) # FIXME at some point pretrained_cfg should include dataset-tag, # which will be more robust than a guess based on num_classes num_classes = pretrained_cfg.get('num_classes', None) if not num_classes or num_classes not in _NUM_CLASSES_TO_SUBSET: return None return _NUM_CLASSES_TO_SUBSET[num_classes] class ImageNetInfo(DatasetInfo): def __init__(self, subset: str = 'imagenet-1k'): super().__init__() subset = re.sub(r'[-_\s]', '', subset.lower()) assert subset in _SUBSETS, f'Unknown imagenet subset {subset}.' # WordNet synsets (part-of-speach + offset) are the unique class label names for ImageNet classifiers synset_file = _SUBSETS[subset] synset_data = pkgutil.get_data(__name__, os.path.join('_info', synset_file)) self._synsets = synset_data.decode('utf-8').splitlines() # WordNet lemmas (canonical dictionary form of word) and definitions are used to build # the class descriptions. If detailed=True both are used, otherwise just the lemmas. lemma_data = pkgutil.get_data(__name__, os.path.join('_info', _LEMMA_FILE)) reader = csv.reader(lemma_data.decode('utf-8').splitlines(), delimiter='\t') self._lemmas = dict(reader) definition_data = pkgutil.get_data(__name__, os.path.join('_info', _DEFINITION_FILE)) reader = csv.reader(definition_data.decode('utf-8').splitlines(), delimiter='\t') self._definitions = dict(reader) def num_classes(self): return len(self._synsets) def label_names(self): return self._synsets def label_descriptions(self, detailed: bool = False, as_dict: bool = False) -> Union[List[str], Dict[str, str]]: if as_dict: return {label: self.label_name_to_description(label, detailed=detailed) for label in self._synsets} else: return [self.label_name_to_description(label, detailed=detailed) for label in self._synsets] def index_to_label_name(self, index) -> str: assert 0 <= index < len(self._synsets), \ f'Index ({index}) out of range for dataset with {len(self._synsets)} classes.' return self._synsets[index] def index_to_description(self, index: int, detailed: bool = False) -> str: label = self.index_to_label_name(index) return self.label_name_to_description(label, detailed=detailed) def label_name_to_description(self, label: str, detailed: bool = False) -> str: if detailed: description = f'{self._lemmas[label]}: {self._definitions[label]}' else: description = f'{self._lemmas[label]}' return description
pytorch-image-models/timm/data/imagenet_info.py/0
{ "file_path": "pytorch-image-models/timm/data/imagenet_info.py", "repo_id": "pytorch-image-models", "token_count": 1733 }
204
from multiprocessing import Value class SharedCount: def __init__(self, epoch: int = 0): self.shared_epoch = Value('i', epoch) @property def value(self): return self.shared_epoch.value @value.setter def value(self, epoch): self.shared_epoch.value = epoch
pytorch-image-models/timm/data/readers/shared_count.py/0
{ "file_path": "pytorch-image-models/timm/data/readers/shared_count.py", "repo_id": "pytorch-image-models", "token_count": 122 }
205
""" PyTorch Conditionally Parameterized Convolution (CondConv) Paper: CondConv: Conditionally Parameterized Convolutions for Efficient Inference (https://arxiv.org/abs/1904.04971) Hacked together by / Copyright 2020 Ross Wightman """ import math from functools import partial import numpy as np import torch from torch import nn as nn from torch.nn import functional as F from .helpers import to_2tuple from .conv2d_same import conv2d_same from .padding import get_padding_value def get_condconv_initializer(initializer, num_experts, expert_shape): def condconv_initializer(weight): """CondConv initializer function.""" num_params = np.prod(expert_shape) if (len(weight.shape) != 2 or weight.shape[0] != num_experts or weight.shape[1] != num_params): raise (ValueError( 'CondConv variables must have shape [num_experts, num_params]')) for i in range(num_experts): initializer(weight[i].view(expert_shape)) return condconv_initializer class CondConv2d(nn.Module): """ Conditionally Parameterized Convolution Inspired by: https://github.com/tensorflow/tpu/blob/master/models/official/efficientnet/condconv/condconv_layers.py Grouped convolution hackery for parallel execution of the per-sample kernel filters inspired by this discussion: https://github.com/pytorch/pytorch/issues/17983 """ __constants__ = ['in_channels', 'out_channels', 'dynamic_padding'] def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding='', dilation=1, groups=1, bias=False, num_experts=4): super(CondConv2d, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.kernel_size = to_2tuple(kernel_size) self.stride = to_2tuple(stride) padding_val, is_padding_dynamic = get_padding_value( padding, kernel_size, stride=stride, dilation=dilation) self.dynamic_padding = is_padding_dynamic # if in forward to work with torchscript self.padding = to_2tuple(padding_val) self.dilation = to_2tuple(dilation) self.groups = groups self.num_experts = num_experts self.weight_shape = (self.out_channels, self.in_channels // self.groups) + self.kernel_size weight_num_param = 1 for wd in self.weight_shape: weight_num_param *= wd self.weight = torch.nn.Parameter(torch.Tensor(self.num_experts, weight_num_param)) if bias: self.bias_shape = (self.out_channels,) self.bias = torch.nn.Parameter(torch.Tensor(self.num_experts, self.out_channels)) else: self.register_parameter('bias', None) self.reset_parameters() def reset_parameters(self): init_weight = get_condconv_initializer( partial(nn.init.kaiming_uniform_, a=math.sqrt(5)), self.num_experts, self.weight_shape) init_weight(self.weight) if self.bias is not None: fan_in = np.prod(self.weight_shape[1:]) bound = 1 / math.sqrt(fan_in) init_bias = get_condconv_initializer( partial(nn.init.uniform_, a=-bound, b=bound), self.num_experts, self.bias_shape) init_bias(self.bias) def forward(self, x, routing_weights): B, C, H, W = x.shape weight = torch.matmul(routing_weights, self.weight) new_weight_shape = (B * self.out_channels, self.in_channels // self.groups) + self.kernel_size weight = weight.view(new_weight_shape) bias = None if self.bias is not None: bias = torch.matmul(routing_weights, self.bias) bias = bias.view(B * self.out_channels) # move batch elements with channels so each batch element can be efficiently convolved with separate kernel # reshape instead of view to work with channels_last input x = x.reshape(1, B * C, H, W) if self.dynamic_padding: out = conv2d_same( x, weight, bias, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups * B) else: out = F.conv2d( x, weight, bias, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups * B) out = out.permute([1, 0, 2, 3]).view(B, self.out_channels, out.shape[-2], out.shape[-1]) # Literal port (from TF definition) # x = torch.split(x, 1, 0) # weight = torch.split(weight, 1, 0) # if self.bias is not None: # bias = torch.matmul(routing_weights, self.bias) # bias = torch.split(bias, 1, 0) # else: # bias = [None] * B # out = [] # for xi, wi, bi in zip(x, weight, bias): # wi = wi.view(*self.weight_shape) # if bi is not None: # bi = bi.view(*self.bias_shape) # out.append(self.conv_fn( # xi, wi, bi, stride=self.stride, padding=self.padding, # dilation=self.dilation, groups=self.groups)) # out = torch.cat(out, 0) return out
pytorch-image-models/timm/layers/cond_conv2d.py/0
{ "file_path": "pytorch-image-models/timm/layers/cond_conv2d.py", "repo_id": "pytorch-image-models", "token_count": 2314 }
206
""" Global Context Attention Block Paper: `GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond` - https://arxiv.org/abs/1904.11492 Official code consulted as reference: https://github.com/xvjiarui/GCNet Hacked together by / Copyright 2021 Ross Wightman """ from torch import nn as nn import torch.nn.functional as F from .create_act import create_act_layer, get_act_layer from .helpers import make_divisible from .mlp import ConvMlp from .norm import LayerNorm2d class GlobalContext(nn.Module): def __init__(self, channels, use_attn=True, fuse_add=False, fuse_scale=True, init_last_zero=False, rd_ratio=1./8, rd_channels=None, rd_divisor=1, act_layer=nn.ReLU, gate_layer='sigmoid'): super(GlobalContext, self).__init__() act_layer = get_act_layer(act_layer) self.conv_attn = nn.Conv2d(channels, 1, kernel_size=1, bias=True) if use_attn else None if rd_channels is None: rd_channels = make_divisible(channels * rd_ratio, rd_divisor, round_limit=0.) if fuse_add: self.mlp_add = ConvMlp(channels, rd_channels, act_layer=act_layer, norm_layer=LayerNorm2d) else: self.mlp_add = None if fuse_scale: self.mlp_scale = ConvMlp(channels, rd_channels, act_layer=act_layer, norm_layer=LayerNorm2d) else: self.mlp_scale = None self.gate = create_act_layer(gate_layer) self.init_last_zero = init_last_zero self.reset_parameters() def reset_parameters(self): if self.conv_attn is not None: nn.init.kaiming_normal_(self.conv_attn.weight, mode='fan_in', nonlinearity='relu') if self.mlp_add is not None: nn.init.zeros_(self.mlp_add.fc2.weight) def forward(self, x): B, C, H, W = x.shape if self.conv_attn is not None: attn = self.conv_attn(x).reshape(B, 1, H * W) # (B, 1, H * W) attn = F.softmax(attn, dim=-1).unsqueeze(3) # (B, 1, H * W, 1) context = x.reshape(B, C, H * W).unsqueeze(1) @ attn context = context.view(B, C, 1, 1) else: context = x.mean(dim=(2, 3), keepdim=True) if self.mlp_scale is not None: mlp_x = self.mlp_scale(context) x = x * self.gate(mlp_x) if self.mlp_add is not None: mlp_x = self.mlp_add(context) x = x + mlp_x return x
pytorch-image-models/timm/layers/global_context.py/0
{ "file_path": "pytorch-image-models/timm/layers/global_context.py", "repo_id": "pytorch-image-models", "token_count": 1169 }
207
""" Normalization layers and wrappers Norm layer definitions that support fast norm and consistent channel arg order (always first arg). Hacked together by / Copyright 2022 Ross Wightman """ import numbers from typing import Tuple import torch import torch.nn as nn import torch.nn.functional as F from .fast_norm import is_fast_norm, fast_group_norm, fast_layer_norm, fast_rms_norm class GroupNorm(nn.GroupNorm): def __init__(self, num_channels, num_groups=32, eps=1e-5, affine=True): # NOTE num_channels is swapped to first arg for consistency in swapping norm layers with BN super().__init__(num_groups, num_channels, eps=eps, affine=affine) self.fast_norm = is_fast_norm() # can't script unless we have these flags here (no globals) def forward(self, x): if self.fast_norm: return fast_group_norm(x, self.num_groups, self.weight, self.bias, self.eps) else: return F.group_norm(x, self.num_groups, self.weight, self.bias, self.eps) class GroupNorm1(nn.GroupNorm): """ Group Normalization with 1 group. Input: tensor in shape [B, C, *] """ def __init__(self, num_channels, **kwargs): super().__init__(1, num_channels, **kwargs) self.fast_norm = is_fast_norm() # can't script unless we have these flags here (no globals) def forward(self, x: torch.Tensor) -> torch.Tensor: if self.fast_norm: return fast_group_norm(x, self.num_groups, self.weight, self.bias, self.eps) else: return F.group_norm(x, self.num_groups, self.weight, self.bias, self.eps) class LayerNorm(nn.LayerNorm): """ LayerNorm w/ fast norm option """ def __init__(self, num_channels, eps=1e-6, affine=True): super().__init__(num_channels, eps=eps, elementwise_affine=affine) self._fast_norm = is_fast_norm() # can't script unless we have these flags here (no globals) def forward(self, x: torch.Tensor) -> torch.Tensor: if self._fast_norm: x = fast_layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) else: x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) return x class LayerNorm2d(nn.LayerNorm): """ LayerNorm for channels of '2D' spatial NCHW tensors """ def __init__(self, num_channels, eps=1e-6, affine=True): super().__init__(num_channels, eps=eps, elementwise_affine=affine) self._fast_norm = is_fast_norm() # can't script unless we have these flags here (no globals) def forward(self, x: torch.Tensor) -> torch.Tensor: x = x.permute(0, 2, 3, 1) if self._fast_norm: x = fast_layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) else: x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) x = x.permute(0, 3, 1, 2) return x def _is_contiguous(tensor: torch.Tensor) -> bool: # jit is oh so lovely :/ if torch.jit.is_scripting(): return tensor.is_contiguous() else: return tensor.is_contiguous(memory_format=torch.contiguous_format) def _layer_norm_cf(x: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor, eps: float): s, u = torch.var_mean(x, dim=1, unbiased=False, keepdim=True) x = (x - u) * torch.rsqrt(s + eps) x = x * weight[:, None, None] + bias[:, None, None] return x def _layer_norm_cf_sqm(x: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor, eps: float): u = x.mean(dim=1, keepdim=True) s = ((x * x).mean(dim=1, keepdim=True) - (u * u)).clamp(0) x = (x - u) * torch.rsqrt(s + eps) x = x * weight.view(1, -1, 1, 1) + bias.view(1, -1, 1, 1) return x class LayerNormExp2d(nn.LayerNorm): """ LayerNorm for channels_first tensors with 2d spatial dimensions (ie N, C, H, W). Experimental implementation w/ manual norm for tensors non-contiguous tensors. This improves throughput in some scenarios (tested on Ampere GPU), esp w/ channels_last layout. However, benefits are not always clear and can perform worse on other GPUs. """ def __init__(self, num_channels, eps=1e-6): super().__init__(num_channels, eps=eps) def forward(self, x) -> torch.Tensor: if _is_contiguous(x): x = F.layer_norm( x.permute(0, 2, 3, 1), self.normalized_shape, self.weight, self.bias, self.eps).permute(0, 3, 1, 2) else: x = _layer_norm_cf(x, self.weight, self.bias, self.eps) return x class RmsNorm(nn.Module): """ RmsNorm w/ fast (apex) norm if available """ __constants__ = ['normalized_shape', 'eps', 'elementwise_affine'] normalized_shape: Tuple[int, ...] eps: float elementwise_affine: bool def __init__(self, channels, eps=1e-6, affine=True, device=None, dtype=None) -> None: factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() normalized_shape = channels if isinstance(normalized_shape, numbers.Integral): # mypy error: incompatible types in assignment normalized_shape = (normalized_shape,) # type: ignore[assignment] self.normalized_shape = tuple(normalized_shape) # type: ignore[arg-type] self.eps = eps self.elementwise_affine = affine if self.elementwise_affine: self.weight = nn.Parameter(torch.empty(self.normalized_shape, **factory_kwargs)) else: self.register_parameter('weight', None) self.reset_parameters() def reset_parameters(self) -> None: if self.elementwise_affine: nn.init.ones_(self.weight) def forward(self, x: torch.Tensor) -> torch.Tensor: # NOTE fast norm fallback needs our rms norm impl, so both paths through here. # Since there is no built-in PyTorch impl, always use APEX RmsNorm if is installed. x = fast_rms_norm(x, self.normalized_shape, self.weight, self.eps) return x
pytorch-image-models/timm/layers/norm.py/0
{ "file_path": "pytorch-image-models/timm/layers/norm.py", "repo_id": "pytorch-image-models", "token_count": 2512 }
208
""" Test Time Pooling (Average-Max Pool) Hacked together by / Copyright 2020 Ross Wightman """ import logging from torch import nn import torch.nn.functional as F from .adaptive_avgmax_pool import adaptive_avgmax_pool2d _logger = logging.getLogger(__name__) class TestTimePoolHead(nn.Module): def __init__(self, base, original_pool=7): super(TestTimePoolHead, self).__init__() self.base = base self.original_pool = original_pool base_fc = self.base.get_classifier() if isinstance(base_fc, nn.Conv2d): self.fc = base_fc else: self.fc = nn.Conv2d( self.base.num_features, self.base.num_classes, kernel_size=1, bias=True) self.fc.weight.data.copy_(base_fc.weight.data.view(self.fc.weight.size())) self.fc.bias.data.copy_(base_fc.bias.data.view(self.fc.bias.size())) self.base.reset_classifier(0) # delete original fc layer def forward(self, x): x = self.base.forward_features(x) x = F.avg_pool2d(x, kernel_size=self.original_pool, stride=1) x = self.fc(x) x = adaptive_avgmax_pool2d(x, 1) return x.view(x.size(0), -1) def apply_test_time_pool(model, config, use_test_size=False): test_time_pool = False if not hasattr(model, 'default_cfg') or not model.default_cfg: return model, False if use_test_size and 'test_input_size' in model.default_cfg: df_input_size = model.default_cfg['test_input_size'] else: df_input_size = model.default_cfg['input_size'] if config['input_size'][-1] > df_input_size[-1] and config['input_size'][-2] > df_input_size[-2]: _logger.info('Target input size %s > pretrained default %s, using test time pooling' % (str(config['input_size'][-2:]), str(df_input_size[-2:]))) model = TestTimePoolHead(model, original_pool=model.default_cfg['pool_size']) test_time_pool = True return model, test_time_pool
pytorch-image-models/timm/layers/test_time_pool.py/0
{ "file_path": "pytorch-image-models/timm/layers/test_time_pool.py", "repo_id": "pytorch-image-models", "token_count": 881 }
209
""" Model creation / weight loading / state_dict helpers Hacked together by / Copyright 2020 Ross Wightman """ import logging import os from collections import OrderedDict from typing import Any, Callable, Dict, Optional, Union import torch try: import safetensors.torch _has_safetensors = True except ImportError: _has_safetensors = False _logger = logging.getLogger(__name__) __all__ = ['clean_state_dict', 'load_state_dict', 'load_checkpoint', 'remap_state_dict', 'resume_checkpoint'] def _remove_prefix(text, prefix): # FIXME replace with 3.9 stdlib fn when min at 3.9 if text.startswith(prefix): return text[len(prefix):] return text def clean_state_dict(state_dict: Dict[str, Any]) -> Dict[str, Any]: # 'clean' checkpoint by removing .module prefix from state dict if it exists from parallel training cleaned_state_dict = {} to_remove = ( 'module.', # DDP wrapper '_orig_mod.', # torchcompile dynamo wrapper ) for k, v in state_dict.items(): for r in to_remove: k = _remove_prefix(k, r) cleaned_state_dict[k] = v return cleaned_state_dict def load_state_dict( checkpoint_path: str, use_ema: bool = True, device: Union[str, torch.device] = 'cpu', weights_only: bool = False, ) -> Dict[str, Any]: if checkpoint_path and os.path.isfile(checkpoint_path): # Check if safetensors or not and load weights accordingly if str(checkpoint_path).endswith(".safetensors"): assert _has_safetensors, "`pip install safetensors` to use .safetensors" checkpoint = safetensors.torch.load_file(checkpoint_path, device=device) else: try: checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=weights_only) except TypeError: checkpoint = torch.load(checkpoint_path, map_location=device) state_dict_key = '' if isinstance(checkpoint, dict): if use_ema and checkpoint.get('state_dict_ema', None) is not None: state_dict_key = 'state_dict_ema' elif use_ema and checkpoint.get('model_ema', None) is not None: state_dict_key = 'model_ema' elif 'state_dict' in checkpoint: state_dict_key = 'state_dict' elif 'model' in checkpoint: state_dict_key = 'model' state_dict = clean_state_dict(checkpoint[state_dict_key] if state_dict_key else checkpoint) _logger.info("Loaded {} from checkpoint '{}'".format(state_dict_key, checkpoint_path)) return state_dict else: _logger.error("No checkpoint found at '{}'".format(checkpoint_path)) raise FileNotFoundError() def load_checkpoint( model: torch.nn.Module, checkpoint_path: str, use_ema: bool = True, device: Union[str, torch.device] = 'cpu', strict: bool = True, remap: bool = False, filter_fn: Optional[Callable] = None, weights_only: bool = False, ): if os.path.splitext(checkpoint_path)[-1].lower() in ('.npz', '.npy'): # numpy checkpoint, try to load via model specific load_pretrained fn if hasattr(model, 'load_pretrained'): model.load_pretrained(checkpoint_path) else: raise NotImplementedError('Model cannot load numpy checkpoint') return state_dict = load_state_dict(checkpoint_path, use_ema, device=device, weights_only=weights_only) if remap: state_dict = remap_state_dict(state_dict, model) elif filter_fn: state_dict = filter_fn(state_dict, model) incompatible_keys = model.load_state_dict(state_dict, strict=strict) return incompatible_keys def remap_state_dict( state_dict: Dict[str, Any], model: torch.nn.Module, allow_reshape: bool = True ): """ remap checkpoint by iterating over state dicts in order (ignoring original keys). This assumes models (and originating state dict) were created with params registered in same order. """ out_dict = {} for (ka, va), (kb, vb) in zip(model.state_dict().items(), state_dict.items()): assert va.numel() == vb.numel(), f'Tensor size mismatch {ka}: {va.shape} vs {kb}: {vb.shape}. Remap failed.' if va.shape != vb.shape: if allow_reshape: vb = vb.reshape(va.shape) else: assert False, f'Tensor shape mismatch {ka}: {va.shape} vs {kb}: {vb.shape}. Remap failed.' out_dict[ka] = vb return out_dict def resume_checkpoint( model: torch.nn.Module, checkpoint_path: str, optimizer: torch.optim.Optimizer = None, loss_scaler: Any = None, log_info: bool = True, ): resume_epoch = None if os.path.isfile(checkpoint_path): checkpoint = torch.load(checkpoint_path, map_location='cpu', weights_only=False) if isinstance(checkpoint, dict) and 'state_dict' in checkpoint: if log_info: _logger.info('Restoring model state from checkpoint...') state_dict = clean_state_dict(checkpoint['state_dict']) model.load_state_dict(state_dict) if optimizer is not None and 'optimizer' in checkpoint: if log_info: _logger.info('Restoring optimizer state from checkpoint...') optimizer.load_state_dict(checkpoint['optimizer']) if loss_scaler is not None and loss_scaler.state_dict_key in checkpoint: if log_info: _logger.info('Restoring AMP loss scaler state from checkpoint...') loss_scaler.load_state_dict(checkpoint[loss_scaler.state_dict_key]) if 'epoch' in checkpoint: resume_epoch = checkpoint['epoch'] if 'version' in checkpoint and checkpoint['version'] > 1: resume_epoch += 1 # start at the next epoch, old checkpoints incremented before save if log_info: _logger.info("Loaded checkpoint '{}' (epoch {})".format(checkpoint_path, checkpoint['epoch'])) else: model.load_state_dict(checkpoint) if log_info: _logger.info("Loaded checkpoint '{}'".format(checkpoint_path)) return resume_epoch else: _logger.error("No checkpoint found at '{}'".format(checkpoint_path)) raise FileNotFoundError()
pytorch-image-models/timm/models/_helpers.py/0
{ "file_path": "pytorch-image-models/timm/models/_helpers.py", "repo_id": "pytorch-image-models", "token_count": 2801 }
210
""" ConViT Model @article{d2021convit, title={ConViT: Improving Vision Transformers with Soft Convolutional Inductive Biases}, author={d'Ascoli, St{\'e}phane and Touvron, Hugo and Leavitt, Matthew and Morcos, Ari and Biroli, Giulio and Sagun, Levent}, journal={arXiv preprint arXiv:2103.10697}, year={2021} } Paper link: https://arxiv.org/abs/2103.10697 Original code: https://github.com/facebookresearch/convit, original copyright below Modifications and additions for timm hacked together by / Copyright 2021, Ross Wightman """ # Copyright (c) 2015-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the CC-by-NC license found in the # LICENSE file in the root directory of this source tree. # '''These modules are adapted from those of timm, see https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py ''' from typing import Optional import torch import torch.nn as nn from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.layers import DropPath, trunc_normal_, PatchEmbed, Mlp, LayerNorm, HybridEmbed from ._builder import build_model_with_cfg from ._features_fx import register_notrace_module from ._registry import register_model, generate_default_cfgs __all__ = ['ConVit'] @register_notrace_module # reason: FX can't symbolically trace control flow in forward method class GPSA(nn.Module): def __init__( self, dim, num_heads=8, qkv_bias=False, attn_drop=0., proj_drop=0., locality_strength=1., ): super().__init__() self.num_heads = num_heads self.dim = dim head_dim = dim // num_heads self.scale = head_dim ** -0.5 self.locality_strength = locality_strength self.qk = nn.Linear(dim, dim * 2, bias=qkv_bias) self.v = nn.Linear(dim, dim, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.pos_proj = nn.Linear(3, num_heads) self.proj_drop = nn.Dropout(proj_drop) self.gating_param = nn.Parameter(torch.ones(self.num_heads)) self.rel_indices: torch.Tensor = torch.zeros(1, 1, 1, 3) # silly torchscript hack, won't work with None def forward(self, x): B, N, C = x.shape if self.rel_indices is None or self.rel_indices.shape[1] != N: self.rel_indices = self.get_rel_indices(N) attn = self.get_attention(x) v = self.v(x).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3) x = (attn @ v).transpose(1, 2).reshape(B, N, C) x = self.proj(x) x = self.proj_drop(x) return x def get_attention(self, x): B, N, C = x.shape qk = self.qk(x).reshape(B, N, 2, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k = qk[0], qk[1] pos_score = self.rel_indices.expand(B, -1, -1, -1) pos_score = self.pos_proj(pos_score).permute(0, 3, 1, 2) patch_score = (q @ k.transpose(-2, -1)) * self.scale patch_score = patch_score.softmax(dim=-1) pos_score = pos_score.softmax(dim=-1) gating = self.gating_param.view(1, -1, 1, 1) attn = (1. - torch.sigmoid(gating)) * patch_score + torch.sigmoid(gating) * pos_score attn /= attn.sum(dim=-1).unsqueeze(-1) attn = self.attn_drop(attn) return attn def get_attention_map(self, x, return_map=False): attn_map = self.get_attention(x).mean(0) # average over batch distances = self.rel_indices.squeeze()[:, :, -1] ** .5 dist = torch.einsum('nm,hnm->h', (distances, attn_map)) / distances.size(0) if return_map: return dist, attn_map else: return dist def local_init(self): self.v.weight.data.copy_(torch.eye(self.dim)) locality_distance = 1 # max(1,1/locality_strength**.5) kernel_size = int(self.num_heads ** .5) center = (kernel_size - 1) / 2 if kernel_size % 2 == 0 else kernel_size // 2 for h1 in range(kernel_size): for h2 in range(kernel_size): position = h1 + kernel_size * h2 self.pos_proj.weight.data[position, 2] = -1 self.pos_proj.weight.data[position, 1] = 2 * (h1 - center) * locality_distance self.pos_proj.weight.data[position, 0] = 2 * (h2 - center) * locality_distance self.pos_proj.weight.data *= self.locality_strength def get_rel_indices(self, num_patches: int) -> torch.Tensor: img_size = int(num_patches ** .5) rel_indices = torch.zeros(1, num_patches, num_patches, 3) ind = torch.arange(img_size).view(1, -1) - torch.arange(img_size).view(-1, 1) indx = ind.repeat(img_size, img_size) indy = ind.repeat_interleave(img_size, dim=0).repeat_interleave(img_size, dim=1) indd = indx ** 2 + indy ** 2 rel_indices[:, :, :, 2] = indd.unsqueeze(0) rel_indices[:, :, :, 1] = indy.unsqueeze(0) rel_indices[:, :, :, 0] = indx.unsqueeze(0) device = self.qk.weight.device return rel_indices.to(device) class MHSA(nn.Module): def __init__( self, dim, num_heads=8, qkv_bias=False, attn_drop=0., proj_drop=0., ): super().__init__() self.num_heads = num_heads head_dim = dim // num_heads self.scale = head_dim ** -0.5 self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) def get_attention_map(self, x, return_map=False): B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] attn_map = (q @ k.transpose(-2, -1)) * self.scale attn_map = attn_map.softmax(dim=-1).mean(0) img_size = int(N ** .5) ind = torch.arange(img_size).view(1, -1) - torch.arange(img_size).view(-1, 1) indx = ind.repeat(img_size, img_size) indy = ind.repeat_interleave(img_size, dim=0).repeat_interleave(img_size, dim=1) indd = indx ** 2 + indy ** 2 distances = indd ** .5 distances = distances.to(x.device) dist = torch.einsum('nm,hnm->h', (distances, attn_map)) / N if return_map: return dist, attn_map else: return dist def forward(self, x): B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv.unbind(0) attn = (q @ k.transpose(-2, -1)) * self.scale attn = attn.softmax(dim=-1) attn = self.attn_drop(attn) x = (attn @ v).transpose(1, 2).reshape(B, N, C) x = self.proj(x) x = self.proj_drop(x) return x class Block(nn.Module): def __init__( self, dim, num_heads, mlp_ratio=4., qkv_bias=False, proj_drop=0., attn_drop=0., drop_path=0., act_layer=nn.GELU, norm_layer=LayerNorm, use_gpsa=True, locality_strength=1., ): super().__init__() self.norm1 = norm_layer(dim) self.use_gpsa = use_gpsa if self.use_gpsa: self.attn = GPSA( dim, num_heads=num_heads, qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=proj_drop, locality_strength=locality_strength, ) else: self.attn = MHSA( dim, num_heads=num_heads, qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=proj_drop, ) self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp( in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=proj_drop, ) def forward(self, x): x = x + self.drop_path(self.attn(self.norm1(x))) x = x + self.drop_path(self.mlp(self.norm2(x))) return x class ConVit(nn.Module): """ Vision Transformer with support for patch or hybrid CNN input stage """ def __init__( self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, global_pool='token', embed_dim=768, depth=12, num_heads=12, mlp_ratio=4., qkv_bias=False, drop_rate=0., pos_drop_rate=0., proj_drop_rate=0., attn_drop_rate=0., drop_path_rate=0., hybrid_backbone=None, norm_layer=LayerNorm, local_up_to_layer=3, locality_strength=1., use_pos_embed=True, ): super().__init__() assert global_pool in ('', 'avg', 'token') embed_dim *= num_heads self.num_classes = num_classes self.global_pool = global_pool self.local_up_to_layer = local_up_to_layer self.num_features = self.head_hidden_size = self.embed_dim = embed_dim # for consistency with other models self.locality_strength = locality_strength self.use_pos_embed = use_pos_embed if hybrid_backbone is not None: self.patch_embed = HybridEmbed( hybrid_backbone, img_size=img_size, in_chans=in_chans, embed_dim=embed_dim) else: self.patch_embed = PatchEmbed( img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim, ) num_patches = self.patch_embed.num_patches self.num_patches = num_patches self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) self.pos_drop = nn.Dropout(p=pos_drop_rate) if self.use_pos_embed: self.pos_embed = nn.Parameter(torch.zeros(1, num_patches, embed_dim)) trunc_normal_(self.pos_embed, std=.02) dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule self.blocks = nn.ModuleList([ Block( dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, proj_drop=proj_drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer, use_gpsa=i < local_up_to_layer, locality_strength=locality_strength, ) for i in range(depth)]) self.norm = norm_layer(embed_dim) # Classifier head self.feature_info = [dict(num_chs=embed_dim, reduction=0, module='head')] self.head_drop = nn.Dropout(drop_rate) self.head = nn.Linear(embed_dim, num_classes) if num_classes > 0 else nn.Identity() trunc_normal_(self.cls_token, std=.02) self.apply(self._init_weights) for n, m in self.named_modules(): if hasattr(m, 'local_init'): m.local_init() def _init_weights(self, m): if isinstance(m, nn.Linear): trunc_normal_(m.weight, std=.02) if isinstance(m, nn.Linear) and m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.LayerNorm): nn.init.constant_(m.bias, 0) nn.init.constant_(m.weight, 1.0) @torch.jit.ignore def no_weight_decay(self): return {'pos_embed', 'cls_token'} @torch.jit.ignore def group_matcher(self, coarse=False): return dict( stem=r'^cls_token|pos_embed|patch_embed', # stem and embed blocks=[(r'^blocks\.(\d+)', None), (r'^norm', (99999,))] ) @torch.jit.ignore def set_grad_checkpointing(self, enable=True): assert not enable, 'gradient checkpointing not supported' @torch.jit.ignore def get_classifier(self) -> nn.Module: return self.head def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None): self.num_classes = num_classes if global_pool is not None: assert global_pool in ('', 'token', 'avg') self.global_pool = global_pool self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() def forward_features(self, x): x = self.patch_embed(x) if self.use_pos_embed: x = x + self.pos_embed x = self.pos_drop(x) cls_tokens = self.cls_token.expand(x.shape[0], -1, -1) for u, blk in enumerate(self.blocks): if u == self.local_up_to_layer: x = torch.cat((cls_tokens, x), dim=1) x = blk(x) x = self.norm(x) return x def forward_head(self, x, pre_logits: bool = False): if self.global_pool: x = x[:, 1:].mean(dim=1) if self.global_pool == 'avg' else x[:, 0] x = self.head_drop(x) return x if pre_logits else self.head(x) def forward(self, x): x = self.forward_features(x) x = self.forward_head(x) return x def _create_convit(variant, pretrained=False, **kwargs): if kwargs.get('features_only', None): raise RuntimeError('features_only not implemented for Vision Transformer models.') return build_model_with_cfg(ConVit, variant, pretrained, **kwargs) def _cfg(url='', **kwargs): return { 'url': url, 'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': None, 'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD, 'fixed_input_size': True, 'first_conv': 'patch_embed.proj', 'classifier': 'head', **kwargs } default_cfgs = generate_default_cfgs({ # ConViT 'convit_tiny.fb_in1k': _cfg(hf_hub_id='timm/'), 'convit_small.fb_in1k': _cfg(hf_hub_id='timm/'), 'convit_base.fb_in1k': _cfg(hf_hub_id='timm/') }) @register_model def convit_tiny(pretrained=False, **kwargs) -> ConVit: model_args = dict( local_up_to_layer=10, locality_strength=1.0, embed_dim=48, num_heads=4) model = _create_convit(variant='convit_tiny', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def convit_small(pretrained=False, **kwargs) -> ConVit: model_args = dict( local_up_to_layer=10, locality_strength=1.0, embed_dim=48, num_heads=9) model = _create_convit(variant='convit_small', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def convit_base(pretrained=False, **kwargs) -> ConVit: model_args = dict( local_up_to_layer=10, locality_strength=1.0, embed_dim=48, num_heads=16) model = _create_convit(variant='convit_base', pretrained=pretrained, **dict(model_args, **kwargs)) return model
pytorch-image-models/timm/models/convit.py/0
{ "file_path": "pytorch-image-models/timm/models/convit.py", "repo_id": "pytorch-image-models", "token_count": 7721 }
211
""" EVA EVA from https://github.com/baaivision/EVA , paper: https://arxiv.org/abs/2211.07636 @article{EVA, title={EVA: Exploring the Limits of Masked Visual Representation Learning at Scale}, author={Fang, Yuxin and Wang, Wen and Xie, Binhui and Sun, Quan and Wu, Ledell and Wang, Xinggang and Huang, Tiejun and Wang, Xinlong and Cao, Yue}, journal={arXiv preprint arXiv:2211.07636}, year={2022} } EVA-02: A Visual Representation for Neon Genesis - https://arxiv.org/abs/2303.11331 @article{EVA02, title={EVA-02: A Visual Representation for Neon Genesis}, author={Fang, Yuxin and Sun, Quan and Wang, Xinggang and Huang, Tiejun and Wang, Xinlong and Cao, Yue}, journal={arXiv preprint arXiv:2303.11331}, year={2023} } This file contains EVA & EVA02 model implementations evolved from BEiT, additional models in vision_transformer.py. Modifications by / Copyright 2023 Ross Wightman, original copyrights below """ # EVA models Copyright (c) 2022 BAAI-Vision # EVA02 models Copyright (c) 2023 BAAI-Vision import math from typing import Callable, List, Optional, Tuple, Union import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.checkpoint import checkpoint from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, OPENAI_CLIP_MEAN, OPENAI_CLIP_STD from timm.layers import PatchEmbed, Mlp, GluMlp, SwiGLU, LayerNorm, DropPath, PatchDropout, RotaryEmbeddingCat, \ apply_rot_embed_cat, apply_keep_indices_nlc, trunc_normal_, resample_patch_embed, resample_abs_pos_embed, \ to_2tuple, use_fused_attn from ._builder import build_model_with_cfg from ._features import feature_take_indices from ._registry import generate_default_cfgs, register_model __all__ = ['Eva'] class EvaAttention(nn.Module): fused_attn: torch.jit.Final[bool] def __init__( self, dim: int, num_heads: int = 8, qkv_bias: bool = True, qkv_fused: bool = True, num_prefix_tokens: int = 1, qkv_bias_separate: bool = False, attn_drop: float = 0., proj_drop: float = 0., attn_head_dim: Optional[int] = None, norm_layer: Optional[Callable] = None, ): """ Args: dim: num_heads: qkv_bias: qkv_fused: attn_drop: proj_drop: attn_head_dim: norm_layer: """ super().__init__() self.num_heads = num_heads head_dim = dim // num_heads if attn_head_dim is not None: head_dim = attn_head_dim all_head_dim = head_dim * self.num_heads self.scale = head_dim ** -0.5 self.num_prefix_tokens = num_prefix_tokens self.fused_attn = use_fused_attn() self.qkv_bias_separate = qkv_bias_separate if qkv_fused: self.qkv = nn.Linear(dim, all_head_dim * 3, bias=False) self.q_proj = self.k_proj = self.v_proj = None if qkv_bias: self.q_bias = nn.Parameter(torch.zeros(all_head_dim)) self.register_buffer('k_bias', torch.zeros(all_head_dim), persistent=False) self.v_bias = nn.Parameter(torch.zeros(all_head_dim)) else: self.q_bias = self.k_bias = self.v_bias = None else: self.q_proj = nn.Linear(dim, all_head_dim, bias=qkv_bias) self.k_proj = nn.Linear(dim, all_head_dim, bias=False) self.v_proj = nn.Linear(dim, all_head_dim, bias=qkv_bias) self.qkv = None self.q_bias = self.k_bias = self.v_bias = None self.attn_drop = nn.Dropout(attn_drop) self.norm = norm_layer(all_head_dim) if norm_layer is not None else nn.Identity() self.proj = nn.Linear(all_head_dim, dim) self.proj_drop = nn.Dropout(proj_drop) def forward( self, x, rope: Optional[torch.Tensor] = None, attn_mask: Optional[torch.Tensor] = None, ): B, N, C = x.shape if self.qkv is not None: if self.q_bias is None: qkv = self.qkv(x) else: qkv_bias = torch.cat((self.q_bias, self.k_bias, self.v_bias)) if self.qkv_bias_separate: qkv = self.qkv(x) qkv += qkv_bias else: qkv = F.linear(x, weight=self.qkv.weight, bias=qkv_bias) qkv = qkv.reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) q, k, v = qkv.unbind(0) # B, num_heads, N, head_dim else: q = self.q_proj(x).reshape(B, N, self.num_heads, -1).transpose(1, 2) # B, num_heads, N, C k = self.k_proj(x).reshape(B, N, self.num_heads, -1).transpose(1, 2) v = self.v_proj(x).reshape(B, N, self.num_heads, -1).transpose(1, 2) if rope is not None: npt = self.num_prefix_tokens q = torch.cat([q[:, :, :npt, :], apply_rot_embed_cat(q[:, :, npt:, :], rope)], dim=2).type_as(v) k = torch.cat([k[:, :, :npt, :], apply_rot_embed_cat(k[:, :, npt:, :], rope)], dim=2).type_as(v) if self.fused_attn: x = F.scaled_dot_product_attention( q, k, v, attn_mask=attn_mask, dropout_p=self.attn_drop.p if self.training else 0., ) else: q = q * self.scale attn = (q @ k.transpose(-2, -1)) if attn_mask is not None: attn_mask = attn_mask.to(torch.bool) attn = attn.masked_fill(~attn_mask[:, None, None, :], float("-inf")) attn = attn.softmax(dim=-1) attn = self.attn_drop(attn) x = attn @ v x = x.transpose(1, 2).reshape(B, N, C) x = self.norm(x) x = self.proj(x) x = self.proj_drop(x) return x class EvaBlock(nn.Module): def __init__( self, dim: int, num_heads: int, qkv_bias: bool = True, qkv_fused: bool = True, mlp_ratio: float = 4., swiglu_mlp: bool = False, scale_mlp: bool = False, scale_attn_inner: bool = False, num_prefix_tokens: int = 1, proj_drop: float = 0., attn_drop: float = 0., drop_path: float = 0., init_values: Optional[float] = None, act_layer: Callable = nn.GELU, norm_layer: Callable = LayerNorm, attn_head_dim: Optional[int] = None, ): """ Args: dim: num_heads: qkv_bias: qkv_fused: mlp_ratio: swiglu_mlp: scale_mlp: scale_attn_inner: proj_drop: attn_drop: drop_path: init_values: act_layer: norm_layer: attn_head_dim: """ super().__init__() self.norm1 = norm_layer(dim) self.attn = EvaAttention( dim, num_heads=num_heads, qkv_bias=qkv_bias, qkv_fused=qkv_fused, num_prefix_tokens=num_prefix_tokens, attn_drop=attn_drop, proj_drop=proj_drop, attn_head_dim=attn_head_dim, norm_layer=norm_layer if scale_attn_inner else None, ) self.gamma_1 = nn.Parameter(init_values * torch.ones(dim)) if init_values is not None else None self.drop_path1 = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.norm2 = norm_layer(dim) hidden_features = int(dim * mlp_ratio) if swiglu_mlp: if scale_mlp: # when norm in SwiGLU used, an impl with separate fc for gate & x is used self.mlp = SwiGLU( in_features=dim, hidden_features=hidden_features, norm_layer=norm_layer if scale_mlp else None, drop=proj_drop, ) else: # w/o any extra norm, an impl with packed weights is used, matches existing GluMLP self.mlp = GluMlp( in_features=dim, hidden_features=hidden_features * 2, norm_layer=norm_layer if scale_mlp else None, act_layer=nn.SiLU, gate_last=False, drop=proj_drop, ) else: self.mlp = Mlp( in_features=dim, hidden_features=hidden_features, act_layer=act_layer, norm_layer=norm_layer if scale_mlp else None, drop=proj_drop, ) self.gamma_2 = nn.Parameter(init_values * torch.ones(dim)) if init_values is not None else None self.drop_path2 = DropPath(drop_path) if drop_path > 0. else nn.Identity() def forward(self, x, rope: Optional[torch.Tensor] = None, attn_mask: Optional[torch.Tensor] = None): if self.gamma_1 is None: x = x + self.drop_path1(self.attn(self.norm1(x), rope=rope, attn_mask=attn_mask)) x = x + self.drop_path2(self.mlp(self.norm2(x))) else: x = x + self.drop_path1(self.gamma_1 * self.attn(self.norm1(x), rope=rope, attn_mask=attn_mask)) x = x + self.drop_path2(self.gamma_2 * self.mlp(self.norm2(x))) return x class EvaBlockPostNorm(nn.Module): """ EVA block w/ post-norm and support for swiglu, MLP norm scale, ROPE. """ def __init__( self, dim: int, num_heads: int, qkv_bias: bool = True, qkv_fused: bool = True, mlp_ratio: float = 4., swiglu_mlp: bool = False, scale_mlp: bool = False, scale_attn_inner: bool = False, num_prefix_tokens: int = 1, proj_drop: float = 0., attn_drop: float = 0., drop_path: float = 0., init_values: Optional[float] = None, # ignore for post-norm act_layer: Callable = nn.GELU, norm_layer: Callable = nn.LayerNorm, attn_head_dim: Optional[int] = None, ): """ Args: dim: num_heads: qkv_bias: qkv_fused: mlp_ratio: swiglu_mlp: scale_mlp: scale_attn_inner: proj_drop: attn_drop: drop_path: init_values: act_layer: norm_layer: attn_head_dim: """ super().__init__() self.attn = EvaAttention( dim, num_heads=num_heads, qkv_bias=qkv_bias, qkv_fused=qkv_fused, num_prefix_tokens=num_prefix_tokens, attn_drop=attn_drop, proj_drop=proj_drop, attn_head_dim=attn_head_dim, norm_layer=norm_layer if scale_attn_inner else None, ) self.norm1 = norm_layer(dim) self.drop_path1 = DropPath(drop_path) if drop_path > 0. else nn.Identity() hidden_features = int(dim * mlp_ratio) if swiglu_mlp: if scale_mlp: # when norm in SwiGLU used, an impl with separate fc for gate & x is used self.mlp = SwiGLU( in_features=dim, hidden_features=hidden_features, norm_layer=norm_layer if scale_mlp else None, drop=proj_drop, ) else: # w/o any extra norm, an impl with packed fc1 weights is used, matches existing GluMLP self.mlp = GluMlp( in_features=dim, hidden_features=hidden_features * 2, norm_layer=norm_layer if scale_mlp else None, act_layer=nn.SiLU, gate_last=False, drop=proj_drop, ) else: self.mlp = Mlp( in_features=dim, hidden_features=hidden_features, act_layer=act_layer, norm_layer=norm_layer if scale_mlp else None, drop=proj_drop, ) self.norm2 = norm_layer(dim) self.drop_path2 = DropPath(drop_path) if drop_path > 0. else nn.Identity() def forward(self, x, rope: Optional[torch.Tensor] = None, attn_mask: Optional[torch.Tensor] = None): x = x + self.drop_path1(self.norm1(self.attn(x, rope=rope, attn_mask=attn_mask))) x = x + self.drop_path2(self.norm2(self.mlp(x))) return x class Eva(nn.Module): """ Eva Vision Transformer w/ Abs & Rotary Pos Embed This class implements the EVA and EVA02 models that were based on the BEiT ViT variant * EVA - abs pos embed, global avg pool * EVA02 - abs + rope pos embed, global avg pool, SwiGLU, scale Norm in MLP (ala normformer) """ def __init__( self, img_size: Union[int, Tuple[int, int]] = 224, patch_size: Union[int, Tuple[int, int]] = 16, in_chans: int = 3, num_classes: int = 1000, global_pool: str = 'avg', embed_dim: int = 768, depth: int = 12, num_heads: int = 12, qkv_bias: bool = True, qkv_fused: bool = True, mlp_ratio: float = 4., swiglu_mlp: bool = False, scale_mlp: bool = False, scale_attn_inner: bool = False, drop_rate: float = 0., pos_drop_rate: float = 0., patch_drop_rate: float = 0., proj_drop_rate: float = 0., attn_drop_rate: float = 0., drop_path_rate: float = 0., norm_layer: Callable = LayerNorm, init_values: Optional[float] = None, class_token: bool = True, num_reg_tokens: int = 0, use_abs_pos_emb: bool = True, use_rot_pos_emb: bool = False, use_post_norm: bool = False, dynamic_img_size: bool = False, dynamic_img_pad: bool = False, ref_feat_shape: Optional[Union[Tuple[int, int], int]] = None, head_init_scale: float = 0.001, ): """ Args: img_size: patch_size: in_chans: num_classes: global_pool: embed_dim: depth: num_heads: qkv_bias: qkv_fused: mlp_ratio: swiglu_mlp: scale_mlp: scale_attn_inner: drop_rate: pos_drop_rate: proj_drop_rate: attn_drop_rate: drop_path_rate: norm_layer: init_values: class_token: use_abs_pos_emb: use_rot_pos_emb: use_post_norm: ref_feat_shape: head_init_scale: """ super().__init__() self.num_classes = num_classes self.global_pool = global_pool self.num_features = self.head_hidden_size = self.embed_dim = embed_dim # for consistency with other models self.num_prefix_tokens = (1 if class_token else 0) + num_reg_tokens self.dynamic_img_size = dynamic_img_size self.grad_checkpointing = False embed_args = {} if dynamic_img_size: # flatten deferred until after pos embed embed_args.update(dict(strict_img_size=False, output_fmt='NHWC')) self.patch_embed = PatchEmbed( img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim, dynamic_img_pad=dynamic_img_pad, **embed_args, ) num_patches = self.patch_embed.num_patches r = self.patch_embed.feat_ratio() if hasattr(self.patch_embed, 'feat_ratio') else patch_size self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) if class_token else None self.reg_token = nn.Parameter(torch.zeros(1, num_reg_tokens, embed_dim)) if num_reg_tokens else None self.cls_embed = class_token and self.reg_token is None self.pos_embed = nn.Parameter( torch.zeros(1, num_patches + self.num_prefix_tokens, embed_dim)) if use_abs_pos_emb else None self.pos_drop = nn.Dropout(p=pos_drop_rate) if patch_drop_rate > 0: self.patch_drop = PatchDropout( patch_drop_rate, num_prefix_tokens=self.num_prefix_tokens, return_indices=True, ) else: self.patch_drop = None if use_rot_pos_emb: ref_feat_shape = to_2tuple(ref_feat_shape) if ref_feat_shape is not None else None self.rope = RotaryEmbeddingCat( embed_dim // num_heads, in_pixels=False, feat_shape=None if dynamic_img_size else self.patch_embed.grid_size, ref_feat_shape=ref_feat_shape, ) else: self.rope = None dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule block_fn = EvaBlockPostNorm if use_post_norm else EvaBlock self.blocks = nn.ModuleList([ block_fn( dim=embed_dim, num_heads=num_heads, qkv_bias=qkv_bias, qkv_fused=qkv_fused, mlp_ratio=mlp_ratio, swiglu_mlp=swiglu_mlp, scale_mlp=scale_mlp, scale_attn_inner=scale_attn_inner, num_prefix_tokens=self.num_prefix_tokens, proj_drop=proj_drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer, init_values=init_values, ) for i in range(depth)]) self.feature_info = [ dict(module=f'blocks.{i}', num_chs=embed_dim, reduction=r) for i in range(depth)] use_fc_norm = self.global_pool == 'avg' self.norm = nn.Identity() if use_fc_norm else norm_layer(embed_dim) self.fc_norm = norm_layer(embed_dim) if use_fc_norm else nn.Identity() self.head_drop = nn.Dropout(drop_rate) self.head = nn.Linear(embed_dim, num_classes) if num_classes > 0 else nn.Identity() self.apply(self._init_weights) if self.pos_embed is not None: trunc_normal_(self.pos_embed, std=.02) if self.cls_token is not None: trunc_normal_(self.cls_token, std=.02) if self.reg_token is not None: trunc_normal_(self.reg_token, std=.02) self.fix_init_weight() if isinstance(self.head, nn.Linear): trunc_normal_(self.head.weight, std=.02) self.head.weight.data.mul_(head_init_scale) self.head.bias.data.mul_(head_init_scale) def fix_init_weight(self): def rescale(param, layer_id): param.div_(math.sqrt(2.0 * layer_id)) for layer_id, layer in enumerate(self.blocks): rescale(layer.attn.proj.weight.data, layer_id + 1) rescale(layer.mlp.fc2.weight.data, layer_id + 1) def _init_weights(self, m): if isinstance(m, nn.Linear): trunc_normal_(m.weight, std=.02) if m.bias is not None: nn.init.zeros_(m.bias) @torch.jit.ignore def no_weight_decay(self): nwd = {'pos_embed', 'cls_token'} return nwd @torch.jit.ignore def set_grad_checkpointing(self, enable=True): self.grad_checkpointing = enable @torch.jit.ignore def group_matcher(self, coarse=False): matcher = dict( stem=r'^cls_token|pos_embed|patch_embed', # stem and embed blocks=[(r'^blocks\.(\d+)', None), (r'^norm', (99999,))], ) return matcher @torch.jit.ignore def get_classifier(self) -> nn.Module: return self.head def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None): self.num_classes = num_classes if global_pool is not None: self.global_pool = global_pool self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() def _pos_embed(self, x) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: if self.dynamic_img_size: B, H, W, C = x.shape if self.pos_embed is not None: pos_embed = resample_abs_pos_embed( self.pos_embed, (H, W), num_prefix_tokens=self.num_prefix_tokens, ) else: pos_embed = None x = x.view(B, -1, C) rot_pos_embed = self.rope.get_embed(shape=(H, W)) if self.rope is not None else None else: pos_embed = self.pos_embed rot_pos_embed = self.rope.get_embed() if self.rope is not None else None if self.cls_token is not None: x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1) if pos_embed is not None: x = x + pos_embed if self.reg_token is not None: to_cat = [] if self.cls_token is not None: to_cat.append(self.cls_token.expand(x.shape[0], -1, -1)) to_cat.append(self.reg_token.expand(x.shape[0], -1, -1)) x = torch.cat(to_cat + [x], dim=1) x = self.pos_drop(x) # obtain shared rotary position embedding and apply patch dropout if self.patch_drop is not None: x, keep_indices = self.patch_drop(x) if rot_pos_embed is not None and keep_indices is not None: rot_pos_embed = apply_keep_indices_nlc(x, rot_pos_embed, keep_indices) return x, rot_pos_embed def forward_intermediates( self, x: torch.Tensor, indices: Optional[Union[int, List[int]]] = None, return_prefix_tokens: bool = False, norm: bool = False, stop_early: bool = False, output_fmt: str = 'NCHW', intermediates_only: bool = False, ) -> Union[List[torch.Tensor], Tuple[torch.Tensor, List[torch.Tensor]]]: """ Forward features that returns intermediates. Args: x: Input image tensor indices: Take last n blocks if an int, if is a sequence, select by matching indices return_prefix_tokens: Return both prefix and spatial intermediate tokens norm: Apply norm layer to all intermediates stop_early: Stop iterating over blocks when last desired intermediate hit output_fmt: Shape of intermediate feature outputs intermediates_only: Only return intermediate features """ assert output_fmt in ('NCHW', 'NLC'), 'Output format for EVA-ViT features must be one of NCHW or NLC.' reshape = output_fmt == 'NCHW' intermediates = [] take_indices, max_index = feature_take_indices(len(self.blocks), indices) # forward pass B, _, height, width = x.shape x = self.patch_embed(x) x, rot_pos_embed = self._pos_embed(x) if torch.jit.is_scripting() or not stop_early: # can't slice blocks in torchscript blocks = self.blocks else: blocks = self.blocks[:max_index + 1] for i, blk in enumerate(blocks): x = blk(x, rope=rot_pos_embed) if i in take_indices: intermediates.append(self.norm(x) if norm else x) # process intermediates if self.num_prefix_tokens: # split prefix (e.g. class, distill) and spatial feature tokens prefix_tokens = [y[:, 0:self.num_prefix_tokens] for y in intermediates] intermediates = [y[:, self.num_prefix_tokens:] for y in intermediates] if reshape: # reshape to BCHW output format H, W = self.patch_embed.dynamic_feat_size((height, width)) intermediates = [y.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous() for y in intermediates] if not torch.jit.is_scripting() and return_prefix_tokens: # return_prefix not support in torchscript due to poor type handling intermediates = list(zip(intermediates, prefix_tokens)) if intermediates_only: return intermediates x = self.norm(x) return x, intermediates def prune_intermediate_layers( self, indices: Union[int, List[int]] = 1, prune_norm: bool = False, prune_head: bool = True, ): """ Prune layers not required for specified intermediates. """ take_indices, max_index = feature_take_indices(len(self.blocks), indices) self.blocks = self.blocks[:max_index + 1] # truncate blocks if prune_norm: self.norm = nn.Identity() if prune_head: self.fc_norm = nn.Identity() self.reset_classifier(0, '') return take_indices def forward_features(self, x): x = self.patch_embed(x) x, rot_pos_embed = self._pos_embed(x) for blk in self.blocks: if self.grad_checkpointing and not torch.jit.is_scripting(): x = checkpoint(blk, x, rope=rot_pos_embed) else: x = blk(x, rope=rot_pos_embed) x = self.norm(x) return x def forward_head(self, x, pre_logits: bool = False): if self.global_pool: x = x[:, self.num_prefix_tokens:].mean(dim=1) if self.global_pool == 'avg' else x[:, 0] x = self.fc_norm(x) x = self.head_drop(x) return x if pre_logits else self.head(x) def forward(self, x): x = self.forward_features(x) x = self.forward_head(x) return x def checkpoint_filter_fn( state_dict, model, interpolation='bicubic', antialias=True, ): """ convert patch embedding weight from manual patchify + linear proj to conv""" out_dict = {} state_dict = state_dict.get('model_ema', state_dict) state_dict = state_dict.get('model', state_dict) state_dict = state_dict.get('module', state_dict) state_dict = state_dict.get('state_dict', state_dict) # prefix for loading OpenCLIP compatible weights if 'visual.trunk.pos_embed' in state_dict: prefix = 'visual.trunk.' elif 'visual.pos_embed' in state_dict: prefix = 'visual.' else: prefix = '' mim_weights = prefix + 'mask_token' in state_dict no_qkv = prefix + 'blocks.0.attn.q_proj.weight' in state_dict len_prefix = len(prefix) for k, v in state_dict.items(): if prefix: if k.startswith(prefix): k = k[len_prefix:] else: continue if 'rope' in k: # fixed embedding no need to load buffer from checkpoint continue if 'patch_embed.proj.weight' in k: _, _, H, W = model.patch_embed.proj.weight.shape if v.shape[-1] != W or v.shape[-2] != H: v = resample_patch_embed( v, (H, W), interpolation=interpolation, antialias=antialias, verbose=True, ) elif k == 'pos_embed' and v.shape[1] != model.pos_embed.shape[1]: # To resize pos embedding when using model at different size from pretrained weights num_prefix_tokens = 0 if getattr(model, 'no_embed_class', False) else getattr(model, 'num_prefix_tokens', 1) v = resample_abs_pos_embed( v, new_size=model.patch_embed.grid_size, num_prefix_tokens=num_prefix_tokens, interpolation=interpolation, antialias=antialias, verbose=True, ) k = k.replace('mlp.ffn_ln', 'mlp.norm') k = k.replace('attn.inner_attn_ln', 'attn.norm') k = k.replace('mlp.w12', 'mlp.fc1') k = k.replace('mlp.w1', 'mlp.fc1_g') k = k.replace('mlp.w2', 'mlp.fc1_x') k = k.replace('mlp.w3', 'mlp.fc2') if no_qkv: k = k.replace('q_bias', 'q_proj.bias') k = k.replace('v_bias', 'v_proj.bias') if mim_weights and k in ('mask_token', 'lm_head.weight', 'lm_head.bias', 'norm.weight', 'norm.bias'): if k == 'norm.weight' or k == 'norm.bias': # try moving norm -> fc norm on fine-tune, probably a better starting point than new init k = k.replace('norm', 'fc_norm') else: # skip pretrain mask token & head weights continue out_dict[k] = v return out_dict def _create_eva(variant, pretrained=False, **kwargs): out_indices = kwargs.pop('out_indices', 3) model = build_model_with_cfg( Eva, variant, pretrained, pretrained_filter_fn=checkpoint_filter_fn, feature_cfg=dict(out_indices=out_indices, feature_cls='getter'), **kwargs, ) return model def _cfg(url='', **kwargs): return { 'url': url, 'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': None, 'crop_pct': .9, 'interpolation': 'bicubic', 'fixed_input_size': True, 'mean': OPENAI_CLIP_MEAN, 'std': OPENAI_CLIP_STD, 'first_conv': 'patch_embed.proj', 'classifier': 'head', 'license': 'mit', **kwargs } default_cfgs = generate_default_cfgs({ # EVA 01 CLIP fine-tuned on imagenet-1k 'eva_giant_patch14_224.clip_ft_in1k': _cfg( # hf_hub_id='BAAI/EVA', hf_hub_filename='eva_clip_vis_enc_sz224_ftcls_89p1.pt', hf_hub_id='timm/', ), 'eva_giant_patch14_336.clip_ft_in1k': _cfg( # hf_hub_id='BAAI/EVA', hf_hub_filename='eva_clip_vis_enc_sz336_ftcls_89p4.pt', hf_hub_id='timm/', input_size=(3, 336, 336), crop_pct=1.0, crop_mode='squash'), # MIM EVA 01 pretrain, ft on in22k -> in1k 'eva_giant_patch14_336.m30m_ft_in22k_in1k': _cfg( # hf_hub_id='BAAI/EVA', hf_hub_filename='eva_21k_1k_336px_psz14_ema_89p6.pt', hf_hub_id='timm/', mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD, input_size=(3, 336, 336), crop_pct=1.0, crop_mode='squash'), 'eva_giant_patch14_560.m30m_ft_in22k_in1k': _cfg( # hf_hub_id='BAAI/EVA', hf_hub_filename='eva_21k_1k_560px_psz14_ema_89p7.pt', hf_hub_id='timm/', mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD, input_size=(3, 560, 560), crop_pct=1.0, crop_mode='squash'), # in22k or m38m MIM pretrain w/ intermediate in22k fine-tune and final in1k fine-tune 'eva02_base_patch14_448.mim_in22k_ft_in22k_in1k': _cfg( # hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in21k_to_in1k/eva02_B_pt_in21k_medft_in21k_ft_in1k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, crop_mode='squash', ), 'eva02_large_patch14_448.mim_in22k_ft_in22k_in1k': _cfg( # hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in21k_to_in1k/eva02_L_pt_in21k_medft_in21k_ft_in1k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, crop_mode='squash', ), 'eva02_large_patch14_448.mim_m38m_ft_in22k_in1k': _cfg( hf_hub_id='timm/', #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in21k_to_in1k/eva02_L_pt_m38m_medft_in21k_ft_in1k_p14.pt', input_size=(3, 448, 448), crop_pct=1.0, crop_mode='squash', ), # in22k or m3m MIM pretrain w/ in1k fine-tune 'eva02_tiny_patch14_336.mim_in22k_ft_in1k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in1k/eva02_Ti_pt_in21k_ft_in1k_p14.pt', hf_hub_id='timm/', input_size=(3, 336, 336), crop_pct=1.0, ), 'eva02_small_patch14_336.mim_in22k_ft_in1k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in1k/eva02_S_pt_in21k_ft_in1k_p14.pt', hf_hub_id='timm/', input_size=(3, 336, 336), crop_pct=1.0, ), 'eva02_base_patch14_448.mim_in22k_ft_in1k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in1k/eva02_B_pt_in21k_ft_in1k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, ), 'eva02_large_patch14_448.mim_in22k_ft_in1k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in1k/eva02_L_pt_in21k_ft_in1k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, ), 'eva02_large_patch14_448.mim_m38m_ft_in1k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in1k/eva02_L_pt_m38m_ft_in1k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, ), # in22k or m3m MIM pretrain w/ in22k fine-tune 'eva02_base_patch14_448.mim_in22k_ft_in22k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in21k/eva02_B_pt_in21k_medft_in21k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, crop_mode='squash', num_classes=21841, ), 'eva02_large_patch14_448.mim_in22k_ft_in22k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in21k/eva02_L_pt_in21k_medft_in21k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, crop_mode='squash', num_classes=21841, ), 'eva02_large_patch14_448.mim_m38m_ft_in22k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/cls/in21k/eva02_L_pt_m38m_medft_in21k_p14.pt', hf_hub_id='timm/', input_size=(3, 448, 448), crop_pct=1.0, crop_mode='squash', num_classes=21841, ), # in22k or m38m MIM pretrain 'eva02_tiny_patch14_224.mim_in22k': _cfg( # hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/pt/eva02_Ti_pt_in21k_p14.pt', hf_hub_id='timm/', num_classes=0, ), 'eva02_small_patch14_224.mim_in22k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/pt/eva02_S_pt_in21k_p14.pt', hf_hub_id='timm/', num_classes=0, ), 'eva02_base_patch14_224.mim_in22k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/pt/eva02_B_pt_in21k_p14.pt', hf_hub_id='timm/', num_classes=0, ), 'eva02_large_patch14_224.mim_in22k': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/pt/eva02_L_pt_in21k_p14.pt', hf_hub_id='timm/', num_classes=0, ), 'eva02_large_patch14_224.mim_m38m': _cfg( #hf_hub_id='Yuxin-CV/EVA-02', hf_hub_filename='eva02/pt/eva02_L_pt_m38m_p14.pt', hf_hub_id='timm/', num_classes=0, ), # EVA01 and EVA02 CLIP image towers 'eva_giant_patch14_clip_224.laion400m': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA01_CLIP_g_14_plus_psz14_s11B.pt', hf_hub_id='timm/eva_giant_patch14_clip_224.laion400m_s11b_b41k', # float16 weights hf_hub_filename='open_clip_pytorch_model.bin', num_classes=1024, ), 'eva_giant_patch14_clip_224.merged2b': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA01_CLIP_g_14_plus_psz14_s11B.pt', hf_hub_id='timm/eva_giant_patch14_plus_clip_224.merged2b_s11b_b114k', # float16 weights hf_hub_filename='open_clip_pytorch_model.bin', num_classes=1024, ), 'eva02_base_patch16_clip_224.merged2b': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA02_CLIP_L_psz14_s4B.pt', hf_hub_id='timm/eva02_base_patch16_clip_224.merged2b_s8b_b131k', # float16 weights hf_hub_filename='open_clip_pytorch_model.bin', num_classes=512, ), 'eva02_large_patch14_clip_224.merged2b': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA02_CLIP_L_psz14_s4B.pt', hf_hub_id='timm/eva02_large_patch14_clip_224.merged2b_s4b_b131k', # float16 weights hf_hub_filename='open_clip_pytorch_model.bin', num_classes=768, ), 'eva02_large_patch14_clip_336.merged2b': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA02_CLIP_L_psz14_s4B.pt', hf_hub_id='timm/eva02_large_patch14_clip_336.merged2b_s6b_b61k', # float16 weights hf_hub_filename='open_clip_pytorch_model.bin', input_size=(3, 336, 336), crop_pct=1.0, num_classes=768, ), 'eva02_enormous_patch14_clip_224.laion2b': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA02_CLIP_E_psz14_plus_s9B.pt', hf_hub_id='timm/eva02_enormous_patch14_clip_224.laion2b_s4b_b115k', # float16 weights hf_hub_filename='open_clip_pytorch_model.bin', num_classes=1024, ), 'eva02_enormous_patch14_clip_224.laion2b_plus': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA02_CLIP_E_psz14_plus_s9B.pt', hf_hub_id='timm/eva02_enormous_patch14_plus_clip_224.laion2b_s9b_b144k', # bfloat16 weights hf_hub_filename='open_clip_pytorch_model.bin', num_classes=1024, ), 'eva02_enormous_patch14_clip_224.pretrain': _cfg( # hf_hub_id='QuanSun/EVA-CLIP', hf_hub_filename='EVA02_E_psz14.pt', num_classes=0, ), 'vit_medium_patch16_rope_reg1_gap_256.sbb_in1k': _cfg( hf_hub_id='timm/', input_size=(3, 256, 256), crop_pct=0.95, mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5) ), 'vit_mediumd_patch16_rope_reg1_gap_256.sbb_in1k': _cfg( hf_hub_id='timm/', input_size=(3, 256, 256), crop_pct=0.95, mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5) ), 'vit_betwixt_patch16_rope_reg4_gap_256.sbb_in1k': _cfg( hf_hub_id='timm/', input_size=(3, 256, 256), crop_pct=0.95, ), 'vit_base_patch16_rope_reg1_gap_256.sbb_in1k': _cfg( hf_hub_id='timm/', input_size=(3, 256, 256), crop_pct=0.95, mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5) ), }) @register_model def eva_giant_patch14_224(pretrained=False, **kwargs) -> Eva: """ EVA-g model https://arxiv.org/abs/2211.07636 """ model_args = dict(patch_size=14, embed_dim=1408, depth=40, num_heads=16, mlp_ratio=6144 / 1408) model = _create_eva('eva_giant_patch14_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva_giant_patch14_336(pretrained=False, **kwargs) -> Eva: """ EVA-g model https://arxiv.org/abs/2211.07636 """ model_args = dict(patch_size=14, embed_dim=1408, depth=40, num_heads=16, mlp_ratio=6144 / 1408) model = _create_eva('eva_giant_patch14_336', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva_giant_patch14_560(pretrained=False, **kwargs) -> Eva: """ EVA-g model https://arxiv.org/abs/2211.07636 """ model_args = dict(patch_size=14, embed_dim=1408, depth=40, num_heads=16, mlp_ratio=6144 / 1408) model = _create_eva('eva_giant_patch14_560', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_tiny_patch14_224(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=224, patch_size=14, embed_dim=192, depth=12, num_heads=3, mlp_ratio=4 * 2 / 3, swiglu_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_tiny_patch14_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_small_patch14_224(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=224, patch_size=14, embed_dim=384, depth=12, num_heads=6, mlp_ratio=4 * 2 / 3, swiglu_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_small_patch14_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_base_patch14_224(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=224, patch_size=14, embed_dim=768, depth=12, num_heads=12, qkv_fused=False, mlp_ratio=4 * 2 / 3, swiglu_mlp=True, scale_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_base_patch14_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_large_patch14_224(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=224, patch_size=14, embed_dim=1024, depth=24, num_heads=16, mlp_ratio=4 * 2 / 3, qkv_fused=False, swiglu_mlp=True, scale_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_large_patch14_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_tiny_patch14_336(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=336, patch_size=14, embed_dim=192, depth=12, num_heads=3, mlp_ratio=4 * 2 / 3, swiglu_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_tiny_patch14_336', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_small_patch14_336(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=336, patch_size=14, embed_dim=384, depth=12, num_heads=6, mlp_ratio=4 * 2 / 3, swiglu_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_small_patch14_336', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_base_patch14_448(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=448, patch_size=14, embed_dim=768, depth=12, num_heads=12, qkv_fused=False, mlp_ratio=4 * 2 / 3, swiglu_mlp=True, scale_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_base_patch14_448', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_large_patch14_448(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=448, patch_size=14, embed_dim=1024, depth=24, num_heads=16, mlp_ratio=4 * 2 / 3, qkv_fused=False, swiglu_mlp=True, scale_mlp=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('eva02_large_patch14_448', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva_giant_patch14_clip_224(pretrained=False, **kwargs) -> Eva: """ EVA-g CLIP model (only difference from non-CLIP is the pooling) """ model_args = dict( patch_size=14, embed_dim=1408, depth=40, num_heads=16, mlp_ratio=6144 / 1408, global_pool=kwargs.pop('global_pool', 'token')) model = _create_eva('eva_giant_patch14_clip_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_base_patch16_clip_224(pretrained=False, **kwargs) -> Eva: """ A EVA-CLIP specific variant that adds additional attn scale layernorm to eva02_base """ model_args = dict( img_size=224, patch_size=16, embed_dim=768, depth=12, num_heads=12, qkv_fused=False, mlp_ratio=4 * 2 / 3, swiglu_mlp=True, scale_mlp=True, scale_attn_inner=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 global_pool=kwargs.pop('global_pool', 'token'), ) model = _create_eva('eva02_base_patch16_clip_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_large_patch14_clip_224(pretrained=False, **kwargs) -> Eva: """ A EVA-CLIP specific variant that adds additional attn scale layernorm to eva02_large """ model_args = dict( img_size=224, patch_size=14, embed_dim=1024, depth=24, num_heads=16, mlp_ratio=4 * 2 / 3, qkv_fused=False, swiglu_mlp=True, scale_mlp=True, scale_attn_inner=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 global_pool=kwargs.pop('global_pool', 'token'), ) model = _create_eva('eva02_large_patch14_clip_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_large_patch14_clip_336(pretrained=False, **kwargs) -> Eva: """ A EVA-CLIP specific variant that adds additional attn scale layernorm to eva02_large """ model_args = dict( img_size=336, patch_size=14, embed_dim=1024, depth=24, num_heads=16, mlp_ratio=4 * 2 / 3, qkv_fused=False, swiglu_mlp=True, scale_mlp=True, scale_attn_inner=True, use_rot_pos_emb=True, ref_feat_shape=(16, 16), # 224/14 global_pool=kwargs.pop('global_pool', 'token'), ) model = _create_eva('eva02_large_patch14_clip_336', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def eva02_enormous_patch14_clip_224(pretrained=False, **kwargs) -> Eva: """ A EVA-CLIP specific variant that uses residual post-norm in blocks """ model_args = dict( img_size=224, patch_size=14, embed_dim=1792, depth=64, num_heads=16, mlp_ratio=15360 / 1792, use_post_norm=True, global_pool=kwargs.pop('global_pool', 'token'), ) model = _create_eva('eva02_enormous_patch14_clip_224', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def vit_medium_patch16_rope_reg1_gap_256(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=256, patch_size=16, embed_dim=512, depth=12, num_heads=8, qkv_fused=True, qkv_bias=True, init_values=1e-5, class_token=False, num_reg_tokens=1, use_rot_pos_emb=True, use_abs_pos_emb=False, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('vit_medium_patch16_rope_reg1_gap_256', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def vit_mediumd_patch16_rope_reg1_gap_256(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=256, patch_size=16, embed_dim=512, depth=20, num_heads=8, qkv_fused=True, qkv_bias=False, init_values=1e-5, class_token=False, num_reg_tokens=1, use_rot_pos_emb=True, use_abs_pos_emb=False, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('vit_mediumd_patch16_rope_reg1_gap_256', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def vit_betwixt_patch16_rope_reg4_gap_256(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=256, patch_size=16, embed_dim=640, depth=12, num_heads=10, qkv_fused=True, qkv_bias=True, init_values=1e-5, class_token=False, num_reg_tokens=4, use_rot_pos_emb=True, use_abs_pos_emb=False, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('vit_betwixt_patch16_rope_reg4_gap_256', pretrained=pretrained, **dict(model_args, **kwargs)) return model @register_model def vit_base_patch16_rope_reg1_gap_256(pretrained=False, **kwargs) -> Eva: model_args = dict( img_size=256, patch_size=16, embed_dim=768, depth=12, num_heads=12, qkv_fused=True, qkv_bias=True, init_values=1e-5, class_token=False, num_reg_tokens=1, use_rot_pos_emb=True, use_abs_pos_emb=False, ref_feat_shape=(16, 16), # 224/14 ) model = _create_eva('vit_base_patch16_rope_reg1_gap_256', pretrained=pretrained, **dict(model_args, **kwargs)) return model
pytorch-image-models/timm/models/eva.py/0
{ "file_path": "pytorch-image-models/timm/models/eva.py", "repo_id": "pytorch-image-models", "token_count": 25759 }
212
""" Pytorch Inception-Resnet-V2 implementation Sourced from https://github.com/Cadene/tensorflow-model-zoo.torch (MIT License) which is based upon Google's Tensorflow implementation and pretrained weights (Apache 2.0 License) """ from functools import partial import torch import torch.nn as nn import torch.nn.functional as F from timm.data import IMAGENET_INCEPTION_MEAN, IMAGENET_INCEPTION_STD from timm.layers import create_classifier, ConvNormAct from ._builder import build_model_with_cfg from ._manipulate import flatten_modules from ._registry import register_model, generate_default_cfgs, register_model_deprecations __all__ = ['InceptionResnetV2'] class Mixed_5b(nn.Module): def __init__(self, conv_block=None): super(Mixed_5b, self).__init__() conv_block = conv_block or ConvNormAct self.branch0 = conv_block(192, 96, kernel_size=1, stride=1) self.branch1 = nn.Sequential( conv_block(192, 48, kernel_size=1, stride=1), conv_block(48, 64, kernel_size=5, stride=1, padding=2) ) self.branch2 = nn.Sequential( conv_block(192, 64, kernel_size=1, stride=1), conv_block(64, 96, kernel_size=3, stride=1, padding=1), conv_block(96, 96, kernel_size=3, stride=1, padding=1) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), conv_block(192, 64, kernel_size=1, stride=1) ) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) x3 = self.branch3(x) out = torch.cat((x0, x1, x2, x3), 1) return out class Block35(nn.Module): def __init__(self, scale=1.0, conv_block=None): super(Block35, self).__init__() self.scale = scale conv_block = conv_block or ConvNormAct self.branch0 = conv_block(320, 32, kernel_size=1, stride=1) self.branch1 = nn.Sequential( conv_block(320, 32, kernel_size=1, stride=1), conv_block(32, 32, kernel_size=3, stride=1, padding=1) ) self.branch2 = nn.Sequential( conv_block(320, 32, kernel_size=1, stride=1), conv_block(32, 48, kernel_size=3, stride=1, padding=1), conv_block(48, 64, kernel_size=3, stride=1, padding=1) ) self.conv2d = nn.Conv2d(128, 320, kernel_size=1, stride=1) self.act = nn.ReLU() def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) out = torch.cat((x0, x1, x2), 1) out = self.conv2d(out) out = out * self.scale + x out = self.act(out) return out class Mixed_6a(nn.Module): def __init__(self, conv_block=None): super(Mixed_6a, self).__init__() conv_block = conv_block or ConvNormAct self.branch0 = conv_block(320, 384, kernel_size=3, stride=2) self.branch1 = nn.Sequential( conv_block(320, 256, kernel_size=1, stride=1), conv_block(256, 256, kernel_size=3, stride=1, padding=1), conv_block(256, 384, kernel_size=3, stride=2) ) self.branch2 = nn.MaxPool2d(3, stride=2) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) out = torch.cat((x0, x1, x2), 1) return out class Block17(nn.Module): def __init__(self, scale=1.0, conv_block=None): super(Block17, self).__init__() self.scale = scale conv_block = conv_block or ConvNormAct self.branch0 = conv_block(1088, 192, kernel_size=1, stride=1) self.branch1 = nn.Sequential( conv_block(1088, 128, kernel_size=1, stride=1), conv_block(128, 160, kernel_size=(1, 7), stride=1, padding=(0, 3)), conv_block(160, 192, kernel_size=(7, 1), stride=1, padding=(3, 0)) ) self.conv2d = nn.Conv2d(384, 1088, kernel_size=1, stride=1) self.act = nn.ReLU() def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) out = torch.cat((x0, x1), 1) out = self.conv2d(out) out = out * self.scale + x out = self.act(out) return out class Mixed_7a(nn.Module): def __init__(self, conv_block=None): super(Mixed_7a, self).__init__() conv_block = conv_block or ConvNormAct self.branch0 = nn.Sequential( conv_block(1088, 256, kernel_size=1, stride=1), conv_block(256, 384, kernel_size=3, stride=2) ) self.branch1 = nn.Sequential( conv_block(1088, 256, kernel_size=1, stride=1), conv_block(256, 288, kernel_size=3, stride=2) ) self.branch2 = nn.Sequential( conv_block(1088, 256, kernel_size=1, stride=1), conv_block(256, 288, kernel_size=3, stride=1, padding=1), conv_block(288, 320, kernel_size=3, stride=2) ) self.branch3 = nn.MaxPool2d(3, stride=2) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) x3 = self.branch3(x) out = torch.cat((x0, x1, x2, x3), 1) return out class Block8(nn.Module): def __init__(self, scale=1.0, no_relu=False, conv_block=None): super(Block8, self).__init__() self.scale = scale conv_block = conv_block or ConvNormAct self.branch0 = conv_block(2080, 192, kernel_size=1, stride=1) self.branch1 = nn.Sequential( conv_block(2080, 192, kernel_size=1, stride=1), conv_block(192, 224, kernel_size=(1, 3), stride=1, padding=(0, 1)), conv_block(224, 256, kernel_size=(3, 1), stride=1, padding=(1, 0)) ) self.conv2d = nn.Conv2d(448, 2080, kernel_size=1, stride=1) self.relu = None if no_relu else nn.ReLU() def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) out = torch.cat((x0, x1), 1) out = self.conv2d(out) out = out * self.scale + x if self.relu is not None: out = self.relu(out) return out class InceptionResnetV2(nn.Module): def __init__( self, num_classes=1000, in_chans=3, drop_rate=0., output_stride=32, global_pool='avg', norm_layer='batchnorm2d', norm_eps=1e-3, act_layer='relu', ): super(InceptionResnetV2, self).__init__() self.num_classes = num_classes self.num_features = self.head_hidden_size = 1536 assert output_stride == 32 conv_block = partial( ConvNormAct, padding=0, norm_layer=norm_layer, act_layer=act_layer, norm_kwargs=dict(eps=norm_eps), act_kwargs=dict(inplace=True), ) self.conv2d_1a = conv_block(in_chans, 32, kernel_size=3, stride=2) self.conv2d_2a = conv_block(32, 32, kernel_size=3, stride=1) self.conv2d_2b = conv_block(32, 64, kernel_size=3, stride=1, padding=1) self.feature_info = [dict(num_chs=64, reduction=2, module='conv2d_2b')] self.maxpool_3a = nn.MaxPool2d(3, stride=2) self.conv2d_3b = conv_block(64, 80, kernel_size=1, stride=1) self.conv2d_4a = conv_block(80, 192, kernel_size=3, stride=1) self.feature_info += [dict(num_chs=192, reduction=4, module='conv2d_4a')] self.maxpool_5a = nn.MaxPool2d(3, stride=2) self.mixed_5b = Mixed_5b(conv_block=conv_block) self.repeat = nn.Sequential(*[Block35(scale=0.17, conv_block=conv_block) for _ in range(10)]) self.feature_info += [dict(num_chs=320, reduction=8, module='repeat')] self.mixed_6a = Mixed_6a(conv_block=conv_block) self.repeat_1 = nn.Sequential(*[Block17(scale=0.10, conv_block=conv_block) for _ in range(20)]) self.feature_info += [dict(num_chs=1088, reduction=16, module='repeat_1')] self.mixed_7a = Mixed_7a(conv_block=conv_block) self.repeat_2 = nn.Sequential(*[Block8(scale=0.20, conv_block=conv_block) for _ in range(9)]) self.block8 = Block8(no_relu=True, conv_block=conv_block) self.conv2d_7b = conv_block(2080, self.num_features, kernel_size=1, stride=1) self.feature_info += [dict(num_chs=self.num_features, reduction=32, module='conv2d_7b')] self.global_pool, self.head_drop, self.classif = create_classifier( self.num_features, self.num_classes, pool_type=global_pool, drop_rate=drop_rate) @torch.jit.ignore def group_matcher(self, coarse=False): module_map = {k: i for i, (k, _) in enumerate(flatten_modules(self.named_children(), prefix=()))} module_map.pop(('classif',)) def _matcher(name): if any([name.startswith(n) for n in ('conv2d_1', 'conv2d_2')]): return 0 elif any([name.startswith(n) for n in ('conv2d_3', 'conv2d_4')]): return 1 elif any([name.startswith(n) for n in ('block8', 'conv2d_7')]): return len(module_map) + 1 else: for k in module_map.keys(): if k == tuple(name.split('.')[:len(k)]): return module_map[k] return float('inf') return _matcher @torch.jit.ignore def set_grad_checkpointing(self, enable=True): assert not enable, "checkpointing not supported" @torch.jit.ignore def get_classifier(self) -> nn.Module: return self.classif def reset_classifier(self, num_classes: int, global_pool: str = 'avg'): self.num_classes = num_classes self.global_pool, self.classif = create_classifier(self.num_features, self.num_classes, pool_type=global_pool) def forward_features(self, x): x = self.conv2d_1a(x) x = self.conv2d_2a(x) x = self.conv2d_2b(x) x = self.maxpool_3a(x) x = self.conv2d_3b(x) x = self.conv2d_4a(x) x = self.maxpool_5a(x) x = self.mixed_5b(x) x = self.repeat(x) x = self.mixed_6a(x) x = self.repeat_1(x) x = self.mixed_7a(x) x = self.repeat_2(x) x = self.block8(x) x = self.conv2d_7b(x) return x def forward_head(self, x, pre_logits: bool = False): x = self.global_pool(x) x = self.head_drop(x) return x if pre_logits else self.classif(x) def forward(self, x): x = self.forward_features(x) x = self.forward_head(x) return x def _create_inception_resnet_v2(variant, pretrained=False, **kwargs): return build_model_with_cfg(InceptionResnetV2, variant, pretrained, **kwargs) default_cfgs = generate_default_cfgs({ # ported from http://download.tensorflow.org/models/inception_resnet_v2_2016_08_30.tar.gz 'inception_resnet_v2.tf_in1k': { 'hf_hub_id': 'timm/', 'num_classes': 1000, 'input_size': (3, 299, 299), 'pool_size': (8, 8), 'crop_pct': 0.8975, 'interpolation': 'bicubic', 'mean': IMAGENET_INCEPTION_MEAN, 'std': IMAGENET_INCEPTION_STD, 'first_conv': 'conv2d_1a.conv', 'classifier': 'classif', }, # As per https://arxiv.org/abs/1705.07204 and # ported from http://download.tensorflow.org/models/ens_adv_inception_resnet_v2_2017_08_18.tar.gz 'inception_resnet_v2.tf_ens_adv_in1k': { 'hf_hub_id': 'timm/', 'num_classes': 1000, 'input_size': (3, 299, 299), 'pool_size': (8, 8), 'crop_pct': 0.8975, 'interpolation': 'bicubic', 'mean': IMAGENET_INCEPTION_MEAN, 'std': IMAGENET_INCEPTION_STD, 'first_conv': 'conv2d_1a.conv', 'classifier': 'classif', } }) @register_model def inception_resnet_v2(pretrained=False, **kwargs) -> InceptionResnetV2: return _create_inception_resnet_v2('inception_resnet_v2', pretrained=pretrained, **kwargs) register_model_deprecations(__name__, { 'ens_adv_inception_resnet_v2': 'inception_resnet_v2.tf_ens_adv_in1k', })
pytorch-image-models/timm/models/inception_resnet_v2.py/0
{ "file_path": "pytorch-image-models/timm/models/inception_resnet_v2.py", "repo_id": "pytorch-image-models", "token_count": 6034 }
213
""" pnasnet5large implementation grabbed from Cadene's pretrained models Additional credit to https://github.com/creafz https://github.com/Cadene/pretrained-models.pytorch/blob/master/pretrainedmodels/models/pnasnet.py """ from collections import OrderedDict from functools import partial import torch import torch.nn as nn import torch.nn.functional as F from timm.layers import ConvNormAct, create_conv2d, create_pool2d, create_classifier from ._builder import build_model_with_cfg from ._registry import register_model, generate_default_cfgs __all__ = ['PNASNet5Large'] class SeparableConv2d(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride, padding=''): super(SeparableConv2d, self).__init__() self.depthwise_conv2d = create_conv2d( in_channels, in_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=in_channels) self.pointwise_conv2d = create_conv2d( in_channels, out_channels, kernel_size=1, padding=padding) def forward(self, x): x = self.depthwise_conv2d(x) x = self.pointwise_conv2d(x) return x class BranchSeparables(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, stem_cell=False, padding=''): super(BranchSeparables, self).__init__() middle_channels = out_channels if stem_cell else in_channels self.act_1 = nn.ReLU() self.separable_1 = SeparableConv2d( in_channels, middle_channels, kernel_size, stride=stride, padding=padding) self.bn_sep_1 = nn.BatchNorm2d(middle_channels, eps=0.001) self.act_2 = nn.ReLU() self.separable_2 = SeparableConv2d( middle_channels, out_channels, kernel_size, stride=1, padding=padding) self.bn_sep_2 = nn.BatchNorm2d(out_channels, eps=0.001) def forward(self, x): x = self.act_1(x) x = self.separable_1(x) x = self.bn_sep_1(x) x = self.act_2(x) x = self.separable_2(x) x = self.bn_sep_2(x) return x class ActConvBn(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=''): super(ActConvBn, self).__init__() self.act = nn.ReLU() self.conv = create_conv2d( in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding) self.bn = nn.BatchNorm2d(out_channels, eps=0.001) def forward(self, x): x = self.act(x) x = self.conv(x) x = self.bn(x) return x class FactorizedReduction(nn.Module): def __init__(self, in_channels, out_channels, padding=''): super(FactorizedReduction, self).__init__() self.act = nn.ReLU() self.path_1 = nn.Sequential(OrderedDict([ ('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False)), ('conv', create_conv2d(in_channels, out_channels // 2, kernel_size=1, padding=padding)), ])) self.path_2 = nn.Sequential(OrderedDict([ ('pad', nn.ZeroPad2d((-1, 1, -1, 1))), # shift ('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False)), ('conv', create_conv2d(in_channels, out_channels // 2, kernel_size=1, padding=padding)), ])) self.final_path_bn = nn.BatchNorm2d(out_channels, eps=0.001) def forward(self, x): x = self.act(x) x_path1 = self.path_1(x) x_path2 = self.path_2(x) out = self.final_path_bn(torch.cat([x_path1, x_path2], 1)) return out class CellBase(nn.Module): def cell_forward(self, x_left, x_right): x_comb_iter_0_left = self.comb_iter_0_left(x_left) x_comb_iter_0_right = self.comb_iter_0_right(x_left) x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right x_comb_iter_1_left = self.comb_iter_1_left(x_right) x_comb_iter_1_right = self.comb_iter_1_right(x_right) x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right x_comb_iter_2_left = self.comb_iter_2_left(x_right) x_comb_iter_2_right = self.comb_iter_2_right(x_right) x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right x_comb_iter_3_left = self.comb_iter_3_left(x_comb_iter_2) x_comb_iter_3_right = self.comb_iter_3_right(x_right) x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right x_comb_iter_4_left = self.comb_iter_4_left(x_left) if self.comb_iter_4_right is not None: x_comb_iter_4_right = self.comb_iter_4_right(x_right) else: x_comb_iter_4_right = x_right x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right x_out = torch.cat([x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1) return x_out class CellStem0(CellBase): def __init__(self, in_chs_left, out_chs_left, in_chs_right, out_chs_right, pad_type=''): super(CellStem0, self).__init__() self.conv_1x1 = ActConvBn(in_chs_right, out_chs_right, kernel_size=1, padding=pad_type) self.comb_iter_0_left = BranchSeparables( in_chs_left, out_chs_left, kernel_size=5, stride=2, stem_cell=True, padding=pad_type) self.comb_iter_0_right = nn.Sequential(OrderedDict([ ('max_pool', create_pool2d('max', 3, stride=2, padding=pad_type)), ('conv', create_conv2d(in_chs_left, out_chs_left, kernel_size=1, padding=pad_type)), ('bn', nn.BatchNorm2d(out_chs_left, eps=0.001)), ])) self.comb_iter_1_left = BranchSeparables( out_chs_right, out_chs_right, kernel_size=7, stride=2, padding=pad_type) self.comb_iter_1_right = create_pool2d('max', 3, stride=2, padding=pad_type) self.comb_iter_2_left = BranchSeparables( out_chs_right, out_chs_right, kernel_size=5, stride=2, padding=pad_type) self.comb_iter_2_right = BranchSeparables( out_chs_right, out_chs_right, kernel_size=3, stride=2, padding=pad_type) self.comb_iter_3_left = BranchSeparables( out_chs_right, out_chs_right, kernel_size=3, padding=pad_type) self.comb_iter_3_right = create_pool2d('max', 3, stride=2, padding=pad_type) self.comb_iter_4_left = BranchSeparables( in_chs_right, out_chs_right, kernel_size=3, stride=2, stem_cell=True, padding=pad_type) self.comb_iter_4_right = ActConvBn( out_chs_right, out_chs_right, kernel_size=1, stride=2, padding=pad_type) def forward(self, x_left): x_right = self.conv_1x1(x_left) x_out = self.cell_forward(x_left, x_right) return x_out class Cell(CellBase): def __init__( self, in_chs_left, out_chs_left, in_chs_right, out_chs_right, pad_type='', is_reduction=False, match_prev_layer_dims=False, ): super(Cell, self).__init__() # If `is_reduction` is set to `True` stride 2 is used for # convolution and pooling layers to reduce the spatial size of # the output of a cell approximately by a factor of 2. stride = 2 if is_reduction else 1 # If `match_prev_layer_dimensions` is set to `True` # `FactorizedReduction` is used to reduce the spatial size # of the left input of a cell approximately by a factor of 2. self.match_prev_layer_dimensions = match_prev_layer_dims if match_prev_layer_dims: self.conv_prev_1x1 = FactorizedReduction(in_chs_left, out_chs_left, padding=pad_type) else: self.conv_prev_1x1 = ActConvBn(in_chs_left, out_chs_left, kernel_size=1, padding=pad_type) self.conv_1x1 = ActConvBn(in_chs_right, out_chs_right, kernel_size=1, padding=pad_type) self.comb_iter_0_left = BranchSeparables( out_chs_left, out_chs_left, kernel_size=5, stride=stride, padding=pad_type) self.comb_iter_0_right = create_pool2d('max', 3, stride=stride, padding=pad_type) self.comb_iter_1_left = BranchSeparables( out_chs_right, out_chs_right, kernel_size=7, stride=stride, padding=pad_type) self.comb_iter_1_right = create_pool2d('max', 3, stride=stride, padding=pad_type) self.comb_iter_2_left = BranchSeparables( out_chs_right, out_chs_right, kernel_size=5, stride=stride, padding=pad_type) self.comb_iter_2_right = BranchSeparables( out_chs_right, out_chs_right, kernel_size=3, stride=stride, padding=pad_type) self.comb_iter_3_left = BranchSeparables(out_chs_right, out_chs_right, kernel_size=3) self.comb_iter_3_right = create_pool2d('max', 3, stride=stride, padding=pad_type) self.comb_iter_4_left = BranchSeparables( out_chs_left, out_chs_left, kernel_size=3, stride=stride, padding=pad_type) if is_reduction: self.comb_iter_4_right = ActConvBn( out_chs_right, out_chs_right, kernel_size=1, stride=stride, padding=pad_type) else: self.comb_iter_4_right = None def forward(self, x_left, x_right): x_left = self.conv_prev_1x1(x_left) x_right = self.conv_1x1(x_right) x_out = self.cell_forward(x_left, x_right) return x_out class PNASNet5Large(nn.Module): def __init__( self, num_classes=1000, in_chans=3, output_stride=32, drop_rate=0., global_pool='avg', pad_type='', ): super(PNASNet5Large, self).__init__() self.num_classes = num_classes self.num_features = self.head_hidden_size = 4320 assert output_stride == 32 self.conv_0 = ConvNormAct( in_chans, 96, kernel_size=3, stride=2, padding=0, norm_layer=partial(nn.BatchNorm2d, eps=0.001, momentum=0.1), apply_act=False) self.cell_stem_0 = CellStem0( in_chs_left=96, out_chs_left=54, in_chs_right=96, out_chs_right=54, pad_type=pad_type) self.cell_stem_1 = Cell( in_chs_left=96, out_chs_left=108, in_chs_right=270, out_chs_right=108, pad_type=pad_type, match_prev_layer_dims=True, is_reduction=True) self.cell_0 = Cell( in_chs_left=270, out_chs_left=216, in_chs_right=540, out_chs_right=216, pad_type=pad_type, match_prev_layer_dims=True) self.cell_1 = Cell( in_chs_left=540, out_chs_left=216, in_chs_right=1080, out_chs_right=216, pad_type=pad_type) self.cell_2 = Cell( in_chs_left=1080, out_chs_left=216, in_chs_right=1080, out_chs_right=216, pad_type=pad_type) self.cell_3 = Cell( in_chs_left=1080, out_chs_left=216, in_chs_right=1080, out_chs_right=216, pad_type=pad_type) self.cell_4 = Cell( in_chs_left=1080, out_chs_left=432, in_chs_right=1080, out_chs_right=432, pad_type=pad_type, is_reduction=True) self.cell_5 = Cell( in_chs_left=1080, out_chs_left=432, in_chs_right=2160, out_chs_right=432, pad_type=pad_type, match_prev_layer_dims=True) self.cell_6 = Cell( in_chs_left=2160, out_chs_left=432, in_chs_right=2160, out_chs_right=432, pad_type=pad_type) self.cell_7 = Cell( in_chs_left=2160, out_chs_left=432, in_chs_right=2160, out_chs_right=432, pad_type=pad_type) self.cell_8 = Cell( in_chs_left=2160, out_chs_left=864, in_chs_right=2160, out_chs_right=864, pad_type=pad_type, is_reduction=True) self.cell_9 = Cell( in_chs_left=2160, out_chs_left=864, in_chs_right=4320, out_chs_right=864, pad_type=pad_type, match_prev_layer_dims=True) self.cell_10 = Cell( in_chs_left=4320, out_chs_left=864, in_chs_right=4320, out_chs_right=864, pad_type=pad_type) self.cell_11 = Cell( in_chs_left=4320, out_chs_left=864, in_chs_right=4320, out_chs_right=864, pad_type=pad_type) self.act = nn.ReLU() self.feature_info = [ dict(num_chs=96, reduction=2, module='conv_0'), dict(num_chs=270, reduction=4, module='cell_stem_1.conv_1x1.act'), dict(num_chs=1080, reduction=8, module='cell_4.conv_1x1.act'), dict(num_chs=2160, reduction=16, module='cell_8.conv_1x1.act'), dict(num_chs=4320, reduction=32, module='act'), ] self.global_pool, self.head_drop, self.last_linear = create_classifier( self.num_features, self.num_classes, pool_type=global_pool, drop_rate=drop_rate) @torch.jit.ignore def group_matcher(self, coarse=False): return dict(stem=r'^conv_0|cell_stem_[01]', blocks=r'^cell_(\d+)') @torch.jit.ignore def set_grad_checkpointing(self, enable=True): assert not enable, 'gradient checkpointing not supported' @torch.jit.ignore def get_classifier(self) -> nn.Module: return self.last_linear def reset_classifier(self, num_classes: int, global_pool: str = 'avg'): self.num_classes = num_classes self.global_pool, self.last_linear = create_classifier( self.num_features, self.num_classes, pool_type=global_pool) def forward_features(self, x): x_conv_0 = self.conv_0(x) x_stem_0 = self.cell_stem_0(x_conv_0) x_stem_1 = self.cell_stem_1(x_conv_0, x_stem_0) x_cell_0 = self.cell_0(x_stem_0, x_stem_1) x_cell_1 = self.cell_1(x_stem_1, x_cell_0) x_cell_2 = self.cell_2(x_cell_0, x_cell_1) x_cell_3 = self.cell_3(x_cell_1, x_cell_2) x_cell_4 = self.cell_4(x_cell_2, x_cell_3) x_cell_5 = self.cell_5(x_cell_3, x_cell_4) x_cell_6 = self.cell_6(x_cell_4, x_cell_5) x_cell_7 = self.cell_7(x_cell_5, x_cell_6) x_cell_8 = self.cell_8(x_cell_6, x_cell_7) x_cell_9 = self.cell_9(x_cell_7, x_cell_8) x_cell_10 = self.cell_10(x_cell_8, x_cell_9) x_cell_11 = self.cell_11(x_cell_9, x_cell_10) x = self.act(x_cell_11) return x def forward_head(self, x, pre_logits: bool = False): x = self.global_pool(x) x = self.head_drop(x) return x if pre_logits else self.last_linear(x) def forward(self, x): x = self.forward_features(x) x = self.forward_head(x) return x def _create_pnasnet(variant, pretrained=False, **kwargs): return build_model_with_cfg( PNASNet5Large, variant, pretrained, feature_cfg=dict(feature_cls='hook', no_rewrite=True), # not possible to re-write this model **kwargs, ) default_cfgs = generate_default_cfgs({ 'pnasnet5large.tf_in1k': { 'hf_hub_id': 'timm/', 'input_size': (3, 331, 331), 'pool_size': (11, 11), 'crop_pct': 0.911, 'interpolation': 'bicubic', 'mean': (0.5, 0.5, 0.5), 'std': (0.5, 0.5, 0.5), 'num_classes': 1000, 'first_conv': 'conv_0.conv', 'classifier': 'last_linear', }, }) @register_model def pnasnet5large(pretrained=False, **kwargs) -> PNASNet5Large: r"""PNASNet-5 model architecture from the `"Progressive Neural Architecture Search" <https://arxiv.org/abs/1712.00559>`_ paper. """ model_kwargs = dict(pad_type='same', **kwargs) return _create_pnasnet('pnasnet5large', pretrained, **model_kwargs)
pytorch-image-models/timm/models/pnasnet.py/0
{ "file_path": "pytorch-image-models/timm/models/pnasnet.py", "repo_id": "pytorch-image-models", "token_count": 7672 }
214
""" Swin Transformer A PyTorch impl of : `Swin Transformer: Hierarchical Vision Transformer using Shifted Windows` - https://arxiv.org/pdf/2103.14030 Code/weights from https://github.com/microsoft/Swin-Transformer, original copyright/license info below S3 (AutoFormerV2, https://arxiv.org/abs/2111.14725) Swin weights from - https://github.com/microsoft/Cream/tree/main/AutoFormerV2 Modifications and additions for timm hacked together by / Copyright 2021, Ross Wightman """ # -------------------------------------------------------- # Swin Transformer # Copyright (c) 2021 Microsoft # Licensed under The MIT License [see LICENSE for details] # Written by Ze Liu # -------------------------------------------------------- import logging import math from typing import Callable, List, Optional, Tuple, Union import torch import torch.nn as nn from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.layers import PatchEmbed, Mlp, DropPath, ClassifierHead, to_2tuple, to_ntuple, trunc_normal_, \ _assert, use_fused_attn, resize_rel_pos_bias_table, resample_patch_embed, ndgrid from ._builder import build_model_with_cfg from ._features import feature_take_indices from ._features_fx import register_notrace_function from ._manipulate import checkpoint_seq, named_apply from ._registry import generate_default_cfgs, register_model, register_model_deprecations from .vision_transformer import get_init_weights_vit __all__ = ['SwinTransformer'] # model_registry will add each entrypoint fn to this _logger = logging.getLogger(__name__) _int_or_tuple_2_t = Union[int, Tuple[int, int]] def window_partition( x: torch.Tensor, window_size: Tuple[int, int], ) -> torch.Tensor: """ Partition into non-overlapping windows with padding if needed. Args: x (tensor): input tokens with [B, H, W, C]. window_size (int): window size. Returns: windows: windows after partition with [B * num_windows, window_size, window_size, C]. (Hp, Wp): padded height and width before partition """ B, H, W, C = x.shape x = x.view(B, H // window_size[0], window_size[0], W // window_size[1], window_size[1], C) windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size[0], window_size[1], C) return windows @register_notrace_function # reason: int argument is a Proxy def window_reverse(windows, window_size: Tuple[int, int], H: int, W: int): """ Args: windows: (num_windows*B, window_size, window_size, C) window_size (int): Window size H (int): Height of image W (int): Width of image Returns: x: (B, H, W, C) """ C = windows.shape[-1] x = windows.view(-1, H // window_size[0], W // window_size[1], window_size[0], window_size[1], C) x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, H, W, C) return x def get_relative_position_index(win_h: int, win_w: int): # get pair-wise relative position index for each token inside the window coords = torch.stack(ndgrid(torch.arange(win_h), torch.arange(win_w))) # 2, Wh, Ww coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2 relative_coords[:, :, 0] += win_h - 1 # shift to start from 0 relative_coords[:, :, 1] += win_w - 1 relative_coords[:, :, 0] *= 2 * win_w - 1 return relative_coords.sum(-1) # Wh*Ww, Wh*Ww class WindowAttention(nn.Module): """ Window based multi-head self attention (W-MSA) module with relative position bias. It supports shifted and non-shifted windows. """ fused_attn: torch.jit.Final[bool] def __init__( self, dim: int, num_heads: int, head_dim: Optional[int] = None, window_size: _int_or_tuple_2_t = 7, qkv_bias: bool = True, attn_drop: float = 0., proj_drop: float = 0., ): """ Args: dim: Number of input channels. num_heads: Number of attention heads. head_dim: Number of channels per head (dim // num_heads if not set) window_size: The height and width of the window. qkv_bias: If True, add a learnable bias to query, key, value. attn_drop: Dropout ratio of attention weight. proj_drop: Dropout ratio of output. """ super().__init__() self.dim = dim self.window_size = to_2tuple(window_size) # Wh, Ww win_h, win_w = self.window_size self.window_area = win_h * win_w self.num_heads = num_heads head_dim = head_dim or dim // num_heads attn_dim = head_dim * num_heads self.scale = head_dim ** -0.5 self.fused_attn = use_fused_attn(experimental=True) # NOTE not tested for prime-time yet # define a parameter table of relative position bias, shape: 2*Wh-1 * 2*Ww-1, nH self.relative_position_bias_table = nn.Parameter(torch.zeros((2 * win_h - 1) * (2 * win_w - 1), num_heads)) # get pair-wise relative position index for each token inside the window self.register_buffer("relative_position_index", get_relative_position_index(win_h, win_w), persistent=False) self.qkv = nn.Linear(dim, attn_dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(attn_dim, dim) self.proj_drop = nn.Dropout(proj_drop) trunc_normal_(self.relative_position_bias_table, std=.02) self.softmax = nn.Softmax(dim=-1) def set_window_size(self, window_size: Tuple[int, int]) -> None: """Update window size & interpolate position embeddings Args: window_size (int): New window size """ window_size = to_2tuple(window_size) if window_size == self.window_size: return self.window_size = window_size win_h, win_w = self.window_size self.window_area = win_h * win_w with torch.no_grad(): new_bias_shape = (2 * win_h - 1) * (2 * win_w - 1), self.num_heads self.relative_position_bias_table = nn.Parameter( resize_rel_pos_bias_table( self.relative_position_bias_table, new_window_size=self.window_size, new_bias_shape=new_bias_shape, )) self.register_buffer("relative_position_index", get_relative_position_index(win_h, win_w), persistent=False) def _get_rel_pos_bias(self) -> torch.Tensor: relative_position_bias = self.relative_position_bias_table[ self.relative_position_index.view(-1)].view(self.window_area, self.window_area, -1) # Wh*Ww,Wh*Ww,nH relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww return relative_position_bias.unsqueeze(0) def forward(self, x, mask: Optional[torch.Tensor] = None): """ Args: x: input features with shape of (num_windows*B, N, C) mask: (0/-inf) mask with shape of (num_windows, Wh*Ww, Wh*Ww) or None """ B_, N, C = x.shape qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) q, k, v = qkv.unbind(0) if self.fused_attn: attn_mask = self._get_rel_pos_bias() if mask is not None: num_win = mask.shape[0] mask = mask.view(1, num_win, 1, N, N).expand(B_ // num_win, -1, self.num_heads, -1, -1) attn_mask = attn_mask + mask.reshape(-1, self.num_heads, N, N) x = torch.nn.functional.scaled_dot_product_attention( q, k, v, attn_mask=attn_mask, dropout_p=self.attn_drop.p if self.training else 0., ) else: q = q * self.scale attn = q @ k.transpose(-2, -1) attn = attn + self._get_rel_pos_bias() if mask is not None: num_win = mask.shape[0] attn = attn.view(-1, num_win, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) attn = attn.view(-1, self.num_heads, N, N) attn = self.softmax(attn) attn = self.attn_drop(attn) x = attn @ v x = x.transpose(1, 2).reshape(B_, N, -1) x = self.proj(x) x = self.proj_drop(x) return x class SwinTransformerBlock(nn.Module): """ Swin Transformer Block. """ def __init__( self, dim: int, input_resolution: _int_or_tuple_2_t, num_heads: int = 4, head_dim: Optional[int] = None, window_size: _int_or_tuple_2_t = 7, shift_size: int = 0, always_partition: bool = False, dynamic_mask: bool = False, mlp_ratio: float = 4., qkv_bias: bool = True, proj_drop: float = 0., attn_drop: float = 0., drop_path: float = 0., act_layer: Callable = nn.GELU, norm_layer: Callable = nn.LayerNorm, ): """ Args: dim: Number of input channels. input_resolution: Input resolution. window_size: Window size. num_heads: Number of attention heads. head_dim: Enforce the number of channels per head shift_size: Shift size for SW-MSA. always_partition: Always partition into full windows and shift mlp_ratio: Ratio of mlp hidden dim to embedding dim. qkv_bias: If True, add a learnable bias to query, key, value. proj_drop: Dropout rate. attn_drop: Attention dropout rate. drop_path: Stochastic depth rate. act_layer: Activation layer. norm_layer: Normalization layer. """ super().__init__() self.dim = dim self.input_resolution = input_resolution self.target_shift_size = to_2tuple(shift_size) # store for later resize self.always_partition = always_partition self.dynamic_mask = dynamic_mask self.window_size, self.shift_size = self._calc_window_shift(window_size, shift_size) self.window_area = self.window_size[0] * self.window_size[1] self.mlp_ratio = mlp_ratio self.norm1 = norm_layer(dim) self.attn = WindowAttention( dim, num_heads=num_heads, head_dim=head_dim, window_size=self.window_size, qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=proj_drop, ) self.drop_path1 = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.norm2 = norm_layer(dim) self.mlp = Mlp( in_features=dim, hidden_features=int(dim * mlp_ratio), act_layer=act_layer, drop=proj_drop, ) self.drop_path2 = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.register_buffer( "attn_mask", None if self.dynamic_mask else self.get_attn_mask(), persistent=False, ) def get_attn_mask(self, x: Optional[torch.Tensor] = None) -> Optional[torch.Tensor]: if any(self.shift_size): # calculate attention mask for SW-MSA if x is not None: H, W = x.shape[1], x.shape[2] device = x.device dtype = x.dtype else: H, W = self.input_resolution device = None dtype = None H = math.ceil(H / self.window_size[0]) * self.window_size[0] W = math.ceil(W / self.window_size[1]) * self.window_size[1] img_mask = torch.zeros((1, H, W, 1), dtype=dtype, device=device) # 1 H W 1 cnt = 0 for h in ( (0, -self.window_size[0]), (-self.window_size[0], -self.shift_size[0]), (-self.shift_size[0], None), ): for w in ( (0, -self.window_size[1]), (-self.window_size[1], -self.shift_size[1]), (-self.shift_size[1], None), ): img_mask[:, h[0]:h[1], w[0]:w[1], :] = cnt cnt += 1 mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1 mask_windows = mask_windows.view(-1, self.window_area) attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) else: attn_mask = None return attn_mask def _calc_window_shift( self, target_window_size: Union[int, Tuple[int, int]], target_shift_size: Optional[Union[int, Tuple[int, int]]] = None, ) -> Tuple[Tuple[int, int], Tuple[int, int]]: target_window_size = to_2tuple(target_window_size) if target_shift_size is None: # if passed value is None, recalculate from default window_size // 2 if it was previously non-zero target_shift_size = self.target_shift_size if any(target_shift_size): target_shift_size = (target_window_size[0] // 2, target_window_size[1] // 2) else: target_shift_size = to_2tuple(target_shift_size) if self.always_partition: return target_window_size, target_shift_size window_size = [r if r <= w else w for r, w in zip(self.input_resolution, target_window_size)] shift_size = [0 if r <= w else s for r, w, s in zip(self.input_resolution, window_size, target_shift_size)] return tuple(window_size), tuple(shift_size) def set_input_size( self, feat_size: Tuple[int, int], window_size: Tuple[int, int], always_partition: Optional[bool] = None, ): """ Args: feat_size: New input resolution window_size: New window size always_partition: Change always_partition attribute if not None """ self.input_resolution = feat_size if always_partition is not None: self.always_partition = always_partition self.window_size, self.shift_size = self._calc_window_shift(window_size) self.window_area = self.window_size[0] * self.window_size[1] self.attn.set_window_size(self.window_size) self.register_buffer( "attn_mask", None if self.dynamic_mask else self.get_attn_mask(), persistent=False, ) def _attn(self, x): B, H, W, C = x.shape # cyclic shift has_shift = any(self.shift_size) if has_shift: shifted_x = torch.roll(x, shifts=(-self.shift_size[0], -self.shift_size[1]), dims=(1, 2)) else: shifted_x = x # pad for resolution not divisible by window size pad_h = (self.window_size[0] - H % self.window_size[0]) % self.window_size[0] pad_w = (self.window_size[1] - W % self.window_size[1]) % self.window_size[1] shifted_x = torch.nn.functional.pad(shifted_x, (0, 0, 0, pad_w, 0, pad_h)) _, Hp, Wp, _ = shifted_x.shape # partition windows x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C x_windows = x_windows.view(-1, self.window_area, C) # nW*B, window_size*window_size, C # W-MSA/SW-MSA if getattr(self, 'dynamic_mask', False): attn_mask = self.get_attn_mask(shifted_x) else: attn_mask = self.attn_mask attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C # merge windows attn_windows = attn_windows.view(-1, self.window_size[0], self.window_size[1], C) shifted_x = window_reverse(attn_windows, self.window_size, Hp, Wp) # B H' W' C shifted_x = shifted_x[:, :H, :W, :].contiguous() # reverse cyclic shift if has_shift: x = torch.roll(shifted_x, shifts=self.shift_size, dims=(1, 2)) else: x = shifted_x return x def forward(self, x): B, H, W, C = x.shape x = x + self.drop_path1(self._attn(self.norm1(x))) x = x.reshape(B, -1, C) x = x + self.drop_path2(self.mlp(self.norm2(x))) x = x.reshape(B, H, W, C) return x class PatchMerging(nn.Module): """ Patch Merging Layer. """ def __init__( self, dim: int, out_dim: Optional[int] = None, norm_layer: Callable = nn.LayerNorm, ): """ Args: dim: Number of input channels. out_dim: Number of output channels (or 2 * dim if None) norm_layer: Normalization layer. """ super().__init__() self.dim = dim self.out_dim = out_dim or 2 * dim self.norm = norm_layer(4 * dim) self.reduction = nn.Linear(4 * dim, self.out_dim, bias=False) def forward(self, x): B, H, W, C = x.shape pad_values = (0, 0, 0, H % 2, 0, W % 2) x = nn.functional.pad(x, pad_values) _, H, W, _ = x.shape x = x.reshape(B, H // 2, 2, W // 2, 2, C).permute(0, 1, 3, 4, 2, 5).flatten(3) x = self.norm(x) x = self.reduction(x) return x class SwinTransformerStage(nn.Module): """ A basic Swin Transformer layer for one stage. """ def __init__( self, dim: int, out_dim: int, input_resolution: Tuple[int, int], depth: int, downsample: bool = True, num_heads: int = 4, head_dim: Optional[int] = None, window_size: _int_or_tuple_2_t = 7, always_partition: bool = False, dynamic_mask: bool = False, mlp_ratio: float = 4., qkv_bias: bool = True, proj_drop: float = 0., attn_drop: float = 0., drop_path: Union[List[float], float] = 0., norm_layer: Callable = nn.LayerNorm, ): """ Args: dim: Number of input channels. out_dim: Number of output channels. input_resolution: Input resolution. depth: Number of blocks. downsample: Downsample layer at the end of the layer. num_heads: Number of attention heads. head_dim: Channels per head (dim // num_heads if not set) window_size: Local window size. mlp_ratio: Ratio of mlp hidden dim to embedding dim. qkv_bias: If True, add a learnable bias to query, key, value. proj_drop: Projection dropout rate. attn_drop: Attention dropout rate. drop_path: Stochastic depth rate. norm_layer: Normalization layer. """ super().__init__() self.dim = dim self.input_resolution = input_resolution self.output_resolution = tuple(i // 2 for i in input_resolution) if downsample else input_resolution self.depth = depth self.grad_checkpointing = False window_size = to_2tuple(window_size) shift_size = tuple([w // 2 for w in window_size]) # patch merging layer if downsample: self.downsample = PatchMerging( dim=dim, out_dim=out_dim, norm_layer=norm_layer, ) else: assert dim == out_dim self.downsample = nn.Identity() # build blocks self.blocks = nn.Sequential(*[ SwinTransformerBlock( dim=out_dim, input_resolution=self.output_resolution, num_heads=num_heads, head_dim=head_dim, window_size=window_size, shift_size=0 if (i % 2 == 0) else shift_size, always_partition=always_partition, dynamic_mask=dynamic_mask, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, proj_drop=proj_drop, attn_drop=attn_drop, drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path, norm_layer=norm_layer, ) for i in range(depth)]) def set_input_size( self, feat_size: Tuple[int, int], window_size: int, always_partition: Optional[bool] = None, ): """ Updates the resolution, window size and so the pair-wise relative positions. Args: feat_size: New input (feature) resolution window_size: New window size always_partition: Always partition / shift the window """ self.input_resolution = feat_size if isinstance(self.downsample, nn.Identity): self.output_resolution = feat_size else: self.output_resolution = tuple(i // 2 for i in feat_size) for block in self.blocks: block.set_input_size( feat_size=self.output_resolution, window_size=window_size, always_partition=always_partition, ) def forward(self, x): x = self.downsample(x) if self.grad_checkpointing and not torch.jit.is_scripting(): x = checkpoint_seq(self.blocks, x) else: x = self.blocks(x) return x class SwinTransformer(nn.Module): """ Swin Transformer A PyTorch impl of : `Swin Transformer: Hierarchical Vision Transformer using Shifted Windows` - https://arxiv.org/pdf/2103.14030 """ def __init__( self, img_size: _int_or_tuple_2_t = 224, patch_size: int = 4, in_chans: int = 3, num_classes: int = 1000, global_pool: str = 'avg', embed_dim: int = 96, depths: Tuple[int, ...] = (2, 2, 6, 2), num_heads: Tuple[int, ...] = (3, 6, 12, 24), head_dim: Optional[int] = None, window_size: _int_or_tuple_2_t = 7, always_partition: bool = False, strict_img_size: bool = True, mlp_ratio: float = 4., qkv_bias: bool = True, drop_rate: float = 0., proj_drop_rate: float = 0., attn_drop_rate: float = 0., drop_path_rate: float = 0.1, embed_layer: Callable = PatchEmbed, norm_layer: Union[str, Callable] = nn.LayerNorm, weight_init: str = '', **kwargs, ): """ Args: img_size: Input image size. patch_size: Patch size. in_chans: Number of input image channels. num_classes: Number of classes for classification head. embed_dim: Patch embedding dimension. depths: Depth of each Swin Transformer layer. num_heads: Number of attention heads in different layers. head_dim: Dimension of self-attention heads. window_size: Window size. mlp_ratio: Ratio of mlp hidden dim to embedding dim. qkv_bias: If True, add a learnable bias to query, key, value. drop_rate: Dropout rate. attn_drop_rate (float): Attention dropout rate. drop_path_rate (float): Stochastic depth rate. embed_layer: Patch embedding layer. norm_layer (nn.Module): Normalization layer. """ super().__init__() assert global_pool in ('', 'avg') self.num_classes = num_classes self.global_pool = global_pool self.output_fmt = 'NHWC' self.num_layers = len(depths) self.embed_dim = embed_dim self.num_features = self.head_hidden_size = int(embed_dim * 2 ** (self.num_layers - 1)) self.feature_info = [] if not isinstance(embed_dim, (tuple, list)): embed_dim = [int(embed_dim * 2 ** i) for i in range(self.num_layers)] # split image into non-overlapping patches self.patch_embed = embed_layer( img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim[0], norm_layer=norm_layer, strict_img_size=strict_img_size, output_fmt='NHWC', ) patch_grid = self.patch_embed.grid_size # build layers head_dim = to_ntuple(self.num_layers)(head_dim) if not isinstance(window_size, (list, tuple)): window_size = to_ntuple(self.num_layers)(window_size) elif len(window_size) == 2: window_size = (window_size,) * self.num_layers assert len(window_size) == self.num_layers mlp_ratio = to_ntuple(self.num_layers)(mlp_ratio) dpr = [x.tolist() for x in torch.linspace(0, drop_path_rate, sum(depths)).split(depths)] layers = [] in_dim = embed_dim[0] scale = 1 for i in range(self.num_layers): out_dim = embed_dim[i] layers += [SwinTransformerStage( dim=in_dim, out_dim=out_dim, input_resolution=( patch_grid[0] // scale, patch_grid[1] // scale ), depth=depths[i], downsample=i > 0, num_heads=num_heads[i], head_dim=head_dim[i], window_size=window_size[i], always_partition=always_partition, dynamic_mask=not strict_img_size, mlp_ratio=mlp_ratio[i], qkv_bias=qkv_bias, proj_drop=proj_drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer, )] in_dim = out_dim if i > 0: scale *= 2 self.feature_info += [dict(num_chs=out_dim, reduction=patch_size * scale, module=f'layers.{i}')] self.layers = nn.Sequential(*layers) self.norm = norm_layer(self.num_features) self.head = ClassifierHead( self.num_features, num_classes, pool_type=global_pool, drop_rate=drop_rate, input_fmt=self.output_fmt, ) if weight_init != 'skip': self.init_weights(weight_init) @torch.jit.ignore def init_weights(self, mode=''): assert mode in ('jax', 'jax_nlhb', 'moco', '') head_bias = -math.log(self.num_classes) if 'nlhb' in mode else 0. named_apply(get_init_weights_vit(mode, head_bias=head_bias), self) @torch.jit.ignore def no_weight_decay(self): nwd = set() for n, _ in self.named_parameters(): if 'relative_position_bias_table' in n: nwd.add(n) return nwd def set_input_size( self, img_size: Optional[Tuple[int, int]] = None, patch_size: Optional[Tuple[int, int]] = None, window_size: Optional[Tuple[int, int]] = None, window_ratio: int = 8, always_partition: Optional[bool] = None, ) -> None: """ Updates the image resolution and window size. Args: img_size: New input resolution, if None current resolution is used patch_size (Optional[Tuple[int, int]): New patch size, if None use current patch size window_size: New window size, if None based on new_img_size // window_div window_ratio: divisor for calculating window size from grid size always_partition: always partition into windows and shift (even if window size < feat size) """ if img_size is not None or patch_size is not None: self.patch_embed.set_input_size(img_size=img_size, patch_size=patch_size) patch_grid = self.patch_embed.grid_size if window_size is None: window_size = tuple([pg // window_ratio for pg in patch_grid]) for index, stage in enumerate(self.layers): stage_scale = 2 ** max(index - 1, 0) stage.set_input_size( feat_size=(patch_grid[0] // stage_scale, patch_grid[1] // stage_scale), window_size=window_size, always_partition=always_partition, ) @torch.jit.ignore def group_matcher(self, coarse=False): return dict( stem=r'^patch_embed', # stem and embed blocks=r'^layers\.(\d+)' if coarse else [ (r'^layers\.(\d+).downsample', (0,)), (r'^layers\.(\d+)\.\w+\.(\d+)', None), (r'^norm', (99999,)), ] ) @torch.jit.ignore def set_grad_checkpointing(self, enable=True): for l in self.layers: l.grad_checkpointing = enable @torch.jit.ignore def get_classifier(self) -> nn.Module: return self.head.fc def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None): self.num_classes = num_classes self.head.reset(num_classes, pool_type=global_pool) def forward_intermediates( self, x: torch.Tensor, indices: Optional[Union[int, List[int]]] = None, norm: bool = False, stop_early: bool = False, output_fmt: str = 'NCHW', intermediates_only: bool = False, ) -> Union[List[torch.Tensor], Tuple[torch.Tensor, List[torch.Tensor]]]: """ Forward features that returns intermediates. Args: x: Input image tensor indices: Take last n blocks if int, all if None, select matching indices if sequence norm: Apply norm layer to compatible intermediates stop_early: Stop iterating over blocks when last desired intermediate hit output_fmt: Shape of intermediate feature outputs intermediates_only: Only return intermediate features Returns: """ assert output_fmt in ('NCHW',), 'Output shape must be NCHW.' intermediates = [] take_indices, max_index = feature_take_indices(len(self.layers), indices) # forward pass x = self.patch_embed(x) num_stages = len(self.layers) if torch.jit.is_scripting() or not stop_early: # can't slice blocks in torchscript stages = self.layers else: stages = self.layers[:max_index + 1] for i, stage in enumerate(stages): x = stage(x) if i in take_indices: if norm and i == num_stages - 1: x_inter = self.norm(x) # applying final norm last intermediate else: x_inter = x x_inter = x_inter.permute(0, 3, 1, 2).contiguous() intermediates.append(x_inter) if intermediates_only: return intermediates x = self.norm(x) return x, intermediates def prune_intermediate_layers( self, indices: Union[int, List[int]] = 1, prune_norm: bool = False, prune_head: bool = True, ): """ Prune layers not required for specified intermediates. """ take_indices, max_index = feature_take_indices(len(self.layers), indices) self.layers = self.layers[:max_index + 1] # truncate blocks if prune_norm: self.norm = nn.Identity() if prune_head: self.reset_classifier(0, '') return take_indices def forward_features(self, x): x = self.patch_embed(x) x = self.layers(x) x = self.norm(x) return x def forward_head(self, x, pre_logits: bool = False): return self.head(x, pre_logits=True) if pre_logits else self.head(x) def forward(self, x): x = self.forward_features(x) x = self.forward_head(x) return x def checkpoint_filter_fn(state_dict, model): """ convert patch embedding weight from manual patchify + linear proj to conv""" old_weights = True if 'head.fc.weight' in state_dict: old_weights = False import re out_dict = {} state_dict = state_dict.get('model', state_dict) state_dict = state_dict.get('state_dict', state_dict) for k, v in state_dict.items(): if any([n in k for n in ('relative_position_index', 'attn_mask')]): continue # skip buffers that should not be persistent if 'patch_embed.proj.weight' in k: _, _, H, W = model.patch_embed.proj.weight.shape if v.shape[-2] != H or v.shape[-1] != W: v = resample_patch_embed( v, (H, W), interpolation='bicubic', antialias=True, verbose=True, ) if k.endswith('relative_position_bias_table'): m = model.get_submodule(k[:-29]) if v.shape != m.relative_position_bias_table.shape or m.window_size[0] != m.window_size[1]: v = resize_rel_pos_bias_table( v, new_window_size=m.window_size, new_bias_shape=m.relative_position_bias_table.shape, ) if old_weights: k = re.sub(r'layers.(\d+).downsample', lambda x: f'layers.{int(x.group(1)) + 1}.downsample', k) k = k.replace('head.', 'head.fc.') out_dict[k] = v return out_dict def _create_swin_transformer(variant, pretrained=False, **kwargs): default_out_indices = tuple(i for i, _ in enumerate(kwargs.get('depths', (1, 1, 3, 1)))) out_indices = kwargs.pop('out_indices', default_out_indices) model = build_model_with_cfg( SwinTransformer, variant, pretrained, pretrained_filter_fn=checkpoint_filter_fn, feature_cfg=dict(flatten_sequential=True, out_indices=out_indices), **kwargs) return model def _cfg(url='', **kwargs): return { 'url': url, 'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': (7, 7), 'crop_pct': .9, 'interpolation': 'bicubic', 'fixed_input_size': True, 'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD, 'first_conv': 'patch_embed.proj', 'classifier': 'head.fc', 'license': 'mit', **kwargs } default_cfgs = generate_default_cfgs({ 'swin_small_patch4_window7_224.ms_in22k_ft_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.8/swin_small_patch4_window7_224_22kto1k_finetune.pth', ), 'swin_base_patch4_window7_224.ms_in22k_ft_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window7_224_22kto1k.pth',), 'swin_base_patch4_window12_384.ms_in22k_ft_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window12_384_22kto1k.pth', input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0), 'swin_large_patch4_window7_224.ms_in22k_ft_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window7_224_22kto1k.pth',), 'swin_large_patch4_window12_384.ms_in22k_ft_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22kto1k.pth', input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0), 'swin_tiny_patch4_window7_224.ms_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth',), 'swin_small_patch4_window7_224.ms_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_small_patch4_window7_224.pth',), 'swin_base_patch4_window7_224.ms_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window7_224.pth',), 'swin_base_patch4_window12_384.ms_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window12_384.pth', input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0), # tiny 22k pretrain is worse than 1k, so moved after (untagged priority is based on order) 'swin_tiny_patch4_window7_224.ms_in22k_ft_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.8/swin_tiny_patch4_window7_224_22kto1k_finetune.pth',), 'swin_tiny_patch4_window7_224.ms_in22k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.8/swin_tiny_patch4_window7_224_22k.pth', num_classes=21841), 'swin_small_patch4_window7_224.ms_in22k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.8/swin_small_patch4_window7_224_22k.pth', num_classes=21841), 'swin_base_patch4_window7_224.ms_in22k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window7_224_22k.pth', num_classes=21841), 'swin_base_patch4_window12_384.ms_in22k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window12_384_22k.pth', input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0, num_classes=21841), 'swin_large_patch4_window7_224.ms_in22k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window7_224_22k.pth', num_classes=21841), 'swin_large_patch4_window12_384.ms_in22k': _cfg( hf_hub_id='timm/', url='https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22k.pth', input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0, num_classes=21841), 'swin_s3_tiny_224.ms_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/s3_t-1d53f6a8.pth'), 'swin_s3_small_224.ms_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/s3_s-3bb4c69d.pth'), 'swin_s3_base_224.ms_in1k': _cfg( hf_hub_id='timm/', url='https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/s3_b-a1e95db4.pth'), }) @register_model def swin_tiny_patch4_window7_224(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-T @ 224x224, trained ImageNet-1k """ model_args = dict(patch_size=4, window_size=7, embed_dim=96, depths=(2, 2, 6, 2), num_heads=(3, 6, 12, 24)) return _create_swin_transformer( 'swin_tiny_patch4_window7_224', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_small_patch4_window7_224(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-S @ 224x224 """ model_args = dict(patch_size=4, window_size=7, embed_dim=96, depths=(2, 2, 18, 2), num_heads=(3, 6, 12, 24)) return _create_swin_transformer( 'swin_small_patch4_window7_224', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_base_patch4_window7_224(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-B @ 224x224 """ model_args = dict(patch_size=4, window_size=7, embed_dim=128, depths=(2, 2, 18, 2), num_heads=(4, 8, 16, 32)) return _create_swin_transformer( 'swin_base_patch4_window7_224', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_base_patch4_window12_384(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-B @ 384x384 """ model_args = dict(patch_size=4, window_size=12, embed_dim=128, depths=(2, 2, 18, 2), num_heads=(4, 8, 16, 32)) return _create_swin_transformer( 'swin_base_patch4_window12_384', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_large_patch4_window7_224(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-L @ 224x224 """ model_args = dict(patch_size=4, window_size=7, embed_dim=192, depths=(2, 2, 18, 2), num_heads=(6, 12, 24, 48)) return _create_swin_transformer( 'swin_large_patch4_window7_224', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_large_patch4_window12_384(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-L @ 384x384 """ model_args = dict(patch_size=4, window_size=12, embed_dim=192, depths=(2, 2, 18, 2), num_heads=(6, 12, 24, 48)) return _create_swin_transformer( 'swin_large_patch4_window12_384', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_s3_tiny_224(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-S3-T @ 224x224, https://arxiv.org/abs/2111.14725 """ model_args = dict( patch_size=4, window_size=(7, 7, 14, 7), embed_dim=96, depths=(2, 2, 6, 2), num_heads=(3, 6, 12, 24)) return _create_swin_transformer('swin_s3_tiny_224', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_s3_small_224(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-S3-S @ 224x224, https://arxiv.org/abs/2111.14725 """ model_args = dict( patch_size=4, window_size=(14, 14, 14, 7), embed_dim=96, depths=(2, 2, 18, 2), num_heads=(3, 6, 12, 24)) return _create_swin_transformer('swin_s3_small_224', pretrained=pretrained, **dict(model_args, **kwargs)) @register_model def swin_s3_base_224(pretrained=False, **kwargs) -> SwinTransformer: """ Swin-S3-B @ 224x224, https://arxiv.org/abs/2111.14725 """ model_args = dict( patch_size=4, window_size=(7, 7, 14, 7), embed_dim=96, depths=(2, 2, 30, 2), num_heads=(3, 6, 12, 24)) return _create_swin_transformer('swin_s3_base_224', pretrained=pretrained, **dict(model_args, **kwargs)) register_model_deprecations(__name__, { 'swin_base_patch4_window7_224_in22k': 'swin_base_patch4_window7_224.ms_in22k', 'swin_base_patch4_window12_384_in22k': 'swin_base_patch4_window12_384.ms_in22k', 'swin_large_patch4_window7_224_in22k': 'swin_large_patch4_window7_224.ms_in22k', 'swin_large_patch4_window12_384_in22k': 'swin_large_patch4_window12_384.ms_in22k', })
pytorch-image-models/timm/models/swin_transformer.py/0
{ "file_path": "pytorch-image-models/timm/models/swin_transformer.py", "repo_id": "pytorch-image-models", "token_count": 21031 }
215
""" Ported to pytorch thanks to [tstandley](https://github.com/tstandley/Xception-PyTorch) @author: tstandley Adapted by cadene Creates an Xception Model as defined in: Francois Chollet Xception: Deep Learning with Depthwise Separable Convolutions https://arxiv.org/pdf/1610.02357.pdf This weights ported from the Keras implementation. Achieves the following performance on the validation set: Loss:0.9173 Prec@1:78.892 Prec@5:94.292 REMEMBER to set your image size to 3x299x299 for both test and validation normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) The resize parameter of the validation transform should be 333, and make sure to center crop at 299x299 """ import torch.jit import torch.nn as nn import torch.nn.functional as F from timm.layers import create_classifier from ._builder import build_model_with_cfg from ._registry import register_model, generate_default_cfgs, register_model_deprecations __all__ = ['Xception'] class SeparableConv2d(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, dilation=1): super(SeparableConv2d, self).__init__() self.conv1 = nn.Conv2d( in_channels, in_channels, kernel_size, stride, padding, dilation, groups=in_channels, bias=False) self.pointwise = nn.Conv2d(in_channels, out_channels, 1, 1, 0, 1, 1, bias=False) def forward(self, x): x = self.conv1(x) x = self.pointwise(x) return x class Block(nn.Module): def __init__(self, in_channels, out_channels, reps, strides=1, start_with_relu=True, grow_first=True): super(Block, self).__init__() if out_channels != in_channels or strides != 1: self.skip = nn.Conv2d(in_channels, out_channels, 1, stride=strides, bias=False) self.skipbn = nn.BatchNorm2d(out_channels) else: self.skip = None rep = [] for i in range(reps): if grow_first: inc = in_channels if i == 0 else out_channels outc = out_channels else: inc = in_channels outc = in_channels if i < (reps - 1) else out_channels rep.append(nn.ReLU(inplace=True)) rep.append(SeparableConv2d(inc, outc, 3, stride=1, padding=1)) rep.append(nn.BatchNorm2d(outc)) if not start_with_relu: rep = rep[1:] else: rep[0] = nn.ReLU(inplace=False) if strides != 1: rep.append(nn.MaxPool2d(3, strides, 1)) self.rep = nn.Sequential(*rep) def forward(self, inp): x = self.rep(inp) if self.skip is not None: skip = self.skip(inp) skip = self.skipbn(skip) else: skip = inp x += skip return x class Xception(nn.Module): """ Xception optimized for the ImageNet dataset, as specified in https://arxiv.org/pdf/1610.02357.pdf """ def __init__(self, num_classes=1000, in_chans=3, drop_rate=0., global_pool='avg'): """ Constructor Args: num_classes: number of classes """ super(Xception, self).__init__() self.drop_rate = drop_rate self.global_pool = global_pool self.num_classes = num_classes self.num_features = self.head_hidden_size = 2048 self.conv1 = nn.Conv2d(in_chans, 32, 3, 2, 0, bias=False) self.bn1 = nn.BatchNorm2d(32) self.act1 = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(32, 64, 3, bias=False) self.bn2 = nn.BatchNorm2d(64) self.act2 = nn.ReLU(inplace=True) self.block1 = Block(64, 128, 2, 2, start_with_relu=False) self.block2 = Block(128, 256, 2, 2) self.block3 = Block(256, 728, 2, 2) self.block4 = Block(728, 728, 3, 1) self.block5 = Block(728, 728, 3, 1) self.block6 = Block(728, 728, 3, 1) self.block7 = Block(728, 728, 3, 1) self.block8 = Block(728, 728, 3, 1) self.block9 = Block(728, 728, 3, 1) self.block10 = Block(728, 728, 3, 1) self.block11 = Block(728, 728, 3, 1) self.block12 = Block(728, 1024, 2, 2, grow_first=False) self.conv3 = SeparableConv2d(1024, 1536, 3, 1, 1) self.bn3 = nn.BatchNorm2d(1536) self.act3 = nn.ReLU(inplace=True) self.conv4 = SeparableConv2d(1536, self.num_features, 3, 1, 1) self.bn4 = nn.BatchNorm2d(self.num_features) self.act4 = nn.ReLU(inplace=True) self.feature_info = [ dict(num_chs=64, reduction=2, module='act2'), dict(num_chs=128, reduction=4, module='block2.rep.0'), dict(num_chs=256, reduction=8, module='block3.rep.0'), dict(num_chs=728, reduction=16, module='block12.rep.0'), dict(num_chs=2048, reduction=32, module='act4'), ] self.global_pool, self.fc = create_classifier(self.num_features, self.num_classes, pool_type=global_pool) # #------- init weights -------- for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() @torch.jit.ignore def group_matcher(self, coarse=False): return dict( stem=r'^conv[12]|bn[12]', blocks=[ (r'^block(\d+)', None), (r'^conv[34]|bn[34]', (99,)), ], ) @torch.jit.ignore def set_grad_checkpointing(self, enable=True): assert not enable, "gradient checkpointing not supported" @torch.jit.ignore def get_classifier(self) -> nn.Module: return self.fc def reset_classifier(self, num_classes: int, global_pool: str = 'avg'): self.num_classes = num_classes self.global_pool, self.fc = create_classifier(self.num_features, self.num_classes, pool_type=global_pool) def forward_features(self, x): x = self.conv1(x) x = self.bn1(x) x = self.act1(x) x = self.conv2(x) x = self.bn2(x) x = self.act2(x) x = self.block1(x) x = self.block2(x) x = self.block3(x) x = self.block4(x) x = self.block5(x) x = self.block6(x) x = self.block7(x) x = self.block8(x) x = self.block9(x) x = self.block10(x) x = self.block11(x) x = self.block12(x) x = self.conv3(x) x = self.bn3(x) x = self.act3(x) x = self.conv4(x) x = self.bn4(x) x = self.act4(x) return x def forward_head(self, x, pre_logits: bool = False): x = self.global_pool(x) if self.drop_rate: F.dropout(x, self.drop_rate, training=self.training) return x if pre_logits else self.fc(x) def forward(self, x): x = self.forward_features(x) x = self.forward_head(x) return x def _xception(variant, pretrained=False, **kwargs): return build_model_with_cfg( Xception, variant, pretrained, feature_cfg=dict(feature_cls='hook'), **kwargs) default_cfgs = generate_default_cfgs({ 'legacy_xception.tf_in1k': { 'url': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-cadene/xception-43020ad28.pth', 'input_size': (3, 299, 299), 'pool_size': (10, 10), 'crop_pct': 0.8975, 'interpolation': 'bicubic', 'mean': (0.5, 0.5, 0.5), 'std': (0.5, 0.5, 0.5), 'num_classes': 1000, 'first_conv': 'conv1', 'classifier': 'fc' # The resize parameter of the validation transform should be 333, and make sure to center crop at 299x299 } }) @register_model def legacy_xception(pretrained=False, **kwargs) -> Xception: return _xception('legacy_xception', pretrained=pretrained, **kwargs) register_model_deprecations(__name__, { 'xception': 'legacy_xception', })
pytorch-image-models/timm/models/xception.py/0
{ "file_path": "pytorch-image-models/timm/models/xception.py", "repo_id": "pytorch-image-models", "token_count": 3992 }
216
""" NAdamW Optimizer Based on simplified algorithm in https://github.com/mlcommons/algorithmic-efficiency/tree/main/baselines/nadamw Added multi-tensor (foreach) path. """ import math from typing import List, Optional import torch from torch import Tensor # Modified from github.com/pytorch/pytorch/blob/v1.12.1/torch/optim/adamw.py. class NAdamW(torch.optim.Optimizer): r"""Implements NAdamW algorithm. See Table 1 in https://arxiv.org/abs/1910.05446 for the implementation of the NAdam algorithm (there is also a comment in the code which highlights the only difference of NAdamW and AdamW). For further details regarding the algorithm we refer to `Decoupled Weight Decay Regularization`_. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-8) weight_decay (float, optional): weight decay coefficient (default: 1e-2) .. _Decoupled Weight Decay Regularization: https://arxiv.org/abs/1711.05101 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=1e-2, maximize: bool = False, foreach: Optional[bool] = None, capturable: bool = False, ): if not 0.0 <= lr: raise ValueError(f'Invalid learning rate: {lr}') if not 0.0 <= eps: raise ValueError(f'Invalid epsilon value: {eps}') if not 0.0 <= betas[0] < 1.0: raise ValueError(f'Invalid beta parameter at index 0: {betas[0]}') if not 0.0 <= betas[1] < 1.0: raise ValueError(f'Invalid beta parameter at index 1: {betas[1]}') if not 0.0 <= weight_decay: raise ValueError(f'Invalid weight_decay value: {weight_decay}') defaults = dict( lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, foreach=foreach, maximize=maximize, capturable=capturable, ) super().__init__(params, defaults) def __setstate__(self, state): super().__setstate__(state) state_values = list(self.state.values()) step_is_tensor = (len(state_values) != 0) and torch.is_tensor( state_values[0]['step']) if not step_is_tensor: for s in state_values: s['step'] = torch.tensor(float(s['step'])) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Args: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ self._cuda_graph_capture_health_check() loss = None if closure is not None: with torch.enable_grad(): loss = closure() for group in self.param_groups: params_with_grad = [] grads = [] exp_avgs = [] exp_avg_sqs = [] state_steps = [] beta1, beta2 = group['betas'] for p in group['params']: if p.grad is None: continue params_with_grad.append(p) if p.grad.is_sparse: raise RuntimeError('NAdamW does not support sparse gradients') grads.append(p.grad) state = self.state[p] # State initialization if len(state) == 0: state['step'] = torch.tensor(0.) # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p, memory_format=torch.preserve_format) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros_like(p, memory_format=torch.preserve_format) exp_avgs.append(state['exp_avg']) exp_avg_sqs.append(state['exp_avg_sq']) state_steps.append(state['step']) nadamw( params_with_grad, grads, exp_avgs, exp_avg_sqs, state_steps, beta1=beta1, beta2=beta2, lr=group['lr'], weight_decay=group['weight_decay'], eps=group['eps'], maximize=group['maximize'], capturable=group['capturable'], ) return loss def nadamw( params: List[Tensor], grads: List[Tensor], exp_avgs: List[Tensor], exp_avg_sqs: List[Tensor], state_steps: List[Tensor], foreach: Optional[bool] = None, capturable: bool = False, *, beta1: float, beta2: float, lr: float, weight_decay: float, eps: float, maximize: bool, ) -> None: r"""Functional API that performs NAdamW algorithm computation. See NAdamW class for details. """ if not all(isinstance(t, torch.Tensor) for t in state_steps): raise RuntimeError( 'API has changed, `state_steps` argument must contain a list of' + ' singleton tensors') if foreach is None: foreach = True if foreach and not torch.jit.is_scripting(): func = _multi_tensor_nadamw else: func = _single_tensor_nadamw func( params, grads, exp_avgs, exp_avg_sqs, state_steps, beta1=beta1, beta2=beta2, lr=lr, weight_decay=weight_decay, eps=eps, maximize=maximize, capturable=capturable, ) def _single_tensor_nadamw( params: List[Tensor], grads: List[Tensor], exp_avgs: List[Tensor], exp_avg_sqs: List[Tensor], state_steps: List[Tensor], *, beta1: float, beta2: float, lr: float, weight_decay: float, eps: float, maximize: bool, capturable: bool ): for i, param in enumerate(params): grad = grads[i] if not maximize else -grads[i] exp_avg = exp_avgs[i] exp_avg_sq = exp_avg_sqs[i] step_t = state_steps[i] # Update step. step_t += 1 # Perform stepweight decay. param.mul_(1. - lr * weight_decay) # Decay the first and second moment running average coefficient. exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) if capturable: step = step_t # 1 - beta1 ** step can't be captured in a CUDA graph, even if step is a CUDA tensor # (incurs "RuntimeError: CUDA error: operation not permitted when stream is capturing") bias_correction1 = 1 - torch.pow(beta1, step) bias_correction2 = 1 - torch.pow(beta2, step) step_size = lr / bias_correction1 step_size_neg = step_size.neg() bias_correction2_sqrt = bias_correction2.sqrt() # Only difference between NAdamW and AdamW in this implementation. # The official PyTorch implementation of NAdam uses a different algorithm. exp_avg = exp_avg.mul(beta1).add_(grad, alpha=1 - beta1) denom = (exp_avg_sq.sqrt() / (bias_correction2_sqrt * step_size_neg)).add_(eps / step_size_neg) param.addcdiv_(exp_avg, denom) else: step = step_t.item() bias_correction1 = 1 - beta1 ** step bias_correction2 = 1 - beta2 ** step step_size = lr / bias_correction1 bias_correction2_sqrt = math.sqrt(bias_correction2) # Only difference between NAdamW and AdamW in this implementation. # The official PyTorch implementation of NAdam uses a different algorithm. exp_avg = exp_avg.mul(beta1).add_(grad, alpha=1 - beta1) denom = (exp_avg_sq.sqrt() / bias_correction2_sqrt).add_(eps) param.addcdiv_(exp_avg, denom, value=-step_size) def _multi_tensor_nadamw( params: List[Tensor], grads: List[Tensor], exp_avgs: List[Tensor], exp_avg_sqs: List[Tensor], state_steps: List[Tensor], *, beta1: float, beta2: float, lr: float, weight_decay: float, eps: float, maximize: bool, capturable: bool, ): if len(params) == 0: return if capturable: assert all( p.is_cuda and step.is_cuda for p, step in zip(params, state_steps) ), "If capturable=True, params and state_steps must be CUDA tensors." if maximize: grads = torch._foreach_neg(tuple(grads)) # type: ignore[assignment] grads = [torch.view_as_real(x) if torch.is_complex(x) else x for x in grads] exp_avgs = [torch.view_as_real(x) if torch.is_complex(x) else x for x in exp_avgs] exp_avg_sqs = [torch.view_as_real(x) if torch.is_complex(x) else x for x in exp_avg_sqs] params = [torch.view_as_real(x) if torch.is_complex(x) else x for x in params] # update steps torch._foreach_add_(state_steps, 1) # Perform stepweight decay torch._foreach_mul_(params, 1 - lr * weight_decay) # Decay the first and second moment running average coefficient torch._foreach_mul_(exp_avgs, beta1) torch._foreach_add_(exp_avgs, grads, alpha=1 - beta1) torch._foreach_mul_(exp_avg_sqs, beta2) torch._foreach_addcmul_(exp_avg_sqs, grads, grads, 1 - beta2) if capturable: # TODO: use foreach_pow if/when foreach_pow is added bias_correction1 = [torch.pow(beta1, step) for step in state_steps] bias_correction2 = [torch.pow(beta2, step) for step in state_steps] # foreach_sub doesn't allow a scalar as the first arg torch._foreach_sub_(bias_correction1, 1) torch._foreach_sub_(bias_correction2, 1) torch._foreach_neg_(bias_correction1) torch._foreach_neg_(bias_correction2) # foreach_div doesn't allow a scalar as the first arg step_size = torch._foreach_div(bias_correction1, lr) torch._foreach_reciprocal_(step_size) torch._foreach_neg_(step_size) bias_correction2_sqrt = torch._foreach_sqrt(bias_correction2) # Only difference between NAdamW and AdamW in this implementation. # The official PyTorch implementation of NAdam uses a different algorithm. exp_avgs = torch._foreach_mul(exp_avgs, beta1) torch._foreach_add_(exp_avgs, grads, alpha=1 - beta1) exp_avg_sq_sqrt = torch._foreach_sqrt(exp_avg_sqs) torch._foreach_div_( exp_avg_sq_sqrt, torch._foreach_mul(bias_correction2_sqrt, step_size) ) eps_over_step_size = torch._foreach_div(step_size, eps) torch._foreach_reciprocal_(eps_over_step_size) denom = torch._foreach_add(exp_avg_sq_sqrt, eps_over_step_size) torch._foreach_addcdiv_(params, exp_avgs, denom) else: bias_correction1 = [1 - beta1 ** step.item() for step in state_steps] bias_correction2 = [1 - beta2 ** step.item() for step in state_steps] step_size = [(lr / bc) * -1 for bc in bias_correction1] bias_correction2_sqrt = [math.sqrt(bc) for bc in bias_correction2] # Only difference between NAdamW and AdamW in this implementation. # The official PyTorch implementation of NAdam uses a different algorithm. exp_avgs = torch._foreach_mul(exp_avgs, beta1) torch._foreach_add_(exp_avgs, grads, alpha=1 - beta1) exp_avg_sq_sqrt = torch._foreach_sqrt(exp_avg_sqs) torch._foreach_div_(exp_avg_sq_sqrt, bias_correction2_sqrt) denom = torch._foreach_add(exp_avg_sq_sqrt, eps) torch._foreach_addcdiv_(params, exp_avgs, denom, step_size)
pytorch-image-models/timm/optim/nadamw.py/0
{ "file_path": "pytorch-image-models/timm/optim/nadamw.py", "repo_id": "pytorch-image-models", "token_count": 5958 }
217
from .agc import adaptive_clip_grad from .attention_extract import AttentionExtract from .checkpoint_saver import CheckpointSaver from .clip_grad import dispatch_clip_grad from .cuda import ApexScaler, NativeScaler from .decay_batch import decay_batch_step, check_batch_size_retry from .distributed import distribute_bn, reduce_tensor, init_distributed_device,\ world_info_from_env, is_distributed_env, is_primary from .jit import set_jit_legacy, set_jit_fuser from .log import setup_default_logging, FormatterNoInfo from .metrics import AverageMeter, accuracy from .misc import natural_key, add_bool_arg, ParseKwargs from .model import unwrap_model, get_state_dict, freeze, unfreeze, reparameterize_model from .model_ema import ModelEma, ModelEmaV2, ModelEmaV3 from .random import random_seed from .summary import update_summary, get_outdir
pytorch-image-models/timm/utils/__init__.py/0
{ "file_path": "pytorch-image-models/timm/utils/__init__.py", "repo_id": "pytorch-image-models", "token_count": 264 }
218
""" Summary utilities Hacked together by / Copyright 2020 Ross Wightman """ import csv import os from collections import OrderedDict try: import wandb except ImportError: pass def get_outdir(path, *paths, inc=False): outdir = os.path.join(path, *paths) if not os.path.exists(outdir): os.makedirs(outdir) elif inc: count = 1 outdir_inc = outdir + '-' + str(count) while os.path.exists(outdir_inc): count = count + 1 outdir_inc = outdir + '-' + str(count) assert count < 100 outdir = outdir_inc os.makedirs(outdir) return outdir def update_summary( epoch, train_metrics, eval_metrics, filename, lr=None, write_header=False, log_wandb=False, ): rowd = OrderedDict(epoch=epoch) rowd.update([('train_' + k, v) for k, v in train_metrics.items()]) if eval_metrics: rowd.update([('eval_' + k, v) for k, v in eval_metrics.items()]) if lr is not None: rowd['lr'] = lr if log_wandb: wandb.log(rowd) with open(filename, mode='a') as cf: dw = csv.DictWriter(cf, fieldnames=rowd.keys()) if write_header: # first iteration (epoch == 1 can't be used) dw.writeheader() dw.writerow(rowd)
pytorch-image-models/timm/utils/summary.py/0
{ "file_path": "pytorch-image-models/timm/utils/summary.py", "repo_id": "pytorch-image-models", "token_count": 633 }
219
{ mkPoetryApplication, pkg-config, protobuf, openssl, }: mkPoetryApplication { # name = "text-generation-server"; projectDir = ./server; # nativeBuildInputs = [ pkg-config ]; # buildInputs = [ openssl.dev protobuf ]; }
text-generation-inference/_server.nix/0
{ "file_path": "text-generation-inference/_server.nix", "repo_id": "text-generation-inference", "token_count": 89 }
220
[package] name = "text-generation-backends-trtllm" version.workspace = true edition.workspace = true authors.workspace = true homepage.workspace = true [dependencies] async-trait = "0.1" async-stream = "0.3" clap = { version = "4.5", features = ["derive"] } cxx = "1.0" log = { version = "0.4", features = [] } text-generation-router = { path = "../../router" } tokenizers = { version = "0.19", features = ["hf-hub"] } tokio = { version = "1.38", features = ["rt", "rt-multi-thread", "parking_lot", "signal", "sync"] } tokio-stream = "0.1.15" thiserror = "1.0.62" tracing = "0.1" tracing-opentelemetry = "0.24" tracing-subscriber = { version = "0.3", features = ["json", "env-filter"] } parking_lot = "0.12" [build-dependencies] cmake = "0.1" cxx-build = { version = "1.0", features = ["parallel"] } pkg-config = "0.3"
text-generation-inference/backends/trtllm/Cargo.toml/0
{ "file_path": "text-generation-inference/backends/trtllm/Cargo.toml", "repo_id": "text-generation-inference", "token_count": 333 }
221
// // Created by mfuntowicz on 6/30/24. // #pragma once #include <cmath> #include <exception> #include <filesystem> #include <limits> #include <iterator> #include <vector> #include <spdlog/spdlog.h> #include "backends/trtllm/include/ffi.h" huggingface::tgi::backends::TensorRtLlmBackendImpl::TensorRtLlmBackendImpl( const std::string_view &engineFolder, const std::string_view &executorWorker ) : TensorRtLlmBackend(engineFolder, executorWorker) {} bool huggingface::tgi::backends::TensorRtLlmBackendImpl::IsReady() const { return TensorRtLlmBackend::IsReady(); } uint64_t huggingface::tgi::backends::TensorRtLlmBackendImpl::Submit( rust::Slice<const uint32_t> tokens, int32_t topK, float_t topP, float_t temperature, float_t repetition_penalty, float_t frequency_penalty, uint64_t seed) { // This will copy all the items from the initial slice std::vector<int32_t> tokens_(std::make_move_iterator(tokens.begin()), std::make_move_iterator(tokens.end())); return TensorRtLlmBackend::Submit( std::move(tokens_), topK, topP, temperature, repetition_penalty, frequency_penalty, seed); } size_t huggingface::tgi::backends::TensorRtLlmBackendImpl::StreamTokens( const uint64_t requestId, huggingface::tgi::backends::GenerationContext *ctx, rust::Fn<void(huggingface::tgi::backends::GenerationContext *, huggingface::tgi::backends::GenerationStep)> callback) { size_t numTokens = 0; for (const auto &item: Poll(requestId)) { GenerationStep step; if (!item.hasError()) { SPDLOG_DEBUG("\tStreamTokens -> Decoding token..."); const auto decoded = item.getResult(); const auto token = decoded.outputTokenIds[0][0]; const auto isFinal = decoded.isFinal; const auto logProb = decoded.logProbs.value()[0][0]; ++numTokens; SPDLOG_DEBUG(FMT_STRING("\tStreamTokens -> {:d} {:.2f} (final = {})"), token, logProb, isFinal); step = huggingface::tgi::backends::GenerationStep{ static_cast<uint32_t>(token), logProb, isFinal, false, std::move(std::string()) }; SPDLOG_DEBUG("\tStreamTokens -> Post callback"); } else { // TODO : Return rest::Result with error const auto what = item.getErrorMsg(); SPDLOG_WARN("\tStreamTokens -> Got error while decoding: {}", what); step = huggingface::tgi::backends::GenerationStep{ std::numeric_limits<uint32_t>::max(), 0.0, true, true, std::move(what) }; } callback(std::move(ctx), std::move(step)); } return numTokens; } std::unique_ptr<huggingface::tgi::backends::TensorRtLlmBackendImpl> huggingface::tgi::backends::CreateTensorRtLlmBackend(rust::Str engineFolder, rust::Str executorWorker) { // Unconditionally call this to initialize and discover TRTLLM plugins InitializeBackend(); const auto enginePath = std::string_view(engineFolder.begin(), engineFolder.end()); const auto executorPath = std::string_view(executorWorker.begin(), executorWorker.end()); return std::make_unique<TensorRtLlmBackendImpl>(std::move(enginePath), std::move(executorPath)); }
text-generation-inference/backends/trtllm/src/ffi.cpp/0
{ "file_path": "text-generation-inference/backends/trtllm/src/ffi.cpp", "repo_id": "text-generation-inference", "token_count": 1376 }
222
[package] name = "text-generation-benchmark" description = "Text Generation Benchmarking tool" version.workspace = true edition.workspace = true authors.workspace = true homepage.workspace = true [lib] path = "src/lib.rs" [[bin]] name = "text-generation-benchmark" path = "src/main.rs" [dependencies] average = "0.14" clap = { version = "4.4.5", features = ["derive", "env"] } crossterm = "0.27" float-ord = "0.3.2" serde = {version = "1.0.188", features = ["derive"]} serde_json = "1.0" tabled = "0.14.0" text-generation-client = { path = "../backends/client" } thiserror = "1.0.48" tokenizers = { workspace = true } tokio = { version = "1.32.0", features = ["rt", "rt-multi-thread", "parking_lot", "signal", "sync", "macros"] } tui = {package = "ratatui", version = "0.23", default-features = false, features = ["crossterm"]} tracing = "0.1.37" tracing-subscriber = { version = "0.3.17", features = ["json", "env-filter"] } hf-hub = { workspace = true }
text-generation-inference/benchmark/Cargo.toml/0
{ "file_path": "text-generation-inference/benchmark/Cargo.toml", "repo_id": "text-generation-inference", "token_count": 368 }
223
from text_generation.errors import ( parse_error, GenerationError, IncompleteGenerationError, OverloadedError, ValidationError, BadRequestError, ShardNotReadyError, ShardTimeoutError, NotFoundError, RateLimitExceededError, UnknownError, ) def test_generation_error(): payload = {"error_type": "generation", "error": "test"} assert isinstance(parse_error(400, payload), GenerationError) def test_incomplete_generation_error(): payload = {"error_type": "incomplete_generation", "error": "test"} assert isinstance(parse_error(400, payload), IncompleteGenerationError) def test_overloaded_error(): payload = {"error_type": "overloaded", "error": "test"} assert isinstance(parse_error(400, payload), OverloadedError) def test_validation_error(): payload = {"error_type": "validation", "error": "test"} assert isinstance(parse_error(400, payload), ValidationError) def test_bad_request_error(): payload = {"error": "test"} assert isinstance(parse_error(400, payload), BadRequestError) def test_shard_not_ready_error(): payload = {"error": "test"} assert isinstance(parse_error(403, payload), ShardNotReadyError) assert isinstance(parse_error(424, payload), ShardNotReadyError) def test_shard_timeout_error(): payload = {"error": "test"} assert isinstance(parse_error(504, payload), ShardTimeoutError) def test_not_found_error(): payload = {"error": "test"} assert isinstance(parse_error(404, payload), NotFoundError) def test_rate_limit_exceeded_error(): payload = {"error": "test"} assert isinstance(parse_error(429, payload), RateLimitExceededError) def test_unknown_error(): payload = {"error": "test"} assert isinstance(parse_error(500, payload), UnknownError)
text-generation-inference/clients/python/tests/test_errors.py/0
{ "file_path": "text-generation-inference/clients/python/tests/test_errors.py", "repo_id": "text-generation-inference", "token_count": 598 }
224
# Non-core Model Serving TGI supports various LLM architectures (see full list [here](../supported_models)). If you wish to serve a model that is not one of the supported models, TGI will fallback to the `transformers` implementation of that model. This means you will be unable to use some of the features introduced by TGI, such as tensor-parallel sharding or flash attention. However, you can still get many benefits of TGI, such as continuous batching or streaming outputs. You can serve these models using the same Docker command-line invocation as with fully supported models ๐Ÿ‘‡ ```bash docker run --gpus all --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:latest --model-id gpt2 ``` If the model you wish to serve is a custom transformers model, and its weights and implementation are available in the Hub, you can still serve the model by passing the `--trust-remote-code` flag to the `docker run` command like below ๐Ÿ‘‡ ```bash docker run --gpus all --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:latest --model-id <CUSTOM_MODEL_ID> --trust-remote-code ``` Finally, if the model is not on Hugging Face Hub but on your local, you can pass the path to the folder that contains your model like below ๐Ÿ‘‡ ```bash # Make sure your model is in the $volume directory docker run --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:latest --model-id /data/<PATH-TO-FOLDER> ``` You can refer to [transformers docs on custom models](https://huggingface.co/docs/transformers/main/en/custom_models) for more information.
text-generation-inference/docs/source/basic_tutorials/non_core_models.md/0
{ "file_path": "text-generation-inference/docs/source/basic_tutorials/non_core_models.md", "repo_id": "text-generation-inference", "token_count": 472 }
225
# Text Generation Inference Text Generation Inference (TGI) is a toolkit for deploying and serving Large Language Models (LLMs). TGI enables high-performance text generation for the most popular open-source LLMs, including Llama, Falcon, StarCoder, BLOOM, GPT-NeoX, and T5. ![Text Generation Inference](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/TGI.png) Text Generation Inference implements many optimizations and features, such as: - Simple launcher to serve most popular LLMs - Production ready (distributed tracing with Open Telemetry, Prometheus metrics) - Tensor Parallelism for faster inference on multiple GPUs - Token streaming using Server-Sent Events (SSE) - Continuous batching of incoming requests for increased total throughput - Optimized transformers code for inference using [Flash Attention](https://github.com/HazyResearch/flash-attention) and [Paged Attention](https://github.com/vllm-project/vllm) on the most popular architectures - Quantization with [bitsandbytes](https://github.com/TimDettmers/bitsandbytes) and [GPT-Q](https://arxiv.org/abs/2210.17323) - [Safetensors](https://github.com/huggingface/safetensors) weight loading - Watermarking with [A Watermark for Large Language Models](https://arxiv.org/abs/2301.10226) - Logits warper (temperature scaling, top-p, top-k, repetition penalty) - Stop sequences - Log probabilities - Fine-tuning Support: Utilize fine-tuned models for specific tasks to achieve higher accuracy and performance. - [Guidance](../conceptual/guidance): Enable function calling and tool-use by forcing the model to generate structured outputs based on your own predefined output schemas. Text Generation Inference is used in production by multiple projects, such as: - [Hugging Chat](https://github.com/huggingface/chat-ui), an open-source interface for open-access models, such as Open Assistant and Llama - [OpenAssistant](https://open-assistant.io/), an open-source community effort to train LLMs in the open - [nat.dev](http://nat.dev/), a playground to explore and compare LLMs.
text-generation-inference/docs/source/index.md/0
{ "file_path": "text-generation-inference/docs/source/index.md", "repo_id": "text-generation-inference", "token_count": 563 }
226
{ "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [ { "id": 100000, "logprob": null, "text": "<๏ฝœbeginโ–ofโ–sentence๏ฝœ>" }, { "id": 3533, "logprob": -9.625, "text": "Test" }, { "id": 3102, "logprob": -11.25, "text": " request" } ], "seed": null, "tokens": [ { "id": 185, "logprob": -1.546875, "special": false, "text": "\n" }, { "id": 549, "logprob": -2.859375, "special": false, "text": "The" }, { "id": 1727, "logprob": -2.484375, "special": false, "text": " test" }, { "id": 3102, "logprob": -0.83203125, "special": false, "text": " request" }, { "id": 317, "logprob": -1.1484375, "special": false, "text": " is" }, { "id": 245, "logprob": -1.578125, "special": false, "text": " a" }, { "id": 3412, "logprob": -2.578125, "special": false, "text": " document" }, { "id": 344, "logprob": -1.125, "special": false, "text": " that" }, { "id": 317, "logprob": -1.6953125, "special": false, "text": " is" }, { "id": 1222, "logprob": -1.71875, "special": false, "text": " used" } ], "top_tokens": null }, "generated_text": "\nThe test request is a document that is used" }
text-generation-inference/integration-tests/models/__snapshots__/test_flash_deepseek_v2/test_flash_deepseek_v2.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_flash_deepseek_v2/test_flash_deepseek_v2.json", "repo_id": "text-generation-inference", "token_count": 1052 }
227
{ "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [ { "id": 1, "logprob": null, "text": "<s>" }, { "id": 4321, "logprob": -13.90625, "text": "Test" }, { "id": 2009, "logprob": -12.328125, "text": "request" } ], "seed": null, "tokens": [ { "id": 13, "logprob": -2.0566406, "special": false, "text": "\n" }, { "id": 13, "logprob": -1.5253906, "special": false, "text": "\n" }, { "id": 29902, "logprob": -2.7578125, "special": false, "text": "I" }, { "id": 4966, "logprob": -1.9033203, "special": false, "text": " hope" }, { "id": 445, "logprob": -0.5019531, "special": false, "text": " this" }, { "id": 6911, "logprob": -0.21264648, "special": false, "text": " helps" }, { "id": 29991, "logprob": -0.5991211, "special": false, "text": "!" }, { "id": 2803, "logprob": -0.37475586, "special": false, "text": " Let" }, { "id": 592, "logprob": -0.018463135, "special": false, "text": " me" }, { "id": 1073, "logprob": -0.0008597374, "special": false, "text": " know" } ], "top_tokens": null }, "generated_text": "\n\nI hope this helps! Let me know" }
text-generation-inference/integration-tests/models/__snapshots__/test_flash_grammar_llama/test_flash_llama_grammar.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_flash_grammar_llama/test_flash_llama_grammar.json", "repo_id": "text-generation-inference", "token_count": 1048 }
228
{ "details": { "finish_reason": "length", "generated_tokens": 40, "prefill": [], "seed": null, "tokens": [ { "id": 13, "logprob": -0.27416992, "special": false, "text": "\n" }, { "id": 13, "logprob": -0.17016602, "special": false, "text": "\n" }, { "id": 28737, "logprob": -2.7109375, "special": false, "text": "I" }, { "id": 28809, "logprob": -1.5, "special": false, "text": "โ€™" }, { "id": 28719, "logprob": -0.34204102, "special": false, "text": "m" }, { "id": 459, "logprob": -1.6914062, "special": false, "text": " not" }, { "id": 1864, "logprob": -0.69140625, "special": false, "text": " sure" }, { "id": 513, "logprob": -1.6171875, "special": false, "text": " if" }, { "id": 315, "logprob": -1.3837891, "special": false, "text": " I" }, { "id": 541, "logprob": -1.2226562, "special": false, "text": " can" }, { "id": 1567, "logprob": -1.8652344, "special": false, "text": " come" }, { "id": 582, "logprob": -0.0070228577, "special": false, "text": " up" }, { "id": 395, "logprob": -0.0054092407, "special": false, "text": " with" }, { "id": 28705, "logprob": -0.62597656, "special": false, "text": " " }, { "id": 28770, "logprob": -0.0035572052, "special": false, "text": "3" }, { "id": 4842, "logprob": -0.93603516, "special": false, "text": " unique" }, { "id": 3085, "logprob": -0.028411865, "special": false, "text": " words" }, { "id": 369, "logprob": -1.0400391, "special": false, "text": " that" }, { "id": 6685, "logprob": -0.09710693, "special": false, "text": " describe" }, { "id": 528, "logprob": -0.066467285, "special": false, "text": " me" }, { "id": 28725, "logprob": -1.0722656, "special": false, "text": "," }, { "id": 562, "logprob": -0.33422852, "special": false, "text": " but" }, { "id": 315, "logprob": -0.5136719, "special": false, "text": " I" }, { "id": 28809, "logprob": -0.8989258, "special": false, "text": "โ€™" }, { "id": 584, "logprob": -0.2076416, "special": false, "text": "ll" }, { "id": 1464, "logprob": -0.8808594, "special": false, "text": " try" }, { "id": 28723, "logprob": -0.88427734, "special": false, "text": "." }, { "id": 13, "logprob": -0.91064453, "special": false, "text": "\n" }, { "id": 13, "logprob": -0.08105469, "special": false, "text": "\n" }, { "id": 28740, "logprob": -1.8486328, "special": false, "text": "1" }, { "id": 28723, "logprob": -0.111572266, "special": false, "text": "." }, { "id": 23626, "logprob": -3.15625, "special": false, "text": " Creative" }, { "id": 13, "logprob": -0.9194336, "special": false, "text": "\n" }, { "id": 28750, "logprob": -0.24841309, "special": false, "text": "2" }, { "id": 28723, "logprob": -9.393692e-05, "special": false, "text": "." }, { "id": 6785, "logprob": -3.1386719, "special": false, "text": " Fun" }, { "id": 1780, "logprob": -0.53564453, "special": false, "text": "ny" }, { "id": 13, "logprob": -0.09033203, "special": false, "text": "\n" }, { "id": 28770, "logprob": -0.00466156, "special": false, "text": "3" }, { "id": 28723, "logprob": -0.00016450882, "special": false, "text": "." } ] }, "generated_text": "\n\nIโ€™m not sure if I can come up with 3 unique words that describe me, but Iโ€™ll try.\n\n1. Creative\n2. Funny\n3." }
text-generation-inference/integration-tests/models/__snapshots__/test_lora_mistral/test_lora_mistral_with_customer_support_adapter.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_lora_mistral/test_lora_mistral_with_customer_support_adapter.json", "repo_id": "text-generation-inference", "token_count": 3128 }
229
{ "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [ { "id": 1, "logprob": null, "text": "<s>" }, { "id": 4321, "logprob": -9.8359375, "text": "Test" }, { "id": 2009, "logprob": -9.6171875, "text": "request" } ], "seed": null, "tokens": [ { "id": 13, "logprob": -2.3417969, "special": false, "text": "\n" }, { "id": 3057, "logprob": -1.8730469, "special": false, "text": "Test" }, { "id": 2009, "logprob": -1.2626953, "special": false, "text": " request" }, { "id": 13, "logprob": -1.7060547, "special": false, "text": "\n" }, { "id": 3057, "logprob": -1.4482422, "special": false, "text": "Test" }, { "id": 2009, "logprob": -0.15246582, "special": false, "text": " request" }, { "id": 13, "logprob": -0.796875, "special": false, "text": "\n" }, { "id": 3057, "logprob": -0.22766113, "special": false, "text": "Test" }, { "id": 2009, "logprob": -0.007045746, "special": false, "text": " request" }, { "id": 13, "logprob": -0.021759033, "special": false, "text": "\n" } ], "top_tokens": null }, "generated_text": "\nTest request\nTest request\nTest request\n" }
text-generation-inference/integration-tests/models/__snapshots__/test_server_gptq_quantized/test_server_gptq_quantized.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_server_gptq_quantized/test_server_gptq_quantized.json", "repo_id": "text-generation-inference", "token_count": 1051 }
230
import pytest @pytest.fixture(scope="module") def flash_deepseek_v2_handle(launcher): with launcher("deepseek-ai/DeepSeek-V2-Lite", num_shard=2) as handle: yield handle @pytest.fixture(scope="module") async def flash_deepseek_v2(flash_deepseek_v2_handle): await flash_deepseek_v2_handle.health(300) return flash_deepseek_v2_handle.client @pytest.mark.release @pytest.mark.asyncio @pytest.mark.private async def test_flash_deepseek_v2(flash_deepseek_v2, response_snapshot): response = await flash_deepseek_v2.generate( "Test request", max_new_tokens=10, decoder_input_details=True ) assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio @pytest.mark.private async def test_flash_deepseek_v2_all_params(flash_deepseek_v2, response_snapshot): response = await flash_deepseek_v2.generate( "Test request", max_new_tokens=10, repetition_penalty=1.2, return_full_text=True, stop_sequences=["test"], temperature=0.5, top_p=0.9, top_k=10, truncate=5, typical_p=0.9, watermark=True, decoder_input_details=True, seed=0, ) assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio @pytest.mark.private async def test_flash_deepseek_v2_load( flash_deepseek_v2, generate_load, response_snapshot ): responses = await generate_load( flash_deepseek_v2, "Test request", max_new_tokens=10, n=4 ) assert len(responses) == 4 assert all([r.generated_text == responses[0].generated_text for r in responses]) assert responses == response_snapshot
text-generation-inference/integration-tests/models/test_flash_deepseek_v2.py/0
{ "file_path": "text-generation-inference/integration-tests/models/test_flash_deepseek_v2.py", "repo_id": "text-generation-inference", "token_count": 710 }
231
import pytest @pytest.fixture(scope="module") def flash_neox_sharded_handle(launcher): with launcher("OpenAssistant/oasst-sft-1-pythia-12b", num_shard=2) as handle: yield handle @pytest.fixture(scope="module") async def flash_neox_sharded(flash_neox_sharded_handle): await flash_neox_sharded_handle.health(300) return flash_neox_sharded_handle.client @pytest.mark.release @pytest.mark.asyncio async def test_flash_neox(flash_neox_sharded, response_snapshot): response = await flash_neox_sharded.generate( "<|prompter|>What is a meme, and what's the history behind this word?<|endoftext|><|assistant|>", max_new_tokens=10, decoder_input_details=True, ) assert response.details.generated_tokens == 10 assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio async def test_flash_neox_load(flash_neox_sharded, generate_load, response_snapshot): responses = await generate_load( flash_neox_sharded, "<|prompter|>What is a meme, and what's the history behind this word?<|endoftext|><|assistant|>", max_new_tokens=10, n=4, ) assert len(responses) == 4 assert all([r.generated_text == responses[0].generated_text for r in responses]) assert responses == response_snapshot
text-generation-inference/integration-tests/models/test_flash_neox_sharded.py/0
{ "file_path": "text-generation-inference/integration-tests/models/test_flash_neox_sharded.py", "repo_id": "text-generation-inference", "token_count": 507 }
232
import pytest @pytest.fixture(scope="module") def mt0_base_handle(launcher): with launcher("bigscience/mt0-base") as handle: yield handle @pytest.fixture(scope="module") async def mt0_base(mt0_base_handle): await mt0_base_handle.health(300) return mt0_base_handle.client @pytest.mark.release @pytest.mark.asyncio async def test_mt0_base(mt0_base, response_snapshot): response = await mt0_base.generate( "Why is the sky blue?", max_new_tokens=10, top_p=0.9, decoder_input_details=True, seed=0, ) assert response.details.generated_tokens == 5 assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio async def test_mt0_base_all_params(mt0_base, response_snapshot): response = await mt0_base.generate( "Why is the sky blue?", max_new_tokens=10, repetition_penalty=1.2, return_full_text=True, stop_sequences=["test"], temperature=0.5, top_p=0.9, top_k=10, truncate=5, typical_p=0.9, watermark=True, decoder_input_details=True, seed=0, ) assert response.details.generated_tokens == 10 assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio async def test_mt0_base_load(mt0_base, generate_load, response_snapshot): responses = await generate_load( mt0_base, "Why is the sky blue?", max_new_tokens=10, n=4, ) assert len(responses) == 4 assert all([r.generated_text == responses[0].generated_text for r in responses]) assert responses == response_snapshot
text-generation-inference/integration-tests/models/test_mt0_base.py/0
{ "file_path": "text-generation-inference/integration-tests/models/test_mt0_base.py", "repo_id": "text-generation-inference", "token_count": 737 }
233
import json def main(): with open("./ShareGPT_V3_unfiltered_cleaned_split.json", "r") as f: data = json.load(f) # Select only the first 2k conversations that start with a human. max = 2000 conversations = [] for conversation in data: conv = conversation.get("conversations") if conv and conv[0]["from"] == "human": # Trim the rest of the output conversation["conversations"] = conversation["conversations"][:1] conversations.append(conversation) if len(conversation) >= max: break with open("./small.json", "w") as f: data = json.dump(conversations, f, indent=4) if __name__ == "__main__": main()
text-generation-inference/load_tests/filter.py/0
{ "file_path": "text-generation-inference/load_tests/filter.py", "repo_id": "text-generation-inference", "token_count": 307 }
234
use crate::infer::Infer; use crate::{ default_parameters, server::{generate_internal, ComputeType}, Deserialize, ErrorResponse, GenerateParameters, GenerateRequest, Serialize, ToSchema, }; use axum::extract::{Extension, Path}; use axum::http::{HeaderMap, StatusCode}; use axum::response::IntoResponse; use axum::Json; use futures::stream::FuturesUnordered; use futures::TryStreamExt; #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct OutputChunk { pub name: String, pub shape: Vec<usize>, pub datatype: String, pub data: Vec<u8>, } #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct InferenceOutput { pub id: String, pub outputs: Vec<OutputChunk>, } #[derive(Debug, Deserialize, ToSchema)] pub(crate) struct InferenceRequest { pub id: String, #[serde(default = "default_parameters")] pub parameters: GenerateParameters, pub inputs: Vec<Input>, pub outputs: Vec<Output>, } #[derive(Debug, Serialize, Deserialize, ToSchema)] pub(crate) struct Input { pub name: String, pub shape: Vec<usize>, pub datatype: String, pub data: Vec<u8>, } #[derive(Debug, Serialize, Deserialize, ToSchema)] pub(crate) struct Output { pub name: String, } #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct LiveResponse { pub live: bool, } #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct ReadyResponse { pub live: bool, } #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct MetadataServerResponse { pub name: String, pub version: String, pub extensions: Vec<String>, } #[utoipa::path( post, tag = "Text Generation Inference", path = "/v2/health/live", responses( (status = 200, description = "Service is live", body = LiveReponse), (status = 404, description = "Service not found", body = ErrorResponse, example = json!({"error": "No response"})) ) )] pub async fn kserve_health_live() -> Json<LiveResponse> { let data = LiveResponse { live: true }; Json(data) } #[utoipa::path( get, tag = "Text Generation Inference", path = "/v2/health/ready", responses( (status = 200, description = "Service is ready", body = ReadyResponse), (status = 404, description = "Service not found", body = ErrorResponse, example = json!({"error": "No response"})) ) )] pub async fn kserve_health_ready() -> Json<ReadyResponse> { let data = ReadyResponse { live: true }; Json(data) } #[utoipa::path( get, tag = "Text Generation Inference", path = "/v2", responses( (status = 200, description = "Metadata retrieved", body = MetadataServerResponse), (status = 404, description = "Service not found", body = ErrorResponse, example = json!({"error": "No response"})) ) )] pub async fn kerve_server_metadata() -> Json<MetadataServerResponse> { let data = MetadataServerResponse { name: "text-generation-inference".to_string(), version: env!("CARGO_PKG_VERSION").to_string(), extensions: vec![ "health".to_string(), "models".to_string(), "metrics".to_string(), ], }; Json(data) } #[utoipa::path( get, tag = "Text Generation Inference", path = "/v2/models/{model_name}/versions/{model_version}", responses( (status = 200, description = "Model version metadata retrieved", body = MetadataServerResponse), (status = 404, description = "Model or version not found", body = ErrorResponse, example = json!({"error": "No response"})) ) )] pub async fn kserve_model_metadata( Path((model_name, model_version)): Path<(String, String)>, ) -> Json<MetadataServerResponse> { let data = MetadataServerResponse { name: model_name, version: model_version, extensions: vec!["infer".to_string(), "ready".to_string()], }; Json(data) } #[utoipa::path( get, tag = "Text Generation Inference", path = "/v2/models/{model_name}/versions/{model_version}/ready", responses( (status = 200, description = "Model version is ready", body = ReadyResponse), (status = 404, description = "Model or version not found", body = ErrorResponse, example = json!({"error": "No response"})) ) )] pub async fn kserve_model_metadata_ready( Path((_model_name, _model_version)): Path<(String, String)>, ) -> Json<ReadyResponse> { let data = ReadyResponse { live: true }; Json(data) } #[utoipa::path( post, tag = "Text Generation Inference", path = "/v2/models/{model_name}/versions/{model_version}/infer", request_body = Json<InferenceRequest>, responses( (status = 200, description = "Inference executed successfully", body = InferenceOutput), (status = 404, description = "Model or version not found", body = ErrorResponse, example = json!({"error": "No response"})) ) )] pub async fn kserve_model_infer( infer: Extension<Infer>, Extension(compute_type): Extension<ComputeType>, Json(payload): Json<InferenceRequest>, ) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> { let id = payload.id.clone(); let str_inputs = payload .inputs .iter() .map(|input| { std::str::from_utf8(&input.data).map_err(|e| { ( StatusCode::UNPROCESSABLE_ENTITY, Json(ErrorResponse { error: e.to_string(), error_type: "utf8".to_string(), }), ) }) }) .collect::<Result<Vec<_>, _>>()?; if str_inputs.len() != payload.outputs.len() { return Err(( StatusCode::UNPROCESSABLE_ENTITY, Json(ErrorResponse { error: "Inputs and outputs length mismatch".to_string(), error_type: "length mismatch".to_string(), }), )); } let output_chunks = str_inputs .iter() .zip(&payload.outputs) .map(|(str_input, output)| { let generate_request = GenerateRequest { inputs: str_input.to_string(), parameters: payload.parameters.clone(), }; let infer = infer.clone(); let compute_type = compute_type.clone(); let span = tracing::Span::current(); async move { generate_internal(infer, compute_type, Json(generate_request), span) .await .map(|(_, Json(generation))| { let generation_as_bytes = generation.generated_text.as_bytes().to_vec(); OutputChunk { name: output.name.clone(), shape: vec![1, generation_as_bytes.len()], datatype: "BYTES".to_string(), data: generation_as_bytes, } }) .map_err(|_| { ( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: "Incomplete generation".into(), error_type: "Incomplete generation".into(), }), ) }) } }) .collect::<FuturesUnordered<_>>() .try_collect::<Vec<_>>() .await?; let inference_output = InferenceOutput { id: id.clone(), outputs: output_chunks, }; Ok((HeaderMap::new(), Json(inference_output))) }
text-generation-inference/router/src/kserve.rs/0
{ "file_path": "text-generation-inference/router/src/kserve.rs", "repo_id": "text-generation-inference", "token_count": 3505 }
235
flash_att_v2_commit_cuda := v2.6.1 flash_att_v2_commit_rocm := 2554f490101742ccdc56620a938f847f61754be6 build-flash-attention-v2-cuda: pip install -U packaging wheel pip install flash-attn==$(flash_att_v2_commit_cuda) install-flash-attention-v2-cuda: build-flash-attention-v2-cuda echo "Flash v2 installed" build-flash-attention-v2-rocm: if [ ! -d 'flash-attention-v2' ]; then \ pip install -U packaging ninja --no-cache-dir && \ git clone https://github.com/ROCm/flash-attention.git flash-attention-v2 && \ cd flash-attention-v2 && git fetch && git checkout $(flash_att_v2_commit_rocm) && \ git submodule update --init --recursive && GPU_ARCHS="gfx90a;gfx942" PYTORCH_ROCM_ARCH="gfx90a;gfx942" python setup.py build; \ fi install-flash-attention-v2-rocm: build-flash-attention-v2-rocm cd flash-attention-v2 && \ GPU_ARCHS="gfx90a;gfx942" PYTORCH_ROCM_ARCH="gfx90a;gfx942" python setup.py install
text-generation-inference/server/Makefile-flash-att-v2/0
{ "file_path": "text-generation-inference/server/Makefile-flash-att-v2", "repo_id": "text-generation-inference", "token_count": 396 }
236
// Adapted from turboderp exllama: https://github.com/turboderp/exllama #include <ATen/cuda/CUDAContext.h> #include "q4_matrix.cuh" #include <vector> #include "../util.cuh" #include "../matrix.cuh" using namespace std; const int UNSHUF_BLOCKSIZE_X = 64; const int RECONS_THREADS_X = 64; // Block size and thread count along columns in out, each thread converts 1 column const int RECONS_THREADS_Y = 1; // Block size and thread count along rows in x and out, each thread converts 8 rows vector<Q4Matrix*> g_q4_matrices; void g_q4_keep_matrix(Q4Matrix* m) { g_q4_matrices.push_back(m); } void g_q4_free_matrices() { for (const auto& m : g_q4_matrices) delete m; g_q4_matrices.clear(); } Q4Matrix::Q4Matrix ( const int _height, const int _width, const int _groups, uint32_t* _qweight, uint32_t* _qzeros, half* _scales, uint32_t* _g_idx, const int _device ) : height(_height), width(_width), groups(_groups), device(_device) { cudaSetDevice(device); cuda_qweight = _qweight; cuda_qzeros = _qzeros; cuda_scales = _scales; groupsize = height / groups; if (_g_idx) make_sequential(_g_idx); } Q4Matrix::~Q4Matrix() { } // Make sequential __global__ void make_sequential_kernel ( const uint32_t* __restrict__ w, uint32_t* __restrict__ w_new, const uint32_t* __restrict__ x_map, const int w_height, const int w_width ) { const uint64_t* w2 = (uint64_t*) w; uint64_t* w_new2 = (uint64_t*) w_new; int w2_stride = w_width >> 1; int w2_column = UNSHUF_BLOCKSIZE_X * blockIdx.x + threadIdx.x; int w_new2_row = blockIdx.y; int x_map_idx = w_new2_row << 3; uint64_t dst = 0; #pragma unroll for (int i = 0; i < 8; i++) { int source_row = x_map[x_map_idx++]; int w2_row = source_row >> 3; int w2_subrow = source_row & 0x07; int w2_row_shift = w2_subrow << 2; int wnew2_row_shift = i << 2; uint64_t src = w2[w2_row * w2_stride + w2_column]; src >>= w2_row_shift; src &= 0x0000000f0000000f; src <<= wnew2_row_shift; dst |= src; } w_new2[w_new2_row * w2_stride + w2_column] = dst; } void Q4Matrix::make_sequential(const uint32_t* cpu_g_idx) { uint32_t* cuda_new_qweight = NULL; cudaMalloc(&cuda_new_qweight, height / 8 * width * sizeof(uint32_t)); cudaMalloc(&cuda_x_map, height * sizeof(uint32_t)); // TODO: Should probably be allocated in PyTorch uint32_t* cpu_g_idx_map = (uint32_t*) calloc(groups, sizeof(uint32_t)); uint32_t* cpu_x_map = (uint32_t*) malloc(height * sizeof(uint32_t)); uint32_t* cpu_x_map_inv = (uint32_t*) malloc(height * sizeof(uint32_t)); // Group histogram for (int i = 0; i < height; i++) cpu_g_idx_map[cpu_g_idx[i]]++; // Group map for (int i = 0, acc = 0; i < groups; i++) { short tmp = cpu_g_idx_map[i]; cpu_g_idx_map[i] = acc; acc += tmp; } // X map (inverse) for (int row = 0; row < height; row++) { uint32_t target_group = cpu_g_idx[row]; uint32_t target_row = cpu_g_idx_map[target_group]; cpu_g_idx_map[target_group]++; cpu_x_map_inv[row] = target_row; } // X map for (int row = 0; row < height; row++) cpu_x_map[cpu_x_map_inv[row]] = row; // Move to CUDA cudaMemcpyAsync(cuda_x_map, cpu_x_map, height * sizeof(uint32_t), cudaMemcpyHostToDevice); // Rearrange rows in w dim3 threads(UNSHUF_BLOCKSIZE_X, 1, 1); dim3 blocks(width / UNSHUF_BLOCKSIZE_X / 2, height / 8, 1); const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); make_sequential_kernel<<<blocks, threads, 0, stream>>>(cuda_qweight, cuda_new_qweight, cuda_x_map, height / 8, width); // Replace qweights cudaMemcpyAsync(cuda_qweight, cuda_new_qweight, height / 8 * width * sizeof(uint32_t), cudaMemcpyDeviceToDevice); // Cleanup cudaDeviceSynchronize(); cudaFree(cuda_new_qweight); free(cpu_g_idx_map); free(cpu_x_map); free(cpu_x_map_inv); } __global__ void reconstruct_kernel ( const uint32_t* __restrict__ w, half* __restrict__ out, // (y) const half* __restrict__ w_scales, const uint32_t* __restrict__ w_zeros, const int height, const int width, const int groupsize ) { // Start of block int column = RECONS_THREADS_X * blockIdx.x + threadIdx.x; int row = (RECONS_THREADS_Y * blockIdx.y + threadIdx.y) * 8; // Views MatrixView_q4_column w_(w, height, width); MatrixView_half_rw out_(out, height, width); MatrixView_half w_scales_(w_scales, height / groupsize, width); MatrixView_q4_row w_zeros_(w_zeros, height / groupsize, width); // Groupsize version int group = row / groupsize; half w_scale = w_scales_.item(group, column); uint32_t w_zero = (w_zeros_.item(group, column) + 1) & 0x0F; uint32_t w_read = w_.item_uint32_t(row, column); half* out_ptr = out_.item_ptr(row, column); #pragma unroll for (int s = 0; s < 32; s += 4) { half w_item = __hmul(__int2half_rn((int)((w_read >> s) & 0x0f) - w_zero), w_scale); *out_ptr = w_item; out_ptr += out_.width; } } void Q4Matrix::reconstruct(half* out) { dim3 threads(RECONS_THREADS_X, RECONS_THREADS_Y, 1); dim3 blocks ( (width + threads.x - 1) / threads.x, (height / 8 + threads.y - 1) / threads.y, 1 ); const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); reconstruct_kernel<<<blocks, threads, 0, stream>>>(cuda_qweight, out, cuda_scales, cuda_qzeros, height / 8, width, groupsize); }
text-generation-inference/server/exllama_kernels/exllama_kernels/cuda_func/q4_matrix.cu/0
{ "file_path": "text-generation-inference/server/exllama_kernels/exllama_kernels/cuda_func/q4_matrix.cu", "repo_id": "text-generation-inference", "token_count": 2592 }
237
#include "q_matrix.cuh" #include "matrix_view.cuh" #include "util.cuh" #include "quant/qdq_2.cuh" #include "quant/qdq_3.cuh" #include "quant/qdq_4.cuh" #include "quant/qdq_5.cuh" #include "quant/qdq_6.cuh" #include "quant/qdq_8.cuh" #define BLOCK_KN_SIZE 128 #define THREADS_X 32 #define THREADS_Y 32 // Shuffle quantized data on load __global__ void shuffle_kernel ( uint32_t* __restrict__ b_q_weight, const int size_k, const int size_n, const int rows_8, const int rows_6, const int rows_5, const int rows_4, const int rows_3, const int rows_2 ) { int n = blockIdx.x * THREADS_X + threadIdx.x; if (n >= size_n) return; int k = 0; uint32_t* b_ptr = b_q_weight + n; while (k < rows_8) { shuffle_8bit_4 (b_ptr, size_n); b_ptr += 1 * size_n; k += 4; } while (k < rows_6) { shuffle_6bit_16(b_ptr, size_n); b_ptr += 3 * size_n; k += 16; } while (k < rows_5) { shuffle_5bit_32(b_ptr, size_n); b_ptr += 5 * size_n; k += 32; } while (k < rows_4) { shuffle_4bit_8 (b_ptr, size_n); b_ptr += 1 * size_n; k += 8; } while (k < rows_3) { shuffle_3bit_32(b_ptr, size_n); b_ptr += 3 * size_n; k += 32; } while (k < rows_2) { shuffle_2bit_16(b_ptr, size_n); b_ptr += 1 * size_n; k += 16; } } // QMatrix constructor QMatrix::QMatrix ( const int _device, const int _height, const int _width, const int _groups, uint32_t* _q_weight, uint16_t* _q_perm, uint16_t* _q_invperm, uint32_t* _q_scale, half* _q_scale_max, uint16_t* _q_groups, uint16_t* _q_group_map, uint32_t* _gptq_qzeros, half* _gptq_scales, uint32_t* _gptq_g_idx, half* _temp_dq ) : device(_device), height(_height), width(_width), groups(_groups), temp_dq(_temp_dq) { cudaSetDevice(device); failed = false; cuda_q_weight = _q_weight; cuda_q_perm = _q_perm; cuda_q_invperm = _q_invperm; cuda_q_scale = _q_scale; cuda_q_scale_max = _q_scale_max; cuda_q_groups = _q_groups; cuda_q_group_map = _q_group_map; cuda_gptq_qzeros = _gptq_qzeros; cuda_gptq_scales = _gptq_scales; is_gptq = (_gptq_qzeros != NULL); if (is_gptq) { gptq_groupsize = 1; while (gptq_groupsize * groups < height) gptq_groupsize *= 2; } // Create group map rows_8 = 0; rows_6 = 0; rows_5 = 0; rows_4 = 0; rows_3 = 0; rows_2 = 0; if (!is_gptq) { uint16_t* cpu_q_groups = (uint16_t*)calloc(groups * 2, sizeof(uint16_t)); cudaMemcpy(cpu_q_groups, cuda_q_groups, groups * 2 * sizeof(uint16_t), cudaMemcpyDeviceToHost); int row = 0; for (int i = 0; i < groups; i++) { int bits = cpu_q_groups[i * 2]; int rows; if (i < groups - 1) { int qrows = cpu_q_groups[i * 2 + 3] - cpu_q_groups[i * 2 + 1]; rows = qrows * 32 / bits; } else rows = height - row; if (bits == 8) rows_8 += rows; if (bits == 6) rows_6 += rows; if (bits == 5) rows_5 += rows; if (bits == 4) rows_4 += rows; if (bits == 3) rows_3 += rows; if (bits == 2) rows_2 += rows; row += rows; } free(cpu_q_groups); rows_6 += rows_8; rows_5 += rows_6; rows_4 += rows_5; rows_3 += rows_4; rows_2 += rows_3; } else { rows_4 = height; rows_3 = height; rows_2 = height; if (_gptq_g_idx) { if (!make_sequential(_gptq_g_idx)) { failed = true; //printf("FAIL\n"); return; } } } // DBGI(rows_8); // DBGI(rows_6); // DBGI(rows_5); // DBGI(rows_4); // DBGI(rows_3); // DBGI(rows_2); // Shuffle quantized data dim3 blockDim, gridDim; blockDim.x = THREADS_X; blockDim.y = 1; gridDim.x = DIVIDE(width, THREADS_X); gridDim.y = 1; const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); shuffle_kernel<<<gridDim, blockDim, 0, stream>>>(cuda_q_weight, height, width, rows_8, rows_6, rows_5, rows_4, rows_3, rows_2); } QMatrix::~QMatrix() { } // Reconstruct b[k,n] (GPTQ) __global__ void reconstruct_gptq_kernel ( const uint32_t* __restrict__ b_q_weight, const uint16_t* __restrict__ b_q_perm, const uint32_t* __restrict__ b_gptq_qzeros, const half* __restrict__ b_gptq_scales, //const uint16_t* __restrict__ b_q_groups, const int size_k, const int size_n, const int groupsize, const int groups, half* __restrict__ b, const int rows_4 ) { MatrixView_half_rw b_(b, size_k, size_n); MatrixView_q4_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n); MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n); int offset_k = BLOCK_KN_SIZE * blockIdx.y; int offset_n = BLOCK_KN_SIZE * blockIdx.x * 4; int end_k = min(offset_k + BLOCK_KN_SIZE, size_k); // Preload remapping table __shared__ uint16_t perm[BLOCK_KN_SIZE]; int t = threadIdx.x; if (b_q_perm) { if (offset_k + t < size_k) perm[t] = b_q_perm[offset_k + t]; } // Column int n = offset_n + t * 4; if (n >= size_n) return; // Find initial group int group = offset_k / groupsize; int nextgroup = offset_k + groupsize; // b offset int qk = offset_k / (32 / 4); const uint32_t* b_ptr = b_q_weight + qk * size_n + n; // Initial zeros/scale int zeros[4]; half2 scales[4]; half2 z1z16[4][2]; half2 y1y16[4][2]; b_gptq_qzeros_.item4(zeros, group, n); b_gptq_scales_.item4_h2(scales, group, n); dequant_4bit_8_prep_zero((zeros[0] + 1) & 0x0F, z1z16[0], y1y16[0]); dequant_4bit_8_prep_zero((zeros[1] + 1) & 0x0F, z1z16[1], y1y16[1]); dequant_4bit_8_prep_zero((zeros[2] + 1) & 0x0F, z1z16[2], y1y16[2]); dequant_4bit_8_prep_zero((zeros[3] + 1) & 0x0F, z1z16[3], y1y16[3]); __syncthreads(); int k = offset_k; int lk = 0; while (k < end_k) { if (k == nextgroup) { group++; nextgroup += groupsize; b_gptq_qzeros_.item4(zeros, group, n); b_gptq_scales_.item4_h2(scales, group, n); dequant_4bit_8_prep_zero((zeros[0] + 1) & 0x0F, z1z16[0], y1y16[0]); dequant_4bit_8_prep_zero((zeros[1] + 1) & 0x0F, z1z16[1], y1y16[1]); dequant_4bit_8_prep_zero((zeros[2] + 1) & 0x0F, z1z16[2], y1y16[2]); dequant_4bit_8_prep_zero((zeros[3] + 1) & 0x0F, z1z16[3], y1y16[3]); } for (int p = 0; p < 4; p++) { half2 dq[4][4]; const int4* b_ptr4 = (int4*) b_ptr; int4 load_int4 = *b_ptr4; dequant_4bit_8_gptq(load_int4.x, dq[0], z1z16[0], y1y16[0], size_n, false); dequant_4bit_8_gptq(load_int4.y, dq[1], z1z16[1], y1y16[1], size_n, false); dequant_4bit_8_gptq(load_int4.z, dq[2], z1z16[2], y1y16[2], size_n, false); dequant_4bit_8_gptq(load_int4.w, dq[3], z1z16[3], y1y16[3], size_n, false); b_ptr += size_n; //half* dqh = (half*)dq; if (b_q_perm) { for (int j = 0; j < 4; j++) { for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]); b_.set4(perm[lk++], n, __low2half(dq[0][j]), __low2half(dq[1][j]), __low2half(dq[2][j]), __low2half(dq[3][j])); b_.set4(perm[lk++], n, __high2half(dq[0][j]), __high2half(dq[1][j]), __high2half(dq[2][j]), __high2half(dq[3][j])); } } else { for (int j = 0; j < 4; j++) { for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]); b_.set4(offset_k + lk++, n, __low2half(dq[0][j]), __low2half(dq[1][j]), __low2half(dq[2][j]), __low2half(dq[3][j])); b_.set4(offset_k + lk++, n, __high2half(dq[0][j]), __high2half(dq[1][j]), __high2half(dq[2][j]), __high2half(dq[3][j])); } } } k += 32; } } // Reconstruct b[k,n] __global__ void reconstruct_kernel ( const uint32_t* __restrict__ b_q_weight, const uint16_t* __restrict__ b_q_perm, const uint32_t* __restrict__ b_q_scale, const half* __restrict__ b_q_scale_max, const uint16_t* __restrict__ b_q_group_map, const int size_k, const int size_n, //const int groupsize, const int groups, half* __restrict__ b, const int rows_8, const int rows_6, const int rows_5, const int rows_4, const int rows_3, const int rows_2 ) { MatrixView_half_rw b_(b, size_k, size_n); MatrixView_q4_row b_q_scale_(b_q_scale, groups, size_n); int offset_k = BLOCK_KN_SIZE * blockIdx.y; int offset_n = BLOCK_KN_SIZE * blockIdx.x; // Preload remapping table int t = threadIdx.x; __shared__ uint16_t perm[BLOCK_KN_SIZE]; if (offset_k + t < size_k) perm[t] = b_q_perm[offset_k + t]; // Column int n = offset_n + t; if (n >= size_n) return; // Find initial group // int group = offset_k / groupsize; int group = b_q_group_map[offset_k * 2]; int pre_rows_8 = min(rows_8, offset_k); int pre_rows_6 = offset_k > rows_8 ? min(rows_6, offset_k) - rows_8 : 0; int pre_rows_5 = offset_k > rows_6 ? min(rows_5, offset_k) - rows_6 : 0; int pre_rows_4 = offset_k > rows_5 ? min(rows_4, offset_k) - rows_5 : 0; int pre_rows_3 = offset_k > rows_4 ? min(rows_3, offset_k) - rows_4 : 0; int pre_rows_2 = offset_k > rows_3 ? min(rows_2, offset_k) - rows_3 : 0; int qk = 0; qk += pre_rows_8 / 32 * 8; qk += pre_rows_6 / 32 * 6; qk += pre_rows_5 / 32 * 5; qk += pre_rows_4 / 32 * 4; qk += pre_rows_3 / 32 * 3; qk += pre_rows_2 / 32 * 2; const uint32_t* b_ptr = b_q_weight + qk * size_n + n; half qs_h = dq_scale(b_q_scale_.item(group, n), b_q_scale_max[group]); half2 qs_h2 = __halves2half2(qs_h, qs_h); int nextgroup = offset_k + b_q_group_map[offset_k * 2 + 1]; int end_k = min(offset_k + BLOCK_KN_SIZE, size_k); int k = offset_k; int lk = 0; __syncthreads(); while (k < rows_8 && k < end_k) { if (k == nextgroup) { group++; qs_h = dq_scale(b_q_scale_.item(group, n), b_q_scale_max[group]); nextgroup += b_q_group_map[k * 2 + 1]; qs_h2 = __halves2half2(qs_h, qs_h); } for (int p = 0; p < 4; p++) { half2 dq[4]; uint32_t q_0 = *b_ptr; b_ptr += size_n; uint32_t q_1 = *b_ptr; b_ptr += size_n; dequant_8bit_8(q_0, q_1, dq, size_n); for (int j = 0; j < 4; j++) dq[j] = __hmul2(dq[j], qs_h2); half* dqh = (half*) dq; for (int j = 0; j < 8; j++) b_.set(perm[lk++], n, dqh[j]); } k += 32; } while (k < rows_6 && k < end_k) { if (k == nextgroup) { group++; qs_h = dq_scale(b_q_scale_.item(group, n), b_q_scale_max[group]); nextgroup += b_q_group_map[k * 2 + 1]; qs_h2 = __halves2half2(qs_h, qs_h); } for (int p = 0; p < 2; p++) { half2 dq[8]; uint32_t q_0 = *b_ptr; b_ptr += size_n; uint32_t q_1 = *b_ptr; b_ptr += size_n; uint32_t q_2 = *b_ptr; b_ptr += size_n; dequant_6bit_16(q_0, q_1, q_2, dq, size_n); for (int j = 0; j < 8; j++) dq[j] = __hmul2(dq[j], qs_h2); half* dqh = (half*) dq; for (int j = 0; j < 16; j++) b_.set(perm[lk++], n, dqh[j]); } k += 32; } while (k < rows_5 && k < end_k) { if (k == nextgroup) { group++; qs_h = dq_scale(b_q_scale_.item(group, n), b_q_scale_max[group]); nextgroup += b_q_group_map[k * 2 + 1]; qs_h2 = __halves2half2(qs_h, qs_h); } for (int p = 0; p < 1; p++) { half2 dq[16]; uint32_t q_0 = *b_ptr; b_ptr += size_n; uint32_t q_1 = *b_ptr; b_ptr += size_n; uint32_t q_2 = *b_ptr; b_ptr += size_n; uint32_t q_3 = *b_ptr; b_ptr += size_n; uint32_t q_4 = *b_ptr; b_ptr += size_n; dequant_5bit_32(q_0, q_1, q_2, q_3, q_4, dq, size_n); for (int j = 0; j < 16; j++) dq[j] = __hmul2(dq[j], qs_h2); half* dqh = (half*) dq; for (int j = 0; j < 32; j++) b_.set(perm[lk++], n, dqh[j]); } k += 32; } while (k < rows_4 && k < end_k) { if (k == nextgroup) { group++; qs_h = dq_scale(b_q_scale_.item(group, n), b_q_scale_max[group]); nextgroup += b_q_group_map[k * 2 + 1]; qs_h2 = __halves2half2(qs_h, qs_h); } for (int p = 0; p < 4; p++) { half2 dq[4]; uint32_t q_0 = *b_ptr; b_ptr += size_n; dequant_4bit_8(q_0, dq, size_n); for (int j = 0; j < 4; j++) dq[j] = __hmul2(dq[j], qs_h2); half* dqh = (half*) dq; for (int j = 0; j < 8; j++) b_.set(perm[lk++], n, dqh[j]); } k += 32; } while (k < rows_3 && k < end_k) { if (k == nextgroup) { group++; qs_h = dq_scale(b_q_scale_.item(group, n), b_q_scale_max[group]); nextgroup += b_q_group_map[k * 2 + 1]; qs_h2 = __halves2half2(qs_h, qs_h); } for (int p = 0; p < 1; p++) { half2 dq[16]; uint32_t q_0 = *b_ptr; b_ptr += size_n; uint32_t q_1 = *b_ptr; b_ptr += size_n; uint32_t q_2 = *b_ptr; b_ptr += size_n; dequant_3bit_32(q_0, q_1, q_2, dq, size_n); for (int j = 0; j < 16; j++) dq[j] = __hmul2(dq[j], qs_h2); half* dqh = (half*) dq; for (int j = 0; j < 32; j++) b_.set(perm[lk++], n, dqh[j]); } k += 32; } while (k < rows_2 && k < end_k) { if (k == nextgroup) { group++; qs_h = dq_scale(b_q_scale_.item(group, n), b_q_scale_max[group]); nextgroup += b_q_group_map[k * 2 + 1]; qs_h2 = __halves2half2(qs_h, qs_h); } for (int p = 0; p < 1; p++) { half2 dq[8]; uint32_t q_0 = *b_ptr; b_ptr += size_n; dequant_2bit_16(q_0, dq, size_n); for (int j = 0; j < 8; j++) dq[j] = __hmul2(dq[j], qs_h2); half* dqh = (half*) dq; for (int j = 0; j < 16; j++) b_.set(perm[lk++], n, dqh[j]); } k += 16; } } void QMatrix::reconstruct(half* out) { dim3 blockDim, gridDim; blockDim.x = BLOCK_KN_SIZE; blockDim.y = 1; gridDim.y = DIVIDE(height, BLOCK_KN_SIZE); const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); if (!is_gptq) { gridDim.x = DIVIDE(width, BLOCK_KN_SIZE); reconstruct_kernel<<<gridDim, blockDim, 0, stream>>> ( cuda_q_weight, cuda_q_perm, cuda_q_scale, cuda_q_scale_max, cuda_q_group_map, height, width, //groupsize, groups, out, rows_8, rows_6, rows_5, rows_4, rows_3, rows_2 ); } else { gridDim.x = DIVIDE(width, BLOCK_KN_SIZE * 4); reconstruct_gptq_kernel<<<gridDim, blockDim, 0, stream>>> ( cuda_q_weight, cuda_q_perm, cuda_gptq_qzeros, cuda_gptq_scales, //const uint16_t* __restrict__ b_q_groups, height, width, gptq_groupsize, groups, out, rows_4 ); } } __global__ void make_sequential_kernel ( const uint32_t* __restrict__ w, uint32_t* __restrict__ w_new, const uint16_t* __restrict__ q_perm, const int w_height, const int w_width ) { const uint64_t* w2 = (uint64_t*) w; uint64_t* w_new2 = (uint64_t*) w_new; int w2_stride = w_width >> 1; int w2_column = THREADS_X * blockIdx.x + threadIdx.x; if (w2_column >= w2_stride) return; int w_new2_row = blockIdx.y; int q_perm_idx = w_new2_row << 3; uint64_t dst = 0; #pragma unroll for (int i = 0; i < 8; i++) { int source_row = q_perm[q_perm_idx++]; int w2_row = source_row >> 3; int w2_subrow = source_row & 0x07; int w2_row_shift = w2_subrow << 2; int wnew2_row_shift = i << 2; uint64_t src = w2[w2_row * w2_stride + w2_column]; src >>= w2_row_shift; src &= 0x0000000f0000000f; src <<= wnew2_row_shift; dst |= src; } w_new2[w_new2_row * w2_stride + w2_column] = dst; } bool QMatrix::make_sequential(const uint32_t* cpu_g_idx) { const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); uint32_t* cuda_new_qweight = NULL; cudaError_t err = cudaMalloc(&cuda_new_qweight, height / 8 * width * sizeof(uint32_t)); if (err != cudaSuccess) { cudaError_t cuda_status = cudaGetLastError(); // Clear error return false; } uint32_t* cpu_g_idx_map = (uint32_t*) calloc(groups, sizeof(uint32_t)); uint32_t* cpu_x_map = (uint32_t*) malloc(height * sizeof(uint32_t)); uint32_t* cpu_x_map_inv = (uint32_t*) malloc(height * sizeof(uint32_t)); // Group histogram for (int i = 0; i < height; i++) cpu_g_idx_map[cpu_g_idx[i]]++; // Group map for (int i = 0, acc = 0; i < groups; i++) { short tmp = cpu_g_idx_map[i]; cpu_g_idx_map[i] = acc; acc += tmp; } // X map (inverse) for (int row = 0; row < height; row++) { uint32_t target_group = cpu_g_idx[row]; uint32_t target_row = cpu_g_idx_map[target_group]; cpu_g_idx_map[target_group]++; cpu_x_map_inv[row] = target_row; } // X map for (int row = 0; row < height; row++) cpu_x_map[cpu_x_map_inv[row]] = row; // Reduce to uint16_t uint16_t* cpu_x_map16 = (uint16_t*)cpu_x_map; uint16_t* cpu_x_map_inv16 = (uint16_t*)cpu_x_map_inv; for (int row = 0; row < height; row++) cpu_x_map16[row] = (uint16_t) cpu_x_map[row]; for (int row = 0; row < height; row++) cpu_x_map_inv16[row] = (uint16_t) cpu_x_map_inv[row]; // Move to CUDA cudaMemcpyAsync(cuda_q_perm, cpu_x_map16, height * sizeof(uint16_t), cudaMemcpyHostToDevice); cudaMemcpyAsync(cuda_q_invperm, cpu_x_map_inv16, height * sizeof(uint16_t), cudaMemcpyHostToDevice); // Rearrange rows in w dim3 blockDim, gridDim; blockDim.x = THREADS_X; blockDim.y = 1; gridDim.x = DIVIDE(width, THREADS_X); gridDim.y = height / 8; make_sequential_kernel<<<gridDim, blockDim, 0, stream>>> ( cuda_q_weight, cuda_new_qweight, cuda_q_perm, height / 8, width ); // Replace qweights cudaMemcpyAsync(cuda_q_weight, cuda_new_qweight, height / 8 * width * sizeof(uint32_t), cudaMemcpyDeviceToDevice); // Cleanup cudaDeviceSynchronize(); cudaFree(cuda_new_qweight); free(cpu_g_idx_map); free(cpu_x_map); free(cpu_x_map_inv); return true; }
text-generation-inference/server/exllamav2_kernels/exllamav2_kernels/cuda/q_matrix.cu/0
{ "file_path": "text-generation-inference/server/exllamav2_kernels/exllamav2_kernels/cuda/q_matrix.cu", "repo_id": "text-generation-inference", "token_count": 10524 }
238
# Origin: https://github.com/predibase/lorax # Path: lorax/server/lorax_server/adapters/config.py # License: Apache License Version 2.0, January 2004 from abc import ABC, abstractmethod from dataclasses import dataclass from typing import Dict, Set, Tuple import torch from text_generation_server.adapters.weights import AdapterWeights @dataclass class ModuleMap: module_name: str module_weights: Dict[str, Tuple[torch.Tensor, str]] @dataclass class AdapterConfig(ABC): base_model_name_or_path: str @abstractmethod def map_weights_for_model( self, adapter_weights: Dict[int, AdapterWeights], weight_names: Tuple[str], ) -> Tuple[ModuleMap, Set[str]]: pass
text-generation-inference/server/text_generation_server/adapters/config.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/adapters/config.py", "repo_id": "text-generation-inference", "token_count": 275 }
239
from dataclasses import dataclass import bitsandbytes as bnb import torch from bitsandbytes.nn import Int8Params, Params4bit from text_generation_server.utils.weights import UnquantizedWeight @dataclass class BNBWeight(UnquantizedWeight): weight: torch.Tensor def get_linear(self, bias: torch.Tensor): return Linear8bitLt(self.weight, bias, has_fp16_weights=False, threshold=6.0) class Linear8bitLt(torch.nn.Module): def __init__( self, weight, bias, has_fp16_weights=True, memory_efficient_backward=False, threshold=0.0, index=None, ): super().__init__() assert ( not memory_efficient_backward ), "memory_efficient_backward is no longer required and the argument is deprecated in 0.37.0 and will be removed in 0.39.0" self.state = bnb.MatmulLtState() self.index = index # Necessary for stacked layers self.state.threshold = threshold self.state.has_fp16_weights = has_fp16_weights self.state.memory_efficient_backward = memory_efficient_backward if threshold > 0.0 and not has_fp16_weights: self.state.use_pool = True self.weight = Int8Params( weight.data, has_fp16_weights=has_fp16_weights, requires_grad=has_fp16_weights, ) self.weight.cuda(weight.device) self.bias = bias def init_8bit_state(self): self.state.CB = self.weight.CB self.state.SCB = self.weight.SCB self.weight.CB = None self.weight.SCB = None def forward(self, x: torch.Tensor): self.state.is_training = self.training if self.weight.CB is not None: self.init_8bit_state() # weights are cast automatically as Int8Params, but the bias has to be cast manually if self.bias is not None and self.bias.dtype != x.dtype: self.bias.data = self.bias.data.to(x.dtype) out = bnb.matmul(x, self.weight, bias=self.bias, state=self.state) if not self.state.has_fp16_weights: if self.state.CB is not None and self.state.CxB is not None: # we converted 8-bit row major to turing/ampere format in the first inference pass # we no longer need the row-major weight del self.state.CB self.weight.data = self.state.CxB return out @dataclass class BNBFP4Weight(UnquantizedWeight): weight: torch.Tensor def get_linear(self, bias: torch.Tensor): return Linear4bit(self.weight, bias, quant_type="fp4") @dataclass class BNBNF4Weight(UnquantizedWeight): weight: torch.Tensor def get_linear(self, bias: torch.Tensor): return Linear4bit(self.weight, bias, quant_type="nf4") class Linear4bit(torch.nn.Module): def __init__(self, weight, bias, quant_type): super().__init__() self.weight = Params4bit( weight.data, requires_grad=False, compress_statistics=True, quant_type=quant_type, ) self.compute_dtype = None self.weight.cuda(weight.device) self.bias = bias def forward(self, x: torch.Tensor): # weights are cast automatically as Int8Params, but the bias has to be cast manually if self.bias is not None and self.bias.dtype != x.dtype: self.bias.data = self.bias.data.to(x.dtype) if getattr(self.weight, "quant_state", None) is None: print( "FP4 quantization state not initialized. Please call .cuda() or .to(device) on the LinearFP4 layer first." ) inp_dtype = x.dtype if self.compute_dtype is not None: x = x.to(self.compute_dtype) bias = None if self.bias is None else self.bias.to(self.compute_dtype) out = bnb.matmul_4bit( x, self.weight.t(), bias=bias, quant_state=self.weight.quant_state ) out = out.to(inp_dtype) return out
text-generation-inference/server/text_generation_server/layers/bnb.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/layers/bnb.py", "repo_id": "text-generation-inference", "token_count": 1825 }
240
from typing import Optional import torch import torch.nn as nn from loguru import logger from text_generation_server.layers.fp8 import fp8_quantize from text_generation_server.layers.marlin.gptq import _check_valid_shape from text_generation_server.layers.marlin.util import ( _check_marlin_kernels, permute_scales, ) from text_generation_server.utils.log import log_once try: import marlin_kernels except ImportError: marlin_kernels = None MARLIN_TILE_SIZE = 16 class GPTQMarlinFP8Linear(nn.Module): """ FP8 GPTQ-Marlin linear layer. """ def __init__( self, qweight: torch.Tensor, scales: torch.Tensor, bias: Optional[torch.Tensor], ) -> None: super().__init__() _check_marlin_kernels() assert marlin_kernels is not None log_once(logger.info, "GPU does not support FP8, using Marlin FP8 kernel") scales = scales.unsqueeze(0) if scales.shape[1] == 1: out_features, in_features = qweight.shape scales = scales.repeat(1, out_features) qweight, scales = repack_fp8_for_marlin(qweight, scales) in_features = qweight.shape[0] * MARLIN_TILE_SIZE out_features = scales.shape[1] _check_valid_shape(in_features=in_features, out_features=out_features) self.qweight = qweight self.scales = scales self.bias = bias if bias is not None else None self.workspace = torch.zeros( out_features // 64 * 16, dtype=torch.int, device=qweight.device ) @classmethod def from_unquant(cls, weight, bias, dtype): qweight, scales = fp8_quantize(weight) return cls(qweight=qweight, scales=scales.to(dtype), bias=bias) @classmethod def from_fp8(cls, weight, scale, _input_scale, bias, dtype): return cls(qweight=weight, scales=scale.to(dtype), bias=bias) def forward(self, A: torch.Tensor) -> torch.Tensor: assert marlin_kernels is not None A_flat = A.view(-1, A.shape[-1]) C = marlin_kernels.fp8_marlin_gemm( A_flat, self.qweight, self.scales, self.workspace, 8, A_flat.shape[0], self.scales.shape[1], A_flat.shape[1], ) C = C.reshape(A.shape[:-1] + (self.scales.shape[1],)) if self.bias is not None: C += self.bias return C def pack_fp8_as_int32(fp8_tensor: torch.Tensor) -> torch.Tensor: """ Repack FP8 weights to gptq format (packed int32 elements). """ assert fp8_tensor.dtype == torch.float8_e4m3fn if fp8_tensor.shape[0] % 4 != 0: raise ValueError( f"Leading tensor dimension is not divisable by 4: {fp8_tensor.shape[0]}" ) # Reshape to prepare for packing reshaped = fp8_tensor.reshape(-1, 4, *fp8_tensor.shape[1:]) # Convert fp8 to uint8 (byte) representation byte_tensor = reshaped.view(torch.uint8) # Pack 4 uint8 values into one int32 packed = torch.zeros( fp8_tensor.shape[0] // 4, fp8_tensor.shape[1], dtype=torch.int32, device=fp8_tensor.device, ) for i in range(4): packed.bitwise_or_(byte_tensor[:, i].to(torch.int32) << i * 8) return packed def repack_fp8_for_marlin(weight: torch.Tensor, scales: torch.Tensor): """ Repack FP8 tensor for GPTQ-Marlin. """ out_features, in_features = weight.shape # Torch linear layers weights with shape [out_features, in_features], # GPTQ-quantized weights use [in_feateres/pack_factor, in_features], # so transpose before packing. qweight = pack_fp8_as_int32(weight.t()) perm = torch.empty(0, dtype=torch.int, device=qweight.device) repacked = marlin_kernels.gptq_marlin_repack( qweight, perm, in_features, out_features, 8 ) scales = permute_scales(scales) return repacked, scales
text-generation-inference/server/text_generation_server/layers/marlin/fp8.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/layers/marlin/fp8.py", "repo_id": "text-generation-inference", "token_count": 1787 }
241
# coding=utf-8 # Copyright 2022 HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch import torch.distributed from torch import nn from transformers.activations import ACT2FN from transformers.configuration_utils import PretrainedConfig from typing import Optional, List, Tuple, Any from text_generation_server.utils.import_utils import SYSTEM if SYSTEM != "ipex": from vllm.model_executor.layers.fused_moe import fused_moe from text_generation_server.layers.attention import ( paged_attention, attention, reshape_and_cache, Seqlen, ) from text_generation_server.layers import ( FastLinear, TensorParallelRowLinear, TensorParallelColumnLinear, TensorParallelEmbedding, SpeculativeHead, get_linear, ) from text_generation_server.layers.rotary import ( PositionRotaryEmbedding, ) from text_generation_server.layers.layernorm import ( FastLayerNorm, ) class DbrxAttentionConfig(PretrainedConfig): def __init__( self, attn_pdrop: float = 0, clip_qkv: Optional[float] = None, kv_n_heads: int = 1, rope_theta: float = 10000.0, **kwargs: Any, ): super().__init__(**kwargs) self.attn_pdrop = attn_pdrop self.clip_qkv = clip_qkv self.kv_n_heads = kv_n_heads self.rope_theta = rope_theta for k in ["model_type"]: if k in kwargs: kwargs.pop(k) if len(kwargs) != 0: raise ValueError(f"Found unknown {kwargs=}") class DbrxFFNConfig(PretrainedConfig): def __init__( self, ffn_act_fn: Optional[dict] = None, ffn_hidden_size: int = 3584, moe_num_experts: int = 4, moe_top_k: int = 1, moe_jitter_eps: Optional[float] = None, moe_loss_weight: float = 0.01, moe_normalize_expert_weights: Optional[float] = 1, uniform_expert_assignment: bool = False, **kwargs: Any, ): super().__init__() if ffn_act_fn is None: ffn_act_fn = {"name": "silu"} self.ffn_act_fn = ffn_act_fn self.ffn_hidden_size = ffn_hidden_size self.moe_num_experts = moe_num_experts self.moe_top_k = moe_top_k self.moe_jitter_eps = moe_jitter_eps self.moe_loss_weight = moe_loss_weight self.moe_normalize_expert_weights = moe_normalize_expert_weights self.uniform_expert_assignment = uniform_expert_assignment if uniform_expert_assignment: raise ValueError("`uniform_expert_assignment = True` is not supported") for k in ["model_type"]: if k in kwargs: kwargs.pop(k) if len(kwargs) != 0: raise ValueError(f"Found unknown {kwargs=}") class DbrxConfig(PretrainedConfig): attribute_map = { "hidden_size": "d_model", "num_attention_heads": "n_heads", "num_hidden_layers": "n_layers", } def __init__( self, d_model: int = 2048, n_heads: int = 16, n_layers: int = 24, max_seq_len: int = 2048, vocab_size: int = 32000, resid_pdrop: float = 0.0, emb_pdrop: float = 0.0, attn_config: Optional[DbrxAttentionConfig] = None, ffn_config: Optional[DbrxFFNConfig] = None, use_cache: bool = True, initializer_range: float = 0.02, output_router_logits: bool = False, router_aux_loss_coef: float = 0.05, **kwargs: Any, ): if attn_config is None: self.attn_config = DbrxAttentionConfig() elif isinstance(attn_config, dict): self.attn_config = DbrxAttentionConfig(**attn_config) else: self.attn_config = attn_config if ffn_config is None: self.ffn_config = DbrxFFNConfig() elif isinstance(ffn_config, dict): self.ffn_config = DbrxFFNConfig(**ffn_config) else: self.ffn_config = ffn_config self.d_model = d_model self.n_heads = n_heads self.n_layers = n_layers self.max_seq_len = max_seq_len self.vocab_size = vocab_size self.resid_pdrop = resid_pdrop self.emb_pdrop = emb_pdrop self.use_cache = use_cache self.initializer_range = initializer_range self.output_router_logits = output_router_logits self.router_aux_loss_coef = router_aux_loss_coef tie_word_embeddings = kwargs.pop("tie_word_embeddings", False) if tie_word_embeddings: raise ValueError("tie_word_embeddings is not supported for Dbrx models.") super().__init__( tie_word_embeddings=tie_word_embeddings, **kwargs, ) @property def num_key_value_heads(self): # We can't use the attribute map, since this the number of KV # heads is not top-level. return self.attn_config.kv_n_heads def promote_scalar(x: torch.Tensor) -> torch.Tensor: return x.view(1) if len(x.size()) == 0 else x def load_attention(config, prefix, weights): return TensorParallelColumnLinear.load_qkv( config, prefix=f"{prefix}.Wqkv", weights=weights, bias=False, num_heads=config.n_heads, num_key_value_heads=config.attn_config.kv_n_heads, ) def _load_experts(config, prefix, weights): world_size = weights.process_group.size() rank = weights.process_group.rank() assert ( config.ffn_config.ffn_hidden_size % world_size == 0 ), f"The chosen size {config.ffn_config.ffn_hidden_size} is not compatible with sharding on {world_size} shards" expert_size = config.ffn_config.ffn_hidden_size block_size = expert_size // world_size start = rank * block_size stop = (rank + 1) * block_size tensor = torch.empty( (config.ffn_config.moe_num_experts * block_size, config.d_model), dtype=weights.dtype, device=weights.device, ) slice_ = weights._get_slice(f"{prefix}") for i in range(config.ffn_config.moe_num_experts): offset = i * expert_size expert_slice = slice_[start + offset : stop + offset] tensor[i * block_size : (i + 1) * block_size] = expert_slice.to( dtype=weights.dtype ).to(device=weights.device) return tensor def _load_experts_quantized(config, prefix, weights, cls): world_size = weights.process_group.size() rank = weights.process_group.rank() assert ( config.ffn_config.ffn_hidden_size % world_size == 0 ), f"The chosen size {config.ffn_config.ffn_hidden_size} is not compatible with sharding on {world_size} shards" expert_size = config.ffn_config.ffn_hidden_size block_size = expert_size // world_size start = rank * block_size stop = (rank + 1) * block_size slice_ = weights._get_slice(f"{prefix}") experts = [] for i in range(config.ffn_config.moe_num_experts): if config.quantize in ["gptq", "awq"]: raise NotImplementedError( "Dbrx does not support gptq/awq quantization yet." ) else: offset = i * expert_size expert_slice = ( slice_[start + offset : stop + offset] .to(dtype=weights.dtype) .to(device=weights.device) ) if cls == TensorParallelRowLinear: expert_slice = expert_slice.t().contiguous() linear = get_linear(expert_slice, None) experts.append(cls(linear, weights.process_group)) else: linear = get_linear(expert_slice, None) experts.append(cls(linear)) return experts class DbrxAttention(torch.nn.Module): def __init__( self, prefix: str, config, weights, ): super().__init__() self.clip_qkv = config.attn_config.clip_qkv self.num_heads = config.n_heads self.hidden_size = config.d_model self.head_size = self.hidden_size // self.num_heads self.rotary_emb = PositionRotaryEmbedding.static( config=config, dim=self.head_size, base=config.attn_config.rope_theta, device=weights.device, ) self.softmax_scale = self.head_size**-0.5 if self.num_heads % weights.process_group.size() != 0: raise ValueError( f"`num_heads` must be divisible by `num_shards` (got `num_heads`: {self.num_heads} " f"and `num_shards`: {weights.process_group.size()}" ) self.num_heads = self.num_heads // weights.process_group.size() self.num_key_value_heads = ( config.attn_config.kv_n_heads // weights.process_group.size() ) self.query_key_value = load_attention(config, prefix, weights) self.o_proj = TensorParallelRowLinear.load( config, prefix=f"{prefix}.out_proj", weights=weights, bias=False, ) self.num_groups = self.num_heads // self.num_key_value_heads self.kv_head_mapping = torch.arange( 0, self.num_key_value_heads, dtype=torch.int32, device=weights.device ).repeat_interleave(self.num_groups) def forward( self, hidden_states, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, ): qkv = self.query_key_value(hidden_states) if self.clip_qkv is not None: qkv = qkv.clamp(min=-self.clip_qkv, max=self.clip_qkv) query, kv = qkv.split( [ self.head_size * self.num_heads, 2 * self.head_size * self.num_key_value_heads, ], dim=1, ) query = query.view(-1, self.num_heads, self.head_size) kv = kv.view(-1, 2, self.num_key_value_heads, self.head_size) self.rotary_emb(query, torch.select(kv, dim=1, index=0), cos, sin) reshape_and_cache(kv[:, 0], kv[:, 1], kv_cache[0], kv_cache[1], slots) # Prefill if cu_seqlen_prefill is not None: # flash attention attn_output = attention( query, kv_cache[0], kv_cache[1], seqlen, block_tables, self.softmax_scale, ) # Decode else: attn_output = paged_attention( query, kv_cache[0], kv_cache[1], self.kv_head_mapping, self.softmax_scale, block_tables, seqlen, max_s, ) return self.o_proj(attn_output.view(-1, self.num_heads * self.head_size)) class DbrxNormAttentionNorm(nn.Module): def __init__( self, prefix: str, config, weights, ): super().__init__() self.norm_1 = FastLayerNorm.load_no_bias( prefix=f"{prefix}.norm_1", weights=weights, eps=1e-5 ) self.self_attn = DbrxAttention( prefix=f"{prefix}.attn", config=config, weights=weights ) self.norm_2 = FastLayerNorm.load_no_bias( prefix=f"{prefix}.norm_2", weights=weights, eps=1e-5, ) def forward( self, hidden_states, residual, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, ): normed_hidden_states, res = self.norm_1(hidden_states, residual) # Self Attention attn_output = self.self_attn( normed_hidden_states, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, ) # faster post attention rms norm normed_attn_res_output, attn_res = self.norm_2(attn_output, res) return normed_attn_res_output, attn_res @torch.jit.script def select_experts( gate_logits: torch.Tensor, top_k: int, moe_normalize_expert_weights: int ): # all_probs: (sequence_length, n_experts) and upcast for softmax all_probs = torch.nn.functional.softmax(gate_logits, dim=1, dtype=torch.float) # weights, selected_experts: (sequence_length, top-k) weights, selected_experts = torch.topk(all_probs, top_k, dim=-1) if moe_normalize_expert_weights: weights = weights / torch.norm( weights, p=moe_normalize_expert_weights, dim=-1, keepdim=True ) weights = weights.view(-1) selected_experts = selected_experts.view(-1) return selected_experts, weights @torch.jit.script def round_up(x: torch.Tensor, value: int): return torch.div(x + (value - 1), value, rounding_mode="trunc") * value class BlockSparseMoE(nn.Module): def __init__(self, prefix, config: DbrxConfig, weights): super().__init__() self.moe_normalize_expert_weights = ( config.ffn_config.moe_normalize_expert_weights ) self.hidden_dim = config.d_model self.ffn_dim = config.ffn_config.ffn_hidden_size // weights.process_group.size() self.num_experts = config.ffn_config.moe_num_experts self.top_k = config.ffn_config.moe_top_k act = config.ffn_config.ffn_act_fn["name"] if "gelu" in act: self.act = lambda x: torch.nn.functional.gelu( x, approximate=( "tanh" if act in ["gelu_fast", "gelu_pytorch_tanh"] else "none" ), ) elif "silu" in act: self.act = torch.nn.functional.silu else: self.act = ACT2FN[act] # gating self.gate = FastLinear.load( config, f"{prefix}.router.layer", weights, bias=False ) # merged expert weights, all of size (n_experts * ffn_dim, hidden_dim) w1 = _load_experts(config, f"{prefix}.experts.mlp.w1", weights).view( self.num_experts, self.ffn_dim, self.hidden_dim ) v1 = _load_experts(config, f"{prefix}.experts.mlp.v1", weights).view( self.num_experts, self.ffn_dim, self.hidden_dim ) self.wv1 = torch.cat([w1, v1], dim=1) self.w2 = ( _load_experts(config, f"{prefix}.experts.mlp.w2", weights) .view(self.num_experts, self.ffn_dim, self.hidden_dim) .transpose(1, 2) .contiguous() ) self.process_group = weights.process_group def forward(self, x: torch.Tensor) -> torch.Tensor: # router_logits: (num_tokens, n_experts) router_logits = self.gate(x) out = fused_moe( x, self.wv1, self.w2, router_logits, self.top_k, renormalize=self.moe_normalize_expert_weights, inplace=True, ) # Reduce sum if self.process_group.size() > 1: torch.distributed.all_reduce(out, group=self.process_group) return out.view(*x.shape) class DenseMoE(nn.Module): def __init__(self, prefix, config: DbrxConfig, weights): super().__init__() self.moe_normalize_expert_weights = ( config.ffn_config.moe_normalize_expert_weights ) self.hidden_dim = config.d_model self.ffn_dim = config.ffn_config.ffn_hidden_size // weights.process_group.size() self.num_experts = config.ffn_config.moe_num_experts self.top_k = config.ffn_config.moe_top_k act = config.ffn_config.ffn_act_fn["name"] if "gelu" in act: self.act = lambda x: torch.nn.functional.gelu( x, approximate=( "tanh" if act in ["gelu_fast", "gelu_pytorch_tanh"] else "none" ), ) elif "silu" in act: self.act = torch.nn.functional.silu else: self.act = ACT2FN[act] # gating self.gate = FastLinear.load( config, f"{prefix}.router.layer", weights, bias=False ) self.w1 = _load_experts_quantized( config, prefix=f"{prefix}.experts.mlp.w1", weights=weights, cls=TensorParallelColumnLinear, ) self.w2 = _load_experts_quantized( config, prefix=f"{prefix}.experts.mlp.w2", weights=weights, cls=TensorParallelRowLinear, ) self.v1 = _load_experts_quantized( config, prefix=f"{prefix}.experts.mlp.v1", weights=weights, cls=TensorParallelColumnLinear, ) self.process_group = weights.process_group def forward(self, x: torch.Tensor) -> torch.Tensor: """ x: (sequence_length, model_dim) gate_logits: (sequence_length, n_experts) """ # optional reshape input_shape = x.shape x = x.view(-1, input_shape[-1]) # gate_logits: (sequence_length, n_experts) gate_logits = self.gate(x) # all_probs: (sequence_length, n_experts) and upcast for softmax weights = torch.nn.functional.softmax(gate_logits, dim=1, dtype=torch.float) if self.top_k < self.num_experts: _, not_selected_experts = torch.topk( weights, self.num_experts - self.top_k, largest=False, sorted=False, dim=1, ) # Mask not selected experts weights.scatter_(1, not_selected_experts, 0) # Re-normalize if self.moe_normalize_expert_weights: weights = weights / torch.norm( weights, p=self.moe_normalize_expert_weights, dim=-1, keepdim=True ) weights = weights.to(x.dtype) # Final output tensor out = x.new_zeros(x.shape[0], self.hidden_dim) for i in range(self.num_experts): h = self.act(self.w1[i](x)) * self.v1[i](x) h = self.w2[i](h, reduce=False) # Add expert output to out with masking out += h * weights[:, i].view(-1, 1) # Reduce sum if self.process_group.size() > 1: torch.distributed.all_reduce(out, group=self.process_group) return out class DbrxLayer(nn.Module): def __init__(self, prefix: str, layer_id, config, weights): super().__init__() prefix = f"{prefix}.blocks.{layer_id}" self.attn = DbrxNormAttentionNorm( prefix=f"{prefix}.norm_attn_norm", config=config, weights=weights ) moe_cls = BlockSparseMoE if config.quantize is None else DenseMoE self.moe = moe_cls(f"{prefix}.ffn", config, weights) def forward( self, hidden_states, residual, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, ): # Self Attention attn_output, attn_res = self.attn( hidden_states, residual, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, ) moe_output = self.moe(attn_output) return moe_output, attn_res class DbrxModel(torch.nn.Module): def __init__(self, prefix: str, config, weights): super().__init__() self.embed_tokens = TensorParallelEmbedding( prefix=f"{prefix}.wte", weights=weights ) self.layers = nn.ModuleList( [ DbrxLayer( prefix, layer_id, config, weights, ) for layer_id in range(config.n_layers) ] ) self.norm = FastLayerNorm.load_no_bias( prefix=f"{prefix}.norm_f", weights=weights, eps=1e-5 ) self.head_size = self.layers[0].attn.self_attn.head_size self.num_heads = self.layers[0].attn.self_attn.num_heads self.num_key_value_heads = self.layers[0].attn.self_attn.num_key_value_heads def forward( self, input_ids: torch.Tensor, position_ids: torch.Tensor, cu_seqlen_prefill: Optional[torch.Tensor], kv_cache: List[Tuple[torch.Tensor, torch.Tensor]], block_tables: torch.Tensor, slots: torch.Tensor, seqlen: Seqlen, max_s: int, ) -> torch.Tensor: hidden_states = self.embed_tokens(input_ids) # Get rotary cos and sin for this forward # Avoid to index in each layer cos, sin = self.layers[0].attn.self_attn.rotary_emb.get_cos_sin( position_ids, max_s, hidden_states.dtype ) residual = None for i, layer in enumerate(self.layers): hidden_states, residual = layer( hidden_states, residual, cos, sin, cu_seqlen_prefill, kv_cache[i], block_tables, slots, seqlen, max_s, ) hidden_states, _ = self.norm(hidden_states, residual) return hidden_states class FlashDbrxForCausalLM(torch.nn.Module): def __init__(self, prefix: str, config, weights): super().__init__() if not prefix: prefix = "transformer" else: prefix = f"{prefix}.transformer" self.model = DbrxModel(prefix, config, weights) self.lm_head = SpeculativeHead.load( config, prefix="lm_head", weights=weights, ) def forward( self, input_ids: torch.Tensor, position_ids: torch.Tensor, cu_seqlen_prefill: Optional[torch.Tensor], kv_cache: List[Tuple[torch.Tensor, torch.Tensor]], block_tables: torch.Tensor, slots: torch.Tensor, seqlen: Seqlen, max_s: int, prefill_cache_indices: Optional[torch.Tensor], lm_head_indices: Optional[torch.Tensor] = None, adapter_data: Optional[torch.Tensor] = None, ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: hidden_states = self.model( input_ids, position_ids, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, ) if lm_head_indices is not None: hidden_states = hidden_states[lm_head_indices] logits, speculative_logits = self.lm_head(hidden_states) return logits, speculative_logits
text-generation-inference/server/text_generation_server/models/custom_modeling/flash_dbrx_modeling.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/models/custom_modeling/flash_dbrx_modeling.py", "repo_id": "text-generation-inference", "token_count": 11877 }
242
# coding=utf-8 # Copyright 2024 the HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ PyTorch Idefics2 model.""" from typing import List, Optional, Tuple import torch import torch.utils.checkpoint from torch import nn import math from transformers.activations import ACT2FN from text_generation_server.models.custom_modeling.vlm import ( load_text_model, ) from text_generation_server.layers.attention import Seqlen from transformers.modeling_attn_mask_utils import _prepare_4d_attention_mask from text_generation_server.layers import ( TensorParallelColumnLinear, TensorParallelEmbedding, TensorParallelRowLinear, ) from text_generation_server.utils.weights import DefaultWeightsLoader, UnquantizedWeight def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: """ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) """ batch, num_key_value_heads, slen, head_dim = hidden_states.shape if n_rep == 1: return hidden_states hidden_states = hidden_states[:, :, None, :, :].expand( batch, num_key_value_heads, n_rep, slen, head_dim ) return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) class Idefics2VisionEmbeddings(nn.Module): """ This is a modified version of `siglip.modelign_siglip.SiglipVisionEmbeddings` to enable images of variable resolution. The modifications are adapted from [Patch n' Pack: NaViT, a Vision Transformer for any Aspect Ratio and Resolution](https://arxiv.org/abs/2307.06304) which allows treating images in their native aspect ratio and without the need to resize them to the same fixed size. In particular, we start from the original pre-trained SigLIP model (which uses images of fixed-size square images) and adapt it by training on images of variable resolutions. """ def __init__(self, prefix, config, weights): super().__init__() self.embed_dim = config.hidden_size self.image_size = config.image_size self.patch_size = config.patch_size self.patch_embedding = nn.Conv2d( in_channels=config.num_channels, out_channels=self.embed_dim, kernel_size=self.patch_size, stride=self.patch_size, padding="valid", ) self.patch_embedding.weight = nn.Parameter( weights.get_tensor(f"{prefix}.patch_embedding.weight"), requires_grad=False ) self.patch_embedding.bias = nn.Parameter( weights.get_tensor(f"{prefix}.patch_embedding.bias"), requires_grad=False ) self.num_patches_per_side = self.image_size // self.patch_size self.num_patches = self.num_patches_per_side**2 self.num_positions = self.num_patches self.position_embedding = TensorParallelEmbedding( prefix=f"{prefix}.position_embedding", weights=weights ) def forward( self, pixel_values: torch.FloatTensor, patch_attention_mask: torch.BoolTensor ) -> torch.Tensor: batch_size, _, max_im_h, max_im_w = pixel_values.shape patch_embeds = self.patch_embedding(pixel_values) embeddings = patch_embeds.flatten(2).transpose(1, 2) max_nb_patches_h, max_nb_patches_w = ( max_im_h // self.patch_size, max_im_w // self.patch_size, ) boundaries = torch.arange( 1 / self.num_patches_per_side, 1.0, 1 / self.num_patches_per_side ) position_ids = torch.full( size=(batch_size, max_nb_patches_h * max_nb_patches_w), fill_value=0 ) for batch_idx, p_attn_mask in enumerate(patch_attention_mask): nb_patches_h = p_attn_mask[:, 0].sum() nb_patches_w = p_attn_mask[0].sum() fractional_coords_h = torch.arange(0, 1 - 1e-6, 1 / nb_patches_h) fractional_coords_w = torch.arange(0, 1 - 1e-6, 1 / nb_patches_w) bucket_coords_h = torch.bucketize( fractional_coords_h, boundaries, right=True ) bucket_coords_w = torch.bucketize( fractional_coords_w, boundaries, right=True ) pos_ids = ( bucket_coords_h[:, None] * self.num_patches_per_side + bucket_coords_w ).flatten() position_ids[batch_idx][p_attn_mask.view(-1).cpu()] = pos_ids position_ids = position_ids.to(self.position_embedding.weight.device) embeddings = embeddings + self.position_embedding(position_ids) return embeddings class Idefics2VisionAttention(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.config = config self.embed_dim = config.hidden_size self.num_heads = config.num_attention_heads self.head_size = self.embed_dim // self.num_heads if self.head_size * self.num_heads != self.embed_dim: raise ValueError( f"embed_dim must be divisible by num_heads (got `embed_dim`: {self.embed_dim} and `num_heads`:" f" {self.num_heads})." ) self.scale = self.head_size**-0.5 self.dropout = config.attention_dropout self.num_heads = self.num_heads // weights.process_group.size() self.embed_dim = self.embed_dim // weights.process_group.size() self.qkv = TensorParallelColumnLinear.load_multi( config, prefixes=[f"{prefix}.q_proj", f"{prefix}.k_proj", f"{prefix}.v_proj"], dim=0, weights=weights, bias=True, ) self.out_proj = TensorParallelRowLinear.load( config=config, prefix=f"{prefix}.out_proj", weights=weights, bias=True ) self.is_causal = False def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, ) -> torch.Tensor: batch_size, q_len, _ = hidden_states.size() qkv = self.qkv(hidden_states) query_states, key_states, value_states = qkv.split( [ self.head_size * self.num_heads, self.head_size * self.num_heads, self.head_size * self.num_heads, ], dim=2, ) query_states = query_states.view( batch_size, q_len, self.num_heads, self.head_size ).transpose(1, 2) key_states = key_states.view( batch_size, q_len, self.num_heads, self.head_size ).transpose(1, 2) value_states = value_states.view( batch_size, q_len, self.num_heads, self.head_size ).transpose(1, 2) k_v_seq_len = key_states.shape[-2] attn_weights = ( torch.matmul(query_states, key_states.transpose(2, 3)) * self.scale ) if attn_weights.size() != (batch_size, self.num_heads, q_len, k_v_seq_len): raise ValueError( f"Attention weights should be of size {(batch_size, self.num_heads, q_len, k_v_seq_len)}, but is" f" {attn_weights.size()}" ) if attention_mask is not None: if attention_mask.size() != (batch_size, 1, q_len, k_v_seq_len): raise ValueError( f"Attention mask should be of size {(batch_size, 1, q_len, k_v_seq_len)}, but is {attention_mask.size()}" ) attn_weights = attn_weights + attention_mask # upcast attention to fp32 attn_weights = nn.functional.softmax( attn_weights, dim=-1, dtype=torch.float32 ).to(query_states.dtype) attn_weights = nn.functional.dropout( attn_weights, p=self.dropout, training=self.training ) attn_output = torch.matmul(attn_weights, value_states) if attn_output.size() != (batch_size, self.num_heads, q_len, self.head_size): raise ValueError( f"`attn_output` should be of size {(batch_size, self.num_heads, q_len, self.head_size)}, but is" f" {attn_output.size()}" ) attn_output = attn_output.transpose(1, 2).contiguous() attn_output = attn_output.reshape(batch_size, q_len, self.embed_dim) attn_output = self.out_proj(attn_output) return attn_output class Idefics2VisionMLP(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.config = config self.activation_fn = ACT2FN[config.hidden_act] self.fc1 = TensorParallelColumnLinear.load( prefix=f"{prefix}.fc1", config=config, weights=weights, bias=True ) self.fc2 = TensorParallelRowLinear.load( prefix=f"{prefix}.fc2", config=config, weights=weights, bias=True ) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.fc1(hidden_states) hidden_states = self.activation_fn(hidden_states) hidden_states = self.fc2(hidden_states) return hidden_states class Idefics2EncoderLayer(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.embed_dim = config.hidden_size self.self_attn = Idefics2VisionAttention( prefix=f"{prefix}.self_attn", config=config, weights=weights ) self.layer_norm1 = nn.LayerNorm.load( prefix=f"{prefix}.layer_norm1", eps=config.layer_norm_eps, weights=weights ) self.layer_norm2 = nn.LayerNorm.load( prefix=f"{prefix}.layer_norm2", eps=config.layer_norm_eps, weights=weights ) self.mlp = Idefics2VisionMLP( prefix=f"{prefix}.mlp", config=config, weights=weights ) # Copied from transformers.models.siglip.modeling_siglip.SiglipEncoderLayer.forward def forward( self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, ) -> torch.Tensor: residual = hidden_states hidden_states = self.layer_norm1(hidden_states) hidden_states = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, ) hidden_states = residual + hidden_states residual = hidden_states hidden_states = self.layer_norm2(hidden_states) hidden_states = self.mlp(hidden_states) hidden_states = residual + hidden_states return hidden_states class Idefics2Encoder(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.config = config self.layers = nn.ModuleList( [ Idefics2EncoderLayer( prefix=f"{prefix}.layers.{i}", config=config, weights=weights ) for i in range(config.num_hidden_layers) ] ) # Ignore copy def forward( self, inputs_embeds, attention_mask: Optional[torch.Tensor] = None, ): hidden_states = inputs_embeds for encoder_layer in self.layers: hidden_states = encoder_layer( hidden_states, attention_mask, ) return hidden_states class Idefics2VisionTransformer(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.config = config self.embeddings = Idefics2VisionEmbeddings( prefix=f"{prefix}.embeddings", config=config, weights=weights ) self.encoder = Idefics2Encoder( prefix=f"{prefix}.encoder", config=config, weights=weights ) self.post_layernorm = nn.LayerNorm.load( prefix=f"{prefix}.post_layernorm", weights=weights, eps=config.layer_norm_eps, ) def forward( self, pixel_values, patch_attention_mask: Optional[torch.BoolTensor] = None, ): batch_size = pixel_values.size(0) if patch_attention_mask is None: patch_size = self.config.patch_size patch_attention_mask = torch.ones( ( batch_size, pixel_values.size(2) // patch_size, pixel_values.size(3) // patch_size, ) ) patch_attention_mask = patch_attention_mask.to( dtype=torch.bool, device=pixel_values.device ) hidden_states = self.embeddings( pixel_values=pixel_values, patch_attention_mask=patch_attention_mask ) patch_attention_mask = patch_attention_mask.view(batch_size, -1) # The call to `_upad_input` in `_flash_attention_forward` is expensive # So when the `patch_attention_mask` is full of 1s (i.e. attending to the whole sequence), # avoiding passing the attention_mask, which is equivalent to attending to the full sequence if not torch.any(~patch_attention_mask): patch_attention_mask = None else: patch_attention_mask = _prepare_4d_attention_mask( patch_attention_mask, hidden_states.dtype ) encoder_outputs = self.encoder( inputs_embeds=hidden_states, attention_mask=patch_attention_mask, ) last_hidden_state = encoder_outputs last_hidden_state = self.post_layernorm(last_hidden_state) return last_hidden_state class Idefics2MLP(nn.Module): def __init__(self, prefix, config, weights): super().__init__() act = config.text_config.hidden_act self.act = ( ACT2FN[act] if "gelu" not in act else lambda x: torch.nn.functional.gelu( x, approximate=( "tanh" if act in ["gelu_fast", "gelu_pytorch_tanh"] else "none" ), ) ) self.gate_up_proj = TensorParallelColumnLinear.load_multi( config, prefixes=[f"{prefix}.gate_proj", f"{prefix}.up_proj"], weights=weights, dim=0, bias=False, ) self.down_proj = TensorParallelRowLinear.load( config, prefix=f"{prefix}.down_proj", weights=weights, bias=False, ) def forward(self, hidden_states): start_shape = hidden_states.shape[:-1] gate_up_states = self.gate_up_proj(hidden_states) intermediate_size = gate_up_states.shape[-1] // 2 gate_up_states = gate_up_states.view(-1, 2, intermediate_size) return self.down_proj( self.act(gate_up_states[:, 0]) * gate_up_states[:, 1] ).view(*start_shape, -1) class Idefics2RMSNorm(nn.Module): def __init__(self, prefix, weights, eps): """ Idefics2RMSNorm is equivalent to T5LayerNorm """ super().__init__() self.weight = nn.Parameter( weights.get_tensor(f"{prefix}.weight"), requires_grad=False ) self.variance_epsilon = eps def forward(self, hidden_states): input_dtype = hidden_states.dtype hidden_states = hidden_states.to(torch.float32) variance = hidden_states.pow(2).mean(-1, keepdim=True) hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) return self.weight * hidden_states.to(input_dtype) class Idefics2PerceiverAttention(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.layer_idx = None self.hidden_size = config.text_config.hidden_size self.num_heads = config.perceiver_config.resampler_n_heads self.head_size = config.perceiver_config.resampler_head_dim self.num_key_value_heads = config.perceiver_config.num_key_value_heads self.num_key_value_groups = self.num_heads // self.num_key_value_heads self.attention_dropout = config.perceiver_config.attention_dropout self.num_heads = self.num_heads // weights.process_group.size() self.num_key_value_heads = ( self.num_key_value_heads // weights.process_group.size() ) self.q_proj = TensorParallelColumnLinear.load( config, prefix=f"{prefix}.q_proj", weights=weights, bias=False, ) self.kv = TensorParallelColumnLinear.load_multi( config, prefixes=[f"{prefix}.k_proj", f"{prefix}.v_proj"], dim=0, weights=weights, bias=False, ) self.o_proj = TensorParallelRowLinear.load( config=config, prefix=f"{prefix}.o_proj", weights=weights, bias=False ) self.is_causal = False def forward( self, latents: torch.Tensor, context: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: bsz, q_len, _ = latents.size() kv_seq_len = q_len + context.size()[1] hidden_states = torch.concat([context, latents], dim=-2) query_states = self.q_proj(latents) kv = self.kv(hidden_states) key_states, value_states = kv.split( [ self.head_size * self.num_key_value_heads, self.head_size * self.num_key_value_heads, ], dim=2, ) query_states = query_states.view( bsz, q_len, self.num_heads, self.head_size ).transpose(1, 2) key_states = key_states.view( bsz, kv_seq_len, self.num_key_value_heads, self.head_size ).transpose(1, 2) value_states = value_states.view( bsz, kv_seq_len, self.num_key_value_heads, self.head_size ).transpose(1, 2) # repeat k/v heads if n_kv_heads < n_heads key_states = repeat_kv(key_states, self.num_key_value_groups) value_states = repeat_kv(value_states, self.num_key_value_groups) attn_weights = torch.matmul( query_states, key_states.transpose(2, 3) ) / math.sqrt(self.head_size) if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len): raise ValueError( f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is" f" {attn_weights.size()}" ) if attention_mask is not None: if attention_mask.size() != (bsz, 1, q_len, kv_seq_len): raise ValueError( f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}" ) attn_weights = attn_weights + attention_mask # upcast attention to fp32 attn_weights = nn.functional.softmax( attn_weights, dim=-1, dtype=torch.float32 ).to(query_states.dtype) attn_output = torch.matmul(attn_weights, value_states) if attn_output.size() != (bsz, self.num_heads, q_len, self.head_size): raise ValueError( f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_size)}, but is" f" {attn_output.size()}" ) attn_output = attn_output.transpose(1, 2).contiguous() attn_output = attn_output.reshape(bsz, q_len, self.num_heads * self.head_size) attn_output = self.o_proj(attn_output) return attn_output class Idefics2PerceiverLayer(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.hidden_size = config.text_config.hidden_size self.n_latents = config.perceiver_config.resampler_n_latents self.depth = config.perceiver_config.resampler_depth self.rms_norm_eps = config.text_config.rms_norm_eps self.input_latents_norm = Idefics2RMSNorm( prefix=f"{prefix}.input_latents_norm", weights=weights, eps=self.rms_norm_eps, ) self.input_context_norm = Idefics2RMSNorm( prefix=f"{prefix}.input_context_norm", weights=weights, eps=self.rms_norm_eps, ) self.self_attn = Idefics2PerceiverAttention( prefix=f"{prefix}.self_attn", config=config, weights=weights ) self.post_attention_layernorm = Idefics2RMSNorm( prefix=f"{prefix}.post_attention_layernorm", weights=weights, eps=self.rms_norm_eps, ) self.mlp = Idefics2MLP(prefix=f"{prefix}.mlp", config=config, weights=weights) def forward( self, latents: torch.Tensor, context: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, ): """ Args: latents (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` context (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` attention_mask (`torch.FloatTensor`, *optional*): attention mask of size `(batch, sequence_length)` where padding elements are indicated by 0. """ residual = latents latents = self.input_latents_norm(latents) context = self.input_context_norm(context) latents = self.self_attn( latents=latents, context=context, attention_mask=attention_mask, ) latents = residual + latents residual = latents latents = self.post_attention_layernorm(latents) latents = self.mlp(latents) latents = residual + latents return latents class Idefics2PerceiverResampler(nn.Module): def __init__(self, prefix, config, weights) -> None: super().__init__() self.hidden_size = config.text_config.hidden_size self.hidden_act = config.perceiver_config.hidden_act self.n_latents = config.perceiver_config.resampler_n_latents self.depth = config.perceiver_config.resampler_depth self.rms_norm_eps = config.text_config.rms_norm_eps # Create Latents for Perceiver self.latents = weights.get_tensor(f"{prefix}.latents") # Create Transformer Blocks self.layers = nn.ModuleList( [ Idefics2PerceiverLayer( prefix=f"{prefix}.layers.{idx}", config=config, weights=weights ) for idx in range(self.depth) ] ) self.norm = Idefics2RMSNorm( prefix=f"{prefix}.norm", weights=weights, eps=config.text_config.rms_norm_eps, ) def forward( self, context: torch.Tensor, attention_mask, ) -> torch.Tensor: # seq embed -> bsz seq embed latents = self.latents.unsqueeze(0).expand( (context.shape[0], *self.latents.size()) ) latent_attention_mask = torch.ones( (attention_mask.size(0), latents.size(1)), dtype=attention_mask.dtype, device=attention_mask.device, ) attention_mask = torch.cat([attention_mask, latent_attention_mask], dim=-1) attention_mask = _prepare_4d_attention_mask( attention_mask, latents.dtype, tgt_len=self.n_latents ) compressed_context = latents for perceiver_layer in self.layers: compressed_context = perceiver_layer( compressed_context, context, attention_mask=attention_mask, ) compressed_context = self.norm(compressed_context) return compressed_context class Idefics2Connector(nn.Module): def __init__(self, prefix, config, weights): super().__init__() self.modality_projection = Idefics2MLP( prefix=f"{prefix}.modality_projection", config=config, weights=weights ) self.perceiver_resampler = Idefics2PerceiverResampler( prefix=f"{prefix}.perceiver_resampler", config=config, weights=weights ) def forward(self, image_hidden_states, attention_mask): image_hidden_states = self.modality_projection(image_hidden_states) image_hidden_states = self.perceiver_resampler( context=image_hidden_states, attention_mask=attention_mask ) return image_hidden_states class Idefics2ForConditionalGeneration(nn.Module): def __init__(self, prefix, config, weights): super().__init__() config.vision_config.quantize = None config.vision_config.speculator = config.speculator config.text_config.quantize = config.quantize config.text_config.speculator = config.speculator vision_config = config.vision_config self.text_model = load_text_model( prefix="model" if not prefix else f"{prefix}.model", config=config.text_config, weights=weights, name="text_model", ) self.dtype = weights.dtype # The vision and connector models are not quantized. with weights.use_loader(DefaultWeightsLoader(UnquantizedWeight)): self.vision_model = Idefics2VisionTransformer( prefix=( f"{prefix}.model.vision_model" if prefix else "model.vision_model" ), config=vision_config, weights=weights, ) config.quantize = None self.connector = Idefics2Connector( prefix=f"{prefix}.model.connector" if prefix else "model.connector", config=config, weights=weights, ) self.config = config self.image_seq_len = config.perceiver_config.resampler_n_latents self.image_token_id = config.image_token_id self.pad_token_id = ( config.pad_token_id if config.pad_token_id is not None else -1 ) def _merge_input_ids_with_image_features( self, input_ids: torch.Tensor, inputs_embeds: torch.Tensor, image_features: torch.Tensor, ): """In place merges in vision_embeddings with inputs_embeds.""" # mask = input_ids == self.config.image_token_index mask = input_ids == self.config.image_token_id # Let's pray we have enabled enough slots ! inputs_embeds[mask] = image_features.view(-1, image_features.shape[-1]) return inputs_embeds def forward( self, input_ids: torch.Tensor, position_ids: torch.Tensor, cu_seqlen_prefill: Optional[torch.Tensor], kv_cache: List[Tuple[torch.Tensor, torch.Tensor]], block_tables: torch.Tensor, slots: torch.Tensor, seqlen: Seqlen, max_s: int, prefill_cache_indices: Optional[torch.Tensor], lm_head_indices: Optional[torch.Tensor] = None, pixel_values: torch.FloatTensor = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, # Unused here image_sizes: Optional[torch.Tensor] = None, adapter_data: Optional[torch.Tensor] = None, ): inputs_embeds = self.text_model.embed_tokens(input_ids) if pixel_values is not None: batch_size, num_images, num_channels, height, width = pixel_values.shape all_states = [] all_pixel_values = pixel_values all_pixel_mask = pixel_attention_mask for i in range(batch_size): pixel_values = all_pixel_values.to( dtype=self.dtype ) # fp16 compatibility pixel_values = pixel_values[i : i + 1] pixel_values = pixel_values.view(num_images, *pixel_values.shape[2:]) # Remove padding images - padding images are full 0. nb_values_per_image = pixel_values.shape[1:].numel() real_images_inds = (pixel_values == 0.0).sum( dim=(-1, -2, -3) ) != nb_values_per_image pixel_values = pixel_values[real_images_inds].contiguous() # Handle the vision attention mask if pixel_attention_mask is None: pixel_attention_mask = torch.ones( size=( pixel_values.size(0), pixel_values.size(2), pixel_values.size(3), ), dtype=torch.bool, device=pixel_values.device, ) else: # Remove padding images from the mask/pP p pixel_attention_mask = all_pixel_mask[i : i + 1] pixel_attention_mask = pixel_attention_mask.view( 1 * num_images, *pixel_attention_mask.shape[2:] ) pixel_attention_mask = pixel_attention_mask[ real_images_inds ].contiguous() patch_size = self.config.vision_config.patch_size patches_subgrid = pixel_attention_mask.unfold( dimension=1, size=patch_size, step=patch_size ) patches_subgrid = patches_subgrid.unfold( dimension=2, size=patch_size, step=patch_size ) patch_attention_mask = (patches_subgrid.sum(dim=(-1, -2)) > 0).bool() # Get sequence from the vision encoder image_hidden_states = self.vision_model( pixel_values=pixel_values, patch_attention_mask=patch_attention_mask, ) # Modality projection & resampling image_hidden_states = self.connector( image_hidden_states, attention_mask=patch_attention_mask.view(pixel_values.size(0), -1), ) all_states.append(image_hidden_states) image_hidden_states = torch.stack(all_states, dim=0) # When we generate, we don't want to replace the potential image_token_id that we generated by images # that simply don't exist inputs_embeds = self._merge_input_ids_with_image_features( input_ids, inputs_embeds, image_hidden_states ) hidden_states = self.text_model.model( inputs_embeds=inputs_embeds, position_ids=position_ids, cu_seqlen_prefill=cu_seqlen_prefill, kv_cache=kv_cache, block_tables=block_tables, slots=slots, seqlen=seqlen, max_s=max_s, true_max_s=max_s, prefill_cache_indices=None, adapter_data=adapter_data, ) if lm_head_indices is not None: hidden_states = hidden_states[lm_head_indices] logits, speculative_logits = self.text_model.lm_head(hidden_states) return logits, speculative_logits
text-generation-inference/server/text_generation_server/models/custom_modeling/idefics2.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/models/custom_modeling/idefics2.py", "repo_id": "text-generation-inference", "token_count": 15271 }
243
from contextlib import nullcontext import math import os import time import torch import torch.distributed import numpy as np from loguru import logger from dataclasses import dataclass from opentelemetry import trace from transformers import ( PreTrainedTokenizerBase, AutoConfig, AutoTokenizer, GenerationConfig, ) from typing import Any, ContextManager, Iterable, Optional, Tuple, List, Type, Dict from text_generation_server.adapters import AdapterBatchData, AdapterBatchMetadata from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from text_generation_server.utils.chunks import concat_text_chunks from text_generation_server.utils.import_utils import SYSTEM from text_generation_server.models import Model from text_generation_server.utils.log import log_master from text_generation_server.utils.tokens import batch_top_tokens from text_generation_server.utils.speculate import get_speculate from text_generation_server.utils import ( initialize_torch_distributed, weight_files, Weights, ) from text_generation_server.models.types import ( Batch, Tokens, Generation, GeneratedText, ) from text_generation_server.pb import generate_pb2 from text_generation_server.models.globals import ( MEM_POOL, ATTENTION, BLOCK_SIZE, CUDA_GRAPHS, TGI_WIGGLE_ROOM, get_adapter_to_index, ) from text_generation_server.layers.attention import Seqlen from text_generation_server.utils import StoppingCriteria, HeterogeneousNextTokenChooser from text_generation_server.utils.dist import MEMORY_FRACTION from text_generation_server.utils.quantization import get_loader from text_generation_server.utils.segments import SegmentConcatBuilder, find_segments from text_generation_server.utils.import_utils import ( empty_cache, synchronize, get_free_memory, ) tracer = trace.get_tracer(__name__) # Will be set in init SLIDING_WINDOW: Optional[int] = None def set_sliding_window(sliding_window: int): global SLIDING_WINDOW SLIDING_WINDOW = sliding_window def get_sliding_windows() -> int: global SLIDING_WINDOW return SLIDING_WINDOW def init_cpu_threads_env(rank_id: int, world_size: int): import importlib.util if importlib.util.find_spec("numa") is not None: import numa import psutil nodes = numa.get_max_node() + 1 rank_per_node = math.ceil(world_size / nodes) num_cpus_per_nodes = int(psutil.cpu_count(logical=False) / nodes) node_id = int(rank_id / rank_per_node) rank_offset_per_node = rank_id % rank_per_node if os.getenv("OMP_NUM_THREADS") is None: num_cpus_per_rank = max(int(num_cpus_per_nodes / rank_per_node), 1) else: num_cpus_per_rank = int(os.getenv("OMP_NUM_THREADS")) if len(numa.get_membind()) == nodes: numa.set_membind([node_id]) torch.set_num_threads(num_cpus_per_rank) if len(numa.get_affinity(0)) == psutil.cpu_count(logical=True): cpu_start = num_cpus_per_rank * rank_offset_per_node numa.set_affinity( 0, list(numa.node_to_cpus(node_id))[ cpu_start : cpu_start + num_cpus_per_rank ], ) logger.info(f"affinity={numa.get_affinity(0)}, membind = {numa.get_membind()}") @dataclass class FlashCausalLMBatch(Batch): batch_id: int requests: List[generate_pb2.Request] # request id -> idx in list mapping requests_idx_mapping: Dict[int, int] # Decoder values input_ids: torch.Tensor position_ids: torch.Tensor speculative_ids: Optional[torch.Tensor] # Flash Attention values # tensor of length b containing the cumulative sequence lengths of the sequences in the batch, only used in prefill cu_seqlen_prefill: Optional[torch.Tensor] # Prefill cache indices is used to slice into the kv tensor before caching it into the paged attention buffers # as we only keep SLIDING_WINDOW values instead of the whole tensor prefill_cache_indices: Optional[torch.Tensor] # Paged Attention values # Set when creating the batch # CPU tensor of length b indicating the start of each sequence in slots start_slots: torch.Tensor # tensor of indices of the currently used slots, length = \sum_{i=0}^{b} s_i in prefill, length = b in decode slot_indices: torch.Tensor # list of length b of list of length s_i // block_size block_tables: List[List[int]] # tensor of size [b, max_total_seqlen // block_size] holding the paged attention block tables for all sequences block_tables_tensor: torch.Tensor # tensor of length \sum_{i=0}^{b} max_s_i holding the paged attention slots for all sequences slots: torch.Tensor # size [b], containing the number of blocks that can be retrieved from the cache prefix_lens: List[int] prefix_lens_tensor: torch.Tensor max_seqlen: int # Prefill metadata tensors to efficiently compute logprobs prefill_head_indices: Optional[torch.Tensor] prefill_next_token_indices: Optional[torch.tensor] prefill_cu_outlens: Optional[List[int]] # Prefixes prefix_ids: List[List[int]] # All tokens all_input_ids: List[List[int]] all_input_ids_tensor: torch.Tensor # Lengths of all generations present in the batch input_lengths: List[int] input_lengths_tensor: torch.Tensor prefix_offsets: List[Optional[int]] read_offsets: List[Optional[int]] # Generation helpers next_token_chooser: HeterogeneousNextTokenChooser stopping_criterias: List[StoppingCriteria] top_n_tokens: List[int] top_n_tokens_tensor: torch.Tensor # Adapter metadata for each request adapter_meta: AdapterBatchMetadata # Number of blocks in this batch num_blocks: int # Maximum number of blocks max_blocks: int def to_pb(self) -> generate_pb2.CachedBatch: return generate_pb2.CachedBatch( id=self.batch_id, request_ids=[r.id for r in self.requests], size=len(self), max_tokens=self.num_blocks * BLOCK_SIZE, ) @classmethod def batch_tokenized_inputs( cls, requests: Iterable[generate_pb2.Request], tokenizer ): max_length = 0 all_input_ids = [] batch_size = 0 for r in requests: batch_size += 1 inputs = concat_text_chunks(r.input_chunks.chunks) input_ids = tokenizer( inputs, truncation=True, max_length=r.truncate, add_special_tokens=r.add_special_tokens, )["input_ids"] max_length = max(max_length, len(input_ids)) all_input_ids.append(input_ids) return all_input_ids @classmethod def from_tokenized( cls, pb: generate_pb2.Batch, tokenizer: PreTrainedTokenizerBase, batch_tokenized_inputs, dtype: torch.dtype, device: torch.device, ) -> "FlashCausalLMBatch": sliding_window = get_sliding_windows() position_ids = [] cu_seqlen_prefill = [0] start_slots = [] slot_indices = [] prefill_cache_indices = [] input_lengths = [] prefix_offsets = [] read_offsets = [] all_input_ids = [] prefix_ids = [] requests_idx_mapping = {} all_prefill_logprobs = True no_prefill_logprobs = True prefill_head_indices = [] prefill_next_token_indices = [] prefill_cu_outlens = [0] next_token_chooser_parameters = [] stopping_criterias = [] top_n_tokens = [] adapter_indices_list = [] adapter_set = set() # Cumulative length cumulative_length = 0 cumulative_slot_tokens = 0 prefill_out_cumulative_length = 0 num_blocks = 0 max_seqlen = 0 max_length = 0 max_blocks = 0 block_tables = [] slots = [] prefix_lens = [] # Parse batch for i, (r, tokenized_input) in enumerate( zip(pb.requests, batch_tokenized_inputs) ): # request id -> idx in list mapping requests_idx_mapping[r.id] = i orig_input_length = len(tokenized_input) prefix_len = r.prefix_len assert ( prefix_len <= orig_input_length ), f"Prefix {prefix_len} vs input {orig_input_length}" if prefix_len == orig_input_length: assert prefix_len > 0 prefix_len -= 1 prefix_ids.append(tokenized_input[:prefix_len]) tokenized_input = tokenized_input[prefix_len:] input_length = len(tokenized_input) input_lengths.append(input_length) prefix_offsets.append(input_length - 5) read_offsets.append(input_length) all_input_ids.append(tokenized_input) # Position ids request_position_ids = torch.arange( prefix_len, orig_input_length, dtype=torch.int32 ) position_ids.append(request_position_ids) # Add cumulative lengths of all previous inputs cu_seqlen_prefill.append(cumulative_length + input_length) next_token_chooser_parameters.append(r.parameters) stopping_criteria = StoppingCriteria.from_pb( r.stopping_parameters, tokenizer ) max_new_tokens = stopping_criteria.max_new_tokens stopping_criterias.append(stopping_criteria) top_n_tokens.append(r.top_n_tokens) ADAPTER_TO_INDEX = get_adapter_to_index() adapter_index = ADAPTER_TO_INDEX.get(r.adapter_id, 0) adapter_indices_list.append(torch.full((input_length,), adapter_index)) adapter_set.add(adapter_index) # Paged attention # Remove one as the first token des not have a past speculative_length = get_speculate() speculative_length = 0 if speculative_length is None else speculative_length # Tokens that need to be mapped to blocks. block_tokens = orig_input_length + max_new_tokens - 1 + speculative_length # Tokens that need to be mapped to slots. We don't need slots for the # cached prefix (if present). slot_tokens = input_length + max_new_tokens - 1 + speculative_length # blocks and slots can be empty (for example in warmup) if not r.blocks: needed_blocks = math.ceil(block_tokens / BLOCK_SIZE) request_blocks = [ b for b in range(num_blocks, num_blocks + needed_blocks) ] request_slots = [ s for b in request_blocks for s in range(b * BLOCK_SIZE, (b + 1) * BLOCK_SIZE) ] else: request_blocks = r.blocks request_slots = r.slots[ prefix_len: #: orig_input_length + max_new_tokens + speculative_length ] block_tables.append(request_blocks) slots.extend(request_slots) prefix_lens.append(prefix_len) num_blocks += len(request_blocks) start_slots.append(cumulative_slot_tokens) request_slot_indices = torch.arange( cumulative_slot_tokens, cumulative_slot_tokens + input_length, dtype=torch.int64, ) slot_indices.append(request_slot_indices) # Create tensor to slice into the kv tensor in prefill if sliding_window is not None: request_prefill_cache_indices = torch.arange( cumulative_length + max(0, input_length - sliding_window), cumulative_length + input_length, dtype=torch.int64, ) prefill_cache_indices.append(request_prefill_cache_indices) all_prefill_logprobs = all_prefill_logprobs and r.prefill_logprobs no_prefill_logprobs = no_prefill_logprobs and not r.prefill_logprobs if r.prefill_logprobs: prefill_head_indices.append(request_position_ids + cumulative_length) prefill_next_token_indices.append( prefill_out_cumulative_length + input_length - 1 ) prefill_cu_outlens.append(prefill_out_cumulative_length + input_length) prefill_out_cumulative_length += input_length else: prefill_head_indices.append( torch.tensor( [cumulative_length + input_length - 1], dtype=torch.int32 ) ) prefill_next_token_indices.append(prefill_out_cumulative_length) prefill_cu_outlens.append(prefill_out_cumulative_length + 1) prefill_out_cumulative_length += 1 # Update cumulative_length += input_length cumulative_slot_tokens += slot_tokens max_seqlen = max(max_seqlen, input_length) max_blocks = max(max_blocks, len(request_blocks)) max_length = max( max_length, input_length + max_new_tokens + speculative_length ) adapter_indices = torch.cat(adapter_indices_list).to( dtype=torch.int64, device=device ) next_token_chooser = HeterogeneousNextTokenChooser.from_pb( next_token_chooser_parameters, dtype, device, tokenizer ) start_slots = torch.tensor(start_slots, dtype=torch.int64) # Padded all_input_ids_tensor all_input_ids_tensor = np.zeros( (len(all_input_ids), max_length), dtype=np.int64 ) for i, input_ids in enumerate(all_input_ids): all_input_ids_tensor[i, : len(input_ids)] = input_ids # Create tensors on device all_input_ids_tensor = torch.tensor( all_input_ids_tensor, dtype=torch.int64, device=device ) if len(pb.requests) > 1: input_ids = np.concatenate(all_input_ids, dtype=np.int64) position_ids = torch.cat(position_ids) slot_indices = torch.cat(slot_indices) if sliding_window is not None: prefill_cache_indices = torch.cat(prefill_cache_indices) else: input_ids = all_input_ids[0] position_ids = position_ids[0] slot_indices = slot_indices[0] if sliding_window is not None: prefill_cache_indices = prefill_cache_indices[0] cu_seqlen_prefill = torch.tensor( cu_seqlen_prefill, device=device, dtype=torch.int32 ) position_ids = position_ids.to(device) slot_indices = slot_indices.to(device) prefill_cache_indices = ( prefill_cache_indices.to(device) if sliding_window is not None else None ) input_ids = torch.tensor(input_ids, dtype=torch.int64, device=device) input_lengths_tensor = torch.tensor( input_lengths, dtype=torch.int32, device=device ) adapter_segments, adapter_segment_indices = find_segments(adapter_indices) adapter_segments = torch.tensor( adapter_segments, dtype=torch.int32, device=device ) if all_prefill_logprobs: prefill_head_indices = None prefill_next_token_indices = cu_seqlen_prefill[1:] - 1 elif no_prefill_logprobs: prefill_head_indices = cu_seqlen_prefill[1:] - 1 prefill_next_token_indices = None else: prefill_head_indices = torch.tensor( torch.cat(prefill_head_indices), dtype=torch.int64, device=device ) prefill_next_token_indices = torch.tensor( prefill_next_token_indices, dtype=torch.int64, device=device ) top_n_tokens_tensor = torch.tensor( top_n_tokens, device=device, dtype=torch.int64 ) slots = torch.tensor(slots, dtype=torch.int64, device=device) block_tables_tensor = torch.zeros( (len(block_tables), max_blocks), dtype=torch.int32, device="cpu" ) for i, request_blocks in enumerate(block_tables): block_tables_tensor[i, : len(request_blocks)] = torch.tensor(request_blocks) block_tables_tensor = block_tables_tensor.to(device) prefix_lens_tensor = torch.tensor(prefix_lens, dtype=torch.int32, device=device) return cls( batch_id=pb.id, requests=pb.requests, requests_idx_mapping=requests_idx_mapping, input_ids=input_ids, position_ids=position_ids, cu_seqlen_prefill=cu_seqlen_prefill, prefill_cache_indices=prefill_cache_indices, start_slots=start_slots, slot_indices=slot_indices, block_tables=block_tables, block_tables_tensor=block_tables_tensor, slots=slots, prefix_lens=prefix_lens, prefix_lens_tensor=prefix_lens_tensor, max_seqlen=max_seqlen, prefill_head_indices=prefill_head_indices, prefill_next_token_indices=prefill_next_token_indices, prefill_cu_outlens=prefill_cu_outlens, input_lengths=input_lengths, input_lengths_tensor=input_lengths_tensor, prefix_offsets=prefix_offsets, read_offsets=read_offsets, all_input_ids=all_input_ids, all_input_ids_tensor=all_input_ids_tensor, prefix_ids=prefix_ids, next_token_chooser=next_token_chooser, stopping_criterias=stopping_criterias, top_n_tokens=top_n_tokens, top_n_tokens_tensor=top_n_tokens_tensor, num_blocks=num_blocks, max_blocks=max_blocks, adapter_meta=AdapterBatchMetadata( adapter_indices=adapter_indices, adapter_set=adapter_set, adapter_segments=adapter_segments, segment_indices=adapter_segment_indices, ), speculative_ids=None, ) @classmethod def from_pb( cls, pb: generate_pb2.Batch, tokenizer: PreTrainedTokenizerBase, dtype: torch.dtype, device: torch.device, ) -> "FlashCausalLMBatch": batch_tokenized_inputs = cls.batch_tokenized_inputs(pb.requests, tokenizer) return cls.from_tokenized(pb, tokenizer, batch_tokenized_inputs, dtype, device) @tracer.start_as_current_span("filter") def filter(self, request_ids: List[int]) -> "FlashCausalLMBatch": if len(request_ids) == 0: raise ValueError("Batch must have at least one request") # We assume that if len(requests) == len(self) then the requests are the same if len(request_ids) == len(self): return self device = self.input_ids.device # New values after filtering requests_idx_mapping = {} # Used to index into tensors indices = [] # slots to keep after filtering slot_filtering_indices = torch.zeros( self.slots.shape[0], dtype=torch.bool, device=device ) # Create on CPU to only move to GPU once instead of at every copy slot_indices = torch.empty(len(request_ids), dtype=torch.int64) max_seqlen = 0 requests = [] start_slots = [] block_tables = [] all_input_ids = [] prefix_ids = [] input_lengths = [] prefix_lens = [] prefix_offsets = [] read_offsets = [] stopping_criterias = [] top_n_tokens = [] adapter_set = set() num_blocks = 0 max_blocks = 0 # Cumulative length cumulative_max_length = 0 for i, request_id in enumerate(request_ids): idx = self.requests_idx_mapping[request_id] indices.append(idx) requests_idx_mapping[request_id] = i requests.append(self.requests[idx]) # Get length request_input_length = self.input_lengths[idx] prefix_len = self.prefix_lens[idx] max_seqlen = max(max_seqlen, request_input_length) all_input_ids.append(self.all_input_ids[idx]) prefix_ids.append(self.prefix_ids[idx]) input_lengths.append(request_input_length) prefix_lens.append(prefix_len) prefix_offsets.append(self.prefix_offsets[idx]) read_offsets.append(self.read_offsets[idx]) stopping_criteria = self.stopping_criterias[idx] stopping_criterias.append(stopping_criteria) top_n_tokens.append(self.top_n_tokens[idx]) ADAPTER_TO_INDEX = get_adapter_to_index() adapter_index = ADAPTER_TO_INDEX.get(self.requests[idx].adapter_id, 0) adapter_set.add(adapter_index) remaining_tokens = ( stopping_criteria.max_new_tokens - stopping_criteria.current_tokens ) request_block_table = self.block_tables[idx] num_blocks += len(request_block_table) block_tables.append(request_block_table) start_slots.append(cumulative_max_length) # Copy to tensor (CPU) slot_indices[i] = cumulative_max_length + request_input_length - 1 # Set slice slot_filtering_indices[ self.start_slots[idx] : self.start_slots[idx] + request_input_length + remaining_tokens - 1 ] = True cumulative_max_length += request_input_length + remaining_tokens - 1 max_blocks = max(max_blocks, len(request_block_table)) # Index into tensors input_ids = self.input_ids[indices] position_ids = self.position_ids[indices] adapter_indices = self.adapter_meta.adapter_indices[indices] all_input_ids_tensor = self.all_input_ids_tensor[indices] block_tables_tensor = self.block_tables_tensor[indices] input_lengths_tensor = self.input_lengths_tensor[indices] slots = self.slots[slot_filtering_indices] prefix_lens_tensor = self.prefix_lens_tensor[indices] next_token_chooser = self.next_token_chooser.filter(indices) top_n_tokens_tensor = self.top_n_tokens_tensor[indices] speculative_ids = ( self.speculative_ids[indices] if self.speculative_ids is not None else None ) start_slots = torch.tensor(start_slots, dtype=torch.int64) # Move to GPU now that we have the whole tensor slot_indices = slot_indices.to(device) adapter_segments, adapter_segment_indices = find_segments(adapter_indices) adapter_segments = torch.tensor( adapter_segments, dtype=torch.int32, device=device ) return type(self)( batch_id=self.batch_id, requests=requests, requests_idx_mapping=requests_idx_mapping, input_ids=input_ids, position_ids=position_ids, cu_seqlen_prefill=None, prefill_cache_indices=None, start_slots=start_slots, slot_indices=slot_indices, block_tables=block_tables, block_tables_tensor=block_tables_tensor, slots=slots, max_seqlen=max_seqlen, prefill_head_indices=None, prefill_next_token_indices=None, prefill_cu_outlens=None, input_lengths=input_lengths, input_lengths_tensor=input_lengths_tensor, prefix_lens=prefix_lens, prefix_lens_tensor=prefix_lens_tensor, prefix_offsets=prefix_offsets, read_offsets=read_offsets, all_input_ids=all_input_ids, all_input_ids_tensor=all_input_ids_tensor, prefix_ids=prefix_ids, next_token_chooser=next_token_chooser, stopping_criterias=stopping_criterias, top_n_tokens=top_n_tokens, top_n_tokens_tensor=top_n_tokens_tensor, num_blocks=num_blocks, max_blocks=max_blocks, speculative_ids=speculative_ids, adapter_meta=AdapterBatchMetadata( adapter_indices=adapter_indices, adapter_set=adapter_set, adapter_segments=adapter_segments, segment_indices=adapter_segment_indices, ), ) @classmethod @tracer.start_as_current_span("concatenate") def concatenate(cls, batches: List["FlashCausalLMBatch"]) -> "FlashCausalLMBatch": # Batch attributes requests = [] requests_idx_mapping = {} num_blocks = 0 total_batch_size = 0 total_slots = 0 max_blocks = 0 max_length = 0 max_seqlen = 0 for b in batches: total_batch_size += len(b) total_slots += len(b.slots) num_blocks += b.num_blocks speculative_length = ( b.speculative_ids.shape[1] if b.speculative_ids is not None else 0 ) max_blocks = max(max_blocks, b.max_blocks) max_seqlen = max(max_seqlen, b.max_seqlen) max_length = max( max_length, max( input_length + stopping_criteria.max_new_tokens + speculative_length - stopping_criteria.current_tokens for input_length, stopping_criteria in zip( b.input_lengths, b.stopping_criterias ) ), ) input_ids = batches[0].input_ids.new_empty(total_batch_size) position_ids = batches[0].position_ids.new_empty(total_batch_size) slots = batches[0].slots.new_empty(total_slots) slot_indices = batches[0].slot_indices.new_empty(total_batch_size) input_lengths_tensor = batches[0].input_lengths_tensor.new_empty( total_batch_size ) block_tables_tensor = batches[0].block_tables_tensor.new_zeros( (total_batch_size, max_blocks) ) prefix_lens_tensor = batches[0].prefix_lens_tensor.new_empty(total_batch_size) all_input_ids_tensor = batches[0].all_input_ids_tensor.new_zeros( (total_batch_size, max_length) ) top_n_tokens_tensor = batches[0].top_n_tokens_tensor.new_zeros( total_batch_size, ) total_indices_size = sum( b.adapter_meta.adapter_indices.shape[0] for b in batches ) adapter_indices = batches[0].adapter_meta.adapter_indices.new_empty( total_indices_size ) adapter_set = set() adapter_segment_builder = SegmentConcatBuilder() start_slots = [] block_tables = [] prefix_lens = [] all_input_ids = [] prefix_ids = [] input_lengths = [] prefix_offsets = [] read_offsets = [] next_token_chooser_parameters = [] fsm_grammar_states = [] stopping_criterias = [] top_n_tokens = [] # Cumulative length cumulative_batch_size = 0 cumulative_slots = 0 cumulative_adapter_indices_size = 0 for i, batch in enumerate(batches): requests.extend(batch.requests) if i == 0: requests_idx_mapping = batch.requests_idx_mapping else: # We need to offset the mapping for each batch by the cumulative batch size for k, v in batch.requests_idx_mapping.items(): requests_idx_mapping[k] = v + cumulative_batch_size start_index = cumulative_batch_size end_index = cumulative_batch_size + len(batch) slots_start_index = cumulative_slots slots_end_index = cumulative_slots + len(batch.slots) # Copy tensors (GPU) input_ids[start_index:end_index] = batch.input_ids position_ids[start_index:end_index] = batch.position_ids slot_indices[start_index:end_index] = batch.slot_indices + cumulative_slots input_lengths_tensor[start_index:end_index] = batch.input_lengths_tensor top_n_tokens_tensor[start_index:end_index] = batch.top_n_tokens_tensor slots[slots_start_index:slots_end_index] = batch.slots # Copy over adapter indices adapter_start_index = cumulative_adapter_indices_size adapter_end_index = ( cumulative_adapter_indices_size + batch.adapter_meta.adapter_indices.shape[0] ) adapter_indices[adapter_start_index:adapter_end_index] = ( batch.adapter_meta.adapter_indices ) cumulative_adapter_indices_size = adapter_end_index adapter_set.update(batch.adapter_meta.adapter_set) adapter_segment_builder.concat( batch.adapter_meta.adapter_segments, batch.adapter_meta.segment_indices ) all_input_ids_tensor[ start_index:end_index, : batch.all_input_ids_tensor.shape[1] ] = batch.all_input_ids_tensor[:, :max_length] block_tables_tensor[ start_index:end_index, : batch.block_tables_tensor.shape[1] ] = batch.block_tables_tensor[:, :max_blocks] prefix_lens_tensor[start_index:end_index] = batch.prefix_lens_tensor start_slots.append(batch.start_slots + cumulative_slots) block_tables.extend(batch.block_tables) prefix_lens.extend(batch.prefix_lens) all_input_ids.extend(batch.all_input_ids) prefix_ids.extend(batch.prefix_ids) input_lengths.extend(batch.input_lengths) prefix_offsets.extend(batch.prefix_offsets) read_offsets.extend(batch.read_offsets) next_token_chooser_parameters.extend([r.parameters for r in batch.requests]) fsm_grammar_states.extend(batch.next_token_chooser.fsm_grammar_states) stopping_criterias.extend(batch.stopping_criterias) top_n_tokens.extend(batch.top_n_tokens) # Update cumulative_batch_size += len(batch) cumulative_slots += len(batch.slots) start_slots = torch.concat(start_slots) next_token_chooser = HeterogeneousNextTokenChooser.from_pb( next_token_chooser_parameters, dtype=batches[0].next_token_chooser.dtype, device=batches[0].next_token_chooser.device, tokenizer=batches[0].next_token_chooser.tokenizer, fsm_grammar_states=fsm_grammar_states, ) speculative_ids = ( torch.cat([b.speculative_ids for b in batches], dim=0) if batches[0].speculative_ids is not None else None ) adapter_segments, adapter_segment_indices = adapter_segment_builder.build() return cls( batch_id=batches[0].batch_id, requests=requests, requests_idx_mapping=requests_idx_mapping, input_ids=input_ids, position_ids=position_ids, cu_seqlen_prefill=None, prefill_cache_indices=None, start_slots=start_slots, slot_indices=slot_indices, block_tables=block_tables, block_tables_tensor=block_tables_tensor, prefix_lens=prefix_lens, prefix_lens_tensor=prefix_lens_tensor, slots=slots, max_seqlen=max_seqlen, prefill_head_indices=None, prefill_next_token_indices=None, prefill_cu_outlens=None, input_lengths=input_lengths, input_lengths_tensor=input_lengths_tensor, prefix_offsets=prefix_offsets, read_offsets=read_offsets, all_input_ids=all_input_ids, all_input_ids_tensor=all_input_ids_tensor, prefix_ids=prefix_ids, next_token_chooser=next_token_chooser, stopping_criterias=stopping_criterias, top_n_tokens=top_n_tokens, top_n_tokens_tensor=top_n_tokens_tensor, num_blocks=num_blocks, max_blocks=max_blocks, speculative_ids=speculative_ids, adapter_meta=AdapterBatchMetadata( adapter_indices=adapter_indices, adapter_set=adapter_set, adapter_segments=adapter_segments, segment_indices=adapter_segment_indices, ), ) def __len__(self): return len(self.requests) ADAPTER_LAYERS = [ "q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj", ] ROW_PARALLEL = {"o_proj", "down_proj", "lm_head"} class FlashCausalLM(Model): def __init__( self, model_id: str, model_class, revision: Optional[str] = None, quantize: Optional[str] = None, speculator: Optional[str] = None, dtype: Optional[torch.dtype] = None, trust_remote_code: bool = False, lora_adapter_ids: Optional[list] = [], tokenizer_class: PreTrainedTokenizerBase = AutoTokenizer, config_class: PreTrainedTokenizerBase = AutoConfig, default_dtype=torch.float16, aliases=None, # Used for Santacoder override of config num_kv_heads: Optional[int] = None, # Deepseek V2 uses different QK and V dims. head_size: Optional[int] = None, skip_special_tokens: bool = True, ): self.quantize = quantize self.process_group, rank, world_size = initialize_torch_distributed() if torch.cuda.is_available(): device = torch.device(f"cuda:{rank}") dtype = default_dtype if dtype is None else dtype elif SYSTEM == "ipex": if hasattr(torch, "xpu") and torch.xpu.is_available(): device = torch.device(f"xpu:{rank}") dtype = default_dtype if dtype is None else dtype else: device = torch.device("cpu") # Float16 doesn't exist on target. dtype = torch.bfloat16 if dtype is None else dtype init_cpu_threads_env(rank_id=rank, world_size=world_size) else: raise NotImplementedError(f"{model_class} is only available on GPU") tokenizer = tokenizer_class.from_pretrained( model_id, revision=revision, padding_side="left", truncation_side="left", trust_remote_code=trust_remote_code, ) try: generation_config = GenerationConfig.from_pretrained( model_id, revision=revision, trust_remote_code=trust_remote_code ) if isinstance(generation_config.eos_token_id, (list, set)): # TODO Huge hack tokenizer._eos_token_ids = set(generation_config.eos_token_id) except Exception: pass config = config_class.from_pretrained( model_id, revision=revision, trust_remote_code=trust_remote_code ) config.quantize = quantize config.speculator = speculator torch.distributed.barrier(group=self.process_group) weights_loader = get_loader(quantize, model_id, revision) filenames = weight_files(model_id, revision=revision, extension=".safetensors") weights = Weights( filenames, device, dtype, process_group=self.process_group, aliases=aliases, weights_loader=weights_loader, ) prefix = "" model = model_class(prefix, config, weights) torch.distributed.barrier(group=self.process_group) # VLM models define the config we care about in their text_config text_config = getattr(config, "text_config", None) if text_config is not None: config = text_config if getattr(config, "sliding_window", None) is not None: set_sliding_window(config.sliding_window) else: config.sliding_window = None self.num_layers = config.num_hidden_layers self.num_heads = config.num_attention_heads // self.process_group.size() # Validation is done in the model itself if num_kv_heads is None: num_kv_heads = getattr(config, "num_key_value_heads", None) # GPT-2 workaround if num_kv_heads is None: num_kv_heads = getattr(config, "n_head", None) if num_kv_heads is None: raise ValueError("Cannot get the number of key/value heads") self.num_kv_heads = ( num_kv_heads // self.process_group.size() if num_kv_heads > 1 else num_kv_heads ) assert self.num_kv_heads > 0 if head_size is None: # Some models use GQA and different sizes for o_proj # and q_proj, that allows for that. if hasattr(config, "head_dim"): self.head_size = config.head_dim else: self.head_size = config.hidden_size // config.num_attention_heads else: self.head_size = head_size self.cuda_graphs = {} self.kv_cache = [] if ATTENTION == "flashinfer": from text_generation_server.layers.attention.flashinfer import ( create_prefill_state, create_decode_state, create_prefill_with_paged_kv_state, ) self.prefill_state = create_prefill_state(device=device) self.prefill_with_paged_kv_state = create_prefill_with_paged_kv_state( device=device ) self.decode_state = create_decode_state( device=device, num_heads=self.num_heads, num_kv_heads=self.num_kv_heads, ) super().__init__( model_id=model_id, model=model, tokenizer=tokenizer, requires_padding=False, dtype=dtype, device=device, rank=rank, world_size=world_size, sliding_window=config.sliding_window, ) @property def batch_type(self) -> Type[FlashCausalLMBatch]: return FlashCausalLMBatch def max_past(self) -> int: return getattr(self.model, "max_past", None) def init_kv_cache( self, num_blocks: int, num_layers: int, num_heads: int, head_size: int, dtype: torch.dtype, device: torch.device, ): self.kv_cache = [] empty_cache() element_size = torch.tensor([], dtype=dtype).element_size() if SYSTEM == "ipex" and device.type == "xpu": x = 1 else: x = BLOCK_SIZE // element_size if ATTENTION in {"flashdecoding", "flashinfer"}: self.kv_cache = [ ( torch.empty( (num_blocks, BLOCK_SIZE, num_heads, head_size), dtype=dtype, device=device, ), torch.empty( (num_blocks, BLOCK_SIZE, num_heads, head_size), dtype=dtype, device=device, ), ) for _ in range(num_layers) ] elif SYSTEM == "ipex" and device == torch.device("cpu"): self.kv_cache = [ ( torch.empty( (num_blocks, num_heads, BLOCK_SIZE, head_size), dtype=dtype, device=device, ), torch.empty( (num_blocks, num_heads, BLOCK_SIZE, head_size), dtype=dtype, device=device, ), ) for _ in range(num_layers) ] else: self.kv_cache = [ ( torch.empty( (num_blocks, num_heads, head_size // x, BLOCK_SIZE, x), dtype=dtype, device=device, ), torch.empty( (num_blocks, num_heads, head_size, BLOCK_SIZE), dtype=dtype, device=device, ), ) for _ in range(num_layers) ] def cuda_graph_warmup(self, bs: int, max_s: int, max_bt: int): input_ids = torch.zeros(bs, dtype=torch.int64, device=self.device) position_ids = torch.zeros(bs, dtype=torch.int32, device=self.device) slots = torch.arange(bs, dtype=torch.int64, device=self.device) input_lengths = [max_s] * bs prefix_lengths = [0] * bs input_lengths_tensor = ( torch.ones(bs, dtype=torch.int32, device=self.device) * max_s ) prefix_lengths_tensor = torch.zeros(bs, dtype=torch.int32, device=self.device) block_tables = torch.arange( max_bt, dtype=torch.int32, device=self.device ).repeat(bs) block_tables = block_tables.reshape((bs, max_bt)) if ATTENTION == "flashinfer": block_tables = block_tables_to_ragged( block_tables=block_tables, input_lengths=input_lengths, prefix_lens=prefix_lengths, ) self.cuda_graphs[bs] = { "input_ids": input_ids, "position_ids": position_ids, "kv_cache": self.kv_cache, "block_tables": block_tables, "slots": slots, "input_lengths": input_lengths_tensor, "prefix_lengths": prefix_lengths_tensor, } seqlen = Seqlen( input_lengths=input_lengths_tensor, prefix_lengths=prefix_lengths_tensor, cu_seqlen_q=None, max_q=1, max_k=max_s, ) graph = torch.cuda.CUDAGraph() self.cuda_graphs[bs]["graph"] = graph if ATTENTION == "flashinfer": from text_generation_server.layers.attention.flashinfer import ( create_decode_state_cuda_graphs, ) block_tables_ptr = torch.zeros( bs + 1, dtype=torch.int32, device=self.device ) last_page_len = torch.ones(bs, dtype=torch.int32, device=self.device) state = create_decode_state_cuda_graphs( device=input_ids.device, block_tables=block_tables, block_tables_ptr=block_tables_ptr, last_page_len=last_page_len, num_heads=self.num_heads, num_kv_heads=self.num_kv_heads, ) self.cuda_graphs[bs]["state"] = state else: state = None torch.cuda.synchronize() # Run once outside to warmup with self._forward_context( block_tables=block_tables, cu_seqlen_prefill=None, input_lengths=input_lengths, input_lengths_tensor=input_lengths_tensor, state=state, prefix_lens=prefix_lengths, prefix_lens_tensor=prefix_lengths_tensor, ): self.model.forward( input_ids=input_ids, position_ids=position_ids, cu_seqlen_prefill=None, kv_cache=self.kv_cache, block_tables=block_tables, slots=slots, seqlen=seqlen, max_s=max_s, prefill_cache_indices=None, lm_head_indices=None, ) torch.cuda.synchronize() with torch.cuda.graph(graph, pool=MEM_POOL): seqlen = Seqlen( input_lengths=input_lengths_tensor, prefix_lengths=prefix_lengths_tensor, cu_seqlen_q=None, max_q=1, max_k=max_s, ) logits, speculative_logits = self.model.forward( input_ids=input_ids, position_ids=position_ids, cu_seqlen_prefill=None, kv_cache=self.kv_cache, block_tables=block_tables, slots=slots, seqlen=seqlen, max_s=max_s, prefill_cache_indices=None, lm_head_indices=None, ) self.cuda_graphs[bs]["logits"] = logits self.cuda_graphs[bs]["speculative_logits"] = speculative_logits torch.cuda.synchronize() def warmup(self, batch: FlashCausalLMBatch): # The warmup batch is the biggest batch we could ever receive empty_cache() try: self.init_kv_cache( batch.num_blocks, self.num_layers, self.num_kv_heads, self.head_size, self.dtype, self.device, ) max_bt = batch.max_blocks max_s = max_bt * BLOCK_SIZE if SYSTEM == "rocm" and os.environ.get("PYTORCH_TUNABLEOP_ENABLED", False): torch.cuda.tunable.tuning_enable(False) _, batch, _ = self.generate_token(batch) except torch.cuda.OutOfMemoryError as e: raise RuntimeError( f"Not enough memory to handle {len(batch.input_ids)} prefill tokens. " f"You need to decrease `--max-batch-prefill-tokens`" ) from e synchronize(self.device) # Inspired by the original implementation in [vllm](https://github.com/vllm-project/vllm) # Calculate the number of blocks that can be allocated with the free memory dtype_size = torch.tensor([], dtype=self.dtype).element_size() cache_block_size = BLOCK_SIZE * self.num_kv_heads * self.head_size total_cache_size = self.num_layers * cache_block_size * 2 * dtype_size free_memory = get_free_memory(self.device, MEMORY_FRACTION) batch_num_blocks = batch.num_blocks if batch is not None else 0 num_blocks = ( # Leave 5% for some wiggle room int((free_memory * TGI_WIGGLE_ROOM) // total_cache_size) # Add batch.num_blocks as we allocated it above, so it is included in the peak memory. + batch_num_blocks ) del batch self.init_kv_cache( num_blocks, self.num_layers, self.num_kv_heads, self.head_size, self.dtype, self.device, ) if SYSTEM == "rocm": if ( os.environ.get("PYTORCH_TUNABLEOP_ENABLED") is None or os.environ.get("PYTORCH_TUNABLEOP_ENABLED") == "1" ): torch.cuda.tunable.enable() if os.environ.get("PYTORCH_TUNABLEOP_TUNING") != "0": torch.cuda.tunable.tuning_enable(True) if os.environ.get("PYTORCH_TUNABLEOP_SEQLENS") is not None: tuning_sequences = [ int(val) for val in os.environ["PYTORCH_TUNABLEOP_SEQLENS"].split(",") ] elif CUDA_GRAPHS is not None: tuning_sequences = CUDA_GRAPHS else: # For seqlen = 1, we dispatch to LLMM1 kernel. tuning_sequences = [2, 3, 4, 5, 6, 7] tunableop_filepath = os.path.join( HUGGINGFACE_HUB_CACHE, f"tunableop_{self.model_id.replace('/', '-')}_tp{self.world_size}_rank{self.rank}.csv", ) log_master( logger.info, f"PyTorch TunableOp (https://github.com/fxmarty/pytorch/tree/2.3-patched/aten/src/ATen/cuda/tunable) is enabled. The warmup may take several minutes, picking the ROCm optimal matrix multiplication kernel for the target lengths {', '.join([str(seqlen) for seqlen in tuning_sequences])}, with typical 5-8% latency improvement for small sequence lengths. The picked GEMMs are saved in the file {tunableop_filepath}. To disable TunableOp, please launch TGI with `PYTORCH_TUNABLEOP_ENABLED=0`.", ) if os.path.isfile(tunableop_filepath): log_master( logger.info, f"The file {tunableop_filepath} already exists and will be reused.", ) torch.cuda.tunable.read_file(tunableop_filepath) os.makedirs(HUGGINGFACE_HUB_CACHE, exist_ok=True) for seqlen in tuning_sequences: log_master(logger.info, f"Warming up TunableOp for seqlen={seqlen}") self.tunableop_warmup(seqlen) torch.cuda.tunable.write_file(tunableop_filepath) torch.cuda.tunable.tuning_enable(False) else: log_master( logger.info, "PyTorch ROCm TunableOp (https://github.com/pytorch/pytorch/tree/main/aten/src/ATen/cuda/tunable) is disabled. TunableOp brings an additional 5-8% latency improvement for small sequence lengths but requires a warmup. If necessary, please use the environment variable PYTORCH_TUNABLEOP_ENABLED=1 to enable TunableOp.", ) if CUDA_GRAPHS: try: log_master( logger.info, f"Cuda Graphs are enabled for sizes {CUDA_GRAPHS}" ) # Warmup cuda graphs for bs in CUDA_GRAPHS: if self.speculate is None or self.speculate + 1 <= bs: self.cuda_graph_warmup(bs, max_s, max_bt) except torch.cuda.OutOfMemoryError: logger.exception("Decode cuda graph warmup failed") else: log_master( logger.info, f"Cuda Graphs are disabled (CUDA_GRAPHS={CUDA_GRAPHS})." ) return int(num_blocks * BLOCK_SIZE) def tunableop_warmup(self, seqlen: int): input_ids = torch.zeros(seqlen, dtype=torch.int64, device=self.device) position_ids = torch.zeros(seqlen, dtype=torch.int32, device=self.device) slots = torch.arange(seqlen, dtype=torch.int64, device=self.device) # Dummy value, some models (starcoder2) don't accept `None`. input_lengths = torch.ones(seqlen, dtype=torch.int32, device=self.device) prefix_lens_tensor = torch.zeros(seqlen, dtype=torch.int32, device=self.device) cu_seqlen_prefill = torch.tensor( [0, seqlen], device=self.device, dtype=torch.int32 ) seqlen = Seqlen( input_lengths=input_lengths, prefix_lengths=prefix_lens_tensor, cu_seqlen_q=cu_seqlen_prefill, max_q=1, max_k=seqlen, ) # We pass a `cu_seqlen_prefill` in order not to have to deal with paged attention cache allocation/deallocation. self.model.forward( input_ids=input_ids, position_ids=position_ids, cu_seqlen_prefill=cu_seqlen_prefill, kv_cache=self.kv_cache, block_tables=None, seqlen=seqlen, slots=slots, max_s=seqlen, lm_head_indices=None, prefill_cache_indices=None, ) def forward( self, batch: FlashCausalLMBatch, adapter_data: AdapterBatchData ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: # Model Forward if batch.speculative_ids is not None: input_ids = batch.input_ids position_ids = batch.position_ids cu_seqlen_prefill = batch.cu_seqlen_prefill kv_cache = self.kv_cache block_tables = batch.block_tables_tensor slots = batch.slots[batch.slot_indices] input_lengths = batch.input_lengths_tensor max_s = batch.max_seqlen lm_head_indices = batch.prefill_head_indices speculative_ids = batch.speculative_ids B, speculative_length = speculative_ids.shape new_length = speculative_length + 1 new_input_ids = torch.cat( [input_ids.unsqueeze(-1), speculative_ids], dim=1 ).reshape(-1) arange = torch.arange(new_length, device=position_ids.device).unsqueeze(0) arange_int = arange.to(dtype=torch.int32) new_position_ids = ( position_ids.unsqueeze(-1).expand(B, new_length) + arange ).view(-1) slots = (slots.unsqueeze(-1).expand(B, new_length) + arange_int).view(-1) input_lengths = ( input_lengths.unsqueeze(-1).expand(B, new_length) + arange_int ).view(-1) prefix_lens_tensor = ( batch.prefix_lens_tensor.unsqueeze(-1).expand(B, new_length) ).reshape(-1) # Add Copy the block tables for all members block_tables = ( block_tables.unsqueeze(1) .expand(B, new_length, -1) .reshape(B * new_length, -1) .contiguous() ) max_s = max_s + speculative_length input_ids = new_input_ids position_ids = new_position_ids else: input_ids = batch.input_ids position_ids = batch.position_ids cu_seqlen_prefill = batch.cu_seqlen_prefill kv_cache = self.kv_cache block_tables = batch.block_tables_tensor slots = batch.slots[batch.slot_indices] input_lengths = batch.input_lengths_tensor prefix_lens_tensor = batch.prefix_lens_tensor max_s = batch.max_seqlen lm_head_indices = batch.prefill_head_indices if cu_seqlen_prefill is None and self.max_past() is not None: # In decode, not prefill, we're actually overwriting the KV-cache # in a circular buffer mode. # This makes sure the max_s for the decode pass is correct. max_s = min(self.max_past(), max_s) bs = input_ids.shape[0] sorted_padded_bs = sorted([k for k in self.cuda_graphs.keys() if k >= bs]) if sorted_padded_bs: # Get associated cuda graph cuda_graph = self.cuda_graphs[sorted_padded_bs[0]] else: cuda_graph = None if cu_seqlen_prefill is not None or cuda_graph is None: if ATTENTION == "flashinfer": block_tables = block_tables_to_ragged( block_tables=block_tables, input_lengths=batch.input_lengths, prefix_lens=batch.prefix_lens, ) with self._forward_context( block_tables=block_tables, cu_seqlen_prefill=cu_seqlen_prefill, input_lengths=batch.input_lengths, input_lengths_tensor=input_lengths + prefix_lens_tensor, prefix_lens=batch.prefix_lens, prefix_lens_tensor=prefix_lens_tensor, ): max_k = (input_lengths + prefix_lens_tensor).max().item() seqlen = Seqlen( input_lengths=input_lengths, prefix_lengths=prefix_lens_tensor, cu_seqlen_q=cu_seqlen_prefill, max_q=max_s, max_k=max_k, ) logits, speculative_logits = self.model.forward( input_ids=input_ids, position_ids=position_ids, cu_seqlen_prefill=cu_seqlen_prefill, kv_cache=kv_cache, block_tables=block_tables, slots=slots, seqlen=seqlen, max_s=max_s, prefill_cache_indices=batch.prefill_cache_indices, lm_head_indices=lm_head_indices, adapter_data=adapter_data, ) if batch.prefill_cache_indices is not None: batch.prefill_cache_indices = None return logits, speculative_logits # Copy inputs to the static inputs of the cuda graph # Static inputs are potentially padded cuda_graph["input_ids"][: input_ids.shape[0]] = input_ids cuda_graph["position_ids"][: position_ids.shape[0]] = position_ids if ATTENTION == "flashinfer": block_tables = block_tables_to_ragged( block_tables=block_tables, input_lengths=batch.input_lengths, prefix_lens=batch.prefix_lens, ) cuda_graph["block_tables"][: block_tables.shape[0]] = block_tables else: cuda_graph["block_tables"][ : block_tables.shape[0], : block_tables.shape[1] ] = block_tables cuda_graph["slots"].fill_(-1) cuda_graph["slots"][: slots.shape[0]] = slots cuda_graph["input_lengths"].zero_() cuda_graph["input_lengths"][: input_lengths.shape[0]] = ( input_lengths + prefix_lens_tensor ) with self._forward_context( block_tables=cuda_graph["block_tables"], cu_seqlen_prefill=None, input_lengths=batch.input_lengths, input_lengths_tensor=cuda_graph["input_lengths"], prefix_lens=batch.prefix_lens, prefix_lens_tensor=prefix_lens_tensor, state=cuda_graph.get("state"), ): # Replay the graph cuda_graph["graph"].replay() # Slice output to the correct shape speculative_logits = ( cuda_graph["speculative_logits"][:bs] if cuda_graph["speculative_logits"] is not None else None ) logits = cuda_graph["logits"][:bs] return logits, speculative_logits @tracer.start_as_current_span("generate_token") def generate_token( self, batch: FlashCausalLMBatch ) -> Tuple[List[Generation], Optional[FlashCausalLMBatch], Tuple[int, int]]: start = time.time_ns() prefill = batch.cu_seqlen_prefill is not None prefill_logprobs = batch.prefill_next_token_indices is not None # Update adapter indices for speculative tokens (if present) adapter_meta = batch.adapter_meta if batch.speculative_ids is not None: B, speculative_length = batch.speculative_ids.shape new_length = speculative_length + 1 adapter_indices = ( adapter_meta.adapter_indices.unsqueeze(-1) .expand(B, new_length) .reshape(-1) ) adapter_segments = adapter_meta.adapter_segments * new_length adapter_meta = AdapterBatchMetadata( adapter_indices=adapter_indices, adapter_set=adapter_meta.adapter_set, adapter_segments=adapter_segments, segment_indices=adapter_meta.segment_indices, ) # Assign pointers to adapter weights # TODO(travis): don't update this if indices haven't changed adapter_data = AdapterBatchData.from_meta( adapter_meta, self.layer_to_adapter_weights, prefill, batch.prefill_head_indices, ) out, speculative_logits = self.forward(batch, adapter_data) if prefill: next_token_logits = ( out[batch.prefill_next_token_indices] if prefill_logprobs else out ) if speculative_logits is not None: speculative_logits = ( speculative_logits[batch.prefill_next_token_indices] if prefill_logprobs else speculative_logits ) next_adapter_indices = batch.adapter_meta.adapter_indices.new_empty( len(batch) ) else: next_token_logits = out next_adapter_indices = batch.adapter_meta.adapter_indices speculate = get_speculate() ( next_input_ids, next_token_logprobs, logprobs, accepted_ids, speculative_ids, ) = batch.next_token_chooser( batch.all_input_ids_tensor[:, : batch.max_seqlen], next_token_logits, speculate, batch.speculative_ids, speculative_logits, ) batch_top_token_ids, batch_top_token_logprobs = batch_top_tokens( batch.top_n_tokens, batch.top_n_tokens_tensor, logprobs, accepted_ids ) if prefill: if len(batch) > 1 and prefill_logprobs: # We create the prefill_tokens_indices tensor that will be used to gather prefill logprobs # When batch == 1, we will just use the batch.input_ids values directly prefill_tokens_indices = batch.input_ids.new_zeros(len(out)) next_position_ids = batch.position_ids.new_empty(len(batch)) batch.slot_indices = batch.slot_indices[batch.cu_seqlen_prefill[1:] - 1] # We do not need cu_seqlen_prefill anymore batch.cu_seqlen_prefill = None else: prefill_logprobs = None next_position_ids = batch.position_ids # Cumulative length cumulative_length = 0 # Results generations: List[Generation] = [] stopped = True # Zipped iterator iterator = zip(batch.input_lengths, batch.all_input_ids, accepted_ids) # We do two for loops as the first one can run completely asynchronously from the GPU while for the second # one, we need to first do a GPU <-> CPU sync # It is faster if we delay this sync for the maximum amount of time # For each member of the batch index = 0 for i, (input_length, all_input_ids, n_accepted_ids) in enumerate(iterator): # Indexing metadata start_index = cumulative_length end_index = cumulative_length + input_length if prefill: # Indexing metadata out_start_index = batch.prefill_cu_outlens[i] out_end_index = batch.prefill_cu_outlens[i + 1] out_length = out_end_index - out_start_index # Initialize position_ids # In decode, we do not need this as we can just increment position ids next_position_ids[i] = batch.position_ids[end_index - 1] # Initialize adapter indices # In decode, we only have one token per row in the batch, so grab last index next_adapter_indices[i] = batch.adapter_meta.adapter_indices[ end_index - 1 ] # Used to gather prefill logprobs # Copy batch.input_ids to prefill_token_indices if prefill_logprobs: if len(batch) > 1: prefill_tokens_indices[out_start_index : out_end_index - 1] = ( batch.input_ids[start_index + 1 : start_index + out_length] ) else: # Set prefill_tokens_indices to the correct slice prefill_tokens_indices = batch.input_ids[ start_index + 1 : start_index + out_length ] for j in range(n_accepted_ids): batch.all_input_ids_tensor[i, input_length + j] = next_input_ids[index] index += 1 cumulative_length += input_length # Update values batch.input_ids = next_input_ids[accepted_ids.cumsum(dim=-1) - 1] batch.speculative_ids = speculative_ids batch.position_ids = next_position_ids + accepted_ids batch.input_lengths_tensor += accepted_ids batch.slot_indices += accepted_ids batch.adapter_meta.adapter_indices = next_adapter_indices if prefill: # adjust segment lengths to account for all request lengths being 1 during decoding adapter_segments, _ = find_segments(batch.adapter_meta.adapter_indices) batch.adapter_meta.adapter_segments = torch.tensor( adapter_segments, dtype=torch.int32, device=batch.adapter_meta.adapter_segments.device, ) if prefill and prefill_logprobs: # Get prefill logprobs prefill_logprobs_tensor = torch.log_softmax(out, -1) prefill_logprobs = torch.gather( prefill_logprobs_tensor, 1, prefill_tokens_indices.view(-1, 1) ) # GPU <-> CPU sync prefill_logprobs = prefill_logprobs.view(-1).tolist() # GPU <-> CPU sync next_token_logprobs = next_token_logprobs.tolist() next_token_ids = next_input_ids.tolist() accepted_ids = accepted_ids.tolist() start_decode = time.time_ns() # Zipped iterator iterator = zip( batch.requests, batch.input_lengths, batch.prefix_offsets, batch.read_offsets, batch.stopping_criterias, batch.all_input_ids, batch.prefix_ids, batch.next_token_chooser.do_sample, batch.next_token_chooser.seeds, batch.top_n_tokens, accepted_ids, batch_top_token_ids, batch_top_token_logprobs, ) # For each member of the batch index = 0 for i, ( request, input_length, prefix_offset, read_offset, stopping_criteria, all_input_ids, prefix_ids, do_sample, seed, top_n_tokens, n_accepted_ids, top_token_ids, top_token_logprobs, ) in enumerate(iterator): # Append next token to all tokens next_token_texts = [] left = 0 if n_accepted_ids > 1: log_master(logger.debug, f"Speculated ids {n_accepted_ids - 1}") current_stopped = False for j in range(index, index + n_accepted_ids): # Generated token next_token_id = next_token_ids[j] all_input_ids.append(next_token_id) next_token_text, prefix_offset, read_offset = self.decode_token( all_input_ids, prefix_offset, read_offset, ) next_token_texts.append(next_token_text) stop, reason = stopping_criteria( next_token_id, next_token_text, ) if stop: left = index + n_accepted_ids - j - 1 current_stopped = True break else: current_stopped = False stopped = stopped and current_stopped _next_token_ids = next_token_ids[index : index + n_accepted_ids - left] _next_token_logprobs = next_token_logprobs[ index : index + n_accepted_ids - left ] index += n_accepted_ids # Shard generations # All generations will be appended in the rust sharded client if i % self.world_size == self.rank: if stop: # Decode generated tokens output_text, _, _ = self.decode_token( all_input_ids, prefix_offset=len(all_input_ids) - stopping_criteria.current_tokens - 1, read_offset=len(all_input_ids) - stopping_criteria.current_tokens, skip_special_tokens=True, ) generated_text = GeneratedText( output_text, stopping_criteria.current_tokens, reason, seed if do_sample else None, ) else: generated_text = None # Prefill if prefill and request.prefill_logprobs: out_start_index = batch.prefill_cu_outlens[i] out_end_index = batch.prefill_cu_outlens[i + 1] # Remove generated token to only have prefill and add nan for first prompt token request_prefill_logprobs = ( [float("nan")] * (len(prefix_ids) + 1) ) + prefill_logprobs[out_start_index : out_end_index - 1] prefill_token_ids = all_input_ids[:-1] prefill_texts = self.tokenizer.batch_decode( prefix_ids + prefill_token_ids, clean_up_tokenization_spaces=False, skip_special_tokens=False, ) prefill_tokens = Tokens( prefix_ids + prefill_token_ids, request_prefill_logprobs, prefill_texts, is_special=[], ) else: prefill_tokens = None if top_n_tokens > 0: all_top_tokens = [] for top_token_ids, top_token_logprobs in zip( top_token_ids, top_token_logprobs ): toptoken_texts = self.tokenizer.batch_decode( top_token_ids, clean_up_tokenization_spaces=False, skip_special_tokens=False, ) special_toptokens = [ token_id in self.all_special_ids for token_id in top_token_ids ] top_tokens = Tokens( top_token_ids, top_token_logprobs, toptoken_texts, special_toptokens, ) all_top_tokens.append(top_tokens) top_tokens = all_top_tokens else: top_tokens = None generation = Generation( request.id, prefill_tokens, Tokens( _next_token_ids, _next_token_logprobs, next_token_texts, [nid in self.all_special_ids for nid in _next_token_ids], ), generated_text, top_tokens, ) generations.append(generation) # accept each new token for this specific request since we may # have more than one new token per request with speculative decoding for next_token_id in _next_token_ids: batch.next_token_chooser = ( batch.next_token_chooser.advance_grammar_single(i, next_token_id) ) # Update values batch.input_lengths[i] = input_length + n_accepted_ids if batch.input_lengths[i] > batch.max_seqlen: batch.max_seqlen = batch.input_lengths[i] batch.prefix_offsets[i] = prefix_offset batch.read_offsets[i] = read_offset batch.all_input_ids[i] = all_input_ids if stopped: # No need to return a batch if we know that all requests stopped forward_ns = start_decode - start decode_ns = time.time_ns() - start_decode return generations, None, (forward_ns, decode_ns) batch.prefill_cu_outlens = None batch.prefill_head_indices = None batch.prefill_next_token_indices = None forward_ns = start_decode - start decode_ns = time.time_ns() - start_decode return generations, batch, (forward_ns, decode_ns) def _forward_context( self, *, block_tables: torch.Tensor, cu_seqlen_prefill: Optional[torch.Tensor], input_lengths: List[int], input_lengths_tensor: torch.Tensor, prefix_lens: List[int], prefix_lens_tensor: torch.Tensor, state: Optional[Any] = None, ) -> ContextManager: if ATTENTION != "flashinfer": return nullcontext() from text_generation_server.layers.attention.flashinfer import ( use_decode_state, use_prefill_with_paged_kv_state, ) # has_prefix_lens = any(prefix_len > 0 for prefix_len in prefix_lens) if cu_seqlen_prefill is not None: return use_prefill_with_paged_kv_state( state=( state if state is not None else self.prefill_with_paged_kv_state ), # block_tables=block_tables_to_ragged( # block_tables=block_tables, # input_lengths=input_lengths, # prefix_lens=prefix_lens, # ), block_tables=block_tables, cu_seqlens=cu_seqlen_prefill, input_lengths=input_lengths_tensor, num_heads=self.num_heads, num_kv_heads=self.num_kv_heads, head_size=self.head_size, page_size=BLOCK_SIZE, ) else: assert input_lengths_tensor is not None return use_decode_state( state=state if state is not None else self.decode_state, input_lengths=input_lengths_tensor, block_tables=block_tables, num_heads=self.num_heads, num_kv_heads=self.num_kv_heads, head_size=self.head_size, page_size=BLOCK_SIZE, ) def block_tables_to_ragged( *, block_tables: torch.Tensor, input_lengths: List[int], prefix_lens: List[int] ) -> torch.Tensor: """Convert block table to ragged format compatible with FlashInfer.""" assert len(input_lengths) == len(prefix_lens) total_len = sum(input_lengths) + sum(prefix_lens) block_tables_ragged = torch.empty( total_len, dtype=torch.int32, device=block_tables.device ) offset = 0 for i, (input_length, prefix_len) in enumerate(zip(input_lengths, prefix_lens)): seq_len = prefix_len + input_length block_tables_ragged[offset : offset + seq_len] = block_tables[i][:seq_len] offset += seq_len return block_tables_ragged
text-generation-inference/server/text_generation_server/models/flash_causal_lm.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/models/flash_causal_lm.py", "repo_id": "text-generation-inference", "token_count": 39451 }
244
from typing import Iterable from loguru import logger from text_generation_server.pb import generate_pb2 def concat_text_chunks(chunks: Iterable[generate_pb2.InputChunk]) -> str: """ Concatenate text in text chunks. Non-text chunks are dropped. """ text = None for chunk in chunks: chunk_type = chunk.WhichOneof("chunk") if chunk_type == "text": if text is None: text = chunk.text else: raise NotImplementedError("Request contained more than one text chunk") else: # We cannot reject this, e.g. warmup sends an image chunk. logger.debug(f"Encountered non-text chunk type {chunk_type}") if text is None: raise NotImplementedError("Request without a text chunk") return text
text-generation-inference/server/text_generation_server/utils/chunks.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/utils/chunks.py", "repo_id": "text-generation-inference", "token_count": 332 }
245
import torch from abc import ABC, abstractmethod from contextlib import contextmanager from pathlib import Path from typing import Dict, List, Optional, Union, Type from safetensors import safe_open from dataclasses import dataclass from text_generation_server.utils.import_utils import SYSTEM class WeightsLoader(ABC): """ Instances of this type implement higher-level weight loading. At a low-level, every weight is stored in the Safetensors format. The interpretation of weights may be different however, for instance could be packed, quantized weights. Loaders are responsible for interpreting the raw tensors, sharding tensors in a manner compatible with the format, etc. """ @abstractmethod def get_weights(self, weights: "Weights", prefix: str): """ Get weights at the given prefix and apply without tensor paralllism. """ ... @abstractmethod def get_weights_col_packed( self, weights: "Weights", prefix: str, block_sizes: Union[int, List[int]], ): """ Get the packed weights at the given prefix with column-splitting for tensor parallelism. This method should be used when multiple different weights are packed into a tensor, for instance, query/key/value weights or a gate/up projection. The `block_sizes` determines the proportions of the packed tensors. The columns are split in equally sized blocks when `block_sizes` is an `int`, or in blocks proportional given to the sizes. For instance `[2, 1, 1]` will divide an input with dimensionality `1024` in `[512, 256, 256]`. """ ... def get_weights_col(self, weights: "Weights", prefix: str): """ Get weights at the given prefix and apply column-splitting for tensor paralllism. """ return weights.get_multi_weights_col([prefix], 0) @abstractmethod def get_multi_weights_col(self, weights: "Weights", prefixes: List[str], dim: int): """ Get the weights at the given prefixes, column-split them for tensor parallelim, and then concatenate the weights along the given dimension. """ ... @abstractmethod def get_weights_row(self, weights: "Weights", prefix: str): """ Get the weights at the given prefix and apply row-splitting for tensor parallism. """ ... class Weight(ABC): """Instances of this type implement unquantized/quantized/to-be quantized weights.""" @abstractmethod def get_linear(self, bias: torch.Tensor): """Create a linear layer from this weight.""" ... @dataclass class UnquantizedWeight(Weight): weight: torch.Tensor def get_linear(self, bias: torch.Tensor): from text_generation_server.layers.linear import FastLinear, FastLinearROCm if SYSTEM == "rocm": return FastLinearROCm(self.weight, bias) else: return FastLinear(self.weight, bias) class DefaultWeightsLoader(WeightsLoader): """Weight loader that loads (unquantized) Torch tensors.""" def __init__(self, weight_class: Type[UnquantizedWeight]): """Create a loader. Weights will be wrapped using the given `weights_class`, normally this will be `UnquantizedWeight`, but a quantizer-specific class such as `Fp8Weight` can be used to quantize the weights during loading. """ self.weight_class = weight_class """ Loader that uses tensors as-is with the exception of applying sharding and/or concatenation. """ def get_weights(self, weights: "Weights", prefix: str): return weights.get_tensor(f"{prefix}.weight") def get_weights_col_packed( self, weights: "Weights", prefix: str, block_sizes: Union[int, List[int]], ): return self.weight_class( weights.get_packed_sharded( f"{prefix}.weight", dim=0, block_sizes=block_sizes ), ) def get_multi_weights_col(self, weights: "Weights", prefixes: List[str], dim: int): w = [weights.get_sharded(f"{p}.weight", dim=0) for p in prefixes] return self.weight_class(torch.cat(w, dim=dim)) def get_weights_row(self, weights: "Weights", prefix: str): return self.weight_class( weights.get_sharded(f"{prefix}.weight", dim=1), ) class Weights: def __init__( self, filenames: List[Path], device, dtype, process_group, weights_loader: WeightsLoader, aliases: Optional[Dict[str, List[str]]] = None, prefix: Optional[str] = None, ): routing = {} for filename in filenames: with safe_open(filename, framework="pytorch") as f: for k in f.keys(): if k in routing: raise RuntimeError( f"Key {k} was found in multiple files: {filename} and {routing[k]}" ) routing[k] = filename if aliases is None: aliases = {} self.aliases = aliases self.routing = routing self.device = device self.dtype = dtype self.process_group = process_group self.prefix = prefix self.weights_loader = weights_loader self._handles = {} def _get_handle(self, filename): if filename not in self._handles: f = safe_open(filename, framework="pytorch") self._handles[filename] = f return self._handles[filename] def get_filename(self, tensor_name: str) -> (str, str): names = [tensor_name] if self.prefix is not None: prefixed = f"{self.prefix}.{tensor_name}" names.append(prefixed) for name in names: filename = self.routing.get(name, None) if filename is not None: return str(filename), name aliases = self.aliases.get(name, []) for alias in aliases: filename = self.routing.get(alias, None) if filename is not None: return str(filename), alias raise RuntimeError(f"weight {tensor_name} does not exist") def _get_slice(self, tensor_name: str): filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) slice_ = f.get_slice(tensor_name) return slice_ def _has_tensor(self, tensor_name: str): try: self.get_filename(tensor_name) except Exception: return False return True def get_shape(self, tensor_name: str): return self._get_slice(tensor_name).get_shape() def get_tensor(self, tensor_name: str, to_device=True, to_dtype=True): filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) tensor = f.get_tensor(tensor_name) # Special case for gptq which shouldn't convert # u4 which are disguised as int32. Exl2 uses int16 # as well. FP8 uses torch.float8_e4m3fn if ( tensor.dtype not in [ torch.float8_e4m3fn, torch.int16, torch.int32, torch.int64, ] and to_dtype ): tensor = tensor.to(dtype=self.dtype) if to_device: tensor = tensor.to(device=self.device) return tensor def get_partial_sharded( self, tensor_name: str, dim: int, to_device=True, to_dtype=True ): filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) slice_ = f.get_slice(tensor_name) world_size = self.process_group.size() rank = self.process_group.rank() size = slice_.get_shape()[dim] block_size = (size + world_size - 1) // world_size start = rank * block_size stop = (rank + 1) * block_size if dim == 0: tensor = slice_[start:stop] elif dim == 1: tensor = slice_[:, start:stop] else: raise NotImplementedError("Let's make that generic when needed") # Special case for gptq which shouldn't convert # u4 which are disguised as int32. exl2 uses int16. # FP8 uses torch.float8_e4m3fn. if ( tensor.dtype not in (torch.float8_e4m3fn, torch.int16, torch.int32) and to_dtype ): tensor = tensor.to(dtype=self.dtype) if to_device: tensor = tensor.to(device=self.device) return tensor def get_sharded(self, tensor_name: str, dim: int, to_device=True, to_dtype=True): filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) slice_ = f.get_slice(tensor_name) world_size = self.process_group.size() size = slice_.get_shape()[dim] assert ( size % world_size == 0 ), f"The choosen size {size} is not compatible with sharding on {world_size} shards" return self.get_partial_sharded( tensor_name, dim, to_device=to_device, to_dtype=to_dtype ) def get_packed_sharded( self, tensor_name: str, dim: int, block_sizes: Union[int, List[int]], to_dtype=True, ) -> torch.Tensor: """ Get a shard from a tensor that packs multiple tensors. When a tensor packs multiple tensors (such as QKV or an up projection + gate projection), sharding with `get_sharded` is not safe since it would not split the packed tensors across shards. This method shards a tensor, such that the packed tensors are split across shards. The columns are split in equally sized blocks when blocks is an `int`, or in blocks proportional given to the sizes. For instance `[2, 1, 1]` will divide an input with dimensionality `1024` in `[512, 256, 256]`. This is convenient for e.g. splitting QKV without knowing the storage details of quantized weights. """ slice_ = self._get_slice(tensor_name) total_size = slice_.get_shape()[dim] block_sizes = _blocks_to_block_sizes(total_size=total_size, blocks=block_sizes) world_size = self.process_group.size() rank = self.process_group.rank() tensors = [] block_offset = 0 for block_size in block_sizes: assert ( block_size % world_size == 0 ), f"Prepacked tensor cannot be sharded across {world_size} shards" shard_block_size = block_size // world_size start = rank * shard_block_size stop = (rank + 1) * shard_block_size if dim == 0: tensor = slice_[block_offset + start : block_offset + stop] elif dim == 1: tensor = slice_[:, block_offset + start : block_offset + stop] else: raise NotImplementedError("Currently only dim=0 or dim=1 is supported") tensors.append(tensor) block_offset += block_size tensor = torch.cat(tensors, dim=dim) tensor = tensor.to(device=self.device) # Avoid casting quantizer dtypes. if ( tensor.dtype not in [ torch.float8_e4m3fn, torch.int16, torch.int32, torch.int64, ] and to_dtype ): tensor = tensor.to(dtype=self.dtype) return tensor def get_weights(self, prefix: str): return self.weights_loader.get_weights(self, prefix) def get_weights_col_packed_qkv( self, prefix: str, num_heads: int, num_key_value_heads: int, ): return self.get_weights_col_packed( prefix, [num_heads, num_key_value_heads, num_key_value_heads] ) def get_weights_col_packed_gate_up(self, prefix: str): return self.get_weights_col_packed(prefix, 2) def get_weights_col_packed(self, prefix: str, block_sizes: Union[int, List[int]]): """ The columns are split in equally sized blocks when blocks is an `int`, or in blocks proportional given to the sizes. For instance `[2, 1, 1]` will divide an input with dimensionality `1024` in `[512, 256, 256]`. This is convenient for e.g. splitting QKV without knowing the storage details of quantized weights. """ return self.weights_loader.get_weights_col_packed(self, prefix, block_sizes) def get_weights_col(self, prefix: str): return self.weights_loader.get_weights_col(self, prefix) def get_multi_weights_col(self, prefixes: List[str], dim: int): return self.weights_loader.get_multi_weights_col(self, prefixes, dim) def get_tensor_shard(self, var, dim): world_size = self.process_group.size() rank = self.process_group.rank() block_size = var.size()[dim] // world_size start = rank * block_size stop = (rank + 1) * block_size if dim == 0: tensor = var[start:stop] elif dim == 1: tensor = var[:, start:stop] else: raise NotImplementedError("Let's make that generic when needed") tensor = tensor.to(dtype=self.dtype) tensor = tensor.to(device=self.device) return tensor def get_weights_row(self, prefix: str): return self.weights_loader.get_weights_row(self, prefix) @contextmanager def use_loader(self, weights_loader: WeightsLoader): """ This method is a context manager that can be used to use `Weights` with a different loader for the duration of the context. """ old_loader = self.weights_loader self.weights_loader = weights_loader try: yield finally: self.weights_loader = old_loader def _blocks_to_block_sizes(total_size: int, blocks: Union[int, List[int]]) -> List[int]: """ Convert block count or proportions to block sizes. This function accepts - The number of blocks (int), in which case the block size is total_size//blocks; or - A list of block sizes (List[int]). In the latter case, if sum(blocks) < total_size, the ratios between the block sizes will be preserved. For instance, if blocks is [2, 1, 1] and total_size is 1024, the returned block sizes are [512, 256, 256]. """ if isinstance(blocks, list): total_blocks = sum(blocks) assert ( total_size % total_blocks == 0 ), f"Cannot split {total_size} in proportional blocks: {blocks}" part_size = total_size // total_blocks return [part_size * block for block in blocks] else: assert total_size % blocks == 0, f"Prepacked is not divisible by {blocks}" single_size = total_size // blocks return [single_size] * blocks
text-generation-inference/server/text_generation_server/utils/weights.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/utils/weights.py", "repo_id": "text-generation-inference", "token_count": 6634 }
246
.PHONY: style check-style test DATA_DIR = data dir_guard=@mkdir -p $(@D) # Format source code automatically style: npm run lint # Check the source code is formatted correctly check-style: npm run lint-check TESTS_RESOURCES = $(DATA_DIR)/small.txt $(DATA_DIR)/roberta.json $(DATA_DIR)/tokenizer-wiki.json $(DATA_DIR)/bert-wiki.json # Launch the test suite test: $(TESTS_RESOURCES) npm run test $(DATA_DIR)/big.txt : $(dir_guard) wget https://norvig.com/big.txt -O $@ $(DATA_DIR)/small.txt : $(DATA_DIR)/big.txt head -100 $(DATA_DIR)/big.txt > $@ $(DATA_DIR)/roberta.json : $(dir_guard) wget https://huggingface.co/roberta-large/raw/main/tokenizer.json -O $@ $(DATA_DIR)/tokenizer-wiki.json : $(dir_guard) wget https://s3.amazonaws.com/models.huggingface.co/bert/anthony/doc-quicktour/tokenizer.json -O $@ $(DATA_DIR)/bert-wiki.json : $(dir_guard) wget https://s3.amazonaws.com/models.huggingface.co/bert/anthony/doc-pipeline/tokenizer.json -O $@
tokenizers/bindings/node/Makefile/0
{ "file_path": "tokenizers/bindings/node/Makefile", "repo_id": "tokenizers", "token_count": 406 }
247
import { byteLevelPreTokenizer, metaspacePreTokenizer, punctuationPreTokenizer, sequencePreTokenizer, splitPreTokenizer, whitespaceSplitPreTokenizer, } from '../../' describe('byteLevelPreTokenizer', () => { it('instantiates correctly', () => { const processor = byteLevelPreTokenizer() expect(processor.constructor.name).toEqual('PreTokenizer') }) }) describe('metaspacePreTokenizer', () => { it('instantiates correctly without any parameter', () => { const processor = metaspacePreTokenizer() expect(processor.constructor.name).toEqual('PreTokenizer') }) it('accepts `undefined` as first parameter', () => { expect(metaspacePreTokenizer(undefined)).toBeDefined() }) it('accepts `undefined` as second parameter', () => { expect(metaspacePreTokenizer('t', undefined)).toBeDefined() }) it('can pre-tokenize strings', () => { const pretok = metaspacePreTokenizer() expect(pretok.preTokenizeString('Hello there friend')).toEqual([ ['โ–Hello', [0, 5]], ['โ–there', [5, 11]], ['โ–friend', [11, 18]], ]) }) }) describe('punctuationPreTokenizer', () => { it('instantiates correctly without any parameter', () => { const processor = punctuationPreTokenizer() expect(processor.constructor.name).toEqual('PreTokenizer') }) it('instantiates correctly with non-default split delimeter', () => { const processor = punctuationPreTokenizer('removed') expect(processor.constructor.name).toEqual('PreTokenizer') }) }) describe('splitPreTokenizer', () => { it('instantiates correctly with invert parameter', () => { const processor = splitPreTokenizer(' ', 'mergedWithPrevious', false) expect(processor.constructor.name).toEqual('PreTokenizer') }) }) describe('sequencePreTokenizer', () => { it('instantiates correctly', () => { const punctuation = punctuationPreTokenizer() const whitespace = whitespaceSplitPreTokenizer() const sequence2 = sequencePreTokenizer([]) expect(sequence2.constructor.name).toEqual('PreTokenizer') const sequence3 = sequencePreTokenizer([punctuation, whitespace]) expect(sequence3.constructor.name).toEqual('PreTokenizer') }) })
tokenizers/bindings/node/lib/bindings/pre-tokenizers.test.ts/0
{ "file_path": "tokenizers/bindings/node/lib/bindings/pre-tokenizers.test.ts", "repo_id": "tokenizers", "token_count": 728 }
248
{ "name": "tokenizers-linux-arm64-gnu", "version": "0.13.4-rc1", "os": [ "linux" ], "cpu": [ "arm64" ], "main": "tokenizers.linux-arm64-gnu.node", "files": [ "tokenizers.linux-arm64-gnu.node" ], "description": "Tokenizers platform specific bindings", "keywords": [ "napi-rs", "NAPI", "N-API", "Rust", "node-addon", "node-addon-api" ], "license": "MIT", "engines": { "node": ">= 10" }, "publishConfig": { "registry": "https://registry.npmjs.org/", "access": "public" }, "repository": "tokenizers", "libc": [ "glibc" ] }
tokenizers/bindings/node/npm/linux-arm64-gnu/package.json/0
{ "file_path": "tokenizers/bindings/node/npm/linux-arm64-gnu/package.json", "repo_id": "tokenizers", "token_count": 289 }
249
use crate::arc_rwlock_serde; use serde::{Deserialize, Serialize}; extern crate tokenizers as tk; use napi::bindgen_prelude::*; use napi_derive::napi; use std::sync::{Arc, RwLock}; use tk::decoders::DecoderWrapper; /// Decoder #[derive(Clone, Serialize, Deserialize)] #[napi] pub struct Decoder { #[serde(flatten, with = "arc_rwlock_serde")] decoder: Option<Arc<RwLock<DecoderWrapper>>>, } #[napi] impl Decoder { #[napi] pub fn decode(&self, tokens: Vec<String>) -> Result<String> { use tk::Decoder; self .decoder .as_ref() .unwrap() .read() .unwrap() .decode(tokens) .map_err(|e| Error::from_reason(format!("{}", e))) } } impl tk::Decoder for Decoder { fn decode_chain(&self, tokens: Vec<String>) -> tk::Result<Vec<String>> { self .decoder .as_ref() .ok_or("Uninitialized Decoder")? .read() .unwrap() .decode_chain(tokens) } } #[napi] pub fn bpe_decoder(suffix: Option<String>) -> Decoder { let suffix = suffix.unwrap_or("</w>".to_string()); let decoder = Some(Arc::new(RwLock::new( tk::decoders::bpe::BPEDecoder::new(suffix).into(), ))); Decoder { decoder } } #[napi] pub fn byte_fallback_decoder() -> Decoder { Decoder { decoder: Some(Arc::new(RwLock::new( tk::decoders::byte_fallback::ByteFallback::new().into(), ))), } } #[napi] pub fn ctc_decoder( #[napi(ts_arg_type = "string = '<pad>'")] pad_token: Option<String>, word_delimiter_token: Option<String>, cleanup: Option<bool>, ) -> Decoder { let pad_token = pad_token.unwrap_or("<pad>".to_string()); let word_delimiter_token = word_delimiter_token.unwrap_or("|".to_string()); let cleanup = cleanup.unwrap_or(true); let decoder = Some(Arc::new(RwLock::new( tk::decoders::ctc::CTC::new(pad_token, word_delimiter_token, cleanup).into(), ))); Decoder { decoder } } #[napi] pub fn fuse_decoder() -> Decoder { Decoder { decoder: Some(Arc::new(RwLock::new( tk::decoders::fuse::Fuse::new().into(), ))), } } #[napi] pub fn metaspace_decoder( #[napi(ts_arg_type = "string = 'โ–'")] replacement: Option<String>, #[napi(ts_arg_type = "prepend_scheme = 'always'")] prepend_scheme: Option<String>, #[napi(ts_arg_type = "split = true")] split: Option<bool>, ) -> Result<Decoder> { use tk::pre_tokenizers::metaspace::PrependScheme; let split = split.unwrap_or(true); let replacement = replacement.unwrap_or("โ–".to_string()); if replacement.chars().count() != 1 { return Err(Error::from_reason( "replacement is supposed to be a single char", )); } let replacement = replacement.chars().next().unwrap(); let prepend_scheme: PrependScheme = match prepend_scheme.unwrap_or(String::from("always")).as_str() { "always" => PrependScheme::Always, "first" => PrependScheme::First, "never" => PrependScheme::Never, _ => { return Err(Error::from_reason( "prepend_scheme is supposed to be either 'always', 'first' or 'never'", )); } }; Ok(Decoder { decoder: Some(Arc::new(RwLock::new( tk::decoders::metaspace::Metaspace::new(replacement, prepend_scheme, split).into(), ))), }) } #[napi] pub fn replace_decoder(pattern: String, content: String) -> Result<Decoder> { Ok(Decoder { decoder: Some(Arc::new(RwLock::new( tk::normalizers::replace::Replace::new(pattern, content) .map_err(|e| Error::from_reason(e.to_string()))? .into(), ))), }) } #[napi] pub fn sequence_decoder(decoders: Vec<&Decoder>) -> Decoder { let sequence: Vec<tk::DecoderWrapper> = decoders .into_iter() .filter_map(|decoder| { decoder .decoder .as_ref() .map(|decoder| (**decoder).read().unwrap().clone()) }) .clone() .collect(); Decoder { decoder: Some(Arc::new(RwLock::new(tk::DecoderWrapper::Sequence( tk::decoders::sequence::Sequence::new(sequence), )))), } } #[napi] pub fn strip_decoder(content: String, left: u32, right: u32) -> Result<Decoder> { let content: char = content.chars().next().ok_or(Error::from_reason( "Expected non empty string for strip pattern", ))?; Ok(Decoder { decoder: Some(Arc::new(RwLock::new( tk::decoders::strip::Strip::new(content, left as usize, right as usize).into(), ))), }) } #[napi] pub fn word_piece_decoder( #[napi(ts_arg_type = "string = '##'")] prefix: Option<String>, #[napi(ts_arg_type = "bool = true")] cleanup: Option<bool>, ) -> Decoder { let prefix = prefix.unwrap_or("##".to_string()); let cleanup = cleanup.unwrap_or(true); Decoder { decoder: Some(Arc::new(RwLock::new( tk::decoders::wordpiece::WordPiece::new(prefix, cleanup).into(), ))), } }
tokenizers/bindings/node/src/decoders.rs/0
{ "file_path": "tokenizers/bindings/node/src/decoders.rs", "repo_id": "tokenizers", "token_count": 2038 }
250
[target.x86_64-apple-darwin] rustflags = [ "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup", "-C", "link-arg=-mmacosx-version-min=10.11", ] [target.aarch64-apple-darwin] rustflags = [ "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup", "-C", "link-arg=-mmacosx-version-min=10.11", ]
tokenizers/bindings/python/.cargo/config.toml/0
{ "file_path": "tokenizers/bindings/python/.cargo/config.toml", "repo_id": "tokenizers", "token_count": 146 }
251
# Generated content DO NOT EDIT class AddedToken: """ Represents a token that can be be added to a :class:`~tokenizers.Tokenizer`. It can have special options that defines the way it should behave. Args: content (:obj:`str`): The content of the token single_word (:obj:`bool`, defaults to :obj:`False`): Defines whether this token should only match single words. If :obj:`True`, this token will never match inside of a word. For example the token ``ing`` would match on ``tokenizing`` if this option is :obj:`False`, but not if it is :obj:`True`. The notion of "`inside of a word`" is defined by the word boundaries pattern in regular expressions (ie. the token should start and end with word boundaries). lstrip (:obj:`bool`, defaults to :obj:`False`): Defines whether this token should strip all potential whitespaces on its left side. If :obj:`True`, this token will greedily match any whitespace on its left. For example if we try to match the token ``[MASK]`` with ``lstrip=True``, in the text ``"I saw a [MASK]"``, we would match on ``" [MASK]"``. (Note the space on the left). rstrip (:obj:`bool`, defaults to :obj:`False`): Defines whether this token should strip all potential whitespaces on its right side. If :obj:`True`, this token will greedily match any whitespace on its right. It works just like :obj:`lstrip` but on the right. normalized (:obj:`bool`, defaults to :obj:`True` with :meth:`~tokenizers.Tokenizer.add_tokens` and :obj:`False` with :meth:`~tokenizers.Tokenizer.add_special_tokens`): Defines whether this token should match against the normalized version of the input text. For example, with the added token ``"yesterday"``, and a normalizer in charge of lowercasing the text, the token could be extract from the input ``"I saw a lion Yesterday"``. special (:obj:`bool`, defaults to :obj:`False` with :meth:`~tokenizers.Tokenizer.add_tokens` and :obj:`False` with :meth:`~tokenizers.Tokenizer.add_special_tokens`): Defines whether this token should be skipped when decoding. """ def __init__(self, content, single_word=False, lstrip=False, rstrip=False, normalized=True, special=False): pass @property def content(self): """ Get the content of this :obj:`AddedToken` """ pass @property def lstrip(self): """ Get the value of the :obj:`lstrip` option """ pass @property def normalized(self): """ Get the value of the :obj:`normalized` option """ pass @property def rstrip(self): """ Get the value of the :obj:`rstrip` option """ pass @property def single_word(self): """ Get the value of the :obj:`single_word` option """ pass @property def special(self): """ Get the value of the :obj:`special` option """ pass class Encoding: """ The :class:`~tokenizers.Encoding` represents the output of a :class:`~tokenizers.Tokenizer`. """ @property def attention_mask(self): """ The attention mask This indicates to the LM which tokens should be attended to, and which should not. This is especially important when batching sequences, where we need to applying padding. Returns: :obj:`List[int]`: The attention mask """ pass def char_to_token(self, char_pos, sequence_index=0): """ Get the token that contains the char at the given position in the input sequence. Args: char_pos (:obj:`int`): The position of a char in the input string sequence_index (:obj:`int`, defaults to :obj:`0`): The index of the sequence that contains the target char Returns: :obj:`int`: The index of the token that contains this char in the encoded sequence """ pass def char_to_word(self, char_pos, sequence_index=0): """ Get the word that contains the char at the given position in the input sequence. Args: char_pos (:obj:`int`): The position of a char in the input string sequence_index (:obj:`int`, defaults to :obj:`0`): The index of the sequence that contains the target char Returns: :obj:`int`: The index of the word that contains this char in the input sequence """ pass @property def ids(self): """ The generated IDs The IDs are the main input to a Language Model. They are the token indices, the numerical representations that a LM understands. Returns: :obj:`List[int]`: The list of IDs """ pass @staticmethod def merge(encodings, growing_offsets=True): """ Merge the list of encodings into one final :class:`~tokenizers.Encoding` Args: encodings (A :obj:`List` of :class:`~tokenizers.Encoding`): The list of encodings that should be merged in one growing_offsets (:obj:`bool`, defaults to :obj:`True`): Whether the offsets should accumulate while merging Returns: :class:`~tokenizers.Encoding`: The resulting Encoding """ pass @property def n_sequences(self): """ The number of sequences represented Returns: :obj:`int`: The number of sequences in this :class:`~tokenizers.Encoding` """ pass @property def offsets(self): """ The offsets associated to each token These offsets let's you slice the input string, and thus retrieve the original part that led to producing the corresponding token. Returns: A :obj:`List` of :obj:`Tuple[int, int]`: The list of offsets """ pass @property def overflowing(self): """ A :obj:`List` of overflowing :class:`~tokenizers.Encoding` When using truncation, the :class:`~tokenizers.Tokenizer` takes care of splitting the output into as many pieces as required to match the specified maximum length. This field lets you retrieve all the subsequent pieces. When you use pairs of sequences, the overflowing pieces will contain enough variations to cover all the possible combinations, while respecting the provided maximum length. """ pass def pad(self, length, direction="right", pad_id=0, pad_type_id=0, pad_token="[PAD]"): """ Pad the :class:`~tokenizers.Encoding` at the given length Args: length (:obj:`int`): The desired length direction: (:obj:`str`, defaults to :obj:`right`): The expected padding direction. Can be either :obj:`right` or :obj:`left` pad_id (:obj:`int`, defaults to :obj:`0`): The ID corresponding to the padding token pad_type_id (:obj:`int`, defaults to :obj:`0`): The type ID corresponding to the padding token pad_token (:obj:`str`, defaults to `[PAD]`): The pad token to use """ pass @property def sequence_ids(self): """ The generated sequence indices. They represent the index of the input sequence associated to each token. The sequence id can be None if the token is not related to any input sequence, like for example with special tokens. Returns: A :obj:`List` of :obj:`Optional[int]`: A list of optional sequence index. """ pass def set_sequence_id(self, sequence_id): """ Set the given sequence index Set the given sequence index for the whole range of tokens contained in this :class:`~tokenizers.Encoding`. """ pass @property def special_tokens_mask(self): """ The special token mask This indicates which tokens are special tokens, and which are not. Returns: :obj:`List[int]`: The special tokens mask """ pass def token_to_chars(self, token_index): """ Get the offsets of the token at the given index. The returned offsets are related to the input sequence that contains the token. In order to determine in which input sequence it belongs, you must call :meth:`~tokenizers.Encoding.token_to_sequence()`. Args: token_index (:obj:`int`): The index of a token in the encoded sequence. Returns: :obj:`Tuple[int, int]`: The token offsets :obj:`(first, last + 1)` """ pass def token_to_sequence(self, token_index): """ Get the index of the sequence represented by the given token. In the general use case, this method returns :obj:`0` for a single sequence or the first sequence of a pair, and :obj:`1` for the second sequence of a pair Args: token_index (:obj:`int`): The index of a token in the encoded sequence. Returns: :obj:`int`: The sequence id of the given token """ pass def token_to_word(self, token_index): """ Get the index of the word that contains the token in one of the input sequences. The returned word index is related to the input sequence that contains the token. In order to determine in which input sequence it belongs, you must call :meth:`~tokenizers.Encoding.token_to_sequence()`. Args: token_index (:obj:`int`): The index of a token in the encoded sequence. Returns: :obj:`int`: The index of the word in the relevant input sequence. """ pass @property def tokens(self): """ The generated tokens They are the string representation of the IDs. Returns: :obj:`List[str]`: The list of tokens """ pass def truncate(self, max_length, stride=0, direction="right"): """ Truncate the :class:`~tokenizers.Encoding` at the given length If this :class:`~tokenizers.Encoding` represents multiple sequences, when truncating this information is lost. It will be considered as representing a single sequence. Args: max_length (:obj:`int`): The desired length stride (:obj:`int`, defaults to :obj:`0`): The length of previous content to be included in each overflowing piece direction (:obj:`str`, defaults to :obj:`right`): Truncate direction """ pass @property def type_ids(self): """ The generated type IDs Generally used for tasks like sequence classification or question answering, these tokens let the LM know which input sequence corresponds to each tokens. Returns: :obj:`List[int]`: The list of type ids """ pass @property def word_ids(self): """ The generated word indices. They represent the index of the word associated to each token. When the input is pre-tokenized, they correspond to the ID of the given input label, otherwise they correspond to the words indices as defined by the :class:`~tokenizers.pre_tokenizers.PreTokenizer` that was used. For special tokens and such (any token that was generated from something that was not part of the input), the output is :obj:`None` Returns: A :obj:`List` of :obj:`Optional[int]`: A list of optional word index. """ pass def word_to_chars(self, word_index, sequence_index=0): """ Get the offsets of the word at the given index in one of the input sequences. Args: word_index (:obj:`int`): The index of a word in one of the input sequences. sequence_index (:obj:`int`, defaults to :obj:`0`): The index of the sequence that contains the target word Returns: :obj:`Tuple[int, int]`: The range of characters (span) :obj:`(first, last + 1)` """ pass def word_to_tokens(self, word_index, sequence_index=0): """ Get the encoded tokens corresponding to the word at the given index in one of the input sequences. Args: word_index (:obj:`int`): The index of a word in one of the input sequences. sequence_index (:obj:`int`, defaults to :obj:`0`): The index of the sequence that contains the target word Returns: :obj:`Tuple[int, int]`: The range of tokens: :obj:`(first, last + 1)` """ pass @property def words(self): """ The generated word indices. .. warning:: This is deprecated and will be removed in a future version. Please use :obj:`~tokenizers.Encoding.word_ids` instead. They represent the index of the word associated to each token. When the input is pre-tokenized, they correspond to the ID of the given input label, otherwise they correspond to the words indices as defined by the :class:`~tokenizers.pre_tokenizers.PreTokenizer` that was used. For special tokens and such (any token that was generated from something that was not part of the input), the output is :obj:`None` Returns: A :obj:`List` of :obj:`Optional[int]`: A list of optional word index. """ pass class NormalizedString: """ NormalizedString A NormalizedString takes care of modifying an "original" string, to obtain a "normalized" one. While making all the requested modifications, it keeps track of the alignment information between the two versions of the string. Args: sequence: str: The string sequence used to initialize this NormalizedString """ def append(self, s): """ Append the given sequence to the string """ pass def clear(self): """ Clears the string """ pass def filter(self, func): """ Filter each character of the string using the given func """ pass def for_each(self, func): """ Calls the given function for each character of the string """ pass def lowercase(self): """ Lowercase the string """ pass def lstrip(self): """ Strip the left of the string """ pass def map(self, func): """ Calls the given function for each character of the string Replaces each character of the string using the returned value. Each returned value **must** be a str of length 1 (ie a character). """ pass def nfc(self): """ Runs the NFC normalization """ pass def nfd(self): """ Runs the NFD normalization """ pass def nfkc(self): """ Runs the NFKC normalization """ pass def nfkd(self): """ Runs the NFKD normalization """ pass @property def normalized(self): """ The normalized part of the string """ pass def prepend(self, s): """ Prepend the given sequence to the string """ pass def replace(self, pattern, content): """ Replace the content of the given pattern with the provided content Args: pattern: Pattern: A pattern used to match the string. Usually a string or a Regex content: str: The content to be used as replacement """ pass def rstrip(self): """ Strip the right of the string """ pass def slice(self, range): """ Slice the string using the given range """ pass def split(self, pattern, behavior): """ Split the NormalizedString using the given pattern and the specified behavior Args: pattern: Pattern: A pattern used to split the string. Usually a string or a regex built with `tokenizers.Regex` behavior: SplitDelimiterBehavior: The behavior to use when splitting. Choices: "removed", "isolated", "merged_with_previous", "merged_with_next", "contiguous" Returns: A list of NormalizedString, representing each split """ pass def strip(self): """ Strip both ends of the string """ pass def uppercase(self): """ Uppercase the string """ pass class PreTokenizedString: """ PreTokenizedString Wrapper over a string, that provides a way to normalize, pre-tokenize, tokenize the underlying string, while keeping track of the alignment information (offsets). The PreTokenizedString manages what we call `splits`. Each split represents a substring which is a subpart of the original string, with the relevant offsets and tokens. When calling one of the methods used to modify the PreTokenizedString (namely one of `split`, `normalize` or `tokenize), only the `splits` that don't have any associated tokens will get modified. Args: sequence: str: The string sequence used to initialize this PreTokenizedString """ def __init__(self, sequence): pass def get_splits(self, offset_referential="original", offset_type="char"): """ Get the splits currently managed by the PreTokenizedString Args: offset_referential: :obj:`str` Whether the returned splits should have offsets expressed relative to the original string, or the normalized one. choices: "original", "normalized". offset_type: :obj:`str` Whether the returned splits should have offsets expressed in bytes or chars. When slicing an str, we usually want to use chars, which is the default value. Now in some cases it might be interesting to get these offsets expressed in bytes, so it is possible to change this here. choices: "char", "bytes" Returns A list of splits """ pass def normalize(self, func): """ Normalize each split of the `PreTokenizedString` using the given `func` Args: func: Callable[[NormalizedString], None]: The function used to normalize each underlying split. This function does not need to return anything, just calling the methods on the provided NormalizedString allow its modification. """ pass def split(self, func): """ Split the PreTokenizedString using the given `func` Args: func: Callable[[index, NormalizedString], List[NormalizedString]]: The function used to split each underlying split. It is expected to return a list of `NormalizedString`, that represent the new splits. If the given `NormalizedString` does not need any splitting, we can just return it directly. In order for the offsets to be tracked accurately, any returned `NormalizedString` should come from calling either `.split` or `.slice` on the received one. """ pass def to_encoding(self, type_id=0, word_idx=None): """ Return an Encoding generated from this PreTokenizedString Args: type_id: int = 0: The type_id to be used on the generated Encoding. word_idx: Optional[int] = None: An optional word index to be used for each token of this Encoding. If provided, all the word indices in the generated Encoding will use this value, instead of the one automatically tracked during pre-tokenization. Returns: An Encoding """ pass def tokenize(self, func): """ Tokenize each split of the `PreTokenizedString` using the given `func` Args: func: Callable[[str], List[Token]]: The function used to tokenize each underlying split. This function must return a list of Token generated from the input str. """ pass class Regex: """ Instantiate a new Regex with the given pattern """ def __init__(self, pattern): pass class Token: pass class Tokenizer: """ A :obj:`Tokenizer` works as a pipeline. It processes some raw text as input and outputs an :class:`~tokenizers.Encoding`. Args: model (:class:`~tokenizers.models.Model`): The core algorithm that this :obj:`Tokenizer` should be using. """ def __init__(self, model): pass def add_special_tokens(self, tokens): """ Add the given special tokens to the Tokenizer. If these tokens are already part of the vocabulary, it just let the Tokenizer know about them. If they don't exist, the Tokenizer creates them, giving them a new id. These special tokens will never be processed by the model (ie won't be split into multiple tokens), and they can be removed from the output when decoding. Args: tokens (A :obj:`List` of :class:`~tokenizers.AddedToken` or :obj:`str`): The list of special tokens we want to add to the vocabulary. Each token can either be a string or an instance of :class:`~tokenizers.AddedToken` for more customization. Returns: :obj:`int`: The number of tokens that were created in the vocabulary """ pass def add_tokens(self, tokens): """ Add the given tokens to the vocabulary The given tokens are added only if they don't already exist in the vocabulary. Each token then gets a new attributed id. Args: tokens (A :obj:`List` of :class:`~tokenizers.AddedToken` or :obj:`str`): The list of tokens we want to add to the vocabulary. Each token can be either a string or an instance of :class:`~tokenizers.AddedToken` for more customization. Returns: :obj:`int`: The number of tokens that were created in the vocabulary """ pass def decode(self, ids, skip_special_tokens=True): """ Decode the given list of ids back to a string This is used to decode anything coming back from a Language Model Args: ids (A :obj:`List/Tuple` of :obj:`int`): The list of ids that we want to decode skip_special_tokens (:obj:`bool`, defaults to :obj:`True`): Whether the special tokens should be removed from the decoded string Returns: :obj:`str`: The decoded string """ pass def decode_batch(self, sequences, skip_special_tokens=True): """ Decode a batch of ids back to their corresponding string Args: sequences (:obj:`List` of :obj:`List[int]`): The batch of sequences we want to decode skip_special_tokens (:obj:`bool`, defaults to :obj:`True`): Whether the special tokens should be removed from the decoded strings Returns: :obj:`List[str]`: A list of decoded strings """ pass @property def decoder(self): """ The `optional` :class:`~tokenizers.decoders.Decoder` in use by the Tokenizer """ pass def enable_padding( self, direction="right", pad_id=0, pad_type_id=0, pad_token="[PAD]", length=None, pad_to_multiple_of=None ): """ Enable the padding Args: direction (:obj:`str`, `optional`, defaults to :obj:`right`): The direction in which to pad. Can be either ``right`` or ``left`` pad_to_multiple_of (:obj:`int`, `optional`): If specified, the padding length should always snap to the next multiple of the given value. For example if we were going to pad witha length of 250 but ``pad_to_multiple_of=8`` then we will pad to 256. pad_id (:obj:`int`, defaults to 0): The id to be used when padding pad_type_id (:obj:`int`, defaults to 0): The type id to be used when padding pad_token (:obj:`str`, defaults to :obj:`[PAD]`): The pad token to be used when padding length (:obj:`int`, `optional`): If specified, the length at which to pad. If not specified we pad using the size of the longest sequence in a batch. """ pass def enable_truncation(self, max_length, stride=0, strategy="longest_first", direction="right"): """ Enable truncation Args: max_length (:obj:`int`): The max length at which to truncate stride (:obj:`int`, `optional`): The length of the previous first sequence to be included in the overflowing sequence strategy (:obj:`str`, `optional`, defaults to :obj:`longest_first`): The strategy used to truncation. Can be one of ``longest_first``, ``only_first`` or ``only_second``. direction (:obj:`str`, defaults to :obj:`right`): Truncate direction """ pass def encode(self, sequence, pair=None, is_pretokenized=False, add_special_tokens=True): """ Encode the given sequence and pair. This method can process raw text sequences as well as already pre-tokenized sequences. Example: Here are some examples of the inputs that are accepted:: encode("A single sequence")` encode("A sequence", "And its pair")` encode([ "A", "pre", "tokenized", "sequence" ], is_pretokenized=True)` encode( [ "A", "pre", "tokenized", "sequence" ], [ "And", "its", "pair" ], is_pretokenized=True ) Args: sequence (:obj:`~tokenizers.InputSequence`): The main input sequence we want to encode. This sequence can be either raw text or pre-tokenized, according to the ``is_pretokenized`` argument: - If ``is_pretokenized=False``: :class:`~tokenizers.TextInputSequence` - If ``is_pretokenized=True``: :class:`~tokenizers.PreTokenizedInputSequence` pair (:obj:`~tokenizers.InputSequence`, `optional`): An optional input sequence. The expected format is the same that for ``sequence``. is_pretokenized (:obj:`bool`, defaults to :obj:`False`): Whether the input is already pre-tokenized add_special_tokens (:obj:`bool`, defaults to :obj:`True`): Whether to add the special tokens Returns: :class:`~tokenizers.Encoding`: The encoded result """ pass def encode_batch(self, input, is_pretokenized=False, add_special_tokens=True): """ Encode the given batch of inputs. This method accept both raw text sequences as well as already pre-tokenized sequences. Example: Here are some examples of the inputs that are accepted:: encode_batch([ "A single sequence", ("A tuple with a sequence", "And its pair"), [ "A", "pre", "tokenized", "sequence" ], ([ "A", "pre", "tokenized", "sequence" ], "And its pair") ]) Args: input (A :obj:`List`/:obj:`Tuple` of :obj:`~tokenizers.EncodeInput`): A list of single sequences or pair sequences to encode. Each sequence can be either raw text or pre-tokenized, according to the ``is_pretokenized`` argument: - If ``is_pretokenized=False``: :class:`~tokenizers.TextEncodeInput` - If ``is_pretokenized=True``: :class:`~tokenizers.PreTokenizedEncodeInput` is_pretokenized (:obj:`bool`, defaults to :obj:`False`): Whether the input is already pre-tokenized add_special_tokens (:obj:`bool`, defaults to :obj:`True`): Whether to add the special tokens Returns: A :obj:`List` of :class:`~tokenizers.Encoding`: The encoded batch """ pass def encode_batch_fast(self, input, is_pretokenized=False, add_special_tokens=True): """ Encode the given batch of inputs. This method is faster than `encode_batch` because it doesn't keep track of offsets, they will be all zeros. Example: Here are some examples of the inputs that are accepted:: encode_batch_fast([ "A single sequence", ("A tuple with a sequence", "And its pair"), [ "A", "pre", "tokenized", "sequence" ], ([ "A", "pre", "tokenized", "sequence" ], "And its pair") ]) Args: input (A :obj:`List`/:obj:`Tuple` of :obj:`~tokenizers.EncodeInput`): A list of single sequences or pair sequences to encode. Each sequence can be either raw text or pre-tokenized, according to the ``is_pretokenized`` argument: - If ``is_pretokenized=False``: :class:`~tokenizers.TextEncodeInput` - If ``is_pretokenized=True``: :class:`~tokenizers.PreTokenizedEncodeInput` is_pretokenized (:obj:`bool`, defaults to :obj:`False`): Whether the input is already pre-tokenized add_special_tokens (:obj:`bool`, defaults to :obj:`True`): Whether to add the special tokens Returns: A :obj:`List` of :class:`~tokenizers.Encoding`: The encoded batch """ pass @property def encode_special_tokens(self): """ Modifies the tokenizer in order to use or not the special tokens during encoding. Args: value (:obj:`bool`): Whether to use the special tokens or not """ pass @staticmethod def from_buffer(buffer): """ Instantiate a new :class:`~tokenizers.Tokenizer` from the given buffer. Args: buffer (:obj:`bytes`): A buffer containing a previously serialized :class:`~tokenizers.Tokenizer` Returns: :class:`~tokenizers.Tokenizer`: The new tokenizer """ pass @staticmethod def from_file(path): """ Instantiate a new :class:`~tokenizers.Tokenizer` from the file at the given path. Args: path (:obj:`str`): A path to a local JSON file representing a previously serialized :class:`~tokenizers.Tokenizer` Returns: :class:`~tokenizers.Tokenizer`: The new tokenizer """ pass @staticmethod def from_pretrained(identifier, revision="main", auth_token=None): """ Instantiate a new :class:`~tokenizers.Tokenizer` from an existing file on the Hugging Face Hub. Args: identifier (:obj:`str`): The identifier of a Model on the Hugging Face Hub, that contains a tokenizer.json file revision (:obj:`str`, defaults to `main`): A branch or commit id auth_token (:obj:`str`, `optional`, defaults to `None`): An optional auth token used to access private repositories on the Hugging Face Hub Returns: :class:`~tokenizers.Tokenizer`: The new tokenizer """ pass @staticmethod def from_str(json): """ Instantiate a new :class:`~tokenizers.Tokenizer` from the given JSON string. Args: json (:obj:`str`): A valid JSON string representing a previously serialized :class:`~tokenizers.Tokenizer` Returns: :class:`~tokenizers.Tokenizer`: The new tokenizer """ pass def get_added_tokens_decoder(self): """ Get the underlying vocabulary Returns: :obj:`Dict[int, AddedToken]`: The vocabulary """ pass def get_vocab(self, with_added_tokens=True): """ Get the underlying vocabulary Args: with_added_tokens (:obj:`bool`, defaults to :obj:`True`): Whether to include the added tokens Returns: :obj:`Dict[str, int]`: The vocabulary """ pass def get_vocab_size(self, with_added_tokens=True): """ Get the size of the underlying vocabulary Args: with_added_tokens (:obj:`bool`, defaults to :obj:`True`): Whether to include the added tokens Returns: :obj:`int`: The size of the vocabulary """ pass def id_to_token(self, id): """ Convert the given id to its corresponding token if it exists Args: id (:obj:`int`): The id to convert Returns: :obj:`Optional[str]`: An optional token, :obj:`None` if out of vocabulary """ pass @property def model(self): """ The :class:`~tokenizers.models.Model` in use by the Tokenizer """ pass def no_padding(self): """ Disable padding """ pass def no_truncation(self): """ Disable truncation """ pass @property def normalizer(self): """ The `optional` :class:`~tokenizers.normalizers.Normalizer` in use by the Tokenizer """ pass def num_special_tokens_to_add(self, is_pair): """ Return the number of special tokens that would be added for single/pair sentences. :param is_pair: Boolean indicating if the input would be a single sentence or a pair :return: """ pass @property def padding(self): """ Get the current padding parameters `Cannot be set, use` :meth:`~tokenizers.Tokenizer.enable_padding` `instead` Returns: (:obj:`dict`, `optional`): A dict with the current padding parameters if padding is enabled """ pass def post_process(self, encoding, pair=None, add_special_tokens=True): """ Apply all the post-processing steps to the given encodings. The various steps are: 1. Truncate according to the set truncation params (provided with :meth:`~tokenizers.Tokenizer.enable_truncation`) 2. Apply the :class:`~tokenizers.processors.PostProcessor` 3. Pad according to the set padding params (provided with :meth:`~tokenizers.Tokenizer.enable_padding`) Args: encoding (:class:`~tokenizers.Encoding`): The :class:`~tokenizers.Encoding` corresponding to the main sequence. pair (:class:`~tokenizers.Encoding`, `optional`): An optional :class:`~tokenizers.Encoding` corresponding to the pair sequence. add_special_tokens (:obj:`bool`): Whether to add the special tokens Returns: :class:`~tokenizers.Encoding`: The final post-processed encoding """ pass @property def post_processor(self): """ The `optional` :class:`~tokenizers.processors.PostProcessor` in use by the Tokenizer """ pass @property def pre_tokenizer(self): """ The `optional` :class:`~tokenizers.pre_tokenizers.PreTokenizer` in use by the Tokenizer """ pass def save(self, path, pretty=True): """ Save the :class:`~tokenizers.Tokenizer` to the file at the given path. Args: path (:obj:`str`): A path to a file in which to save the serialized tokenizer. pretty (:obj:`bool`, defaults to :obj:`True`): Whether the JSON file should be pretty formatted. """ pass def to_str(self, pretty=False): """ Gets a serialized string representing this :class:`~tokenizers.Tokenizer`. Args: pretty (:obj:`bool`, defaults to :obj:`False`): Whether the JSON string should be pretty formatted. Returns: :obj:`str`: A string representing the serialized Tokenizer """ pass def token_to_id(self, token): """ Convert the given token to its corresponding id if it exists Args: token (:obj:`str`): The token to convert Returns: :obj:`Optional[int]`: An optional id, :obj:`None` if out of vocabulary """ pass def train(self, files, trainer=None): """ Train the Tokenizer using the given files. Reads the files line by line, while keeping all the whitespace, even new lines. If you want to train from data store in-memory, you can check :meth:`~tokenizers.Tokenizer.train_from_iterator` Args: files (:obj:`List[str]`): A list of path to the files that we should use for training trainer (:obj:`~tokenizers.trainers.Trainer`, `optional`): An optional trainer that should be used to train our Model """ pass def train_from_iterator(self, iterator, trainer=None, length=None): """ Train the Tokenizer using the provided iterator. You can provide anything that is a Python Iterator * A list of sequences :obj:`List[str]` * A generator that yields :obj:`str` or :obj:`List[str]` * A Numpy array of strings * ... Args: iterator (:obj:`Iterator`): Any iterator over strings or list of strings trainer (:obj:`~tokenizers.trainers.Trainer`, `optional`): An optional trainer that should be used to train our Model length (:obj:`int`, `optional`): The total number of sequences in the iterator. This is used to provide meaningful progress tracking """ pass @property def truncation(self): """ Get the currently set truncation parameters `Cannot set, use` :meth:`~tokenizers.Tokenizer.enable_truncation` `instead` Returns: (:obj:`dict`, `optional`): A dict with the current truncation parameters if truncation is enabled """ pass
tokenizers/bindings/python/py_src/tokenizers/__init__.pyi/0
{ "file_path": "tokenizers/bindings/python/py_src/tokenizers/__init__.pyi", "repo_id": "tokenizers", "token_count": 17199 }
252
# Generated content DO NOT EDIT from .. import processors PostProcessor = processors.PostProcessor BertProcessing = processors.BertProcessing ByteLevel = processors.ByteLevel RobertaProcessing = processors.RobertaProcessing Sequence = processors.Sequence TemplateProcessing = processors.TemplateProcessing
tokenizers/bindings/python/py_src/tokenizers/processors/__init__.py/0
{ "file_path": "tokenizers/bindings/python/py_src/tokenizers/processors/__init__.py", "repo_id": "tokenizers", "token_count": 74 }
253
#![warn(clippy::all)] #![allow(clippy::upper_case_acronyms)] // Many false positives with pyo3 it seems &str, and &PyAny get flagged #![allow(clippy::borrow_deref_ref)] extern crate tokenizers as tk; mod decoders; mod encoding; mod error; mod models; mod normalizers; mod pre_tokenizers; mod processors; mod token; mod tokenizer; mod trainers; mod utils; use pyo3::prelude::*; use pyo3::wrap_pymodule; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); // For users using multiprocessing in python, it is quite easy to fork the process running // tokenizers, ending up with a deadlock because we internaly make use of multithreading. So // we register a callback to be called in the event of a fork so that we can warn the user. #[cfg(target_family = "unix")] static mut REGISTERED_FORK_CALLBACK: bool = false; #[cfg(target_family = "unix")] extern "C" fn child_after_fork() { use tk::parallelism::*; if has_parallelism_been_used() && !is_parallelism_configured() { eprintln!( "huggingface/tokenizers: The current process just got forked, after parallelism has \ already been used. Disabling parallelism to avoid deadlocks..." ); eprintln!("To disable this warning, you can either:"); eprintln!( "\t- Avoid using `tokenizers` before the fork if possible\n\ \t- Explicitly set the environment variable {}=(true | false)", ENV_VARIABLE ); set_parallelism(false); } } /// Tokenizers Module #[pymodule] pub fn tokenizers(m: &Bound<'_, PyModule>) -> PyResult<()> { let _ = env_logger::try_init_from_env("TOKENIZERS_LOG"); // Register the fork callback #[cfg(target_family = "unix")] unsafe { if !REGISTERED_FORK_CALLBACK { libc::pthread_atfork(None, None, Some(child_after_fork)); REGISTERED_FORK_CALLBACK = true; } } m.add_class::<tokenizer::PyTokenizer>()?; m.add_class::<tokenizer::PyAddedToken>()?; m.add_class::<token::PyToken>()?; m.add_class::<encoding::PyEncoding>()?; m.add_class::<utils::PyRegex>()?; m.add_class::<utils::PyNormalizedString>()?; m.add_class::<utils::PyPreTokenizedString>()?; m.add_wrapped(wrap_pymodule!(models::models))?; m.add_wrapped(wrap_pymodule!(pre_tokenizers::pre_tokenizers))?; m.add_wrapped(wrap_pymodule!(decoders::decoders))?; m.add_wrapped(wrap_pymodule!(processors::processors))?; m.add_wrapped(wrap_pymodule!(normalizers::normalizers))?; m.add_wrapped(wrap_pymodule!(trainers::trainers))?; m.add("__version__", env!("CARGO_PKG_VERSION"))?; Ok(()) }
tokenizers/bindings/python/src/lib.rs/0
{ "file_path": "tokenizers/bindings/python/src/lib.rs", "repo_id": "tokenizers", "token_count": 1087 }
254
from tokenizers import BertWordPieceTokenizer from ..utils import bert_files, data_dir, multiprocessing_with_parallelism class TestBertWordPieceTokenizer: def test_basic_encode(self, bert_files): tokenizer = BertWordPieceTokenizer.from_file(bert_files["vocab"]) # Encode with special tokens by default output = tokenizer.encode("My name is John", "pair") assert output.ids == [101, 2026, 2171, 2003, 2198, 102, 3940, 102] assert output.tokens == [ "[CLS]", "my", "name", "is", "john", "[SEP]", "pair", "[SEP]", ] assert output.offsets == [ (0, 0), (0, 2), (3, 7), (8, 10), (11, 15), (0, 0), (0, 4), (0, 0), ] assert output.type_ids == [0, 0, 0, 0, 0, 0, 1, 1] # Can encode without the special tokens output = tokenizer.encode("My name is John", "pair", add_special_tokens=False) assert output.ids == [2026, 2171, 2003, 2198, 3940] assert output.tokens == ["my", "name", "is", "john", "pair"] assert output.offsets == [(0, 2), (3, 7), (8, 10), (11, 15), (0, 4)] assert output.type_ids == [0, 0, 0, 0, 1] def test_multiprocessing_with_parallelism(self, bert_files): tokenizer = BertWordPieceTokenizer.from_file(bert_files["vocab"]) multiprocessing_with_parallelism(tokenizer, False) multiprocessing_with_parallelism(tokenizer, True) def test_train_from_iterator(self): text = ["A first sentence", "Another sentence", "And a last one"] tokenizer = BertWordPieceTokenizer() tokenizer.train_from_iterator(text, show_progress=False) output = tokenizer.encode("A sentence") assert output.tokens == ["a", "sentence"]
tokenizers/bindings/python/tests/implementations/test_bert_wordpiece.py/0
{ "file_path": "tokenizers/bindings/python/tests/implementations/test_bert_wordpiece.py", "repo_id": "tokenizers", "token_count": 914 }
255
# Post-processors <tokenizerslangcontent> <python> ## BertProcessing [[autodoc]] tokenizers.processors.BertProcessing ## ByteLevel [[autodoc]] tokenizers.processors.ByteLevel ## RobertaProcessing [[autodoc]] tokenizers.processors.RobertaProcessing ## TemplateProcessing [[autodoc]] tokenizers.processors.TemplateProcessing </python> <rust> The Rust API Reference is available directly on the [Docs.rs](https://docs.rs/tokenizers/latest/tokenizers/) website. </rust> <node> The node API has not been documented yet. </node> </tokenizerslangcontent>
tokenizers/docs/source-doc-builder/api/post-processors.mdx/0
{ "file_path": "tokenizers/docs/source-doc-builder/api/post-processors.mdx", "repo_id": "tokenizers", "token_count": 174 }
256
Crates.io ---------------------------------------------------------------------------------------------------- ๐Ÿค— Tokenizers is available on `crates.io <https://crates.io/crates/tokenizers>`__. You just need to add it to your :obj:`Cargo.toml`:: tokenizers = "0.10"
tokenizers/docs/source/installation/rust.inc/0
{ "file_path": "tokenizers/docs/source/installation/rust.inc", "repo_id": "tokenizers", "token_count": 74 }
257
use tokenizers::Tokenizer; fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let tokenizer = Tokenizer::from_pretrained("meta-llama/Meta-Llama-3.1-8B-Instruct", None)?; let data = std::fs::read_to_string("data/big.txt")?; let data: Vec<_> = data.lines().collect(); let add_special_tokens = false; tokenizer.encode_batch_char_offsets(data, add_special_tokens)?; Ok(()) }
tokenizers/tokenizers/examples/encode_batch.rs/0
{ "file_path": "tokenizers/tokenizers/examples/encode_batch.rs", "repo_id": "tokenizers", "token_count": 165 }
258
import * as wasm from "unstable_wasm"; console.log(wasm.tokenize("ab")); console.log(wasm.tokenize("abc"));
tokenizers/tokenizers/examples/unstable_wasm/www/index.js/0
{ "file_path": "tokenizers/tokenizers/examples/unstable_wasm/www/index.js", "repo_id": "tokenizers", "token_count": 43 }
259
use super::{super::OrderedVocabIter, convert_merges_to_hashmap, BpeBuilder, Pair, BPE}; use serde::{ de::{Error, MapAccess, Visitor}, ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer, }; use std::collections::HashMap; impl Serialize for BPE { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { let mut model = serializer.serialize_struct("BPE", 8)?; // Start by small fields model.serialize_field("type", "BPE")?; model.serialize_field("dropout", &self.dropout)?; model.serialize_field("unk_token", &self.unk_token)?; model.serialize_field("continuing_subword_prefix", &self.continuing_subword_prefix)?; model.serialize_field("end_of_word_suffix", &self.end_of_word_suffix)?; model.serialize_field("fuse_unk", &self.fuse_unk)?; model.serialize_field("byte_fallback", &self.byte_fallback)?; model.serialize_field("ignore_merges", &self.ignore_merges)?; // Then the large ones let mut merges: Vec<(&Pair, &u32)> = self .merges .iter() .map(|(pair, (rank, _))| (pair, rank)) .collect(); merges.sort_unstable_by_key(|k| *k.1); let merges = merges .into_iter() .map(|(pair, _)| (self.vocab_r[&pair.0].clone(), self.vocab_r[&pair.1].clone())) .collect::<Vec<_>>(); let ordered_vocab = OrderedVocabIter::new(&self.vocab_r); model.serialize_field("vocab", &ordered_vocab)?; model.serialize_field("merges", &merges)?; model.end() } } impl<'de> Deserialize<'de> for BPE { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { deserializer.deserialize_struct( "BPE", &[ "type", "dropout", "unk_token", "continuing_subword_prefix", "end_of_word_suffix", "fuse_unk", "byte_fallback", "ignore_merges", "vocab", "merges", ], BPEVisitor, ) } } struct BPEVisitor; impl<'de> Visitor<'de> for BPEVisitor { type Value = BPE; fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { write!(fmt, "struct BPE") } fn visit_map<V>(self, mut map: V) -> std::result::Result<Self::Value, V::Error> where V: MapAccess<'de>, { let mut builder = BpeBuilder::new(); let mut vocab: Option<HashMap<String, u32>> = None; #[derive(Debug, Deserialize)] #[serde(untagged)] enum MergeType { Tuple(Vec<(String, String)>), Legacy(Vec<String>), } let mut merges: Option<MergeType> = None; while let Some(key) = map.next_key::<String>()? { match key.as_ref() { "dropout" => { if let Some(dropout) = map.next_value()? { builder = builder.dropout(dropout); } } "unk_token" => { if let Some(unk) = map.next_value()? { builder = builder.unk_token(unk); } } "continuing_subword_prefix" => { if let Some(prefix) = map.next_value()? { builder = builder.continuing_subword_prefix(prefix); } } "end_of_word_suffix" => { if let Some(suffix) = map.next_value()? { builder = builder.end_of_word_suffix(suffix); } } "fuse_unk" => { if let Some(suffix) = map.next_value()? { builder = builder.fuse_unk(suffix); } } "byte_fallback" => { if let Some(suffix) = map.next_value()? { builder = builder.byte_fallback(suffix); } } "ignore_merges" => { if let Some(suffix) = map.next_value()? { builder = builder.ignore_merges(suffix); } } "vocab" => vocab = Some(map.next_value()?), "merges" => merges = Some(map.next_value()?), "type" => match map.next_value()? { "BPE" => {} u => { return Err(serde::de::Error::invalid_value( serde::de::Unexpected::Str(u), &"BPE", )) } }, _ => {} } } if let (Some(vocab), Some(merges)) = (vocab, merges) { let merges = match merges { MergeType::Tuple(merges) => merges, MergeType::Legacy(merges) => { convert_merges_to_hashmap(merges.into_iter(), &vocab).map_err(Error::custom)? } }; builder = builder.vocab_and_merges(vocab, merges); Ok(builder.build().map_err(Error::custom)?) } else { Err(Error::custom("Missing vocab/merges")) } } } #[cfg(test)] mod test { use super::*; use crate::models::bpe::Vocab; #[test] fn test_serialization() { let vocab: Vocab = [ ("<unk>".into(), 0), ("a".into(), 1), ("b".into(), 2), ("ab".into(), 3), ] .iter() .cloned() .collect(); let bpe = BpeBuilder::default() .vocab_and_merges(vocab, vec![("a".to_string(), "b".to_string())]) .unk_token("<unk>".to_string()) .ignore_merges(true) .build() .unwrap(); let legacy = r#"{"type":"BPE","dropout":null,"unk_token":"<unk>","continuing_subword_prefix":null,"end_of_word_suffix":null,"fuse_unk":false,"byte_fallback":false,"ignore_merges":true,"vocab":{"<unk>":0,"a":1,"b":2,"ab":3},"merges":["a b"]}"#; let legacy = serde_json::from_str(legacy).unwrap(); assert_eq!(bpe, legacy); let data = serde_json::to_string(&bpe).unwrap(); assert_eq!( data, r#"{"type":"BPE","dropout":null,"unk_token":"<unk>","continuing_subword_prefix":null,"end_of_word_suffix":null,"fuse_unk":false,"byte_fallback":false,"ignore_merges":true,"vocab":{"<unk>":0,"a":1,"b":2,"ab":3},"merges":[["a","b"]]}"# ); let reconstructed = serde_json::from_str(&data).unwrap(); assert_eq!(bpe, reconstructed); // With a space in the token let vocab: Vocab = [ ("<unk>".into(), 0), ("a".into(), 1), ("b c d".into(), 2), ("ab c d".into(), 3), ] .iter() .cloned() .collect(); let bpe = BpeBuilder::default() .vocab_and_merges(vocab, vec![("a".to_string(), "b c d".to_string())]) .unk_token("<unk>".to_string()) .ignore_merges(true) .build() .unwrap(); let data = serde_json::to_string(&bpe).unwrap(); assert_eq!( data, r#"{"type":"BPE","dropout":null,"unk_token":"<unk>","continuing_subword_prefix":null,"end_of_word_suffix":null,"fuse_unk":false,"byte_fallback":false,"ignore_merges":true,"vocab":{"<unk>":0,"a":1,"b c d":2,"ab c d":3},"merges":[["a","b c d"]]}"# ); let reconstructed = serde_json::from_str(&data).unwrap(); assert_eq!(bpe, reconstructed); } #[test] fn test_serialization_ignore_merges() { let vocab: Vocab = [("<unk>".into(), 0), ("a".into(), 1), ("b".into(), 2)] .iter() .cloned() .collect(); let mut bpe = BpeBuilder::default() .vocab_and_merges(vocab, vec![]) .unk_token("<unk>".to_string()) .ignore_merges(true) .build() .unwrap(); let bpe_string = r#"{"type":"BPE","dropout":null,"unk_token":"<unk>","continuing_subword_prefix":null,"end_of_word_suffix":null,"fuse_unk":false,"byte_fallback":false,"ignore_merges":true,"vocab":{"<unk>":0,"a":1,"b":2},"merges":[]}"#; assert_eq!(serde_json::from_str::<BPE>(bpe_string).unwrap(), bpe); bpe.ignore_merges = false; let bpe_string = r#"{"type":"BPE","dropout":null,"unk_token":"<unk>","continuing_subword_prefix":null,"end_of_word_suffix":null,"fuse_unk":false,"byte_fallback":false,"vocab":{"<unk>":0,"a":1,"b":2},"merges":[]}"#; assert_eq!(serde_json::from_str::<BPE>(bpe_string).unwrap(), bpe); } }
tokenizers/tokenizers/src/models/bpe/serialization.rs/0
{ "file_path": "tokenizers/tokenizers/src/models/bpe/serialization.rs", "repo_id": "tokenizers", "token_count": 4848 }
260
use crate::tokenizer::{NormalizedString, Normalizer, Result}; use serde::{Deserialize, Serialize}; use unicode_categories::UnicodeCategories; /// Checks whether a character is whitespace fn is_whitespace(c: char) -> bool { // These are technically control characters but we count them as whitespace match c { '\t' | '\n' | '\r' => true, _ => c.is_whitespace(), } } /// Checks whether a character is a control character fn is_control(c: char) -> bool { // These are technically control characters but we count them as whitespace match c { '\t' | '\n' | '\r' => false, // The definition of `is_control` here is quite large and contains also // Cc, Cf, Cn or Co // cf. https://unicode.org/reports/tr44/ (Table 12) _ => c.is_other(), } } /// Checks whether a character is chinese /// This defines a "chinese character" as anything in the CJK Unicode block: /// https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) /// /// Note that the CJK Unicode block is NOT all Japanese and Korean characters, /// despite its name. The modern Korean Hangul alphabet is a different block, /// as is Japanese Hiragana and Katakana. Those alphabets are used to write /// space-separated words, so they are not treated specially and handled /// like for all of the other languages. fn is_chinese_char(c: char) -> bool { matches!( c as usize, 0x4E00..=0x9FFF | 0x3400..=0x4DBF | 0x20000..=0x2A6DF | 0x2A700..=0x2B73F | 0x2B740..=0x2B81F | 0x2B920..=0x2CEAF | 0xF900..=0xFAFF | 0x2F800..=0x2FA1F ) } #[derive(Copy, Clone, Debug, Deserialize, Serialize)] #[serde(tag = "type")] #[non_exhaustive] pub struct BertNormalizer { /// Whether to do the bert basic cleaning: /// 1. Remove any control characters /// 2. Replace all sorts of whitespace by the classic one ` ` pub clean_text: bool, /// Whether to put spaces around chinese characters so they get split pub handle_chinese_chars: bool, /// Whether to strip accents pub strip_accents: Option<bool>, /// Whether to lowercase the input pub lowercase: bool, } impl Default for BertNormalizer { fn default() -> Self { Self { clean_text: true, handle_chinese_chars: true, strip_accents: None, lowercase: true, } } } impl BertNormalizer { pub fn new( clean_text: bool, handle_chinese_chars: bool, strip_accents: Option<bool>, lowercase: bool, ) -> Self { Self { clean_text, handle_chinese_chars, strip_accents, lowercase, } } fn do_clean_text(&self, normalized: &mut NormalizedString) { normalized .filter(|c| !(c as usize == 0 || c as usize == 0xfffd || is_control(c))) .map(|c| if is_whitespace(c) { ' ' } else { c }); } fn do_handle_chinese_chars(&self, normalized: &mut NormalizedString) { let mut new_chars: Vec<(char, isize)> = vec![]; normalized.for_each(|c| { if is_chinese_char(c) { new_chars.extend([(' ', 0), (c, 1), (' ', 1)]); } else { new_chars.push((c, 0)); } }); normalized.transform(new_chars, 0); } fn do_strip_accents(&self, normalized: &mut NormalizedString) { normalized.nfd().filter(|c| !c.is_mark_nonspacing()); } fn do_lowercase(&self, normalized: &mut NormalizedString) { normalized.lowercase(); } } impl Normalizer for BertNormalizer { fn normalize(&self, normalized: &mut NormalizedString) -> Result<()> { if self.clean_text { self.do_clean_text(normalized); } if self.handle_chinese_chars { self.do_handle_chinese_chars(normalized); } let strip_accents = self.strip_accents.unwrap_or(self.lowercase); if strip_accents { self.do_strip_accents(normalized); } if self.lowercase { self.do_lowercase(normalized); } Ok(()) } }
tokenizers/tokenizers/src/normalizers/bert.rs/0
{ "file_path": "tokenizers/tokenizers/src/normalizers/bert.rs", "repo_id": "tokenizers", "token_count": 1856 }
261
use crate::pre_tokenizers::PreTokenizerWrapper; use crate::tokenizer::{PreTokenizedString, PreTokenizer, Result}; use crate::utils::macro_rules_attribute; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq)] #[macro_rules_attribute(impl_serde_type!)] pub struct Sequence { pretokenizers: Vec<PreTokenizerWrapper>, } impl Sequence { pub fn new(pretokenizers: Vec<PreTokenizerWrapper>) -> Self { Self { pretokenizers } } pub fn get_pre_tokenizers(&self) -> &[PreTokenizerWrapper] { &self.pretokenizers } pub fn get_pre_tokenizers_mut(&mut self) -> &mut [PreTokenizerWrapper] { &mut self.pretokenizers } } impl PreTokenizer for Sequence { fn pre_tokenize(&self, pretokenized: &mut PreTokenizedString) -> Result<()> { for pretokenizer in &self.pretokenizers { pretokenizer.pre_tokenize(pretokenized)?; } Ok(()) } } #[cfg(test)] mod tests { use super::*; use crate::pre_tokenizers::{punctuation::Punctuation, whitespace::WhitespaceSplit}; use crate::{OffsetReferential, OffsetType}; #[test] fn sequence_basic() { let pretokenizers = vec![ PreTokenizerWrapper::WhitespaceSplit(WhitespaceSplit), PreTokenizerWrapper::Punctuation(Punctuation::default()), ]; let pretok = Sequence::new(pretokenizers); let mut pretokenized: PreTokenizedString = "Hey friend! How are you?!?".into(); pretok.pre_tokenize(&mut pretokenized).unwrap(); assert_eq!( pretokenized .get_splits(OffsetReferential::Original, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![ ("Hey", (0, 3)), ("friend", (4, 10)), ("!", (10, 11)), ("How", (16, 19)), ("are", (20, 23)), ("you", (24, 27)), ("?", (27, 28)), ("!", (28, 29)), ("?", (29, 30)), ] ); } }
tokenizers/tokenizers/src/pre_tokenizers/sequence.rs/0
{ "file_path": "tokenizers/tokenizers/src/pre_tokenizers/sequence.rs", "repo_id": "tokenizers", "token_count": 1011 }
262
use crate::{ normalizer::Range, Encoding, NormalizedString, OffsetReferential, Offsets, Result, Token, }; use std::collections::HashMap; /// Various possible types of offsets #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum OffsetType { Byte, Char, None, } /// Wrapper for a subpart of a `NormalizedString`. /// /// This Split contains the underlying `NormalizedString` as well as its offsets /// in the original string. These offsets are in the `original` referential. /// It also contains any `Token` associated to the current split #[derive(Debug, Clone, PartialEq, Eq)] pub struct Split { /// The underlying `NormalizedString`. Each SubString is represented by a `NormalizedString` /// and in the end we might be carrying a lot of SubString representing various parts of the /// original input string. normalized: NormalizedString, /// Optional Tokens associated to this Split tokens: Option<Vec<Token>>, } impl From<NormalizedString> for Split { fn from(n: NormalizedString) -> Self { Self { normalized: n, tokens: None, } } } impl From<(NormalizedString, Option<Vec<Token>>)> for Split { fn from(f: (NormalizedString, Option<Vec<Token>>)) -> Self { Self { normalized: f.0, tokens: f.1, } } } /// The `PreTokenizedString` is in charge of splitting an underlying string, /// making sure everything is fine while doing so, and providing ways to normalize /// and tokenize these splits. /// Once everything has been normalized and tokenized, the `PreTokenizedString` is able /// to build an `Encoding` with all the relevant offsets and word ids, relative to the /// original string. #[derive(Debug, Clone, PartialEq, Eq)] pub struct PreTokenizedString { original: String, splits: Vec<Split>, } impl PreTokenizedString { /// Split the `PreTokenizedString` by providing a `split_fn` in charge of splitting /// each substring (`NormalizedString`) into multiple parts. /// /// `split_fn` takes a `NormalizedString` and is in charge of returning an iterator /// over the produced `NormalizedString`. `split_fn` is free of modifying these /// `NormalizedString` as relevant, as long as it respects the constraint stated below. /// /// There are only one constraint that *MUST* be respected: /// > The produced `NormalizedString`, if combined back together, must have the /// > same `original` string as the original one given to `split_fn`. This concretely /// > means that for the offset tracking to work as expected, `split_fn` must produce /// > "splits" of the original string. pub fn split<F, U, R>(&mut self, mut split_fn: F) -> Result<()> where F: FnMut(usize, NormalizedString) -> Result<U>, U: IntoIterator<Item = R>, R: Into<Split>, { // new_splits is at least as big as self.splits let mut new_splits = Vec::with_capacity(self.splits.len()); for (i, original_split) in self.splits.drain(..).enumerate() { if original_split.tokens.is_some() { new_splits.push(original_split); continue; } new_splits.extend( split_fn(i, original_split.normalized)? .into_iter() .filter_map(|split| { let split: Split = split.into(); if split.normalized.is_empty() { None } else { Some(split) } }), ); } self.splits = new_splits; Ok(()) } /// Normalized all the splits that do not have attached `Tokens`, using the provided /// `normalize` function. pub fn normalize<F>(&mut self, normalize: F) -> Result<()> where F: Fn(&mut NormalizedString) -> Result<()>, { for split in self.splits.iter_mut().filter(|s| s.tokens.is_none()) { normalize(&mut split.normalized)?; } Ok(()) } /// Tokenize all the splits that do not have attached `Tokens`, using the provided /// `tokenize` function pub fn tokenize<F>(&mut self, tokenize: F) -> Result<()> where F: Fn(&NormalizedString) -> Result<Vec<Token>>, { for split in self.splits.iter_mut().filter(|s| s.tokens.is_none()) { split.tokens = Some(tokenize(&split.normalized)?); } Ok(()) } /// Transform the current `PreTokenizedString` into an `Encoding`. /// /// If a `word_idx` is provided, any word in the generated `Encoding` /// will be set to this value. This is generally used with pre-tokenized /// input, that do not need the `PreTokenizedString` to generate word ids. /// /// This method will fail if some splits do not have associated `Token`. pub fn into_encoding( self, word_idx: Option<u32>, type_id: u32, offset_type: OffsetType, ) -> Result<Encoding> { if self.splits.is_empty() { Ok(Encoding::default()) } else if !self.splits.iter().all(|split| split.tokens.is_some()) { Err("Split has not been tokenized, call `PreTokenizedString::tokenize` first".into()) } else { let offset_converter = match offset_type { OffsetType::Char => Some(BytesToCharOffsetConverter::new(&self.original)), OffsetType::Byte => None, OffsetType::None => { let tokens = self .splits .into_iter() .flat_map(|split| { split.tokens.unwrap().into_iter().map(|token| { // Replace this with the actual fields you need for the Encoding type (token.id, String::with_capacity(0), (0, 0), None, 0) }) }) .collect(); return Ok(tokens); } }; Ok(self .splits .into_iter() .enumerate() .flat_map(|(idx, split)| { let normalized = split.normalized; let offsets = normalized.offsets_original(); let offset_converter = &offset_converter; split.tokens.unwrap().into_iter().map(move |token| { let mut offsets = normalized .convert_offsets(Range::Normalized(token.offsets.0..token.offsets.1)) .map_or(token.offsets, |range| { (offsets.0 + range.start, offsets.0 + range.end) }); // Convert to char offsets if relevant if let Some(converter) = offset_converter { offsets = converter.convert(offsets).unwrap_or(offsets); } ( token.id, token.value, offsets, if word_idx.is_some() { word_idx } else { Some(idx as u32) }, type_id, ) }) }) .collect()) } } /// Returns a list of splits, each of them being a slice of the normalized /// string, the associated offsets either in original or normalized /// referential, as well as the potention tokens pub fn get_splits( &self, offset_ref: OffsetReferential, offset_type: OffsetType, ) -> Vec<(&str, Offsets, &Option<Vec<Token>>)> { let offset_converter = match offset_type { OffsetType::Char => Some(BytesToCharOffsetConverter::new(&self.original)), OffsetType::Byte => None, OffsetType::None => None, }; let mut offset = 0; self.splits .iter() .map(|split| { let mut offsets = match offset_ref { OffsetReferential::Original => split.normalized.offsets_original(), OffsetReferential::Normalized => { let len = split.normalized.len(); offset += len; (offset - len, offset) } }; // Convert to char offsets if relevant if let Some(ref converter) = offset_converter { offsets = converter.convert(offsets).unwrap_or(offsets); } (split.normalized.get(), offsets, &split.tokens) }) .collect() } } impl From<NormalizedString> for PreTokenizedString { fn from(s: NormalizedString) -> Self { Self { original: s.get_original().to_owned(), splits: vec![Split { normalized: s, tokens: None, }], } } } impl From<&str> for PreTokenizedString { fn from(s: &str) -> Self { let normalized: NormalizedString = s.into(); normalized.into() } } impl From<String> for PreTokenizedString { fn from(s: String) -> Self { let normalized: NormalizedString = s.into(); normalized.into() } } struct BytesToCharOffsetConverter { map: HashMap<usize, usize>, } impl BytesToCharOffsetConverter { pub fn new(sequence: &str) -> Self { Self { map: sequence .char_indices() .enumerate() .flat_map(|(i, (b, c))| { let mut n = 0; std::iter::repeat_with(move || { let o = (b + n, i); n += 1; o }) .take(c.len_utf8()) }) .collect(), } } pub fn convert(&self, offsets: Offsets) -> Option<Offsets> { match (self.map.get(&offsets.0), self.map.get(&offsets.1)) { (Some(start), Some(end)) => Some((*start, *end)), // If we reached the end, `end` is not in the map (Some(start), None) => { // But the one just before should be let last = self.map.get(&(offsets.1 - 1)).copied().unwrap_or(start + 1); Some((*start, last + 1)) } _ => None, } } }
tokenizers/tokenizers/src/tokenizer/pre_tokenizer.rs/0
{ "file_path": "tokenizers/tokenizers/src/tokenizer/pre_tokenizer.rs", "repo_id": "tokenizers", "token_count": 5310 }
263
mod common; use common::*; use tokenizers::tokenizer::AddedToken; macro_rules! check_offsets { ($input: expr, $output:expr, $offset:expr, $result:expr) => { let offsets = $output.get_offsets()[$offset]; assert_eq!(&$input[offsets.0..offsets.1], $result); }; } #[test] fn byte_level_basic() { // Without trimming offsets let tokenizer = get_byte_level(true, false); let input = "Hello there, how are you?"; let output = tokenizer.encode(input, false).unwrap(); check_offsets!(input, output, 0, "Hello"); check_offsets!(input, output, 1, " there"); check_offsets!(input, output, 2, ","); check_offsets!(input, output, 3, " how"); check_offsets!(input, output, 4, " are"); check_offsets!(input, output, 5, " you"); check_offsets!(input, output, 6, "?"); // And when trimming offsets: let tokenizer = get_byte_level(true, true); let input = "Hello there, how are you?"; let output = tokenizer.encode(input, false).unwrap(); check_offsets!(input, output, 0, "Hello"); check_offsets!(input, output, 1, "there"); check_offsets!(input, output, 2, ","); check_offsets!(input, output, 3, "how"); check_offsets!(input, output, 4, "are"); check_offsets!(input, output, 5, "you"); check_offsets!(input, output, 6, "?"); } #[test] fn byte_level_unicode() { let tokenizer = get_byte_level(true, false); let input = "iโญขj"; let output = tokenizer.encode(input, false).unwrap(); check_offsets!(input, output, 1, "โญข"); check_offsets!(input, output, 2, "โญข"); check_offsets!(input, output, 3, "โญข"); } #[test] fn byte_level_double_sequence() { let input_a = "My name is Anthony"; let input_b = "What is my name?"; // Without trimming offsets let tokenizer = get_byte_level(true, false); let output = tokenizer.encode((input_a, input_b), false).unwrap(); let offsets = output.get_offsets(); assert_eq!( offsets, &[ (0, 2), (2, 7), (7, 10), (10, 18), (0, 4), (4, 7), (7, 10), (10, 15), (15, 16) ] ); assert_eq!( output.get_word_ids(), &[ Some(0), Some(1), Some(2), Some(3), Some(0), Some(1), Some(2), Some(3), Some(4) ] ); assert_eq!(output.get_type_ids(), &[0, 0, 0, 0, 1, 1, 1, 1, 1]); // When trimming offsets let tokenizer = get_byte_level(true, true); let output = tokenizer.encode((input_a, input_b), false).unwrap(); let offsets = output.get_offsets(); assert_eq!( offsets, &[ (0, 2), (3, 7), (8, 10), (11, 18), (0, 4), (5, 7), (8, 10), (11, 15), (15, 16) ] ); } #[test] fn byte_level_pre_tokenized_sequence() { let input = ["My", "name", "is", "Anthonino"]; // Without trimming offsets let tokenizer = get_byte_level(true, false); let output = tokenizer.encode(&input[..], false).unwrap(); assert_eq!( output.get_tokens(), &["ฤ My", "ฤ name", "ฤ is", "ฤ Anth", "on", "ino"] ); assert_eq!( output.get_word_ids(), &[Some(0), Some(1), Some(2), Some(3), Some(3), Some(3)] ); assert_eq!( output.get_offsets(), &[(0, 2), (0, 4), (0, 2), (0, 4), (4, 6), (6, 9)] ); } #[test] #[ignore] fn byte_level_pre_tokenized_sequence_with_trimming() { let input = ["My", "name", "is", "Anthonino"]; // When trimming offsets (expect same result) let tokenizer = get_byte_level(true, true); let output = tokenizer.encode(&input[..], false).unwrap(); assert_eq!( output.get_word_ids(), &[Some(0), Some(1), Some(2), Some(3), Some(3), Some(3)] ); assert_eq!( output.get_offsets(), &[(0, 2), (0, 4), (0, 2), (0, 4), (4, 6), (6, 9)] ); } #[test] fn split_on_added_tokens_bert() { let input = "Yesterday I saw a [MASK] far away"; let mut tokenizer = get_bert(); tokenizer.add_special_tokens(&[AddedToken::from("[MASK]", true)]); let output = tokenizer.encode(input, false).unwrap(); assert_eq!( output.get_offsets(), &[ (0, 9), (10, 11), (12, 15), (16, 17), (18, 24), (25, 28), (29, 33) ] ); assert_eq!( output.get_tokens(), &["yesterday", "i", "saw", "a", "[MASK]", "far", "away"] ); assert_eq!( output.get_word_ids(), &[ Some(0), Some(1), Some(2), Some(3), Some(4), Some(5), Some(6) ] ); }
tokenizers/tokenizers/tests/offsets.rs/0
{ "file_path": "tokenizers/tokenizers/tests/offsets.rs", "repo_id": "tokenizers", "token_count": 2497 }
264
import argparse import subprocess def main(config_dir, config_name, args): subprocess.run(["optimum-benchmark", "--config-dir", f"{config_dir}", "--config-name", f"{config_name}"] + ["hydra/job_logging=disabled", "hydra/hydra_logging=disabled"] + args) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--config-dir", type=str, required=True, help="The path to the config directory.") parser.add_argument("--config-name", type=str, required=True, help="The config name.") args, unknown = parser.parse_known_args() main(args.config_dir, args.config_name, unknown)
transformers/benchmark/optimum_benchmark_wrapper.py/0
{ "file_path": "transformers/benchmark/optimum_benchmark_wrapper.py", "repo_id": "transformers", "token_count": 216 }
265
FROM python:3.10 LABEL maintainer="Hugging Face" RUN apt update RUN git clone https://github.com/huggingface/transformers RUN python3 -m pip install --no-cache-dir --upgrade pip && python3 -m pip install --no-cache-dir git+https://github.com/huggingface/doc-builder ./transformers[dev] RUN apt-get -y update && apt-get install -y libsndfile1-dev && apt install -y tesseract-ocr # Torch needs to be installed before deepspeed RUN python3 -m pip install --no-cache-dir ./transformers[deepspeed] RUN python3 -m pip install --no-cache-dir torchvision git+https://github.com/facebookresearch/detectron2.git pytesseract RUN python3 -m pip install -U "itsdangerous<2.1.0" # Test if the image could successfully build the doc. before publishing the image RUN doc-builder build transformers transformers/docs/source/en --build_dir doc-build-dev --notebook_dir notebooks/transformers_doc --clean RUN rm -rf doc-build-dev
transformers/docker/transformers-doc-builder/Dockerfile/0
{ "file_path": "transformers/docker/transformers-doc-builder/Dockerfile", "repo_id": "transformers", "token_count": 292 }
266
# docstyle-ignore INSTALL_CONTENT = """ # Transformers installation ! pip install transformers datasets evaluate accelerate # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git """ notebook_first_cells = [{"type": "code", "content": INSTALL_CONTENT}] black_avoid_patterns = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
transformers/docs/source/_config.py/0
{ "file_path": "transformers/docs/source/_config.py", "repo_id": "transformers", "token_count": 157 }
267
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Schnellstart [[open-in-colab]] Mit ๐Ÿค— Transformers kรถnnen Sie sofort loslegen! Verwenden Sie die [`pipeline`] fรผr schnelle Inferenz und laden Sie schnell ein vortrainiertes Modell und einen Tokenizer mit einer [AutoClass](./model_doc/auto), um Ihre Text-, Bild- oder Audioaufgabe zu lรถsen. <Tip> Alle in der Dokumentation vorgestellten Codebeispiele haben oben links einen Umschalter fรผr PyTorch und TensorFlow. Wenn nicht, wird erwartet, dass der Code fรผr beide Backends ohne ร„nderungen funktioniert. </Tip> ## Pipeline [`pipeline`] ist der einfachste Weg, ein vortrainiertes Modell fรผr eine bestimmte Aufgabe zu verwenden. <Youtube id="tiZFewofSLM"/> Die [`pipeline`] unterstรผtzt viele gรคngige Aufgaben: **Text**: * Stimmungsanalyse: Klassifizierung der Polaritรคt eines gegebenen Textes. * Textgenerierung (auf Englisch): Generierung von Text aus einer gegebenen Eingabe. * Name-Entity-Recognition (NER): Kennzeichnung jedes Worts mit der Entitรคt, die es reprรคsentiert (Person, Datum, Ort usw.). * Beantwortung von Fragen: Extrahieren der Antwort aus dem Kontext, wenn ein gewisser Kontext und eine Frage gegeben sind. * Fill-mask: Ausfรผllen von Lรผcken in einem Text mit maskierten Wรถrtern. * Zusammenfassung: Erstellung einer Zusammenfassung einer langen Text- oder Dokumentensequenz. * รœbersetzung: รœbersetzen eines Textes in eine andere Sprache. * Merkmalsextraktion: Erstellen einer Tensordarstellung des Textes. **Bild**: * Bildklassifizierung: Klassifizierung eines Bildes. * Bildsegmentierung: Klassifizierung jedes Pixels in einem Bild. * Objekterkennung: Erkennen von Objekten innerhalb eines Bildes. **Audio**: * Audioklassifizierung: Zuweisung eines Labels zu einem bestimmten Audiosegment. * Automatische Spracherkennung (ASR): Transkription von Audiodaten in Text. <Tip> Fรผr mehr Details รผber die [`pipeline`] und assoziierte Aufgaben, schauen Sie in die Dokumentation [hier](./main_classes/pipelines). </Tip> ### Verwendung der Pipeline Im folgenden Beispiel werden Sie die [`pipeline`] fรผr die Stimmungsanalyse verwenden. Installieren Sie die folgenden Abhรคngigkeiten, falls Sie dies nicht bereits getan haben: <frameworkcontent> <pt> ```bash pip install torch ``` </pt> <tf> ```bash pip install tensorflow ``` </tf> </frameworkcontent> Importieren sie die [`pipeline`] und spezifizieren sie die Aufgabe, welche sie lรถsen mรถchten: ```py >>> from transformers import pipeline >>> classifier = pipeline("sentiment-analysis") ``` Die Pipeline lรคdt ein standardmรครŸiges [vortrainiertes Modell](https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english) und einen Tokenizer fรผr die Stimmungs-Analyse herunter und speichert sie. Jetzt kรถnnen Sie den "Klassifikator" auf Ihren Zieltext anwenden: ```py >>> classifier("We are very happy to show you the ๐Ÿค— Transformers library.") [{'label': 'POSITIVE', 'score': 0.9998}] ``` For more than one sentence, pass a list of sentences to the [`pipeline`] which returns a list of dictionaries: ```py >>> results = classifier(["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."]) >>> for result in results: ... print(f"label: {result['label']}, with score: {round(result['score'], 4)}") label: POSITIVE, with score: 0.9998 label: NEGATIVE, with score: 0.5309 ``` Die [`pipeline`] kann auch รผber einen ganzen Datensatz iterieren. Starten wir mit der Installation der [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/) Bibliothek: ```bash pip install datasets ``` Erstellen wir eine [`pipeline`] mit der Aufgabe die wir lรถsen und dem Modell welches wir nutzen mรถchten. ```py >>> import torch >>> from transformers import pipeline >>> speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h") ``` Als nรคchstes laden wir den Datensatz (siehe ๐Ÿค— Datasets [Quick Start](https://huggingface.co/docs/datasets/quickstart) fรผr mehr Details) welches wir nutzen mรถchten. Zum Beispiel laden wir den [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) Datensatz: ```py >>> from datasets import load_dataset, Audio >>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") # doctest: +IGNORE_RESULT ``` Wir mรผssen sicherstellen, dass die Abtastrate des Datensatzes der Abtastrate entspricht, mit der `facebook/wav2vec2-base-960h` trainiert wurde. ```py >>> dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate)) ``` Audiodateien werden automatisch geladen und neu abgetastet, wenn die Spalte "audio" aufgerufen wird. Extrahieren wir die rohen Wellenform-Arrays der ersten 4 Beispiele und รผbergeben wir sie als Liste an die Pipeline: ```py >>> result = speech_recognizer(dataset[:4]["audio"]) >>> print([d["text"] for d in result]) ['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FODING HOW I'D SET UP A JOIN TO HET WITH MY WIFE AND WHERE THE AP MIGHT BE", "I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M NOT SEEING THE OPTION TO DO IT ON THE AP SO I CALLED IN TO GET SOME HELP CAN I JUST DO IT OVER THE PHONE WITH YOU AND GIVE YOU THE INFORMATION OR SHOULD I DO IT IN THE AP AND I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS", 'HOW DO I THURN A JOIN A COUNT'] ``` Bei einem grรถรŸeren Datensatz mit vielen Eingaben (wie bei Sprache oder Bildverarbeitung) sollten Sie einen Generator anstelle einer Liste รผbergeben, der alle Eingaben in den Speicher lรคdt. Weitere Informationen finden Sie in der [Pipeline-Dokumentation](./main_classes/pipelines). ### Ein anderes Modell und einen anderen Tokenizer in der Pipeline verwenden Die [`pipeline`] kann jedes Modell aus dem [Model Hub](https://huggingface.co/models) verwenden, wodurch es einfach ist, die [`pipeline`] fรผr andere Anwendungsfรคlle anzupassen. Wenn Sie beispielsweise ein Modell wรผnschen, das franzรถsischen Text verarbeiten kann, verwenden Sie die Tags im Model Hub, um nach einem geeigneten Modell zu filtern. Das oberste gefilterte Ergebnis liefert ein mehrsprachiges [BERT-Modell](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment), das auf die Stimmungsanalyse abgestimmt ist. GroรŸartig, verwenden wir dieses Modell! ```py >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" ``` <frameworkcontent> <pt> Use the [`AutoModelForSequenceClassification`] and [`AutoTokenizer`] to load the pretrained model and it's associated tokenizer (more on an `AutoClass` below): ```py >>> from transformers import AutoTokenizer, AutoModelForSequenceClassification >>> model = AutoModelForSequenceClassification.from_pretrained(model_name) >>> tokenizer = AutoTokenizer.from_pretrained(model_name) ``` </pt> <tf> Use the [`TFAutoModelForSequenceClassification`] and [`AutoTokenizer`] to load the pretrained model and it's associated tokenizer (more on an `TFAutoClass` below): ```py >>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification >>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name) >>> tokenizer = AutoTokenizer.from_pretrained(model_name) ``` </tf> </frameworkcontent> Dann kรถnnen Sie das Modell und den Tokenizer in der [`pipeline`] angeben und den `Klassifikator` auf Ihren Zieltext anwenden: ```py >>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) >>> classifier("Nous sommes trรจs heureux de vous prรฉsenter la bibliothรจque ๐Ÿค— Transformers.") [{'label': '5 stars', 'score': 0.7273}] ``` Wenn Sie kein Modell fรผr Ihren Anwendungsfall finden kรถnnen, mรผssen Sie ein vortrainiertes Modell auf Ihren Daten feinabstimmen. Schauen Sie sich unser [Feinabstimmungs-Tutorial](./training) an, um zu erfahren, wie das geht. Und schlieรŸlich, nachdem Sie Ihr trainiertes Modell verfeinert haben, sollten Sie es mit der Community im Model Hub teilen (siehe Tutorial [hier](./model_sharing)), um NLP fรผr alle zu demokratisieren! ๐Ÿค— ## AutoClass <Youtube id="AhChOFRegn4"/> Unter der Haube arbeiten die Klassen [`AutoModelForSequenceClassification`] und [`AutoTokenizer`] zusammen, um die [`pipeline`] zu betreiben. Eine [`AutoClass`](./model_doc/auto) ist eine Abkรผrzung, die automatisch die Architektur eines trainierten Modells aus dessen Namen oder Pfad abruft. Sie mรผssen nur die passende `AutoClass` fรผr Ihre Aufgabe und den zugehรถrigen Tokenizer mit [`AutoTokenizer`] auswรคhlen. Kehren wir zu unserem Beispiel zurรผck und sehen wir uns an, wie Sie die `AutoClass` verwenden kรถnnen, um die Ergebnisse der [`pipeline`] zu replizieren. ### AutoTokenizer Ein Tokenizer ist fรผr die Vorverarbeitung von Text in ein fรผr das Modell verstรคndliches Format zustรคndig. Zunรคchst zerlegt der Tokenisierer den Text in Wรถrter, die *Token* genannt werden. Es gibt mehrere Regeln fรผr den Tokenisierungsprozess, z. B. wie und auf welcher Ebene ein Wort aufgespalten wird (weitere Informationen รผber Tokenisierung [hier](./tokenizer_summary)). Das Wichtigste ist jedoch, dass Sie den Tokenizer mit demselben Modellnamen instanziieren mรผssen, um sicherzustellen, dass Sie dieselben Tokenisierungsregeln verwenden, mit denen ein Modell zuvor trainiert wurde. Laden sie einen Tokenizer mit [`AutoTokenizer`]: ```py >>> from transformers import AutoTokenizer >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" >>> tokenizer = AutoTokenizer.from_pretrained(model_name) ``` AnschlieรŸend wandelt der Tokenizer die Token in Zahlen um, um einen Tensor als Eingabe fรผr das Modell zu konstruieren. Dieser wird als *Vokabular* des Modells bezeichnet. รœbergeben Sie Ihren Text an den Tokenizer: ```py >>> encoding = tokenizer("We are very happy to show you the ๐Ÿค— Transformers library.") >>> print(encoding) {'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} ``` Der Tokenizer gibt ein Wรถrterbuch zurรผck, das Folgendes enthรคlt: * [input_ids](./glossary#input-ids): numerische Reprรคsentationen Ihrer Token. * [atttention_mask](.glossary#attention-mask): gibt an, welche Token beachtet werden sollen. Genau wie die [`pipeline`] akzeptiert der Tokenizer eine Liste von Eingaben. Darรผber hinaus kann der Tokenizer den Text auch auffรผllen und kรผrzen, um einen Stapel mit einheitlicher Lรคnge zurรผckzugeben: <frameworkcontent> <pt> ```py >>> pt_batch = tokenizer( ... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], ... padding=True, ... truncation=True, ... max_length=512, ... return_tensors="pt", ... ) ``` </pt> <tf> ```py >>> tf_batch = tokenizer( ... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], ... padding=True, ... truncation=True, ... max_length=512, ... return_tensors="tf", ... ) ``` </tf> </frameworkcontent> Lesen Sie das Tutorial [preprocessing](./preprocessing) fรผr weitere Details zur Tokenisierung. ### AutoModel <frameworkcontent> <pt> ๐Ÿค— Transformers bietet eine einfache und einheitliche Mรถglichkeit, vortrainierte Instanzen zu laden. Das bedeutet, dass Sie ein [`AutoModel`] laden kรถnnen, wie Sie einen [`AutoTokenizer`] laden wรผrden. Der einzige Unterschied ist die Auswahl des richtigen [`AutoModel`] fรผr die Aufgabe. Da Sie eine Text- oder Sequenzklassifizierung vornehmen, laden Sie [`AutoModelForSequenceClassification`]: ```py >>> from transformers import AutoModelForSequenceClassification >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" >>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name) ``` <Tip> In der [Aufgabenzusammenfassung](./task_summary) steht, welche [AutoModel]-Klasse fรผr welche Aufgabe zu verwenden ist. </Tip> Jetzt kรถnnen Sie Ihren vorverarbeiteten Stapel von Eingaben direkt an das Modell รผbergeben. Sie mรผssen nur das Wรถrterbuch entpacken, indem Sie `**` hinzufรผgen: ```py >>> pt_outputs = pt_model(**pt_batch) ``` Das Modell gibt die endgรผltigen Aktivierungen in dem Attribut "logits" aus. Wenden Sie die Softmax-Funktion auf die "logits" an, um die Wahrscheinlichkeiten zu erhalten: ```py >>> from torch import nn >>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) >>> print(pt_predictions) tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725], [0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=<SoftmaxBackward0>) ``` </pt> <tf> ๐Ÿค— Transformers bietet eine einfache und einheitliche Methode zum Laden von vortrainierten Instanzen. Das bedeutet, dass Sie ein [`TFAutoModel`] genauso laden kรถnnen, wie Sie einen [`AutoTokenizer`] laden wรผrden. Der einzige Unterschied ist die Auswahl des richtigen [`TFAutoModel`] fรผr die Aufgabe. Da Sie Text - oder Sequenz - Klassifizierung machen, laden Sie [`TFAutoModelForSequenceClassification`]: ```py >>> from transformers import TFAutoModelForSequenceClassification >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" >>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name) ``` <Tip> In der [Aufgabenzusammenfassung](./task_summary) steht, welche [AutoModel]-Klasse fรผr welche Aufgabe zu verwenden ist. </Tip> Jetzt kรถnnen Sie Ihren vorverarbeiteten Stapel von Eingaben direkt an das Modell รผbergeben, indem Sie die Wรถrterbuchschlรผssel direkt an die Tensoren รผbergeben: ```py >>> tf_outputs = tf_model(tf_batch) ``` Das Modell gibt die endgรผltigen Aktivierungen in dem Attribut "logits" aus. Wenden Sie die Softmax-Funktion auf die "logits" an, um die Wahrscheinlichkeiten zu erhalten: ```py >>> import tensorflow as tf >>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) >>> tf_predictions # doctest: +IGNORE_RESULT ``` </tf> </frameworkcontent> <Tip> Alle ๐Ÿค— Transformers-Modelle (PyTorch oder TensorFlow) geben die Tensoren *vor* der endgรผltigen Aktivierungsfunktion Funktion (wie Softmax) aus, da die endgรผltige Aktivierungsfunktion oft mit dem Verlusten verschmolzen ist. </Tip> Modelle sind ein standardmรครŸiges [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) oder ein [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model), sodass Sie sie in Ihrer รผblichen Trainingsschleife verwenden kรถnnen. Um jedoch die Dinge einfacher zu machen, bietet ๐Ÿค— Transformers eine [`Trainer`]-Klasse fรผr PyTorch, die Funktionalitรคt fรผr verteiltes Training, gemischte Prรคzision und mehr bietet. Fรผr TensorFlow kรถnnen Sie die Methode `fit` aus [Keras](https://keras.io/) verwenden. Siehe das [training tutorial](./training) fรผr weitere Details. <Tip> Transformers-Modellausgaben sind spezielle Datenklassen, so dass ihre Attribute in einer IDE automatisch vervollstรคndigt werden. Die Modellausgรคnge verhalten sich auch wie ein Tupel oder ein Wรถrterbuch (z.B. kรถnnen Sie mit einem Integer, einem Slice oder einem String indexieren), wobei die Attribute, die "None" sind, ignoriert werden. </Tip> ### Modell speichern <frameworkcontent> <pt> Sobald Ihr Modell feinabgestimmt ist, kรถnnen Sie es mit seinem Tokenizer speichern, indem Sie [`PreTrainedModel.save_pretrained`] verwenden: ```py >>> pt_save_directory = "./pt_save_pretrained" >>> tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT >>> pt_model.save_pretrained(pt_save_directory) ``` Wenn Sie bereit sind, das Modell erneut zu verwenden, laden Sie es mit [`PreTrainedModel.from_pretrained`]: ```py >>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained") ``` </pt> <tf> Sobald Ihr Modell feinabgestimmt ist, kรถnnen Sie es mit seinem Tokenizer unter Verwendung von [`TFPreTrainedModel.save_pretrained`] speichern: ```py >>> tf_save_directory = "./tf_save_pretrained" >>> tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT >>> tf_model.save_pretrained(tf_save_directory) ``` Wenn Sie bereit sind, das Modell wieder zu verwenden, laden Sie es mit [`TFPreTrainedModel.from_pretrained`]: ```py >>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained") ``` </tf> </frameworkcontent> Ein besonders cooles ๐Ÿค— Transformers-Feature ist die Mรถglichkeit, ein Modell zu speichern und es entweder als PyTorch- oder TensorFlow-Modell wieder zu laden. Der Parameter "from_pt" oder "from_tf" kann das Modell von einem Framework in das andere konvertieren: <frameworkcontent> <pt> ```py >>> from transformers import AutoModel >>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) >>> pt_model = AutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True) ``` </pt> <tf> ```py >>> from transformers import TFAutoModel >>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) >>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True) ``` </tf> </frameworkcontent> ## Custom model builds Sie kรถnnen die Konfigurationsklasse des Modells รคndern, um zu bestimmen, wie ein Modell aufgebaut ist. Die Konfiguration legt die Attribute eines Modells fest, z. B. die Anzahl der verborgenen Schichten oder der Aufmerksamkeitskรถpfe. Wenn Sie ein Modell aus einer benutzerdefinierten Konfigurationsklasse initialisieren, beginnen Sie bei Null. Die Modellattribute werden zufรคllig initialisiert, und Sie mรผssen das Modell trainieren, bevor Sie es verwenden kรถnnen, um aussagekrรคftige Ergebnisse zu erhalten. Beginnen Sie mit dem Import von [`AutoConfig`] und laden Sie dann das trainierte Modell, das Sie รคndern mรถchten. Innerhalb von [`AutoConfig.from_pretrained`] kรถnnen Sie das Attribut angeben, das Sie รคndern mรถchten, z. B. die Anzahl der Aufmerksamkeitskรถpfe: ```py >>> from transformers import AutoConfig >>> my_config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased", n_heads=12) ``` <frameworkcontent> <pt> Create a model from your custom configuration with [`AutoModel.from_config`]: ```py >>> from transformers import AutoModel >>> my_model = AutoModel.from_config(my_config) ``` </pt> <tf> Create a model from your custom configuration with [`TFAutoModel.from_config`]: ```py >>> from transformers import TFAutoModel >>> my_model = TFAutoModel.from_config(my_config) ``` </tf> </frameworkcontent> Weitere Informationen zur Erstellung von benutzerdefinierten Konfigurationen finden Sie in der Anleitung [Erstellen einer benutzerdefinierten Architektur](./create_a_model). ## Wie geht es weiter? Nachdem Sie nun die ๐Ÿค— Transformers-Kurztour abgeschlossen haben, schauen Sie sich unsere Anleitungen an und erfahren Sie, wie Sie spezifischere Dinge tun kรถnnen, wie das Schreiben eines benutzerdefinierten Modells, die Feinabstimmung eines Modells fรผr eine Aufgabe und wie man ein Modell mit einem Skript trainiert. Wenn Sie mehr รผber die Kernkonzepte von ๐Ÿค— Transformers erfahren mรถchten, nehmen Sie sich eine Tasse Kaffee und werfen Sie einen Blick auf unsere konzeptionellen Leitfรคden!
transformers/docs/source/de/quicktour.md/0
{ "file_path": "transformers/docs/source/de/quicktour.md", "repo_id": "transformers", "token_count": 7330 }
268
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Instantiate a big model A barrier to accessing very large pretrained models is the amount of memory required. When loading a pretrained PyTorch model, you usually: 1. Create a model with random weights. 2. Load your pretrained weights. 3. Put those pretrained weights in the model. The first two steps both require a full version of the model in memory and if the model weighs several GBs, you may not have enough memory for two copies of it. This problem is amplified in distributed training environments because each process loads a pretrained model and stores two copies in memory. > [!TIP] > The randomly created model is initialized with "empty" tensors, which take space in memory without filling it. The random values are whatever was in this chunk of memory at the time. To improve loading speed, the [`_fast_init`](https://github.com/huggingface/transformers/blob/c9f6e5e35156e068b227dd9b15521767f6afd4d2/src/transformers/modeling_utils.py#L2710) parameter is set to `True` by default to skip the random initialization for all weights that are correctly loaded. This guide will show you how Transformers can help you load large pretrained models despite their memory requirements. ## Sharded checkpoints From Transformers v4.18.0, a checkpoint larger than 10GB is automatically sharded by the [`~PreTrainedModel.save_pretrained`] method. It is split into several smaller partial checkpoints and creates an index file that maps parameter names to the files they're stored in. The maximum shard size is controlled with the `max_shard_size` parameter, but by default it is 5GB, because it is easier to run on free-tier GPU instances without running out of memory. For example, let's shard [BioMistral/BioMistral-7B](https://hf.co/BioMistral/BioMistral-7B). ```py >>> with tempfile.TemporaryDirectory() as tmp_dir: ... model.save_pretrained(tmp_dir, max_shard_size="5GB") ... print(sorted(os.listdir(tmp_dir))) ['config.json', 'generation_config.json', 'model-00001-of-00006.safetensors', 'model-00002-of-00006.safetensors', 'model-00003-of-00006.safetensors', 'model-00004-of-00006.safetensors', 'model-00005-of-00006.safetensors', 'model-00006-of-00006.safetensors', 'model.safetensors.index.json'] ``` The sharded checkpoint is reloaded with the [`~PreTrainedModel.from_pretrained`] method. ```py >>> with tempfile.TemporaryDirectory() as tmp_dir: ... model.save_pretrained(tmp_dir, max_shard_size="5GB") ... new_model = AutoModel.from_pretrained(tmp_dir) ``` The main advantage of sharded checkpoints for big models is that each shard is loaded after the previous one, which caps the memory usage to only the model size and the largest shard size. You could also directly load a sharded checkpoint inside a model without the [`~PreTrainedModel.from_pretrained`] method (similar to PyTorch's `load_state_dict()` method for a full checkpoint). In this case, use the [`~modeling_utils.load_sharded_checkpoint`] method. ```py >>> from transformers.modeling_utils import load_sharded_checkpoint >>> with tempfile.TemporaryDirectory() as tmp_dir: ... model.save_pretrained(tmp_dir, max_shard_size="5GB") ... load_sharded_checkpoint(model, tmp_dir) ``` ### Shard metadata The index file determines which keys are in the checkpoint and where the corresponding weights are stored. This file is loaded like any other JSON file and you can get a dictionary from it. ```py >>> import json >>> with tempfile.TemporaryDirectory() as tmp_dir: ... model.save_pretrained(tmp_dir, max_shard_size="5GB") ... with open(os.path.join(tmp_dir, "model.safetensors.index.json"), "r") as f: ... index = json.load(f) >>> print(index.keys()) dict_keys(['metadata', 'weight_map']) ``` The `metadata` key provides the total model size. ```py >>> index["metadata"] {'total_size': 28966928384} ``` The `weight_map` key maps each parameter name (typically `state_dict` in a PyTorch model) to the shard it's stored in. ```py >>> index["weight_map"] {'lm_head.weight': 'model-00006-of-00006.safetensors', 'model.embed_tokens.weight': 'model-00001-of-00006.safetensors', 'model.layers.0.input_layernorm.weight': 'model-00001-of-00006.safetensors', 'model.layers.0.mlp.down_proj.weight': 'model-00001-of-00006.safetensors', ... } ``` ## Accelerate's Big Model Inference > [!TIP] > Make sure you have Accelerate v0.9.0 or later and PyTorch v1.9.0 or later installed. From Transformers v4.20.0, the [`~PreTrainedModel.from_pretrained`] method is supercharged with Accelerate's [Big Model Inference](https://hf.co/docs/accelerate/usage_guides/big_modeling) feature to efficiently handle really big models! Big Model Inference creates a *model skeleton* on PyTorch's [**meta**](https://pytorch.org/docs/main/meta.html) device. The randomly initialized parameters are only created when the pretrained weights are loaded. This way, you aren't keeping two copies of the model in memory at the same time (one for the randomly initialized model and one for the pretrained weights), and the maximum memory consumed is only the full model size. To enable Big Model Inference in Transformers, set `low_cpu_mem_usage=True` in the [`~PreTrainedModel.from_pretrained`] method. ```py from transformers import AutoModelForCausalLM gemma = AutoModelForCausalLM.from_pretrained("google/gemma-7b", low_cpu_mem_usage=True) ``` Accelerate automatically dispatches the model weights across all available devices, starting with the fastest device (GPU) first and then offloading to the slower devices (CPU and even hard drive). This is enabled by setting `device_map="auto"` in the [`~PreTrainedModel.from_pretrained`] method. When you pass the `device_map` parameter, `low_cpu_mem_usage` is automatically set to `True` so you don't need to specify it. ```py from transformers import AutoModelForCausalLM # these loading methods are equivalent gemma = AutoModelForCausalLM.from_pretrained("google/gemma-7b", device_map="auto") gemma = AutoModelForCausalLM.from_pretrained("google/gemma-7b", device_map="auto", low_cpu_mem_usage=True) ``` You can also write your own `device_map` by mapping each layer to a device. It should map all model parameters to a device, but you don't have to detail where all the submodules of a layer go if the entire layer is on the same device. ```python device_map = {"model.layers.1": 0, "model.layers.14": 1, "model.layers.31": "cpu", "lm_head": "disk"} ``` Access `hf_device_map` attribute to see how Accelerate split the model across devices. ```py gemma.hf_device_map ``` ```python out {'model.embed_tokens': 0, 'model.layers.0': 0, 'model.layers.1': 0, 'model.layers.2': 0, 'model.layers.3': 0, 'model.layers.4': 0, 'model.layers.5': 0, 'model.layers.6': 0, 'model.layers.7': 0, 'model.layers.8': 0, 'model.layers.9': 0, 'model.layers.10': 0, 'model.layers.11': 0, 'model.layers.12': 0, 'model.layers.13': 0, 'model.layers.14': 'cpu', 'model.layers.15': 'cpu', 'model.layers.16': 'cpu', 'model.layers.17': 'cpu', 'model.layers.18': 'cpu', 'model.layers.19': 'cpu', 'model.layers.20': 'cpu', 'model.layers.21': 'cpu', 'model.layers.22': 'cpu', 'model.layers.23': 'cpu', 'model.layers.24': 'cpu', 'model.layers.25': 'cpu', 'model.layers.26': 'cpu', 'model.layers.27': 'cpu', 'model.layers.28': 'cpu', 'model.layers.29': 'cpu', 'model.layers.30': 'cpu', 'model.layers.31': 'cpu', 'model.norm': 'cpu', 'lm_head': 'cpu'} ``` ## Model data type PyTorch model weights are normally instantiated as torch.float32 and it can be an issue if you try to load a model as a different data type. For example, you'd need twice as much memory to load the weights in torch.float32 and then again to load them in your desired data type, like torch.float16. > [!WARNING] > Due to how PyTorch is designed, the `torch_dtype` parameter only supports floating data types. To avoid wasting memory like this, explicitly set the `torch_dtype` parameter to the desired data type or set `torch_dtype="auto"` to load the weights with the most optimal memory pattern (the data type is automatically derived from the model weights). <hfoptions id="dtype"> <hfoption id="specific dtype"> ```py from transformers import AutoModelForCausalLM gemma = AutoModelForCausalLM.from_pretrained("google/gemma-7b", torch_dtype=torch.float16) ``` </hfoption> <hfoption id="auto dtype"> ```py from transformers import AutoModelForCausalLM gemma = AutoModelForCausalLM.from_pretrained("google/gemma-7b", torch_dtype="auto") ``` </hfoption> </hfoptions> You can also set the data type to use for models instantiated from scratch. ```python import torch from transformers import AutoConfig, AutoModel my_config = AutoConfig.from_pretrained("google/gemma-2b", torch_dtype=torch.float16) model = AutoModel.from_config(my_config) ```
transformers/docs/source/en/big_models.md/0
{ "file_path": "transformers/docs/source/en/big_models.md", "repo_id": "transformers", "token_count": 3022 }
269
<!--- Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Installation Install ๐Ÿค— Transformers for whichever deep learning library you're working with, setup your cache, and optionally configure ๐Ÿค— Transformers to run offline. ๐Ÿค— Transformers is tested on Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+, and Flax. Follow the installation instructions below for the deep learning library you are using: * [PyTorch](https://pytorch.org/get-started/locally/) installation instructions. * [TensorFlow 2.0](https://www.tensorflow.org/install/pip) installation instructions. * [Flax](https://flax.readthedocs.io/en/latest/) installation instructions. ## Install with pip You should install ๐Ÿค— Transformers in a [virtual environment](https://docs.python.org/3/library/venv.html). If you're unfamiliar with Python virtual environments, take a look at this [guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). A virtual environment makes it easier to manage different projects, and avoid compatibility issues between dependencies. Start by creating a virtual environment in your project directory: ```bash python -m venv .env ``` Activate the virtual environment. On Linux and MacOs: ```bash source .env/bin/activate ``` Activate Virtual environment on Windows ```bash .env/Scripts/activate ``` Now you're ready to install ๐Ÿค— Transformers with the following command: ```bash pip install transformers ``` For CPU-support only, you can conveniently install ๐Ÿค— Transformers and a deep learning library in one line. For example, install ๐Ÿค— Transformers and PyTorch with: ```bash pip install 'transformers[torch]' ``` ๐Ÿค— Transformers and TensorFlow 2.0: ```bash pip install 'transformers[tf-cpu]' ``` <Tip warning={true}> M1 / ARM Users You will need to install the following before installing TensorFLow 2.0 ```bash brew install cmake brew install pkg-config ``` </Tip> ๐Ÿค— Transformers and Flax: ```bash pip install 'transformers[flax]' ``` Finally, check if ๐Ÿค— Transformers has been properly installed by running the following command. It will download a pretrained model: ```bash python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" ``` Then print out the label and score: ```bash [{'label': 'POSITIVE', 'score': 0.9998704791069031}] ``` ## Install from source Install ๐Ÿค— Transformers from source with the following command: ```bash pip install git+https://github.com/huggingface/transformers ``` This command installs the bleeding edge `main` version rather than the latest `stable` version. The `main` version is useful for staying up-to-date with the latest developments. For instance, if a bug has been fixed since the last official release but a new release hasn't been rolled out yet. However, this means the `main` version may not always be stable. We strive to keep the `main` version operational, and most issues are usually resolved within a few hours or a day. If you run into a problem, please open an [Issue](https://github.com/huggingface/transformers/issues) so we can fix it even sooner! Check if ๐Ÿค— Transformers has been properly installed by running the following command: ```bash python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" ``` ## Editable install You will need an editable install if you'd like to: * Use the `main` version of the source code. * Contribute to ๐Ÿค— Transformers and need to test changes in the code. Clone the repository and install ๐Ÿค— Transformers with the following commands: ```bash git clone https://github.com/huggingface/transformers.git cd transformers pip install -e . ``` These commands will link the folder you cloned the repository to and your Python library paths. Python will now look inside the folder you cloned to in addition to the normal library paths. For example, if your Python packages are typically installed in `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python will also search the folder you cloned to: `~/transformers/`. <Tip warning={true}> You must keep the `transformers` folder if you want to keep using the library. </Tip> Now you can easily update your clone to the latest version of ๐Ÿค— Transformers with the following command: ```bash cd ~/transformers/ git pull ``` Your Python environment will find the `main` version of ๐Ÿค— Transformers on the next run. ## Install with conda Install from the conda channel `conda-forge`: ```bash conda install conda-forge::transformers ``` ## Cache setup Pretrained models are downloaded and locally cached at: `~/.cache/huggingface/hub`. This is the default directory given by the shell environment variable `TRANSFORMERS_CACHE`. On Windows, the default directory is given by `C:\Users\username\.cache\huggingface\hub`. You can change the shell environment variables shown below - in order of priority - to specify a different cache directory: 1. Shell environment variable (default): `HUGGINGFACE_HUB_CACHE` or `TRANSFORMERS_CACHE`. 2. Shell environment variable: `HF_HOME`. 3. Shell environment variable: `XDG_CACHE_HOME` + `/huggingface`. <Tip> ๐Ÿค— Transformers will use the shell environment variables `PYTORCH_TRANSFORMERS_CACHE` or `PYTORCH_PRETRAINED_BERT_CACHE` if you are coming from an earlier iteration of this library and have set those environment variables, unless you specify the shell environment variable `TRANSFORMERS_CACHE`. </Tip> ## Offline mode Run ๐Ÿค— Transformers in a firewalled or offline environment with locally cached files by setting the environment variable `HF_HUB_OFFLINE=1`. <Tip> Add [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/) to your offline training workflow with the environment variable `HF_DATASETS_OFFLINE=1`. </Tip> ```bash HF_DATASETS_OFFLINE=1 HF_HUB_OFFLINE=1 \ python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... ``` This script should run without hanging or waiting to timeout because it won't attempt to download the model from the Hub. You can also bypass loading a model from the Hub from each [`~PreTrainedModel.from_pretrained`] call with the [`local_files_only`] parameter. When set to `True`, only local files are loaded: ```py from transformers import T5Model model = T5Model.from_pretrained("./path/to/local/directory", local_files_only=True) ``` ### Fetch models and tokenizers to use offline Another option for using ๐Ÿค— Transformers offline is to download the files ahead of time, and then point to their local path when you need to use them offline. There are three ways to do this: * Download a file through the user interface on the [Model Hub](https://huggingface.co/models) by clicking on the โ†“ icon. ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png) * Use the [`PreTrainedModel.from_pretrained`] and [`PreTrainedModel.save_pretrained`] workflow: 1. Download your files ahead of time with [`PreTrainedModel.from_pretrained`]: ```py >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B") ``` 2. Save your files to a specified directory with [`PreTrainedModel.save_pretrained`]: ```py >>> tokenizer.save_pretrained("./your/path/bigscience_t0") >>> model.save_pretrained("./your/path/bigscience_t0") ``` 3. Now when you're offline, reload your files with [`PreTrainedModel.from_pretrained`] from the specified directory: ```py >>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0") ``` * Programmatically download files with the [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub) library: 1. Install the `huggingface_hub` library in your virtual environment: ```bash python -m pip install huggingface_hub ``` 2. Use the [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) function to download a file to a specific path. For example, the following command downloads the `config.json` file from the [T0](https://huggingface.co/bigscience/T0_3B) model to your desired path: ```py >>> from huggingface_hub import hf_hub_download >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0") ``` Once your file is downloaded and locally cached, specify it's local path to load and use it: ```py >>> from transformers import AutoConfig >>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json") ``` <Tip> See the [How to download files from the Hub](https://huggingface.co/docs/hub/how-to-downstream) section for more details on downloading files stored on the Hub. </Tip>
transformers/docs/source/en/installation.md/0
{ "file_path": "transformers/docs/source/en/installation.md", "repo_id": "transformers", "token_count": 2902 }
270
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Callbacks Callbacks are objects that can customize the behavior of the training loop in the PyTorch [`Trainer`] (this feature is not yet implemented in TensorFlow) that can inspect the training loop state (for progress reporting, logging on TensorBoard or other ML platforms...) and take decisions (like early stopping). Callbacks are "read only" pieces of code, apart from the [`TrainerControl`] object they return, they cannot change anything in the training loop. For customizations that require changes in the training loop, you should subclass [`Trainer`] and override the methods you need (see [trainer](trainer) for examples). By default, `TrainingArguments.report_to` is set to `"all"`, so a [`Trainer`] will use the following callbacks. - [`DefaultFlowCallback`] which handles the default behavior for logging, saving and evaluation. - [`PrinterCallback`] or [`ProgressCallback`] to display progress and print the logs (the first one is used if you deactivate tqdm through the [`TrainingArguments`], otherwise it's the second one). - [`~integrations.TensorBoardCallback`] if tensorboard is accessible (either through PyTorch >= 1.4 or tensorboardX). - [`~integrations.WandbCallback`] if [wandb](https://www.wandb.com/) is installed. - [`~integrations.CometCallback`] if [comet_ml](https://www.comet.com/site/) is installed. - [`~integrations.MLflowCallback`] if [mlflow](https://www.mlflow.org/) is installed. - [`~integrations.NeptuneCallback`] if [neptune](https://neptune.ai/) is installed. - [`~integrations.AzureMLCallback`] if [azureml-sdk](https://pypi.org/project/azureml-sdk/) is installed. - [`~integrations.CodeCarbonCallback`] if [codecarbon](https://pypi.org/project/codecarbon/) is installed. - [`~integrations.ClearMLCallback`] if [clearml](https://github.com/allegroai/clearml) is installed. - [`~integrations.DagsHubCallback`] if [dagshub](https://dagshub.com/) is installed. - [`~integrations.FlyteCallback`] if [flyte](https://flyte.org/) is installed. - [`~integrations.DVCLiveCallback`] if [dvclive](https://dvc.org/doc/dvclive) is installed. If a package is installed but you don't wish to use the accompanying integration, you can change `TrainingArguments.report_to` to a list of just those integrations you want to use (e.g. `["azure_ml", "wandb"]`). The main class that implements callbacks is [`TrainerCallback`]. It gets the [`TrainingArguments`] used to instantiate the [`Trainer`], can access that Trainer's internal state via [`TrainerState`], and can take some actions on the training loop via [`TrainerControl`]. ## Available Callbacks Here is the list of the available [`TrainerCallback`] in the library: [[autodoc]] integrations.CometCallback - setup [[autodoc]] DefaultFlowCallback [[autodoc]] PrinterCallback [[autodoc]] ProgressCallback [[autodoc]] EarlyStoppingCallback [[autodoc]] integrations.TensorBoardCallback [[autodoc]] integrations.WandbCallback - setup [[autodoc]] integrations.MLflowCallback - setup [[autodoc]] integrations.AzureMLCallback [[autodoc]] integrations.CodeCarbonCallback [[autodoc]] integrations.NeptuneCallback [[autodoc]] integrations.ClearMLCallback [[autodoc]] integrations.DagsHubCallback [[autodoc]] integrations.FlyteCallback [[autodoc]] integrations.DVCLiveCallback - setup ## TrainerCallback [[autodoc]] TrainerCallback Here is an example of how to register a custom callback with the PyTorch [`Trainer`]: ```python class MyCallback(TrainerCallback): "A callback that prints a message at the beginning of training" def on_train_begin(self, args, state, control, **kwargs): print("Starting training") trainer = Trainer( model, args, train_dataset=train_dataset, eval_dataset=eval_dataset, callbacks=[MyCallback], # We can either pass the callback class this way or an instance of it (MyCallback()) ) ``` Another way to register a callback is to call `trainer.add_callback()` as follows: ```python trainer = Trainer(...) trainer.add_callback(MyCallback) # Alternatively, we can pass an instance of the callback class trainer.add_callback(MyCallback()) ``` ## TrainerState [[autodoc]] TrainerState ## TrainerControl [[autodoc]] TrainerControl
transformers/docs/source/en/main_classes/callback.md/0
{ "file_path": "transformers/docs/source/en/main_classes/callback.md", "repo_id": "transformers", "token_count": 1520 }
271
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Tokenizer A tokenizer is in charge of preparing the inputs for a model. The library contains tokenizers for all the models. Most of the tokenizers are available in two flavors: a full python implementation and a "Fast" implementation based on the Rust library [๐Ÿค— Tokenizers](https://github.com/huggingface/tokenizers). The "Fast" implementations allows: 1. a significant speed-up in particular when doing batched tokenization and 2. additional methods to map between the original string (character and words) and the token space (e.g. getting the index of the token comprising a given character or the span of characters corresponding to a given token). The base classes [`PreTrainedTokenizer`] and [`PreTrainedTokenizerFast`] implement the common methods for encoding string inputs in model inputs (see below) and instantiating/saving python and "Fast" tokenizers either from a local file or directory or from a pretrained tokenizer provided by the library (downloaded from HuggingFace's AWS S3 repository). They both rely on [`~tokenization_utils_base.PreTrainedTokenizerBase`] that contains the common methods, and [`~tokenization_utils_base.SpecialTokensMixin`]. [`PreTrainedTokenizer`] and [`PreTrainedTokenizerFast`] thus implement the main methods for using all the tokenizers: - Tokenizing (splitting strings in sub-word token strings), converting tokens strings to ids and back, and encoding/decoding (i.e., tokenizing and converting to integers). - Adding new tokens to the vocabulary in a way that is independent of the underlying structure (BPE, SentencePiece...). - Managing special tokens (like mask, beginning-of-sentence, etc.): adding them, assigning them to attributes in the tokenizer for easy access and making sure they are not split during tokenization. [`BatchEncoding`] holds the output of the [`~tokenization_utils_base.PreTrainedTokenizerBase`]'s encoding methods (`__call__`, `encode_plus` and `batch_encode_plus`) and is derived from a Python dictionary. When the tokenizer is a pure python tokenizer, this class behaves just like a standard python dictionary and holds the various model inputs computed by these methods (`input_ids`, `attention_mask`...). When the tokenizer is a "Fast" tokenizer (i.e., backed by HuggingFace [tokenizers library](https://github.com/huggingface/tokenizers)), this class provides in addition several advanced alignment methods which can be used to map between the original string (character and words) and the token space (e.g., getting the index of the token comprising a given character or the span of characters corresponding to a given token). ## PreTrainedTokenizer [[autodoc]] PreTrainedTokenizer - __call__ - add_tokens - add_special_tokens - apply_chat_template - batch_decode - decode - encode - push_to_hub - all ## PreTrainedTokenizerFast The [`PreTrainedTokenizerFast`] depend on the [tokenizers](https://huggingface.co/docs/tokenizers) library. The tokenizers obtained from the ๐Ÿค— tokenizers library can be loaded very simply into ๐Ÿค— transformers. Take a look at the [Using tokenizers from ๐Ÿค— tokenizers](../fast_tokenizers) page to understand how this is done. [[autodoc]] PreTrainedTokenizerFast - __call__ - add_tokens - add_special_tokens - apply_chat_template - batch_decode - decode - encode - push_to_hub - all ## BatchEncoding [[autodoc]] BatchEncoding
transformers/docs/source/en/main_classes/tokenizer.md/0
{ "file_path": "transformers/docs/source/en/main_classes/tokenizer.md", "repo_id": "transformers", "token_count": 1144 }
272
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # BERTweet ## Overview The BERTweet model was proposed in [BERTweet: A pre-trained language model for English Tweets](https://www.aclweb.org/anthology/2020.emnlp-demos.2.pdf) by Dat Quoc Nguyen, Thanh Vu, Anh Tuan Nguyen. The abstract from the paper is the following: *We present BERTweet, the first public large-scale pre-trained language model for English Tweets. Our BERTweet, having the same architecture as BERT-base (Devlin et al., 2019), is trained using the RoBERTa pre-training procedure (Liu et al., 2019). Experiments show that BERTweet outperforms strong baselines RoBERTa-base and XLM-R-base (Conneau et al., 2020), producing better performance results than the previous state-of-the-art models on three Tweet NLP tasks: Part-of-speech tagging, Named-entity recognition and text classification.* This model was contributed by [dqnguyen](https://huggingface.co/dqnguyen). The original code can be found [here](https://github.com/VinAIResearch/BERTweet). ## Usage example ```python >>> import torch >>> from transformers import AutoModel, AutoTokenizer >>> bertweet = AutoModel.from_pretrained("vinai/bertweet-base") >>> # For transformers v4.x+: >>> tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base", use_fast=False) >>> # For transformers v3.x: >>> # tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base") >>> # INPUT TWEET IS ALREADY NORMALIZED! >>> line = "SC has first two presumptive cases of coronavirus , DHEC confirms HTTPURL via @USER :cry:" >>> input_ids = torch.tensor([tokenizer.encode(line)]) >>> with torch.no_grad(): ... features = bertweet(input_ids) # Models outputs are now tuples >>> # With TensorFlow 2.0+: >>> # from transformers import TFAutoModel >>> # bertweet = TFAutoModel.from_pretrained("vinai/bertweet-base") ``` <Tip> This implementation is the same as BERT, except for tokenization method. Refer to [BERT documentation](bert) for API reference information. </Tip> ## BertweetTokenizer [[autodoc]] BertweetTokenizer
transformers/docs/source/en/model_doc/bertweet.md/0
{ "file_path": "transformers/docs/source/en/model_doc/bertweet.md", "repo_id": "transformers", "token_count": 806 }
273
<!--Copyright 2024 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Chameleon ## Overview The Chameleon model was proposed in [Chameleon: Mixed-Modal Early-Fusion Foundation Models ](https://arxiv.org/abs/2405.09818v1) by META AI Chameleon Team. Chameleon is a Vision-Language Model that use vector quantization to tokenize images which enables the model to generate multimodal output. The model takes images and texts as input, including an interleaved format, and generates textual response. Image generation module is not released yet. The abstract from the paper is the following: *We present Chameleon, a family of early-fusion token-based mixed-modal models capable of understanding and generating images and text in any arbitrary sequence. We outline a stable training approach from inception, an alignment recipe, and an architectural parameterization tailored for the early-fusion, token-based, mixed-modal setting. The models are evaluated on a comprehensive range of tasks, including visual question answering, image captioning, text generation, image generation, and long-form mixed modal generation. Chameleon demonstrates broad and general capabilities, including state-of-the-art performance in image captioning tasks, outperforms Llama-2 in text-only tasks while being competitive with models such as Mixtral 8x7B and Gemini-Pro, and performs non-trivial image generation, all in a single model. It also matches or exceeds the performance of much larger models, including Gemini Pro and GPT-4V, according to human judgments on a new long-form mixed-modal generation evaluation, where either the prompt or outputs contain mixed sequences of both images and text. Chameleon marks a significant step forward in unified modeling of full multimodal documents* <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/chameleon_arch.png" alt="drawing" width="600"/> <small> Chameleon incorporates a vector quantizer module to transform images into discrete tokens. That also enables image generation using an auto-regressive transformer. Taken from the <a href="https://arxiv.org/abs/2405.09818v1">original paper.</a> </small> This model was contributed by [joaogante](https://huggingface.co/joaogante) and [RaushanTurganbay](https://huggingface.co/RaushanTurganbay). The original code can be found [here](https://github.com/facebookresearch/chameleon). ## Usage tips - We advise users to use `padding_side="left"` when computing batched generation as it leads to more accurate results. Simply make sure to set `processor.tokenizer.padding_side = "left"` before generating. - Note that Chameleon was tuned for safety alignment. If the model is refusing to answer, consider asking a more concrete question, instead of an open question. - Chameleon generates in chat format which means that the generated text will always be the "assistant's turn". You can enable a text completion generation by passing `return_for_text_completion=True` when calling the processor. > [!NOTE] > Chameleon implementation in Transformers uses a special image token to indicate where to merge image embeddings. For special image token we didn't add a new one but used one of the reserved tokens: `<reserved08707>`. You have to add `<image>` to your prompt in the place where the image should be embedded for correct generation. ## Usage example ### Single image inference Chameleon is a gated model so make sure to have access and login to Hugging Face Hub using a token. Here's how to load the model and perform inference in half-precision (`torch.bfloat16`): ```python from transformers import ChameleonProcessor, ChameleonForConditionalGeneration import torch from PIL import Image import requests processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", torch_dtype=torch.bfloat16, device_map="cuda") # prepare image and text prompt url = 'http://images.cocodataset.org/val2017/000000039769.jpg' image = Image.open(requests.get(url, stream=True).raw) prompt = "What do you see in this image?<image>" inputs = processor(prompt, image, return_tensors="pt").to(model.device) # autoregressively complete prompt output = model.generate(**inputs, max_new_tokens=50) print(processor.decode(output[0], skip_special_tokens=True)) ``` ### Multi image inference Chameleon can perform inference with multiple images as input, where images either belong to the same prompt or different prompts (in batched inference). Here is how you can do it: ```python from transformers import ChameleonProcessor, ChameleonForConditionalGeneration import torch from PIL import Image import requests processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", torch_dtype=torch.bfloat16, device_map="cuda") # Get three different images url = "https://www.ilankelman.org/stopsigns/australia.jpg" image_stop = Image.open(requests.get(url, stream=True).raw) url = "http://images.cocodataset.org/val2017/000000039769.jpg" image_cats = Image.open(requests.get(url, stream=True).raw) url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg" image_snowman = Image.open(requests.get(url, stream=True).raw) # Prepare a batched prompt, where the first one is a multi-image prompt and the second is not prompts = [ "What do these images have in common?<image><image>", "<image>What is shown in this image?" ] # We can simply feed images in the order they have to be used in the text prompt # Each "<image>" token uses one image leaving the next for the subsequent "<image>" tokens inputs = processor(text=prompts, images=[image_stop, image_cats, image_snowman], padding=True, return_tensors="pt").to(device="cuda", dtype=torch.bfloat16) # Generate generate_ids = model.generate(**inputs, max_new_tokens=50) processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) ``` ## Model optimization ### Quantization using Bitsandbytes The model can be loaded in 8 or 4 bits, greatly reducing the memory requirements while maintaining the performance of the original model. First make sure to install bitsandbytes, `pip install bitsandbytes` and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with: ```python from transformers import ChameleonForConditionalGeneration, BitsAndBytesConfig # specify how to quantize the model quantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, ) model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", quantization_config=quantization_config, device_map="cuda") ``` ### Use Flash-Attention 2 and SDPA to further speed-up generation The models supports both, Flash-Attention 2 and PyTorch's [`torch.nn.functional.scaled_dot_product_attention`](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html) which can be enables for optimization. SDPA is the default options when you load the model, If you want to switch for Flash Attention 2, first make sure to install flash-attn. Refer to the [original repository](https://github.com/Dao-AILab/flash-attention) regarding that package installation. Simply change the snippet above with: ```python from transformers import ChameleonForConditionalGeneration model_id = "facebook/chameleon-7b" model = ChameleonForConditionalGeneration.from_pretrained( model_id, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, attn_implementation="flash_attention_2" ).to(0) ``` ## ChameleonConfig [[autodoc]] ChameleonConfig ## ChameleonVQVAEConfig [[autodoc]] ChameleonVQVAEConfig ## ChameleonProcessor [[autodoc]] ChameleonProcessor ## ChameleonImageProcessor [[autodoc]] ChameleonImageProcessor - preprocess ## ChameleonVQVAE [[autodoc]] ChameleonVQVAE - forward ## ChameleonModel [[autodoc]] ChameleonModel - forward ## ChameleonForConditionalGeneration [[autodoc]] ChameleonForConditionalGeneration - forward
transformers/docs/source/en/model_doc/chameleon.md/0
{ "file_path": "transformers/docs/source/en/model_doc/chameleon.md", "repo_id": "transformers", "token_count": 2563 }
274
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Convolutional Vision Transformer (CvT) ## Overview The CvT model was proposed in [CvT: Introducing Convolutions to Vision Transformers](https://arxiv.org/abs/2103.15808) by Haiping Wu, Bin Xiao, Noel Codella, Mengchen Liu, Xiyang Dai, Lu Yuan and Lei Zhang. The Convolutional vision Transformer (CvT) improves the [Vision Transformer (ViT)](vit) in performance and efficiency by introducing convolutions into ViT to yield the best of both designs. The abstract from the paper is the following: *We present in this paper a new architecture, named Convolutional vision Transformer (CvT), that improves Vision Transformer (ViT) in performance and efficiency by introducing convolutions into ViT to yield the best of both designs. This is accomplished through two primary modifications: a hierarchy of Transformers containing a new convolutional token embedding, and a convolutional Transformer block leveraging a convolutional projection. These changes introduce desirable properties of convolutional neural networks (CNNs) to the ViT architecture (\ie shift, scale, and distortion invariance) while maintaining the merits of Transformers (\ie dynamic attention, global context, and better generalization). We validate CvT by conducting extensive experiments, showing that this approach achieves state-of-the-art performance over other Vision Transformers and ResNets on ImageNet-1k, with fewer parameters and lower FLOPs. In addition, performance gains are maintained when pretrained on larger datasets (\eg ImageNet-22k) and fine-tuned to downstream tasks. Pre-trained on ImageNet-22k, our CvT-W24 obtains a top-1 accuracy of 87.7\% on the ImageNet-1k val set. Finally, our results show that the positional encoding, a crucial component in existing Vision Transformers, can be safely removed in our model, simplifying the design for higher resolution vision tasks.* This model was contributed by [anugunj](https://huggingface.co/anugunj). The original code can be found [here](https://github.com/microsoft/CvT). ## Usage tips - CvT models are regular Vision Transformers, but trained with convolutions. They outperform the [original model (ViT)](vit) when fine-tuned on ImageNet-1K and CIFAR-100. - You can check out demo notebooks regarding inference as well as fine-tuning on custom data [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/VisionTransformer) (you can just replace [`ViTFeatureExtractor`] by [`AutoImageProcessor`] and [`ViTForImageClassification`] by [`CvtForImageClassification`]). - The available checkpoints are either (1) pre-trained on [ImageNet-22k](http://www.image-net.org/) (a collection of 14 million images and 22k classes) only, (2) also fine-tuned on ImageNet-22k or (3) also fine-tuned on [ImageNet-1k](http://www.image-net.org/challenges/LSVRC/2012/) (also referred to as ILSVRC 2012, a collection of 1.3 million images and 1,000 classes). ## Resources A list of official Hugging Face and community (indicated by ๐ŸŒŽ) resources to help you get started with CvT. <PipelineTag pipeline="image-classification"/> - [`CvtForImageClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb). - See also: [Image classification task guide](../tasks/image_classification) If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. ## CvtConfig [[autodoc]] CvtConfig <frameworkcontent> <pt> ## CvtModel [[autodoc]] CvtModel - forward ## CvtForImageClassification [[autodoc]] CvtForImageClassification - forward </pt> <tf> ## TFCvtModel [[autodoc]] TFCvtModel - call ## TFCvtForImageClassification [[autodoc]] TFCvtForImageClassification - call </tf> </frameworkcontent>
transformers/docs/source/en/model_doc/cvt.md/0
{ "file_path": "transformers/docs/source/en/model_doc/cvt.md", "repo_id": "transformers", "token_count": 1314 }
275
<!--Copyright 2021 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. specific language governing permissions and limitations under the License. --> # ImageGPT ## Overview The ImageGPT model was proposed in [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt) by Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever. ImageGPT (iGPT) is a GPT-2-like model trained to predict the next pixel value, allowing for both unconditional and conditional image generation. The abstract from the paper is the following: *Inspired by progress in unsupervised representation learning for natural language, we examine whether similar models can learn useful representations for images. We train a sequence Transformer to auto-regressively predict pixels, without incorporating knowledge of the 2D input structure. Despite training on low-resolution ImageNet without labels, we find that a GPT-2 scale model learns strong image representations as measured by linear probing, fine-tuning, and low-data classification. On CIFAR-10, we achieve 96.3% accuracy with a linear probe, outperforming a supervised Wide ResNet, and 99.0% accuracy with full fine-tuning, matching the top supervised pre-trained models. We are also competitive with self-supervised benchmarks on ImageNet when substituting pixels for a VQVAE encoding, achieving 69.0% top-1 accuracy on a linear probe of our features.* <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/imagegpt_architecture.png" alt="drawing" width="600"/> <small> Summary of the approach. Taken from the [original paper](https://cdn.openai.com/papers/Generative_Pretraining_from_Pixels_V2.pdf). </small> This model was contributed by [nielsr](https://huggingface.co/nielsr), based on [this issue](https://github.com/openai/image-gpt/issues/7). The original code can be found [here](https://github.com/openai/image-gpt). ## Usage tips - ImageGPT is almost exactly the same as [GPT-2](gpt2), with the exception that a different activation function is used (namely "quick gelu"), and the layer normalization layers don't mean center the inputs. ImageGPT also doesn't have tied input- and output embeddings. - As the time- and memory requirements of the attention mechanism of Transformers scales quadratically in the sequence length, the authors pre-trained ImageGPT on smaller input resolutions, such as 32x32 and 64x64. However, feeding a sequence of 32x32x3=3072 tokens from 0..255 into a Transformer is still prohibitively large. Therefore, the authors applied k-means clustering to the (R,G,B) pixel values with k=512. This way, we only have a 32*32 = 1024-long sequence, but now of integers in the range 0..511. So we are shrinking the sequence length at the cost of a bigger embedding matrix. In other words, the vocabulary size of ImageGPT is 512, + 1 for a special "start of sentence" (SOS) token, used at the beginning of every sequence. One can use [`ImageGPTImageProcessor`] to prepare images for the model. - Despite being pre-trained entirely unsupervised (i.e. without the use of any labels), ImageGPT produces fairly performant image features useful for downstream tasks, such as image classification. The authors showed that the features in the middle of the network are the most performant, and can be used as-is to train a linear model (such as a sklearn logistic regression model for example). This is also referred to as "linear probing". Features can be easily obtained by first forwarding the image through the model, then specifying `output_hidden_states=True`, and then average-pool the hidden states at whatever layer you like. - Alternatively, one can further fine-tune the entire model on a downstream dataset, similar to BERT. For this, you can use [`ImageGPTForImageClassification`]. - ImageGPT comes in different sizes: there's ImageGPT-small, ImageGPT-medium and ImageGPT-large. The authors did also train an XL variant, which they didn't release. The differences in size are summarized in the following table: | **Model variant** | **Depths** | **Hidden sizes** | **Decoder hidden size** | **Params (M)** | **ImageNet-1k Top 1** | |---|---|---|---|---|---| | MiT-b0 | [2, 2, 2, 2] | [32, 64, 160, 256] | 256 | 3.7 | 70.5 | | MiT-b1 | [2, 2, 2, 2] | [64, 128, 320, 512] | 256 | 14.0 | 78.7 | | MiT-b2 | [3, 4, 6, 3] | [64, 128, 320, 512] | 768 | 25.4 | 81.6 | | MiT-b3 | [3, 4, 18, 3] | [64, 128, 320, 512] | 768 | 45.2 | 83.1 | | MiT-b4 | [3, 8, 27, 3] | [64, 128, 320, 512] | 768 | 62.6 | 83.6 | | MiT-b5 | [3, 6, 40, 3] | [64, 128, 320, 512] | 768 | 82.0 | 83.8 | ## Resources A list of official Hugging Face and community (indicated by ๐ŸŒŽ) resources to help you get started with ImageGPT. <PipelineTag pipeline="image-classification"/> - Demo notebooks for ImageGPT can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/ImageGPT). - [`ImageGPTForImageClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb). - See also: [Image classification task guide](../tasks/image_classification) If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. ## ImageGPTConfig [[autodoc]] ImageGPTConfig ## ImageGPTFeatureExtractor [[autodoc]] ImageGPTFeatureExtractor - __call__ ## ImageGPTImageProcessor [[autodoc]] ImageGPTImageProcessor - preprocess ## ImageGPTModel [[autodoc]] ImageGPTModel - forward ## ImageGPTForCausalImageModeling [[autodoc]] ImageGPTForCausalImageModeling - forward ## ImageGPTForImageClassification [[autodoc]] ImageGPTForImageClassification - forward
transformers/docs/source/en/model_doc/imagegpt.md/0
{ "file_path": "transformers/docs/source/en/model_doc/imagegpt.md", "repo_id": "transformers", "token_count": 1915 }
276
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # T5 <div class="flex flex-wrap space-x-1"> <a href="https://huggingface.co/models?filter=t5"> <img alt="Models" src="https://img.shields.io/badge/All_model_pages-t5-blueviolet"> </a> <a href="https://huggingface.co/spaces/docs-demos/t5-base"> <img alt="Spaces" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue"> </a> <a href="https://huggingface.co/papers/1910.10683"> <img alt="Paper page" src="https://img.shields.io/badge/Paper%20page-1910.10683-green"> </a> </div> ## Overview The T5 model was presented in [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/pdf/1910.10683.pdf) by [Colin Raffel](https://huggingface.co/craffel), Noam Shazeer, [Adam Roberts](https://huggingface.co/adarob), Katherine Lee, Sharan Narang, Michael Matena, Yanqi Zhou, Wei Li, [Peter J. Liu](https://huggingface.co/peterjliu). The abstract from the paper is the following: *Transfer learning, where a model is first pre-trained on a data-rich task before being fine-tuned on a downstream task, has emerged as a powerful technique in natural language processing (NLP). The effectiveness of transfer learning has given rise to a diversity of approaches, methodology, and practice. In this paper, we explore the landscape of transfer learning techniques for NLP by introducing a unified framework that converts every language problem into a text-to-text format. Our systematic study compares pretraining objectives, architectures, unlabeled datasets, transfer approaches, and other factors on dozens of language understanding tasks. By combining the insights from our exploration with scale and our new "Colossal Clean Crawled Corpus", we achieve state-of-the-art results on many benchmarks covering summarization, question answering, text classification, and more. To facilitate future work on transfer learning for NLP, we release our dataset, pre-trained models, and code.* All checkpoints can be found on the [hub](https://huggingface.co/models?search=t5). This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://github.com/google-research/text-to-text-transfer-transformer). ## Usage tips - T5 is an encoder-decoder model pre-trained on a multi-task mixture of unsupervised and supervised tasks and for which each task is converted into a text-to-text format. T5 works well on a variety of tasks out-of-the-box by prepending a different prefix to the input corresponding to each task, e.g., for translation: *translate English to German: ...*, for summarization: *summarize: ...*. - The pretraining includes both supervised and self-supervised training. Supervised training is conducted on downstream tasks provided by the GLUE and SuperGLUE benchmarks (converting them into text-to-text tasks as explained above). - Self-supervised training uses corrupted tokens, by randomly removing 15% of the tokens and replacing them with individual sentinel tokens (if several consecutive tokens are marked for removal, the whole group is replaced with a single sentinel token). The input of the encoder is the corrupted sentence, the input of the decoder is the original sentence and the target is then the dropped out tokens delimited by their sentinel tokens. - T5 uses relative scalar embeddings. Encoder input padding can be done on the left and on the right. - See the [training](#training), [inference](#inference) and [resources](#resources) sections below for all details regarding usage. T5 comes in different sizes: - [google-t5/t5-small](https://huggingface.co/google-t5/t5-small) - [google-t5/t5-base](https://huggingface.co/google-t5/t5-base) - [google-t5/t5-large](https://huggingface.co/google-t5/t5-large) - [google-t5/t5-3b](https://huggingface.co/google-t5/t5-3b) - [google-t5/t5-11b](https://huggingface.co/google-t5/t5-11b). Based on the original T5 model, Google has released some follow-up works: - **T5v1.1**: T5v1.1 is an improved version of T5 with some architectural tweaks, and is pre-trained on C4 only without mixing in the supervised tasks. Refer to the documentation of T5v1.1 which can be found [here](t5v1.1). - **mT5**: mT5 is a multilingual T5 model. It is pre-trained on the mC4 corpus, which includes 101 languages. Refer to the documentation of mT5 which can be found [here](mt5). - **byT5**: byT5 is a T5 model pre-trained on byte sequences rather than SentencePiece subword token sequences. Refer to the documentation of byT5 which can be found [here](byt5). - **UL2**: UL2 is a T5 like model pretrained on various denoising objectives - **Flan-T5**: Flan is a pretraining methods that is based on prompting. The Flan-T5 are T5 models trained on the Flan collection of datasets which include: `taskmaster2`, `djaym7/wiki_dialog`, `deepmind/code_contests`, `lambada`, `gsm8k`, `aqua_rat`, `esnli`, `quasc` and `qed`. - **FLan-UL2** : the UL2 model finetuned using the "Flan" prompt tuning and dataset collection. - **UMT5**: UmT5 is a multilingual T5 model trained on an improved and refreshed mC4 multilingual corpus, 29 trillion characters across 107 language, using a new sampling method, UniMax. Refer to the documentation of mT5 which can be found [here](umt5). ## Training T5 is an encoder-decoder model and converts all NLP problems into a text-to-text format. It is trained using teacher forcing. This means that for training, we always need an input sequence and a corresponding target sequence. The input sequence is fed to the model using `input_ids`. The target sequence is shifted to the right, i.e., prepended by a start-sequence token and fed to the decoder using the `decoder_input_ids`. In teacher-forcing style, the target sequence is then appended by the EOS token and corresponds to the `labels`. The PAD token is hereby used as the start-sequence token. T5 can be trained / fine-tuned both in a supervised and unsupervised fashion. One can use [`T5ForConditionalGeneration`] (or the Tensorflow/Flax variant), which includes the language modeling head on top of the decoder. - Unsupervised denoising training In this setup, spans of the input sequence are masked by so-called sentinel tokens (*a.k.a* unique mask tokens) and the output sequence is formed as a concatenation of the same sentinel tokens and the *real* masked tokens. Each sentinel token represents a unique mask token for this sentence and should start with `<extra_id_0>`, `<extra_id_1>`, ... up to `<extra_id_99>`. As a default, 100 sentinel tokens are available in [`T5Tokenizer`]. For instance, the sentence "The cute dog walks in the park" with the masks put on "cute dog" and "the" should be processed as follows: ```python >>> from transformers import T5Tokenizer, T5ForConditionalGeneration >>> tokenizer = T5Tokenizer.from_pretrained("google-t5/t5-small") >>> model = T5ForConditionalGeneration.from_pretrained("google-t5/t5-small") >>> input_ids = tokenizer("The <extra_id_0> walks in <extra_id_1> park", return_tensors="pt").input_ids >>> labels = tokenizer("<extra_id_0> cute dog <extra_id_1> the <extra_id_2>", return_tensors="pt").input_ids >>> # the forward function automatically creates the correct decoder_input_ids >>> loss = model(input_ids=input_ids, labels=labels).loss >>> loss.item() 3.7837 ``` If you're interested in pre-training T5 on a new corpus, check out the [run_t5_mlm_flax.py](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling) script in the Examples directory. - Supervised training In this setup, the input sequence and output sequence are a standard sequence-to-sequence input-output mapping. Suppose that we want to fine-tune the model for translation for example, and we have a training example: the input sequence "The house is wonderful." and output sequence "Das Haus ist wunderbar.", then they should be prepared for the model as follows: ```python >>> from transformers import T5Tokenizer, T5ForConditionalGeneration >>> tokenizer = T5Tokenizer.from_pretrained("google-t5/t5-small") >>> model = T5ForConditionalGeneration.from_pretrained("google-t5/t5-small") >>> input_ids = tokenizer("translate English to German: The house is wonderful.", return_tensors="pt").input_ids >>> labels = tokenizer("Das Haus ist wunderbar.", return_tensors="pt").input_ids >>> # the forward function automatically creates the correct decoder_input_ids >>> loss = model(input_ids=input_ids, labels=labels).loss >>> loss.item() 0.2542 ``` As you can see, only 2 inputs are required for the model in order to compute a loss: `input_ids` (which are the `input_ids` of the encoded input sequence) and `labels` (which are the `input_ids` of the encoded target sequence). The model will automatically create the `decoder_input_ids` based on the `labels`, by shifting them one position to the right and prepending the `config.decoder_start_token_id`, which for T5 is equal to 0 (i.e. the id of the pad token). Also note the task prefix: we prepend the input sequence with 'translate English to German: ' before encoding it. This will help in improving the performance, as this task prefix was used during T5's pre-training. However, the example above only shows a single training example. In practice, one trains deep learning models in batches. This entails that we must pad/truncate examples to the same length. For encoder-decoder models, one typically defines a `max_source_length` and `max_target_length`, which determine the maximum length of the input and output sequences respectively (otherwise they are truncated). These should be carefully set depending on the task. In addition, we must make sure that padding token id's of the `labels` are not taken into account by the loss function. In PyTorch and Tensorflow, this can be done by replacing them with -100, which is the `ignore_index` of the `CrossEntropyLoss`. In Flax, one can use the `decoder_attention_mask` to ignore padded tokens from the loss (see the [Flax summarization script](https://github.com/huggingface/transformers/tree/main/examples/flax/summarization) for details). We also pass `attention_mask` as additional input to the model, which makes sure that padding tokens of the inputs are ignored. The code example below illustrates all of this. ```python >>> from transformers import T5Tokenizer, T5ForConditionalGeneration >>> import torch >>> tokenizer = T5Tokenizer.from_pretrained("google-t5/t5-small") >>> model = T5ForConditionalGeneration.from_pretrained("google-t5/t5-small") >>> # the following 2 hyperparameters are task-specific >>> max_source_length = 512 >>> max_target_length = 128 >>> # Suppose we have the following 2 training examples: >>> input_sequence_1 = "Welcome to NYC" >>> output_sequence_1 = "Bienvenue ร  NYC" >>> input_sequence_2 = "HuggingFace is a company" >>> output_sequence_2 = "HuggingFace est une entreprise" >>> # encode the inputs >>> task_prefix = "translate English to French: " >>> input_sequences = [input_sequence_1, input_sequence_2] >>> encoding = tokenizer( ... [task_prefix + sequence for sequence in input_sequences], ... padding="longest", ... max_length=max_source_length, ... truncation=True, ... return_tensors="pt", ... ) >>> input_ids, attention_mask = encoding.input_ids, encoding.attention_mask >>> # encode the targets >>> target_encoding = tokenizer( ... [output_sequence_1, output_sequence_2], ... padding="longest", ... max_length=max_target_length, ... truncation=True, ... return_tensors="pt", ... ) >>> labels = target_encoding.input_ids >>> # replace padding token id's of the labels by -100 so it's ignored by the loss >>> labels[labels == tokenizer.pad_token_id] = -100 >>> # forward pass >>> loss = model(input_ids=input_ids, attention_mask=attention_mask, labels=labels).loss >>> loss.item() 0.188 ``` Additional training tips: - T5 models need a slightly higher learning rate than the default one set in the `Trainer` when using the AdamW optimizer. Typically, 1e-4 and 3e-4 work well for most problems (classification, summarization, translation, question answering, question generation). Note that T5 was pre-trained using the AdaFactor optimizer. According to [this forum post](https://discuss.huggingface.co/t/t5-finetuning-tips/684), task prefixes matter when (1) doing multi-task training (2) your task is similar or related to one of the supervised tasks used in T5's pre-training mixture (see Appendix D of the [paper](https://arxiv.org/pdf/1910.10683.pdf) for the task prefixes used). If training on TPU, it is recommended to pad all examples of the dataset to the same length or make use of *pad_to_multiple_of* to have a small number of predefined bucket sizes to fit all examples in. Dynamically padding batches to the longest example is not recommended on TPU as it triggers a recompilation for every batch shape that is encountered during training thus significantly slowing down the training. only padding up to the longest example in a batch) leads to very slow training on TPU. ## Inference At inference time, it is recommended to use [`~generation.GenerationMixin.generate`]. This method takes care of encoding the input and feeding the encoded hidden states via cross-attention layers to the decoder and auto-regressively generates the decoder output. Check out [this blog post](https://huggingface.co/blog/how-to-generate) to know all the details about generating text with Transformers. There's also [this blog post](https://huggingface.co/blog/encoder-decoder#encoder-decoder) which explains how generation works in general in encoder-decoder models. ```python >>> from transformers import T5Tokenizer, T5ForConditionalGeneration >>> tokenizer = T5Tokenizer.from_pretrained("google-t5/t5-small") >>> model = T5ForConditionalGeneration.from_pretrained("google-t5/t5-small") >>> input_ids = tokenizer("translate English to German: The house is wonderful.", return_tensors="pt").input_ids >>> outputs = model.generate(input_ids) >>> print(tokenizer.decode(outputs[0], skip_special_tokens=True)) Das Haus ist wunderbar. ``` Note that T5 uses the `pad_token_id` as the `decoder_start_token_id`, so when doing generation without using [`~generation.GenerationMixin.generate`], make sure you start it with the `pad_token_id`. The example above only shows a single example. You can also do batched inference, like so: ```python >>> from transformers import T5Tokenizer, T5ForConditionalGeneration >>> tokenizer = T5Tokenizer.from_pretrained("google-t5/t5-small") >>> model = T5ForConditionalGeneration.from_pretrained("google-t5/t5-small") >>> task_prefix = "translate English to German: " >>> # use different length sentences to test batching >>> sentences = ["The house is wonderful.", "I like to work in NYC."] >>> inputs = tokenizer([task_prefix + sentence for sentence in sentences], return_tensors="pt", padding=True) >>> output_sequences = model.generate( ... input_ids=inputs["input_ids"], ... attention_mask=inputs["attention_mask"], ... do_sample=False, # disable sampling to test if batching affects output ... ) >>> print(tokenizer.batch_decode(output_sequences, skip_special_tokens=True)) ['Das Haus ist wunderbar.', 'Ich arbeite gerne in NYC.'] ``` Because T5 has been trained with the span-mask denoising objective, it can be used to predict the sentinel (masked-out) tokens during inference. The predicted tokens will then be placed between the sentinel tokens. ```python >>> from transformers import T5Tokenizer, T5ForConditionalGeneration >>> tokenizer = T5Tokenizer.from_pretrained("google-t5/t5-small") >>> model = T5ForConditionalGeneration.from_pretrained("google-t5/t5-small") >>> input_ids = tokenizer("The <extra_id_0> walks in <extra_id_1> park", return_tensors="pt").input_ids >>> sequence_ids = model.generate(input_ids) >>> sequences = tokenizer.batch_decode(sequence_ids) >>> sequences ['<pad> <extra_id_0> park offers <extra_id_1> the <extra_id_2> park.</s>'] ``` ## Performance If you'd like a faster training and inference performance, install [NVIDIA APEX](https://github.com/NVIDIA/apex#quick-start) for NVIDIA GPUs, or [ROCm APEX](https://github.com/ROCmSoftwarePlatform/apex) for AMD GPUs and then the model will automatically use `apex.normalization.FusedRMSNorm` instead of `T5LayerNorm`. The former uses an optimized fused kernel which is several times faster than the latter. ## Resources A list of official Hugging Face and community (indicated by ๐ŸŒŽ) resources to help you get started with T5. If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. <PipelineTag pipeline="text-classification"/> - A notebook for how to [finetune T5 for classification and multiple choice](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb). - A notebook for how to [finetune T5 for sentiment span extraction](https://colab.research.google.com/github/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb). ๐ŸŒŽ <PipelineTag pipeline="token-classification"/> - A notebook for how to [finetune T5 for named entity recognition](https://colab.research.google.com/drive/1obr78FY_cBmWY5ODViCmzdY6O1KB65Vc?usp=sharing). ๐ŸŒŽ <PipelineTag pipeline="text-generation"/> - A notebook for [Finetuning CodeT5 for generating docstrings from Ruby code](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/T5/Fine_tune_CodeT5_for_generating_docstrings_from_Ruby_code.ipynb). <PipelineTag pipeline="summarization"/> - A notebook to [Finetune T5-base-dutch to perform Dutch abstractive summarization on a TPU](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/T5/Fine_tuning_Dutch_T5_base_on_CNN_Daily_Mail_for_summarization_(on_TPU_using_HuggingFace_Accelerate).ipynb). - A notebook for how to [finetune T5 for summarization in PyTorch and track experiments with WandB](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_summarization_wandb.ipynb#scrollTo=OKRpFvYhBauC). ๐ŸŒŽ - A blog post on [Distributed Training: Train BART/T5 for Summarization using ๐Ÿค— Transformers and Amazon SageMaker](https://huggingface.co/blog/sagemaker-distributed-training-seq2seq). - [`T5ForConditionalGeneration`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb). - [`TFT5ForConditionalGeneration`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb). - [`FlaxT5ForConditionalGeneration`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/summarization). - [Summarization](https://huggingface.co/course/chapter7/5?fw=pt#summarization) chapter of the ๐Ÿค— Hugging Face course. - [Summarization task guide](../tasks/summarization) <PipelineTag pipeline="fill-mask"/> - [`FlaxT5ForConditionalGeneration`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#t5-like-span-masked-language-modeling) for training T5 with a span-masked language model objective. The script also shows how to train a T5 tokenizer. [`FlaxT5ForConditionalGeneration`] is also supported by this [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb). <PipelineTag pipeline="translation"/> - [`T5ForConditionalGeneration`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/translation) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb). - [`TFT5ForConditionalGeneration`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/translation) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb). - [Translation task guide](../tasks/translation) <PipelineTag pipeline="question-answering"/> - A notebook on how to [finetune T5 for question answering with TensorFlow 2](https://colab.research.google.com/github/snapthat/TF-T5-text-to-text/blob/master/snapthatT5/notebooks/TF-T5-Datasets%20Training.ipynb). ๐ŸŒŽ - A notebook on how to [finetune T5 for question answering on a TPU](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb#scrollTo=QLGiFCDqvuil). ๐Ÿš€ **Deploy** - A blog post on how to deploy [T5 11B for inference for less than $500](https://www.philschmid.de/deploy-t5-11b). ## T5Config [[autodoc]] T5Config ## T5Tokenizer [[autodoc]] T5Tokenizer - build_inputs_with_special_tokens - get_special_tokens_mask - create_token_type_ids_from_sequences - save_vocabulary ## T5TokenizerFast [[autodoc]] T5TokenizerFast <frameworkcontent> <pt> ## T5Model [[autodoc]] T5Model - forward ## T5ForConditionalGeneration [[autodoc]] T5ForConditionalGeneration - forward ## T5EncoderModel [[autodoc]] T5EncoderModel - forward ## T5ForSequenceClassification [[autodoc]] T5ForSequenceClassification - forward ## T5ForTokenClassification [[autodoc]] T5ForTokenClassification - forward ## T5ForQuestionAnswering [[autodoc]] T5ForQuestionAnswering - forward </pt> <tf> ## TFT5Model [[autodoc]] TFT5Model - call ## TFT5ForConditionalGeneration [[autodoc]] TFT5ForConditionalGeneration - call ## TFT5EncoderModel [[autodoc]] TFT5EncoderModel - call </tf> <jax> ## FlaxT5Model [[autodoc]] FlaxT5Model - __call__ - encode - decode ## FlaxT5ForConditionalGeneration [[autodoc]] FlaxT5ForConditionalGeneration - __call__ - encode - decode ## FlaxT5EncoderModel [[autodoc]] FlaxT5EncoderModel - __call__ </jax> </frameworkcontent>
transformers/docs/source/en/model_doc/t5.md/0
{ "file_path": "transformers/docs/source/en/model_doc/t5.md", "repo_id": "transformers", "token_count": 7103 }
277
<!--Copyright 2021 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # UniSpeech ## Overview The UniSpeech model was proposed in [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://arxiv.org/abs/2101.07597) by Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang . The abstract from the paper is the following: *In this paper, we propose a unified pre-training approach called UniSpeech to learn speech representations with both unlabeled and labeled data, in which supervised phonetic CTC learning and phonetically-aware contrastive self-supervised learning are conducted in a multi-task learning manner. The resultant representations can capture information more correlated with phonetic structures and improve the generalization across languages and domains. We evaluate the effectiveness of UniSpeech for cross-lingual representation learning on public CommonVoice corpus. The results show that UniSpeech outperforms self-supervised pretraining and supervised transfer learning for speech recognition by a maximum of 13.4% and 17.8% relative phone error rate reductions respectively (averaged over all testing languages). The transferability of UniSpeech is also demonstrated on a domain-shift speech recognition task, i.e., a relative word error rate reduction of 6% against the previous approach.* This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The Authors' code can be found [here](https://github.com/microsoft/UniSpeech/tree/main/UniSpeech). ## Usage tips - UniSpeech is a speech model that accepts a float array corresponding to the raw waveform of the speech signal. Please use [`Wav2Vec2Processor`] for the feature extraction. - UniSpeech model can be fine-tuned using connectionist temporal classification (CTC) so the model output has to be decoded using [`Wav2Vec2CTCTokenizer`]. ## Resources - [Audio classification task guide](../tasks/audio_classification) - [Automatic speech recognition task guide](../tasks/asr) ## UniSpeechConfig [[autodoc]] UniSpeechConfig ## UniSpeech specific outputs [[autodoc]] models.unispeech.modeling_unispeech.UniSpeechForPreTrainingOutput ## UniSpeechModel [[autodoc]] UniSpeechModel - forward ## UniSpeechForCTC [[autodoc]] UniSpeechForCTC - forward ## UniSpeechForSequenceClassification [[autodoc]] UniSpeechForSequenceClassification - forward ## UniSpeechForPreTraining [[autodoc]] UniSpeechForPreTraining - forward
transformers/docs/source/en/model_doc/unispeech.md/0
{ "file_path": "transformers/docs/source/en/model_doc/unispeech.md", "repo_id": "transformers", "token_count": 853 }
278
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # XLNet <div class="flex flex-wrap space-x-1"> <a href="https://huggingface.co/models?filter=xlnet"> <img alt="Models" src="https://img.shields.io/badge/All_model_pages-xlnet-blueviolet"> </a> <a href="https://huggingface.co/spaces/docs-demos/xlnet-base-cased"> <img alt="Spaces" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue"> </a> </div> ## Overview The XLNet model was proposed in [XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://arxiv.org/abs/1906.08237) by Zhilin Yang, Zihang Dai, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. XLnet is an extension of the Transformer-XL model pre-trained using an autoregressive method to learn bidirectional contexts by maximizing the expected likelihood over all permutations of the input sequence factorization order. The abstract from the paper is the following: *With the capability of modeling bidirectional contexts, denoising autoencoding based pretraining like BERT achieves better performance than pretraining approaches based on autoregressive language modeling. However, relying on corrupting the input with masks, BERT neglects dependency between the masked positions and suffers from a pretrain-finetune discrepancy. In light of these pros and cons, we propose XLNet, a generalized autoregressive pretraining method that (1) enables learning bidirectional contexts by maximizing the expected likelihood over all permutations of the factorization order and (2) overcomes the limitations of BERT thanks to its autoregressive formulation. Furthermore, XLNet integrates ideas from Transformer-XL, the state-of-the-art autoregressive model, into pretraining. Empirically, under comparable experiment settings, XLNet outperforms BERT on 20 tasks, often by a large margin, including question answering, natural language inference, sentiment analysis, and document ranking.* This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://github.com/zihangdai/xlnet/). ## Usage tips - The specific attention pattern can be controlled at training and test time using the `perm_mask` input. - Due to the difficulty of training a fully auto-regressive model over various factorization order, XLNet is pretrained using only a sub-set of the output tokens as target which are selected with the `target_mapping` input. - To use XLNet for sequential decoding (i.e. not in fully bi-directional setting), use the `perm_mask` and `target_mapping` inputs to control the attention span and outputs (see examples in *examples/pytorch/text-generation/run_generation.py*) - XLNet is one of the few models that has no sequence length limit. - XLNet is not a traditional autoregressive model but uses a training strategy that builds on that. It permutes the tokens in the sentence, then allows the model to use the last n tokens to predict the token n+1. Since this is all done with a mask, the sentence is actually fed in the model in the right order, but instead of masking the first n tokens for n+1, XLNet uses a mask that hides the previous tokens in some given permutation of 1,โ€ฆ,sequence length. - XLNet also uses the same recurrence mechanism as Transformer-XL to build long-term dependencies. ## Resources - [Text classification task guide](../tasks/sequence_classification) - [Token classification task guide](../tasks/token_classification) - [Question answering task guide](../tasks/question_answering) - [Causal language modeling task guide](../tasks/language_modeling) - [Multiple choice task guide](../tasks/multiple_choice) ## XLNetConfig [[autodoc]] XLNetConfig ## XLNetTokenizer [[autodoc]] XLNetTokenizer - build_inputs_with_special_tokens - get_special_tokens_mask - create_token_type_ids_from_sequences - save_vocabulary ## XLNetTokenizerFast [[autodoc]] XLNetTokenizerFast ## XLNet specific outputs [[autodoc]] models.xlnet.modeling_xlnet.XLNetModelOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetLMHeadModelOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForSequenceClassificationOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForMultipleChoiceOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForTokenClassificationOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForQuestionAnsweringSimpleOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForQuestionAnsweringOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetModelOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetLMHeadModelOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForSequenceClassificationOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForMultipleChoiceOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForTokenClassificationOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForQuestionAnsweringSimpleOutput <frameworkcontent> <pt> ## XLNetModel [[autodoc]] XLNetModel - forward ## XLNetLMHeadModel [[autodoc]] XLNetLMHeadModel - forward ## XLNetForSequenceClassification [[autodoc]] XLNetForSequenceClassification - forward ## XLNetForMultipleChoice [[autodoc]] XLNetForMultipleChoice - forward ## XLNetForTokenClassification [[autodoc]] XLNetForTokenClassification - forward ## XLNetForQuestionAnsweringSimple [[autodoc]] XLNetForQuestionAnsweringSimple - forward ## XLNetForQuestionAnswering [[autodoc]] XLNetForQuestionAnswering - forward </pt> <tf> ## TFXLNetModel [[autodoc]] TFXLNetModel - call ## TFXLNetLMHeadModel [[autodoc]] TFXLNetLMHeadModel - call ## TFXLNetForSequenceClassification [[autodoc]] TFXLNetForSequenceClassification - call ## TFLNetForMultipleChoice [[autodoc]] TFXLNetForMultipleChoice - call ## TFXLNetForTokenClassification [[autodoc]] TFXLNetForTokenClassification - call ## TFXLNetForQuestionAnsweringSimple [[autodoc]] TFXLNetForQuestionAnsweringSimple - call </tf> </frameworkcontent>
transformers/docs/source/en/model_doc/xlnet.md/0
{ "file_path": "transformers/docs/source/en/model_doc/xlnet.md", "repo_id": "transformers", "token_count": 2042 }
279
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # GPU inference GPUs are the standard choice of hardware for machine learning, unlike CPUs, because they are optimized for memory bandwidth and parallelism. To keep up with the larger sizes of modern models or to run these large models on existing and older hardware, there are several optimizations you can use to speed up GPU inference. In this guide, you'll learn how to use FlashAttention-2 (a more memory-efficient attention mechanism), BetterTransformer (a PyTorch native fastpath execution), and bitsandbytes to quantize your model to a lower precision. Finally, learn how to use ๐Ÿค— Optimum to accelerate inference with ONNX Runtime on Nvidia and AMD GPUs. <Tip> The majority of the optimizations described here also apply to multi-GPU setups! </Tip> ## FlashAttention-2 <Tip> FlashAttention-2 is experimental and may change considerably in future versions. </Tip> [FlashAttention-2](https://huggingface.co/papers/2205.14135) is a faster and more efficient implementation of the standard attention mechanism that can significantly speedup inference by: 1. additionally parallelizing the attention computation over sequence length 2. partitioning the work between GPU threads to reduce communication and shared memory reads/writes between them FlashAttention-2 is currently supported for the following architectures: * [Bark](https://huggingface.co/docs/transformers/model_doc/bark#transformers.BarkModel) * [Bart](https://huggingface.co/docs/transformers/model_doc/bart#transformers.BartModel) * [Chameleon](https://huggingface.co/docs/transformers/model_doc/chameleon#transformers.Chameleon) * [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPModel) * [Cohere](https://huggingface.co/docs/transformers/model_doc/cohere#transformers.CohereModel) * [Dbrx](https://huggingface.co/docs/transformers/model_doc/dbrx#transformers.DbrxModel) * [DistilBert](https://huggingface.co/docs/transformers/model_doc/distilbert#transformers.DistilBertModel) * [Gemma](https://huggingface.co/docs/transformers/model_doc/gemma#transformers.GemmaModel) * [Gemma2](https://huggingface.co/docs/transformers/model_doc/gemma2#transformers.Gemma2Model) * [GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2) * [GPTBigCode](https://huggingface.co/docs/transformers/model_doc/gpt_bigcode#transformers.GPTBigCodeModel) * [GPTNeo](https://huggingface.co/docs/transformers/model_doc/gpt_neo#transformers.GPTNeoModel) * [GPTNeoX](https://huggingface.co/docs/transformers/model_doc/gpt_neox#transformers.GPTNeoXModel) * [GPT-J](https://huggingface.co/docs/transformers/model_doc/gptj#transformers.GPTJModel) * [Granite](https://huggingface.co/docs/transformers/model_doc/granite#transformers.GraniteModel) * [Idefics2](https://huggingface.co/docs/transformers/model_doc/idefics2#transformers.Idefics2Model) * [Falcon](https://huggingface.co/docs/transformers/model_doc/falcon#transformers.FalconModel) * [JetMoe](https://huggingface.co/docs/transformers/model_doc/jetmoe#transformers.JetMoeModel) * [Jamba](https://huggingface.co/docs/transformers/model_doc/jamba#transformers.JambaModel) * [Llama](https://huggingface.co/docs/transformers/model_doc/llama#transformers.LlamaModel) * [Llava](https://huggingface.co/docs/transformers/model_doc/llava) * [Llava-NeXT](https://huggingface.co/docs/transformers/model_doc/llava_next) * [Llava-NeXT-Video](https://huggingface.co/docs/transformers/model_doc/llava_next_video) * [VipLlava](https://huggingface.co/docs/transformers/model_doc/vipllava) * [VideoLlava](https://huggingface.co/docs/transformers/model_doc/video_llava) * [M2M100](https://huggingface.co/docs/transformers/model_doc/m2m_100) * [MBart](https://huggingface.co/docs/transformers/model_doc/mbart#transformers.MBartModel) * [Mistral](https://huggingface.co/docs/transformers/model_doc/mistral#transformers.MistralModel) * [Mixtral](https://huggingface.co/docs/transformers/model_doc/mixtral#transformers.MixtralModel) * [Musicgen](https://huggingface.co/docs/transformers/model_doc/musicgen#transformers.MusicgenModel) * [MusicGen Melody](https://huggingface.co/docs/transformers/model_doc/musicgen_melody#transformers.MusicgenMelodyModel) * [Nemotron](https://huggingface.co/docs/transformers/model_doc/nemotron) * [NLLB](https://huggingface.co/docs/transformers/model_doc/nllb) * [OLMo](https://huggingface.co/docs/transformers/model_doc/olmo#transformers.OlmoModel) * [OPT](https://huggingface.co/docs/transformers/model_doc/opt#transformers.OPTModel) * [Phi](https://huggingface.co/docs/transformers/model_doc/phi#transformers.PhiModel) * [Phi3](https://huggingface.co/docs/transformers/model_doc/phi3#transformers.Phi3Model) * [StableLm](https://huggingface.co/docs/transformers/model_doc/stablelm#transformers.StableLmModel) * [Starcoder2](https://huggingface.co/docs/transformers/model_doc/starcoder2#transformers.Starcoder2Model) * [Qwen2](https://huggingface.co/docs/transformers/model_doc/qwen2#transformers.Qwen2Model) * [Qwen2Audio](https://huggingface.co/docs/transformers/model_doc/qwen2_audio#transformers.Qwen2AudioEncoder) * [Qwen2MoE](https://huggingface.co/docs/transformers/model_doc/qwen2_moe#transformers.Qwen2MoeModel) * [Qwen2VL](https://huggingface.co/docs/transformers/model_doc/qwen2_vl#transformers.Qwen2VLModel) * [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper#transformers.WhisperModel) * [Wav2Vec2](https://huggingface.co/docs/transformers/model_doc/wav2vec2#transformers.Wav2Vec2Model) * [Hubert](https://huggingface.co/docs/transformers/model_doc/hubert#transformers.HubertModel) * [data2vec_audio](https://huggingface.co/docs/transformers/main/en/model_doc/data2vec#transformers.Data2VecAudioModel) * [Sew](https://huggingface.co/docs/transformers/main/en/model_doc/sew#transformers.SEWModel) * [SigLIP](https://huggingface.co/docs/transformers/model_doc/siglip) * [UniSpeech](https://huggingface.co/docs/transformers/v4.39.3/en/model_doc/unispeech#transformers.UniSpeechModel) * [unispeech_sat](https://huggingface.co/docs/transformers/v4.39.3/en/model_doc/unispeech-sat#transformers.UniSpeechSatModel) You can request to add FlashAttention-2 support for another model by opening a GitHub Issue or Pull Request. Before you begin, make sure you have FlashAttention-2 installed. <hfoptions id="install"> <hfoption id="NVIDIA"> ```bash pip install flash-attn --no-build-isolation ``` We strongly suggest referring to the detailed [installation instructions](https://github.com/Dao-AILab/flash-attention?tab=readme-ov-file#installation-and-features) to learn more about supported hardware and data types! </hfoption> <hfoption id="AMD"> FlashAttention-2 is also supported on AMD GPUs and current support is limited to **Instinct MI210**, **Instinct MI250** and **Instinct MI300**. We strongly suggest using this [Dockerfile](https://github.com/huggingface/optimum-amd/tree/main/docker/transformers-pytorch-amd-gpu-flash/Dockerfile) to use FlashAttention-2 on AMD GPUs. </hfoption> </hfoptions> To enable FlashAttention-2, pass the argument `attn_implementation="flash_attention_2"` to [`~AutoModelForCausalLM.from_pretrained`]: ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM model_id = "tiiuae/falcon-7b" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained( model_id, torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2", ) ``` <Tip> FlashAttention-2 can only be used when the model's dtype is `fp16` or `bf16`. Make sure to cast your model to the appropriate dtype and load them on a supported device before using FlashAttention-2. <br> You can also set `use_flash_attention_2=True` to enable FlashAttention-2 but it is deprecated in favor of `attn_implementation="flash_attention_2"`. </Tip> FlashAttention-2 can be combined with other optimization techniques like quantization to further speedup inference. For example, you can combine FlashAttention-2 with 8-bit or 4-bit quantization: ```py import torch from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM model_id = "tiiuae/falcon-7b" tokenizer = AutoTokenizer.from_pretrained(model_id) # load in 8bit model = AutoModelForCausalLM.from_pretrained( model_id, load_in_8bit=True, attn_implementation="flash_attention_2", ) # load in 4bit model = AutoModelForCausalLM.from_pretrained( model_id, load_in_4bit=True, attn_implementation="flash_attention_2", ) ``` ### Expected speedups You can benefit from considerable speedups for inference, especially for inputs with long sequences. However, since FlashAttention-2 does not support computing attention scores with padding tokens, you must manually pad/unpad the attention scores for batched inference when the sequence contains padding tokens. This leads to a significant slowdown for batched generations with padding tokens. To overcome this, you should use FlashAttention-2 without padding tokens in the sequence during training (by packing a dataset or [concatenating sequences](https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_clm.py#L516) until reaching the maximum sequence length). For a single forward pass on [tiiuae/falcon-7b](https://hf.co/tiiuae/falcon-7b) with a sequence length of 4096 and various batch sizes without padding tokens, the expected speedup is: <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/falcon-7b-inference-large-seqlen.png"> </div> For a single forward pass on [meta-llama/Llama-7b-hf](https://hf.co/meta-llama/Llama-7b-hf) with a sequence length of 4096 and various batch sizes without padding tokens, the expected speedup is: <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/llama-7b-inference-large-seqlen.png"> </div> For sequences with padding tokens (generating with padding tokens), you need to unpad/pad the input sequences to correctly compute the attention scores. With a relatively small sequence length, a single forward pass creates overhead leading to a small speedup (in the example below, 30% of the input is filled with padding tokens): <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/llama-2-small-seqlen-padding.png"> </div> But for larger sequence lengths, you can expect even more speedup benefits: <Tip> FlashAttention is more memory efficient, meaning you can train on much larger sequence lengths without running into out-of-memory issues. You can potentially reduce memory usage up to 20x for larger sequence lengths. Take a look at the [flash-attention](https://github.com/Dao-AILab/flash-attention) repository for more details. </Tip> <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/llama-2-large-seqlen-padding.png"> </div> ## PyTorch scaled dot product attention PyTorch's [`torch.nn.functional.scaled_dot_product_attention`](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html) (SDPA) can also call FlashAttention and memory-efficient attention kernels under the hood. SDPA support is currently being added natively in Transformers and is used by default for `torch>=2.1.1` when an implementation is available. You may also set `attn_implementation="sdpa"` in `from_pretrained()` to explicitly request SDPA to be used. For now, Transformers supports SDPA inference and training for the following architectures: * [Audio Spectrogram Transformer](https://huggingface.co/docs/transformers/model_doc/audio-spectrogram-transformer#transformers.ASTModel) * [Bart](https://huggingface.co/docs/transformers/model_doc/bart#transformers.BartModel) * [Bert](https://huggingface.co/docs/transformers/model_doc/bert#transformers.BertModel) * [CamemBERT](https://huggingface.co/docs/transformers/model_doc/camembert#transformers.CamembertModel) * [Chameleon](https://huggingface.co/docs/transformers/model_doc/chameleon#transformers.Chameleon) * [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPModel) * [Cohere](https://huggingface.co/docs/transformers/model_doc/cohere#transformers.CohereModel) * [data2vec_audio](https://huggingface.co/docs/transformers/main/en/model_doc/data2vec#transformers.Data2VecAudioModel) * [Dbrx](https://huggingface.co/docs/transformers/model_doc/dbrx#transformers.DbrxModel) * [DeiT](https://huggingface.co/docs/transformers/model_doc/deit#transformers.DeiTModel) * [Dpr](https://huggingface.co/docs/transformers/model_doc/dpr#transformers.DprReader) * [Falcon](https://huggingface.co/docs/transformers/model_doc/falcon#transformers.FalconModel) * [Gemma](https://huggingface.co/docs/transformers/model_doc/gemma#transformers.GemmaModel) * [Gemma2](https://huggingface.co/docs/transformers/model_doc/gemma2#transformers.Gemma2Model) * [GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2) * [GPTBigCode](https://huggingface.co/docs/transformers/model_doc/gpt_bigcode#transformers.GPTBigCodeModel) * [GPTNeoX](https://huggingface.co/docs/transformers/model_doc/gpt_neox#transformers.GPTNeoXModel) * [Hubert](https://huggingface.co/docs/transformers/model_doc/hubert#transformers.HubertModel) * [Idefics](https://huggingface.co/docs/transformers/model_doc/idefics#transformers.IdeficsModel) * [Granite](https://huggingface.co/docs/transformers/model_doc/granite#transformers.GraniteModel) * [JetMoe](https://huggingface.co/docs/transformers/model_doc/jetmoe#transformers.JetMoeModel) * [Jamba](https://huggingface.co/docs/transformers/model_doc/jamba#transformers.JambaModel) * [Llama](https://huggingface.co/docs/transformers/model_doc/llama#transformers.LlamaModel) * [Mistral](https://huggingface.co/docs/transformers/model_doc/mistral#transformers.MistralModel) * [Mixtral](https://huggingface.co/docs/transformers/model_doc/mixtral#transformers.MixtralModel) * [Musicgen](https://huggingface.co/docs/transformers/model_doc/musicgen#transformers.MusicgenModel) * [MusicGen Melody](https://huggingface.co/docs/transformers/model_doc/musicgen_melody#transformers.MusicgenMelodyModel) * [OLMo](https://huggingface.co/docs/transformers/model_doc/olmo#transformers.OlmoModel) * [PaliGemma](https://huggingface.co/docs/transformers/model_doc/paligemma#transformers.PaliGemmaForConditionalGeneration) * [Phi](https://huggingface.co/docs/transformers/model_doc/phi#transformers.PhiModel) * [Phi3](https://huggingface.co/docs/transformers/model_doc/phi3#transformers.Phi3Model) * [Idefics](https://huggingface.co/docs/transformers/model_doc/idefics#transformers.IdeficsModel) * [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper#transformers.WhisperModel) * [Mistral](https://huggingface.co/docs/transformers/model_doc/mistral#transformers.MistralModel) * [Mixtral](https://huggingface.co/docs/transformers/model_doc/mixtral#transformers.MixtralModel) * [StableLm](https://huggingface.co/docs/transformers/model_doc/stablelm#transformers.StableLmModel) * [Starcoder2](https://huggingface.co/docs/transformers/model_doc/starcoder2#transformers.Starcoder2Model) * [Qwen2](https://huggingface.co/docs/transformers/model_doc/qwen2#transformers.Qwen2Model) * [Qwen2Audio](https://huggingface.co/docs/transformers/model_doc/qwen2_audio#transformers.Qwen2AudioEncoder) * [Qwen2MoE](https://huggingface.co/docs/transformers/model_doc/qwen2_moe#transformers.Qwen2MoeModel) * [RoBERTa](https://huggingface.co/docs/transformers/model_doc/roberta#transformers.RobertaModel) * [Sew](https://huggingface.co/docs/transformers/main/en/model_doc/sew#transformers.SEWModel) * [SigLIP](https://huggingface.co/docs/transformers/model_doc/siglip) * [StableLm](https://huggingface.co/docs/transformers/model_doc/stablelm#transformers.StableLmModel) * [Starcoder2](https://huggingface.co/docs/transformers/model_doc/starcoder2#transformers.Starcoder2Model) * [UniSpeech](https://huggingface.co/docs/transformers/v4.39.3/en/model_doc/unispeech#transformers.UniSpeechModel) * [unispeech_sat](https://huggingface.co/docs/transformers/v4.39.3/en/model_doc/unispeech-sat#transformers.UniSpeechSatModel) * [RoBERTa](https://huggingface.co/docs/transformers/model_doc/roberta#transformers.RobertaModel) * [Qwen2VL](https://huggingface.co/docs/transformers/model_doc/qwen2_vl#transformers.Qwen2VLModel) * [Musicgen](https://huggingface.co/docs/transformers/model_doc/musicgen#transformers.MusicgenModel) * [MusicGen Melody](https://huggingface.co/docs/transformers/model_doc/musicgen_melody#transformers.MusicgenMelodyModel) * [Nemotron](https://huggingface.co/docs/transformers/model_doc/nemotron) * [ViT](https://huggingface.co/docs/transformers/model_doc/vit#transformers.ViTModel) * [ViTHybrid](https://huggingface.co/docs/transformers/model_doc/vit_hybrid#transformers.ViTHybridModel) * [ViTMAE](https://huggingface.co/docs/transformers/model_doc/vit_mae#transformers.ViTMAEModel) * [ViTMSN](https://huggingface.co/docs/transformers/model_doc/vit_msn#transformers.ViTMSNModel) * [VideoMAE](https://huggingface.co/docs/transformers/model_doc/videomae#transformers.VideoMAEModell) * [wav2vec2](https://huggingface.co/docs/transformers/model_doc/wav2vec2#transformers.Wav2Vec2Model) * [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper#transformers.WhisperModel) * [XLM-RoBERTa](https://huggingface.co/docs/transformers/model_doc/xlm-roberta#transformers.XLMRobertaModel) * [XLM-RoBERTa-XL](https://huggingface.co/docs/transformers/model_doc/xlm-roberta-xl#transformers.XLMRobertaXLModel) * [YOLOS](https://huggingface.co/docs/transformers/model_doc/yolos#transformers.YolosModel) <Tip> FlashAttention can only be used for models with the `fp16` or `bf16` torch type, so make sure to cast your model to the appropriate type first. The memory-efficient attention backend is able to handle `fp32` models. </Tip> <Tip> SDPA does not support certain sets of attention parameters, such as `head_mask` and `output_attentions=True`. In that case, you should see a warning message and we will fall back to the (slower) eager implementation. </Tip> By default, SDPA selects the most performant kernel available but you can check whether a backend is available in a given setting (hardware, problem size) with [`torch.backends.cuda.sdp_kernel`](https://pytorch.org/docs/master/backends.html#torch.backends.cuda.sdp_kernel) as a context manager: ```diff import torch from transformers import AutoModelForCausalLM, AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", torch_dtype=torch.float16).to("cuda") input_text = "Hello my dog is cute and" inputs = tokenizer(input_text, return_tensors="pt").to("cuda") + with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False): outputs = model.generate(**inputs) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` If you see a bug with the traceback below, try using the nightly version of PyTorch which may have broader coverage for FlashAttention: ```bash RuntimeError: No available kernel. Aborting execution. # install PyTorch nightly pip3 install -U --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu118 ``` ## BetterTransformer <Tip warning={true}> Some BetterTransformer features are being upstreamed to Transformers with default support for native `torch.nn.scaled_dot_product_attention`. BetterTransformer still has a wider coverage than the Transformers SDPA integration, but you can expect more and more architectures to natively support SDPA in Transformers. </Tip> <Tip> Check out our benchmarks with BetterTransformer and scaled dot product attention in the [Out of the box acceleration and memory savings of ๐Ÿค— decoder models with PyTorch 2.0](https://pytorch.org/blog/out-of-the-box-acceleration/) and learn more about the fastpath execution in the [BetterTransformer](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2) blog post. </Tip> BetterTransformer accelerates inference with its fastpath (native PyTorch specialized implementation of Transformer functions) execution. The two optimizations in the fastpath execution are: 1. fusion, which combines multiple sequential operations into a single "kernel" to reduce the number of computation steps 2. skipping the inherent sparsity of padding tokens to avoid unnecessary computation with nested tensors BetterTransformer also converts all attention operations to use the more memory-efficient [scaled dot product attention (SDPA)](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention), and it calls optimized kernels like [FlashAttention](https://huggingface.co/papers/2205.14135) under the hood. Before you start, make sure you have ๐Ÿค— Optimum [installed](https://huggingface.co/docs/optimum/installation). Then you can enable BetterTransformer with the [`PreTrainedModel.to_bettertransformer`] method: ```python model = model.to_bettertransformer() ``` You can return the original Transformers model with the [`~PreTrainedModel.reverse_bettertransformer`] method. You should use this before saving your model to use the canonical Transformers modeling: ```py model = model.reverse_bettertransformer() model.save_pretrained("saved_model") ``` ## bitsandbytes bitsandbytes is a quantization library that includes support for 4-bit and 8-bit quantization. Quantization reduces your model size compared to its native full precision version, making it easier to fit large models onto GPUs with limited memory. Make sure you have bitsandbytes and ๐Ÿค— Accelerate installed: ```bash # these versions support 8-bit and 4-bit pip install bitsandbytes>=0.39.0 accelerate>=0.20.0 # install Transformers pip install transformers ``` ### 4-bit To load a model in 4-bit for inference, use the `load_in_4bit` parameter. The `device_map` parameter is optional, but we recommend setting it to `"auto"` to allow ๐Ÿค— Accelerate to automatically and efficiently allocate the model given the available resources in the environment. ```py from transformers import AutoModelForCausalLM model_name = "bigscience/bloom-2b5" model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True) ``` To load a model in 4-bit for inference with multiple GPUs, you can control how much GPU RAM you want to allocate to each GPU. For example, to distribute 600MB of memory to the first GPU and 1GB of memory to the second GPU: ```py max_memory_mapping = {0: "600MB", 1: "1GB"} model_name = "bigscience/bloom-3b" model_4bit = AutoModelForCausalLM.from_pretrained( model_name, device_map="auto", load_in_4bit=True, max_memory=max_memory_mapping ) ``` ### 8-bit <Tip> If you're curious and interested in learning more about the concepts underlying 8-bit quantization, read the [Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using Hugging Face Transformers, Accelerate and bitsandbytes](https://huggingface.co/blog/hf-bitsandbytes-integration) blog post. </Tip> To load a model in 8-bit for inference, use the `load_in_8bit` parameter. The `device_map` parameter is optional, but we recommend setting it to `"auto"` to allow ๐Ÿค— Accelerate to automatically and efficiently allocate the model given the available resources in the environment: ```py from transformers import AutoModelForCausalLM, BitsAndBytesConfig model_name = "bigscience/bloom-2b5" model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) ``` If you're loading a model in 8-bit for text generation, you should use the [`~transformers.GenerationMixin.generate`] method instead of the [`Pipeline`] function which is not optimized for 8-bit models and will be slower. Some sampling strategies, like nucleus sampling, are also not supported by the [`Pipeline`] for 8-bit models. You should also place all inputs on the same device as the model: ```py from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig model_name = "bigscience/bloom-2b5" tokenizer = AutoTokenizer.from_pretrained(model_name) model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) prompt = "Hello, my llama is cute" inputs = tokenizer(prompt, return_tensors="pt").to("cuda") generated_ids = model.generate(**inputs) outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) ``` To load a model in 4-bit for inference with multiple GPUs, you can control how much GPU RAM you want to allocate to each GPU. For example, to distribute 1GB of memory to the first GPU and 2GB of memory to the second GPU: ```py max_memory_mapping = {0: "1GB", 1: "2GB"} model_name = "bigscience/bloom-3b" model_8bit = AutoModelForCausalLM.from_pretrained( model_name, device_map="auto", load_in_8bit=True, max_memory=max_memory_mapping ) ``` <Tip> Feel free to try running a 11 billion parameter [T5 model](https://colab.research.google.com/drive/1YORPWx4okIHXnjW7MSAidXN29mPVNT7F?usp=sharing) or the 3 billion parameter [BLOOM model](https://colab.research.google.com/drive/1qOjXfQIAULfKvZqwCen8-MoWKGdSatZ4?usp=sharing) for inference on Google Colab's free tier GPUs! </Tip> ## ๐Ÿค— Optimum <Tip> Learn more details about using ORT with ๐Ÿค— Optimum in the [Accelerated inference on NVIDIA GPUs](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/gpu#accelerated-inference-on-nvidia-gpus) and [Accelerated inference on AMD GPUs](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/amdgpu#accelerated-inference-on-amd-gpus) guides. This section only provides a brief and simple example. </Tip> ONNX Runtime (ORT) is a model accelerator that supports accelerated inference on Nvidia GPUs, and AMD GPUs that use [ROCm](https://www.amd.com/en/products/software/rocm.html) stack. ORT uses optimization techniques like fusing common operations into a single node and constant folding to reduce the number of computations performed and speedup inference. ORT also places the most computationally intensive operations on the GPU and the rest on the CPU to intelligently distribute the workload between the two devices. ORT is supported by ๐Ÿค— Optimum which can be used in ๐Ÿค— Transformers. You'll need to use an [`~optimum.onnxruntime.ORTModel`] for the task you're solving, and specify the `provider` parameter which can be set to either [`CUDAExecutionProvider`](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/gpu#cudaexecutionprovider), [`ROCMExecutionProvider`](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/amdgpu) or [`TensorrtExecutionProvider`](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/gpu#tensorrtexecutionprovider). If you want to load a model that was not yet exported to ONNX, you can set `export=True` to convert your model on-the-fly to the ONNX format: ```py from optimum.onnxruntime import ORTModelForSequenceClassification ort_model = ORTModelForSequenceClassification.from_pretrained( "distilbert/distilbert-base-uncased-finetuned-sst-2-english", export=True, provider="CUDAExecutionProvider", ) ``` Now you're free to use the model for inference: ```py from optimum.pipelines import pipeline from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased-finetuned-sst-2-english") pipeline = pipeline(task="text-classification", model=ort_model, tokenizer=tokenizer, device="cuda:0") result = pipeline("Both the music and visual were astounding, not to mention the actors performance.") ``` ## Combine optimizations It is often possible to combine several of the optimization techniques described above to get the best inference performance possible for your model. For example, you can load a model in 4-bit, and then enable BetterTransformer with FlashAttention: ```py import torch from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig # load model in 4-bit quantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16 ) tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", quantization_config=quantization_config) # enable BetterTransformer model = model.to_bettertransformer() input_text = "Hello my dog is cute and" inputs = tokenizer(input_text, return_tensors="pt").to("cuda") # enable FlashAttention with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False): outputs = model.generate(**inputs) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ```
transformers/docs/source/en/perf_infer_gpu_one.md/0
{ "file_path": "transformers/docs/source/en/perf_infer_gpu_one.md", "repo_id": "transformers", "token_count": 9727 }
280
<!--Copyright 2024 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # AWQ <Tip> Try AWQ quantization with this [notebook](https://colab.research.google.com/drive/1HzZH89yAXJaZgwJDhQj9LqSBux932BvY)! </Tip> [Activation-aware Weight Quantization (AWQ)](https://hf.co/papers/2306.00978) doesn't quantize all the weights in a model, and instead, it preserves a small percentage of weights that are important for LLM performance. This significantly reduces quantization loss such that you can run models in 4-bit precision without experiencing any performance degradation. There are several libraries for quantizing models with the AWQ algorithm, such as [llm-awq](https://github.com/mit-han-lab/llm-awq), [autoawq](https://github.com/casper-hansen/AutoAWQ) or [optimum-intel](https://huggingface.co/docs/optimum/main/en/intel/optimization_inc). Transformers supports loading models quantized with the llm-awq and autoawq libraries. This guide will show you how to load models quantized with autoawq, but the process is similar for llm-awq quantized models. Make sure you have autoawq installed: ```bash pip install autoawq ``` AWQ-quantized models can be identified by checking the `quantization_config` attribute in the model's [config.json](https://huggingface.co/TheBloke/zephyr-7B-alpha-AWQ/blob/main/config.json) file: ```json { "_name_or_path": "/workspace/process/huggingfaceh4_zephyr-7b-alpha/source", "architectures": [ "MistralForCausalLM" ], ... ... ... "quantization_config": { "quant_method": "awq", "zero_point": true, "group_size": 128, "bits": 4, "version": "gemm" } } ``` A quantized model is loaded with the [`~PreTrainedModel.from_pretrained`] method. If you loaded your model on the CPU, make sure to move it to a GPU device first. Use the `device_map` parameter to specify where to place the model: ```py from transformers import AutoModelForCausalLM, AutoTokenizer model_id = "TheBloke/zephyr-7B-alpha-AWQ" model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda:0") ``` Loading an AWQ-quantized model automatically sets other weights to fp16 by default for performance reasons. If you want to load these other weights in a different format, use the `torch_dtype` parameter: ```py from transformers import AutoModelForCausalLM, AutoTokenizer model_id = "TheBloke/zephyr-7B-alpha-AWQ" model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32) ``` AWQ quantization can also be combined with [FlashAttention-2](../perf_infer_gpu_one#flashattention-2) to further accelerate inference: ```py from transformers import AutoModelForCausalLM, AutoTokenizer model = AutoModelForCausalLM.from_pretrained("TheBloke/zephyr-7B-alpha-AWQ", attn_implementation="flash_attention_2", device_map="cuda:0") ``` ## Fused modules Fused modules offers improved accuracy and performance and it is supported out-of-the-box for AWQ modules for [Llama](https://huggingface.co/meta-llama) and [Mistral](https://huggingface.co/mistralai/Mistral-7B-v0.1) architectures, but you can also fuse AWQ modules for unsupported architectures. <Tip warning={true}> Fused modules cannot be combined with other optimization techniques such as FlashAttention-2. </Tip> <hfoptions id="fuse"> <hfoption id="supported architectures"> To enable fused modules for supported architectures, create an [`AwqConfig`] and set the parameters `fuse_max_seq_len` and `do_fuse=True`. The `fuse_max_seq_len` parameter is the total sequence length and it should include the context length and the expected generation length. You can set it to a larger value to be safe. For example, to fuse the AWQ modules of the [TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ) model. ```python import torch from transformers import AwqConfig, AutoModelForCausalLM model_id = "TheBloke/Mistral-7B-OpenOrca-AWQ" quantization_config = AwqConfig( bits=4, fuse_max_seq_len=512, do_fuse=True, ) model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config).to(0) ``` The [TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ) model was benchmarked with `batch_size=1` with and without fused modules. <figcaption class="text-center text-gray-500 text-lg">Unfused module</figcaption> | Batch Size | Prefill Length | Decode Length | Prefill tokens/s | Decode tokens/s | Memory (VRAM) | |-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| | 1 | 32 | 32 | 60.0984 | 38.4537 | 4.50 GB (5.68%) | | 1 | 64 | 64 | 1333.67 | 31.6604 | 4.50 GB (5.68%) | | 1 | 128 | 128 | 2434.06 | 31.6272 | 4.50 GB (5.68%) | | 1 | 256 | 256 | 3072.26 | 38.1731 | 4.50 GB (5.68%) | | 1 | 512 | 512 | 3184.74 | 31.6819 | 4.59 GB (5.80%) | | 1 | 1024 | 1024 | 3148.18 | 36.8031 | 4.81 GB (6.07%) | | 1 | 2048 | 2048 | 2927.33 | 35.2676 | 5.73 GB (7.23%) | <figcaption class="text-center text-gray-500 text-lg">Fused module</figcaption> | Batch Size | Prefill Length | Decode Length | Prefill tokens/s | Decode tokens/s | Memory (VRAM) | |-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| | 1 | 32 | 32 | 81.4899 | 80.2569 | 4.00 GB (5.05%) | | 1 | 64 | 64 | 1756.1 | 106.26 | 4.00 GB (5.05%) | | 1 | 128 | 128 | 2479.32 | 105.631 | 4.00 GB (5.06%) | | 1 | 256 | 256 | 1813.6 | 85.7485 | 4.01 GB (5.06%) | | 1 | 512 | 512 | 2848.9 | 97.701 | 4.11 GB (5.19%) | | 1 | 1024 | 1024 | 3044.35 | 87.7323 | 4.41 GB (5.57%) | | 1 | 2048 | 2048 | 2715.11 | 89.4709 | 5.57 GB (7.04%) | The speed and throughput of fused and unfused modules were also tested with the [optimum-benchmark](https://github.com/huggingface/optimum-benchmark) library. <div class="flex gap-4"> <div> <img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/quantization/fused_forward_memory_plot.png" alt="generate throughput per batch size" /> <figcaption class="mt-2 text-center text-sm text-gray-500">forward peak memory/batch size</figcaption> </div> <div> <img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/quantization/fused_generate_throughput_plot.png" alt="forward latency per batch size" /> <figcaption class="mt-2 text-center text-sm text-gray-500">generate throughput/batch size</figcaption> </div> </div> </hfoption> <hfoption id="unsupported architectures"> For architectures that don't support fused modules yet, you need to create a custom fusing mapping to define which modules need to be fused with the `modules_to_fuse` parameter. For example, to fuse the AWQ modules of the [TheBloke/Yi-34B-AWQ](https://huggingface.co/TheBloke/Yi-34B-AWQ) model. ```python import torch from transformers import AwqConfig, AutoModelForCausalLM model_id = "TheBloke/Yi-34B-AWQ" quantization_config = AwqConfig( bits=4, fuse_max_seq_len=512, modules_to_fuse={ "attention": ["q_proj", "k_proj", "v_proj", "o_proj"], "layernorm": ["ln1", "ln2", "norm"], "mlp": ["gate_proj", "up_proj", "down_proj"], "use_alibi": False, "num_attention_heads": 56, "num_key_value_heads": 8, "hidden_size": 7168 } ) model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config).to(0) ``` The parameter `modules_to_fuse` should include: - `"attention"`: The names of the attention layers to fuse in the following order: query, key, value and output projection layer. If you don't want to fuse these layers, pass an empty list. - `"layernorm"`: The names of all the LayerNorm layers you want to replace with a custom fused LayerNorm. If you don't want to fuse these layers, pass an empty list. - `"mlp"`: The names of the MLP layers you want to fuse into a single MLP layer in the order: (gate (dense, layer, post-attention) / up / down layers). - `"use_alibi"`: If your model uses ALiBi positional embedding. - `"num_attention_heads"`: The number of attention heads. - `"num_key_value_heads"`: The number of key value heads that should be used to implement Grouped Query Attention (GQA). If `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if `num_key_value_heads=1` the model will use Multi Query Attention (MQA), otherwise GQA is used. - `"hidden_size"`: The dimension of the hidden representations. </hfoption> </hfoptions> ## ExLlama-v2 support Recent versions of `autoawq` supports ExLlama-v2 kernels for faster prefill and decoding. To get started, first install the latest version of `autoawq` by running: ```bash pip install git+https://github.com/casper-hansen/AutoAWQ.git ``` Get started by passing an `AwqConfig()` with `version="exllama"`. ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig quantization_config = AwqConfig(version="exllama") model = AutoModelForCausalLM.from_pretrained( "TheBloke/Mistral-7B-Instruct-v0.1-AWQ", quantization_config=quantization_config, device_map="auto", ) input_ids = torch.randint(0, 100, (1, 128), dtype=torch.long, device="cuda") output = model(input_ids) print(output.logits) tokenizer = AutoTokenizer.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-AWQ") input_ids = tokenizer.encode("How to make a cake", return_tensors="pt").to(model.device) output = model.generate(input_ids, do_sample=True, max_length=50, pad_token_id=50256) print(tokenizer.decode(output[0], skip_special_tokens=True)) ``` <Tip warning={true}> Note this feature is supported on AMD GPUs. </Tip>
transformers/docs/source/en/quantization/awq.md/0
{ "file_path": "transformers/docs/source/en/quantization/awq.md", "repo_id": "transformers", "token_count": 4388 }
281
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Automatic speech recognition [[open-in-colab]] <Youtube id="TksaY_FDgnk"/> Automatic speech recognition (ASR) converts a speech signal to text, mapping a sequence of audio inputs to text outputs. Virtual assistants like Siri and Alexa use ASR models to help users everyday, and there are many other useful user-facing applications like live captioning and note-taking during meetings. This guide will show you how to: 1. Finetune [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) on the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset to transcribe audio to text. 2. Use your finetuned model for inference. <Tip> To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/automatic-speech-recognition) </Tip> Before you begin, make sure you have all the necessary libraries installed: ```bash pip install transformers datasets evaluate jiwer ``` We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: ```py >>> from huggingface_hub import notebook_login >>> notebook_login() ``` ## Load MInDS-14 dataset Start by loading a smaller subset of the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset from the ๐Ÿค— Datasets library. This'll give you a chance to experiment and make sure everything works before spending more time training on the full dataset. ```py >>> from datasets import load_dataset, Audio >>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train[:100]") ``` Split the dataset's `train` split into a train and test set with the [`~Dataset.train_test_split`] method: ```py >>> minds = minds.train_test_split(test_size=0.2) ``` Then take a look at the dataset: ```py >>> minds DatasetDict({ train: Dataset({ features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], num_rows: 16 }) test: Dataset({ features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], num_rows: 4 }) }) ``` While the dataset contains a lot of useful information, like `lang_id` and `english_transcription`, you'll focus on the `audio` and `transcription` in this guide. Remove the other columns with the [`~datasets.Dataset.remove_columns`] method: ```py >>> minds = minds.remove_columns(["english_transcription", "intent_class", "lang_id"]) ``` Take a look at the example again: ```py >>> minds["train"][0] {'audio': {'array': array([-0.00024414, 0. , 0. , ..., 0.00024414, 0.00024414, 0.00024414], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', 'sampling_rate': 8000}, 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} ``` There are two fields: - `audio`: a 1-dimensional `array` of the speech signal that must be called to load and resample the audio file. - `transcription`: the target text. ## Preprocess The next step is to load a Wav2Vec2 processor to process the audio signal: ```py >>> from transformers import AutoProcessor >>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base") ``` The MInDS-14 dataset has a sampling rate of 8000kHz (you can find this information in its [dataset card](https://huggingface.co/datasets/PolyAI/minds14)), which means you'll need to resample the dataset to 16000kHz to use the pretrained Wav2Vec2 model: ```py >>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) >>> minds["train"][0] {'audio': {'array': array([-2.38064706e-04, -1.58618059e-04, -5.43987835e-06, ..., 2.78103951e-04, 2.38446111e-04, 1.18740834e-04], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', 'sampling_rate': 16000}, 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} ``` As you can see in the `transcription` above, the text contains a mix of upper and lowercase characters. The Wav2Vec2 tokenizer is only trained on uppercase characters so you'll need to make sure the text matches the tokenizer's vocabulary: ```py >>> def uppercase(example): ... return {"transcription": example["transcription"].upper()} >>> minds = minds.map(uppercase) ``` Now create a preprocessing function that: 1. Calls the `audio` column to load and resample the audio file. 2. Extracts the `input_values` from the audio file and tokenize the `transcription` column with the processor. ```py >>> def prepare_dataset(batch): ... audio = batch["audio"] ... batch = processor(audio["array"], sampling_rate=audio["sampling_rate"], text=batch["transcription"]) ... batch["input_length"] = len(batch["input_values"][0]) ... return batch ``` To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] function. You can speed up `map` by increasing the number of processes with the `num_proc` parameter. Remove the columns you don't need with the [`~datasets.Dataset.remove_columns`] method: ```py >>> encoded_minds = minds.map(prepare_dataset, remove_columns=minds.column_names["train"], num_proc=4) ``` ๐Ÿค— Transformers doesn't have a data collator for ASR, so you'll need to adapt the [`DataCollatorWithPadding`] to create a batch of examples. It'll also dynamically pad your text and labels to the length of the longest element in its batch (instead of the entire dataset) so they are a uniform length. While it is possible to pad your text in the `tokenizer` function by setting `padding=True`, dynamic padding is more efficient. Unlike other data collators, this specific data collator needs to apply a different padding method to `input_values` and `labels`: ```py >>> import torch >>> from dataclasses import dataclass, field >>> from typing import Any, Dict, List, Optional, Union >>> @dataclass ... class DataCollatorCTCWithPadding: ... processor: AutoProcessor ... padding: Union[bool, str] = "longest" ... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: ... # split inputs and labels since they have to be of different lengths and need ... # different padding methods ... input_features = [{"input_values": feature["input_values"][0]} for feature in features] ... label_features = [{"input_ids": feature["labels"]} for feature in features] ... batch = self.processor.pad(input_features, padding=self.padding, return_tensors="pt") ... labels_batch = self.processor.pad(labels=label_features, padding=self.padding, return_tensors="pt") ... # replace padding with -100 to ignore loss correctly ... labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100) ... batch["labels"] = labels ... return batch ``` Now instantiate your `DataCollatorForCTCWithPadding`: ```py >>> data_collator = DataCollatorCTCWithPadding(processor=processor, padding="longest") ``` ## Evaluate Including a metric during training is often helpful for evaluating your model's performance. You can quickly load a evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [word error rate](https://huggingface.co/spaces/evaluate-metric/wer) (WER) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): ```py >>> import evaluate >>> wer = evaluate.load("wer") ``` Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the WER: ```py >>> import numpy as np >>> def compute_metrics(pred): ... pred_logits = pred.predictions ... pred_ids = np.argmax(pred_logits, axis=-1) ... pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id ... pred_str = processor.batch_decode(pred_ids) ... label_str = processor.batch_decode(pred.label_ids, group_tokens=False) ... wer = wer.compute(predictions=pred_str, references=label_str) ... return {"wer": wer} ``` Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. ## Train <frameworkcontent> <pt> <Tip> If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! </Tip> You're ready to start training your model now! Load Wav2Vec2 with [`AutoModelForCTC`]. Specify the reduction to apply with the `ctc_loss_reduction` parameter. It is often better to use the average instead of the default summation: ```py >>> from transformers import AutoModelForCTC, TrainingArguments, Trainer >>> model = AutoModelForCTC.from_pretrained( ... "facebook/wav2vec2-base", ... ctc_loss_reduction="mean", ... pad_token_id=processor.tokenizer.pad_token_id, ... ) ``` At this point, only three steps remain: 1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the WER and save the training checkpoint. 2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. 3. Call [`~Trainer.train`] to finetune your model. ```py >>> training_args = TrainingArguments( ... output_dir="my_awesome_asr_mind_model", ... per_device_train_batch_size=8, ... gradient_accumulation_steps=2, ... learning_rate=1e-5, ... warmup_steps=500, ... max_steps=2000, ... gradient_checkpointing=True, ... fp16=True, ... group_by_length=True, ... eval_strategy="steps", ... per_device_eval_batch_size=8, ... save_steps=1000, ... eval_steps=1000, ... logging_steps=25, ... load_best_model_at_end=True, ... metric_for_best_model="wer", ... greater_is_better=False, ... push_to_hub=True, ... ) >>> trainer = Trainer( ... model=model, ... args=training_args, ... train_dataset=encoded_minds["train"], ... eval_dataset=encoded_minds["test"], ... tokenizer=processor, ... data_collator=data_collator, ... compute_metrics=compute_metrics, ... ) >>> trainer.train() ``` Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: ```py >>> trainer.push_to_hub() ``` </pt> </frameworkcontent> <Tip> For a more in-depth example of how to finetune a model for automatic speech recognition, take a look at this blog [post](https://huggingface.co/blog/fine-tune-wav2vec2-english) for English ASR and this [post](https://huggingface.co/blog/fine-tune-xlsr-wav2vec2) for multilingual ASR. </Tip> ## Inference Great, now that you've finetuned a model, you can use it for inference! Load an audio file you'd like to run inference on. Remember to resample the sampling rate of the audio file to match the sampling rate of the model if you need to! ```py >>> from datasets import load_dataset, Audio >>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train") >>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) >>> sampling_rate = dataset.features["audio"].sampling_rate >>> audio_file = dataset[0]["audio"]["path"] ``` The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for automatic speech recognition with your model, and pass your audio file to it: ```py >>> from transformers import pipeline >>> transcriber = pipeline("automatic-speech-recognition", model="stevhliu/my_awesome_asr_minds_model") >>> transcriber(audio_file) {'text': 'I WOUD LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'} ``` <Tip> The transcription is decent, but it could be better! Try finetuning your model on more examples to get even better results! </Tip> You can also manually replicate the results of the `pipeline` if you'd like: <frameworkcontent> <pt> Load a processor to preprocess the audio file and transcription and return the `input` as PyTorch tensors: ```py >>> from transformers import AutoProcessor >>> processor = AutoProcessor.from_pretrained("stevhliu/my_awesome_asr_mind_model") >>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") ``` Pass your inputs to the model and return the logits: ```py >>> from transformers import AutoModelForCTC >>> model = AutoModelForCTC.from_pretrained("stevhliu/my_awesome_asr_mind_model") >>> with torch.no_grad(): ... logits = model(**inputs).logits ``` Get the predicted `input_ids` with the highest probability, and use the processor to decode the predicted `input_ids` back into text: ```py >>> import torch >>> predicted_ids = torch.argmax(logits, dim=-1) >>> transcription = processor.batch_decode(predicted_ids) >>> transcription ['I WOUL LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'] ``` </pt> </frameworkcontent>
transformers/docs/source/en/tasks/asr.md/0
{ "file_path": "transformers/docs/source/en/tasks/asr.md", "repo_id": "transformers", "token_count": 4907 }
282
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # LLM prompting guide [[open-in-colab]] Large Language Models such as Falcon, LLaMA, etc. are pretrained transformer models initially trained to predict the next token given some input text. They typically have billions of parameters and have been trained on trillions of tokens for an extended period of time. As a result, these models become quite powerful and versatile, and you can use them to solve multiple NLP tasks out of the box by instructing the models with natural language prompts. Designing such prompts to ensure the optimal output is often called "prompt engineering". Prompt engineering is an iterative process that requires a fair amount of experimentation. Natural languages are much more flexible and expressive than programming languages, however, they can also introduce some ambiguity. At the same time, prompts in natural language are quite sensitive to changes. Even minor modifications in prompts can lead to wildly different outputs. While there is no exact recipe for creating prompts to match all cases, researchers have worked out a number of best practices that help to achieve optimal results more consistently. This guide covers the prompt engineering best practices to help you craft better LLM prompts and solve various NLP tasks. You'll learn: - [Basics of prompting](#basics-of-prompting) - [Best practices of LLM prompting](#best-practices-of-llm-prompting) - [Advanced prompting techniques: few-shot prompting and chain-of-thought](#advanced-prompting-techniques) - [When to fine-tune instead of prompting](#prompting-vs-fine-tuning) <Tip> Prompt engineering is only a part of the LLM output optimization process. Another essential component is choosing the optimal text generation strategy. You can customize how your LLM selects each of the subsequent tokens when generating the text without modifying any of the trainable parameters. By tweaking the text generation parameters, you can reduce repetition in the generated text and make it more coherent and human-sounding. Text generation strategies and parameters are out of scope for this guide, but you can learn more about these topics in the following guides: * [Generation with LLMs](../llm_tutorial) * [Text generation strategies](../generation_strategies) </Tip> ## Basics of prompting ### Types of models The majority of modern LLMs are decoder-only transformers. Some examples include: [LLaMA](../model_doc/llama), [Llama2](../model_doc/llama2), [Falcon](../model_doc/falcon), [GPT2](../model_doc/gpt2). However, you may encounter encoder-decoder transformer LLMs as well, for instance, [Flan-T5](../model_doc/flan-t5) and [BART](../model_doc/bart). Encoder-decoder-style models are typically used in generative tasks where the output **heavily** relies on the input, for example, in translation and summarization. The decoder-only models are used for all other types of generative tasks. When using a pipeline to generate text with an LLM, it's important to know what type of LLM you are using, because they use different pipelines. Run inference with decoder-only models with the `text-generation` pipeline: ```python >>> from transformers import pipeline >>> import torch >>> torch.manual_seed(0) # doctest: +IGNORE_RESULT >>> generator = pipeline('text-generation', model = 'openai-community/gpt2') >>> prompt = "Hello, I'm a language model" >>> generator(prompt, max_length = 30) [{'generated_text': "Hello, I'm a language model programmer so you can use some of my stuff. But you also need some sort of a C program to run."}] ``` To run inference with an encoder-decoder, use the `text2text-generation` pipeline: ```python >>> text2text_generator = pipeline("text2text-generation", model = 'google/flan-t5-base') >>> prompt = "Translate from English to French: I'm very happy to see you" >>> text2text_generator(prompt) [{'generated_text': 'Je suis trรจs heureuse de vous rencontrer.'}] ``` ### Base vs instruct/chat models Most of the recent LLM checkpoints available on ๐Ÿค— Hub come in two versions: base and instruct (or chat). For example, [`tiiuae/falcon-7b`](https://huggingface.co/tiiuae/falcon-7b) and [`tiiuae/falcon-7b-instruct`](https://huggingface.co/tiiuae/falcon-7b-instruct). Base models are excellent at completing the text when given an initial prompt, however, they are not ideal for NLP tasks where they need to follow instructions, or for conversational use. This is where the instruct (chat) versions come in. These checkpoints are the result of further fine-tuning of the pre-trained base versions on instructions and conversational data. This additional fine-tuning makes them a better choice for many NLP tasks. Let's illustrate some simple prompts that you can use with [`tiiuae/falcon-7b-instruct`](https://huggingface.co/tiiuae/falcon-7b-instruct) to solve some common NLP tasks. ### NLP tasks First, let's set up the environment: ```bash pip install -q transformers accelerate ``` Next, let's load the model with the appropriate pipeline (`"text-generation"`): ```python >>> from transformers import pipeline, AutoTokenizer >>> import torch >>> torch.manual_seed(0) # doctest: +IGNORE_RESULT >>> model = "tiiuae/falcon-7b-instruct" >>> tokenizer = AutoTokenizer.from_pretrained(model) >>> pipe = pipeline( ... "text-generation", ... model=model, ... tokenizer=tokenizer, ... torch_dtype=torch.bfloat16, ... device_map="auto", ... ) ``` <Tip> Note that Falcon models were trained using the `bfloat16` datatype, so we recommend you use the same. This requires a recent version of CUDA and works best on modern cards. </Tip> Now that we have the model loaded via the pipeline, let's explore how you can use prompts to solve NLP tasks. #### Text classification One of the most common forms of text classification is sentiment analysis, which assigns a label like "positive", "negative", or "neutral" to a sequence of text. Let's write a prompt that instructs the model to classify a given text (a movie review). We'll start by giving the instruction, and then specifying the text to classify. Note that instead of leaving it at that, we're also adding the beginning of the response - `"Sentiment: "`: ```python >>> torch.manual_seed(0) # doctest: +IGNORE_RESULT >>> prompt = """Classify the text into neutral, negative or positive. ... Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen. ... Sentiment: ... """ >>> sequences = pipe( ... prompt, ... max_new_tokens=10, ... ) >>> for seq in sequences: ... print(f"Result: {seq['generated_text']}") Result: Classify the text into neutral, negative or positive. Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen. Sentiment: Positive ``` As a result, the output contains a classification label from the list we have provided in the instructions, and it is a correct one! <Tip> You may notice that in addition to the prompt, we pass a `max_new_tokens` parameter. It controls the number of tokens the model shall generate, and it is one of the many text generation parameters that you can learn about in [Text generation strategies](../generation_strategies) guide. </Tip> #### Named Entity Recognition Named Entity Recognition (NER) is a task of finding named entities in a piece of text, such as a person, location, or organization. Let's modify the instructions in the prompt to make the LLM perform this task. Here, let's also set `return_full_text = False` so that output doesn't contain the prompt: ```python >>> torch.manual_seed(1) # doctest: +IGNORE_RESULT >>> prompt = """Return a list of named entities in the text. ... Text: The Golden State Warriors are an American professional basketball team based in San Francisco. ... Named entities: ... """ >>> sequences = pipe( ... prompt, ... max_new_tokens=15, ... return_full_text = False, ... ) >>> for seq in sequences: ... print(f"{seq['generated_text']}") - Golden State Warriors - San Francisco ``` As you can see, the model correctly identified two named entities from the given text. #### Translation Another task LLMs can perform is translation. You can choose to use encoder-decoder models for this task, however, here, for the simplicity of the examples, we'll keep using Falcon-7b-instruct, which does a decent job. Once again, here's how you can write a basic prompt to instruct a model to translate a piece of text from English to Italian: ```python >>> torch.manual_seed(2) # doctest: +IGNORE_RESULT >>> prompt = """Translate the English text to Italian. ... Text: Sometimes, I've believed as many as six impossible things before breakfast. ... Translation: ... """ >>> sequences = pipe( ... prompt, ... max_new_tokens=20, ... do_sample=True, ... top_k=10, ... return_full_text = False, ... ) >>> for seq in sequences: ... print(f"{seq['generated_text']}") A volte, ho creduto a sei impossibili cose prima di colazione. ``` Here we've added a `do_sample=True` and `top_k=10` to allow the model to be a bit more flexible when generating output. #### Text summarization Similar to the translation, text summarization is another generative task where the output **heavily** relies on the input, and encoder-decoder models can be a better choice. However, decoder-style models can be used for this task as well. Previously, we have placed the instructions at the very beginning of the prompt. However, the very end of the prompt can also be a suitable location for instructions. Typically, it's better to place the instruction on one of the extreme ends. ```python >>> torch.manual_seed(3) # doctest: +IGNORE_RESULT >>> prompt = """Permaculture is a design process mimicking the diversity, functionality and resilience of natural ecosystems. The principles and practices are drawn from traditional ecological knowledge of indigenous cultures combined with modern scientific understanding and technological innovations. Permaculture design provides a framework helping individuals and communities develop innovative, creative and effective strategies for meeting basic needs while preparing for and mitigating the projected impacts of climate change. ... Write a summary of the above text. ... Summary: ... """ >>> sequences = pipe( ... prompt, ... max_new_tokens=30, ... do_sample=True, ... top_k=10, ... return_full_text = False, ... ) >>> for seq in sequences: ... print(f"{seq['generated_text']}") Permaculture is an ecological design mimicking natural ecosystems to meet basic needs and prepare for climate change. It is based on traditional knowledge and scientific understanding. ``` #### Question answering For question answering task we can structure the prompt into the following logical components: instructions, context, question, and the leading word or phrase (`"Answer:"`) to nudge the model to start generating the answer: ```python >>> torch.manual_seed(4) # doctest: +IGNORE_RESULT >>> prompt = """Answer the question using the context below. ... Context: Gazpacho is a cold soup and drink made of raw, blended vegetables. Most gazpacho includes stale bread, tomato, cucumbers, onion, bell peppers, garlic, olive oil, wine vinegar, water, and salt. Northern recipes often include cumin and/or pimentรณn (smoked sweet paprika). Traditionally, gazpacho was made by pounding the vegetables in a mortar with a pestle; this more laborious method is still sometimes used as it helps keep the gazpacho cool and avoids the foam and silky consistency of smoothie versions made in blenders or food processors. ... Question: What modern tool is used to make gazpacho? ... Answer: ... """ >>> sequences = pipe( ... prompt, ... max_new_tokens=10, ... do_sample=True, ... top_k=10, ... return_full_text = False, ... ) >>> for seq in sequences: ... print(f"Result: {seq['generated_text']}") Result: Modern tools often used to make gazpacho include ``` #### Reasoning Reasoning is one of the most difficult tasks for LLMs, and achieving good results often requires applying advanced prompting techniques, like [Chain-of-though](#chain-of-thought). Let's try if we can make a model reason about a simple arithmetics task with a basic prompt: ```python >>> torch.manual_seed(5) # doctest: +IGNORE_RESULT >>> prompt = """There are 5 groups of students in the class. Each group has 4 students. How many students are there in the class?""" >>> sequences = pipe( ... prompt, ... max_new_tokens=30, ... do_sample=True, ... top_k=10, ... return_full_text = False, ... ) >>> for seq in sequences: ... print(f"Result: {seq['generated_text']}") Result: There are a total of 5 groups, so there are 5 x 4=20 students in the class. ``` Correct! Let's increase the complexity a little and see if we can still get away with a basic prompt: ```python >>> torch.manual_seed(6) # doctest: +IGNORE_RESULT >>> prompt = """I baked 15 muffins. I ate 2 muffins and gave 5 muffins to a neighbor. My partner then bought 6 more muffins and ate 2. How many muffins do we now have?""" >>> sequences = pipe( ... prompt, ... max_new_tokens=10, ... do_sample=True, ... top_k=10, ... return_full_text = False, ... ) >>> for seq in sequences: ... print(f"Result: {seq['generated_text']}") Result: The total number of muffins now is 21 ``` This is a wrong answer, it should be 12. In this case, this can be due to the prompt being too basic, or due to the choice of model, after all we've picked the smallest version of Falcon. Reasoning is difficult for models of all sizes, but larger models are likely to perform better. ## Best practices of LLM prompting In this section of the guide we have compiled a list of best practices that tend to improve the prompt results: * When choosing the model to work with, the latest and most capable models are likely to perform better. * Start with a simple and short prompt, and iterate from there. * Put the instructions at the beginning of the prompt, or at the very end. When working with large context, models apply various optimizations to prevent Attention complexity from scaling quadratically. This may make a model more attentive to the beginning or end of a prompt than the middle. * Clearly separate instructions from the text they apply to - more on this in the next section. * Be specific and descriptive about the task and the desired outcome - its format, length, style, language, etc. * Avoid ambiguous descriptions and instructions. * Favor instructions that say "what to do" instead of those that say "what not to do". * "Lead" the output in the right direction by writing the first word (or even begin the first sentence for the model). * Use advanced techniques like [Few-shot prompting](#few-shot-prompting) and [Chain-of-thought](#chain-of-thought) * Test your prompts with different models to assess their robustness. * Version and track the performance of your prompts. ## Advanced prompting techniques ### Few-shot prompting The basic prompts in the sections above are the examples of "zero-shot" prompts, meaning, the model has been given instructions and context, but no examples with solutions. LLMs that have been fine-tuned on instruction datasets, generally perform well on such "zero-shot" tasks. However, you may find that your task has more complexity or nuance, and, perhaps, you have some requirements for the output that the model doesn't catch on just from the instructions. In this case, you can try the technique called few-shot prompting. In few-shot prompting, we provide examples in the prompt giving the model more context to improve the performance. The examples condition the model to generate the output following the patterns in the examples. Here's an example: ```python >>> torch.manual_seed(0) # doctest: +IGNORE_RESULT >>> prompt = """Text: The first human went into space and orbited the Earth on April 12, 1961. ... Date: 04/12/1961 ... Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. ... Date:""" >>> sequences = pipe( ... prompt, ... max_new_tokens=8, ... do_sample=True, ... top_k=10, ... ) >>> for seq in sequences: ... print(f"Result: {seq['generated_text']}") Result: Text: The first human went into space and orbited the Earth on April 12, 1961. Date: 04/12/1961 Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. Date: 09/28/1960 ``` In the above code snippet we used a single example to demonstrate the desired output to the model, so this can be called a "one-shot" prompting. However, depending on the task complexity you may need to use more than one example. Limitations of the few-shot prompting technique: - While LLMs can pick up on the patterns in the examples, these technique doesn't work well on complex reasoning tasks - Few-shot prompting requires creating lengthy prompts. Prompts with large number of tokens can increase computation and latency. There's also a limit to the length of the prompts. - Sometimes when given a number of examples, models can learn patterns that you didn't intend them to learn, e.g. that the third movie review is always negative. ### Chain-of-thought Chain-of-thought (CoT) prompting is a technique that nudges a model to produce intermediate reasoning steps thus improving the results on complex reasoning tasks. There are two ways of steering a model to producing the reasoning steps: - few-shot prompting by illustrating examples with detailed answers to questions, showing the model how to work through a problem. - by instructing the model to reason by adding phrases like "Let's think step by step" or "Take a deep breath and work through the problem step by step." If we apply the CoT technique to the muffins example from the [reasoning section](#reasoning) and use a larger model, such as (`tiiuae/falcon-180B-chat`) which you can play with in the [HuggingChat](https://huggingface.co/chat/), we'll get a significant improvement on the reasoning result: ```text Let's go through this step-by-step: 1. You start with 15 muffins. 2. You eat 2 muffins, leaving you with 13 muffins. 3. You give 5 muffins to your neighbor, leaving you with 8 muffins. 4. Your partner buys 6 more muffins, bringing the total number of muffins to 14. 5. Your partner eats 2 muffins, leaving you with 12 muffins. Therefore, you now have 12 muffins. ``` ## Prompting vs fine-tuning You can achieve great results by optimizing your prompts, however, you may still ponder whether fine-tuning a model would work better for your case. Here are some scenarios when fine-tuning a smaller model may be a preferred option: - Your domain is wildly different from what LLMs were pre-trained on and extensive prompt optimization did not yield sufficient results. - You need your model to work well in a low-resource language. - You need the model to be trained on sensitive data that is under strict regulations. - You have to use a small model due to cost, privacy, infrastructure or other limitations. In all of the above examples, you will need to make sure that you either already have or can easily obtain a large enough domain-specific dataset at a reasonable cost to fine-tune a model. You will also need to have enough time and resources to fine-tune a model. If the above examples are not the case for you, optimizing prompts can prove to be more beneficial.
transformers/docs/source/en/tasks/prompting.md/0
{ "file_path": "transformers/docs/source/en/tasks/prompting.md", "repo_id": "transformers", "token_count": 5574 }
283
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Export to TFLite [TensorFlow Lite](https://www.tensorflow.org/lite/guide) is a lightweight framework for deploying machine learning models on resource-constrained devices, such as mobile phones, embedded systems, and Internet of Things (IoT) devices. TFLite is designed to optimize and run models efficiently on these devices with limited computational power, memory, and power consumption. A TensorFlow Lite model is represented in a special efficient portable format identified by the `.tflite` file extension. ๐Ÿค— Optimum offers functionality to export ๐Ÿค— Transformers models to TFLite through the `exporters.tflite` module. For the list of supported model architectures, please refer to [๐Ÿค— Optimum documentation](https://huggingface.co/docs/optimum/exporters/tflite/overview). To export a model to TFLite, install the required dependencies: ```bash pip install optimum[exporters-tf] ``` To check out all available arguments, refer to the [๐Ÿค— Optimum docs](https://huggingface.co/docs/optimum/main/en/exporters/tflite/usage_guides/export_a_model), or view help in command line: ```bash optimum-cli export tflite --help ``` To export a model's checkpoint from the ๐Ÿค— Hub, for example, `google-bert/bert-base-uncased`, run the following command: ```bash optimum-cli export tflite --model google-bert/bert-base-uncased --sequence_length 128 bert_tflite/ ``` You should see the logs indicating progress and showing where the resulting `model.tflite` is saved, like this: ```bash Validating TFLite model... -[โœ“] TFLite model output names match reference model (logits) - Validating TFLite Model output "logits": -[โœ“] (1, 128, 30522) matches (1, 128, 30522) -[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05) The TensorFlow Lite export succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the set tolerance 1e-05: - logits: max diff = 5.817413330078125e-05. The exported model was saved at: bert_tflite ``` The example above illustrates exporting a checkpoint from ๐Ÿค— Hub. When exporting a local model, first make sure that you saved both the model's weights and tokenizer files in the same directory (`local_path`). When using CLI, pass the `local_path` to the `model` argument instead of the checkpoint name on ๐Ÿค— Hub.
transformers/docs/source/en/tflite.md/0
{ "file_path": "transformers/docs/source/en/tflite.md", "repo_id": "transformers", "token_count": 878 }
284
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Crea una arquitectura personalizada Una [`AutoClass`](model_doc/auto) infiere, automรกticamente, la arquitectura del modelo y descarga la configuraciรณn y los pesos del modelo preentrenado. Normalmente, recomendamos usar una `AutoClass` para producir un cรณdigo agnรณstico a puntos de guardado o checkpoints. Sin embargo, los usuarios que quieran mรกs control sobre los parรกmetros especรญficos de los modelos pueden crear su propio modelo ๐Ÿค— Transformers personalizado a partir de varias clases base. Esto puede ser particularmente รบtil para alguien que estรฉ interesado en estudiar, entrenar o experimentar con modelos ๐Ÿค— Transformers. En esta guรญa vamos a profundizar en la creaciรณn de modelos personalizados sin usar `AutoClass`. Aprenderemos a: - Cargar y personalizar una configuraciรณn para un modelo. - Crear una arquitectura para un modelo. - Crear tokenizadores rรกpidos y lentos para textos. - Crear un extractor de propiedades para tareas de audio o imรกgenes. - Crear un procesador para tareas multimodales. ## Configuraciรณn Una [configuraciรณn](main_classes/configuration) es un conjunto de atributos especรญficos de un modelo. Cada configuraciรณn de modelo tiene atributos diferentes. Por ejemplo, todos los modelos de PLN tienen los atributos `hidden_size`, `num_attention_heads`, `num_hidden_layers` y `vocab_size` en comรบn. Estos atributos especifican el nรบmero de cabezas de atenciรณn o de capas ocultas con las que se construyen los modelos. Puedes echarle un vistazo a [DistilBERT](model_doc/distilbert) y sus atributos accediendo a [`DistilBertConfig`]: ```py >>> from transformers import DistilBertConfig >>> config = DistilBertConfig() >>> print(config) DistilBertConfig { "activation": "gelu", "attention_dropout": 0.1, "dim": 768, "dropout": 0.1, "hidden_dim": 3072, "initializer_range": 0.02, "max_position_embeddings": 512, "model_type": "distilbert", "n_heads": 12, "n_layers": 6, "pad_token_id": 0, "qa_dropout": 0.1, "seq_classif_dropout": 0.2, "sinusoidal_pos_embds": false, "transformers_version": "4.16.2", "vocab_size": 30522 } ``` [`DistilBertConfig`] muestra todos los atributos por defecto que se han usado para construir un modelo [`DistilBertModel`] base. Todos ellos son personalizables, lo que deja espacio para poder experimentar. Por ejemplo, puedes personalizar un modelo predeterminado para: - Probar una funciรณn de activaciรณn diferente, usando el parรกmetro `activation`. - Usar un valor de abandono (tambiรฉn conocido como _dropout_) mรกs alto para las probabilidades de las capas de atenciรณn, usando el parรกmetro `attention_dropout`. ```py >>> my_config = DistilBertConfig(activation="relu", attention_dropout=0.4) >>> print(my_config) DistilBertConfig { "activation": "relu", "attention_dropout": 0.4, "dim": 768, "dropout": 0.1, "hidden_dim": 3072, "initializer_range": 0.02, "max_position_embeddings": 512, "model_type": "distilbert", "n_heads": 12, "n_layers": 6, "pad_token_id": 0, "qa_dropout": 0.1, "seq_classif_dropout": 0.2, "sinusoidal_pos_embds": false, "transformers_version": "4.16.2", "vocab_size": 30522 } ``` Los atributos de los modelos preentrenados pueden ser modificados con la funciรณn [`~PretrainedConfig.from_pretrained`]: ```py >>> my_config = DistilBertConfig.from_pretrained("distilbert/distilbert-base-uncased", activation="relu", attention_dropout=0.4) ``` Cuando estรฉs satisfecho con la configuraciรณn de tu modelo, puedes guardarlo con la funciรณn [`~PretrainedConfig.save_pretrained`]. Tu configuraciรณn se guardarรก en un archivo JSON dentro del directorio que le especifiques como parรกmetro. ```py >>> my_config.save_pretrained(save_directory="./your_model_save_path") ``` Para volver a usar el archivo de configuraciรณn, puedes cargarlo usando [`~PretrainedConfig.from_pretrained`]: ```py >>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") ``` <Tip> Tambiรฉn puedes guardar los archivos de configuraciรณn como un diccionario; o incluso guardar solo la diferencia entre tu archivo personalizado y la configuraciรณn por defecto. Consulta la [documentaciรณn sobre configuraciรณn](main_classes/configuration) para ver mรกs detalles. </Tip> ## Modelo El siguiente paso serรก crear un [modelo](main_classes/models). El modelo, al que a veces tambiรฉn nos referimos como arquitectura, es el encargado de definir cada capa y quรฉ operaciones se realizan. Los atributos como `num_hidden_layers` de la configuraciรณn se usan para definir la arquitectura. Todos los modelos comparten una clase base, [`PreTrainedModel`], y algunos mรฉtodos comunes que se pueden usar para redimensionar los _embeddings_ o para recortar cabezas de auto-atenciรณn (tambiรฉn llamadas _self-attention heads_). Ademรกs, todos los modelos son subclases de [`torch.nn.Module`](https://pytorch.org/docs/stable/generated/torch.nn.Module.html), [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) o [`flax.linen.Module`](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html), lo que significa que son compatibles con su respectivo framework. <frameworkcontent> <pt> Carga los atributos de tu configuraciรณn personalizada en el modelo de la siguiente forma: ```py >>> from transformers import DistilBertModel >>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") >>> model = DistilBertModel(my_config) ``` Esto crea un modelo con valores aleatorios, en lugar de crearlo con los pesos del preentrenamiento, por lo que no serรกs capaz de usar este modelo para nada รบtil hasta que no lo entrenes. El entrenamiento es un proceso costoso, tanto en cuestiรณn de recursos como de tiempo, por lo que generalmente es mejor usar un modelo preentrenado para obtener mejores resultados mรกs rรกpido, consumiendo una fracciรณn de los recursos que un entrenamiento completo hubiera requerido. Puedes crear un modelo preentrenado con [`~PreTrainedModel.from_pretrained`]: ```py >>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") ``` Cuando cargues tus pesos del preentrenamiento, el modelo por defecto se carga automรกticamente si nos lo proporciona ๐Ÿค— Transformers. Sin embargo, siempre puedes reemplazar (todos o algunos de) los atributos del modelo por defecto por los tuyos: ```py >>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) ``` </pt> <tf> Carga los atributos de tu configuraciรณn personalizada en el modelo de la siguiente forma: ```py >>> from transformers import TFDistilBertModel >>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") >>> tf_model = TFDistilBertModel(my_config) ``` Esto crea un modelo con valores aleatorios, en lugar de crearlo con los pesos del preentrenamiento, por lo que no serรกs capaz de usar este modelo para nada รบtil hasta que no lo entrenes. El entrenamiento es un proceso costoso, tanto en cuestiรณn de recursos como de tiempo, por lo que generalmente es mejor usar un modelo preentrenado para obtener mejores resultados mรกs rรกpido, consumiendo solo una fracciรณn de los recursos que un entrenamiento completo hubiera requerido. Puedes crear un modelo preentrenado con [`~TFPreTrainedModel.from_pretrained`]: ```py >>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") ``` Cuando cargues tus pesos del preentrenamiento, el modelo por defecto se carga automรกticamente si este nos lo proporciona ๐Ÿค— Transformers. Sin embargo, siempre puedes reemplazar (todos o algunos de) los atributos del modelo por defecto por los tuyos: ```py >>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) ``` </tf> </frameworkcontent> ### Cabezas de modelo En este punto del tutorial, tenemos un modelo DistilBERT base que devuelve los *hidden states* o estados ocultos. Los *hidden states* se pasan como parรกmetros de entrada a la cabeza del modelo para producir la salida. ๐Ÿค— Transformers ofrece una cabeza de modelo diferente para cada tarea, siempre y cuando el modelo sea compatible para la tarea (por ejemplo, no puedes usar DistilBERT para una tarea secuencia a secuencia como la traducciรณn). <frameworkcontent> <pt> Por ejemplo, [`DistilBertForSequenceClassification`] es un modelo DistilBERT base con una cabeza de clasificaciรณn de secuencias. La cabeza de clasificaciรณn de secuencias es una capa superior que precede a la recolecciรณn de las salidas. ```py >>> from transformers import DistilBertForSequenceClassification >>> model = DistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") ``` Puedes reutilizar este punto de guardado o *checkpoint* para otra tarea fรกcilmente cambiando a una cabeza de un modelo diferente. Para una tarea de respuesta a preguntas, puedes usar la cabeza del modelo [`DistilBertForQuestionAnswering`]. La cabeza de respuesta a preguntas es similar a la de clasificaciรณn de secuencias, excepto porque consta de una capa lineal delante de la salida de los *hidden states*. ```py >>> from transformers import DistilBertForQuestionAnswering >>> model = DistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") ``` </pt> <tf> Por ejemplo, [`TFDistilBertForSequenceClassification`] es un modelo DistilBERT base con una cabeza de clasificaciรณn de secuencias. La cabeza de clasificaciรณn de secuencias es una capa superior que precede a la recolecciรณn de las salidas. ```py >>> from transformers import TFDistilBertForSequenceClassification >>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") ``` Puedes reutilizar este punto de guardado o *checkpoint* para otra tarea fรกcilmente cambiando a una cabeza de un modelo diferente. Para una tarea de respuesta a preguntas, puedes usar la cabeza del modelo [`TFDistilBertForQuestionAnswering`]. La cabeza de respuesta a preguntas es similar a la de clasificaciรณn de secuencias, excepto porque consta de una capa lineal delante de la salida de los *hidden states*. ```py >>> from transformers import TFDistilBertForQuestionAnswering >>> tf_model = TFDistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") ``` </tf> </frameworkcontent> ## Tokenizer La ultima clase base que debes conocer antes de usar un modelo con datos textuales es la clase [tokenizer](main_classes/tokenizer), que convierte el texto bruto en tensores. Hay dos tipos de *tokenizers* que puedes usar con ๐Ÿค— Transformers: - [`PreTrainedTokenizer`]: una implementaciรณn de un *tokenizer* hecha en Python. - [`PreTrainedTokenizerFast`]: un *tokenizer* de nuestra librerรญa [๐Ÿค— Tokenizer](https://huggingface.co/docs/tokenizers/python/latest/), basada en Rust. Este tipo de *tokenizer* es bastante mรกs rรกpido, especialmente durante la tokenizaciรณn por lotes, gracias a estar implementado en Rust. Esta rรกpida tokenizaciรณn tambiรฉn ofrece mรฉtodos adicionales como el *offset mapping*, que relaciona los tokens con sus palabras o caracteres originales. Ambos *tokenizers* son compatibles con los mรฉtodos comunes, como los de encodificaciรณn y decodificaciรณn, los mรฉtodos para aรฑadir tokens y aquellos que manejan tokens especiales. <Tip warning={true}> No todos los modelos son compatibles con un *tokenizer* rรกpido. ร‰chale un vistazo a esta [tabla](index#supported-frameworks) para comprobar si un modelo especรญfico es compatible con un *tokenizer* rรกpido. </Tip> Si has entrenado tu propio *tokenizer*, puedes crear uno desde tu archivo de โ€œvocabularioโ€: ```py >>> from transformers import DistilBertTokenizer >>> my_tokenizer = DistilBertTokenizer(vocab_file="my_vocab_file.txt", do_lower_case=False, padding_side="left") ``` Es importante recordar que los vocabularios que provienen de un *tokenizer* personalizado serรกn diferentes a los vocabularios generados por el *tokenizer* de un modelo preentrenado. Debes usar el vocabulario de un *tokenizer* preentrenado si vas a usar un modelo preentrenado, de lo contrario las entradas no tendrรกn sentido. Crea un *tokenizer* con el vocabulario de un modelo preentrenado usando la clase [`DistilBertTokenizer`]: ```py >>> from transformers import DistilBertTokenizer >>> slow_tokenizer = DistilBertTokenizer.from_pretrained("distilbert/distilbert-base-uncased") ``` Crea un *tokenizer* rรกpido con la clase [`DistilBertTokenizerFast`]: ```py >>> from transformers import DistilBertTokenizerFast >>> fast_tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert/distilbert-base-uncased") ``` <Tip> Por defecto, el [`AutoTokenizer`] intentarรก cargar un *tokenizer* rรกpido. Puedes desactivar este comportamiento cambiando el parรกmetro `use_fast=False` de `from_pretrained`. </Tip> ## Extractor de Caracterรญsticas Un extractor de caracterรญsticas procesa entradas de audio e imagen. Hereda de la clase base [`~feature_extraction_utils.FeatureExtractionMixin`] y tambiรฉn puede heredar de la clase [`ImageFeatureExtractionMixin`] para el procesamiento de caracterรญsticas de las imรกgenes o de la clase [`SequenceFeatureExtractor`] para el procesamiento de entradas de audio. Dependiendo de si trabajas en una tarea de audio o de video, puedes crear un extractor de caracterรญsticas asociado al modelo que estรฉs usando. Por ejemplo, podrรญas crear un [`ViTFeatureExtractor`] por defecto si estรกs usando [ViT](model_doc/vit) para clasificaciรณn de imรกgenes: ```py >>> from transformers import ViTFeatureExtractor >>> vit_extractor = ViTFeatureExtractor() >>> print(vit_extractor) ViTFeatureExtractor { "do_normalize": true, "do_resize": true, "feature_extractor_type": "ViTFeatureExtractor", "image_mean": [ 0.5, 0.5, 0.5 ], "image_std": [ 0.5, 0.5, 0.5 ], "resample": 2, "size": 224 } ``` <Tip> Si no estรกs buscando ninguna personalizaciรณn en especรญfico, usa el mรฉtodo `from_pretrained` para cargar los parรกmetros del extractor de caracterรญsticas por defecto del modelo. </Tip> Puedes modificar cualquier parรกmetro de [`ViTFeatureExtractor`] para crear tu extractor de caracterรญsticas personalizado: ```py >>> from transformers import ViTFeatureExtractor >>> my_vit_extractor = ViTFeatureExtractor(resample="PIL.Image.BOX", do_normalize=False, image_mean=[0.3, 0.3, 0.3]) >>> print(my_vit_extractor) ViTFeatureExtractor { "do_normalize": false, "do_resize": true, "feature_extractor_type": "ViTFeatureExtractor", "image_mean": [ 0.3, 0.3, 0.3 ], "image_std": [ 0.5, 0.5, 0.5 ], "resample": "PIL.Image.BOX", "size": 224 } ``` Para las entradas de audio, puedes crear un [`Wav2Vec2FeatureExtractor`] y personalizar los parรกmetros de una forma similar: ```py >>> from transformers import Wav2Vec2FeatureExtractor >>> w2v2_extractor = Wav2Vec2FeatureExtractor() >>> print(w2v2_extractor) Wav2Vec2FeatureExtractor { "do_normalize": true, "feature_extractor_type": "Wav2Vec2FeatureExtractor", "feature_size": 1, "padding_side": "right", "padding_value": 0.0, "return_attention_mask": false, "sampling_rate": 16000 } ``` ## Procesador Para modelos que son compatibles con tareas multimodales, ๐Ÿค— Transformers ofrece una clase *procesador* que agrupa un extractor de caracterรญsticas y un *tokenizer* en el mismo objeto. Por ejemplo, probemos a usar el procesador [`Wav2Vec2Processor`] para una tarea de reconocimiento de voz (ASR). Un ASR transcribe el audio a texto, por lo que necesitaremos un extractor de caracterรญsticas y un *tokenizer*. Crea un extractor de caracterรญsticas para manejar la entrada de audio: ```py >>> from transformers import Wav2Vec2FeatureExtractor >>> feature_extractor = Wav2Vec2FeatureExtractor(padding_value=1.0, do_normalize=True) ``` Crea un *tokenizer* para manejar la entrada de texto: ```py >>> from transformers import Wav2Vec2CTCTokenizer >>> tokenizer = Wav2Vec2CTCTokenizer(vocab_file="my_vocab_file.txt") ``` Puedes combinar el extractor de caracterรญsticas y el *tokenizer* en el [`Wav2Vec2Processor`]: ```py >>> from transformers import Wav2Vec2Processor >>> processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer) ``` Con dos clases base (la configuraciรณn y el modelo) y una clase de preprocesamiento adicional (*tokenizer*, extractor de caracterรญsticas o procesador), puedes crear cualquiera de los modelos compatibles con ๐Ÿค— Transformers. Cada una de estas clases son configurables, permitiรฉndote usar sus atributos especรญficos. Puedes crear un modelo para entrenarlo de una forma fรกcil, o modificar un modelo preentrenado disponible para especializarlo.
transformers/docs/source/es/create_a_model.md/0
{ "file_path": "transformers/docs/source/es/create_a_model.md", "repo_id": "transformers", "token_count": 6229 }
285
<!--- Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Verificaciones en un Pull Request Cuando abres un _pull request_ en ๐Ÿค— Transformers, se ejecutarรกn una serie de verificaciones para asegurarte de que el _patch_ que estรกs agregando no rompa nada existente. Estas verificaciones son de cuatro tipos: - pruebas regulares - creaciรณn de la documentaciรณn - estilo del cรณdigo y documentaciรณn - consistencia del repositorio En este documento, intentaremos explicar cuรกles son esas diferentes verificaciones y el motivo detrรกs de ellas, asรญ como tambiรฉn cรณmo depurarlas localmente si una falla en tu PR. Recuerda que todas las verificaciones requieren que tengas una instalaciรณn de desarrollo: ```bash pip install transformers[dev] ``` o una instalaciรณn editable: ```bash pip install -e .[dev] ``` del repositorio de Transformers. ## Pruebas Todos los procesos que comienzan con `ci/circleci: run_tests_` ejecutan partes del conjunto de pruebas de Transformers. Cada uno de esos procesos se enfoca en una parte de la biblioteca en un entorno determinado: por ejemplo, `ci/circleci: run_tests_pipelines_tf` ejecuta la prueba de _pipelines_ en un entorno donde solo estรก instalado TensorFlow. Ten en cuenta que para evitar ejecutar pruebas cuando no hay un cambio real en los mรณdulos que estรกs probando, solo se ejecuta una parte del conjunto de pruebas: se ejecuta una tarea auxiliar para determinar las diferencias en la biblioteca antes y despuรฉs del PR (lo que GitHub te muestra en la pestaรฑa "Files changes") y selecciona las pruebas afectadas por esa diferencia. Este auxiliar se puede ejecutar localmente usando: ```bash python utils/tests_fetcher.py ``` desde el directorio raiz del repositorio de Transformers. Se ejecutarรก lo siguiente: 1. Verificaciรณn para cada archivo en el _diff_ si los cambios estรกn en el cรณdigo, solo en comentarios o _docstrings_. Solo los archivos con cambios reales de cรณdigo se conservan. 2. Creaciรณn de un mapa interno que proporciona para cada archivo del cรณdigo fuente de la biblioteca todos los archivos a los que impacta recursivamente. Se dice que el mรณdulo A impacta al mรณdulo B si el mรณdulo B importa el mรณdulo A. Para el impacto recursivo, necesitamos una cadena de mรณdulos que va del mรณdulo A al mรณdulo B en la que cada mรณdulo importa el anterior. 3. Aplicaciรณn de este mapa en los archivos recopilados en el paso 1, lo que nos da una lista de archivos modelo afectados por el PR. 4. Asignaciรณn de cada uno de esos archivos a sus archivos de prueba correspondientes y para obtener una la lista de pruebas a ejecutar. Al ejecutar el _script_ localmente, debes obtener los resultados de los pasos 1, 3 y 4 impresos y asรญ saber quรฉ pruebas se ejecutarรกn. El _script_ tambiรฉn crearรก un archivo llamado `test_list.txt` que contiene la lista de pruebas para ejecutar, y puede ejecutarlas localmente con el siguiente comando: ```bash python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt) ``` En caso de que se te escape algo, el conjunto completo de pruebas tambiรฉn se ejecuta a diario. ## Creaciรณn de la documentaciรณn El proceso `build_pr_documentation` compila y genera una vista previa de la documentaciรณn para asegurarse de que todo se vea bien una vez que se fusione tu PR. Un bot agregarรก un enlace para obtener una vista previa de la documentaciรณn en tu PR. Cualquier cambio que realices en el PR se actualiza automรกticamente en la vista previa. Si la documentaciรณn no se genera, haz clic en **Detalles** junto al proceso fallido para ver dรณnde saliรณ mal. A menudo, el error es tan simple como que falta un archivo en `toctree`. Si estรกs interesado en compilar u obtener una vista previa de la documentaciรณn localmente, echa un vistazo al [`README.md`](https://github.com/huggingface/transformers/tree/main/docs) en la carpeta `docs`. ## Estilo de cรณdigo y documentaciรณn. El formato de cรณdigo se aplica a todos los archivos fuente, los ejemplos y las pruebas utilizando `black` e `ruff`. Tambiรฉn tenemos una herramienta personalizada que se ocupa del formato de los _docstrings_ y archivos `rst` (`utils/style_doc.py`), asรญ como del orden de las importaciones _lazy_ realizadas en los archivos `__init__.py` de Transformers (`utils /custom_init_isort.py`). Todo esto se puede probar ejecutando ```bash make style ``` CI verifica que se hayan aplicado dentro de la verificaciรณn `ci/circleci: check_code_quality`. Tambiรฉn se ejecuta `ruff`, que harรก una verificaciรณn bรกsica a tu cรณdigo y te harรก saber si encuentra una variable no definida, o una que no se usa. Para ejecutar esa verificaciรณn localmente, usa ```bash make quality ``` Esto puede llevar mucho tiempo, asรญ que para ejecutar lo mismo solo en los archivos que modificaste en la rama actual, ejecuta ```bash make fixup ``` Este รบltimo comando tambiรฉn ejecutarรก todas las verificaciones adicionales para la consistencia del repositorio. Echemos un vistazo a estas pruebas. ## Consistencia del repositorio Esta verificaciรณn reagrupa todas las pruebas para asegurarse de que tu PR deja el repositorio en buen estado, y se realiza mediante `ci/circleci: check_repository_consistency`. Puedes ejecutar localmente esta verificaciรณn ejecutando lo siguiente: ```bash make repo-consistency ``` Esta instrucciรณn verifica que: - Todos los objetos agregados al _init_ estรกn documentados (realizados por `utils/check_repo.py`) - Todos los archivos `__init__.py` tienen el mismo contenido en sus dos secciones (realizado por `utils/check_inits.py`) - Todo el cรณdigo identificado como una copia de otro mรณdulo es consistente con el original (realizado por `utils/check_copies.py`) - Todas las clases de configuraciรณn tienen al menos _checkpoint_ vรกlido mencionado en sus _docstrings_ (realizado por `utils/check_config_docstrings.py`) - Las traducciones de los README y el รญndice del documento tienen la misma lista de modelos que el README principal (realizado por `utils/check_copies.py`) - Las tablas generadas automaticamente en la documentaciรณn estรกn actualizadas (realizadas por `utils/check_table.py`) - La biblioteca tiene todos los objetos disponibles incluso si no estรกn instaladas todas las dependencias opcionales (realizadas por `utils/check_dummies.py`) Si esta verificaciรณn falla, los primeros dos elementos requieren una reparaciรณn manual, los รบltimos cuatro pueden repararse automรกticamente ejecutando el comando ```bash make fix-copies ``` Las verificaciones adicionales se refieren a los PRs que agregan nuevos modelos, principalmente que: - Todos los modelos agregados estรกn en un Auto-mapping (realizado por `utils/check_repo.py`) <!-- TODO Sylvain, add a check that makes sure the common tests are implemented.--> - Todos los modelos se verifican correctamente (realizados por `utils/check_repo.py`) <!-- TODO Sylvain, add the following - All models are added to the main README, inside the main doc - All checkpoints used actually exist on the Hub -->
transformers/docs/source/es/pr_checks.md/0
{ "file_path": "transformers/docs/source/es/pr_checks.md", "repo_id": "transformers", "token_count": 2659 }
286
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Allenamento distribuito con ๐Ÿค— Accelerate La parallelizzazione รจ emersa come strategia per allenare modelli sempre piรน grandi su hardware limitato e accelerarne la velocitร  di allenamento di diversi ordini di magnitudine. In Hugging Face, abbiamo creato la libreria [๐Ÿค— Accelerate](https://huggingface.co/docs/accelerate) per aiutarti ad allenare in modo semplice un modello ๐Ÿค— Transformers su qualsiasi tipo di configurazione distribuita, sia che si tratti di piรน GPU su una sola macchina o di piรน GPU su piรน macchine. In questo tutorial, imparerai come personalizzare il training loop nativo di PyTorch per consentire l'addestramento in un ambiente distribuito. ## Configurazione Inizia installando ๐Ÿค— Accelerate: ```bash pip install accelerate ``` Poi importa e crea un oggetto [`Accelerator`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator). `Accelerator` rileverร  automaticamente il tuo setup distribuito e inizializzerร  tutte le componenti necessarie per l'allenamento. Non dovrai allocare esplicitamente il tuo modello su un device. ```py >>> from accelerate import Accelerator >>> accelerator = Accelerator() ``` ## Preparati ad accelerare Il prossimo passo รจ quello di passare tutti gli oggetti rilevanti per l'allenamento al metodo [`prepare`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator.prepare). Questo include i tuoi DataLoaders per l'allenamento e per la valutazione, un modello e un ottimizzatore: ```py >>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( ... train_dataloader, eval_dataloader, model, optimizer ... ) ``` ## Backward Infine, sostituisci il tipico metodo `loss.backward()` nel tuo loop di allenamento con il metodo [`backward`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator.backward) di ๐Ÿค— Accelerate: ```py >>> for epoch in range(num_epochs): ... for batch in train_dataloader: ... outputs = model(**batch) ... loss = outputs.loss ... accelerator.backward(loss) ... optimizer.step() ... lr_scheduler.step() ... optimizer.zero_grad() ... progress_bar.update(1) ``` Come puoi vedere nel seguente codice, hai solo bisogno di aggiungere quattro righe in piรน di codice al tuo training loop per abilitare l'allenamento distribuito! ```diff + from accelerate import Accelerator from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) - device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") - model.to(device) + train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( + train_dataloader, eval_dataloader, model, optimizer + ) num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: - batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss - loss.backward() + accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) ``` ## Allenamento Una volta che hai aggiunto le righe di codice rilevanti, lancia il tuo allenamento in uno script o in un notebook come Colaboratory. ### Allenamento con uno script Se stai eseguendo il tuo allenamento da uno script, esegui il comando seguente per creare e salvare un file di configurazione: ```bash accelerate config ``` Poi lancia il tuo allenamento con: ```bash accelerate launch train.py ``` ### Allenamento con un notebook La libreria ๐Ÿค— Accelerate puรฒ anche essere utilizzata in un notebook se stai pianificando di utilizzare le TPU di Colaboratory. Inserisci tutto il codice legato all'allenamento in una funzione, e passala al `notebook_launcher`: ```py >>> from accelerate import notebook_launcher >>> notebook_launcher(training_function) ``` Per maggiori informazioni relative a ๐Ÿค— Accelerate e le sue numerose funzionalitร , fai riferimento alla [documentazione](https://huggingface.co/docs/accelerate).
transformers/docs/source/it/accelerate.md/0
{ "file_path": "transformers/docs/source/it/accelerate.md", "repo_id": "transformers", "token_count": 1891 }
287
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Inferenza Efficiente su CPU Questa guida si concentra sull'inferenza di modelli di grandi dimensioni in modo efficiente sulla CPU. ## `BetterTransformer` per inferenza piรน rapida Abbiamo integrato di recente `BetterTransformer` per fare inferenza piรน rapidamente con modelli per testi, immagini e audio. Visualizza la documentazione sull'integrazione [qui](https://huggingface.co/docs/optimum/bettertransformer/overview) per maggiori dettagli. ## PyTorch JIT-mode (TorchScript) TorchScript รจ un modo di creare modelli serializzabili e ottimizzabili da codice PyTorch. Ogni programmma TorchScript puรฒ esere salvato da un processo Python e caricato in un processo dove non ci sono dipendenze Python. Comparandolo con l'eager mode di default, jit mode in PyTorch normalmente fornisce prestazioni migliori per l'inferenza del modello da parte di metodologie di ottimizzazione come la operator fusion. Per una prima introduzione a TorchScript, vedi la Introduction to [PyTorch TorchScript tutorial](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html#tracing-modules). ### IPEX Graph Optimization con JIT-mode Intelยฎ Extension per PyTorch fornnisce ulteriori ottimizzazioni in jit mode per i modelli della serie Transformers. Consigliamo vivamente agli utenti di usufruire dei vantaggi di Intelยฎ Extension per PyTorch con jit mode. Alcuni operator patterns usati fequentemente dai modelli Transformers models sono giร  supportati in Intelยฎ Extension per PyTorch con jit mode fusions. Questi fusion patterns come Multi-head-attention fusion, Concat Linear, Linear+Add, Linear+Gelu, Add+LayerNorm fusion and etc. sono abilitati e hanno buone performance. I benefici della fusion รจ fornito agli utenti in modo trasparente. In base alle analisi, il ~70% dei problemi piรน popolari in NLP question-answering, text-classification, and token-classification possono avere benefici sulle performance grazie ai fusion patterns sia per Float32 precision che per BFloat16 Mixed precision. Vedi maggiori informazioni per [IPEX Graph Optimization](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/graph_optimization.html). #### Installazione di IPEX I rilasci di IPEX seguono PyTorch, verifica i vari approcci per [IPEX installation](https://intel.github.io/intel-extension-for-pytorch/). ### Utilizzo del JIT-mode Per abilitare JIT-mode in Trainer per evaluation e prediction, devi aggiungere `jit_mode_eval` negli argomenti di Trainer. <Tip warning={true}> per PyTorch >= 1.14.0. JIT-mode potrebe giovare a qualsiasi modello di prediction e evaluaion visto che il dict input รจ supportato in jit.trace per PyTorch < 1.14.0. JIT-mode potrebbe giovare ai modelli il cui ordine dei parametri corrisponde all'ordine delle tuple in ingresso in jit.trace, come i modelli per question-answering. Nel caso in cui l'ordine dei parametri seguenti non corrisponda all'ordine delle tuple in ingresso in jit.trace, come nei modelli di text-classification, jit.trace fallirร  e lo cattureremo con una eccezione al fine di renderlo un fallback. Il logging รจ usato per notificare gli utenti. </Tip> Trovi un esempo con caso d'uso in [Transformers question-answering](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) - Inference using jit mode on CPU: <pre>python run_qa.py \ --model_name_or_path csarron/bert-base-uncased-squad-v1 \ --dataset_name squad \ --do_eval \ --max_seq_length 384 \ --doc_stride 128 \ --output_dir /tmp/ \ --no_cuda \ <b>--jit_mode_eval </b></pre> - Inference with IPEX using jit mode on CPU: <pre>python run_qa.py \ --model_name_or_path csarron/bert-base-uncased-squad-v1 \ --dataset_name squad \ --do_eval \ --max_seq_length 384 \ --doc_stride 128 \ --output_dir /tmp/ \ --no_cuda \ <b>--use_ipex \</b> <b>--jit_mode_eval</b></pre>
transformers/docs/source/it/perf_infer_cpu.md/0
{ "file_path": "transformers/docs/source/it/perf_infer_cpu.md", "repo_id": "transformers", "token_count": 1497 }
288
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # ๐Ÿค— Accelerate ใ‚’็”จใ„ใŸๅˆ†ๆ•ฃๅญฆ็ฟ’ ใƒขใƒ‡ใƒซใŒๅคงใใใชใ‚‹ใซใคใ‚Œใฆใ€้™ใ‚‰ใ‚ŒใŸใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใ‚’่จ“็ทดใ—ใ€่จ“็ทด้€Ÿๅบฆใ‚’ๅคงๅน…ใซไธŠๆ˜‡ใ•ใ›ใ‚‹ใŸใ‚ใฎๆ–นๆณ•ใจใ—ใฆไธฆๅˆ—ๅ‡ฆ็†ใŒๆตฎไธŠใ—ใฆใใพใ—ใŸใ€‚1ๅฐใฎใƒžใ‚ทใƒณใซ่ค‡ๆ•ฐใฎGPUใŒใ‚ใฃใฆใ‚‚ใ€่ค‡ๆ•ฐใฎใƒžใ‚ทใƒณใซใพใŸใŒใ‚‹่ค‡ๆ•ฐใฎGPUใŒใ‚ใฃใฆใ‚‚ใ€ใ‚ใ‚‰ใ‚†ใ‚‹ใ‚ฟใ‚คใƒ—ใฎๅˆ†ๆ•ฃๅ‡ฆ็†ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ไธŠใงใƒฆใƒผใ‚ถใƒผใŒ็ฐกๅ˜ใซ ๐Ÿค— Transformers ใƒขใƒ‡ใƒซใ‚’่จ“็ทดใงใใ‚‹ใ‚ˆใ†ใซใ€ Hugging Face ใงใฏ [๐Ÿค— Accelerate](https://huggingface.co/docs/accelerate) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝœๆˆใ—ใพใ—ใŸใ€‚ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€PyTorch ใฎ่จ“็ทดใƒซใƒผใƒ—ใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ—ใฆใ€ๅˆ†ๆ•ฃๅ‡ฆ็†็’ฐๅขƒใงใฎ่จ“็ทดใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆๅญฆใณใพใ™ใ€‚ ## ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ— ใฏใ˜ใ‚ใซ ๐Ÿค— Accelerate ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ—ใ‚‡ใ†: ```bash pip install accelerate ``` ใใ—ใŸใ‚‰ใ‚คใƒณใƒใƒผใƒˆใ—ใฆ [`~accelerate.Accelerator`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ†ใ€‚[`~accelerate.Accelerator`] ใฏๅˆ†ๆ•ฃๅ‡ฆ็†ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’่‡ชๅ‹•็š„ใซๆคœๅ‡บใ—ใ€่จ“็ทดใฎใŸใ‚ใซๅฟ…่ฆใชๅ…จใฆใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚’ๅˆๆœŸๅŒ–ใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ใƒ‡ใƒใ‚คใ‚นใซๆ˜Ž็คบ็š„ใซ้…็ฝฎใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ```py >>> from accelerate import Accelerator >>> accelerator = Accelerator() ``` ## Accelerate ใ™ใ‚‹ๆบ–ๅ‚™ใ‚’ใ—ใพใ—ใ‚‡ใ† ๆฌกใซใ€้–ข้€ฃใ™ใ‚‹ๅ…จใฆใฎ่จ“็ทดใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ [`~accelerate.Accelerator.prepare`] ใƒกใ‚ฝใƒƒใƒ‰ใซๆธกใ—ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€่จ“็ทดใจ่ฉ•ไพกใใ‚Œใžใ‚ŒใฎDataloaderใ€ใƒขใƒ‡ใƒซใ€optimizer ใŒๅซใพใ‚Œใพใ™: ```py >>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( ... train_dataloader, eval_dataloader, model, optimizer ... ) ``` ## Backward ๆœ€ๅพŒใซ่จ“็ทดใƒซใƒผใƒ—ๅ†…ใฎ `loss.backward()` ใ‚’ ๐Ÿค— Accelerate ใฎ [`~accelerate.Accelerator.backward`] ใƒกใ‚ฝใƒƒใƒ‰ใง็ฝฎใๆ›ใˆใพใ™๏ผš ```py >>> for epoch in range(num_epochs): ... for batch in train_dataloader: ... outputs = model(**batch) ... loss = outputs.loss ... accelerator.backward(loss) ... optimizer.step() ... lr_scheduler.step() ... optimizer.zero_grad() ... progress_bar.update(1) ``` ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใง็ขบ่ชใงใใ‚‹้€šใ‚Šใ€่จ“็ทดใƒซใƒผใƒ—ใซ4่กŒใฎใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงๅˆ†ๆ•ฃๅญฆ็ฟ’ใŒๅฏ่ƒฝใงใ™๏ผ ```diff + from accelerate import Accelerator from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) - device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") - model.to(device) + train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( + train_dataloader, eval_dataloader, model, optimizer + ) num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: - batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss - loss.backward() + accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) ``` ## ่จ“็ทดใ™ใ‚‹ ้–ข้€ฃใ™ใ‚‹ใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ—ใŸใ‚‰ใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใพใŸใฏ Colaboratory ใชใฉใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใง่จ“็ทดใ‚’้–‹ๅง‹ใ—ใพใ™ใ€‚ ### ใ‚นใ‚ฏใƒชใƒ—ใƒˆใง่จ“็ทดใ™ใ‚‹ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‹ใ‚‰่จ“็ทดใ‚’ใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใƒปไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใซไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„: ```bash accelerate config ``` ใใ—ใฆๆฌกใฎใ‚ˆใ†ใซใ—ใฆ่จ“็ทดใ‚’้–‹ๅง‹ใ—ใพใ™: ```bash accelerate launch train.py ``` ### ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใง่จ“็ทดใ™ใ‚‹ Colaboratory ใฎ TPU ใฎๅˆฉ็”จใ‚’ใŠ่€ƒใˆใฎๅ ดๅˆใ€๐Ÿค— Accelerate ใฏใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏไธŠใงๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚่จ“็ทดใซๅฟ…่ฆใชๅ…จใฆใฎใ‚ณใƒผใƒ‰ใ‚’้–ขๆ•ฐใซๅซใ‚ใ€[`~accelerate.notebook_launcher`] ใซๆธกใ—ใฆใใ ใ•ใ„: ```py >>> from accelerate import notebook_launcher >>> notebook_launcher(training_function) ``` ๐Ÿค— Accelerate ใจ่ฑŠๅฏŒใชๆฉŸ่ƒฝใซใคใ„ใฆใ‚‚ใฃใจ็Ÿฅใ‚ŠใŸใ„ๆ–นใฏ[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/accelerate)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚
transformers/docs/source/ja/accelerate.md/0
{ "file_path": "transformers/docs/source/ja/accelerate.md", "repo_id": "transformers", "token_count": 2185 }
289
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # DeepSpeed Integration [DeepSpeed](https://github.com/microsoft/DeepSpeed) ใฏใ€[ZeRO ่ซ–ๆ–‡](https://arxiv.org/abs/1910.02054) ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ™ในใฆใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚็พๅœจใ€ๆฌกใฎใ‚‚ใฎใ‚’ๅฎŒๅ…จใซใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ 1. ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ๅˆ†ๅ‰ฒ (ZeRO ใ‚นใƒ†ใƒผใ‚ธ 1) 2. ๅ‹พ้…ๅˆ†ๅ‰ฒ (ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2) 3. ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎๅˆ†ๅ‰ฒ (ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3) 4. ใ‚ซใ‚นใ‚ฟใƒ ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅ‡ฆ็† 5. ไธ€้€ฃใฎ้ซ˜้€Ÿ CUDA ๆ‹กๅผตใƒ™ใƒผใ‚นใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ 6. CPU ใŠใ‚ˆใณ NVMe ใธใฎ ZeRO ใ‚ชใƒ•ใƒญใƒผใƒ‰ ZeRO-Offload ใซใฏ็‹ฌ่‡ชใฎๅฐ‚็”จใƒšใƒผใƒ‘ใƒผใŒใ‚ใ‚Šใพใ™: [ZeRO-Offload: Democratizing Billion-Scale Model Training](https://arxiv.org/abs/2101.06840)ใ€‚ NVMe ใ‚ตใƒใƒผใƒˆใซใคใ„ใฆใฏใ€่ซ–ๆ–‡ [ZeRO-Infinity: Breaking the GPU Memory Wall for Extreme Scale Deep Learning](https://arxiv.org/abs/2104.07857)ใ€‚ DeepSpeed ZeRO-2 ใฏใ€ใใฎๆฉŸ่ƒฝใŒๆŽจ่ซ–ใซใฏๅฝนใซ็ซ‹ใŸใชใ„ใŸใ‚ใ€ไธปใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใฟใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ DeepSpeed ZeRO-3 ใฏใ€ๅทจๅคงใชใƒขใƒ‡ใƒซใ‚’่ค‡ๆ•ฐใฎ GPU ใซใƒญใƒผใƒ‰ใงใใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ ๅ˜ไธ€ใฎ GPU ใงใฏไธๅฏ่ƒฝใงใ™ใ€‚ ๐Ÿค— Transformers ใฏใ€2 ใคใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ไป‹ใ—ใฆ [DeepSpeed](https://github.com/microsoft/DeepSpeed) ใ‚’็ตฑๅˆใ—ใพใ™ใ€‚ 1. [`Trainer`] ใซใ‚ˆใ‚‹ใ‚ณใ‚ข DeepSpeed ๆฉŸ่ƒฝใฎ็ตฑๅˆใ€‚ไฝ•ใงใ‚‚ใ‚„ใฃใฆใใ‚Œใ‚‹ใ‚ฟใ‚คใƒ—ใงใ™ ็ตฑๅˆใฎๅ ดๅˆ - ใ‚ซใ‚นใ‚ฟใƒ ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ‹ใ€ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใ ใ‘ใงใ€ไป–ใซไฝ•ใ‚‚ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใŸใ„ใฆใ„ใฎ ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ“ใฎๆฉŸ่ƒฝใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใพใ™ใ€‚ 2. [`Trainer`] ใ‚’ไฝฟ็”จใ›ใšใ€DeepSpeed ใ‚’็ตฑๅˆใ—ใŸ็‹ฌ่‡ชใฎใƒˆใƒฌใƒผใƒŠใƒผใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆ `from_pretrained` ใ‚„ `from_config` ใชใฉใฎใ‚ณใ‚ขๆฉŸ่ƒฝใซใฏใ€้‡่ฆใชๆฉŸ่ƒฝใฎ็ตฑๅˆใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ไปฅ้™ใฎ `zero.Init`ใชใฉใฎ DeepSpeed ใฎ้ƒจๅˆ†ใ€‚ใ“ใฎๆฉŸ่ƒฝใ‚’ๆดป็”จใ™ใ‚‹ใซใฏใ€ๆฌกใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ใŠ่ชญใฟใใ ใ•ใ„ใ€‚ [้žใƒˆใƒฌใƒผใƒŠใƒผ DeepSpeed ็ตฑๅˆ](#nontrainer-deepspeed-integration)ใ€‚ ็ตฑๅˆใ•ใ‚Œใฆใ„ใ‚‹ใ‚‚ใฎ: ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ๏ผš 1. DeepSpeed ZeRO ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€ZeRO-Infinity (CPU ใŠใ‚ˆใณ NVME ใ‚ชใƒ•ใƒญใƒผใƒ‰) ใ‚’ไฝฟ็”จใ—ใฆๅฎŒๅ…จใช ZeRO ใ‚นใƒ†ใƒผใ‚ธ 1ใ€2ใ€ใŠใ‚ˆใณ 3 ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ ๆŽจ่ซ–๏ผš 1. DeepSpeed ZeRO Inference ใฏใ€ZeRO-Infinity ใซใ‚ˆใ‚‹ ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๅŒใ˜ ZeRO ใƒ—ใƒญใƒˆใ‚ณใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใจ lr ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใฏไฝฟ็”จใ›ใšใ€ใ‚นใƒ†ใƒผใ‚ธ 3 ใฎใฟใŒ้–ข้€ฃใ—ใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€ไปฅไธ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ [ใ‚ผใƒญๆŽจ่ซ–](#zero-inference)ใ€‚ DeepSpeed Inference ใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€Tensor Parallelism ใฎไปฃใ‚ใ‚Šใซ Tensor Parallelism ใ‚’ไฝฟ็”จใ™ใ‚‹ใพใฃใŸใ็•ฐใชใ‚‹ใƒ†ใ‚ฏใƒŽใƒญใ‚ธใƒผใงใ™ใ€‚ ZeRO (่ฟ‘ๆ—ฅๅ…ฌ้–‹)ใ€‚ <a id='deepspeed-trainer-integration'></a> ## Trainer Deepspeed Integration <a id='deepspeed-installation'></a> ### Installation pypi ็ตŒ็”ฑใงใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚ ```bash pip install deepspeed ``` ใพใŸใฏ`tansformers`, `extras`็ตŒ็”ฑ: ```bash pip install transformers[deepspeed] ``` ใพใŸใฏใ€[DeepSpeed ใฎ GitHub ใƒšใƒผใ‚ธ](https://github.com/microsoft/deepspeed#installation) ใง่ฉณ็ดฐใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ [้ซ˜ๅบฆใชใ‚คใƒณใ‚นใƒˆใƒผใƒซ](https://www.deepspeed.ai/tutorials/advanced-install/)ใ€‚ ใใ‚Œใงใ‚‚ใƒ“ใƒซใƒ‰ใซ่‹ฆๅŠดใ™ใ‚‹ๅ ดๅˆใฏใ€ใพใš [CUDA ๆ‹กๅผตๆฉŸ่ƒฝใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซ ใƒŽใƒผใƒˆ](trainer#cuda-extension-installation-notes) ใ‚’ๅฟ…ใš่ชญใ‚“ใงใใ ใ•ใ„ใ€‚ ๆ‹กๅผตๆฉŸ่ƒฝใ‚’ไบ‹ๅ‰ใƒ“ใƒซใƒ‰ใ›ใšใ€ๅฎŸ่กŒๆ™‚ใซๆ‹กๅผตๆฉŸ่ƒฝใŒใƒ“ใƒซใƒ‰ใ•ใ‚Œใ‚‹ใ“ใจใซไพๅญ˜ใ—ใฆใŠใ‚Šใ€ไธŠ่จ˜ใฎ่งฃๆฑบ็ญ–ใ‚’ใ™ในใฆ่ฉฆใ—ใŸๅ ดๅˆ ใใ‚ŒใŒๅฝนใซ็ซ‹ใŸใชใ‹ใฃใŸๅ ดๅˆใ€ๆฌกใซ่ฉฆใ™ในใใ“ใจใฏใ€ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅ‰ใซใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไบ‹ๅ‰ใซใƒ“ใƒซใƒ‰ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ DeepSpeed ใฎใƒญใƒผใ‚ซใƒซ ใƒ“ใƒซใƒ‰ใ‚’ไฝœๆˆใ™ใ‚‹ใซใฏ: ```bash git clone https://github.com/microsoft/DeepSpeed/ cd DeepSpeed rm -rf build TORCH_CUDA_ARCH_LIST="8.6" DS_BUILD_CPU_ADAM=1 DS_BUILD_UTILS=1 pip install . \ --global-option="build_ext" --global-option="-j8" --no-cache -v \ --disable-pip-version-check 2>&1 | tee build.log ``` NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ไธŠ่จ˜ใฎๆ‰‹้ †ใซ`DS_BUILD_AIO=1`ใ‚’ๅซใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ (ใพใŸใ€ *libaio-dev* ใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™)ใ€‚ `TORCH_CUDA_ARCH_LIST` ใ‚’็ทจ้›†ใ—ใฆใ€ไฝฟ็”จใ™ใ‚‹ GPU ใ‚ซใƒผใƒ‰ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใ‚ณใƒผใƒ‰ใ‚’ๆŒฟๅ…ฅใ—ใพใ™ใ€‚ใ™ในใฆใ‚’ไปฎๅฎšใ™ใ‚‹ใจ ใ‚ใชใŸใฎใ‚ซใƒผใƒ‰ใฏๅŒใ˜ใงใ€ๆฌกใฎๆ–นๆณ•ใงใ‚ขใƒผใƒใ‚’ๅ–ๅพ—ใงใใพใ™ใ€‚ ```bash CUDA_VISIBLE_DEVICES=0 python -c "import torch; print(torch.cuda.get_device_capability())" ``` ใ—ใŸใŒใฃใฆใ€`8, 6`ใ‚’ๅ–ๅพ—ใ—ใŸๅ ดๅˆใฏใ€`TORCH_CUDA_ARCH_LIST="8.6"`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚่ค‡ๆ•ฐใฎ็•ฐใชใ‚‹ใ‚ซใƒผใƒ‰ใ‚’ใŠๆŒใกใฎๅ ดๅˆใฏใ€ใ™ในใฆใ‚’ใƒชใ‚นใƒˆใ™ใ‚‹ใ“ใจใŒใงใใพใ™ ใใ‚Œใ‚‰ใฎใ†ใกใ€`TORCH_CUDA_ARCH_LIST="6.1;8.6"`ใŒๅฅฝใใงใ™ ่ค‡ๆ•ฐใฎใƒžใ‚ทใƒณใงๅŒใ˜ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใƒใ‚คใƒŠใƒช ใƒ›ใ‚คใƒผใƒซใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ ```bash git clone https://github.com/microsoft/DeepSpeed/ cd DeepSpeed rm -rf build TORCH_CUDA_ARCH_LIST="8.6" DS_BUILD_CPU_ADAM=1 DS_BUILD_UTILS=1 \ python setup.py build_ext -j8 bdist_wheel ``` `dist/deepspeed-0.3.13+8cd046f-cp38-cp38-linux_x86_64.whl`ใฎใ‚ˆใ†ใชใ‚‚ใฎใŒ็”Ÿๆˆใ•ใ‚Œใ‚‹ใฎใงใ€ใ“ใ‚Œใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใพใ™ `pip install deepspeed-0.3.13+8cd046f-cp38-cp38-linux_x86_64.whl`ใจใ—ใฆใƒญใƒผใ‚ซใƒซใพใŸใฏไป–ใฎใƒžใ‚ทใƒณใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚ ็นฐใ‚Š่ฟ”ใ—ใพใ™ใŒใ€`TORCH_CUDA_ARCH_LIST`ใ‚’ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซๅˆใ‚ใ›ใฆ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚ NVIDIA GPU ใฎๅฎŒๅ…จใชใƒชใ‚นใƒˆใจใ€ใใ‚Œใซๅฏพๅฟœใ™ใ‚‹ **ใ‚ณใƒณใƒ”ใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐๆฉŸ่ƒฝ** (ใ“ใฎ่จ˜ไบ‹ใฎ Arch ใจๅŒใ˜) ใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ) [ใ“ใ“](https://developer.nvidia.com/cuda-gpus)ใ€‚ ไปฅไธ‹ใ‚’ไฝฟ็”จใ—ใฆใ€pytorch ใŒๆง‹็ฏ‰ใ•ใ‚ŒใŸใ‚ขใƒผใƒใ‚’็ขบ่ชใงใใพใ™ใ€‚ ```bash python -c "import torch; print(torch.cuda.get_arch_list())" ``` ใ“ใ“ใงใฏใ€ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ GPU ใฎ 1 ใคใฎใ‚ขใƒผใƒใ‚’่ฆ‹ใคใ‘ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€GPU 0 ใฎๅ ดๅˆ: ```bash CUDA_VISIBLE_DEVICES=0 python -c "import torch; \ print(torch.cuda.get_device_properties(torch.device('cuda')))" ``` ๅ‡บๅŠ›ใŒๆฌกใฎๅ ดๅˆ: ```bash _CudaDeviceProperties(name='GeForce RTX 3090', major=8, minor=6, total_memory=24268MB, multi_processor_count=82) ``` ใใ†ใ™ใ‚Œใฐใ€ใ“ใฎใ‚ซใƒผใƒ‰ใฎใ‚ขใƒผใƒใŒ`8.6`ใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ `TORCH_CUDA_ARCH_LIST` ใ‚’ๅฎŒๅ…จใซ็œ็•ฅใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใใ†ใ™ใ‚Œใฐใ€ใƒ“ใƒซใƒ‰ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใŒ่‡ชๅ‹•็š„ใซใ‚ฏใ‚จใƒชใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ ใƒ“ใƒซใƒ‰ใŒ่กŒใ‚ใ‚Œใ‚‹ GPU ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ใ“ใ‚Œใฏใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใƒžใ‚ทใƒณใฎ GPU ใจไธ€่‡ดใ™ใ‚‹ๅ ดๅˆใ‚‚ใ‚ใ‚Œใฐใ€ไธ€่‡ดใ—ใชใ„ๅ ดๅˆใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ ็›ฎ็š„ใฎใ‚ขใƒผใƒใ‚’ๆ˜Ž็คบ็š„ใซๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ ๆๆกˆใ•ใ‚ŒใŸใ“ใจใ‚’ใ™ในใฆ่ฉฆใ—ใฆใ‚‚ใพใ ใƒ“ใƒซใƒ‰ใฎๅ•้กŒใŒ็™บ็”Ÿใ™ใ‚‹ๅ ดๅˆใฏใ€GitHub ใฎๅ•้กŒใซ้€ฒใ‚“ใงใใ ใ•ใ„ใ€‚ [ใƒ‡ใ‚ฃใƒผใƒ—ใ‚นใƒ”ใƒผใƒ‰](https://github.com/microsoft/DeepSpeed/issues)ใ€ <a id='deepspeed-multi-gpu'></a> ### Deployment with multiple GPUs DeepSpeed ็ตฑๅˆใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ใซใฏใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ‚’่ชฟๆ•ดใ—ใฆๆ–ฐใ—ใ„ๅผ•ๆ•ฐ `--deepspeed ds_config.json` ใ‚’ๅซใ‚ใพใ™ใ€‚ใ“ใ“ใงใ€`ds_config.json` ใฏ DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใ™ใ€‚ [ใ“ใกใ‚‰](https://www.deepspeed.ai/docs/config-json/)ใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใƒ•ใ‚กใ‚คใƒซๅใฏใ‚ใชใŸๆฌก็ฌฌใงใ™ใ€‚ DeepSpeed ใฎ`add_config_arguments`ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚’ไฝฟ็”จใ—ใฆใ€ๅฟ…่ฆใชใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ‚’ใ‚ณใƒผใƒ‰ใซ่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[DeepSpeed ใฎๅผ•ๆ•ฐ่งฃๆž](https://deepspeed.readthedocs.io/en/latest/initialize.html#argument-parsing) ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ใ“ใ“ใง้ธๆŠžใ—ใŸใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ pytorch ใƒฉใƒณใƒใƒฃใƒผใ‚’ๅผ•ใ็ถšใไฝฟ็”จใงใใพใ™ใ€‚ ```bash torch.distributed.run --nproc_per_node=2 your_program.py <normal cl args> --deepspeed ds_config.json ``` ใพใŸใฏใ€`deepspeed`ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹ใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ ```bash deepspeed --num_gpus=2 your_program.py <normal cl args> --deepspeed ds_config.json ``` ใ”่ฆงใฎใจใŠใ‚Šใ€ๅผ•ๆ•ฐใฏๅŒใ˜ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใปใจใ‚“ใฉใฎใƒ‹ใƒผใ‚บใงใฏใฉใกใ‚‰ใงใ‚‚ๆฉŸ่ƒฝใ—ใพใ™ใ€‚ใฎ ใ•ใพใ–ใพใชใƒŽใƒผใƒ‰ใจ GPU ใ‚’ๆง‹ๆˆใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใกใ‚‰](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ `deepspeed`ใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ—ใ€ๅˆฉ็”จๅฏ่ƒฝใชใ™ในใฆใฎ GPU ใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€`--num_gpus`ใƒ•ใƒฉใ‚ฐใ‚’็œ็•ฅใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ ไปฅไธ‹ใฏใ€ๅˆฉ็”จๅฏ่ƒฝใชใ™ในใฆใฎ GPU ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ DeepSpeed ใง`run_translation.py`ใ‚’ๅฎŸ่กŒใ™ใ‚‹ไพ‹ใงใ™ใ€‚ ```bash deepspeed examples/pytorch/translation/run_translation.py \ --deepspeed tests/deepspeed/ds_config_zero3.json \ --model_name_or_path google-t5/t5-small --per_device_train_batch_size 1 \ --output_dir output_dir --overwrite_output_dir --fp16 \ --do_train --max_train_samples 500 --num_train_epochs 1 \ --dataset_name wmt16 --dataset_config "ro-en" \ --source_lang en --target_lang ro ``` DeepSpeed ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใซใฏใ€`--deepspeed --deepspeed_config ds_config.json`ใŒ่กจ็คบใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ DeepSpeed ้–ข้€ฃใฎๅผ•ๆ•ฐใŒ 2 ใคใ‚ใ‚Šใพใ™ใŒใ€็ฐกๅ˜ใซใ™ใ‚‹ใŸใ‚ใงใ‚ใ‚Šใ€ๅ‡ฆ็†ใ™ในใๅผ•ๆ•ฐใŒใ™ใงใซ้žๅธธใซๅคšใ„ใŸใ‚ใงใ™ใ€‚ ใ“ใฎ 2 ใคใ‚’ 1 ใคใฎๅผ•ๆ•ฐใซ็ตๅˆใ—ใพใ—ใŸใ€‚ ๅฎŸ้š›ใฎไฝฟ็”จไพ‹ใซใคใ„ใฆใฏใ€ใ“ใฎ [ๆŠ•็จฟ](https://github.com/huggingface/transformers/issues/8771#issuecomment-759248400) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ <a id='deepspeed-one-gpu'></a> ### Deployment with one GPU 1 ใคใฎ GPU ใง DeepSpeed ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ใซใฏใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ‚’ๆฌกใฎใ‚ˆใ†ใซ่ชฟๆ•ดใ—ใพใ™ใ€‚ ```bash deepspeed --num_gpus=1 examples/pytorch/translation/run_translation.py \ --deepspeed tests/deepspeed/ds_config_zero2.json \ --model_name_or_path google-t5/t5-small --per_device_train_batch_size 1 \ --output_dir output_dir --overwrite_output_dir --fp16 \ --do_train --max_train_samples 500 --num_train_epochs 1 \ --dataset_name wmt16 --dataset_config "ro-en" \ --source_lang en --target_lang ro ``` ใ“ใ‚Œใฏ่ค‡ๆ•ฐใฎ GPU ใฎๅ ดๅˆใจใปใผๅŒใ˜ใงใ™ใŒใ€ใ“ใ“ใงใฏใ€DeepSpeed ใซ 1 ใคใฎ GPU ใ ใ‘ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๆ˜Ž็คบ็š„ใซๆŒ‡็คบใ—ใพใ™ใ€‚ `--num_gpus=1`ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€DeepSpeed ใฏๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒŽใƒผใƒ‰ไธŠใง่ช่ญ˜ใงใใ‚‹ใ™ในใฆใฎ GPU ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ—ใพใ™ใ€‚่ตทๅ‹•ใ™ใ‚‹ GPU ใŒ 1 ใคใ ใ‘ใฎๅ ดๅˆ ใฎๅ ดๅˆใ€ใ“ใฎๅผ•ๆ•ฐใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆฌกใฎ [ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node) ใงใฏใ€ใƒฉใƒณใƒใƒฃใƒผ ใ‚ชใƒ—ใ‚ทใƒงใƒณใซใคใ„ใฆ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ 1 ใคใฎ GPU ใ ใ‘ใง DeepSpeed ใ‚’ไฝฟ็”จใ—ใŸใ„ใฎใฏใชใœใงใ™ใ‹? 1. ไธ€้ƒจใฎ่จˆ็ฎ—ใจใƒกใƒขใƒชใ‚’ใƒ›ใ‚นใƒˆใฎ CPU ใจ RAM ใซๅง”ไปปใงใใ‚‹ ZeRO ใ‚ชใƒ•ใƒญใƒผใƒ‰ๆฉŸ่ƒฝใ‚’ๅ‚™ใˆใฆใ„ใ‚‹ใŸใ‚ใ€ ใƒขใƒ‡ใƒซใฎใƒ‹ใƒผใ‚บใซๅˆใ‚ใ›ใฆใ‚ˆใ‚Šๅคšใใฎ GPU ใƒชใ‚ฝใƒผใ‚นใ‚’ๆฎ‹ใ—ใฆใŠใใพใ™ใ€‚ใ‚ˆใ‚Šๅคงใใชใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ€ใพใŸใฏ้žๅธธใซๅคงใใชใƒขใƒ‡ใƒซใฎใƒ•ใ‚ฃใƒƒใƒ†ใ‚ฃใƒณใ‚ฐใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ ๆ™ฎ้€šใฏๅˆใ‚ใชใ„ใงใ—ใ‚‡ใ†ใ€‚ 2. ใ‚นใƒžใƒผใƒˆใช GPU ใƒกใƒขใƒช็ฎก็†ใ‚ทใ‚นใƒ†ใƒ ใ‚’ๆไพ›ใ—ใ€ใƒกใƒขใƒชใฎๆ–ญ็‰‡ๅŒ–ใ‚’ๆœ€ๅฐ้™ใซๆŠ‘ใˆใพใ™ใ€‚ ใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใจใƒ‡ใƒผใ‚ฟ ใƒใƒƒใƒใ€‚ ๆฌกใซๆง‹ๆˆใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใŒใ€ๅ˜ไธ€ใฎ GPU ใงๅคงๅน…ใชๆ”นๅ–„ใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใฎ้ตใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ DeepSpeed ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใซๅฐ‘ใชใใจใ‚‚ๆฌกใฎๆง‹ๆˆใŒๅฟ…่ฆใงใ™ใ€‚ ```json { "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 2e8, "reduce_scatter": true, "reduce_bucket_size": 2e8, "overlap_comm": true, "contiguous_gradients": true } } ``` ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚„ใใฎไป–ใฎ้‡่ฆใชๆฉŸ่ƒฝใŒๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใƒใƒƒใƒ•ใ‚ก ใ‚ตใ‚คใ‚บใ‚’่ฉฆใ—ใฆใฟใ‚‹ใจใ‚ˆใ„ใงใ—ใ‚‡ใ†ใ€‚ ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ไปฅไธ‹ใฎใƒ‡ใ‚ฃใ‚นใ‚ซใƒƒใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ใ“ใฎใ‚ฟใ‚คใƒ—ใฎใƒ‡ใƒ—ใƒญใ‚คใƒกใƒณใƒˆใฎๅฎŸ้š›็š„ใชไฝฟ็”จไพ‹ใซใคใ„ใฆใฏใ€ใ“ใฎ [ๆŠ•็จฟ](https://github.com/huggingface/transformers/issues/8771#issuecomment-759176685) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใง่ฉณใ—ใ่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€CPU ใŠใ‚ˆใณ NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ๅ‚™ใˆใŸ ZeRO-3 ใ‚’่ฉฆใ™ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ใƒŽใƒผใƒˆ๏ผš - GPU 0 ใจใฏ็•ฐใชใ‚‹็‰นๅฎšใฎ GPU ใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€`CUDA_VISIBLE_DEVICES` ใ‚’ไฝฟ็”จใ—ใฆๅˆถ้™ใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ ๅˆฉ็”จๅฏ่ƒฝใช GPU ใฎ่กจ็คบ็ฏ„ๅ›ฒใ€‚ไปฃใ‚ใ‚Šใซใ€ๆฌกใฎๆง‹ๆ–‡ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ```bash deepspeed --include localhost:1 examples/pytorch/translation/run_translation.py ... ``` ใ“ใฎไพ‹ใงใฏใ€DeepSpeed ใซ GPU 1 (2 ็•ช็›ฎใฎ GPU) ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๆŒ‡็คบใ—ใพใ™ใ€‚ <a id='deepspeed-multi-node'></a> ### ่ค‡ๆ•ฐใฎใƒŽใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใŸใƒ‡ใƒ—ใƒญใ‚คใƒกใƒณใƒˆ ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๆƒ…ๅ ฑใฏ DeepSpeed ็ตฑๅˆใซๅ›บๆœ‰ใฎใ‚‚ใฎใงใฏใชใใ€ใ‚ใ‚‰ใ‚†ใ‚‹ใƒžใƒซใƒใƒŽใƒผใƒ‰ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใซ้ฉ็”จใงใใพใ™ใ€‚ใŸใ ใ—ใ€DeepSpeed ใฏใ€SLURM ็’ฐๅขƒใงใชใ„้™ใ‚Šใ€ไป–ใฎใƒฉใƒณใƒใƒฃใƒผใ‚ˆใ‚Šใ‚‚ไฝฟใ„ใ‚„ใ™ใ„`deepspeed`ใƒฉใƒณใƒใƒฃใƒผใ‚’ๆไพ›ใ—ใพใ™ใ€‚ ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใใ‚Œใžใ‚Œ 8 GPU ใ‚’ๅ‚™ใˆใŸ 2 ใคใฎใƒŽใƒผใƒ‰ใŒใ‚ใ‚‹ใจไปฎๅฎšใ—ใพใ™ใ€‚ใพใŸใ€ๆœ€ๅˆใฎใƒŽใƒผใƒ‰ใซใฏ `ssh hostname1` ใ‚’ไฝฟ็”จใ—ใฆใ€2 ็•ช็›ฎใฎใƒŽใƒผใƒ‰ใซใฏ `ssh hostname2` ใ‚’ไฝฟ็”จใ—ใฆๆŽฅ็ถšใงใใพใ™ใ€‚ไธกๆ–นใจใ‚‚ใƒ‘ใ‚นใƒฏใƒผใƒ‰ใชใ—ใงใƒญใƒผใ‚ซใƒซใฎ ssh ็ตŒ็”ฑใง็›ธไบ’ใซๆŽฅ็ถšใงใใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใ‚‰ใฎใƒ›ใ‚นใƒˆ (ใƒŽใƒผใƒ‰) ๅใ‚’ใ€ไฝœๆฅญใ—ใฆใ„ใ‚‹ๅฎŸ้š›ใฎใƒ›ใ‚นใƒˆๅใซๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ #### The torch.distributed.run launcher ใŸใจใˆใฐใ€`torch.distributed.run` ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ ```bash python -m torch.distributed.run --nproc_per_node=8 --nnode=2 --node_rank=0 --master_addr=hostname1 \ --master_port=9901 your_program.py <normal cl args> --deepspeed ds_config.json ``` ๅ„ใƒŽใƒผใƒ‰ใซ SSH ใงๆŽฅ็ถšใ—ใ€ใใ‚Œใžใ‚ŒใฎใƒŽใƒผใƒ‰ใงๅŒใ˜ใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆ€ฅใๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒฉใƒณใƒใƒฃใƒผใฏไธกๆ–นใฎใƒŽใƒผใƒ‰ใŒๅŒๆœŸใ™ใ‚‹ใพใงๅพ…ๆฉŸใ—ใพใ™ใ€‚ ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[torchrun](https://pytorch.org/docs/stable/elastic/run.html) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใกใชใฟใซใ€ใ“ใ‚Œใฏ pytorch ใฎๆ•ฐใƒใƒผใ‚ธใƒงใƒณๅ‰ใฎ`torch.distributed.launch`ใ‚’็ฝฎใๆ›ใˆใŸใƒฉใƒณใƒใƒฃใƒผใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ #### ใƒ‡ใ‚ฃใƒผใƒ—ใ‚นใƒ”ใƒผใƒ‰ ใƒฉใƒณใƒใƒฃใƒผ ไปฃใ‚ใ‚Šใซ`deepspeed`ใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใพใš`hostfile`ใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ``` hostname1 slots=8 hostname2 slots=8 ``` ใใ—ใฆใ€ๆฌกใฎใ‚ˆใ†ใซ่ตทๅ‹•ใงใใพใ™ใ€‚ ```bash deepspeed --num_gpus 8 --num_nodes 2 --hostfile hostfile --master_addr hostname1 --master_port=9901 \ your_program.py <normal cl args> --deepspeed ds_config.json ``` `torch.distributed.run`ใƒฉใƒณใƒใƒฃใƒผใจใฏ็•ฐใชใ‚Šใ€`deepspeed`ใฏไธกๆ–นใฎใƒŽใƒผใƒ‰ใงใ“ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’่‡ชๅ‹•็š„ใซ่ตทๅ‹•ใ—ใพใ™ใ€‚ ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒชใ‚ฝใƒผใ‚นๆง‹ๆˆ (ใƒžใƒซใƒใƒŽใƒผใƒ‰)](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ #### Launching in a SLURM environment SLURM ็’ฐๅขƒใงใฏใ€ๆฌกใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ไปฅไธ‹ใฏใ€็‰นๅฎšใฎ SLURM ็’ฐๅขƒใซ้ฉๅˆใ•ใ›ใ‚‹ใŸใ‚ใซๅฟ…่ฆใช slurm ใ‚นใ‚ฏใƒชใƒ—ใƒˆ `launch.slurm` ใงใ™ใ€‚ ```bash #SBATCH --job-name=test-nodes # name #SBATCH --nodes=2 # nodes #SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! #SBATCH --cpus-per-task=10 # number of cores per tasks #SBATCH --gres=gpu:8 # number of gpus #SBATCH --time 20:00:00 # maximum execution time (HH:MM:SS) #SBATCH --output=%x-%j.out # output file name export GPUS_PER_NODE=8 export MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) export MASTER_PORT=9901 srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ your_program.py <normal cl args> --deepspeed ds_config.json' ``` ใ‚ใจใฏๅฎŸ่กŒใ‚’ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ ```bash sbatch launch.slurm ``` #### Use of Non-shared filesystem ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€DeepSpeed ใฏใƒžใƒซใƒใƒŽใƒผใƒ‰็’ฐๅขƒใŒๅ…ฑๆœ‰ใ‚นใƒˆใƒฌใƒผใ‚ธใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๆƒณๅฎšใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚ŒใŒๅฝ“ใฆใฏใพใ‚‰ใšใ€ๅ„ใƒŽใƒผใƒ‰ใŒใƒญใƒผใ‚ซใƒซ ใƒ•ใ‚กใ‚คใƒซใ‚ทใ‚นใƒ†ใƒ ใ—ใ‹ๅ‚็…งใงใใชใ„ๅ ดๅˆใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’่ชฟๆ•ดใ—ใฆ [`checkpoint`_section](https://www.deepspeed.ai/docs/config-json/#) ใ‚’ๅซใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใ‚ชใƒ—ใ‚ทใƒงใƒณ) ใ‚’ๆฌกใฎ่จญๅฎšใงๆŒ‡ๅฎšใ—ใพใ™ใ€‚ ```json { "checkpoint": { "use_node_local_storage": true } } ``` ใ‚ใ‚‹ใ„ใฏใ€[`Trainer`] ใฎ `--save_on_each_node` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใ€ไธŠ่จ˜ใฎ่จญๅฎšใฏ่‡ชๅ‹•็š„ใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ <a id='deepspeed-notebook'></a> ### Deployment in Notebooks ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฎใ‚ปใƒซใ‚’ใ‚นใ‚ฏใƒชใƒ—ใƒˆใจใ—ใฆๅฎŸ่กŒใ™ใ‚‹ๅ ดๅˆใฎๅ•้กŒใฏใ€ไพๅญ˜ใ™ใ‚‹้€šๅธธใฎ`deepspeed`ใƒฉใƒณใƒใƒฃใƒผใŒใชใ„ใ“ใจใงใ™ใ€‚ ็‰นๅฎšใฎ่จญๅฎšใงใฏใ€ใใ‚Œใ‚’ใ‚จใƒŸใƒฅใƒฌใƒผใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ GPU ใ‚’ 1 ใคใ ใ‘ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€DeepSpeed ใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏๅ†…ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ณใƒผใƒ‰ใ‚’่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ ```python # DeepSpeed requires a distributed environment even when only one process is used. # This emulates a launcher in the notebook import os os.environ["MASTER_ADDR"] = "localhost" os.environ["MASTER_PORT"] = "9994" # modify if RuntimeError: Address already in use os.environ["RANK"] = "0" os.environ["LOCAL_RANK"] = "0" os.environ["WORLD_SIZE"] = "1" # Now proceed as normal, plus pass the deepspeed config file training_args = TrainingArguments(..., deepspeed="ds_config_zero3.json") trainer = Trainer(...) trainer.train() ``` ๆณจ: `...` ใฏใ€้–ขๆ•ฐใซๆธกใ™้€šๅธธใฎๅผ•ๆ•ฐใ‚’่กจใ—ใพใ™ใ€‚ ่ค‡ๆ•ฐใฎ GPU ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€DeepSpeed ใŒๅ‹•ไฝœใ™ใ‚‹ใซใฏใƒžใƒซใƒใƒ—ใƒญใ‚ปใ‚น็’ฐๅขƒใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใคใพใ‚Šใ€ใ‚ใชใŸใฏๆŒใฃใฆใ„ใพใ™ ใใฎ็›ฎ็š„ใงใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใŒใ€ใ“ใ‚Œใฏใ€ๆ็คบใ•ใ‚ŒใŸๅˆ†ๆ•ฃ็’ฐๅขƒใ‚’ใ‚จใƒŸใƒฅใƒฌใƒผใƒˆใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆใฏๅฎŸ็พใงใใพใ›ใ‚“ใ€‚ ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๅ†’้ ญใงใ€‚ ็พๅœจใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใซใใฎๅ ดใงๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ—ใŸใ„ๅ ดๅˆใฏใ€ๅฐ‚็”จใฎ ใ‚ปใƒซใฎๅ†…ๅฎน: ```python no-style %%bash cat <<'EOT' > ds_config_zero3.json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } }, "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "offload_param": { "device": "cpu", "pin_memory": true }, "overlap_comm": true, "contiguous_gradients": true, "sub_group_size": 1e9, "reduce_bucket_size": "auto", "stage3_prefetch_bucket_size": "auto", "stage3_param_persistence_threshold": "auto", "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true }, "gradient_accumulation_steps": "auto", "gradient_clipping": "auto", "steps_per_print": 2000, "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto", "wall_clock_breakdown": false } EOT ``` ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใŒใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฎใ‚ปใƒซใงใฏใชใ้€šๅธธใฎใƒ•ใ‚กใ‚คใƒซใซใ‚ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใฆ`deepspeed`ใ‚’้€šๅธธใฉใŠใ‚Š่ตทๅ‹•ใงใใพใ™ใ€‚ ็ดฐ่ƒžใ‹ใ‚‰ใฎใ‚ทใ‚งใƒซใ€‚ใŸใจใˆใฐใ€`run_translation.py` ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซ่ตทๅ‹•ใ—ใพใ™ใ€‚ ```python no-style !git clone https://github.com/huggingface/transformers !cd transformers; deepspeed examples/pytorch/translation/run_translation.py ... ``` ใพใŸใฏใ€`%%bash` ใƒžใ‚ธใƒƒใ‚ฏใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ‚ทใ‚งใƒซ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใฎ่ค‡ๆ•ฐ่กŒใฎใ‚ณใƒผใƒ‰ใ‚’่จ˜่ฟฐใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ```python no-style %%bash git clone https://github.com/huggingface/transformers cd transformers deepspeed examples/pytorch/translation/run_translation.py ... ``` ใใฎใ‚ˆใ†ใชๅ ดๅˆใ€ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๆœ€ๅˆใซ็คบใ—ใŸใ‚ณใƒผใƒ‰ใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ๆณจ: `%%bash` ใƒžใ‚ธใƒƒใ‚ฏใฏๅ„ชใ‚Œใฆใ„ใพใ™ใŒใ€็พๆ™‚็‚นใงใฏๅ‡บๅŠ›ใ‚’ใƒใƒƒใƒ•ใ‚กใƒชใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใ€ใƒ—ใƒญใ‚ปใ‚นใŒ็ต‚ไบ†ใ™ใ‚‹ใพใงใƒญใ‚ฐใฏ่กจ็คบใ•ใ‚Œใพใ›ใ‚“ใ€‚ ๅฎŒไบ†ใ—ใพใ™ใ€‚ <a id='deepspeed-config'></a> ### Configuration ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใงไฝฟ็”จใงใใ‚‹ DeepSpeed ่จญๅฎšใ‚ชใƒ—ใ‚ทใƒงใƒณใฎๅฎŒๅ…จใชใ‚ฌใ‚คใƒ‰ใซใคใ„ใฆใฏใ€ๆฌกใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ [ๆฌกใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/docs/config-json/) ใซใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆใใ ใ•ใ„ใ€‚ ใ•ใพใ–ใพใชๅฎŸ้š›ใฎใƒ‹ใƒผใ‚บใซๅฏพๅฟœใ™ใ‚‹ๆ•ฐๅใฎ DeepSpeed ๆง‹ๆˆไพ‹ใ‚’ [DeepSpeedExamples](https://github.com/microsoft/DeepSpeedExamples)ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ใƒชใƒใ‚ธใƒˆใƒช: ```bash git clone https://github.com/microsoft/DeepSpeedExamples cd DeepSpeedExamples find . -name '*json' ``` ไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ใ‚’็ถšใ‘ใฆใ€Lamb ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ๆง‹ๆˆใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ใจใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ๆฌกใฎไธญใ‹ใ‚‰ๆคœ็ดขใงใใพใ™ `.json` ใƒ•ใ‚กใ‚คใƒซใฎไพ‹: ```bash grep -i Lamb $(find . -name '*json') ``` ใ•ใ‚‰ใซใ„ใใคใ‹ใฎไพ‹ใŒ [ใƒกใ‚คใƒณ ใƒชใƒใ‚ธใƒˆใƒช](https://github.com/microsoft/DeepSpeed) ใซใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ DeepSpeed ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ๅธธใซ DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใŒใ€ไธ€้ƒจใฎๆง‹ๆˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใซใฏ ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณ็ตŒ็”ฑใง่จญๅฎšใ—ใพใ™ใ€‚ๅพฎๅฆ™ใช้•ใ„ใซใคใ„ใฆใฏใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๆฎ‹ใ‚Šใฎ้ƒจๅˆ†ใง่ชฌๆ˜Žใ—ใพใ™ใ€‚ DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใŒใฉใฎใ‚ˆใ†ใชใ‚‚ใฎใ‹ใ‚’็†่งฃใ™ใ‚‹ใŸใ‚ใซใ€ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 ๆฉŸ่ƒฝใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ็Šถๆ…‹ใฎ CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ๅซใฟใ€`AdamW`ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจ`WarmupLR`ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’ไฝฟ็”จใ—ใ€ๆททๅˆใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ `--fp16` ใŒๆธกใ•ใ‚ŒใŸๅ ดๅˆใฎ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ: ```json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } }, "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 2e8, "contiguous_gradients": true }, "gradient_accumulation_steps": "auto", "gradient_clipping": "auto", "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto", } ``` ใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใจใ€DeepSpeed ใฏ [`Trainer`] ใ‹ใ‚‰ๅ—ใ‘ๅ–ใฃใŸ่จญๅฎšใ‚’ใƒญใ‚ฐใซ่จ˜้Œฒใ—ใพใ™ใ€‚ ใ‚ณใƒณใ‚ฝใƒผใƒซใซๆธกใ•ใ‚Œใ‚‹ใŸใ‚ใ€ๆœ€็ต‚็š„ใซใฉใฎใ‚ˆใ†ใช่จญๅฎšใŒๆธกใ•ใ‚ŒใŸใฎใ‹ใ‚’ๆญฃ็ขบใซ็ขบ่ชใงใใพใ™ใ€‚ <a id='deepspeed-config-passing'></a> ### Passing Configuration ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใง่ชฌๆ˜Žใ—ใŸใ‚ˆใ†ใซใ€้€šๅธธใ€DeepSpeed ่จญๅฎšใฏ json ใƒ•ใ‚กใ‚คใƒซใธใฎใƒ‘ใ‚นใจใ—ใฆๆธกใ•ใ‚Œใพใ™ใŒใ€ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ่จญๅฎšใซใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณ ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใ‚คใ‚นใ‚’ไฝฟ็”จใ›ใšใ€ไปฃใ‚ใ‚Šใซใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ [`Trainer`] via [`TrainingArguments`] ใใฎๅพŒใ€`deepspeed` ๅผ•ๆ•ฐใซใคใ„ใฆใฏๆฌกใฎใ“ใจใŒใงใใพใ™ ใƒใ‚นใƒˆใ•ใ‚ŒใŸ `dict` ใ‚’ๆธกใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใใฎๅ ดใงๆง‹ๆˆใ‚’ไฝœๆˆใงใใ€ใใ‚Œใ‚’ๆ›ธใ่พผใ‚€ๅฟ…่ฆใŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ [`TrainingArguments`] ใซๆธกใ™ๅ‰ใซใƒ•ใ‚กใ‚คใƒซ ใ‚ทใ‚นใƒ†ใƒ ใ‚’ๅค‰ๆ›ดใ—ใพใ™ใ€‚ ่ฆ็ด„ใ™ใ‚‹ใจใ€ๆฌกใฎใ“ใจใŒใงใใพใ™ใ€‚ ```python TrainingArguments(..., deepspeed="/path/to/ds_config.json") ``` ใพใŸใฏ๏ผš ```python ds_config_dict = dict(scheduler=scheduler_params, optimizer=optimizer_params) TrainingArguments(..., deepspeed=ds_config_dict) ``` <a id='deepspeed-config-shared'></a> ### Shared Configuration <Tip warning={true}> ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏๅฟ…่ชญใงใ™ </Tip> [`Trainer`] ใจ DeepSpeed ใฎไธกๆ–นใŒๆญฃใ—ใๆฉŸ่ƒฝใ™ใ‚‹ใซใฏใ€ใ„ใใคใ‹ใฎ่จญๅฎšๅ€คใŒๅฟ…่ฆใงใ™ใ€‚ ใ—ใŸใŒใฃใฆใ€ๆคœๅ‡บใŒๅ›ฐ้›ฃใชใ‚จใƒฉใƒผใซใคใชใŒใ‚‹ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ๅฎš็พฉใฎ็ซถๅˆใ‚’้˜ฒใใŸใ‚ใซใ€ใใ‚Œใ‚‰ใ‚’ๆง‹ๆˆใ™ใ‚‹ใ“ใจใซใ—ใพใ—ใŸใ€‚ [`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐ็ตŒ็”ฑใ€‚ ใ•ใ‚‰ใซใ€ไธ€้ƒจใฎๆง‹ๆˆๅ€คใฏใƒขใƒ‡ใƒซใฎๆง‹ๆˆใซๅŸบใฅใ„ใฆ่‡ชๅ‹•็š„ใซๅฐŽๅ‡บใ•ใ‚Œใพใ™ใ€‚ ่ค‡ๆ•ฐใฎๅ€คใ‚’ๆ‰‹ๅ‹•ใง่ชฟๆ•ดใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚[`Trainer`] ใซๅคง้ƒจๅˆ†ใ‚’ไปปใ›ใ‚‹ใฎใŒๆœ€ๅ–„ใงใ™ ใฎ่จญๅฎšใ‚’่กŒใ„ใพใ™ใ€‚ ใ—ใŸใŒใฃใฆใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๆฎ‹ใ‚Šใฎ้ƒจๅˆ†ใงใฏใ€็‰นๅˆฅใช่จญๅฎšๅ€ค `auto` ใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚’่จญๅฎšใ™ใ‚‹ใจใ€ ๆญฃใ—ใ„ๅ€คใพใŸใฏๆœ€ใ‚‚ๅŠน็އ็š„ใชๅ€คใซ่‡ชๅ‹•็š„ใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚’็„ก่ฆ–ใ™ใ‚‹ใ“ใจใ‚’่‡ช็”ฑใซ้ธๆŠžใ—ใฆใใ ใ•ใ„ ๆŽจๅฅจไบ‹้ …ใ‚’ๅ‚็…งใ—ใ€ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ—ใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ๆฌกใฎ็‚นใซๅๅˆ†ๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ [`Trainer`] ๅผ•ๆ•ฐใจ DeepSpeed ่จญๅฎšใฏไธ€่‡ดใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€ๅŒใ˜ใ‚‚ใฎใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ‹ ๅญฆ็ฟ’็އใ€ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ€ใพใŸใฏๅ‹พ้…็ดฏ็ฉ่จญๅฎš?ใ“ใ‚Œใ‚‰ใŒไธ€่‡ดใ—ใชใ„ๅ ดๅˆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏ้žๅธธใซๅคฑๆ•—ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ ๆ–นๆณ•ใ‚’ๆคœๅ‡บใ™ใ‚‹ใฎใŒ้›ฃใ—ใ„ใ€‚ใ‚ใชใŸใฏ่ญฆๅ‘Šใ‚’ๅ—ใ‘ใพใ—ใŸใ€‚ DeepSpeed ใฎใฟใซๅ›บๆœ‰ใฎๅ€คใ‚„ใ€ใใ‚Œใซๅˆใ‚ใ›ใฆๆ‰‹ๅ‹•ใง่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ€คใŒไป–ใซใ‚‚่ค‡ๆ•ฐใ‚ใ‚Šใพใ™ใ€‚ ใ‚ใชใŸใฎ่ฆๆœ›ใ€‚ ็‹ฌ่‡ชใฎใƒ—ใƒญใ‚ฐใƒฉใƒ ใงใ€DeepSpeed ๆง‹ๆˆใ‚’ใƒžใ‚นใ‚ฟใƒผใจใ—ใฆๅค‰ๆ›ดใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ใใ‚ŒใซๅŸบใฅใ„ใฆ [`TrainingArguments`] ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ๆ‰‹้ †ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ 1. ใƒžใ‚นใ‚ฟใƒผๆง‹ๆˆใจใ—ใฆไฝฟ็”จใ™ใ‚‹ DeepSpeed ๆง‹ๆˆใ‚’ไฝœๆˆใพใŸใฏใƒญใƒผใƒ‰ใ—ใพใ™ 2. ใ“ใ‚Œใ‚‰ใฎๅ€คใซๅŸบใฅใ„ใฆ [`TrainingArguments`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ—ใพใ™ `scheduler.params.total_num_steps`ใชใฉใฎไธ€้ƒจใฎๅ€คใฏๆฌกใฎใ‚ˆใ†ใซ่จˆ็ฎ—ใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ `train` ไธญใซ [`Trainer`] ใ‚’ๅฎŸ่กŒใ—ใพใ™ใŒใ€ใ‚‚ใกใ‚ใ‚“่‡ชๅˆ†ใง่จˆ็ฎ—ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ <a id='deepspeed-zero'></a> ### ZeRO [Zero Redundancy Optimizer (ZeRO)](https://www.deepspeed.ai/tutorials/zero/) ใฏใ€DeepSpeed ใฎไธปๅŠ›่ฃฝๅ“ใงใ™ใ€‚ใใ‚Œ 3 ใคใฎ็•ฐใชใ‚‹ใƒฌใƒ™ใƒซ (ๆฎต้šŽ) ใฎๆœ€้ฉๅŒ–ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ๆœ€ๅˆใฎใ‚‚ใฎใฏใ€ใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใฎ่ฆณ็‚นใ‹ใ‚‰ใฏใ‚ใพใ‚Š่ˆˆๅ‘ณๆทฑใ„ใ‚‚ใฎใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ใ—ใŸใŒใฃใฆใ€ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ‚นใƒ†ใƒผใ‚ธ 2 ใจ 3 ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ™ใ€‚ใ‚นใƒ†ใƒผใ‚ธ 3 ใฏใ€ๆœ€ๆ–ฐใฎ ZeRO-Infinity ใฎ่ฟฝๅŠ ใซใ‚ˆใฃใฆใ•ใ‚‰ใซๆ”นๅ–„ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ ่ฉณ็ดฐใซใคใ„ใฆใฏใ€DeepSpeed ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใฎ `zero_optimization` ใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏๆœ€ใ‚‚้‡่ฆใช้ƒจๅˆ†ใงใ™ ([docs](https://www.deepspeed.ai/docs/config-json/#zero-optimizations-for-fp16-training))ใ€‚ใ“ใ“ใงๅฎš็พฉใ—ใพใ™ ใฉใฎ ZeRO ใ‚นใƒ†ใƒผใ‚ธใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ‹ใ€ใใ—ใฆใใ‚Œใ‚‰ใ‚’ใฉใฎใ‚ˆใ†ใซๆง‹ๆˆใ™ใ‚‹ใ‹ใ€‚ๅ„ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ่ชฌๆ˜Žใฏใ€ DeepSpeed ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ€‚ ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏใ€DeepSpeed ่จญๅฎšใ‚’ไป‹ใ—ใฆใฎใฟ่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ - [`Trainer`] ใŒๆไพ›ใ—ใพใ™ ๅŒ็ญ‰ใฎใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ๆณจ: ็พๅœจใ€DeepSpeed ใฏใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๅใ‚’ๆคœ่จผใ—ใชใ„ใŸใ‚ใ€ใ‚นใƒšใƒซใ‚’้–“้•ใˆใ‚‹ใจใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆ่จญๅฎšใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ ใ‚นใƒšใƒซใŒ้–“้•ใฃใฆใ„ใ‚‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€‚ DeepSpeed ใ‚จใƒณใ‚ธใƒณใฎ่ตทๅ‹•ใƒญใ‚ฐ ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’่ฆ‹ใฆใ€ใใฎๅ€คใ‚’็ขบ่ชใงใใพใ™ใ€‚ ไฝฟ็”จใ™ใ‚‹ใคใ‚‚ใ‚Šใงใ™ใ€‚ <a id='deepspeed-zero2-config'></a> #### ZeRO-2 Config ไปฅไธ‹ใฏใ€ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 ใฎๆง‹ๆˆไพ‹ใงใ™ใ€‚ ```json { "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 5e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 5e8, "contiguous_gradients": true } } ``` **ๆ€ง่ƒฝ่ชฟๆ•ด๏ผš** - `offload_optimizer` ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใจใ€GPU RAM ใฎไฝฟ็”จ้‡ใŒๅ‰Šๆธ›ใ•ใ‚Œใพใ™ (`"stage": 2` ใŒๅฟ…่ฆใงใ™) - `"overlap_comm": true` ใฏใ€GPU RAM ไฝฟ็”จ้‡ใฎๅข—ๅŠ ใจใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใ—ใฆใ€้…ๅปถใ‚’ใ™ในใฆๅ‰Šๆธ›ใ—ใพใ™ใ€‚ `overlap_comm`ใฏ 4.5x ใ‚’ไฝฟ็”จใ—ใพใ™ `allgather_bucket_size`ใจ`reduce_bucket_size`ใฎๅ€คใ€‚ใ—ใŸใŒใฃใฆใ€5e8 ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€9GB ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ ใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆ (`5e8 x 2Bytes x 2 x 4.5`)ใ€‚ใ—ใŸใŒใฃใฆใ€8GB ไปฅไธ‹ใฎ RAM ใ‚’ๆญ่ผ‰ใ—ใŸ GPU ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ OOM ใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`2e8`็จ‹ๅบฆใซๆธ›ใ‚‰ใ™ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใใ‚Œใซใฏ 3.6GB ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ใ‚„ใ‚ŠใŸใใชใ‚‹ใงใ—ใ‚‡ใ† OOM ใซ้”ใ—ๅง‹ใ‚ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ใ‚ˆใ‚Šๅคงๅฎน้‡ใฎ GPU ใงใ‚‚ๅŒๆง˜ใงใ™ใ€‚ - ใ“ใ‚Œใ‚‰ใฎใƒใƒƒใƒ•ใ‚กใ‚’ๆธ›ใ‚‰ใ™ใจใ€ใ‚ˆใ‚Šๅคšใใฎ GPU RAM ใ‚’ๅˆฉ็”จใ™ใ‚‹ใŸใ‚ใซ้€šไฟก้€Ÿๅบฆใ‚’็Š ็‰ฒใซใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ใƒใƒƒใƒ•ใ‚กใ‚ตใ‚คใ‚บใŒๅฐใ•ใ„ใปใฉใ€ ้€šไฟกใŒ้…ใใชใ‚Šใ€ไป–ใฎใ‚ฟใ‚นใ‚ฏใงไฝฟ็”จใงใใ‚‹ GPU RAM ใŒๅข—ใˆใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒๅคงใใ„ๅ ดๅˆใฏใ€ ้‡่ฆใชใฎใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚้–“ใ‚’ๅฐ‘ใ—้…ใ‚‰ใ›ใ‚‹ใ“ใจใฏ่‰ฏใ„ใƒˆใƒฌใƒผใƒ‰ใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ ใ•ใ‚‰ใซใ€`deepspeed==0.4.4`ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใงๆœ‰ๅŠนใซใงใใ‚‹ๆ–ฐใ—ใ„ใ‚ชใƒ—ใ‚ทใƒงใƒณ`round_robin_gradients`ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚ ```json { "zero_optimization": { "round_robin_gradients": true } } ``` ใ“ใ‚Œใฏใ€ใใ‚็ดฐใ‹ใ„ๅ‹พ้…ใƒ‘ใƒผใƒ†ใ‚ฃใ‚ทใƒงใƒ‹ใƒณใ‚ฐใซใ‚ˆใฃใฆใƒฉใƒณใ‚ฏ้–“ใฎ CPU ใƒกใƒขใƒชใธใฎๅ‹พ้…ใ‚ณใƒ”ใƒผใ‚’ไธฆๅˆ—ๅŒ–ใ™ใ‚‹ใ€CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใฎใ‚นใƒ†ใƒผใ‚ธ 2 ๆœ€้ฉๅŒ–ใงใ™ใ€‚ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅˆฉ็‚นใฏใ€ๅ‹พ้…็ดฏ็ฉใ‚นใƒ†ใƒƒใƒ— (ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใ‚นใƒ†ใƒƒใƒ—้–“ใฎใ‚ณใƒ”ใƒผใฎๅข—ๅŠ ) ใพใŸใฏ GPU ๆ•ฐ (ไธฆๅˆ—ๅ‡ฆ็†ใฎๅข—ๅŠ ) ใซๅฟœใ˜ใฆๅข—ๅŠ ใ—ใพใ™ใ€‚ <a id='deepspeed-zero3-config'></a> #### ZeRO-3 Config ไปฅไธ‹ใฏใ€ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ใฎๆง‹ๆˆไพ‹ใงใ™ใ€‚ ```json { "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "offload_param": { "device": "cpu", "pin_memory": true }, "overlap_comm": true, "contiguous_gradients": true, "sub_group_size": 1e9, "reduce_bucket_size": "auto", "stage3_prefetch_bucket_size": "auto", "stage3_param_persistence_threshold": "auto", "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true } } ``` ใƒขใƒ‡ใƒซใพใŸใฏใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใŒ GPU ใƒกใƒขใƒชใซ้ฉๅˆใ›ใšใ€CPU ใŒๆœชไฝฟ็”จใงใ‚ใ‚‹ใŸใ‚ใซ OOM ใŒ็™บ็”Ÿใ—ใฆใ„ใ‚‹ๅ ดๅˆ `"device": "cpu"` ใ‚’ไฝฟ็”จใ—ใฆใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใจใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ CPU ใƒกใƒขใƒชใซใƒกใƒขใƒชใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใ€ใ“ใฎๅˆถ้™ใŒ่งฃๆฑบใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ CPU ใƒกใƒขใƒชใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใŸใใชใ„ๅ ดๅˆใฏใ€`device`ใ‚จใƒณใƒˆใƒชใซ`cpu`ใฎไปฃใ‚ใ‚Šใซ`none`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ‚ชใƒ•ใƒญใƒผใƒ‰ๅ…ˆ NVMe ใซใคใ„ใฆใฏๅพŒใปใฉ่ชฌๆ˜Žใ—ใพใ™ใ€‚ ๅ›บๅฎšใƒกใƒขใƒชใฏใ€`pin_memory`ใ‚’`true`ใซ่จญๅฎšใ™ใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใ“ใฎๆฉŸ่ƒฝใซใ‚ˆใ‚Šใ€ๆฌกใฎใ‚ˆใ†ใชใ‚ณใ‚นใƒˆใ‚’ใ‹ใ‘ใฆใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ไป–ใฎใƒ—ใƒญใ‚ปใ‚นใŒไฝฟ็”จใงใใ‚‹ใƒกใƒขใƒชใŒๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ใƒ”ใƒณ็•™ใ‚ใ•ใ‚ŒใŸใƒกใƒขใƒชใฏใ€ใใ‚Œใ‚’่ฆๆฑ‚ใ—ใŸ็‰นๅฎšใฎใƒ—ใƒญใ‚ปใ‚นใฎใŸใ‚ใซ็ขบไฟใ•ใ‚Œใพใ™ใ€‚ ้€šๅธธใ€้€šๅธธใฎ CPU ใƒกใƒขใƒชใ‚ˆใ‚Šใ‚‚ใฏใ‚‹ใ‹ใซ้ซ˜้€Ÿใซใ‚ขใ‚ฏใ‚ปใ‚นใ•ใ‚Œใพใ™ใ€‚ **ๆ€ง่ƒฝ่ชฟๆ•ด๏ผš** - `stage3_max_live_parameters`: `1e9` - `stage3_max_reuse_distance`: `1e9` OOM ใซ้”ใ—ใŸๅ ดๅˆใฏใ€ใ€Œstage3_max_live_parametersใ€ใจใ€Œstage3_max_reuse_ distanceใ€ใ‚’ๆธ›ใ‚‰ใ—ใพใ™ใ€‚ๅฝฑ้Ÿฟใฏๆœ€ๅฐ้™ใซๆŠ‘ใˆใ‚‰ใ‚Œใ‚‹ใฏใšใงใ™ ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ–ๅŒ–ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅฎŸ่กŒใ—ใชใ„้™ใ‚Šใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซๅฝฑ้Ÿฟใ—ใพใ™ใ€‚ `1e9`ใฏ็ด„ 2GB ใ‚’ๆถˆ่ฒปใ—ใพใ™ใ€‚่จ˜ๆ†ถใ‚’ๅ…ฑๆœ‰ใ—ใฆใ„ใ‚‹ใฎใฏใ€ `stage3_max_live_parameters` ใจ `stage3_max_reuse_distance` ใชใฎใงใ€ๅŠ ็ฎ—ใ•ใ‚Œใ‚‹ใ‚‚ใฎใงใฏใชใใ€ๅˆ่จˆใง 2GB ใซใชใ‚Šใพใ™ใ€‚ `stage3_max_live_parameters` ใฏใ€็‰นๅฎšใฎๆ™‚็‚นใง GPU ไธŠใซไฟๆŒใ™ใ‚‹ๅฎŒๅ…จใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆ•ฐใฎไธŠ้™ใงใ™ใ€‚ ๆ™‚้–“ใ€‚ ใ€Œๅ†ๅˆฉ็”จ่ท้›ขใ€ใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฐ†ๆฅใ„ใคๅ†ใณไฝฟ็”จใ•ใ‚Œใ‚‹ใ‹ใ‚’ๅˆคๆ–ญใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ™ใ‚‹ๆŒ‡ๆจ™ใงใ™ใ€‚ `stage3_max_reuse_ distance`ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’็ ดๆฃ„ใ™ใ‚‹ใ‹ไฟๆŒใ™ใ‚‹ใ‹ใ‚’ๆฑบๅฎšใ—ใพใ™ใ€‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒ ่ฟ‘ใ„ๅฐ†ๆฅใซๅ†ใณไฝฟ็”จใ•ใ‚Œใ‚‹ไบˆๅฎš (`stage3_max_reuse_distance`ๆœชๆบ€) ใชใฎใงใ€้€šไฟกใ‚’ๆธ›ใ‚‰ใ™ใŸใ‚ใซไฟๆŒใ—ใพใ™ใ€‚ ใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใ€‚ใ“ใ‚Œใฏใ€ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๆœ‰ๅŠนใซใ—ใฆใ„ใ‚‹ๅ ดๅˆใซ้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ๅ†่จˆ็ฎ—ใŒ่กŒใ‚ใ‚Œใ€ backward ใฏๅ˜ไธ€ใƒฌใ‚คใƒคใƒผ็ฒ’ๅบฆใ‚’ๆธกใ—ใ€ๅพŒๆ–นๅ†่จˆ็ฎ—ใพใงใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅ‰ๆ–นๅ†่จˆ็ฎ—ใซไฟๆŒใ—ใŸใ„ใจ่€ƒใˆใฆใ„ใพใ™ใ€‚ ๆฌกใฎๆง‹ๆˆๅ€คใฏใ€ใƒขใƒ‡ใƒซใฎ้ž่กจ็คบใ‚ตใ‚คใ‚บใซใ‚ˆใฃใฆ็•ฐใชใ‚Šใพใ™ใ€‚ - `reduce_bucket_size`: `hidden_size*hidden_size` - `stage3_prefetch_bucket_size`: `0.9 * hidden_size * hidden_size` - `stage3_param_persistence_threshold`: `10 * hidden_size` ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใ‚‰ใฎๅ€คใ‚’ `auto` ใซ่จญๅฎšใ™ใ‚‹ใจใ€[`Trainer`] ใŒๆŽจๅฅจใ•ใ‚Œใ‚‹ๅ€คใ‚’่‡ชๅ‹•็š„ใซๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ ไพกๅ€ค่ฆณใ€‚ใŸใ ใ—ใ€ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใ‚‰ใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ `stage3_gather_16bit_weights_on_model_save` ใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๆ™‚ใซใƒขใƒ‡ใƒซ fp16 ใฎ้‡ใฟ็ตฑๅˆใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ๅคงใใ„ ใƒขใƒ‡ใƒซใจ่ค‡ๆ•ฐใฎ GPU ใฎๅ ดๅˆใ€ใ“ใ‚Œใฏใƒกใƒขใƒชใจ้€Ÿๅบฆใฎไธกๆ–นใฎ็‚นใง้ซ˜ไพกใชๆ“ไฝœใงใ™ใ€‚็พๅœจๅฟ…้ ˆใจใชใฃใฆใ„ใ‚‹ใฎใฏใ€ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ไบˆๅฎšใงใ™ใ€‚ใ“ใฎๅˆถ้™ใ‚’ๅ–ใ‚Š้™คใใ€ใ‚ˆใ‚Šไพฟๅˆฉใซใ™ใ‚‹ไปŠๅพŒใฎใ‚ขใƒƒใƒ—ใƒ‡ใƒผใƒˆใซๆณจ็›ฎใ—ใฆใใ ใ•ใ„ใ€‚ ใƒ•ใƒฌใ‚ญใ‚ทใƒ–ใƒซใ€‚ ZeRO-2 ๆง‹ๆˆใ‹ใ‚‰็งป่กŒใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€`allgather_partitions`ใ€`allgather_bucket_size`ใ€ใŠใ‚ˆใณ `reduce_scatter`่จญๅฎšใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏ ZeRO-3 ใงใฏไฝฟ็”จใ•ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใ‚‰ใ‚’่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซไฟๅญ˜ใ—ใฆใŠใใจใ€ ็„ก่ฆ–ใ•ใ‚Œใ‚‹ใ€‚ - `sub_group_size`: `1e9` `sub_group_size` ใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎใ‚นใƒ†ใƒƒใƒ—ไธญใซใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒๆ›ดๆ–ฐใ•ใ‚Œใ‚‹็ฒ’ๅบฆใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ `sub_group_size` ใฎใƒใ‚ฑใƒƒใƒˆใซใ‚ฐใƒซใƒผใƒ—ๅŒ–ใ•ใ‚Œใ€ๅ„ใƒใ‚ฑใƒƒใƒˆใฏไธ€ๅบฆใซ 1 ใคใšใคๆ›ดๆ–ฐใ•ใ‚Œใพใ™ใ€‚ NVMeใ‚ชใƒ•ใƒญใƒผใƒ‰ใงไฝฟ็”จใ™ใ‚‹ๅ ดๅˆ ใ—ใŸใŒใฃใฆใ€ZeRO-Infinity ใฎ `sub_group_size`ใฏใ€ใƒขใƒ‡ใƒซใฎ็Šถๆ…‹ใŒ CPU ใซๅ‡บๅ…ฅใ‚Šใ™ใ‚‹็ฒ’ๅบฆใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚นใƒ†ใƒƒใƒ—ไธญใซ NVMe ใ‹ใ‚‰ใƒกใƒขใƒชใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€้žๅธธใซๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎ CPU ใƒกใƒขใƒชไธ่ถณใŒ้˜ฒๆญขใ•ใ‚Œใพใ™ใ€‚ NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใฏใ€`sub_group_size`ใ‚’ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใฎ *1e9* ใฎใพใพใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ ๆฌกใฎๅ ดๅˆใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€ค: 1. ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใ‚นใƒ†ใƒƒใƒ—ไธญใซ OOM ใŒ็™บ็”Ÿใ™ใ‚‹: `sub_group_size` ใ‚’ๆธ›ใ‚‰ใ—ใฆใ€ไธ€ๆ™‚ใƒใƒƒใƒ•ใ‚กใƒผใฎใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ๅ‰Šๆธ›ใ—ใพใ™ใ€‚ 2. ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใ‚นใƒ†ใƒƒใƒ—ใซๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใพใ™ใ€‚`sub_group_size`ใ‚’ๅข—ใ‚„ใ—ใฆใ€ๅธฏๅŸŸๅน…ใฎไฝฟ็”จ็އใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ใ€‚ ใƒ‡ใƒผใ‚ฟใƒใƒƒใƒ•ใ‚กใฎๅข—ๅŠ ใ€‚ #### ZeRO-0 Config ใ‚นใƒ†ใƒผใ‚ธ 0 ใจ 1 ใฏใ‚ใฃใŸใซไฝฟ็”จใ•ใ‚Œใชใ„ใŸใ‚ใ€ๆœ€ๅพŒใซใƒชใ‚นใƒˆใ—ใฆใ„ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ ใ‚นใƒ†ใƒผใ‚ธ 0 ใงใฏใ€ใ™ในใฆใฎใ‚ฟใ‚คใƒ—ใฎใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’็„กๅŠนใซใ—ใ€DDP ใจใ—ใฆ DeepSpeed ใฎใฟใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใงใ‚ชใƒณใซใงใใพใ™ใ€‚ ```json { "zero_optimization": { "stage": 0 } } ``` ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ไป–ใซไฝ•ใ‚‚ๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใชใใ€ๅŸบๆœฌ็š„ใซ ZeRO ใŒ็„กๅŠนใซใชใ‚Šใพใ™ใ€‚ #### ZeRO-1 Config ใ‚นใƒ†ใƒผใ‚ธ 1 ใฏใ€ใ‚นใƒ†ใƒผใ‚ธ 2 ใ‹ใ‚‰ใ‚ฐใƒฉใƒ‡ใƒผใ‚ทใƒงใƒณ ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’้™คใ„ใŸใ‚‚ใฎใงใ™ใ€‚ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใ‚’ใ‚ทใƒฃใƒผใƒ‰ๅŒ–ใ™ใ‚‹ใ ใ‘ใงใ€ๅ‡ฆ็†ใ‚’ๅฐ‘ใ—้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซใ„ใคใงใ‚‚่ฉฆใ™ใ“ใจใŒใงใใพใ™ใ€‚ ```json { "zero_optimization": { "stage": 1 } } ``` <a id='deepspeed-nvme'></a> ### NVMe Support ZeRO-Infinity ใฏใ€GPU ใจ CPU ใƒกใƒขใƒชใ‚’ NVMe ใƒกใƒขใƒชใงๆ‹กๅผตใ™ใ‚‹ใ“ใจใงใ€้žๅธธใซๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ใŠใ‹ใ’ใง ใ‚นใƒžใƒผใƒˆ ใƒ‘ใƒผใƒ†ใ‚ฃใ‚ทใƒงใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณใ‚ฟใ‚คใƒชใƒณใ‚ฐ ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใงใฏใ€ๅ„ GPU ใŒ้žๅธธใซๅฐ‘้‡ใฎใƒ‡ใƒผใ‚ฟใ‚’้€ๅ—ไฟกใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใ‚ชใƒ•ใƒญใƒผใƒ‰ใซใ‚ˆใ‚Šใ€ๆœ€ๆ–ฐใฎ NVMe ใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅˆฉ็”จใงใใ‚‹ๅˆ่จˆใƒกใƒขใƒช ใƒ—ใƒผใƒซใ‚’ใ•ใ‚‰ใซๅคงใใใ™ใ‚‹ใฎใซ้ฉใ—ใฆใ„ใ‚‹ใ“ใจใŒๅˆคๆ˜Žใ—ใพใ—ใŸใ€‚ ใƒ—ใƒญใ‚ปใ‚นใ€‚ ZeRO-Infinity ใซใฏใ€ZeRO-3 ใŒๆœ‰ๅŠนใซใชใฃใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ๆฌกใฎ่จญๅฎšไพ‹ใงใฏใ€NVMe ใŒใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใจใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎไธกๆ–นใ‚’ใ‚ชใƒ•ใƒญใƒผใƒ‰ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ ```json { "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "nvme", "nvme_path": "/local_nvme", "pin_memory": true, "buffer_count": 4, "fast_init": false }, "offload_param": { "device": "nvme", "nvme_path": "/local_nvme", "pin_memory": true, "buffer_count": 5, "buffer_size": 1e8, "max_in_cpu": 1e9 }, "aio": { "block_size": 262144, "queue_depth": 32, "thread_count": 1, "single_submit": false, "overlap_events": true }, "overlap_comm": true, "contiguous_gradients": true, "sub_group_size": 1e9, "reduce_bucket_size": "auto", "stage3_prefetch_bucket_size": "auto", "stage3_param_persistence_threshold": "auto", "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true }, } ``` ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใจใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎไธกๆ–นใ‚’ NVMe ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ‹ใ€ใฉใกใ‚‰ใ‹ 1 ใคใ ใ‘ใ‚’ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ‹ใ€ใพใฃใŸใใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใชใ„ใ‹ใ‚’้ธๆŠžใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ๆฌกใฎๅ ดๅˆ ๅˆฉ็”จๅฏ่ƒฝใช CPU ใƒกใƒขใƒชใŒๅคง้‡ใซใ‚ใ‚‹ๅ ดๅˆใฏใ€้ซ˜้€Ÿใซใชใ‚‹ใŸใ‚ใ€ๅฟ…ใš CPU ใƒกใƒขใƒชใฎใฟใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใฆใใ ใ•ใ„ (ใƒ’ใƒณใƒˆ: *"device": "CPU"*)ใ€‚ [ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹](https://www.deepspeed.ai/docs/config-json/#optimizer-offloading) ใจ [ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผ](https://www.deepspeed.ai/docs/config-json/#parameter-offloading)ใ€‚ `nvme_path`ใŒๅฎŸ้š›ใซ NVMe ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚NVMe ใฏ้€šๅธธใฎใƒใƒผใƒ‰ใƒ‰ใƒฉใ‚คใƒ–ใพใŸใฏ SSD ใงๅ‹•ไฝœใ—ใพใ™ใŒใ€ ใฏใ‚‹ใ‹ใซ้…ใใชใ‚Šใพใ™ใ€‚้ซ˜้€Ÿใ‚นใ‚ฑใƒผใƒฉใƒ–ใƒซใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€ๆœ€ๆ–ฐใฎ NVMe ่ปข้€้€Ÿๅบฆใ‚’ๅฟต้ ญใซ็ฝฎใ„ใฆ่จญ่จˆใ•ใ‚Œใพใ—ใŸ (ใ“ใฎๆ™‚็‚นใงใฏ ๆ›ธใ่พผใฟใงใฏใ€่ชญใฟๅ–ใ‚Šๆœ€ๅคง 3.5 GB/็ง’ใ€ๆ›ธใ่พผใฟๆœ€ๅคง 3 GB/็ง’ใฎใƒ”ใƒผใ‚ฏ้€ŸๅบฆใŒๅพ—ใ‚‰ใ‚Œใพใ™)ใ€‚ ๆœ€้ฉใช`aio`ๆง‹ๆˆใƒ–ใƒญใƒƒใ‚ฏใ‚’่ฆ‹ใคใ‘ใ‚‹ใซใฏใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จญๅฎšใงใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ [ใ“ใ“ใง่ชฌๆ˜Ž](https://github.com/microsoft/DeepSpeed/issues/998)ใ€‚ <a id='deepspeed-zero2-zero3-performance'></a> #### ZeRO-2 vs ZeRO-3 Performance ZeRO-3 ใฏใ€ไป–ใฎใ™ในใฆใŒๅŒใ˜ใ‚ˆใ†ใซๆง‹ๆˆใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ZeRO-2 ใ‚ˆใ‚Šใ‚‚้…ใใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ๅ‰่€…ใฏๅŽ้›†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ ZeRO-2 ใฎๆฉŸ่ƒฝใซๅŠ ใˆใฆใƒขใƒ‡ใƒซใฎ้‡ใฟไป˜ใ‘ใ‚’่กŒใ„ใพใ™ใ€‚ ZeRO-2 ใŒใƒ‹ใƒผใ‚บใ‚’ๆบ€ใŸใ—ใ€ๆ•ฐๅ€‹ใฎ GPU ใ‚’่ถ…ใˆใฆๆ‹กๅผตใ™ใ‚‹ๅฟ…่ฆใŒใชใ„ๅ ดๅˆ ใใ†ใ™ใ‚Œใฐใ€ใใ‚Œใซๅ›บๅŸทใ™ใ‚‹ใ“ใจใ‚’้ธๆŠžใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ZeRO-3 ใซใ‚ˆใ‚Šใ€ใฏใ‚‹ใ‹ใซ้ซ˜ใ„ใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃๅฎน้‡ใŒๅฏ่ƒฝใซใชใ‚‹ใ“ใจใ‚’็†่งฃใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ ใ‚นใƒ”ใƒผใƒ‰ใ‚’็Š ็‰ฒใซใ—ใฆใ€‚ ZeRO-3 ใฎๆง‹ๆˆใ‚’่ชฟๆ•ดใ—ใฆใ€ZeRO-2 ใซ่ฟ‘ใฅใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ - `stage3_param_persistence_threshold` ใ‚’้žๅธธใซๅคงใใชๆ•ฐๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€`6 * hidden_โ€‹โ€‹size * hidden_โ€‹โ€‹size` ใฎใ‚ˆใ†ใซใ€ๆœ€ๅคงโ€‹โ€‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚ˆใ‚Šใ‚‚ๅคงใใใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒ GPU ใซไฟๆŒใ•ใ‚Œใพใ™ใ€‚ - ZeRO-2 ใซใฏใใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใŒใชใ„ใŸใ‚ใ€`offload_params` ใ‚’ใ‚ชใƒ•ใซใ—ใพใ™ใ€‚ ๅค‰ๆ›ดใ—ใชใใฆใ‚‚ใ€`offload_params`ใ‚’ใ‚ชใƒ•ใซใ™ใ‚‹ใ ใ‘ใงใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅคงๅน…ใซๅ‘ไธŠใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ `stage3_param_persistence_threshold`ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใ‚‰ใฎๅค‰ๆ›ดใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใซๅฝฑ้Ÿฟใ—ใพใ™ใ€‚ใใ‚Œใง ใ“ใ‚Œใ‚‰ใฏใ€ใƒ‹ใƒผใ‚บใซๅฟœใ˜ใฆใ€ใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใจๅผ•ใๆ›ใˆใซ้€Ÿๅบฆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ <a id='deepspeed-zero2-example'></a> #### ZeRO-2 Example ไปฅไธ‹ใฏใ€ๅฎŒๅ…จใช ZeRO-2 ่‡ชๅ‹•ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ `ds_config_zero2.json` ใงใ™ใ€‚ ```json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } }, "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 2e8, "contiguous_gradients": true }, "gradient_accumulation_steps": "auto", "gradient_clipping": "auto", "steps_per_print": 2000, "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto", "wall_clock_breakdown": false } ``` ไปฅไธ‹ใฏใ€ๆ‰‹ๅ‹•ใง่จญๅฎšใ•ใ‚ŒใŸๅฎŒๅ…จใช ZeRO-2 ใฎใ™ในใฆใŒๆœ‰ๅŠนใชๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใ™ใ€‚ใ“ใ“ใงใฏไธปใซใ€ๅ…ธๅž‹็š„ใชใ‚‚ใฎใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™ใ€‚ ๅ€คใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใŒใ€่ค‡ๆ•ฐใฎ`auto`่จญๅฎšใŒๅซใพใ‚Œใ‚‹ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ ```json { "fp16": { "enabled": true, "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": 3e-5, "betas": [0.8, 0.999], "eps": 1e-8, "weight_decay": 3e-7 } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": 0, "warmup_max_lr": 3e-5, "warmup_num_steps": 500 } }, "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 2e8, "contiguous_gradients": true }, "steps_per_print": 2000, "wall_clock_breakdown": false } ``` <a id='deepspeed-zero3-example'></a> #### ZeRO-3 Example ไปฅไธ‹ใฏใ€ๅฎŒๅ…จใช ZeRO-3 ่‡ชๅ‹•ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ`ds_config_zero3.json`ใงใ™ใ€‚ ```json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } }, "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "offload_param": { "device": "cpu", "pin_memory": true }, "overlap_comm": true, "contiguous_gradients": true, "sub_group_size": 1e9, "reduce_bucket_size": "auto", "stage3_prefetch_bucket_size": "auto", "stage3_param_persistence_threshold": "auto", "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true }, "gradient_accumulation_steps": "auto", "gradient_clipping": "auto", "steps_per_print": 2000, "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto", "wall_clock_breakdown": false } ``` ไปฅไธ‹ใฏใ€ๆ‰‹ๅ‹•ใง่จญๅฎšใ•ใ‚ŒใŸๅฎŒๅ…จใช ZeRO-3 ใฎใ™ในใฆใŒๆœ‰ๅŠนใชๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใ™ใ€‚ใ“ใ“ใงใฏไธปใซใ€ๅ…ธๅž‹็š„ใชใ‚‚ใฎใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™ใ€‚ ๅ€คใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใŒใ€่ค‡ๆ•ฐใฎ`auto`่จญๅฎšใŒๅซใพใ‚Œใ‚‹ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ ```json { "fp16": { "enabled": true, "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": 3e-5, "betas": [0.8, 0.999], "eps": 1e-8, "weight_decay": 3e-7 } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": 0, "warmup_max_lr": 3e-5, "warmup_num_steps": 500 } }, "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "offload_param": { "device": "cpu", "pin_memory": true }, "overlap_comm": true, "contiguous_gradients": true, "sub_group_size": 1e9, "reduce_bucket_size": 1e6, "stage3_prefetch_bucket_size": 0.94e6, "stage3_param_persistence_threshold": 1e4, "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true }, "steps_per_print": 2000, "wall_clock_breakdown": false } ``` #### How to Choose Which ZeRO Stage and Offloads To Use For Best Performance ใ“ใ‚Œใงใ€ใ•ใพใ–ใพใชๆฎต้šŽใŒใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ—ใŸใ€‚ใฉใกใ‚‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’ใฉใฎใ‚ˆใ†ใซๆฑบๅฎšใ™ใ‚Œใฐใ‚ˆใ„ใงใ—ใ‚‡ใ†ใ‹?ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใ“ใฎ่ณชๅ•ใซ็ญ”ใˆใฆใ„ใใพใ™ใ€‚ ไธ€่ˆฌใซใ€ๆฌกใฎใ“ใจใŒๅฝ“ใฆใฏใพใ‚Šใพใ™ใ€‚ - ้€Ÿๅบฆใฎ็‚น๏ผˆๅทฆใฎๆ–นใŒๅณใ‚ˆใ‚Š้€Ÿใ„๏ผ‰ ใ‚นใƒ†ใƒผใ‚ธ 0 (DDP) > ใ‚นใƒ†ใƒผใ‚ธ 1 > ใ‚นใƒ†ใƒผใ‚ธ 2 > ใ‚นใƒ†ใƒผใ‚ธ 2 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ > ใ‚นใƒ†ใƒผใ‚ธ 3 > ใ‚นใƒ†ใƒผใ‚ธ 3 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ - GPU ใƒกใƒขใƒชใฎไฝฟ็”จ็Šถๆณ (ๅณใฏๅทฆใ‚ˆใ‚Šใ‚‚ GPU ใƒกใƒขใƒชๅŠน็އใŒ้ซ˜ใ„) ใ‚นใƒ†ใƒผใ‚ธ 0 (DDP) < ใ‚นใƒ†ใƒผใ‚ธ 1 < ใ‚นใƒ†ใƒผใ‚ธ 2 < ใ‚นใƒ†ใƒผใ‚ธ 2 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ < ใ‚นใƒ†ใƒผใ‚ธ 3 < ใ‚นใƒ†ใƒผใ‚ธ 3 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ ใ—ใŸใŒใฃใฆใ€ๆœ€ๅฐ้™ใฎๆ•ฐใฎ GPU ใซๅŽใพใ‚ŠใชใŒใ‚‰ๆœ€้€ŸใฎๅฎŸ่กŒใ‚’ๅฎŸ็พใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎใƒ—ใƒญใ‚ปใ‚นใซๅพ“ใ†ใ“ใจใŒใงใใพใ™ใ€‚ๆœ€ใ‚‚้€Ÿใ„ใ‚ขใƒ—ใƒญใƒผใƒใ‹ใ‚‰้–‹ๅง‹ใ—ใ€GPU OOM ใซ้™ฅใฃใŸๅ ดๅˆใฏใ€ๆฌกใซ้…ใ„ใ‚ขใƒ—ใƒญใƒผใƒใซ้€ฒใฟใพใ™ใŒใ€ใ“ใ‚Œใซใ‚ˆใ‚Šไฝฟ็”จใ•ใ‚Œใ‚‹ GPU ใƒกใƒขใƒชใŒๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ใชใฉใชใฉใ€‚ ใพใšใ€ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ 1 ใซ่จญๅฎšใ—ใพใ™ (ๅฟ…่ฆใชๆœ‰ๅŠนใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใซๅฏพใ—ใฆใ€ใ„ใคใงใ‚‚ๅ‹พ้…็ดฏ็ฉใ‚’ไฝฟ็”จใงใใพใ™)ใ€‚ 1. `--gradient_checkpointing 1` (HF Trainer) ใพใŸใฏ็›ดๆŽฅ `model.gradient_checkpointing_enable()` ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ - OOM ใฎๅ ดๅˆ 2. ๆœ€ๅˆใซ ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 ใ‚’่ฉฆใ—ใฆใใ ใ•ใ„ใ€‚ OOMใฎๅ ดๅˆ 3. ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 + `offload_optimizer` ใ‚’่ฉฆใ—ใพใ™ - OOM ใฎๅ ดๅˆ 4. ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ใซๅˆ‡ใ‚Šๆ›ฟใˆใ‚‹ - OOM ใฎๅ ดๅˆ 5. `cpu` ใซๅฏพใ—ใฆ `offload_param` ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ - OOM ใฎๅ ดๅˆ 6. OOM ใฎๅ ดๅˆใฏใ€`cpu`ใซๅฏพใ—ใฆ`offload_optimizer`ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ 7. ใใ‚Œใงใ‚‚ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บ 1 ใซ้ฉๅˆใ—ใชใ„ๅ ดๅˆใฏใ€ใพใšใ•ใพใ–ใพใชใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใ‚’็ขบ่ชใ—ใ€ๅฏ่ƒฝใงใ‚ใ‚Œใฐๅ€คใ‚’ไธ‹ใ’ใพใ™ใ€‚ใŸใจใˆใฐใ€`generate`ใ‚’ไฝฟ็”จใ—ใ€ๅบƒใ„ๆคœ็ดขใƒ“ใƒผใƒ ใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใฏใ€ๅคง้‡ใฎใƒกใƒขใƒชใ‚’ๆถˆ่ฒปใ™ใ‚‹ใŸใ‚ใ€ๆคœ็ดขใƒ“ใƒผใƒ ใ‚’็‹ญใใ—ใพใ™ใ€‚ 8. fp32 ใงใฏๅฟ…ใšๆททๅˆๅŠ็ฒพๅบฆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใคใพใ‚Šใ€Ampere ไปฅไธŠใฎ GPU ใงใฏ bf16ใ€ๅคใ„ GPU ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงใฏ fp16 ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ 9. ใใ‚Œใงใ‚‚ OOM ใ‚’่กŒใ†ๅ ดๅˆใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ‹ใ€ZeRO-Infinity ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใคใพใ‚Šใ€ใ‚ชใƒ•ใƒญใƒผใƒ‰ `offload_param` ใจ `offload_optimizer` ใ‚’ `nvme` ใซๅˆ‡ใ‚Šๆ›ฟใˆใพใ™ใ€‚้žๅธธใซ้ซ˜้€Ÿใช nvme ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚้€ธ่ฉฑใจใ—ใฆใ€ZeRO-Infinity ใ‚’ไฝฟ็”จใ—ใฆๅฐใ•ใช GPU ใง BLOOM-176B ใ‚’ๆŽจ่ซ–ใ™ใ‚‹ใ“ใจใŒใงใใพใ—ใŸใŒใ€้žๅธธใซ้…ใ‹ใฃใŸใงใ™ใ€‚ใงใ‚‚ใ€ใ†ใพใใ„ใใพใ—ใŸ๏ผ ใ‚‚ใกใ‚ใ‚“ใ€ๆœ€ใ‚‚ GPU ใƒกใƒขใƒชๅŠน็އใฎ้ซ˜ใ„ๆง‹ๆˆใ‹ใ‚‰ๅง‹ใ‚ใฆใ€ๅพŒใ‹ใ‚‰้€†ใซ้€ฒใ‚€ใ“ใจใงใ€ใ“ใ‚Œใ‚‰ใฎๆ‰‹้ †ใ‚’้€†ใซๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ‚ใ‚‹ใ„ใฏไบŒ็ญ‰ๅˆ†ใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ OOM ใ‚’ๅผ•ใ่ตทใ“ใ•ใชใ„ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บ 1 ใ‚’ๅ–ๅพ—ใ—ใŸใ‚‰ใ€ๅฎŸๅŠนใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๆธฌๅฎšใ—ใพใ™ใ€‚ ๆฌกใซใ€ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ใงใใ‚‹ใ ใ‘ๅคงใใใ—ใฆใฟใพใ™ใ€‚ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใŒๅคงใใ„ใปใฉใ€ไน—็ฎ—ใ™ใ‚‹่กŒๅˆ—ใŒๅทจๅคงใชๅ ดๅˆใซ GPU ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๆœ€้ซ˜ใซใชใ‚‹ใŸใ‚ใ€GPU ใฎๅŠน็އใŒๅ‘ไธŠใ—ใพใ™ใ€‚ ใ“ใ“ใงใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๆœ€้ฉๅŒ–ใ‚ฒใƒผใƒ ใŒๅง‹ใพใ‚Šใพใ™ใ€‚ไธ€้ƒจใฎใ‚ชใƒ•ใƒญใƒผใƒ‰ๆฉŸ่ƒฝใ‚’ใ‚ชใƒ•ใซใ™ใ‚‹ใ‹ใ€ZeRO ๆฎต้šŽใงใ‚นใƒ†ใƒƒใƒ—ใƒ€ใ‚ฆใƒณใ—ใฆใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ๅข—ๆธ›ใ—ใฆใ€ๅฎŸๅŠนใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๅ†ๅบฆๆธฌๅฎšใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๆบ€่ถณใ™ใ‚‹ใพใงๆด—ใ„ๆตใ—ใ€็นฐใ‚Š่ฟ”ใ—ใพใ™ใ€‚ ๆฐธ้ ใซใ“ใ‚Œใซ่ฒปใ‚„ใ™ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€3 ใ‹ๆœˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใซ้–ขใ—ใฆๆœ€ใ‚‚ๅŠนๆžœ็š„ใช่จญๅฎšใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซๆ•ฐๆ—ฅใ‹ใ‘ใฆใใ ใ•ใ„ใ€‚ใใฎใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใ‚ณใ‚นใƒˆใŒๆœ€ๅฐ้™ใซใชใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ˆใ‚Šๆ—ฉใๅฎŒไบ†ใงใใพใ™ใ€‚็พๅœจใฎ็›ฎใพใใ‚‹ใ—ใๅค‰ๅŒ–ใ™ใ‚‹ ML ใฎไธ–็•Œใงใฏใ€ไฝ•ใ‹ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใฎใซใ•ใ‚‰ใซ 1 ใ‹ๆœˆใ‹ใ‹ใ‚‹ๅ ดๅˆใ€็ตถๅฅฝใฎๆฉŸไผšใ‚’้€ƒใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใฏ็งใŒๆ„่ฆ‹ใ‚’ๅ…ฑๆœ‰ใ—ใฆใ„ใ‚‹ใ ใ‘ใงใ‚ใ‚Šใ€ๆฑบใ—ใฆใ‚ใชใŸใ‚’ๆ€ฅใ‹ใใ†ใจใ—ใฆใ„ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ BLOOM-176B ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ€ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใซ 2 ๆ—ฅ้–“่ฒปใ‚„ใ—ใ€ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ 90 TFLOP ใ‹ใ‚‰ 150 TFLOP ใซๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ—ใŸใ€‚ใ“ใฎๅ–ใ‚Š็ต„ใฟใซใ‚ˆใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚้–“ใ‚’ 1 ใ‹ๆœˆไปฅไธŠ็ฏ€็ด„ใงใใพใ—ใŸใ€‚ ใ“ใ‚Œใ‚‰ใฎใƒกใƒขใฏไธปใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒขใƒผใƒ‰็”จใซๆ›ธใ‹ใ‚ŒใŸใ‚‚ใฎใงใ™ใŒใ€ใปใจใ‚“ใฉใฎๅ ดๅˆใฏๆŽจ่ซ–ใซใ‚‚้ฉ็”จใ•ใ‚Œใ‚‹ใฏใšใงใ™ใ€‚ใŸใจใˆใฐใ€ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใฎใฟๅฝน็ซ‹ใคใŸใ‚ใ€ๆŽจ่ซ–ไธญใฏไฝ•ใ‚‚่กŒใ‚ใ‚Œใพใ›ใ‚“ใ€‚ใ•ใ‚‰ใซใ€ใƒžใƒซใƒ GPU ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใฆใ„ใฆใ€[DeepSpeed-Inference](https://www.deepspeed.ai/tutorials/inference-tutorial/)ใ€[Accelerate](https://ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚น.co/blog/bloom-inference-pytorch-scripts) ใฏๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๆไพ›ใ™ใ‚‹ใฏใšใงใ™ใ€‚ ใใฎไป–ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น้–ข้€ฃใฎ็ฐกๅ˜ใชใƒกใƒข: - ไฝ•ใ‹ใ‚’ๆœ€ๅˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๅธธใซ 16 ใงๅ‰ฒใ‚Šๅˆ‡ใ‚Œใ‚‹ๅฝข็Šถใฎใƒ†ใƒณใ‚ฝใƒซ (้š ใ‚ŒใŸใ‚ตใ‚คใ‚บใชใฉ) ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„ใ€‚ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใซใคใ„ใฆใฏใ€ๅฐ‘ใชใใจใ‚‚ 2 ใงๅ‰ฒใ‚Šๅˆ‡ใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„ใ€‚ GPU ใ‹ใ‚‰ใ•ใ‚‰ใซ้ซ˜ใ„ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅผ•ใๅ‡บใ—ใŸใ„ๅ ดๅˆใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขๅ›บๆœ‰ใฎ [ๆณขใจใ‚ฟใ‚คใƒซใฎ้‡ๅญๅŒ–](https://developer.nvidia.com/blog/optimizing-gpu-performance-tensor-cores/) ใฎๅฏๅˆ†ๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ ### Activation Checkpointing or Gradient Checkpointing ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ๅŒใ˜ๆ–นๆณ•่ซ–ใ‚’ๆŒ‡ใ™ 2 ใคใฎ็•ฐใชใ‚‹็”จ่ชžใงใ™ใ€‚ใจใฆใ‚‚ใ‚„ใ‚„ใ“ใ—ใ„ใงใ™ใŒใ€ใ“ใ‚“ใชๆ„Ÿใ˜ใงใ™ใ€‚ ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€้€Ÿๅบฆใ‚’ GPU ใƒกใƒขใƒชใจๅผ•ใๆ›ใˆใซใงใใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€GPU OOM ใ‚’ๅ…‹ๆœใ—ใŸใ‚Šใ€ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ๅข—ใ‚„ใ™ใ“ใจใŒใงใใ€ๅคšใใฎๅ ดๅˆใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใซใคใชใŒใ‚Šใพใ™ใ€‚ HF Transformers ใƒขใƒ‡ใƒซใฏใ€DeepSpeed ใฎใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซใคใ„ใฆไฝ•ใ‚‚็Ÿฅใ‚‰ใชใ„ใŸใ‚ใ€DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใใฎๆฉŸ่ƒฝใ‚’ๆœ‰ๅŠนใซใ—ใ‚ˆใ†ใจใ—ใฆใ‚‚ใ€ไฝ•ใ‚‚่ตทใ“ใ‚Šใพใ›ใ‚“ใ€‚ ใ—ใŸใŒใฃใฆใ€ใ“ใฎ้žๅธธใซๆœ‰็›ŠใชๆฉŸ่ƒฝใ‚’ๆดป็”จใ™ใ‚‹ใซใฏ 2 ใคใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ 1. HF Transformers ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€`model.gradient_checkpointing_enable()` ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ‹ใ€HF ใƒˆใƒฌใƒผใƒŠใƒผใง `--gradient_checkpointing` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ“ใ‚ŒใŒ่‡ชๅ‹•็š„ใซๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใใ“ใงไฝฟใ‚ใ‚Œใ‚‹ใฎใŒ `torch.utils.checkpoint` ใงใ™ใ€‚ 2. ็‹ฌ่‡ชใฎใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใ€DeepSpeed ใฎใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€[ใใ“ใง่ฆๅฎšใ•ใ‚Œใฆใ„ใ‚‹ API](https://deepspeed.readthedocs.io/en/latest/activation-checkpointing.html) ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ HF Transformers ใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ณใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€`torch.utils.checkpoint` ใ‚’ DeepSpeed ใฎ API ใซ็ฝฎใๆ›ใˆใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ๅพŒ่€…ใฏใ€้ †ๆ–นๅ‘ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’ๅ†่จˆ็ฎ—ใ™ใ‚‹ไปฃใ‚ใ‚Šใซ CPU ใƒกใƒขใƒชใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใงใใ‚‹ใŸใ‚ใ€ใ‚ˆใ‚ŠๆŸ”่ปŸใงใ™ใ€‚ ### Optimizer and Scheduler `offload_optimizer`ใ‚’ๆœ‰ๅŠนใซใ—ใชใ„้™ใ‚Šใ€DeepSpeed ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใจ HuggingFace ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จโ€‹โ€‹ใงใใพใ™ใ€‚ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ (HuggingFace ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใจ DeepSpeed ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็ต„ใฟๅˆใ‚ใ›ใ‚’้™คใ): | Combos | HF Scheduler | DS Scheduler | |:-------------|:-------------|:-------------| | HF Optimizer | Yes | Yes | | DS Optimizer | No | Yes | `offload_optimizer`ใŒๆœ‰ๅŠนใชๅ ดๅˆใ€CPU ใจ GPU ๅฎŸ่ฃ… (LAMB ใ‚’้™คใ)ใ€‚ <a id='deepspeed-optimizer'></a> #### Optimizer DeepSpeed ใฎไธปใชใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฏใ€Adamใ€AdamWใ€OneBitAdamใ€Lamb ใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏ ZeRO ใงๅพนๅบ•็š„ใซใƒ†ใ‚นใƒˆใ•ใ‚ŒใฆใŠใ‚Šใ€ ใ—ใŸใŒใฃใฆใ€ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ไป–ใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ใ€Œtorchใ€ใ‹ใ‚‰ใ‚คใƒณใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใฏใงใใพใ™ใ€‚ๅฎŒๅ…จใชใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏ [ใ“ใกใ‚‰](https://www.deepspeed.ai/docs/config-json/#optimizer-parameters) ใซใ‚ใ‚Šใพใ™ใ€‚ ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใง `optimizer` ใ‚จใƒณใƒˆใƒชใ‚’่จญๅฎšใ—ใชใ„ๅ ดๅˆใ€[`Trainer`] ใฏ ่‡ชๅ‹•็š„ใซ`AdamW`ใซ่จญๅฎšใ•ใ‚Œใ€ๆŒ‡ๅฎšใ•ใ‚ŒใŸๅ€คใพใŸใฏๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ ๅผ•ๆ•ฐ: `--learning_rate`ใ€`--adam_beta1`ใ€`--adam_beta2`ใ€`--adam_epsilon`ใ€ใŠใ‚ˆใณ `--weight_decay`ใ€‚ ไปฅไธ‹ใฏใ€`AdamW`ใฎ่‡ชๅ‹•ๆง‹ๆˆใ•ใ‚ŒใŸ`optimizer`ใ‚จใƒณใƒˆใƒชใฎไพ‹ใงใ™ใ€‚ ```json { "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } } } ``` ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซใ‚ˆใฃใฆๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซๅ†…ใฎๅ€คใŒ่จญๅฎšใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใฏ 1 ใคใ‚ใ‚‹ใŸใ‚ใงใ™ ๅ€คใฎๆฑบๅฎš็š„ใชใ‚ฝใƒผใ‚นใ‚’ๆไพ›ใ—ใ€ใŸใจใˆใฐๅญฆ็ฟ’็އใŒๆฌกใฎใ‚ˆใ†ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใซใ€่ฆ‹ใคใ‘ใซใใ„ใ‚จใƒฉใƒผใ‚’ๅ›ž้ฟใ—ใพใ™ใ€‚ ใ•ใพใ–ใพใชๅ ดๆ‰€ใงใ•ใพใ–ใพใชไพกๅ€ค่ฆณใ€‚ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใฎใƒซใƒผใƒซใ€‚ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ•ใ‚Œใ‚‹ๅ€คใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ - `lr` ใจ `--learning_rate` ใฎๅ€ค - `betas` ใจ `--adam_beta1 --adam_beta2` ใฎๅ€ค - `eps` ใจ `--adam_epsilon` ใฎๅ€ค - `weight_decay` ใจ `--weight_decay` ใฎๅ€ค ใ—ใŸใŒใฃใฆใ€ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใงๅ…ฑๆœ‰ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ชฟๆ•ดใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚ ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "optimizer": { "type": "AdamW", "params": { "lr": 0.001, "betas": [0.8, 0.999], "eps": 1e-8, "weight_decay": 3e-7 } } } ``` ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ ๆง‹ๆˆใ€‚ ไธŠ่จ˜ใซใƒชใ‚นใƒˆใ•ใ‚Œใฆใ„ใชใ„ๅˆฅใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ใƒˆใƒƒใƒ—ใƒฌใƒ™ใƒซใฎๆง‹ๆˆใซ่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ```json { "zero_allow_untested_optimizer": true } ``` `AdamW`ใจๅŒๆง˜ใซใ€ๅ…ฌๅผใซใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ไป–ใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ๆง‹ๆˆใงใใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏ็•ฐใชใ‚‹่จญๅฎšๅ€คใ‚’ๆŒใคๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ไพ‹ใˆใฐAdam ใฎๅ ดๅˆใฏใ€`weight_decay`ใ‚’`0.01`ไป˜่ฟ‘ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใ•ใ‚‰ใซใ€ใ‚ชใƒ•ใƒญใƒผใƒ‰ใฏใ€Deepspeed ใฎ CPU Adam ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจไฝต็”จใ™ใ‚‹ใจๆœ€ใ‚‚ๅŠนๆžœ็š„ใซๆฉŸ่ƒฝใ—ใพใ™ใ€‚ `deepspeed==0.8.3` ใชใฎใงใ€ใ‚ชใƒ•ใƒญใƒผใƒ‰ใงๅˆฅใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ไปฅไธ‹ใ‚‚่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ```json { "zero_force_ds_cpu_optimizer": false } ``` ๆœ€ไธŠไฝใฎๆง‹ๆˆใซ็งป่กŒใ—ใพใ™ใ€‚ <a id='deepspeed-scheduler'></a> #### Scheduler DeepSpeed ใฏใ€`LRRangeTest`ใ€`OneCycle`ใ€`WarmupLR`ใ€ใŠใ‚ˆใณ`WarmupDecayLR`ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ๅฎŒๅ…จใช ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏ[ใ“ใ“](https://www.deepspeed.ai/docs/config-json/#scheduler-parameters)ใงใ™ใ€‚ ใ“ใ“ใงใฏใ€๐Ÿค— Transformers ใจ DeepSpeed ใฎ้–“ใงใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใŒ้‡่ค‡ใ™ใ‚‹ๅ ดๆ‰€ใ‚’็คบใ—ใพใ™ใ€‚ - `--lr_scheduler_type constant_with_warmup` ็ตŒ็”ฑใฎ `WarmupLR` - `--lr_scheduler_type Linear` ใ‚’ไป‹ใ—ใŸ `WarmupDecayLR`ใ€‚ใ“ใ‚Œใฏ `--lr_scheduler_type` ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ ใ—ใŸใŒใฃใฆใ€ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใ‚’่จญๅฎšใ—ใชใ„ๅ ดๅˆใ€ใ“ใ‚ŒใŒใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง่จญๅฎšใ•ใ‚Œใ‚‹ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใซใชใ‚Šใพใ™ใ€‚ ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใง `scheduler` ใ‚จใƒณใƒˆใƒชใ‚’่จญๅฎšใ—ใชใ„ๅ ดๅˆใ€[`Trainer`] ใฏ `--lr_scheduler_type`ใ€`--learning_rate`ใ€ใŠใ‚ˆใณ `--warmup_steps` ใพใŸใฏ `--warmup_ratio` ใฎๅ€คใ‚’่จญๅฎšใ—ใพใ™ใ€‚ ๐Ÿค— ใใ‚Œใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒใƒผใ‚ธใƒงใƒณใ€‚ ไปฅไธ‹ใฏใ€`WarmupLR`ใฎ่‡ชๅ‹•ๆง‹ๆˆใ•ใ‚ŒใŸ`scheduler`ใ‚จใƒณใƒˆใƒชใฎไพ‹ใงใ™ใ€‚ ```json { "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } } } ``` *"auto"* ใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€[`Trainer`] ๅผ•ๆ•ฐใฏ่จญๅฎšใซๆญฃใ—ใ„ๅ€คใ‚’่จญๅฎšใ—ใพใ™ใ€‚ ใƒ•ใ‚กใ‚คใƒซใ€‚ใ“ใ‚Œใฏใ€ๅ€คใฎๆฑบๅฎš็š„ใชใ‚ฝใƒผใ‚นใŒ 1 ใคใ‚ใ‚‹ใ“ใจใจใ€ใŸใจใˆใฐๆฌกใฎใ‚ˆใ†ใชๅ ดๅˆใซ่ฆ‹ใคใ‘ใซใใ„ใ‚จใƒฉใƒผใ‚’้ฟใ‘ใ‚‹ใŸใ‚ใงใ™ใ€‚ ๅญฆ็ฟ’็އใฏใ€ๅ ดๆ‰€ใ”ใจใซ็•ฐใชใ‚‹ๅ€คใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใฎใƒซใƒผใƒซใ€‚่จญๅฎšใ•ใ‚Œใ‚‹ๅ€คใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ - `warmup_min_lr` ใฎๅ€คใฏ `0` ใงใ™ใ€‚ - `warmup_max_lr` ใจ `--learning_rate` ใฎๅ€คใ€‚ - `warmup_num_steps` ใจ `--warmup_steps` ใฎๅ€ค (ๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆ)ใ€‚ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏ `--warmup_ratio` ใ‚’ไฝฟ็”จใ—ใพใ™ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใƒ†ใƒƒใƒ—ใฎๆ•ฐใ‚’ไน—็ฎ—ใ—ใ€ๅˆ‡ใ‚ŠไธŠใ’ใพใ™ใ€‚ - `total_num_steps` ใซใฏ `--max_steps` ใฎๅ€คใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ‹ใ€ๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏๅฎŸ่กŒๆ™‚ใซ่‡ชๅ‹•็š„ใซๅฐŽๅ‡บใ•ใ‚Œใพใ™ใ€‚ ็’ฐๅขƒใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใ‚คใ‚บใ€ใŠใ‚ˆใณใใฎไป–ใฎใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐ ( `WarmupDecayLR`)ใ€‚ ใ‚‚ใกใ‚ใ‚“ใ€ๆง‹ๆˆๅ€คใฎไธ€้ƒจใพใŸใฏใ™ในใฆใ‚’ๅผ•ใ็ถ™ใ„ใงใ€่‡ชๅˆ†ใง่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": 0, "warmup_max_lr": 0.001, "warmup_num_steps": 1000 } } } ``` ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ ๆง‹ๆˆใ€‚ ใŸใจใˆใฐใ€`WarmupDecayLR`ใฎๅ ดๅˆใฏใ€ๆฌกใฎใ‚จใƒณใƒˆใƒชใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ ```json { "scheduler": { "type": "WarmupDecayLR", "params": { "last_batch_iteration": -1, "total_num_steps": "auto", "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } } } ``` `total_num_steps`ใ€`warmup_max_lr`ใ€`warmup_num_steps`ใ€ใŠใ‚ˆใณ `total_num_steps` ใฏใƒญใƒผใƒ‰ๆ™‚ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ <a id='deepspeed-fp32'></a> ### fp32 Precision Deepspeed ใฏใ€ๅฎŒๅ…จใช fp32 ใจ fp16 ใฎๆททๅˆ็ฒพๅบฆใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ fp16 ๆททๅˆ็ฒพๅบฆใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ๅฟ…่ฆใชใƒกใƒขใƒชใŒๅคงๅน…ใซๅ‰Šๆธ›ใ•ใ‚Œใ€้€ŸๅบฆใŒๅ‘ไธŠใ™ใ‚‹ใŸใ‚ใ€ ไฝฟ็”จใ—ใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใŒใ“ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒขใƒผใƒ‰ใง้ฉๅˆ‡ใซๅ‹•ไฝœใ—ใชใ„ๅ ดๅˆใฏใ€ไฝฟ็”จใ—ใชใ„ๆ–นใŒใ‚ˆใ„ใงใ—ใ‚‡ใ†ใ€‚้€šๅธธใ“ใ‚Œ ใƒขใƒ‡ใƒซใŒ fp16 ๆททๅˆ็ฒพๅบฆใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใซ็™บ็”Ÿใ—ใพใ™ (ใŸใจใˆใฐใ€ใ“ใ‚Œใฏ bf16 ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๅ ดๅˆใซใ‚ˆใ็™บ็”Ÿใ—ใพใ™) ใƒขใƒ‡ใƒซ๏ผ‰ใ€‚ใ“ใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซใงใฏใ€ใ‚ชใƒผใƒใƒผใƒ•ใƒญใƒผใพใŸใฏใ‚ขใƒณใƒ€ใƒผใƒ•ใƒญใƒผใŒ็™บ็”Ÿใ—ใ€`NaN`ๆๅคฑใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚ŒใŒใ‚ใชใŸใฎๅ ดๅˆใฏใ€ไฝฟ็”จใ—ใŸใ„ใจๆ€ใ†ใงใ—ใ‚‡ใ† ๅฎŒๅ…จใช fp32 ใƒขใƒผใƒ‰ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ fp16 ๆททๅˆ็ฒพๅบฆใƒขใƒผใƒ‰ใ‚’ๆฌกใฎใ‚ˆใ†ใซๆ˜Ž็คบ็š„ใซ็„กๅŠนใซใ—ใพใ™ใ€‚ ```json { "fp16": { "enabled": false, } } ``` Ampere ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ ใƒ™ใƒผใ‚นใฎ GPU ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€pytorch ใƒใƒผใ‚ธใƒงใƒณ 1.7 ไปฅ้™ใฏ่‡ชๅ‹•็š„ใซ ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๅˆ‡ใ‚Šๆ›ฟใ‚ใ‚Šใพใ™ใ€‚ ไธ€้ƒจใฎๆ“ไฝœใงใฏใฏใ‚‹ใ‹ใซๅŠน็އ็š„ใช tf32 ๅฝขๅผใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€็ตๆžœใฏไพ็„ถใจใ—ใฆ fp32 ใซใชใ‚Šใพใ™ใ€‚่ฉณ็ดฐใจ ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซใคใ„ใฆใฏใ€[Ampere ใƒ‡ใƒใ‚คใ‚นไธŠใฎ TensorFloat-32(TF32)](https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ๆ–‡ๆ›ธใซใฏไปฅไธ‹ใŒๅซใพใ‚Œใพใ™ ไฝ•ใ‚‰ใ‹ใฎ็†็”ฑใงใ“ใฎ่‡ชๅ‹•ๅค‰ๆ›ใ‚’ไฝฟ็”จใ—ใŸใใชใ„ๅ ดๅˆใฏใ€ใ“ใฎ่‡ชๅ‹•ๅค‰ๆ›ใ‚’็„กๅŠนใซใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ ๐Ÿค— ใƒˆใƒฌใƒผใƒŠใƒผใงใฏใ€`--tf32` ใ‚’ไฝฟ็”จใ—ใฆๆœ‰ๅŠนใซใ™ใ‚‹ใ‹ใ€`--tf32 0` ใพใŸใฏ `--no_tf32` ใ‚’ไฝฟ็”จใ—ใฆ็„กๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€PyTorch ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ <a id='deepspeed-amp'></a> ### Automatic Mixed Precision pytorch ใฎใ‚ˆใ†ใช AMP ใฎๆ–นๆณ•ใพใŸใฏ apex ใฎใ‚ˆใ†ใชๆ–นๆณ•ใง่‡ชๅ‹•ๆททๅˆ็ฒพๅบฆใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ ### fp16 fp16 (float16) ใ‚’่จญๅฎšใ—ใฆ pytorch AMP ใฎใ‚ˆใ†ใชใƒขใƒผใƒ‰ใ‚’่จญๅฎšใ™ใ‚‹ใซใฏ: ```json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 } } ``` [`Trainer`] ใฏใ€ใฎๅ€คใซๅŸบใฅใ„ใฆใใ‚Œใ‚’่‡ชๅ‹•็š„ใซๆœ‰ๅŠนใพใŸใฏ็„กๅŠนใซใ—ใพใ™ใ€‚ `args.fp16_backend`ใ€‚ๆฎ‹ใ‚Šใฎ่จญๅฎšๅ€คใฏใ‚ใชใŸๆฌก็ฌฌใงใ™ใ€‚ ใ“ใฎใƒขใƒผใƒ‰ใฏใ€`--fp16 --fp16_backend amp`ใพใŸใฏ`--fp16_full_eval`ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใŒๆธกใ•ใ‚Œใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ ใ“ใฎใƒขใƒผใƒ‰ใ‚’ๆ˜Ž็คบ็š„ใซๆœ‰ๅŠน/็„กๅŠนใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "fp16": { "enabled": true, "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 } } ``` ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ ๆง‹ๆˆใ€‚ ใ“ใ‚ŒใŒ[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/docs/config-json/#fp16-training-options)ใงใ™ใ€‚ ### BF16 fp16 ใฎไปฃใ‚ใ‚Šใซ bf16 (bfloat16) ใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€ๆฌกใฎๆง‹ๆˆใ‚ปใ‚ฏใ‚ทใƒงใƒณใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ ```json { "bf16": { "enabled": "auto" } } ``` bf16 ใฏ fp32 ใจๅŒใ˜ใƒ€ใ‚คใƒŠใƒŸใƒƒใ‚ฏ ใƒฌใƒณใ‚ธใ‚’ๅ‚™ใˆใฆใ„ใ‚‹ใŸใ‚ใ€ๆๅคฑใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ใ“ใฎใƒขใƒผใƒ‰ใฏใ€`--bf16` ใพใŸใฏ `--bf16_full_eval` ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใŒๆธกใ•ใ‚Œใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ ใ“ใฎใƒขใƒผใƒ‰ใ‚’ๆ˜Ž็คบ็š„ใซๆœ‰ๅŠน/็„กๅŠนใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "bf16": { "enabled": true } } ``` <Tip> `deepspeed==0.6.0`ใฎๆ™‚็‚นใงใฏใ€bf16 ใ‚ตใƒใƒผใƒˆใฏๆ–ฐใ—ใๅฎŸ้จ“็š„ใชใ‚‚ใฎใงใ™ใ€‚ bf16 ใŒๆœ‰ๅŠนใช็Šถๆ…‹ใง [ๅ‹พ้…็ดฏ็ฉ](#gradient-accumulation) ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€bf16 ใงๅ‹พ้…ใŒ็ดฏ็ฉใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๅฝขๅผใฎ็ฒพๅบฆใŒไฝŽใ„ใŸใ‚ใ€ใ“ใ‚ŒใฏๅธŒๆœ›ใฉใŠใ‚Šใงใฏใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ๆๅคฑใฎใ‚ใ‚‹่“„็ฉใซใคใชใŒใ‚Šใพใ™ใ€‚ ใ“ใฎๅ•้กŒใ‚’ไฟฎๆญฃใ—ใ€ใ‚ˆใ‚Š้ซ˜็ฒพๅบฆใฎ `dtype` (fp16 ใพใŸใฏ fp32) ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ๆไพ›ใ™ใ‚‹ใŸใ‚ใฎไฝœๆฅญใŒ่กŒใ‚ใ‚Œใฆใ„ใพใ™ใ€‚ </Tip> ### NCCL Collectives ่จ“็ทดไฝ“ๅˆถใฎ`dtype`ใŒใ‚ใ‚Šใ€ใ•ใพใ–ใพใชๅ‰Šๆธ›ใ‚„ๅŽ้›†/ๅˆ†ๆ•ฃๆ“ไฝœใชใฉใฎใ‚ณใƒŸใƒฅใƒ‹ใ‚ฑใƒผใ‚ทใƒงใƒณ้›†ๅˆไฝ“ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅˆฅใฎ`dtype`ใŒใ‚ใ‚Šใพใ™ใ€‚ ใ™ในใฆใฎๅŽ้›†/ๅˆ†ๆ•ฃๆ“ไฝœใฏใ€ใƒ‡ใƒผใ‚ฟใŒๅซใพใ‚Œใฆใ„ใ‚‹ใฎใจๅŒใ˜ `dtype` ใงๅฎŸ่กŒใ•ใ‚Œใ‚‹ใŸใ‚ใ€bf16 ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไฝ“ๅˆถใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใฏ bf16 ใงๅŽ้›†ใ•ใ‚Œใพใ™ใ€‚ๅŽ้›†ใฏๆๅคฑใฎใชใ„ๆ“ไฝœใงใ™ใ€‚ ใ•ใพใ–ใพใชใƒชใƒ‡ใƒฅใƒผใ‚นๆ“ไฝœใฏ้žๅธธใซๆๅคฑใŒๅคงใใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€่ค‡ๆ•ฐใฎ GPU ้–“ใงๅ‹พ้…ใŒๅนณๅ‡ๅŒ–ใ•ใ‚Œใ‚‹ๅ ดๅˆใ€้€šไฟกใŒ fp16 ใพใŸใฏ bf16 ใง่กŒใ‚ใ‚Œใ‚‹ๅ ดๅˆใ€็ตๆžœใฏๆๅคฑใŒๅคšใใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚่ค‡ๆ•ฐใฎๆ•ฐๅ€คใ‚’ไฝŽ็ฒพๅบฆใงใ‚ขใƒ‰ใƒใ‚ฟใ‚คใ‚บใ™ใ‚‹ใจ็ตๆžœใฏๆญฃ็ขบใงใฏใชใ„ใŸใ‚ใงใ™ใ€‚ ใ€‚ bf16 ใงใฏ fp16 ใ‚ˆใ‚Šใ‚‚็ฒพๅบฆใŒไฝŽใ„ใŸใ‚ใ€ใ•ใ‚‰ใซใใ†ใงใ™ใ€‚้€šๅธธใฏ้žๅธธใซๅฐใ•ใ„ grad ใ‚’ๅนณๅ‡ใ™ใ‚‹้š›ใฎๆๅคฑใŒๆœ€ๅฐ้™ใซๆŠ‘ใˆใ‚‰ใ‚Œใ‚‹ใŸใ‚ใ€fp16 ใงๅๅˆ†ใงใ‚ใ‚‹ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ๅŠ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใฏ fp16 ใŒใƒชใƒ€ใ‚ฏใ‚ทใƒงใƒณๆผ”็ฎ—ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ๅฎŒๅ…จใซๅˆถๅพกใงใใ€ๅฟ…่ฆใซๅฟœใ˜ใฆๅฐใ•ใชใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใฆใ€ใƒชใƒ€ใ‚ฏใ‚ทใƒงใƒณใŒ็ดฏ็ฉ dtype ใจใ—ใฆ fp32 ใ‚’ไฝฟ็”จใ—ใ€็ตๆžœใฎๆบ–ๅ‚™ใŒใงใใŸๅ ดๅˆใซใฎใฟๅŠ็ฒพๅบฆ `dtype` ใซใƒ€ใ‚ฆใƒณใ‚ญใƒฃใ‚นใƒˆใ™ใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใงใ™ใ€‚ ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใซใฏใ€ๆ–ฐใ—ใ„ๆง‹ๆˆใ‚จใƒณใƒˆใƒชใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ ```json { "communication_data_type": "fp32" } ``` ใ“ใฎ่จ˜ไบ‹ใฎๅŸท็ญ†ๆ™‚็‚นใงใฎๆœ‰ๅŠนใชๅ€คใฏใ€"fp16"ใ€"bfp16"ใ€"fp32"ใงใ™ใ€‚ ๆณจ: ใ‚นใƒ†ใƒผใ‚ธ ใ‚ผใƒญ 3 ใซใฏใ€bf16 ้€šไฟกใ‚ฟใ‚คใƒ—ใซ้–ขใ™ใ‚‹ใƒใ‚ฐใŒใ‚ใ‚Šใ€`deepspeed==0.8.1`ใงไฟฎๆญฃใ•ใ‚Œใพใ—ใŸใ€‚ ### apex apex AMP ใฎใ‚ˆใ†ใชใƒขใƒผใƒ‰ ใ‚ปใƒƒใƒˆใ‚’่จญๅฎšใ™ใ‚‹ใซใฏ: ```json "amp": { "enabled": "auto", "opt_level": "auto" } ``` [`Trainer`] ใฏ `args.fp16_backend` ใฎๅ€คใซๅŸบใฅใ„ใฆ่‡ชๅ‹•็š„ใซ่จญๅฎšใ—ใพใ™ใ€‚ `args.fp16_opt_level`ใ€‚ ใ“ใฎใƒขใƒผใƒ‰ใฏใ€`--fp16 --fp16_backend apex --fp16_opt_level 01`ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใŒๆธกใ•ใ‚Œใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ ใ“ใฎใƒขใƒผใƒ‰ใ‚’ๆ˜Ž็คบ็š„ใซๆง‹ๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "amp": { "enabled": true, "opt_level": "O1" } } ``` ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ ๆง‹ๆˆใ€‚ ใ“ใ‚Œใฏ[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/docs/config-json/#automatic-mixed-precision-amp-training-options)ใงใ™ใ€‚ <a id='deepspeed-bs'></a> ### Batch Size ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’่จญๅฎšใ™ใ‚‹ใซใฏใ€ๆฌกใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ ```json { "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto" } ``` [`Trainer`] ใฏ่‡ชๅ‹•็š„ใซ `train_micro_batch_size_per_gpu` ใ‚’ๆฌกใฎๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ `args.per_device_train_batch_size`ใจ`train_batch_size`ใ‚’`args.world_size * args.per_device_train_batch_size * args.gradient_accumulation_steps`ใซๅค‰ๆ›ดใ—ใพใ™ใ€‚ ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "train_batch_size": 12, "train_micro_batch_size_per_gpu": 4 } ``` ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ ๆง‹ๆˆใ€‚ <a id='deepspeed-grad-acc'></a> ### Gradient Accumulation ๅ‹พ้…็ดฏ็ฉใ‚ปใƒƒใƒˆใ‚’ๆง‹ๆˆใ™ใ‚‹ใซใฏ: ```json { "gradient_accumulation_steps": "auto" } ``` [`Trainer`] ใฏ่‡ชๅ‹•็š„ใซใใ‚Œใ‚’ `args.gradient_accumulation_steps` ใฎๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "gradient_accumulation_steps": 3 } ``` ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ ๆง‹ๆˆใ€‚ <a id='deepspeed-grad-clip'></a> ### Gradient Clipping ใ‚ฐใƒฉใƒ‡ใƒผใ‚ทใƒงใƒณ ใ‚ฐใƒฉใƒ‡ใƒผใ‚ทใƒงใƒณ ใ‚ฏใƒชใƒƒใƒ”ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใ‚’ๆง‹ๆˆใ™ใ‚‹ใซใฏ: ```json { "gradient_clipping": "auto" } ``` [`Trainer`] ใฏ่‡ชๅ‹•็š„ใซใใ‚Œใ‚’ `args.max_grad_norm` ใฎๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ```json { "gradient_clipping": 1.0 } ``` ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ ๆง‹ๆˆใ€‚ <a id='deepspeed-weight-extraction'></a> ### Getting The Model Weights Out ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’็ถ™็ถšใ—ใ€DeepSpeed ใฎไฝฟ็”จใ‚’ๅ†้–‹ใ™ใ‚‹้™ใ‚Šใ€ไฝ•ใ‚‚ๅฟƒ้…ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ DeepSpeed ใ‚นใƒˆใ‚ข fp32 ใฎใ‚ซใ‚นใ‚ฟใƒ  ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใƒ•ใ‚กใ‚คใƒซๅ†…ใฎใƒžใ‚นใ‚ฟใƒผใฎ้‡ใฟใ€‚ใ“ใ‚Œใฏ `global_step*/*optim_states.pt` (ใ“ใ‚Œใฏ glob ใƒ‘ใ‚ฟใƒผใƒณ)ใ€้€šๅธธใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎไธ‹ใซไฟๅญ˜ใ•ใ‚Œใพใ™ใ€‚ **FP16 ใ‚ฆใ‚งใ‚คใƒˆ:** ใƒขใƒ‡ใƒซใ‚’ ZeRO-2 ใงไฟๅญ˜ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ๅซใ‚€้€šๅธธใฎ `pytorch_model.bin` ใƒ•ใ‚กใ‚คใƒซใŒไฝœๆˆใ•ใ‚Œใพใ™ใŒใ€ ใ“ใ‚Œใ‚‰ใฏ้‡ใฟใฎ fp16 ใƒใƒผใ‚ธใƒงใƒณใซใ™ใŽใพใ›ใ‚“ใ€‚ ZeRO-3 ใงใฏใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใŒ่ค‡ๆ•ฐใฎ GPU ใซๅˆ†ๅ‰ฒใ•ใ‚Œใ‚‹ใŸใ‚ใ€็Šถๆณใฏใ•ใ‚‰ใซ่ค‡้›‘ใซใชใ‚Šใพใ™ใ€‚ ใ—ใŸใŒใฃใฆใ€fp16 ใ‚’ไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใฎ `Trainer` ใ‚’ๅ–ๅพ—ใ™ใ‚‹ใซใฏใ€`"stage3_gather_16bit_weights_on_model_save": true` ใŒๅฟ…่ฆใงใ™ใ€‚ ้‡ใฟใฎใƒใƒผใ‚ธใƒงใƒณใ€‚ใ“ใฎ่จญๅฎšใŒ`False`ใฎๅ ดๅˆใ€`pytorch_model.bin`ใฏไฝœๆˆใ•ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง DeepSpeed ใฎ `state_dict` ใซๅฎŸ้š›ใฎ้‡ใฟใงใฏใชใใƒ—ใƒฌใƒผใ‚นใƒ›ใƒซใƒ€ใƒผใŒๅซใพใ‚Œใ‚‹ใŸใ‚ใงใ™ใ€‚ใ“ใฎ `state_dict` ใ‚’ไฟๅญ˜ใ—ใŸๅ ดๅˆใ€ใƒญใƒผใƒ‰ใ—็›ดใ™ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ ```json { "zero_optimization": { "stage3_gather_16bit_weights_on_model_save": true } } ``` **FP32 ้‡้‡:** fp16 ใ‚ฆใ‚งใ‚คใƒˆใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ใฎใซ้ฉใ—ใฆใ„ใพใ™ใŒใ€ใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใŒๅฎŒไบ†ใ—ใ€ใใ‚Œใ‚’ [ใƒขใƒ‡ใƒซ ใƒใƒ–](https://huggingface.co/models) ใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใ‹ใ€fp32 ใ‚’ๅ…ฅๆ‰‹ใ—ใŸใ„ใจๆ€ใ‚ใ‚Œใ‚‹ไป–ใฎไบบใซๆธกใ—ใพใ™ใ€‚ ้‡ใฟใ€‚ใ“ใ‚Œใฏๅคง้‡ใฎใƒกใƒขใƒชใ‚’ๅฟ…่ฆใจใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใงใ‚ใ‚‹ใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซ่กŒใ†ในใใงใฏใชใ„ใฎใŒ็†ๆƒณ็š„ใงใ™ใ€‚ ใ—ใŸใŒใฃใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅฎŒไบ†ๅพŒใซใ‚ชใƒ•ใƒฉใ‚คใƒณใงๅฎŸ่กŒใ™ใ‚‹ใฎใŒๆœ€้ฉใงใ™ใ€‚ใŸใ ใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆใ€็ฉบใ CPU ใŒๅๅˆ†ใซใ‚ใ‚‹ๅ ดๅˆใฏใ€ ๅŒใ˜ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใงๅฎŸ่กŒใงใใ‚‹ใ“ใจใ‚’ๆ€ใ„ๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ไธกๆ–นใฎใ‚ขใƒ—ใƒญใƒผใƒใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ **ใƒฉใ‚คใƒ– FP32 ใ‚ฆใ‚งใ‚คใƒˆ ใƒชใ‚ซใƒใƒช:** ใƒขใƒ‡ใƒซใŒๅคงใใใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ็ต‚ไบ†ๆ™‚ใซ็ฉบใ CPU ใƒกใƒขใƒชใŒใปใจใ‚“ใฉๆฎ‹ใฃใฆใ„ใชใ„ๅ ดๅˆใ€ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏๆฉŸ่ƒฝใ—ใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ ๅฐ‘ใชใใจใ‚‚ 1 ใคใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใฆใ„ใฆใ€ๆœ€ๆ–ฐใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ ```python from transformers.trainer_utils import get_last_checkpoint from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint checkpoint_dir = get_last_checkpoint(trainer.args.output_dir) fp32_model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir) ``` `--load_best_model_at_end` class:*~transformers.TrainingArguments* ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆ (ๆœ€้ฉใชใƒขใƒ‡ใƒซใ‚’่ฟฝ่ทกใ™ใ‚‹ใŸใ‚) ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ)ใ€ๆœ€ๅˆใซๆœ€็ต‚ใƒขใƒ‡ใƒซใ‚’ๆ˜Ž็คบ็š„ใซไฟๅญ˜ใ—ใฆใ‹ใ‚‰ใ€ไธŠ่จ˜ใจๅŒใ˜ใ“ใจใ‚’่กŒใ†ใ“ใจใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’็ต‚ไบ†ใงใใพใ™ใ€‚ ```python from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint checkpoint_dir = os.path.join(trainer.args.output_dir, "checkpoint-final") trainer.deepspeed.save_checkpoint(checkpoint_dir) fp32_model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir) ``` <Tip> `load_state_dict_from_zero_checkpoint` ใŒๅฎŸ่กŒใ•ใ‚Œใ‚‹ใจใ€`model` ใฏใ‚‚ใฏใ‚„ไฝฟ็”จใงใใชใใชใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ ๅŒใ˜ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใฎ DeepSpeed ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ€‚ใคใพใ‚Šใ€deepspeed ใ‚จใƒณใ‚ธใƒณใ‚’ๅ†ๅˆๆœŸๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ `model.load_state_dict(state_dict)` ใฏใใ“ใ‹ใ‚‰ใ™ในใฆใฎ DeepSpeed ใƒžใ‚ธใƒƒใ‚ฏใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใฏๆœ€ๅพŒใซใฎใฟๅฎŸ่กŒใ—ใฆใใ ใ•ใ„ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๆง˜ๅญใ€‚ </Tip> ใ‚‚ใกใ‚ใ‚“ใ€class:*~transformers.Trainer* ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใฏใชใใ€ไธŠ่จ˜ใฎไพ‹ใ‚’็‹ฌ่‡ชใฎใ‚‚ใฎใซ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ใƒˆใƒฌใƒผใƒŠใƒผใ€‚ ไฝ•ใ‚‰ใ‹ใฎ็†็”ฑใงใ•ใ‚‰ใซๆ”น่‰ฏใ—ใŸใ„ๅ ดๅˆใฏใ€้‡ใฟใฎ fp32 `state_dict` ใ‚’ๆŠฝๅ‡บใ—ใฆ้ฉ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ๆฌกใฎไพ‹ใซ็คบใ™ใ‚ˆใ†ใซใ€ใ“ใ‚Œใ‚‰ใฏ่‡ชๅˆ†ใงไฝœๆˆใ—ใพใ™ใ€‚ ```python from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu model = model.cpu() model.load_state_dict(state_dict) ``` **ใ‚ชใƒ•ใƒฉใ‚คใƒณ FP32 ใ‚ฆใ‚งใ‚คใƒˆ ใƒชใ‚ซใƒใƒช:** DeepSpeed ใฏ็‰นๅˆฅใชๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆ`zero_to_fp32.py`ใ‚’ไฝœๆˆใ—ใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎๆœ€ไธŠไฝใซ้…็ฝฎใ—ใพใ™ใ€‚ ใƒ•ใ‚ฉใƒซใƒ€ใ€‚ใ“ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ„ใคใงใ‚‚้‡ใฟใ‚’ๆŠฝๅ‡บใงใใพใ™ใ€‚ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ‚นใ‚ฟใƒณใƒ‰ใ‚ขใƒญใƒณใชใฎใงใ€ใ‚‚ใ†ๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ๆŠฝๅ‡บใ‚’่กŒใ†ใŸใ‚ใฎ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใพใŸใฏ `Trainer` ใŒๅฟ…่ฆใงใ™ใ€‚ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใƒ•ใ‚ฉใƒซใƒ€ใƒผใŒๆฌกใฎใ‚ˆใ†ใซใชใฃใฆใ„ใ‚‹ใจใ—ใพใ™ใ€‚ ```bash $ ls -l output_dir/checkpoint-1/ -rw-rw-r-- 1 stas stas 1.4K Mar 27 20:42 config.json drwxrwxr-x 2 stas stas 4.0K Mar 25 19:52 global_step1/ -rw-rw-r-- 1 stas stas 12 Mar 27 13:16 latest -rw-rw-r-- 1 stas stas 827K Mar 27 20:42 optimizer.pt -rw-rw-r-- 1 stas stas 231M Mar 27 20:42 pytorch_model.bin -rw-rw-r-- 1 stas stas 623 Mar 27 20:42 scheduler.pt -rw-rw-r-- 1 stas stas 1.8K Mar 27 20:42 special_tokens_map.json -rw-rw-r-- 1 stas stas 774K Mar 27 20:42 spiece.model -rw-rw-r-- 1 stas stas 1.9K Mar 27 20:42 tokenizer_config.json -rw-rw-r-- 1 stas stas 339 Mar 27 20:42 trainer_state.json -rw-rw-r-- 1 stas stas 2.3K Mar 27 20:42 training_args.bin -rwxrw-r-- 1 stas stas 5.5K Mar 27 13:16 zero_to_fp32.py* ``` ใ“ใฎไพ‹ใงใฏใ€DeepSpeed ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใ‚ตใƒ–ใƒ•ใ‚ฉใƒซใƒ€ใƒผ *global_step1* ใŒ 1 ใคใ ใ‘ใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€FP32ใ‚’ๅ†ๆง‹็ฏ‰ใ™ใ‚‹ใซใฏ ้‡ใฟใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ ใ‘ใงใ™: ```bash python zero_to_fp32.py . pytorch_model.bin ``` ใ“ใ‚Œใ ใ‚ˆใ€‚ `pytorch_model.bin`ใซใฏใ€่ค‡ๆ•ฐใฎ GPU ใ‹ใ‚‰็ตฑๅˆใ•ใ‚ŒใŸๅฎŒๅ…จใช fp32 ใƒขใƒ‡ใƒซใฎ้‡ใฟใŒๅซใพใ‚Œใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ZeRO-2 ใพใŸใฏ ZeRO-3 ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’่‡ชๅ‹•็š„ใซๅ‡ฆ็†ใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ `python zero_to_fp32.py -h` ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใจใ€ไฝฟ็”จๆ–นๆณ•ใฎ่ฉณ็ดฐใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ใƒ•ใ‚กใ‚คใƒซ`latest`ใฎๅ†…ๅฎนใ‚’ไฝฟ็”จใ—ใฆ deepspeed ใ‚ตใƒ–ใƒ•ใ‚ฉใƒซใƒ€ใƒผใ‚’่‡ชๅ‹•ๆคœๅ‡บใ—ใพใ™ใ€‚ ไพ‹ใซใฏ`global_step1`ใŒๅซใพใ‚Œใพใ™ใ€‚ ๆณจ: ็พๅœจใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใซใฏๆœ€็ต‚็š„ใช fp32 ใƒขใƒ‡ใƒซใฎ้‡ใฟใฎ 2 ๅ€ใฎไธ€่ˆฌ RAM ใŒๅฟ…่ฆใงใ™ใ€‚ ### ZeRO-3 ใจ Infinity Nuances ZeRO-3 ใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟ ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆฉŸ่ƒฝใฎ็‚นใง ZeRO-2 ใจใฏๅคงใใ็•ฐใชใ‚Šใพใ™ใ€‚ ZeRO-Infinity ใฏ ZeRO-3 ใ‚’ใ•ใ‚‰ใซๆ‹กๅผตใ—ใ€NVMe ใƒกใƒขใƒชใ‚„ใใฎไป–ใฎ่ค‡ๆ•ฐใฎ้€Ÿๅบฆใจใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใฎๅ‘ไธŠใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ ใƒขใƒ‡ใƒซใซ็‰นๅˆฅใชๅค‰ๆ›ดใ‚’ๅŠ ใˆใ‚‹ๅฟ…่ฆใŒใชใใฆใ‚‚ๆญฃๅธธใซๅ‹•ไฝœใ™ใ‚‹ใ‚ˆใ†ใซใ‚ใ‚‰ใ‚†ใ‚‹ๅŠชๅŠ›ใŒๆ‰•ใ‚ใ‚Œใฆใใพใ—ใŸใŒใ€็‰นๅฎšใฎ็‚นใงใฏ ็Šถๆณใซใ‚ˆใฃใฆใฏใ€ๆฌกใฎๆƒ…ๅ ฑใŒๅฟ…่ฆใซใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ #### Constructing Massive Models DeepSpeed/ZeRO-3 ใฏใ€ๆ—ขๅญ˜ใฎ RAM ใซๅŽใพใ‚‰ใชใ„ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ๆ•ฐๅ…†ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใคใƒขใƒ‡ใƒซใ‚’ๅ‡ฆ็†ใงใใพใ™ใ€‚ใใฎใ‚ˆใ†ใชๅ ดๅˆใ€ ใพใŸใ€ๅˆๆœŸๅŒ–ใ‚’ใ‚ˆใ‚Š้ซ˜้€ŸใซๅฎŸ่กŒใ—ใŸใ„ๅ ดๅˆใฏใ€*deepspeed.zero.Init()* ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅˆๆœŸๅŒ–ใ—ใพใ™ใ€‚ ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ ใƒžใƒใƒผใ‚ธใƒฃใƒผ (้–ขๆ•ฐใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใƒผใงใ‚‚ใ‚ใ‚Šใพใ™)ใ€‚ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ ```python from transformers import T5ForConditionalGeneration, T5Config import deepspeed with deepspeed.zero.Init(): config = T5Config.from_pretrained("google-t5/t5-small") model = T5ForConditionalGeneration(config) ``` ใ”่ฆงใฎใจใŠใ‚Šใ€ใ“ใ‚Œใซใ‚ˆใ‚Šใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใ€`model_class.from_pretrained` ใฏๆฌกใฎๆกไปถใ‚’ๆบ€ใŸใ™้™ใ‚Šใ“ใฎๆฉŸ่ƒฝใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ `is_deepspeed_zero3_enabled()` ใฏ `True` ใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ็พๅœจใ€ [`TrainingArguments`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ (ๆธกใ•ใ‚ŒใŸ DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใซ ZeRO-3 ๆง‹ๆˆใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅ ดๅˆ) ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ€‚ใ—ใŸใŒใฃใฆใ€ๅ‘ผใณๅ‡บใ—ใฎๅ‰ใซ** [`TrainingArguments`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ `from_pretrained`ใ€‚่€ƒใˆใ‚‰ใ‚Œใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎไพ‹ใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ ```python from transformers import AutoModel, Trainer, TrainingArguments training_args = TrainingArguments(..., deepspeed=ds_config) model = AutoModel.from_pretrained("google-t5/t5-small") trainer = Trainer(model=model, args=training_args, ...) ``` ๅ…ฌๅผใฎใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใฆใ€ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ `--deepspeed ds_config.json` ใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅ ดๅˆ ZeRO-3 ่จญๅฎšใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใจใ€ใ“ใ‚ŒใŒใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎ่จ˜่ฟฐๆ–นๆณ•ใงใ‚ใ‚‹ใŸใ‚ใ€ใ™ในใฆใŒใ™ใงใซๅฎŒไบ†ใ—ใฆใ„ใพใ™ใ€‚ ๆณจ: ใƒขใƒ‡ใƒซใฎ fp16 ้‡ใฟใŒๅ˜ไธ€ใฎ GPU ใฎใƒกใƒขใƒชใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆใฏใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใ“ใฎๆ–นๆณ•ใจใใฎไป–ใฎ้–ข้€ฃๆฉŸ่ƒฝใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎๆง‹็ฏ‰](https://deepspeed.readthedocs.io/en/latest/zero3.html#constructing-massive-models) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ใพใŸใ€fp16 ใงไบ‹ๅ‰่จ“็ทดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใใฏใ€`from_pretrained` ใซไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๆŒ‡็คบใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ `torch_dtype=torch.float16`ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[from_pretrained-torch-dtype](#from_pretrained-torch-dtype) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ #### Gathering Parameters ่ค‡ๆ•ฐใฎ GPU ไธŠใฎ ZeRO-3 ใงใฏใ€็พๅœจใฎ GPU ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใงใชใ„้™ใ‚Šใ€ๅ˜ไธ€ใฎ GPU ใŒใ™ในใฆใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใคใ“ใจใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ๅฎŸ่กŒๅฑคใ€‚ใ—ใŸใŒใฃใฆใ€ใ™ในใฆใฎใƒฌใ‚คใƒคใƒผใฎใ™ในใฆใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใซไธ€ๅบฆใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใใ‚Œใ‚’่กŒใ†ใŸใ‚ใฎ็‰นๅฎšใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ ใปใจใ‚“ใฉใฎๅ ดๅˆใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅฟ…่ฆใชๅ ดๅˆใฏใ€[ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๅŽ้›†](https://deepspeed.readthedocs.io/en/latest/zero3.html#manual-parameter-coordination) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ใŸใ ใ—ใ€ใ„ใใคใ‹ใฎๅ ดๆ‰€ใงๅ†…้ƒจ็š„ใซไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใใฎไพ‹ใฎ 1 ใคใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใใงใ™ใ€‚ `from_pretrained`ใ€‚ไธ€ๅบฆใซ 1 ใคใฎใƒฌใ‚คใƒคใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใ€ๅ‚ๅŠ ใ—ใฆใ„ใ‚‹ใ™ในใฆใฎ GPU ใซๅณๅบงใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใงใฏใ€ใƒกใƒขใƒชใฎ้–ขไฟ‚ใงใ€1 ใคใฎ GPU ใซใƒญใƒผใƒ‰ใ—ใฆใ‹ใ‚‰่ค‡ๆ•ฐใฎ GPU ใซๅˆ†ๆ•ฃใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ ๅˆถ้™ใ€‚ ใพใŸใ€ZeRO-3 ใงใฏใ€็‹ฌ่‡ชใฎใ‚ณใƒผใƒ‰ใ‚’ไฝœๆˆใ—ใ€ๆฌกใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ้‡ใฟใŒ็™บ็”Ÿใ™ใ‚‹ใจใ—ใพใ™ใ€‚ ```python tensor([1.0], device="cuda:0", dtype=torch.float16, requires_grad=True) ``` `tensor([1.])` ใซใ‚นใƒˆใƒฌใ‚นใ‚’ๆ„Ÿใ˜ใŸๅ ดๅˆใ€ใพใŸใฏใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎใ‚ตใ‚คใ‚บใŒ `1` ใงใ‚ใ‚‹ใจใ„ใ†ใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆ ใ‚ˆใ‚Šๅคงใใชๅคšๆฌกๅ…ƒๅฝข็Šถใ€‚ใ“ใ‚Œใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒๅˆ†ๅ‰ฒใ•ใ‚ŒใฆใŠใ‚Šใ€่กจ็คบใ•ใ‚Œใ‚‹ใฎใฏ ZeRO-3 ใƒ—ใƒฌใƒผใ‚นใƒ›ใƒซใƒ€ใƒผใงใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ <a id='deepspeed-zero-inference'></a> ### ZeRO Inference ZeRO Inference ใฏใ€ZeRO-3 Training ใจๅŒใ˜ๆง‹ๆˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใง ๅฎŸ้š›ใ€ๅŒใ˜ใ‚‚ใฎใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๅ…ฑๆœ‰ใ—ใŸใ„ๅ ดๅˆใฏใ€ใ“ใ‚Œใ‚‰ใ‚’่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซๆฎ‹ใ™ใ“ใจใŒใงใใพใ™ใ€‚ๅฝผใ‚‰ใฏใŸใ ใใ†ใชใ‚‹ใ ใ‚ใ† ็„ก่ฆ–ใ•ใ‚Œใพใ—ใŸใ€‚ ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏใ€้€šๅธธใฎ [`TrainingArguments`] ๅผ•ๆ•ฐใ‚’ๆธกใ™ใ ใ‘ใงใ™ใ€‚ไพ‹ใˆใฐ๏ผš ```bash deepspeed --num_gpus=2 your_program.py <normal cl args> --do_eval --deepspeed ds_config.json ``` ๅ”ฏไธ€้‡่ฆใชใ“ใจใฏใ€ZeRO-2 ใซใฏไฝ•ใฎๅˆฉ็‚นใ‚‚ใชใ„ใŸใ‚ใ€ZeRO-3 ๆง‹ๆˆใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใจใ„ใ†ใ“ใจใงใ™ใ€‚ ZeRO-3 ใฎใฟใŒใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ™ใ‚‹ใฎใซๅฏพใ—ใ€ZeRO-1 ใฏๅ‹พ้…ใจใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใ‚’ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใซๅฝน็ซ‹ใกใพใ™ใ€‚ ไปฅไธ‹ใฏใ€ๅˆฉ็”จๅฏ่ƒฝใชใ™ในใฆใฎ GPU ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ DeepSpeed ใง`run_translation.py`ใ‚’ๅฎŸ่กŒใ™ใ‚‹ไพ‹ใงใ™ใ€‚ ```bash deepspeed examples/pytorch/translation/run_translation.py \ --deepspeed tests/deepspeed/ds_config_zero3.json \ --model_name_or_path google-t5/t5-small --output_dir output_dir \ --do_eval --max_eval_samples 50 --warmup_steps 50 \ --max_source_length 128 --val_max_target_length 128 \ --overwrite_output_dir --per_device_eval_batch_size 4 \ --predict_with_generate --dataset_config "ro-en" --fp16 \ --source_lang en --target_lang ro --dataset_name wmt16 \ --source_prefix "translate English to Romanian: " ``` ๆŽจ่ซ–ใฎใŸใ‚ใซใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใจๅ‹พ้…ใซใ‚ˆใฃใฆไฝฟ็”จใ•ใ‚Œใ‚‹่ฟฝๅŠ ใฎๅคงใใชใƒกใƒขใƒชใฏๅฟ…่ฆใชใ„ใŸใ‚ใ€ ใฏใ‚‹ใ‹ใซๅคงใใชใƒใƒƒใƒใ‚„ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใ‚’ๅŒใ˜ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซ้ฉๅˆใงใใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใ•ใ‚‰ใซใ€DeepSpeed ใฏ็พๅœจใ€Deepspeed-Inference ใจๅ‘ผใฐใ‚Œใ‚‹้–ข้€ฃ่ฃฝๅ“ใ‚’้–‹็™บใ—ใฆใ„ใพใ™ใŒใ€ใ“ใ‚Œใจใฏไฝ•ใฎ้–ขไฟ‚ใ‚‚ใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ZeRO ใƒ†ใ‚ฏใƒŽใƒญใ‚ธใƒผใซๆบ–ๆ‹ ใ—ใฆใ„ใพใ™ใŒใ€ไปฃใ‚ใ‚Šใซใƒ†ใƒณใ‚ฝใƒซไธฆๅˆ—ๅ‡ฆ็†ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ˜ไธ€ใฎ GPU ใซๅŽใพใ‚‰ใชใ„ใƒขใƒ‡ใƒซใ‚’ใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ ็พๅœจ้–‹็™บไธญใงใ™ใ€‚่ฃฝๅ“ใŒๅฎŒๆˆใ—ใŸใ‚‰็ตฑๅˆใ‚’ๆไพ›ใ™ใ‚‹ไบˆๅฎšใงใ™ใ€‚ ### Memory Requirements Deepspeed ZeRO ใฏใƒกใƒขใƒชใ‚’ CPU (ใŠใ‚ˆใณ NVMe) ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใงใใ‚‹ใŸใ‚ใ€ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฏใ€ไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ GPU ใฎๆ•ฐใซๅฟœใ˜ใฆๅฟ…่ฆใช CPU ใŠใ‚ˆใณ GPU ใƒกใƒขใƒชใฎ้‡ใ‚’็Ÿฅใ‚‹ใ“ใจใŒใงใใ‚‹ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚’ๆไพ›ใ—ใพใ™ใ€‚ ๅ˜ไธ€ใฎ GPU ใง `bigscience/T0_3B`ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชใƒกใƒขใƒชใฎ้‡ใ‚’่ฆ‹็ฉใ‚‚ใฃใฆใฟใพใ—ใ‚‡ใ†ใ€‚ ```bash $ python -c 'from transformers import AutoModel; \ from deepspeed.runtime.zero.stage3 import estimate_zero3_model_states_mem_needs_all_live; \ model = AutoModel.from_pretrained("bigscience/T0_3B"); \ estimate_zero3_model_states_mem_needs_all_live(model, num_gpus_per_node=1, num_nodes=1)' [...] Estimated memory needed for params, optim states and gradients for a: HW: Setup with 1 node, 1 GPU per node. SW: Model with 2783M total params, 65M largest layer params. per CPU | per GPU | Options 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=1 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=0 62.23GB | 5.43GB | offload_param=none, offload_optimizer=cpu , zero_init=1 62.23GB | 5.43GB | offload_param=none, offload_optimizer=cpu , zero_init=0 0.37GB | 46.91GB | offload_param=none, offload_optimizer=none, zero_init=1 15.56GB | 46.91GB | offload_param=none, offload_optimizer=none, zero_init=0 ``` ใ—ใŸใŒใฃใฆใ€ๅ˜ไธ€ใฎ 80 GB GPU ใง CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใชใ—ใงๆญ่ผ‰ใ™ใ‚‹ใ“ใจใ‚‚ใ€ๅฐใ•ใช 8 GB GPU ใงใ‚‚ๆœ€ๅคง 60 GB ใฎ CPU ใƒกใƒขใƒชใŒๅฟ…่ฆใซใชใ‚‹ใ“ใจใ‚‚ๅฏ่ƒฝใงใ™ใ€‚ (ใ“ใ‚Œใฏใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใ€ใŠใ‚ˆใณๅ‹พ้…ใฎใŸใ‚ใฎใƒกใƒขใƒชใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚cuda ใ‚ซใƒผใƒใƒซใ€ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ€ใŠใ‚ˆใณไธ€ๆ™‚ใƒกใƒขใƒชใซใฏใ‚‚ใ†ๅฐ‘ใ—ๅคšใใฎใƒกใƒขใƒชใŒๅฟ…่ฆใงใ™ใ€‚) ๆฌกใซใ€ใ‚ณใ‚นใƒˆใจ้€Ÿๅบฆใฎใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใซใชใ‚Šใพใ™ใ€‚ใ‚ˆใ‚Šๅฐใ•ใ„ GPU ใ‚’่ณผๅ…ฅใพใŸใฏใƒฌใƒณใ‚ฟใƒซใ—ใŸๆ–นใŒๅฎ‰ใใชใ‚Šใพใ™ (Deepspeed ZeRO ใงใฏ่ค‡ๆ•ฐใฎ GPU ใ‚’ไฝฟ็”จใงใใ‚‹ใŸใ‚ใ€GPU ใฎๆ•ฐใ‚’ๆธ›ใ‚‰ใ™ใ“ใจใ‚‚ใงใใพใ™)ใ€‚ใ—ใ‹ใ—ใ€ใใฎๅ ดๅˆใฏ้…ใใชใ‚Šใพใ™ใ€‚ใใฎใŸใ‚ใ€ไฝ•ใ‹ใ‚’ๅฎŸ่กŒใ™ใ‚‹้€Ÿๅบฆใ‚’ๆฐ—ใซใ—ใชใใฆใ‚‚ใ€้€ŸๅบฆใฎไฝŽไธ‹ใฏ GPU ใฎไฝฟ็”จๆ™‚้–“ใซ็›ดๆŽฅๅฝฑ้Ÿฟใ—ใ€ใ‚ณใ‚นใƒˆใŒๅข—ๅคงใ™ใ‚‹ใŸใ‚ใ€ใฉใ‚ŒใŒๆœ€ใ‚‚ๅŠนๆžœ็š„ใ‹ใ‚’ๅฎŸ้จ“ใ—ใฆๆฏ”่ผƒใ—ใฆใใ ใ•ใ„ใ€‚ ๅๅˆ†ใช GPU ใƒกใƒขใƒชใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใ™ในใฆใŒ้ซ˜้€Ÿใซใชใ‚‹ใŸใ‚ใ€CPU/NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ๅฟ…ใš็„กๅŠนใซใ—ใฆใใ ใ•ใ„ใ€‚ ใŸใจใˆใฐใ€2 ใคใฎ GPU ใซๅฏพใ—ใฆๅŒใ˜ใ“ใจใ‚’็นฐใ‚Š่ฟ”ใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ ```bash $ python -c 'from transformers import AutoModel; \ from deepspeed.runtime.zero.stage3 import estimate_zero3_model_states_mem_needs_all_live; \ model = AutoModel.from_pretrained("bigscience/T0_3B"); \ estimate_zero3_model_states_mem_needs_all_live(model, num_gpus_per_node=2, num_nodes=1)' [...] Estimated memory needed for params, optim states and gradients for a: HW: Setup with 1 node, 2 GPUs per node. SW: Model with 2783M total params, 65M largest layer params. per CPU | per GPU | Options 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=1 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=0 62.23GB | 2.84GB | offload_param=none, offload_optimizer=cpu , zero_init=1 62.23GB | 2.84GB | offload_param=none, offload_optimizer=cpu , zero_init=0 0.74GB | 23.58GB | offload_param=none, offload_optimizer=none, zero_init=1 31.11GB | 23.58GB | offload_param=none, offload_optimizer=none, zero_init=0 ``` ใ—ใŸใŒใฃใฆใ€ใ“ใ“ใงใฏใ€CPU ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ›ใšใซ 2x 32GB ไปฅไธŠใฎ GPU ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒกใƒขใƒชๆŽจๅฎšใƒ„ใƒผใƒซ](https://deepspeed.readthedocs.io/en/latest/memory.html) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ### Filing Issues ใ“ใ“ใงใฏใ€ๅ•้กŒใฎ็œŸ็›ธใ‚’ใ™ใใซ่งฃๆ˜Žใ—ใ€ไฝœๆฅญใฎใƒ–ใƒญใƒƒใ‚ฏใ‚’่งฃ้™คใงใใ‚‹ใ‚ˆใ†ใ€ๅ•้กŒใ‚’ๅ ฑๅ‘Šใ™ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ ใƒฌใƒใƒผใƒˆใซใฏๅฟ…ใšๆฌกใฎๅ†…ๅฎนใ‚’ๅซใ‚ใฆใใ ใ•ใ„ใ€‚ 1. ใƒฌใƒใƒผใƒˆๅ†…ใฎๅฎŒๅ…จใช Deepspeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ 2. [`Trainer`] ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ€ใพใŸใฏ ใƒˆใƒฌใƒผใƒŠใƒผใฎใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’่‡ชๅˆ†ใงใ‚นใ‚ฏใƒชใƒ—ใƒˆไฝœๆˆใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€[`TrainingArguments`] ๅผ•ๆ•ฐใ€‚ใ—ใชใ„ใงใใ ใ•ใ„ [`TrainingArguments`] ใซใฏ็„ก้–ขไฟ‚ใชใ‚จใƒณใƒˆใƒชใŒๅคšๆ•ฐๅซใพใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใƒ€ใƒณใƒ—ใ—ใพใ™ใ€‚ 3. ๆฌกใฎๅ‡บๅŠ›: ```bash python -c 'import torch; print(f"torch: {torch.__version__}")' python -c 'import transformers; print(f"transformers: {transformers.__version__}")' python -c 'import deepspeed; print(f"deepspeed: {deepspeed.__version__}")' ``` 4. ๅฏ่ƒฝใงใ‚ใ‚Œใฐใ€ๅ•้กŒใ‚’ๅ†็พใงใใ‚‹ Google Colab ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใธใฎใƒชใƒณใ‚ฏใ‚’ๅซใ‚ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใ‚’ไฝฟใˆใพใ™ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://github.com/stas00/porting/blob/master/transformers/deepspeed/DeepSpeed_on_colab_CLI.ipynb) ใจใ—ใฆ ๅ‡บ็™บ็‚นใ€‚ 5. ไธๅฏ่ƒฝใงใชใ„้™ใ‚Šใ€ใ‚ซใ‚นใ‚ฟใƒ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฏใชใใ€ๅธธใซไฝฟ็”จใงใใ‚‹ๆจ™ๆบ–ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ 6. ๅฏ่ƒฝใงใ‚ใ‚Œใฐใ€ๆ—ขๅญ˜ใฎ [ใ‚ตใƒณใƒ—ใƒซ](https://github.com/huggingface/transformers/tree/main/examples/pytorch) ใฎใ„ใšใ‚Œใ‹ใ‚’ไฝฟ็”จใ—ใฆๅ•้กŒใ‚’ๅ†็พใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ - Deepspeed ใŒๅ•้กŒใฎๅŽŸๅ› ใงใฏใชใ„ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ ๆๅ‡บใ•ใ‚ŒใŸๅ•้กŒใฎไธ€้ƒจใฏใ€Deepspeed ใจใฏ็„ก้–ขไฟ‚ใงใ‚ใ‚‹ใ“ใจใŒๅˆคๆ˜Žใ—ใพใ—ใŸใ€‚ใใ‚Œใฏใ€Deepspeed ใŒใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‹ใ‚‰ๅ‰Š้™คใ•ใ‚ŒใŸๅพŒใงใ™ใ€‚ ๅ•้กŒใฏใพใ ๆฎ‹ใฃใฆใ„ใŸใ€‚ ใ—ใŸใŒใฃใฆใ€ๅฎŒๅ…จใซๆ˜Ž็™ฝใงใชใ„ๅ ดๅˆใฏใ€DeepSpeed ้–ข้€ฃใฎๅ•้กŒใงใ™ใ€‚ ไพ‹ๅค–ใŒ็™บ็”Ÿใ—ใ€DeepSpeed ใƒขใ‚ธใƒฅใƒผใƒซใŒ้–ขไฟ‚ใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใพใšใ€DeepSpeed ใ‚’ๅซใพใชใ„ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’ๅ†ใƒ†ใ‚นใƒˆใ—ใฆใใ ใ•ใ„ใ€‚ ๅ•้กŒใŒ่งฃๆฑบใ—ใชใ„ๅ ดๅˆใซใฎใฟใ€Deepspeed ใซใคใ„ใฆ่จ€ๅŠใ—ใ€ๅฟ…่ฆใช่ฉณ็ดฐใ‚’ใ™ในใฆๆไพ›ใ—ใฆใใ ใ•ใ„ใ€‚ - ๅ•้กŒใŒ็ตฑๅˆ้ƒจๅˆ†ใงใฏใชใ DeepSpeed ใ‚ณใ‚ขใซใ‚ใ‚‹ใ“ใจใŒๆ˜Žใ‚‰ใ‹ใชๅ ดๅˆใฏใ€ๅ•้กŒใ‚’ๆๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚ [Deepspeed](https://github.com/microsoft/DeepSpeed/) ใ‚’็›ดๆŽฅไฝฟ็”จใ—ใพใ™ใ€‚ใ‚ˆใใ‚ใ‹ใ‚‰ใชใ„ๅ ดๅˆใงใ‚‚ใ€ใ”ๅฎ‰ๅฟƒใใ ใ•ใ„ใ€‚ ใฉใกใ‚‰ใฎๅ•้กŒใƒˆใƒฉใƒƒใ‚ซใƒผใงใ‚‚ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆŠ•็จฟใ•ใ‚ŒใŸใ‚‰ใใ‚Œใ‚’ๅˆคๆ–ญใ—ใ€ๆฌกใฎๅ ดๅˆใฏๅˆฅใฎๅ•้กŒใƒˆใƒฉใƒƒใ‚ซใƒผใซใƒชใƒ€ใ‚คใƒฌใ‚ฏใƒˆใ—ใพใ™ใ€‚ ใใ†ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ€‚ ### Troubleshooting #### the `deepspeed` process gets killed at startup without a traceback `deepspeed`ใƒ—ใƒญใ‚ปใ‚นใŒ่ตทๅ‹•ๆ™‚ใซใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใชใ—ใงๅผทๅˆถ็ต‚ไบ†ใ•ใ‚ŒใŸๅ ดๅˆใ€ใใ‚Œใฏ้€šๅธธใ€ใƒ—ใƒญใ‚ฐใƒฉใƒ ใŒ่ฉฆ่กŒใ—ใŸใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ ใ‚ทใ‚นใƒ†ใƒ ใŒๆŒใฃใฆใ„ใ‚‹ใ‚ˆใ‚Šใ‚‚ๅคšใใฎ CPU ใƒกใƒขใƒชใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ใ‹ใ€ใƒ—ใƒญใ‚ปใ‚นใŒๅ‰ฒใ‚Šๅฝ“ใฆใ‚’่จฑๅฏใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€OS ใ‚ซใƒผใƒใƒซใŒใใ‚Œใ‚’ๅผทๅˆถ็ต‚ไบ†ใ—ใพใ™ใ€‚ ใƒ—ใƒญใ‚ปใ‚นใ€‚ใ“ใ‚Œใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซ `offload_optimizer` ใพใŸใฏ `offload_param` ใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใŸใ‚ใงใ™ใ€‚ ใฉใกใ‚‰ใ‚‚`cpu`ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ‚ˆใ†ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใพใ™ใ€‚ NVMe ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎ็’ฐๅขƒใงๅฎŸ่กŒใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏ NVMe ใธใฎใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’่ฉฆใ—ใฆใใ ใ•ใ„ใ€‚ ใ‚ผใƒญ-3ใ€‚ [็‰นๅฎšใฎใƒขใƒ‡ใƒซใซๅฟ…่ฆใชใƒกใƒขใƒช้‡ใ‚’่ฆ‹็ฉใ‚‚ใ‚‹]ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™(https://deepspeed.readthedocs.io/en/latest/memory.html)ใ€‚ #### training and/or eval/predict loss is `NaN` ใ“ใ‚Œใฏใ€bf16 ๆททๅˆ็ฒพๅบฆใƒขใƒผใƒ‰ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅ–ๅพ—ใ—ใ€ใใ‚Œใ‚’ fp16 (ๆททๅˆ็ฒพๅบฆใฎๆœ‰็„กใซใ‹ใ‹ใ‚ใ‚‰ใš) ใงไฝฟ็”จใ—ใ‚ˆใ†ใจใ—ใŸๅ ดๅˆใซใ‚ˆใ็™บ็”Ÿใ—ใพใ™ใ€‚ TPU ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใปใจใ‚“ใฉใฎใƒขใƒ‡ใƒซใ€ใŠใ‚ˆใณๅคšใใฎๅ ดๅˆใ€Google ใซใ‚ˆใฃใฆใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใ€ใ“ใฎใ‚ซใƒ†ใ‚ดใƒชใซๅˆ†้กžใ•ใ‚Œใพใ™ (ใŸใจใˆใฐใ€ใปใผใ™ในใฆใฎ t5 ใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซ)ใ€‚ใ“ใ“ใงใฎ่งฃๆฑบ็ญ–ใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใŒใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ๅ ดๅˆ (TPUใ€Ampere GPU ไปฅ้™)ใ€fp32 ใพใŸใฏ bf16 ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ ```json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 } } ``` ใƒญใ‚ฐใซใฏใ€Deepspeed ใŒๆฌกใฎใ‚ˆใ†ใซ`OVERFLOW!`ใ‚’ๅ ฑๅ‘Šใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ ``` 0%| | 0/189 [00:00<?, ?it/s] [deepscale] OVERFLOW! Rank 0 Skipping step. Attempted loss scale: 262144, reducing to 262144 1%|โ–Œ | 1/189 [00:00<01:26, 2.17it/s] [deepscale] OVERFLOW! Rank 0 Skipping step. Attempted loss scale: 262144, reducing to 131072.0 1%|โ–ˆโ– [...] [deepscale] OVERFLOW! Rank 0 Skipping step. Attempted loss scale: 1, reducing to 1 14%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–Œ | 27/189 [00:14<01:13, 2.21it/s] [deepscale] OVERFLOW! Rank 0 Skipping step. Attempted loss scale: 1, reducing to 1 15%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ– | 28/189 [00:14<01:13, 2.18it/s] [deepscale] OVERFLOW! Rank 0 Skipping step. Attempted loss scale: 1, reducing to 1 15%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–Š | 29/189 [00:15<01:13, 2.18it/s] [deepscale] OVERFLOW! Rank 0 Skipping step. Attempted loss scale: 1, reducing to 1 [...] ``` ใ“ใ‚Œใฏใ€Deepspeed ๆๅคฑใ‚นใ‚ฑใƒผใƒฉใƒผใŒๆๅคฑใ‚ชใƒผใƒใƒผใƒ•ใƒญใƒผใ‚’ๅ…‹ๆœใ™ใ‚‹ใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐไฟ‚ๆ•ฐใ‚’่ฆ‹ใคใ‘ใ‚‰ใ‚Œใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ (ใƒญใ‚ฐใฏใ“ใ“ใง่ชญใฟใ‚„ใ™ใใ™ใ‚‹ใŸใ‚ใซใƒžใƒƒใ‚ตใƒผใ‚ธใ•ใ‚Œใฆใ„ใพใ™ใ€‚) ใ“ใฎๅ ดๅˆใ€้€šๅธธใฏ `initial_scale_power` ใฎๅ€คใ‚’ไธŠใ’ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚้€šๅธธใ€`initial_scale_power: 32` ใซ่จญๅฎšใ™ใ‚‹ใจๅ•้กŒใŒ่งฃๆฑบใ—ใพใ™ใ€‚ ### Notes - DeepSpeed ใซใฏ pip ใงใ‚คใƒณใ‚นใƒˆใƒผใƒซๅฏ่ƒฝใช PyPI ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใŒใ‚ใ‚Šใพใ™ใŒใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซๆœ€ใ‚‚้ฉๅˆใ™ใ‚‹ใ‚ˆใ†ใซใ€ใพใŸๆœ‰ๅŠนใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€[ใ‚ฝใƒผใ‚น](https://github.com/microsoft/deepspeed#installation) ใ‹ใ‚‰ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ 1 ใƒ“ใƒƒใƒˆ Adam ใชใฉใฎ็‰นๅฎšใฎๆฉŸ่ƒฝใฏใ€pypi ใƒ‡ใ‚ฃใ‚นใƒˆใƒชใƒ“ใƒฅใƒผใ‚ทใƒงใƒณใงใฏๅˆฉ็”จใงใใพใ›ใ‚“ใ€‚ - ๐Ÿค— Transformers ใง DeepSpeed ใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซ [`Trainer`] ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ - ไปปๆ„ใฎใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใพใ™ ๅพŒ่€…ใฏ [DeepSpeed ็ตฑๅˆๆ‰‹้ †](https://www.deepspeed.ai/getting-started/#writing-deepspeed-models) ใซๅพ“ใฃใฆ่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ## Non-Trainer Deepspeed Integration [`~integrations.HfDeepSpeedConfig`] ใฏใ€Deepspeed ใ‚’ ๐Ÿค— Transformers ใ‚ณใ‚ขใซ็ตฑๅˆใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ [`Trainer`] ใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใฎๆฉŸ่ƒฝใ€‚ๅฎŸ่กŒใ™ใ‚‹ๅ”ฏไธ€ใฎใ“ใจใฏใ€Deepspeed ZeRO-3 ใƒ‘ใƒฉใƒกใƒผใ‚ฟๅŽ้›†ใ‚’ๅ‡ฆ็†ใ—ใ€`from_pretrained`ๅ‘ผใณๅ‡บใ—ไธญใซใƒขใƒ‡ใƒซใ‚’่ค‡ๆ•ฐใฎ GPU ใซ่‡ชๅ‹•็š„ใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใใ‚Œไปฅๅค–ใฏใ™ในใฆ่‡ชๅˆ†ใง่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ [`Trainer`] ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ™ในใฆใŒ่‡ชๅ‹•็š„ใซๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚ [`Trainer`] ใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใ€DeepSpeed ZeRO-3 ใ‚’ๅŠน็އ็š„ใซๅฐŽๅ…ฅใ™ใ‚‹ใซใฏใ€ ใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ๅ‰ใซ [`~integrations.HfDeepSpeedConfig`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๅ‰Š้™คใ—ใ€ใใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’็”ŸใใŸใพใพใซใ—ใพใ™ใ€‚ Deepspeed ZeRO-1 ใพใŸใฏ ZeRO-2 ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€`HfDeepSpeedConfig`ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใฏใพใฃใŸใใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ใŸใจใˆใฐใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎๅ ดๅˆใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ ```python from transformers.integrations import HfDeepSpeedConfig from transformers import AutoModel import deepspeed ds_config = {...} # deepspeed config object or path to the file # must run before instantiating the model to detect zero 3 dschf = HfDeepSpeedConfig(ds_config) # keep this object alive model = AutoModel.from_pretrained("openai-community/gpt2") engine = deepspeed.initialize(model=model, config_params=ds_config, ...) ``` ใพใŸใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใชใ„ใƒขใƒ‡ใƒซใฎๅ ดๅˆ: ```python from transformers.integrations import HfDeepSpeedConfig from transformers import AutoModel, AutoConfig import deepspeed ds_config = {...} # deepspeed config object or path to the file # must run before instantiating the model to detect zero 3 dschf = HfDeepSpeedConfig(ds_config) # keep this object alive config = AutoConfig.from_pretrained("openai-community/gpt2") model = AutoModel.from_config(config) engine = deepspeed.initialize(model=model, config_params=ds_config, ...) ``` [`Trainer`] ็ตฑๅˆใ‚’ไฝฟ็”จใ—ใฆใ„ใชใ„ๅ ดๅˆใฏใ€ๅฎŒๅ…จใซ็‹ฌๅŠ›ใง่กŒใ†ใ“ใจใซใชใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ๅŸบๆœฌ็š„ใซใฏใ€[Deepspeed](https://www.deepspeed.ai/) Web ใ‚ตใ‚คใƒˆใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใซๅพ“ใฃใฆใใ ใ•ใ„ใ€‚ใพใŸใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚`"auto"`ๅ€คใฏไฝฟ็”จใงใใšใ€ไปฃใ‚ใ‚ŠใซๅฎŸ้š›ใฎๅ€คใ‚’ๅ…ฅๅŠ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ## HfDeepSpeedConfig [[autodoc]] integrations.HfDeepSpeedConfig - all ### Custom DeepSpeed ZeRO Inference ไปฅไธ‹ใฏใ€ๅ˜ไธ€ใฎ GPU ใซใƒขใƒ‡ใƒซใ‚’้ฉๅˆใงใใชใ„ๅ ดๅˆใซใ€[`Trainer`] ใ‚’ไฝฟ็”จใ›ใšใซ DeepSpeed ZeRO ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ใงใ™ใ€‚่งฃๆฑบ็ญ–ใซใฏใ€่ฟฝๅŠ ใฎ GPU ใฎไฝฟ็”จใ€ใพใŸใฏ GPU ใƒกใƒขใƒชใ‚’ CPU ใƒกใƒขใƒชใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใŒๅซใพใ‚Œใพใ™ใ€‚ ใ“ใ“ใง็†่งฃใ™ในใ้‡่ฆใชใƒ‹ใƒฅใ‚ขใƒณใ‚นใฏใ€ZeRO ใฎ่จญ่จˆๆ–นๆณ•ใซใ‚ˆใ‚Šใ€็•ฐใชใ‚‹ GPU ใง็•ฐใชใ‚‹ๅ…ฅๅŠ›ใ‚’ไธฆ่กŒใ—ใฆๅ‡ฆ็†ใงใใ‚‹ใจใ„ใ†ใ“ใจใงใ™ใ€‚ ใ“ใฎไพ‹ใซใฏๅคง้‡ใฎใƒกใƒขใŒใ‚ใ‚Šใ€่‡ชๅทฑๆ–‡ๆ›ธๅŒ–ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ ๅฟ…ใšๆฌกใฎใ“ใจใ‚’่กŒใฃใฆใใ ใ•ใ„ใ€‚ 1. ๅๅˆ†ใช GPU ใƒกใƒขใƒชใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’็„กๅŠนใซใ—ใพใ™ (้€ŸๅบฆใŒไฝŽไธ‹ใ™ใ‚‹ใŸใ‚)ใ€‚ 2. Ampere ใพใŸใฏๆ–ฐใ—ใ„ GPU ใ‚’ๆ‰€ๆœ‰ใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๅ‡ฆ็†ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซ bf16 ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ใใฎใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใŒใชใ„ๅ ดๅˆใฏใ€bf16 ๆททๅˆ็ฒพๅบฆใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซ (ใปใจใ‚“ใฉใฎ t5 ใƒขใƒ‡ใƒซใชใฉ) ใ‚’ไฝฟ็”จใ—ใชใ„้™ใ‚Šใ€fp16 ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏ้€šๅธธใ€fp16 ใงใ‚ชใƒผใƒใƒผใƒ•ใƒญใƒผใ—ใ€ๅ‡บๅŠ›ใจใ—ใฆใ‚ฌใƒ™ใƒผใ‚ธใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ ```python #!/usr/bin/env python # This script demonstrates how to use Deepspeed ZeRO in an inference mode when one can't fit a model # into a single GPU # # 1. Use 1 GPU with CPU offload # 2. Or use multiple GPUs instead # # First you need to install deepspeed: pip install deepspeed # # Here we use a 3B "bigscience/T0_3B" model which needs about 15GB GPU RAM - so 1 largish or 2 # small GPUs can handle it. or 1 small GPU and a lot of CPU memory. # # To use a larger model like "bigscience/T0" which needs about 50GB, unless you have an 80GB GPU - # you will need 2-4 gpus. And then you can adapt the script to handle more gpus if you want to # process multiple inputs at once. # # The provided deepspeed config also activates CPU memory offloading, so chances are that if you # have a lot of available CPU memory and you don't mind a slowdown you should be able to load a # model that doesn't normally fit into a single GPU. If you have enough GPU memory the program will # run faster if you don't want offload to CPU - so disable that section then. # # To deploy on 1 gpu: # # deepspeed --num_gpus 1 t0.py # or: # python -m torch.distributed.run --nproc_per_node=1 t0.py # # To deploy on 2 gpus: # # deepspeed --num_gpus 2 t0.py # or: # python -m torch.distributed.run --nproc_per_node=2 t0.py from transformers import AutoTokenizer, AutoConfig, AutoModelForSeq2SeqLM from transformers.integrations import HfDeepSpeedConfig import deepspeed import os import torch os.environ["TOKENIZERS_PARALLELISM"] = "false" # To avoid warnings about parallelism in tokenizers # distributed setup local_rank = int(os.getenv("LOCAL_RANK", "0")) world_size = int(os.getenv("WORLD_SIZE", "1")) torch.cuda.set_device(local_rank) deepspeed.init_distributed() model_name = "bigscience/T0_3B" config = AutoConfig.from_pretrained(model_name) model_hidden_size = config.d_model # batch size has to be divisible by world_size, but can be bigger than world_size train_batch_size = 1 * world_size # ds_config notes # # - enable bf16 if you use Ampere or higher GPU - this will run in mixed precision and will be # faster. # # - for older GPUs you can enable fp16, but it'll only work for non-bf16 pretrained models - e.g. # all official t5 models are bf16-pretrained # # - set offload_param.device to "none" or completely remove the `offload_param` section if you don't # - want CPU offload # # - if using `offload_param` you can manually finetune stage3_param_persistence_threshold to control # - which params should remain on gpus - the larger the value the smaller the offload size # # For in-depth info on Deepspeed config see # https://huggingface.co/docs/transformers/main/main_classes/deepspeed # keeping the same format as json for consistency, except it uses lower case for true/false # fmt: off ds_config = { "fp16": { "enabled": False }, "bf16": { "enabled": False }, "zero_optimization": { "stage": 3, "offload_param": { "device": "cpu", "pin_memory": True }, "overlap_comm": True, "contiguous_gradients": True, "reduce_bucket_size": model_hidden_size * model_hidden_size, "stage3_prefetch_bucket_size": 0.9 * model_hidden_size * model_hidden_size, "stage3_param_persistence_threshold": 10 * model_hidden_size }, "steps_per_print": 2000, "train_batch_size": train_batch_size, "train_micro_batch_size_per_gpu": 1, "wall_clock_breakdown": False } # fmt: on # next line instructs transformers to partition the model directly over multiple gpus using # deepspeed.zero.Init when model's `from_pretrained` method is called. # # **it has to be run before loading the model AutoModelForSeq2SeqLM.from_pretrained(model_name)** # # otherwise the model will first be loaded normally and only partitioned at forward time which is # less efficient and when there is little CPU RAM may fail dschf = HfDeepSpeedConfig(ds_config) # keep this object alive # now a model can be loaded. model = AutoModelForSeq2SeqLM.from_pretrained(model_name) # initialise Deepspeed ZeRO and store only the engine object ds_engine = deepspeed.initialize(model=model, config_params=ds_config)[0] ds_engine.module.eval() # inference # Deepspeed ZeRO can process unrelated inputs on each GPU. So for 2 gpus you process 2 inputs at once. # If you use more GPUs adjust for more. # And of course if you have just one input to process you then need to pass the same string to both gpus # If you use only one GPU, then you will have only rank 0. rank = torch.distributed.get_rank() if rank == 0: text_in = "Is this review positive or negative? Review: this is the best cast iron skillet you will ever buy" elif rank == 1: text_in = "Is this review positive or negative? Review: this is the worst restaurant ever" tokenizer = AutoTokenizer.from_pretrained(model_name) inputs = tokenizer.encode(text_in, return_tensors="pt").to(device=local_rank) with torch.no_grad(): outputs = ds_engine.module.generate(inputs, synced_gpus=True) text_out = tokenizer.decode(outputs[0], skip_special_tokens=True) print(f"rank{rank}:\n in={text_in}\n out={text_out}") ``` ใใ‚Œใ‚’`t0.py`ใจใ—ใฆไฟๅญ˜ใ—ใฆๅฎŸ่กŒใ—ใพใ—ใ‚‡ใ†ใ€‚ ```bash $ deepspeed --num_gpus 2 t0.py rank0: in=Is this review positive or negative? Review: this is the best cast iron skillet you will ever buy out=Positive rank1: in=Is this review positive or negative? Review: this is the worst restaurant ever out=negative ``` ใ“ใ‚Œใฏ้žๅธธใซๅŸบๆœฌ็š„ใชไพ‹ใงใ‚ใ‚Šใ€ใƒ‹ใƒผใ‚บใซๅˆใ‚ใ›ใฆ่ชฟๆ•ดใ—ใฆใใ ใ•ใ„ใ€‚ ### `generate` nuances ZeRO Stage-3 ใง่ค‡ๆ•ฐใฎ GPU ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`generate(..., synced_gpus=True)`ใ‚’ๅ‘ผใณๅ‡บใ—ใฆ GPU ใ‚’ๅŒๆœŸใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚’่กŒใ‚ใชใ„ใจใ€1 ใคใฎ GPU ใŒไป–ใฎ GPU ใ‚ˆใ‚Šๅ…ˆใซ็”Ÿๆˆใ‚’็ต‚ไบ†ใ—ใŸๅ ดๅˆใ€ๆฎ‹ใ‚Šใฎ GPU ใŒ็”Ÿๆˆใ‚’ๅœๆญขใ—ใŸ GPU ใ‹ใ‚‰ใ‚ฆใ‚งใ‚คใƒˆใฎใ‚ทใƒฃใƒผใƒ‰ใ‚’ๅ—ไฟกใงใใชใใชใ‚‹ใŸใ‚ใ€ใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใŒใƒใƒณใ‚ฐใ—ใพใ™ใ€‚ `transformers>=4.28` ไปฅ้™ใ€`synced_gpus` ใŒๆ˜Ž็คบ็š„ใซๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€ใ“ใ‚Œใ‚‰ใฎๆกไปถใŒๆคœๅ‡บใ•ใ‚Œใ‚‹ใจ่‡ชๅ‹•็š„ใซ `True` ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆ `synced_gpus` ใฎๅ€คใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ## Deepspeed ็ตฑๅˆใฎใƒ†ใ‚นใƒˆ DeepSpeed ็ตฑๅˆใ‚’ๅซใ‚€ PR ใ‚’้€ไฟกใ™ใ‚‹ๅ ดๅˆใฏใ€CircleCI PR CI ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใซใฏ GPU ใŒใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใใฎใŸใ‚ใ€GPU ใ‚’ๅฟ…่ฆใจใ™ใ‚‹ใƒ†ใ‚นใƒˆใฏๅˆฅใฎ CI ใงๆฏŽๆ™ฉใฎใฟๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€PR ใง็ท‘่‰ฒใฎ CI ใƒฌใƒใƒผใƒˆใŒ่กจ็คบใ•ใ‚Œใฆใ‚‚ใ€DeepSpeed ใƒ†ใ‚นใƒˆใŒๅˆๆ ผใ—ใŸใ“ใจใ‚’ๆ„ๅ‘ณใ™ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ DeepSpeed ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๅฐ‘ใชใใจใ‚‚ไปฅไธ‹ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„ใ€‚ ```bash RUN_SLOW=1 pytest tests/deepspeed/test_deepspeed.py ``` ใƒขใƒ‡ใƒชใƒณใ‚ฐใพใŸใฏ pytorch ใ‚ตใƒณใƒ—ใƒซ ใ‚ณใƒผใƒ‰ใฎใ„ใšใ‚Œใ‹ใ‚’ๅค‰ๆ›ดใ—ใŸๅ ดๅˆใฏใ€Model Zoo ใƒ†ใ‚นใƒˆใ‚‚ๅฎŸ่กŒใ—ใพใ™ใ€‚ไปฅไธ‹ใฏใ™ในใฆใฎ DeepSpeed ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ ```bash RUN_SLOW=1 pytest tests/deepspeed ``` ## Main DeepSpeed Resources - [ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใฎ github](https://github.com/microsoft/deepspeed) - [ไฝฟ็”จๆ–นๆณ•ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/getting-started/) - [API ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://deepspeed.readthedocs.io/en/latest/index.html) - [ใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://www.microsoft.com/en-us/research/search/?q=deepspeed) ่ซ–ๆ–‡: - [ZeRO: ๅ…†ใƒ‘ใƒฉใƒกใƒผใ‚ฟ ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅ‘ใ‘ใŸใƒกใƒขใƒชใฎๆœ€้ฉๅŒ–](https://arxiv.org/abs/1910.02054) - [ZeRO-Offload: 10 ๅ„„่ฆๆจกใฎใƒขใƒ‡ใƒซ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๆฐ‘ไธปๅŒ–](https://arxiv.org/abs/2101.06840) - [ZeRO-Infinity: ๆฅต้™ใ‚นใ‚ฑใƒผใƒซใฎๆทฑๅฑคๅญฆ็ฟ’ใฎใŸใ‚ใฎ GPU ใƒกใƒขใƒชใฎๅฃใ‚’ๆ‰“ใก็ ดใ‚‹](https://arxiv.org/abs/2104.07857) ๆœ€ๅพŒใซใ€HuggingFace [`Trainer`] ใฏ DeepSpeed ใฎใฟใ‚’็ตฑๅˆใ—ใฆใ„ใ‚‹ใ“ใจใ‚’่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„ใ€‚ DeepSpeed ใฎไฝฟ็”จใซ้–ขใ—ใฆๅ•้กŒใ‚„่ณชๅ•ใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€[DeepSpeed GitHub](https://github.com/microsoft/DeepSpeed/issues) ใซๅ•้กŒใ‚’ๆๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚
transformers/docs/source/ja/main_classes/deepspeed.md/0
{ "file_path": "transformers/docs/source/ja/main_classes/deepspeed.md", "repo_id": "transformers", "token_count": 49427 }
290
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # ALIGN ## ๆฆ‚่ฆ ALIGNใƒขใƒ‡ใƒซใฏใ€ใ€Œ[Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision](https://arxiv.org/abs/2102.05918)ใ€ใจใ„ใ†่ซ–ๆ–‡ใงChao Jiaใ€Yinfei Yangใ€Ye Xiaใ€Yi-Ting Chenใ€Zarana Parekhใ€Hieu Phamใ€Quoc V. Leใ€Yunhsuan Sungใ€Zhen Liใ€Tom Duerigใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ALIGNใฏใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใช่ฆ–่ฆš่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚ใ“ใ‚Œใฏ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎ้กžไผผๅบฆใ‚„ใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใซไฝฟ็”จใงใใพใ™ใ€‚ALIGNใฏ[EfficientNet](efficientnet)ใ‚’่ฆ–่ฆšใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใ—ใฆใ€[BERT](bert)ใ‚’ใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใ—ใฆๆญ่ผ‰ใ—ใŸใƒ‡ใƒฅใ‚ขใƒซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผๆง‹้€ ใ‚’็‰นๅพดใจใ—ใ€ๅฏพ็…งๅญฆ็ฟ’ใซใ‚ˆใฃใฆ่ฆ–่ฆšใจใƒ†ใ‚ญใ‚นใƒˆใฎ่กจ็พใ‚’ๆ•ดๅˆใ•ใ›ใ‚‹ใ“ใจใ‚’ๅญฆใณใพใ™ใ€‚ใใ‚Œใพใงใฎ็ ”็ฉถใจใฏ็•ฐใชใ‚Šใ€ALIGNใฏๅทจๅคงใงใƒŽใ‚คใ‚ธใƒผใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆดป็”จใ—ใ€ใ‚ณใƒผใƒ‘ใ‚นใฎใ‚นใ‚ฑใƒผใƒซใ‚’ๅˆฉ็”จใ—ใฆๅ˜็ด”ใชๆ–นๆณ•ใชใŒใ‚‰ๆœ€ๅ…ˆ็ซฏใฎ่กจ็พใ‚’้”ๆˆใงใใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ ่ซ–ๆ–‡ใฎ่ฆๆ—จใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš *ไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚ŒใŸ่กจ็พใฏใ€ๅคšใใฎ่‡ช็„ถ่จ€่ชžๅ‡ฆ็†๏ผˆNLP๏ผ‰ใŠใ‚ˆใณ็Ÿฅ่ฆšใ‚ฟใ‚นใ‚ฏใซใจใฃใฆ้‡่ฆใซใชใฃใฆใ„ใพใ™ใ€‚NLPใซใŠใ‘ใ‚‹่กจ็พๅญฆ็ฟ’ใฏใ€ไบบ้–“ใฎใ‚ขใƒŽใƒ†ใƒผใ‚ทใƒงใƒณใฎใชใ„็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใงใฎๅญฆ็ฟ’ใธใจ็งป่กŒใ—ใฆใ„ใพใ™ใŒใ€่ฆ–่ฆšใŠใ‚ˆใณ่ฆ–่ฆš่จ€่ชžใฎ่กจ็พใฏไพ็„ถใจใ—ใฆ็ฒพๅทงใชๅญฆ็ฟ’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅคงใใไพๅญ˜ใ—ใฆใŠใ‚Šใ€ใ“ใ‚Œใฏ้ซ˜ไพกใงใ‚ใฃใŸใ‚Šๅฐ‚้–€็Ÿฅ่ญ˜ใ‚’ๅฟ…่ฆใจใ—ใŸใ‚Šใ—ใพใ™ใ€‚่ฆ–่ฆšใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใฎๅ ดๅˆใ€ImageNetใ‚„OpenImagesใฎใ‚ˆใ†ใชๆ˜Ž็คบ็š„ใชใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใ‚’ๆŒใคใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆๅญฆ็ฟ’ใ•ใ‚Œใ‚‹ใ“ใจใŒใปใจใ‚“ใฉใงใ™ใ€‚่ฆ–่ฆš่จ€่ชžใฎๅ ดๅˆใ€Conceptual Captionsใ€MSCOCOใ€CLIPใชใฉใฎไบบๆฐ—ใฎใ‚ใ‚‹ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏใ™ในใฆใ€ใใ‚Œใžใ‚Œ็„ก่ฆ–ใงใใชใ„ใƒ‡ใƒผใ‚ฟๅŽ้›†๏ผˆใŠใ‚ˆใณใ‚ฏใƒชใƒผใƒ‹ใƒณใ‚ฐ๏ผ‰ใƒ—ใƒญใ‚ปใ‚นใ‚’ๅซใฟใพใ™ใ€‚ใ“ใฎใ‚ณใ‚นใƒˆใฎใ‹ใ‹ใ‚‹ใ‚ญใƒฅใƒฌใƒผใ‚ทใƒงใƒณใƒ—ใƒญใ‚ปใ‚นใฏใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใ‚คใ‚บใ‚’ๅˆถ้™ใ—ใ€่จ“็ทดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ‚’ๅฆจใ’ใพใ™ใ€‚ๆœฌ่ซ–ๆ–‡ใงใฏใ€Conceptual Captionsใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ้ซ˜ไพกใชใƒ•ใ‚ฃใƒซใ‚ฟใƒชใƒณใ‚ฐใ‚„ๅพŒๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใชใ—ใงๅพ—ใ‚‰ใ‚ŒใŸใ€10ๅ„„ใ‚’่ถ…ใˆใ‚‹็”ปๅƒalt-textใƒšใ‚ขใฎใƒŽใ‚คใ‚บใฎๅคšใ„ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆดป็”จใ—ใพใ™ใ€‚ใ‚ทใƒณใƒ—ใƒซใชใƒ‡ใƒฅใ‚ขใƒซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏใ€ๅฏพ็…งๆๅคฑใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใƒšใ‚ขใฎ่ฆ–่ฆš็š„ใŠใ‚ˆใณ่จ€่ชž็š„่กจ็พใ‚’ๆ•ดๅˆใ•ใ›ใ‚‹ใ“ใจใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ๆˆ‘ใ€…ใฏใ€ใ‚ณใƒผใƒ‘ใ‚นใฎ่ฆๆจกใŒใใฎใƒŽใ‚คใ‚บใ‚’่ฃœใ„ใ€ใ“ใฎใ‚ˆใ†ใชๅ˜็ด”ใชๅญฆ็ฟ’ใ‚นใ‚ญใƒผใƒ ใงใ‚‚ๆœ€ๅ…ˆ็ซฏใฎ่กจ็พใซใคใชใŒใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ๆˆ‘ใ€…ใฎ่ฆ–่ฆš่กจ็พใฏใ€ImageNetใ‚„VTABใชใฉใฎๅˆ†้กžใ‚ฟใ‚นใ‚ฏใธใฎ่ปข็งปใซใŠใ„ใฆๅผทๅŠ›ใชๆ€ง่ƒฝใ‚’็™บๆฎใ—ใพใ™ใ€‚ๆ•ดๅˆใ—ใŸ่ฆ–่ฆš็š„ใŠใ‚ˆใณ่จ€่ชž็š„่กจ็พใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใ‚’ๅฏ่ƒฝใซใ—ใ€ใพใŸใ€ใ‚ˆใ‚Šๆด—็ทดใ•ใ‚ŒใŸใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒขใƒ‡ใƒซใจๆฏ”่ผƒใ—ใฆใ‚‚ใ€Flickr30KใŠใ‚ˆใณMSCOCO็”ปๅƒใƒ†ใ‚ญใ‚นใƒˆๆคœ็ดขใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซใŠใ„ใฆๆ–ฐใŸใชๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใ‚’้”ๆˆใ—ใพใ™ใ€‚ใพใŸใ€ใ“ใ‚Œใ‚‰ใฎ่กจ็พใฏใ€่ค‡้›‘ใชใƒ†ใ‚ญใ‚นใƒˆใŠใ‚ˆใณใƒ†ใ‚ญใ‚นใƒˆ+็”ปๅƒใฎใ‚ฏใ‚จใƒชใ‚’็”จใ„ใŸใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซๆคœ็ดขใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚* ใ“ใฎใƒขใƒ‡ใƒซใฏ[Alara Dirik](https://huggingface.co/adirik)ใซใ‚ˆใ‚Šๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใ‚ณใƒผใƒ‰ใฏๅ…ฌ้–‹ใ•ใ‚ŒใฆใŠใ‚‰ใšใ€ใ“ใฎๅฎŸ่ฃ…ใฏๅ…ƒ่ซ–ๆ–‡ใซๅŸบใฅใ„ใŸKakao BrainใฎๅฎŸ่ฃ…ใ‚’ใƒ™ใƒผใ‚นใซใ—ใฆใ„ใพใ™ใ€‚ ## ไฝฟ็”จไพ‹ ALIGNใฏEfficientNetใ‚’ไฝฟ็”จใ—ใฆ่ฆ–่ฆš็š„็‰นๅพดใ‚’ใ€BERTใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆ็‰นๅพดใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใจ่ฆ–่ฆšใฎไธกๆ–นใฎ็‰นๅพดใฏใ€ๅŒไธ€ใฎๆฌกๅ…ƒใ‚’ๆŒใคๆฝœๅœจ็ฉบ้–“ใซๅฐ„ๅฝฑใ•ใ‚Œใพใ™ใ€‚ๅฐ„ๅฝฑใ•ใ‚ŒใŸ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆ็‰นๅพด้–“ใฎใƒ‰ใƒƒใƒˆ็ฉใŒ้กžไผผๅบฆใ‚นใ‚ณใ‚ขใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ [`AlignProcessor`]ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚จใƒณใ‚ณใƒผใƒ‰ใจ็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใ‚’ไธกๆ–น่กŒใ†ใŸใ‚ใซใ€[`EfficientNetImageProcessor`]ใจ[`BertTokenizer`]ใ‚’ๅ˜ไธ€ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใซใƒฉใƒƒใƒ—ใ—ใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใฏใ€[`AlignProcessor`]ใจ[`AlignModel`]ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒ-ใƒ†ใ‚ญใ‚นใƒˆ้กžไผผๅบฆใ‚นใ‚ณใ‚ขใ‚’ๅ–ๅพ—ใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ ```python import requests import torch from PIL import Image from transformers import AlignProcessor, AlignModel processor = AlignProcessor.from_pretrained("kakaobrain/align-base") model = AlignModel.from_pretrained("kakaobrain/align-base") url = "http://images.cocodataset.org/val2017/000000039769.jpg" image = Image.open(requests.get(url, stream=True).raw) candidate_labels = ["an image of a cat", "an image of a dog"] inputs = processor(text=candidate_labels, images=image, return_tensors="pt") with torch.no_grad(): outputs = model(**inputs) # this is the image-text similarity score logits_per_image = outputs.logits_per_image # we can take the softmax to get the label probabilities probs = logits_per_image.softmax(dim=1) print(probs) ``` ## ๅ‚่€ƒ่ณ‡ๆ–™ ALIGNใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผใฎHugging Faceใจใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ๏ผˆ๐ŸŒŽใง็คบใ•ใ‚Œใฆใ„ใ‚‹๏ผ‰ใฎๅ‚่€ƒ่ณ‡ๆ–™ใฎไธ€่ฆงใงใ™ใ€‚ - [ALIGNใจCOYO-700Mใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://huggingface.co/blog/vit-align)ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟใ€‚ - ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กž[ใƒ‡ใƒข](https://huggingface.co/spaces/adirik/ALIGN-zero-shot-image-classification)ใ€‚ - `kakaobrain/align-base` ใƒขใƒ‡ใƒซใฎ[ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰](https://huggingface.co/kakaobrain/align-base)ใ€‚ ใ“ใ“ใซๅ‚่€ƒ่ณ‡ๆ–™ใ‚’ๆๅ‡บใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฐ—ๅ…ผใญใชใPull Requestใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚็งใŸใกใฏใใ‚Œใ‚’ใƒฌใƒ“ใƒฅใƒผใ„ใŸใ—ใพใ™๏ผๅ‚่€ƒ่ณ‡ๆ–™ใฏใ€ๆ—ขๅญ˜ใฎใ‚‚ใฎใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ“ใจใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ ## AlignConfig [[autodoc]] AlignConfig - from_text_vision_configs ## AlignTextConfig [[autodoc]] AlignTextConfig ## AlignVisionConfig [[autodoc]] AlignVisionConfig ## AlignProcessor [[autodoc]] AlignProcessor ## AlignModel [[autodoc]] AlignModel - forward - get_text_features - get_image_features ## AlignTextModel [[autodoc]] AlignTextModel - forward ## AlignVisionModel [[autodoc]] AlignVisionModel - forward
transformers/docs/source/ja/model_doc/align.md/0
{ "file_path": "transformers/docs/source/ja/model_doc/align.md", "repo_id": "transformers", "token_count": 2911 }
291
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # BioGPT ## Overview BioGPT ใƒขใƒ‡ใƒซใฏใ€[BioGPT: generative pre-trained transformer for biomedical text generation and mining](https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbac409/6713511?guestAccessKey=a66d9b5d-4f83-4017-bb52-405815c907b9) by Renqian Luoใ€Liai Sunใ€Yingce Xiaใ€ Tao Qinใ€Sheng Zhangใ€Hoifung Poonใ€Tie-Yan Liuใ€‚ BioGPT ใฏใ€็”Ÿ็‰ฉๅŒปๅญฆใƒ†ใ‚ญใ‚นใƒˆใฎ็”Ÿๆˆใจใƒžใ‚คใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใฎใ€ใƒ‰ใƒกใ‚คใƒณๅ›บๆœ‰ใฎ็”Ÿๆˆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ Transformer ่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚ BioGPT ใฏใ€Transformer ่จ€่ชžใƒขใƒ‡ใƒซใฎใƒใƒƒใ‚ฏใƒœใƒผใƒณใซๅพ“ใ„ใ€1,500 ไธ‡ใฎ PubMed ๆŠ„้Œฒใงๆœ€ๅˆใ‹ใ‚‰ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ ่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ *ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ไธ€่ˆฌ็š„ใช่‡ช็„ถ่จ€่ชž้ ˜ๅŸŸใงใฎๅคงใใชๆˆๅŠŸใซ่งฆ็™บใ•ใ‚Œใฆใ€็”Ÿ็‰ฉๅŒปๅญฆ้ ˜ๅŸŸใงใพใ™ใพใ™ๆณจ็›ฎใ‚’้›†ใ‚ใฆใ„ใพใ™ใ€‚ไธ€่ˆฌ่จ€่ชžใƒ‰ใƒกใ‚คใƒณใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใฎ 2 ใคใฎไธปใชใƒ–ใƒฉใƒณใƒใ€ใคใพใ‚Š BERT (ใŠใ‚ˆใณใใฎใƒใƒชใ‚ขใƒณใƒˆ) ใจ GPT (ใŠใ‚ˆใณใใฎใƒใƒชใ‚ขใƒณใƒˆ) ใฎใ†ใกใ€1 ใค็›ฎใฏ BioBERT ใ‚„ PubMedBERT ใชใฉใฎ็”Ÿ็‰ฉๅŒปๅญฆใƒ‰ใƒกใ‚คใƒณใงๅบƒใ็ ”็ฉถใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏใ•ใพใ–ใพใชไธ‹ๆตใฎ็”Ÿ็‰ฉๅŒปๅญฆ็š„ใ‚ฟใ‚นใ‚ฏใงๅคงใใชๆˆๅŠŸใ‚’ๅŽใ‚ใฆใ„ใพใ™ใŒใ€็”Ÿๆˆ่ƒฝๅŠ›ใฎๆฌ ๅฆ‚ใซใ‚ˆใ‚Šๅฟœ็”จ็ฏ„ๅ›ฒใŒๅˆถ้™ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ๅคง่ฆๆจกใช็”Ÿ็‰ฉๅŒปๅญฆๆ–‡็Œฎใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‰ใƒกใ‚คใƒณๅ›บๆœ‰ใฎ็”Ÿๆˆ Transformer ่จ€่ชžใƒขใƒ‡ใƒซใงใ‚ใ‚‹ BioGPT ใ‚’ๆๆกˆใ—ใพใ™ใ€‚็งใŸใกใฏ 6 ใคใฎ็”Ÿ็‰ฉๅŒปๅญฆ็š„่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ใ‚ฟใ‚นใ‚ฏใง BioGPT ใ‚’่ฉ•ไพกใ—ใ€ใปใจใ‚“ใฉใฎใ‚ฟใ‚นใ‚ฏใง็งใŸใกใฎใƒขใƒ‡ใƒซใŒไปฅๅ‰ใฎใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’ๅฎŸ่จผใ—ใพใ—ใŸใ€‚็‰นใซใ€BC5CDRใ€KD-DTIใ€DDI ใฎใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰้–ขไฟ‚ๆŠฝๅ‡บใ‚ฟใ‚นใ‚ฏใงใฏใใ‚Œใžใ‚Œ 44.98%ใ€38.42%ใ€40.76% ใฎ F1 ใ‚นใ‚ณใ‚ขใ‚’็ฒๅพ—ใ—ใ€PubMedQA ใงใฏ 78.2% ใฎ็ฒพๅบฆใ‚’็ฒๅพ—ใ—ใ€ๆ–ฐ่จ˜้Œฒใ‚’ๆจน็ซ‹ใ—ใพใ—ใŸใ€‚ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใซ้–ขใ™ใ‚‹็งใŸใกใฎใ‚ฑใƒผใ‚นใ‚นใ‚ฟใƒ‡ใ‚ฃใฏใ€็”Ÿ็‰ฉๅŒปๅญฆๆ–‡็ŒฎใซใŠใ‘ใ‚‹ BioGPT ใฎๅˆฉ็‚นใ‚’ใ•ใ‚‰ใซๅฎŸ่จผใ—ใ€็”Ÿ็‰ฉๅŒปๅญฆ็”จ่ชžใฎๆตๆšขใช่ชฌๆ˜Žใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚* ## Usage tips - BioGPT ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅทฆๅดใงใฏใชใๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ - BioGPT ใฏๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ (CLM) ็›ฎ็š„ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใฎใซๅผทๅŠ›ใงใ™ใ€‚ run_generation.py ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใง็ขบ่ชใงใใ‚‹ใ‚ˆใ†ใซใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ๅˆฉ็”จใ™ใ‚‹ใจใ€BioGPT ใฏๆง‹ๆ–‡็š„ใซไธ€่ฒซใ—ใŸใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ - ใƒขใƒ‡ใƒซใฏใ€ไปฅๅ‰ใซ่จˆ็ฎ—ใ•ใ‚ŒใŸใ‚ญใƒผใจๅ€คใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒšใ‚ขใงใ‚ใ‚‹`past_key_values`(PyTorch ใฎๅ ดๅˆ) ใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใ“ใฎ (past_key_values ใพใŸใฏ past) ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใŒใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใงไบ‹ๅ‰ใซ่จˆ็ฎ—ใ•ใ‚ŒใŸๅ€คใ‚’ๅ†่จˆ็ฎ—ใงใใชใใชใ‚Šใพใ™ใ€‚ PyTorch ใฎไฝฟ็”จๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€BioGptForCausalLM.forward() ใƒกใ‚ฝใƒƒใƒ‰ใฎ past_key_values ๅผ•ๆ•ฐใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[kamalkraj](https://huggingface.co/kamalkraj) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/microsoft/BioGPT) ใซใ‚ใ‚Šใพใ™ใ€‚ ## Documentation resources - [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) ## BioGptConfig [[autodoc]] BioGptConfig ## BioGptTokenizer [[autodoc]] BioGptTokenizer - save_vocabulary ## BioGptModel [[autodoc]] BioGptModel - forward ## BioGptForCausalLM [[autodoc]] BioGptForCausalLM - forward ## BioGptForTokenClassification [[autodoc]] BioGptForTokenClassification - forward ## BioGptForSequenceClassification [[autodoc]] BioGptForSequenceClassification - forward
transformers/docs/source/ja/model_doc/biogpt.md/0
{ "file_path": "transformers/docs/source/ja/model_doc/biogpt.md", "repo_id": "transformers", "token_count": 1982 }
292
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # CLIPSeg ## Overview CLIPSeg ใƒขใƒ‡ใƒซใฏใ€Timo Lรผddecke, Alexander Ecker ใซใ‚ˆใฃใฆ [Image Segmentation using Text and Image Prompts](https://arxiv.org/abs/2112.10003) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ ใใ—ใฆใ‚ขใƒฌใ‚ฏใ‚ตใƒณใƒ€ใƒผใƒปใ‚จใƒƒใ‚ซใƒผใ€‚ CLIPSeg ใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใŠใ‚ˆใณใƒฏใƒณใ‚ทใƒงใƒƒใƒˆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎใŸใ‚ใซใ€ๅ‡็ตใ•ใ‚ŒใŸ [CLIP](clip) ใƒขใƒ‡ใƒซใฎไธŠใซๆœ€ๅฐ้™ใฎใƒ‡ใ‚ณใƒผใƒ€ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ ่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ *็”ปๅƒใฎใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏ้€šๅธธใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ‚ˆใฃใฆ่งฃๆฑบใ•ใ‚Œใพใ™ใ€‚ ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใƒฉใ‚นใฎๅ›บๅฎšใ‚ปใƒƒใƒˆใฎใƒขใƒ‡ใƒซใ€‚ๅพŒใง่ฟฝๅŠ ใฎใ‚ฏใƒฉใ‚นใ‚„ใ‚ˆใ‚Š่ค‡้›‘ใชใ‚ฏใ‚จใƒชใ‚’็ต„ใฟ่พผใ‚€ใจใ‚ณใ‚นใƒˆใŒใ‹ใ‹ใ‚Šใพใ™ ใ“ใ‚Œใ‚‰ใฎๅผใ‚’ๅซใ‚€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’ๅ†ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ใ“ใ“ใงใ‚ทใ‚นใƒ†ใƒ ใ‚’ๆๆกˆใ—ใพใ™ ไปปๆ„ใฎๆƒ…ๅ ฑใซๅŸบใฅใ„ใฆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ ใƒ†ใ‚นใƒˆๆ™‚ใซใƒ—ใƒญใƒณใƒ—โ€‹โ€‹ใƒˆใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใฏใƒ†ใ‚ญใ‚นใƒˆใพใŸใฏ ็”ปๅƒใ€‚ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใซใ‚ˆใ‚Šใ€็ตฑไธ€ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใงใใพใ™ใ€‚ 3 ใคใฎไธ€่ˆฌ็š„ใชใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใซใคใ„ใฆ (1 ๅ›žใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ) ๅ‚็…งๅผใฎใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใƒฏใƒณใ‚ทใƒงใƒƒใƒˆ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใจใ„ใ†ๆ˜Ž็ขบใช่ชฒ้กŒใŒไผดใ„ใพใ™ใ€‚ CLIP ใƒขใƒ‡ใƒซใ‚’ใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ—ใฆๆง‹็ฏ‰ใ—ใ€ใ“ใ‚Œใ‚’ใƒˆใƒฉใƒณใ‚นใƒ™ใƒผใ‚นใฎใƒ‡ใ‚ณใƒผใƒ€ใงๆ‹กๅผตใ—ใฆใ€้ซ˜ๅฏ†ๅบฆใชใƒ‡ใƒผใ‚ฟ้€šไฟกใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ ไบˆๆธฌใ€‚ใฎๆ‹กๅผตใƒใƒผใ‚ธใƒงใƒณใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใŸๅพŒใ€ PhraseCut ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€็งใŸใกใฎใ‚ทใ‚นใƒ†ใƒ ใฏใ€ใƒ•ใƒชใƒผใƒ†ใ‚ญใ‚นใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใพใŸใฏ ใ‚ฏใ‚จใƒชใ‚’่กจใ™่ฟฝๅŠ ใฎ็”ปๅƒใ€‚ๅพŒ่€…ใฎ็”ปๅƒใƒ™ใƒผใ‚นใฎใƒ—ใƒญใƒณใƒ—ใƒˆใฎใ•ใพใ–ใพใชใƒใƒชใ‚จใƒผใ‚ทใƒงใƒณใ‚’่ฉณ็ดฐใซๅˆ†ๆžใ—ใพใ™ใ€‚ ใ“ใฎๆ–ฐใ—ใ„ใƒใ‚คใƒ–ใƒชใƒƒใƒ‰ๅ…ฅๅŠ›ใซใ‚ˆใ‚Šใ€ๅ‹•็š„้ฉๅฟœใŒๅฏ่ƒฝใซใชใ‚Šใพใ™ใ€‚ ๅ‰่ฟฐใฎ 3 ใคใฎใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใฎใฟใงใ™ใŒใ€ ใƒ†ใ‚ญใ‚นใƒˆใพใŸใฏ็”ปๅƒใ‚’ใ‚ฏใ‚จใƒชใ™ใ‚‹ใƒใ‚คใƒŠใƒช ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใซ ๅฎšๅผๅŒ–ใ™ใ‚‹ใ“ใจใŒใงใใ‚‹ใ€‚ๆœ€ๅพŒใซใ€ใ‚ทใ‚นใƒ†ใƒ ใŒใ†ใพใ้ฉๅฟœใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ—ใŸ ใ‚ขใƒ•ใ‚ฉใƒผใƒ€ใƒณใ‚นใพใŸใฏใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’ๅซใ‚€ไธ€่ˆฌๅŒ–ใ•ใ‚ŒใŸใ‚ฏใ‚จใƒช* <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/clipseg_architecture.png" alt="ๆ็”ป" width="600"/> <small> CLIPSeg ใฎๆฆ‚่ฆใ€‚ <a href="https://arxiv.org/abs/2112.10003">ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚</a> </small> ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/timojl/clipseg) ใซใ‚ใ‚Šใพใ™ใ€‚ ## Usage tips - [`CLIPSegForImageSegmentation`] ใฏใ€[`CLIPSegModel`] ใฎไธŠใซใƒ‡ใ‚ณใƒผใƒ€ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ๅพŒ่€…ใฏ [`CLIPModel`] ใจๅŒใ˜ใงใ™ใ€‚ - [`CLIPSegForImageSegmentation`] ใฏใ€ใƒ†ใ‚นใƒˆๆ™‚ใซไปปๆ„ใฎใƒ—ใƒญใƒณใƒ—ใƒˆใซๅŸบใฅใ„ใฆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใฏใƒ†ใ‚ญใ‚นใƒˆใฎใ„ใšใ‚Œใ‹ใงใ™ (`input_ids` ใจใ—ใฆใƒขใƒ‡ใƒซใซๆไพ›ใ•ใ‚Œใ‚‹) ใพใŸใฏ็”ปๅƒ (`conditional_pixel_values` ใจใ—ใฆใƒขใƒ‡ใƒซใซๆไพ›ใ•ใ‚Œใ‚‹)ใ€‚ใ‚ซใ‚นใ‚ฟใƒ ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ ๆกไปถไป˜ใๅŸ‹ใ‚่พผใฟ (`conditional_embeddings`ใจใ—ใฆใƒขใƒ‡ใƒซใซๆไพ›ใ•ใ‚Œใพใ™)ใ€‚ ## Resources CLIPSeg ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคใ€ๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ <PipelineTag pipeline="image-segmentation"/> - [CLIPSeg ใ‚’ไฝฟ็”จใ—ใŸใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/CLIPSeg/Zero_shot_image_segmentation_with_CLIPSeg.ipynb) ใ‚’่ชฌๆ˜Žใ™ใ‚‹ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ€‚ ## CLIPSegConfig [[autodoc]] CLIPSegConfig - from_text_vision_configs ## CLIPSegTextConfig [[autodoc]] CLIPSegTextConfig ## CLIPSegVisionConfig [[autodoc]] CLIPSegVisionConfig ## CLIPSegProcessor [[autodoc]] CLIPSegProcessor ## CLIPSegModel [[autodoc]] CLIPSegModel - forward - get_text_features - get_image_features ## CLIPSegTextModel [[autodoc]] CLIPSegTextModel - forward ## CLIPSegVisionModel [[autodoc]] CLIPSegVisionModel - forward ## CLIPSegForImageSegmentation [[autodoc]] CLIPSegForImageSegmentation - forward
transformers/docs/source/ja/model_doc/clipseg.md/0
{ "file_path": "transformers/docs/source/ja/model_doc/clipseg.md", "repo_id": "transformers", "token_count": 2201 }
293
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Deformable DETR ## Overview ๅค‰ๅฝขๅฏ่ƒฝ DETR ใƒขใƒ‡ใƒซใฏใ€Xizhou Zhuใ€Weijie Suใ€Lewei Luใ€Bin Liใ€Xiaogang Wang, Jifeng Dai ใซใ‚ˆใฃใฆ [Deformable DETR: Deformable Transformers for End-to-End Object Detection](https://arxiv.org/abs/2010.04159) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸ ๅค‰ๅฝขๅฏ่ƒฝใช DETR ใฏใ€ๅ‚็…งๅ‘จๅ›ฒใฎๅฐ‘ๆ•ฐใฎไธป่ฆใชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒใ‚คใƒณใƒˆใฎใฟใซๆณจ็›ฎใ™ใ‚‹ๆ–ฐใ—ใ„ๅค‰ๅฝขๅฏ่ƒฝใชใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅˆฉ็”จใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ๅŽๆŸใฎ้…ใ•ใฎๅ•้กŒใจๅ…ƒใฎ [DETR](detr) ใฎๅˆถ้™ใ•ใ‚ŒใŸ็‰นๅพดใฎ็ฉบ้–“่งฃๅƒๅบฆใ‚’่ปฝๆธ›ใ—ใพใ™ใ€‚ ่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ *DETR ใฏใ€ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ่จผใ—ใชใŒใ‚‰ใ€็‰ฉไฝ“ๆคœๅ‡บใซใŠใ‘ใ‚‹ๅคšใใฎๆ‰‹ไฝœๆฅญใง่จญ่จˆใ•ใ‚ŒใŸใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎๅฟ…่ฆๆ€งใ‚’ๆŽ’้™คใ™ใ‚‹ใŸใ‚ใซๆœ€่ฟ‘ๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใŸใ ใ—ใ€็”ปๅƒ็‰นๅพดใƒžใƒƒใƒ—ใฎๅ‡ฆ็†ใซใŠใ‘ใ‚‹ Transformer ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒขใ‚ธใƒฅใƒผใƒซใฎๅˆถ้™ใซใ‚ˆใ‚Šใ€ๅŽๆŸใŒ้…ใใ€็‰นๅพดใฎ็ฉบ้–“่งฃๅƒๅบฆใŒๅˆถ้™ใ•ใ‚Œใ‚‹ใจใ„ใ†ๅ•้กŒใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅ•้กŒใ‚’่ปฝๆธ›ใ™ใ‚‹ใŸใ‚ใซใ€็งใŸใกใฏ Deformable DETR ใ‚’ๆๆกˆใ—ใพใ—ใŸใ€‚ใ“ใฎ DETR ใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒขใ‚ธใƒฅใƒผใƒซใฏใ€ๅ‚็…งๅ‘จๅ›ฒใฎๅฐ‘ๆ•ฐใฎไธป่ฆใชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒใ‚คใƒณใƒˆใฎใฟใซๆณจ็›ฎใ—ใพใ™ใ€‚ๅค‰ๅฝขๅฏ่ƒฝใช DETR ใฏใ€10 ๅˆ†ใฎ 1 ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚จใƒใƒƒใ‚ฏใงใ€DETR ใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น (็‰นใซๅฐใ•ใชใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅ ดๅˆ) ใ‚’้”ๆˆใงใใพใ™ใ€‚ COCO ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซ้–ขใ™ใ‚‹ๅบƒ็ฏ„ใชๅฎŸ้จ“ใซใ‚ˆใ‚Šใ€็งใŸใกใฎใ‚ขใƒ—ใƒญใƒผใƒใฎๆœ‰ๅŠนๆ€งใŒๅฎŸ่จผใ•ใ‚Œใพใ—ใŸใ€‚* <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/deformable_detr_architecture.png" alt="ๆ็”ป" width="600"/> <small> ๅค‰ๅฝขๅฏ่ƒฝใช DETR ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ <a href="https://arxiv.org/abs/2010.04159">ๅ…ƒใฎ่ซ–ๆ–‡</a>ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚</small> ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/fundamentalvision/Deformable-DETR) ใซใ‚ใ‚Šใพใ™ใ€‚ ## Usage tips - ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ Deformable DETR ใฏใ€ๅ…ƒใฎ [DETR](detr) ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใจๅŒ็ญ‰ใงใ™ใ€‚ใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใซใคใ„ใฆใฏใ€ไปฅไธ‹ใฎ [resources](#resources) ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ## Resources Deformable DETR ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ <PipelineTag pipeline="object-detection"/> - [`DeformableDetrForObjectDetection`] ใฎใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฎๆŽจ่ซ–ใจๅพฎ่ชฟๆ•ดใซ้–ขใ™ใ‚‹ใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏใ€[ใ“ใกใ‚‰](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/Deformable-DETR) ใซใ‚ใ‚Šใพใ™ใ€‚ - [็‰ฉไฝ“ๆคœๅ‡บใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/object_detection) ใ‚‚ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ ## DeformableDetrImageProcessor [[autodoc]] DeformableDetrImageProcessor - preprocess - post_process_object_detection ## DeformableDetrFeatureExtractor [[autodoc]] DeformableDetrFeatureExtractor - __call__ - post_process_object_detection ## DeformableDetrConfig [[autodoc]] DeformableDetrConfig ## DeformableDetrModel [[autodoc]] DeformableDetrModel - forward ## DeformableDetrForObjectDetection [[autodoc]] DeformableDetrForObjectDetection - forward
transformers/docs/source/ja/model_doc/deformable_detr.md/0
{ "file_path": "transformers/docs/source/ja/model_doc/deformable_detr.md", "repo_id": "transformers", "token_count": 1792 }
294
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Efficient Inference on a Single GPU ใ“ใฎใ‚ฌใ‚คใƒ‰ใซๅŠ ใˆใฆใ€[1ใคใฎGPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ฌใ‚คใƒ‰](perf_train_gpu_one)ใจ[CPUใงใฎๆŽจ่ซ–ใ‚ฌใ‚คใƒ‰](perf_infer_cpu)ใซ้–ข้€ฃใ™ใ‚‹ๆƒ…ๅ ฑใŒใ‚ใ‚Šใพใ™ใ€‚ ## Flash Attention 2 <Tip> ใ“ใฎๆฉŸ่ƒฝใฏๅฎŸ้จ“็š„ใงใ‚ใ‚Šใ€ๅฐ†ๆฅใฎใƒใƒผใ‚ธใƒงใƒณใงๅคงๅน…ใซๅค‰ๆ›ดใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€Flash Attention 2 APIใฏ่ฟ‘ใ„ๅฐ†ๆฅ`BetterTransformer` APIใซ็งป่กŒใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ </Tip> Flash Attention 2ใฏใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๆŽจ่ซ–้€Ÿๅบฆใ‚’ๅคงๅน…ใซ้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚Flash Attention 2ใฏใ€Tri Daoๆฐใซใ‚ˆใฃใฆ[ๅ…ฌๅผใฎFlash Attentionใƒชใƒใ‚ธใƒˆใƒช](https://github.com/Dao-AILab/flash-attention)ใงๅฐŽๅ…ฅใ•ใ‚Œใพใ—ใŸใ€‚Flash Attentionใซ้–ขใ™ใ‚‹็ง‘ๅญฆ่ซ–ๆ–‡ใฏ[ใ“ใกใ‚‰](https://arxiv.org/abs/2205.14135)ใง่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ Flash Attention 2ใ‚’ๆญฃใ—ใใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใซใฏใ€ไธŠ่จ˜ใฎใƒชใƒใ‚ธใƒˆใƒชใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใ‚‹ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ‚ฌใ‚คใƒ‰ใซๅพ“ใฃใฆใใ ใ•ใ„ใ€‚ ไปฅไธ‹ใฎใƒขใƒ‡ใƒซใซๅฏพใ—ใฆFlash Attention 2ใ‚’ใƒใ‚คใƒ†ใ‚ฃใƒ–ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™๏ผš - Llama - Falcon ใ•ใ‚‰ใซๅคšใใฎใƒขใƒ‡ใƒซใซFlash Attention 2ใฎใ‚ตใƒใƒผใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’GitHubใงๆๆกˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใ€ๅค‰ๆ›ดใ‚’็ตฑๅˆใ™ใ‚‹ใŸใ‚ใซใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅซใ‚€ใ€ๆŽจ่ซ–ใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไฝฟ็”จใงใใพใ™๏ผˆ็พๅœจใฎ`BetterTransformer` APIใงใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„๏ผ‰ใ€‚ <Tip> Flash Attention 2ใฏใ€ใƒขใƒ‡ใƒซใฎdtypeใŒ`fp16`ใพใŸใฏ`bf16`ใฎๅ ดๅˆใซใฎใฟไฝฟ็”จใงใใ€NVIDIA-GPUใƒ‡ใƒใ‚คใ‚นใงใฎใฟๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒขใƒ‡ใƒซใ‚’้ฉๅˆ‡ใชdtypeใซใ‚ญใƒฃใ‚นใƒˆใ—ใ€ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒ‡ใƒใ‚คใ‚นใซใƒญใƒผใƒ‰ใ—ใฆใใ ใ•ใ„ใ€‚ </Tip> ### Quick usage ใƒขใƒ‡ใƒซใงFlash Attention 2ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€`from_pretrained`ใฎๅผ•ๆ•ฐใซ`attn_implementation="flash_attention_2"`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM model_id = "tiiuae/falcon-7b" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained( model_id, torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2", ) ``` ใ“ใกใ‚‰ใฏใ€็”ŸๆˆใพใŸใฏๅพฎ่ชฟๆ•ดใฎใŸใ‚ใซไฝฟ็”จใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใงใ™ใ€‚ ### Expected speedups ็‰นใซ้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฏพใ—ใฆใ€ๅพฎ่ชฟๆ•ดใจๆŽจ่ซ–ใฎ้š›ใซใฏใ€ใ‹ใชใ‚Šใฎ้ซ˜้€ŸๅŒ–ใŒๆœŸๅพ…ใงใใพใ™ใ€‚ใŸใ ใ—ใ€Flash Attentionใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚นใ‚ณใ‚ขใ‚’่จˆ็ฎ—ใ—ใชใ„ใŸใ‚ใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใŒๅซใพใ‚Œใ‚‹ๅ ดๅˆใ€ใƒใƒƒใƒๆŽจ่ซ–ใซใŠใ„ใฆใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚นใ‚ณใ‚ขใ‚’ๆ‰‹ๅ‹•ใงใƒ‘ใƒƒใƒ‰/ใ‚ขใƒณใƒ‘ใƒƒใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ๅซใ‚€ใƒใƒƒใƒ็”Ÿๆˆใฎๅคงๅน…ใช้…ๅปถใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ ใ“ใ‚Œใ‚’ๅ…‹ๆœใ™ใ‚‹ใŸใ‚ใซใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ›ใšใซFlash Attentionใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผˆใŸใจใˆใฐใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ‘ใƒƒใ‚ฏใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆœ€ๅคงใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใซ้”ใ™ใ‚‹ใพใง้€ฃ็ตใ™ใ‚‹ใ“ใจใชใฉ๏ผ‰ใ€‚ใ“ใ“ใซ[ไพ‹](https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_clm.py#L516)ใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ ไปฅไธ‹ใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใฎใชใ„ๅ ดๅˆใซใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใŒ4096ใฎ[tiiuae/falcon-7b](https://hf.co/tiiuae/falcon-7b)ใซๅฏพใ™ใ‚‹ๅ˜็ด”ใชใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฎไบˆๆƒณใ•ใ‚Œใ‚‹้ซ˜้€ŸๅŒ–ใงใ™ใ€‚ใ•ใพใ–ใพใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒ็คบใ•ใ‚Œใฆใ„ใพใ™๏ผš <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/falcon-7b-inference-large-seqlen.png"> </div> ไปฅไธ‹ใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใฎใชใ„ๅ ดๅˆใซใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใŒ4096ใฎ[`meta-llama/Llama-7b-hf`](https://hf.co/meta-llama/Llama-7b-hf)ใซๅฏพใ™ใ‚‹ๅ˜็ด”ใชใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฎไบˆๆƒณใ•ใ‚Œใ‚‹้ซ˜้€ŸๅŒ–ใงใ™ใ€‚ใ•ใพใ–ใพใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒ็คบใ•ใ‚Œใฆใ„ใพใ™๏ผš <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/llama-7b-inference-large-seqlen.png"> </div> ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ๅซใ‚€ใ‚ทใƒผใ‚ฑใƒณใ‚น๏ผˆใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใพใŸใฏ็”Ÿๆˆใ™ใ‚‹๏ผ‰ใฎๅ ดๅˆใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚นใ‚ณใ‚ขใ‚’ๆญฃใ—ใ่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใซๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใ‚ขใƒณใƒ‘ใƒƒใƒ‰/ใƒ‘ใƒƒใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆฏ”่ผƒ็š„ๅฐใ•ใ„ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใฎๅ ดๅˆใ€็ด”็ฒ‹ใชใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใงใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใŒ30%ๆœชๆบ€ใ—ใ‹ๅŸ‹ใ‚ใ‚‰ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ใ“ใ‚Œใฏใ‚ใšใ‹ใช้ซ˜้€ŸๅŒ–ใ‚’ใ‚‚ใŸใ‚‰ใ—ใพใ™ใ€‚ <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/llama-2-small-seqlen-padding.png"> </div> ใ—ใ‹ใ—ใ€ๅคงใใชใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใฎๅ ดๅˆใ€็ด”็ฒ‹ใชๆŽจ่ซ–๏ผˆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚‚ๅซใ‚€๏ผ‰ใซใฏ่ˆˆๅ‘ณๆทฑใ„้ซ˜้€ŸๅŒ–ใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ Flash Attentionใฏใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ่จˆ็ฎ—ใ‚’ใ‚ˆใ‚ŠใƒกใƒขใƒชๅŠน็އใฎ่‰ฏใ„ใ‚‚ใฎใซใ—ใ€ๅคงใใชใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใงใฎCUDA OOMใฎๅ•้กŒใ‚’ๅ›ž้ฟใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ๅคงใใชใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใซๅฏพใ—ใฆๆœ€ๅคง20ใฎใƒกใƒขใƒชๅ‰Šๆธ›ใ‚’ใ‚‚ใŸใ‚‰ใ™ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ๅ…ฌๅผใฎFlash Attentionใƒชใƒใ‚ธใƒˆใƒช](https://github.com/Dao-AILab/flash-attention)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ <div style="text-align: center"> <img src="https://huggingface.co/datasets/ybelkada/documentation-images/resolve/main/llama-2-large-seqlen-padding.png"> </div> ### Advanced usage ใ“ใฎๆฉŸ่ƒฝใ‚’ใƒขใƒ‡ใƒซใฎๆœ€้ฉๅŒ–ใซๅคšใใฎๆ—ขๅญ˜ใฎๆฉŸ่ƒฝใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไปฅไธ‹ใซใ„ใใคใ‹ใฎไพ‹ใ‚’็คบใ—ใพใ™๏ผš ### Combining Flash Attention 2 and 8-bit models ใ“ใฎๆฉŸ่ƒฝใ‚’8ใƒ“ใƒƒใƒˆใฎ้‡ๅญๅŒ–ใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM model_id = "tiiuae/falcon-7b" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained( model_id, load_in_8bit=True, attn_implementation="flash_attention_2", ) ``` ### Combining Flash Attention 2 and 4-bit models ใ“ใฎๆฉŸ่ƒฝใ‚’ 4 ใƒ“ใƒƒใƒˆใฎ้‡ๅญๅŒ–ใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM model_id = "tiiuae/falcon-7b" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained( model_id, load_in_4bit=True, attn_implementation="flash_attention_2", ) ``` ### Combining Flash Attention 2 and PEFT ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ—ใฆใ€Flash Attention 2ใ‚’ใƒ™ใƒผใ‚นใซใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹้š›ใซPEFTใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM from peft import LoraConfig model_id = "tiiuae/falcon-7b" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained( model_id, load_in_4bit=True, attn_implementation="flash_attention_2", ) lora_config = LoraConfig( r=8, task_type="CAUSAL_LM" ) model.add_adapter(lora_config) ... # train your model ``` ## BetterTransformer [BetterTransformer](https://huggingface.co/docs/optimum/bettertransformer/overview)ใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎ้ซ˜้€Ÿใƒ‘ใ‚นๅฎŸ่กŒใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€Flash Attentionใชใฉใฎๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸใ‚ซใƒผใƒใƒซใŒๅ†…้ƒจใงๅ‘ผใณๅ‡บใ•ใ‚Œใพใ™ใ€‚ BetterTransformerใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใ€็”ปๅƒใ€ใŠใ‚ˆใณใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒขใƒ‡ใƒซใฎๅ˜ไธ€ใŠใ‚ˆใณใƒžใƒซใƒGPUใงใฎ้ซ˜้€ŸใชๆŽจ่ซ–ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ <Tip> Flash Attentionใฏใ€fp16ใพใŸใฏbf16ใฎdtypeใ‚’ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใซใฎใฟไฝฟ็”จใงใใพใ™ใ€‚BetterTransformerใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒขใƒ‡ใƒซใ‚’้ฉๅˆ‡ใชdtypeใซใ‚ญใƒฃใ‚นใƒˆใ—ใฆใใ ใ•ใ„ใ€‚ </Tip> ### Encoder models PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎ[`nn.MultiHeadAttention`](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/)ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ้ซ˜้€Ÿใƒ‘ใ‚นใ€BetterTransformerใจๅ‘ผใฐใ‚Œใ‚‹ใ‚‚ใฎใฏใ€[๐Ÿค— Optimumใƒฉใ‚คใƒ–ใƒฉใƒช](https://huggingface.co/docs/optimum/bettertransformer/overview)ใฎ็ตฑๅˆใ‚’้€šใ˜ใฆTransformersใจไธ€็ท’ใซไฝฟ็”จใงใใพใ™ใ€‚ PyTorchใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ้ซ˜้€Ÿใƒ‘ใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ‚ซใƒผใƒใƒซใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใจ[ใƒใ‚นใƒˆใ•ใ‚ŒใŸใƒ†ใƒณใ‚ฝใƒซ](https://pytorch.org/docs/stable/nested.html)ใฎไฝฟ็”จใซใ‚ˆใ‚Šใ€ๆŽจ่ซ–ใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚่ฉณ็ดฐใชใƒ™ใƒณใƒใƒžใƒผใ‚ฏๆƒ…ๅ ฑใฏ[ใ“ใฎใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)ใซใ‚ใ‚Šใพใ™ใ€‚ [`optimum`](https://github.com/huggingface/optimum)ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใŸๅพŒใ€ๆŽจ่ซ–ไธญใซBetter Transformerใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€้–ข้€ฃใ™ใ‚‹ๅ†…้ƒจใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใง็ฝฎใๆ›ใˆใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™[`~PreTrainedModel.to_bettertransformer`]: ```python model = model.to_bettertransformer() ``` ใƒกใ‚ฝใƒƒใƒ‰ [`~PreTrainedModel.reverse_bettertransformer`] ใฏใ€ใƒขใƒ‡ใƒซใ‚’ไฟๅญ˜ใ™ใ‚‹ๅ‰ใซไฝฟ็”จใ™ในใใงใ€ๆจ™ๆบ–ใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™๏ผš ```python model = model.reverse_bettertransformer() model.save_pretrained("saved_model") ``` BetterTransformer APIใ‚’ไฝฟใฃใŸใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใฎๅฏ่ƒฝๆ€งใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚‹ใซใฏใ€[ใ“ใฎใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆ](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ ### Decoder models ใƒ†ใ‚ญใ‚นใƒˆใƒขใƒ‡ใƒซใ€็‰นใซใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซ๏ผˆGPTใ€T5ใ€Llamaใชใฉ๏ผ‰ใซใจใฃใฆใ€BetterTransformer APIใฏใ™ในใฆใฎๆณจๆ„ๆ“ไฝœใ‚’[`torch.nn.functional.scaled_dot_product_attention`ใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผ](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention)๏ผˆSDPA๏ผ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ“ใฎใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผใฏPyTorch 2.0ไปฅ้™ใงใฎใฟๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ ใƒขใƒ‡ใƒซใ‚’BetterTransformerใซๅค‰ๆ›ใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš ```python from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m") # convert the model to BetterTransformer model.to_bettertransformer() # Use it for training or inference ``` SDPAใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚„ๅ•้กŒใฎใ‚ตใ‚คใ‚บใซๅฟœใ˜ใฆ[Flash Attention](https://arxiv.org/abs/2205.14135)ใ‚ซใƒผใƒใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚Flash Attentionใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ‹ใ€็‰นๅฎšใฎ่จญๅฎš๏ผˆใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ€ๅ•้กŒใ‚ตใ‚คใ‚บ๏ผ‰ใงไฝฟ็”จๅฏ่ƒฝใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[`torch.backends.cuda.sdp_kernel`](https://pytorch.org/docs/master/backends.html#torch.backends.cuda.sdp_kernel)ใ‚’ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒžใƒใƒผใ‚ธใƒฃใจใ—ใฆไฝฟ็”จใ—ใพใ™ใ€‚ ```diff import torch from transformers import AutoModelForCausalLM, AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", torch_dtype=torch.float16).to("cuda") # convert the model to BetterTransformer model.to_bettertransformer() input_text = "Hello my dog is cute and" inputs = tokenizer(input_text, return_tensors="pt").to("cuda") + with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False): outputs = model.generate(**inputs) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` ใ‚‚ใ—ใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใซใƒใ‚ฐใŒ่กจ็คบใ•ใ‚ŒใŸๅ ดๅˆ ```bash RuntimeError: No available kernel. Aborting execution. ``` Flash Attention ใฎๅบƒ็ฏ„ใชใ‚ซใƒใƒฌใƒƒใ‚ธใ‚’ๆŒใคใ‹ใ‚‚ใ—ใ‚Œใชใ„ PyTorch ใฎใƒŠใ‚คใƒˆใƒชใƒผใƒใƒผใ‚ธใƒงใƒณใ‚’่ฉฆใ—ใฆใฟใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ ```bash pip3 install -U --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu118 ``` Or make sure your model is correctly casted in float16 or bfloat16 ใƒขใƒ‡ใƒซใŒๆญฃใ—ใfloat16ใพใŸใฏbfloat16ใซใ‚ญใƒฃใ‚นใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ Have a look at [this detailed blogpost](https://pytorch.org/blog/out-of-the-box-acceleration/) to read more about what is possible to do with `BetterTransformer` + SDPA API. `BetterTransformer` + SDPA APIใ‚’ไฝฟ็”จใ—ใฆไฝ•ใŒๅฏ่ƒฝใ‹ใซใคใ„ใฆ่ฉณใ—ใ่ชญใ‚€ใซใฏใ€[ใ“ใฎ่ฉณ็ดฐใชใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆ](https://pytorch.org/blog/out-of-the-box-acceleration/)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ ## `bitsandbytes` integration for FP4 mixed-precision inference FP4ๆททๅˆ็ฒพๅบฆๆŽจ่ซ–ใฎใŸใ‚ใฎ`bitsandbytes`็ตฑๅˆ You can install `bitsandbytes` and benefit from easy model compression on GPUs. Using FP4 quantization you can expect to reduce up to 8x the model size compared to its native full precision version. Check out below how to get started. `bitsandbytes`ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใ€GPUใง็ฐกๅ˜ใชใƒขใƒ‡ใƒซใฎๅœง็ธฎใ‚’ๅˆฉ็”จใงใใพใ™ใ€‚FP4้‡ๅญๅŒ–ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎใƒ•ใƒซใƒ—ใƒฌใ‚ทใ‚ธใƒงใƒณใƒใƒผใ‚ธใƒงใƒณใจๆฏ”่ผƒใ—ใฆใƒขใƒ‡ใƒซใ‚ตใ‚คใ‚บใ‚’ๆœ€ๅคง8ๅ€ๅ‰Šๆธ›ใงใใ‚‹ใ“ใจใŒๆœŸๅพ…ใงใใพใ™ใ€‚ไปฅไธ‹ใ‚’็ขบ่ชใ—ใฆใ€ใฉใฎใ‚ˆใ†ใซๅง‹ใ‚ใ‚‹ใ‹ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ <Tip> Note that this feature can also be used in a multi GPU setup. ใ“ใฎๆฉŸ่ƒฝใฏใ€ใƒžใƒซใƒGPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ </Tip> ### Requirements [[requirements-for-fp4-mixedprecision-inference]] - Latest `bitsandbytes` library `pip install bitsandbytes>=0.39.0` - Install latest `accelerate` from source `pip install git+https://github.com/huggingface/accelerate.git` - Install latest `transformers` from source `pip install git+https://github.com/huggingface/transformers.git` ### Running FP4 models - single GPU setup - Quickstart ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใงใ€็ฐกๅ˜ใซๅ˜ไธ€ใฎGPUใงFP4ใƒขใƒ‡ใƒซใ‚’ๅฎŸ่กŒใงใใพใ™: ```py from transformers import AutoModelForCausalLM model_name = "bigscience/bloom-2b5" model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True) ``` ๆณจๆ„: `device_map`ใฏใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใŒใ€ๆŽจ่ซ–ๆ™‚ใซ `device_map = 'auto'` ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅˆฉ็”จๅฏ่ƒฝใชใƒชใ‚ฝใƒผใ‚นใซๅŠน็އ็š„ใซใƒขใƒ‡ใƒซใŒใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ•ใ‚Œใพใ™ใ€‚ ### Running FP4 models - multi GPU setup ๆททๅˆ4ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใ‚’่ค‡ๆ•ฐใฎGPUใซใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใฏใ€ๅ˜ไธ€GPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใจๅŒใ˜ใงใ™๏ผˆๅ˜ไธ€GPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใจๅŒใ˜ใ‚ณใƒžใƒณใƒ‰ใงใ™๏ผ‰๏ผš ```py model_name = "bigscience/bloom-2b5" model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True) ``` ใ—ใ‹ใ—ใ€`accelerate`ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ„GPUใซๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹GPU RAMใ‚’ๅˆถๅพกใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซใ€`max_memory`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™๏ผš ```py max_memory_mapping = {0: "600MB", 1: "1GB"} model_name = "bigscience/bloom-3b" model_4bit = AutoModelForCausalLM.from_pretrained( model_name, device_map="auto", load_in_4bit=True, max_memory=max_memory_mapping ) ``` ใ“ใฎไพ‹ใงใฏใ€ๆœ€ๅˆใฎGPUใฏ600MBใฎใƒกใƒขใƒชใ‚’ไฝฟ็”จใ—ใ€2็•ช็›ฎใฎGPUใฏ1GBใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ ### Advanced usage ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฎใ•ใ‚‰ใชใ‚‹้ซ˜ๅบฆใชไฝฟ็”จๆณ•ใซใคใ„ใฆใฏใ€[้‡ๅญๅŒ–](main_classes/quantization)ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใƒšใƒผใ‚ธใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ ## `bitsandbytes` integration for Int8 mixed-precision matrix decomposition <Tip> ใ“ใฎๆฉŸ่ƒฝใฏใ€ใƒžใƒซใƒGPU็’ฐๅขƒใงใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ </Tip> ่ซ–ๆ–‡[`LLM.int8()๏ผšใ‚นใ‚ฑใƒผใƒฉใƒ–ใƒซใชTransformerๅ‘ใ‘ใฎ8ใƒ“ใƒƒใƒˆ่กŒๅˆ—ไน—็ฎ—`](https://arxiv.org/abs/2208.07339)ใซใ‚ˆใ‚Œใฐใ€Hugging Face็ตฑๅˆใŒHubๅ†…ใฎใ™ในใฆใฎใƒขใƒ‡ใƒซใงใ‚ใšใ‹ๆ•ฐ่กŒใฎใ‚ณใƒผใƒ‰ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ๅŠ็ฒพๅบฆ๏ผˆ`float16`ใŠใ‚ˆใณ`bfloat16`๏ผ‰ใฎ้‡ใฟใฎๅ ดๅˆใซ`nn.Linear`ใ‚ตใ‚คใ‚บใ‚’2ๅ€ใ€ๅ˜็ฒพๅบฆ๏ผˆ`float32`๏ผ‰ใฎ้‡ใฟใฎๅ ดๅˆใฏ4ๅ€ใซ็ธฎๅฐใ—ใ€ๅค–ใ‚Œๅ€คใซๅฏพใ—ใฆใปใจใ‚“ใฉๅฝฑ้Ÿฟใ‚’ไธŽใˆใพใ›ใ‚“ใ€‚ ![HFxbitsandbytes.png](https://cdn-uploads.huggingface.co/production/uploads/1659861207959-62441d1d9fdefb55a0b7d12c.png) Int8ๆททๅˆ็ฒพๅบฆ่กŒๅˆ—ๅˆ†่งฃใฏใ€่กŒๅˆ—ไน—็ฎ—ใ‚’2ใคใฎใ‚นใƒˆใƒชใƒผใƒ ใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆๅ‹•ไฝœใ—ใพใ™๏ผš(1) ใ‚ทใ‚นใƒ†ใƒžใƒ†ใ‚ฃใƒƒใ‚ฏใช็‰นๅพดๅค–ใ‚Œๅ€คใ‚นใƒˆใƒชใƒผใƒ ใŒfp16ใง่กŒๅˆ—ไน—็ฎ—๏ผˆ0.01%๏ผ‰ใ€(2) int8่กŒๅˆ—ไน—็ฎ—ใฎ้€šๅธธใฎใ‚นใƒˆใƒชใƒผใƒ ๏ผˆ99.9%๏ผ‰ใ€‚ใ“ใฎๆ–นๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€้žๅธธใซๅคงใใชใƒขใƒ‡ใƒซใซๅฏพใ—ใฆไบˆๆธฌใฎๅŠฃๅŒ–ใชใ—ใซint8ๆŽจ่ซ–ใŒๅฏ่ƒฝใงใ™ใ€‚ ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[่ซ–ๆ–‡](https://arxiv.org/abs/2208.07339)ใพใŸใฏ[ใ“ใฎ็ตฑๅˆใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://huggingface.co/blog/hf-bitsandbytes-integration)ใ‚’ใ”็ขบ่ชใใ ใ•ใ„ใ€‚ ![MixedInt8.gif](https://cdn-uploads.huggingface.co/production/uploads/1660567469965-62441d1d9fdefb55a0b7d12c.gif) ใชใŠใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏGPUใŒๅฟ…่ฆใงใ‚ใ‚Šใ€ใ‚ซใƒผใƒใƒซใฏGPUๅฐ‚็”จใซใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒขใƒ‡ใƒซใฎ1/4๏ผˆใพใŸใฏใƒใƒผใƒ•็ฒพๅบฆใฎ้‡ใฟใฎๅ ดๅˆใฏ1/2๏ผ‰ใ‚’ไฟๅญ˜ใ™ใ‚‹ใฎใซๅๅˆ†ใชGPUใƒกใƒขใƒชใŒใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ ใ“ใฎใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ™ใ‚‹้š›ใฎใƒ˜ใƒซใƒ—ใซ้–ขใ™ใ‚‹่ฉณ็ดฐใฏใ€ไปฅไธ‹ใฎใƒŽใƒผใƒˆใ‚’ใ”่ฆงใ„ใŸใ ใใ‹ใ€[Google Colabใฎใƒ‡ใƒข](#colab-demos)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ ### Requirements [[requirements-for-int8-mixedprecision-matrix-decomposition]] - `bitsandbytes<0.37.0`ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€NVIDIA GPUใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€8ใƒ“ใƒƒใƒˆใƒ†ใƒณใ‚ฝใƒซใ‚ณใ‚ขใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผˆTuringใ€Ampereใ€ใพใŸใฏใใ‚Œไปฅ้™ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใƒผใ€ไพ‹๏ผšT4ใ€RTX20s RTX30sใ€A40-A100ใชใฉ๏ผ‰ใ€‚`bitsandbytes>=0.37.0`ใฎๅ ดๅˆใ€ใ™ในใฆใฎGPUใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใ‚‹ใฏใšใงใ™ใ€‚ - ๆญฃใ—ใ„ใƒใƒผใ‚ธใƒงใƒณใฎ`bitsandbytes`ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš `pip install bitsandbytes>=0.31.5` - `accelerate`ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™๏ผš `pip install accelerate>=0.12.0` ### Running mixed-Int8 models - single GPU setup ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใŸๅพŒใ€ใƒŸใƒƒใ‚ฏใ‚น 8 ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€ๆ–นๆณ•ใฏๆฌกใฎ้€šใ‚Šใงใ™๏ผš ```py from transformers import AutoModelForCausalLM, BitsAndBytesConfig model_name = "bigscience/bloom-2b5" model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) ``` ไปฅไธ‹ใฏใ‚ทใƒณใƒ—ใƒซใชไพ‹ใงใ™๏ผš * `pipeline()` ้–ขๆ•ฐใฎไปฃใ‚ใ‚Šใซใ€ใƒขใƒ‡ใƒซใฎ `generate()` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚`pipeline()` ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ™ใ‚‹ใ“ใจใฏๅฏ่ƒฝใงใ™ใŒใ€ๆททๅˆ8ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใซๆœ€้ฉๅŒ–ใ•ใ‚ŒใฆใŠใ‚‰ใšใ€`generate()` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ‚Šใ‚‚้…ใใชใ‚Šใพใ™ใ€‚ใพใŸใ€ไธ€้ƒจใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐๆˆฆ็•ฅ๏ผˆไพ‹๏ผšใƒŒใ‚ฏใƒฌใ‚ฆใ‚นใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ๏ผ‰ใฏใ€`pipeline()` ้–ขๆ•ฐใงใฏๆททๅˆ8ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใงใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ * ใ™ในใฆใฎๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใจๅŒใ˜ใƒ‡ใƒใ‚คใ‚นใซ้…็ฝฎใ—ใฆใใ ใ•ใ„ใ€‚ ```py from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig model_name = "bigscience/bloom-2b5" tokenizer = AutoTokenizer.from_pretrained(model_name) model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) prompt = "Hello, my llama is cute" inputs = tokenizer(prompt, return_tensors="pt").to("cuda") generated_ids = model.generate(**inputs) outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) ``` ### Running mixed-int8 models - multi GPU setup ่ค‡ๆ•ฐใฎGPUใซๆททๅˆ8ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใฏใ€ๆฌกใฎ้€šใ‚Šใงใ™๏ผˆใ‚ทใƒณใ‚ฐใƒซGPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใจๅŒใ˜ใ‚ณใƒžใƒณใƒ‰ใงใ™๏ผ‰๏ผš ```py model_name = "bigscience/bloom-2b5" model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) ``` `accelerate`ใ‚’ไฝฟ็”จใ—ใฆๅ„GPUใซๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹GPU RAMใ‚’ๅˆถๅพกใ™ใ‚‹้š›ใซใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซ`max_memory`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™๏ผš ```py max_memory_mapping = {0: "1GB", 1: "2GB"} model_name = "bigscience/bloom-3b" model_8bit = AutoModelForCausalLM.from_pretrained( model_name, device_map="auto", load_in_8bit=True, max_memory=max_memory_mapping ) ``` In this example, the first GPU will use 1GB of memory and the second 2GB. ### Colab demos ใ“ใฎๆ–นๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ไปฅๅ‰ใฎGoogle ColabใงใฏๆŽจ่ซ–ใงใใชใ‹ใฃใŸใƒขใƒ‡ใƒซใซๅฏพใ—ใฆๆŽจ่ซ–ใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ไปฅไธ‹ใฏใ€Google Colabใง8ใƒ“ใƒƒใƒˆ้‡ๅญๅŒ–ใ‚’ไฝฟ็”จใ—ใฆT5-11b๏ผˆfp32ใง42GB๏ผ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใƒ‡ใƒขใฎใƒชใƒณใ‚ฏใงใ™๏ผš [![Open In Colab: T5-11b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1YORPWx4okIHXnjW7MSAidXN29mPVNT7F?usp=sharing) ใพใŸใ€BLOOM-3Bใฎใƒ‡ใƒขใ‚‚ใ”่ฆงใ„ใŸใ ใ‘ใพใ™๏ผš [![Open In Colab: BLOOM-3b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qOjXfQIAULfKvZqwCen8-MoWKGdSatZ4?usp=sharing) ## Advanced usage: mixing FP4 (or Int8) and BetterTransformer ็•ฐใชใ‚‹ๆ–นๆณ•ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ€ใƒขใƒ‡ใƒซใฎๆœ€้ฉใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€BetterTransformerใ‚’ไฝฟ็”จใ—ใฆFP4ใƒŸใƒƒใ‚ฏใ‚นใƒ—ใƒฌใ‚ทใ‚ธใƒงใƒณๆŽจ่ซ–ใจใƒ•ใƒฉใƒƒใ‚ทใƒฅใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ```py import torch from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig quantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16 ) tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", quantization_config=quantization_config) input_text = "Hello my dog is cute and" inputs = tokenizer(input_text, return_tensors="pt").to("cuda") with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False): outputs = model.generate(**inputs) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ```
transformers/docs/source/ja/perf_infer_gpu_one.md/0
{ "file_path": "transformers/docs/source/ja/perf_infer_gpu_one.md", "repo_id": "transformers", "token_count": 9356 }
295
<!-- Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ ใ“ใฎใƒ•ใ‚กใ‚คใƒซใฏMarkdownๅฝขๅผใงใ™ใŒใ€็‰นๅฎšใฎMDXใซ้กžไผผใ—ใŸใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใƒ“ใƒซใƒ€ใƒผใฎๆง‹ๆ–‡ใ‚’ๅซใ‚“ใงใŠใ‚Šใ€ Markdownใƒ“ใƒฅใƒผใ‚ขใƒผใงๆญฃใ—ใ่กจ็คบใ•ใ‚Œใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ --> # Preprocess [[open-in-colab]] ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ‰ใซใ€ใใ‚Œใ‚’ใƒขใƒ‡ใƒซใฎๆœŸๅพ…ใ™ใ‚‹ๅ…ฅๅŠ›ๅฝขๅผใซๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใƒ‡ใƒผใ‚ฟใŒใƒ†ใ‚ญใ‚นใƒˆใ€็”ปๅƒใ€ใพใŸใฏใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใซใ‹ใ‹ใ‚ใ‚‰ใšใ€ใใ‚Œใ‚‰ใฏใƒ†ใƒณใ‚ฝใƒซใฎใƒใƒƒใƒใซๅค‰ๆ›ใ—ใฆ็ต„ใฟ็ซ‹ใฆใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ๐Ÿค— Transformersใฏใ€ใƒ‡ใƒผใ‚ฟใ‚’ใƒขใƒ‡ใƒซ็”จใซๆบ–ๅ‚™ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใฎใ‚ปใƒƒใƒˆใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ๆฌกใฎใ“ใจใ‚’ๅญฆใณใพใ™๏ผš * ใƒ†ใ‚ญใ‚นใƒˆใฎๅ ดๅˆใ€[Tokenizer](./main_classes/tokenizer)ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅค‰ๆ›ใ—ใ€ใƒˆใƒผใ‚ฏใƒณใฎๆ•ฐๅ€ค่กจ็พใ‚’ไฝœๆˆใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒ†ใƒณใ‚ฝใƒซใซ็ต„ใฟ็ซ‹ใฆใ‚‹ๆ–นๆณ•ใ€‚ * ้Ÿณๅฃฐใจใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใฎๅ ดๅˆใ€[Feature extractor](./main_classes/feature_extractor)ใ‚’ไฝฟ็”จใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๆณขๅฝขใ‹ใ‚‰้€ฃ็ถš็š„ใช็‰นๅพดใ‚’ๆŠฝๅ‡บใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ๆ–นๆณ•ใ€‚ * ็”ปๅƒๅ…ฅๅŠ›ใฎๅ ดๅˆใ€[ImageProcessor](./main_classes/image)ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ๆ–นๆณ•ใ€‚ * ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซๅ…ฅๅŠ›ใฎๅ ดๅˆใ€[Processor](./main_classes/processors)ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจ็‰นๅพดๆŠฝๅ‡บๅ™จใพใŸใฏ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ๆ–นๆณ•ใ€‚ <Tip> `AutoProcessor`ใฏๅธธใซๅ‹•ไฝœใ—ใ€ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใซ้ฉๅˆ‡ใชใ‚ฏใƒฉใ‚นใ‚’่‡ชๅ‹•็š„ใซ้ธๆŠžใ—ใพใ™ใ€‚ ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€็‰นๅพดๆŠฝๅ‡บๅ™จใ€ใพใŸใฏใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‹ใซใ‹ใ‹ใ‚ใ‚‰ใšใ€ๅ‹•ไฝœใ—ใพใ™ใ€‚ </Tip> ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€๐Ÿค— Datasetsใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใ€ใ„ใใคใ‹ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ฉฆใ™ใ“ใจใŒใงใใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„๏ผš ```bash pip install datasets ``` ## Natural Language Processing <Youtube id="Yffk5aydLzg"/> ใƒ†ใ‚ญใ‚นใƒˆใƒ‡ใƒผใ‚ฟใฎๅ‰ๅ‡ฆ็†ใซไฝฟ็”จใ™ใ‚‹ไธป่ฆใชใƒ„ใƒผใƒซใฏใ€[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถ](main_classes/tokenizer)ใงใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ไธ€้€ฃใฎใƒซใƒผใƒซใซๅพ“ใฃใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’*ใƒˆใƒผใ‚ฏใƒณ*ใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณใฏๆ•ฐๅ€คใซๅค‰ๆ›ใ•ใ‚Œใ€ใใฎๅพŒใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใจใชใ‚Šใพใ™ใ€‚ใƒขใƒ‡ใƒซใŒๅฟ…่ฆใจใ™ใ‚‹่ฟฝๅŠ ใฎๅ…ฅๅŠ›ใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซใ‚ˆใฃใฆ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ <Tip> ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ไบˆๅฎšใฎๅ ดๅˆใ€้–ข้€ฃใ™ใ‚‹ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆใŒไบ‹ๅ‰ๅญฆ็ฟ’ใ‚ณใƒผใƒ‘ใ‚นใจๅŒใ˜ๆ–นๆณ•ใงๅˆ†ๅ‰ฒใ•ใ‚Œใ€ไบ‹ๅ‰ๅญฆ็ฟ’ไธญใซ้€šๅธธ*ใƒœใ‚ญใƒฃใƒ–*ใจใ—ใฆๅ‚็…งใ•ใ‚Œใ‚‹ๅฏพๅฟœใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ </Tip> [`AutoTokenizer.from_pretrained`]ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€้–‹ๅง‹ใ—ใพใ—ใ‚‡ใ†ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใŒไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚ŒใŸ*ใƒœใ‚ญใƒฃใƒ–*ใŒใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚Œใพใ™๏ผš ```python >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") ``` ๆฌกใซใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซๆธกใ—ใพใ™๏ผš ```py >>> encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.") >>> print(encoded_input) {'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} ``` ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€้‡่ฆใช3ใคใฎ้ …็›ฎใ‚’ๆŒใค่พžๆ›ธใ‚’่ฟ”ใ—ใพใ™๏ผš * [input_ids](glossary#input-ids) ใฏๆ–‡ไธญใฎๅ„ใƒˆใƒผใ‚ฏใƒณใซๅฏพๅฟœใ™ใ‚‹ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใงใ™ใ€‚ * [attention_mask](glossary#attention-mask) ใฏใƒˆใƒผใ‚ฏใƒณใŒใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ๅ—ใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ * [token_type_ids](glossary#token-type-ids) ใฏ่ค‡ๆ•ฐใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใ‚ใ‚‹ๅ ดๅˆใ€ใƒˆใƒผใ‚ฏใƒณใŒใฉใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฑžใ—ใฆใ„ใ‚‹ใ‹ใ‚’่ญ˜ๅˆฅใ—ใพใ™ใ€‚ `input_ids` ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆๅ…ฅๅŠ›ใ‚’่ฟ”ใ—ใพใ™๏ผš ```python >>> tokenizer.decode(encoded_input["input_ids"]) '[CLS] ้ญ”ๆณ•ไฝฟใ„ใฎไบ‹ใซๅนฒๆธ‰ใ™ใ‚‹ใชใ€ๅฝผใ‚‰ใฏๅพฎๅฆ™ใงๆ€’ใ‚Šใฃใฝใ„ใ€‚ [SEP]' ``` ๅฆ‚ไฝ•ใซใŠๅˆ†ใ‹ใ‚Šใ„ใŸใ ใ‘ใ‚‹ใ‹ใจๆ€ใ„ใพใ™ใŒใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ“ใฎๆ–‡็ซ ใซ2ใคใฎ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใ€`CLS`๏ผˆใ‚ฏใƒฉใ‚ทใƒ•ใ‚กใ‚คใ‚ข๏ผ‰ใจ`SEP`๏ผˆใ‚ปใƒ‘ใƒฌใƒผใ‚ฟ๏ผ‰ใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸใ€‚ ใ™ในใฆใฎใƒขใƒ‡ใƒซใŒ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใ‚’ๅฟ…่ฆใจใ™ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅฟ…่ฆใชๅ ดๅˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏ่‡ชๅ‹•็š„ใซใใ‚Œใ‚‰ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ ่ค‡ๆ•ฐใฎๆ–‡็ซ ใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅ ดๅˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซใƒชใ‚นใƒˆใจใ—ใฆๆธกใ—ใฆใใ ใ•ใ„๏ผš ```py >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_inputs = tokenizer(batch_sentences) >>> print(encoded_inputs) {'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102]], 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]]} ``` ### Pad ๆ–‡็ซ ใฏๅธธใซๅŒใ˜้•ทใ•ใงใฏใชใ„ใ“ใจใŒใ‚ใ‚Šใ€ใ“ใ‚Œใฏใƒ†ใƒณใ‚ฝใƒซ๏ผˆใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›๏ผ‰ใŒๅ‡ไธ€ใชๅฝข็Šถใ‚’ๆŒใคๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ๅ•้กŒใจใชใ‚Šใพใ™ใ€‚ ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใฏใ€็Ÿญใ„ๆ–‡ใซ็‰นๅˆฅใชใ€Œใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ€ใ‚’่ฟฝๅŠ ใ—ใฆใ€ใƒ†ใƒณใ‚ฝใƒซใ‚’้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆใ‚ใ›ใ‚‹ใŸใ‚ใฎๆˆฆ็•ฅใงใ™ใ€‚ ใƒใƒƒใƒๅ†…ใฎ็Ÿญใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆœ€้•ทใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆใ‚ใ›ใ‚‹ใŸใ‚ใซใ€`padding`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`True`ใซ่จญๅฎšใ—ใพใ™๏ผš ```py >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True) >>> print(encoded_input) {'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} ``` 1็•ช็›ฎใจ3็•ช็›ฎใฎๆ–‡ใฏใ€็Ÿญใ„ใŸใ‚ใซ`0`ใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ ### Truncation ้€†ใฎใ‚นใƒšใ‚ฏใƒˆใƒซใงใฏใ€ๆ™‚ๆŠ˜ใ€ใƒขใƒ‡ใƒซใŒๅ‡ฆ็†ใ™ใ‚‹ใฎใซ้•ทใ™ใŽใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใฎๅ ดๅˆใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’็Ÿญ็ธฎใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคงใฎ้•ทใ•ใซใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ใซใฏใ€`truncation`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`True`ใซ่จญๅฎšใ—ใพใ™๏ผš ```py >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True) >>> print(encoded_input) {'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} ``` <Tip> ็•ฐใชใ‚‹ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚ใฎๅผ•ๆ•ฐใซใคใ„ใฆ่ฉณใ—ใใฏใ€[ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚](./pad_truncation)ใฎใ‚ณใƒณใ‚ปใƒ—ใƒˆใ‚ฌใ‚คใƒ‰ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ </Tip> ### Build tensors ๆœ€ๅพŒใซใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒใƒขใƒ‡ใƒซใซไพ›็ตฆใ•ใ‚Œใ‚‹ๅฎŸ้š›ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ™ใ‚ˆใ†ใซ่จญๅฎšใ—ใพใ™ใ€‚ `return_tensors`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`pt`๏ผˆPyTorch็”จ๏ผ‰ใพใŸใฏ`tf`๏ผˆTensorFlow็”จ๏ผ‰ใซ่จญๅฎšใ—ใพใ™๏ผš <frameworkcontent> <pt> ```py >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="pt") >>> print(encoded_input) {'input_ids': tensor([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]]), 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]])} ``` </pt> <tf> ```py >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="tf") >>> print(encoded_input) {'input_ids': <tf.Tensor: shape=(2, 9), dtype=int32, numpy= array([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>, 'token_type_ids': <tf.Tensor: shape=(2, 9), dtype=int32, numpy= array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>, 'attention_mask': <tf.Tensor: shape=(2, 9), dtype=int32, numpy= array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>} ``` </tf> </frameworkcontent> ## Audio ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒขใƒ‡ใƒซ็”จใซๆบ–ๅ‚™ใ™ใ‚‹ใŸใ‚ใซ[็‰นๅพดๆŠฝๅ‡บๅ™จ](main_classes/feature_extractor)ใŒๅฟ…่ฆใงใ™ใ€‚ ็‰นๅพดๆŠฝๅ‡บๅ™จใฏ็”Ÿใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‹ใ‚‰็‰นๅพดใ‚’ๆŠฝๅ‡บใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ [PolyAI/minds14](https://huggingface.co/datasets/PolyAI/minds14)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใฆ๏ผˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒญใƒผใƒ‰ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏ๐Ÿค— [Datasetsใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://huggingface.co/docs/datasets/load_hub)ใ‚’ๅ‚็…ง๏ผ‰ใ€ ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ใฉใฎใ‚ˆใ†ใซไฝฟ็”จใงใใ‚‹ใ‹ใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†๏ผš ```python >>> from datasets import load_dataset, Audio >>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") ``` ใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆ`audio`ๅˆ—ใฎๆœ€ๅˆใฎ่ฆ็ด ใ‚’็ขบ่ชใ—ใพใ™ใ€‚`audio`ๅˆ—ใ‚’ๅ‘ผใณๅ‡บใ™ใจใ€่‡ชๅ‹•็š„ใซใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใŒ่ชญใฟ่พผใพใ‚Œใ€ใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใพใ™๏ผš ```py >>> dataset[0]["audio"] {'array': array([ 0. , 0.00024414, -0.00024414, ..., -0.00024414, 0. , 0. ], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', 'sampling_rate': 8000} ``` ใ“ใ‚Œใซใ‚ˆใ‚Šใ€3ใคใฎใ‚ขใ‚คใƒ†ใƒ ใŒ่ฟ”ใ•ใ‚Œใพใ™๏ผš * `array` ใฏ่ชญใฟ่พผใพใ‚ŒใŸ้Ÿณๅฃฐไฟกๅทใงใ€1Dใฎ้…ๅˆ—ใจใ—ใฆ่ชญใฟ่พผใพใ‚Œใพใ™ใ€‚ๅฟ…่ฆใซๅฟœใ˜ใฆใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใ‚‹ใ“ใจใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ * `path` ใฏ้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใฎๅ ดๆ‰€ใ‚’ๆŒ‡ใ—ใพใ™ใ€‚ * `sampling_rate` ใฏ้Ÿณๅฃฐไฟกๅทๅ†…ใฎใƒ‡ใƒผใ‚ฟใƒใ‚คใƒณใƒˆใŒ1็ง’้–“ใซใ„ใใคๆธฌๅฎšใ•ใ‚Œใ‚‹ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€[Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base)ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใ‚’็ขบ่ชใ™ใ‚‹ใจใ€Wav2Vec2ใŒ16kHzใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚ŒใŸ้Ÿณๅฃฐใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใงไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ๅญฆ็ฟ’ใซไฝฟ็”จใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใจใ€ใ‚ใชใŸใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใŒไธ€่‡ดใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ ใƒ‡ใƒผใ‚ฟใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใŒ็•ฐใชใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใ‚’ใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ 1. ๐Ÿค— Datasetsใฎ [`~datasets.Dataset.cast_column`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใ‚’16kHzใซใ‚ขใƒƒใƒ—ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ—ใพใ™๏ผš ```py >>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16_000)) ``` 2. ๅ†ใณ `audio` ๅˆ—ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใ‚’ใƒชใ‚ตใƒณใƒ—ใƒซใ—ใพใ™๏ผš ```py >>> dataset[0]["audio"] {'array': array([ 2.3443763e-05, 2.1729663e-04, 2.2145823e-04, ..., 3.8356509e-05, -7.3497440e-06, -2.1754686e-05], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', 'sampling_rate': 16000} ``` ๆฌกใซใ€ๅ…ฅๅŠ›ใ‚’ๆญฃ่ฆๅŒ–ใ—ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใซ็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใƒ‡ใƒผใ‚ฟใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€็Ÿญใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซใฏ `0` ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ๅŒใ˜่€ƒใˆๆ–นใŒใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใซใ‚‚้ฉ็”จใ•ใ‚Œใพใ™ใ€‚็‰นๅพดๆŠฝๅ‡บๅ™จใฏ `array` ใซ `0` ใ‚’่ฟฝๅŠ ใ—ใพใ™๏ผˆใ“ใ‚Œใฏ็„ก้Ÿณใจใ—ใฆ่งฃ้‡ˆใ•ใ‚Œใพใ™๏ผ‰ใ€‚ [`AutoFeatureExtractor.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆ็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš ```python >>> from transformers import AutoFeatureExtractor >>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") ``` ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช `array` ใ‚’็‰นๅพดๆŠฝๅ‡บๅ™จใซๆธกใ—ใพใ™ใ€‚็‰นๅพดๆŠฝๅ‡บๅ™จใง็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹็„ก้Ÿณใ‚จใƒฉใƒผใ‚’ใ‚ˆใ‚Š่‰ฏใใƒ‡ใƒใƒƒใ‚ฐใ™ใ‚‹ใŸใ‚ใซใ€็‰นๅพดๆŠฝๅ‡บๅ™จใซ `sampling_rate` ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ ```python >>> audio_input = [dataset[0]["audio"]["array"]] >>> feature_extractor(audio_input, sampling_rate=16000) {'input_values': [array([ 3.8106556e-04, 2.7506407e-03, 2.8015103e-03, ..., 5.6335266e-04, 4.6588284e-06, -1.7142107e-04], dtype=float32)]} ``` ๅŒๆง˜ใซใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจๅŒๆง˜ใซใ€ใƒใƒƒใƒๅ†…ใฎๅฏๅค‰ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใพใŸใฏๅˆ‡ใ‚Š่ฉฐใ‚ใ‚’้ฉ็”จใงใใพใ™ใ€‚ๆฌกใซใ€ใ“ใ‚Œใ‚‰ใฎ2ใคใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ตใƒณใƒ—ใƒซใฎใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†๏ผš ```python >>> dataset[0]["audio"]["array"].shape (173398,) >>> dataset[1]["audio"]["array"].shape (106496,) ``` ใ“ใฎ้–ขๆ•ฐใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅ‰ๅ‡ฆ็†ใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ตใƒณใƒ—ใƒซใฎ้•ทใ•ใ‚’ๅŒใ˜ใซใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™ใ€‚ๆœ€ๅคงใ‚ตใƒณใƒ—ใƒซ้•ทใ‚’ๆŒ‡ๅฎšใ—ใ€็‰นๅพดๆŠฝๅ‡บๅ™จใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใใ‚Œใซๅˆใ‚ใ›ใฆใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใพใŸใฏๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™ใ€‚ ```py >>> def preprocess_function(examples): ... audio_arrays = [x["array"] for x in examples["audio"]] ... inputs = feature_extractor( ... audio_arrays, ... sampling_rate=16000, ... padding=True, ... max_length=100000, ... truncation=True, ... ) ... return inputs ``` `preprocess_function`ใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๆœ€ๅˆใฎๆ•ฐไพ‹ใซ้ฉ็”จใ—ใพใ™๏ผš ```python >>> processed_dataset = preprocess_function(dataset[:5]) ``` ใ‚ตใƒณใƒ—ใƒซใฎ้•ทใ•ใฏ็พๅœจๅŒใ˜ใงใ€ๆŒ‡ๅฎšใ•ใ‚ŒใŸๆœ€ๅคง้•ทใจไธ€่‡ดใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใงๅ‡ฆ็†ใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒขใƒ‡ใƒซใซๆธกใ™ใ“ใจใŒใงใใพใ™๏ผ ```py >>> processed_dataset["input_values"][0].shape (100000,) >>> processed_dataset["input_values"][1].shape (100000,) ``` ## Computer Vision ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใงใฏใ€ใƒขใƒ‡ใƒซ็”จใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ใŸใ‚ใฎ[็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/image_processor)ใŒๅฟ…่ฆใงใ™ใ€‚ ็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใซใฏใ€็”ปๅƒใ‚’ใƒขใƒ‡ใƒซใŒๆœŸๅพ…ใ™ใ‚‹ๅ…ฅๅŠ›ๅฝขๅผใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎใ„ใใคใ‹ใฎใ‚นใƒ†ใƒƒใƒ—ใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚นใƒ†ใƒƒใƒ—ใซใฏใ€ใƒชใ‚ตใ‚คใ‚บใ€ๆญฃ่ฆๅŒ–ใ€ใ‚ซใƒฉใƒผใƒใƒฃใƒใƒซใฎ่ฃœๆญฃใ€ใŠใ‚ˆใณ็”ปๅƒใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ใชใฉใŒๅซใพใ‚Œใพใ™ใ€‚ <Tip> ็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใฏใ€้€šๅธธใ€็”ปๅƒใฎๅข—ๅผทใฎๅฝขๅผใซๅพ“ใ„ใพใ™ใ€‚็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใจ็”ปๅƒใฎๅข—ๅผทใฎไธกๆ–นใฏ็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚’ๅค‰ๆ›ใ—ใพใ™ใŒใ€็•ฐใชใ‚‹็›ฎ็š„ใŒใ‚ใ‚Šใพใ™๏ผš * ็”ปๅƒใฎๅข—ๅผทใฏใ€้Žๅญฆ็ฟ’ใ‚’้˜ฒใŽใ€ใƒขใƒ‡ใƒซใฎๅ …็‰ขๆ€งใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใฎใซๅฝน็ซ‹ใคๆ–นๆณ•ใง็”ปๅƒใ‚’ๅค‰ๆ›ดใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚’ๅข—ๅผทใ™ใ‚‹ๆ–นๆณ•ใฏ็„ก้™ใงใ€ๆ˜Žใ‚‹ใ•ใ‚„่‰ฒใฎ่ชฟๆ•ดใ€ใ‚ฏใƒญใƒƒใƒ—ใ€ๅ›ž่ปขใ€ใƒชใ‚ตใ‚คใ‚บใ€ใ‚บใƒผใƒ ใชใฉใ€ๆง˜ใ€…ใชๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€ๅข—ๅผทๆ“ไฝœใซใ‚ˆใฃใฆ็”ปๅƒใฎๆ„ๅ‘ณใŒๅค‰ใ‚ใ‚‰ใชใ„ใ‚ˆใ†ใซๆณจๆ„ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ * ็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใฏใ€็”ปๅƒใŒใƒขใƒ‡ใƒซใฎๆœŸๅพ…ใ™ใ‚‹ๅ…ฅๅŠ›ๅฝขๅผใจไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’ไฟ่จผใ—ใพใ™ใ€‚ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€็”ปๅƒใฏใƒขใƒ‡ใƒซใŒๆœ€ๅˆใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใจใใจใพใฃใŸใๅŒใ˜ๆ–นๆณ•ใงๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ็”ปๅƒใฎๅข—ๅผทใซใฏไปปๆ„ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใงใใพใ™ใ€‚็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใซใฏใ€ใƒขใƒ‡ใƒซใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸ`ImageProcessor`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ </Tip> ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ™ใŸใ‚ใซใ€[food101](https://huggingface.co/datasets/food101)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒญใƒผใƒ‰ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏ๐Ÿค—[Datasetsใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://huggingface.co/docs/datasets/load_hub)ใ‚’ๅ‚็…ง๏ผ‰๏ผš <Tip> ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒใ‹ใชใ‚Šๅคงใใ„ใŸใ‚ใ€๐Ÿค— Datasetsใฎ`split`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใฎๅฐใ•ใชใ‚ตใƒณใƒ—ใƒซใฎใฟใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผ </Tip> ```python >>> from datasets import load_dataset >>> dataset = load_dataset("food101", split="train[:100]") ``` ๆฌกใซใ€๐Ÿค— Datasetsใฎ [`Image`](https://huggingface.co/docs/datasets/package_reference/main_classes?highlight=image#datasets.Image) ๆฉŸ่ƒฝใง็”ปๅƒใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†๏ผš ```python >>> dataset[0]["image"] ``` <div class="flex justify-center"> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vision-preprocess-tutorial.png"/> </div> AutoImageProcessorใ‚’[`AutoImageProcessor.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใƒญใƒผใƒ‰ใ—ใพใ™๏ผš ```py >>> from transformers import AutoImageProcessor >>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") ``` 1. ใพใšใ€็”ปๅƒใฎๆ‹กๅผตใ‚’่ฟฝๅŠ ใ—ใพใ—ใ‚‡ใ†ใ€‚ๅฅฝใใชใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใงใใพใ™ใŒใ€ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏtorchvisionใฎ[`transforms`](https://pytorch.org/vision/stable/transforms.html)ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅˆฅใฎใƒ‡ใƒผใ‚ฟๆ‹กๅผตใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€[Albumentations](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb)ใพใŸใฏ[Kornia notebooks](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb)ใง่ฉณ็ดฐใ‚’ๅญฆใถใ“ใจใŒใงใใพใ™ใ€‚ ใ“ใ“ใงใฏใ€[`Compose`](https://pytorch.org/vision/master/generated/torchvision.transforms.Compose.html)ใ‚’ไฝฟ็”จใ—ใฆใ„ใใคใ‹ใฎๅค‰ๆ›ใ‚’้€ฃ้Ž–ใ•ใ›ใพใ™ - [`RandomResizedCrop`](https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html)ใจ[`ColorJitter`](https://pytorch.org/vision/main/generated/torchvision.transforms.ColorJitter.html)ใ€‚ ใ‚ตใ‚คใ‚บใฎๅค‰ๆ›ดใซ้–ขใ—ใฆใฏใ€`image_processor`ใ‹ใ‚‰็”ปๅƒใ‚ตใ‚คใ‚บใฎ่ฆไปถใ‚’ๅ–ๅพ—ใงใใพใ™ใ€‚ ไธ€้ƒจใฎใƒขใƒ‡ใƒซใงใฏใ€ๆญฃ็ขบใช้ซ˜ใ•ใจๅน…ใŒๅฟ…่ฆใงใ™ใŒใ€ไป–ใฎใƒขใƒ‡ใƒซใงใฏ`shortest_edge`ใฎใฟใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใพใ™ใ€‚ ```py >>> from torchvision.transforms import RandomResizedCrop, ColorJitter, Compose >>> size = ( ... image_processor.size["shortest_edge"] ... if "shortest_edge" in image_processor.size ... else (image_processor.size["height"], image_processor.size["width"]) ... ) >>> _transforms = Compose([RandomResizedCrop(size), ColorJitter(brightness=0.5, hue=0.5)]) ``` 2. ใƒขใƒ‡ใƒซใฏ[`pixel_values`](model_doc/visionencoderdecoder#transformers.VisionEncoderDecoderModel.forward.pixel_values)ใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใพใ™ใ€‚ `ImageProcessor`ใฏ็”ปๅƒใฎๆญฃ่ฆๅŒ–ใจ้ฉๅˆ‡ใชใƒ†ใƒณใ‚ฝใƒซใฎ็”Ÿๆˆใ‚’ๅ‡ฆ็†ใงใใพใ™ใ€‚ ไธ€้€ฃใฎ็”ปๅƒใซๅฏพใ™ใ‚‹็”ปๅƒๆ‹กๅผตใจ็”ปๅƒๅ‰ๅ‡ฆ็†ใ‚’็ต„ใฟๅˆใ‚ใ›ใ€`pixel_values`ใ‚’็”Ÿๆˆใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™๏ผš ```python >>> def transforms(examples): ... images = [_transforms(img.convert("RGB")) for img in examples["image"]] ... examples["pixel_values"] = image_processor(images, do_resize=False, return_tensors="pt")["pixel_values"] ... return examples ``` <Tip> ไธŠ่จ˜ใฎไพ‹ใงใฏใ€็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚’ๆ—ขใซ็”ปๅƒๅข—ๅผทๅค‰ๆ›ใง่กŒใฃใฆใ„ใ‚‹ใŸใ‚ใ€`do_resize=False`ใ‚’่จญๅฎšใ—ใพใ—ใŸใ€‚ ้ฉๅˆ‡ใช `image_processor` ใ‹ใ‚‰ใฎ `size` ๅฑžๆ€งใ‚’ๆดป็”จใ—ใฆใ„ใพใ™ใ€‚็”ปๅƒๅข—ๅผทไธญใซ็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚’่กŒใ‚ใชใ„ๅ ดๅˆใฏใ€ใ“ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’็œ็•ฅใ—ใฆใใ ใ•ใ„ใ€‚ ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€`ImageProcessor` ใŒใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ ็”ปๅƒใ‚’ๅข—ๅผทๅค‰ๆ›ใฎไธ€้ƒจใจใ—ใฆๆญฃ่ฆๅŒ–ใ—ใŸใ„ๅ ดๅˆใฏใ€`image_processor.image_mean` ใจ `image_processor.image_std` ใฎๅ€คใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ </Tip> 3. ๆฌกใซใ€๐Ÿค— Datasetsใฎ[`set_transform`](https://huggingface.co/docs/datasets/process#format-transform)ใ‚’ไฝฟ็”จใ—ใฆใ€ๅค‰ๆ›ใ‚’ใƒชใ‚ขใƒซใ‚ฟใ‚คใƒ ใง้ฉ็”จใ—ใพใ™๏ผš ```python >>> dataset.set_transform(transforms) ``` 4. ็”ปๅƒใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใจใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใŒ `pixel_values` ใ‚’่ฟฝๅŠ ใ—ใŸใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใงๅ‡ฆ็†ๆธˆใฟใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒขใƒ‡ใƒซใซๆธกใ™ใ“ใจใŒใงใใพใ™๏ผ ```python >>> dataset[0].keys() ``` ไปฅไธ‹ใฏใ€ๅค‰ๆ›ใŒ้ฉ็”จใ•ใ‚ŒใŸๅพŒใฎ็”ปๅƒใฎๅค–่ฆณใงใ™ใ€‚ ็”ปๅƒใฏใƒฉใƒณใƒ€ใƒ ใซๅˆ‡ใ‚ŠๆŠœใ‹ใ‚Œใ€ใใฎ่‰ฒใฎ็‰นๆ€งใ‚‚็•ฐใชใ‚Šใพใ™ใ€‚ ```py >>> import numpy as np >>> import matplotlib.pyplot as plt >>> img = dataset[0]["pixel_values"] >>> plt.imshow(img.permute(1, 2, 0)) ``` <div class="flex justify-center"> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/preprocessed_image.png"/> </div> <Tip> ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ€ๆ„ๅ‘ณใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใŠใ‚ˆใณใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใชใฉใฎใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€`ImageProcessor`ใฏ ใƒใ‚นใƒˆๅ‡ฆ็†ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ใƒขใƒ‡ใƒซใฎ็”Ÿใฎๅ‡บๅŠ›ใ‚’ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚„ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใƒžใƒƒใƒ—ใชใฉใฎๆ„ๅ‘ณใฎใ‚ใ‚‹ไบˆๆธฌใซๅค‰ๆ›ใ—ใพใ™ใ€‚ </Tip> ### Pad ไธ€้ƒจใฎๅ ดๅˆใ€ใŸใจใˆใฐใ€[DETR](./model_doc/detr)ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚ใซใ‚นใ‚ฑใƒผใƒซใฎๅค‰ๆ›ดใ‚’้ฉ็”จใ—ใพใ™ใ€‚ ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒใƒƒใƒๅ†…ใฎ็”ปๅƒใฎใ‚ตใ‚คใ‚บใŒ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚[`DetrImageProcessor`]ใ‹ใ‚‰[`DetrImageProcessor.pad`]ใ‚’ไฝฟ็”จใ—ใ€ ใ‚ซใ‚นใ‚ฟใƒ ใฎ`collate_fn`ใ‚’ๅฎš็พฉใ—ใฆ็”ปๅƒใ‚’ไธ€็ท’ใซใƒใƒƒใƒๅ‡ฆ็†ใงใใพใ™ใ€‚ ```py >>> def collate_fn(batch): ... pixel_values = [item["pixel_values"] for item in batch] ... encoding = image_processor.pad(pixel_values, return_tensors="pt") ... labels = [item["labels"] for item in batch] ... batch = {} ... batch["pixel_values"] = encoding["pixel_values"] ... batch["pixel_mask"] = encoding["pixel_mask"] ... batch["labels"] = labels ... return batch ``` ## Multi Modal ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซๅ…ฅๅŠ›ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซ็”จใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ใŸใ‚ใฎ[ใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/processors)ใŒๅฟ…่ฆใงใ™ใ€‚ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚„็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใชใฉใฎ2ใคใฎๅ‡ฆ็†ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’็ตๅˆใ—ใพใ™ใ€‚ ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜๏ผˆASR๏ผ‰ใฎใŸใ‚ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎไฝฟ็”จๆ–นๆณ•ใ‚’็คบใ™ใŸใ‚ใซใ€[LJ Speech](https://huggingface.co/datasets/lj_speech)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒญใƒผใƒ‰ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏ๐Ÿค— [Datasets ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://huggingface.co/docs/datasets/load_hub)ใ‚’ๅ‚็…ง๏ผ‰๏ผš ```python >>> from datasets import load_dataset >>> lj_speech = load_dataset("lj_speech", split="train") ``` ASR๏ผˆ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜๏ผ‰ใฎๅ ดๅˆใ€ไธปใซ `audio` ใจ `text` ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใ‚‹ใŸใ‚ใ€ไป–ใฎๅˆ—ใ‚’ๅ‰Š้™คใงใใพใ™๏ผš ```python >>> lj_speech = lj_speech.map(remove_columns=["file", "id", "normalized_text"]) ``` ๆฌกใซใ€`audio`ใจ`text`ใฎๅˆ—ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†๏ผš ```python >>> lj_speech[0]["audio"] {'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ..., 7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', 'sampling_rate': 22050} >>> lj_speech[0]["text"] 'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition' ``` ๅธธใซใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใ‚’ใ€ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ๅญฆ็ฟ’ใซไฝฟ็”จใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใจไธ€่‡ดใ•ใ›ใ‚‹ใ‚ˆใ†ใซ[ใƒชใ‚ตใƒณใƒ—ใƒซ](preprocessing#audio)ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผ ```py >>> lj_speech = lj_speech.cast_column("audio", Audio(sampling_rate=16_000)) ``` ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ [`AutoProcessor.from_pretrained`] ใ‚’ไฝฟ็”จใ—ใฆใƒญใƒผใƒ‰ใ—ใพใ™๏ผš ```py >>> from transformers import AutoProcessor >>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h") ``` 1. `array`ๅ†…ใซๅซใพใ‚Œใ‚‹ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‚’`input_values`ใซๅ‡ฆ็†ใ—ใ€`text`ใ‚’`labels`ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™๏ผš ```py >>> def prepare_dataset(example): ... audio = example["audio"] ... example.update(processor(audio=audio["array"], text=example["text"], sampling_rate=16000)) ... return example ``` 2. ใ‚ตใƒณใƒ—ใƒซใซ`prepare_dataset`้–ขๆ•ฐใ‚’้ฉ็”จใ—ใพใ™๏ผš ```py >>> prepare_dataset(lj_speech[0]) ```
transformers/docs/source/ja/preprocessing.md/0
{ "file_path": "transformers/docs/source/ja/preprocessing.md", "repo_id": "transformers", "token_count": 12720 }
296
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Multiple choice [[open-in-colab]] ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏใฏ่ณชๅ•ๅฟœ็ญ”ใซไผผใฆใ„ใพใ™ใŒใ€ใ„ใใคใ‹ใฎๅ€™่ฃœใฎๅ›ž็ญ”ใŒใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใจใจใ‚‚ใซๆไพ›ใ•ใ‚Œใ€ๆญฃใ—ใ„ๅ›ž็ญ”ใ‚’้ธๆŠžใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ‚‹็‚นใŒ็•ฐใชใ‚Šใพใ™ใ€‚ ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ 1. [SWAG](https://huggingface.co/datasets/swag) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ€Œ้€šๅธธใ€ๆง‹ๆˆใง [BERT](https://huggingface.co/google-bert/bert-base-uncased) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใ€ๆœ€้ฉใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’้ธๆŠžใ—ใพใ™่ค‡ๆ•ฐใฎ้ธๆŠž่‚ขใจไฝ•ใ‚‰ใ‹ใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’่€ƒๆ…ฎใ—ใฆๅ›ž็ญ”ใ—ใพใ™ใ€‚ 2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ ```bash pip install transformers datasets evaluate ``` ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ ```py >>> from huggingface_hub import notebook_login >>> notebook_login() ``` ## Load SWAG dataset ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ SWAG ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ€Œ้€šๅธธใ€ๆง‹ๆˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ ```py >>> from datasets import load_dataset >>> swag = load_dataset("swag", "regular") ``` ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ ```py >>> swag["train"][0] {'ending0': 'passes by walking down the street playing their instruments.', 'ending1': 'has heard approaching them.', 'ending2': "arrives and they're outside dancing and asleep.", 'ending3': 'turns the lead singer watches the performance.', 'fold-ind': '3416', 'gold-source': 'gold', 'label': 0, 'sent1': 'Members of the procession walk down the street holding small horn brass instruments.', 'sent2': 'A drum line', 'startphrase': 'Members of the procession walk down the street holding small horn brass instruments. A drum line', 'video-id': 'anetv_jkn6uvmqwh4'} ``` ใ“ใ“ใซใฏใŸใใ•ใ‚“ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒใ‚ใ‚‹ใ‚ˆใ†ใซ่ฆ‹ใˆใพใ™ใŒใ€ๅฎŸ้š›ใฏ้žๅธธใซ็ฐกๅ˜ใงใ™ใ€‚ - `sent1` ใจ `sent2`: ใ“ใ‚Œใ‚‰ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏๆ–‡ใฎๅง‹ใพใ‚Šใ‚’็คบใ—ใ€ใ“ใฎ 2 ใคใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใจ `startphrase` ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ - `ending`: ๆ–‡ใฎ็ต‚ใ‚ใ‚Šๆ–นใจใ—ใฆ่€ƒใˆใ‚‰ใ‚Œใ‚‹็ต‚ใ‚ใ‚Šๆ–นใ‚’็คบๅ”†ใ—ใพใ™ใŒใ€ๆญฃใ—ใ„ใฎใฏ 1 ใคใ ใ‘ใงใ™ใ€‚ - `label`: ๆญฃใ—ใ„ๆ–‡ใฎ็ต‚ใ‚ใ‚Šใ‚’่ญ˜ๅˆฅใ—ใพใ™ใ€‚ ## Preprocess ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€BERT ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€ๆ–‡ใฎๅง‹ใพใ‚Šใจ 4 ใคใฎๅฏ่ƒฝใช็ต‚ใ‚ใ‚Šใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ ```py >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") ``` ไฝœๆˆใ™ใ‚‹ๅ‰ๅ‡ฆ็†้–ขๆ•ฐใฏๆฌกใฎใ“ใจใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ 1. `sent1` ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฎใ‚ณใƒ”ใƒผใ‚’ 4 ใคไฝœๆˆใ—ใ€ใใ‚Œใžใ‚Œใ‚’ `sent2` ใจ็ต„ใฟๅˆใ‚ใ›ใฆๆ–‡ใฎๅง‹ใพใ‚Šใ‚’ๅ†็พใ—ใพใ™ใ€‚ 2. `sent2` ใ‚’ 4 ใคใฎๅฏ่ƒฝใชๆ–‡ๆœซๅฐพใฎใใ‚Œใžใ‚Œใจ็ต„ใฟๅˆใ‚ใ›ใพใ™ใ€‚ 3. ใ“ใ‚Œใ‚‰ 2 ใคใฎใƒชใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใใ‚‹ใ‚ˆใ†ใซใƒ•ใƒฉใƒƒใƒˆๅŒ–ใ—ใ€ใใฎๅพŒใ€ๅ„ไพ‹ใซๅฏพๅฟœใ™ใ‚‹ `input_ids`ใ€`attention_mask`ใ€ใŠใ‚ˆใณ `labels` ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒๅซใพใ‚Œใ‚‹ใ‚ˆใ†ใซ้žใƒ•ใƒฉใƒƒใƒˆๅŒ–ใ—ใพใ™ใ€‚ ```py >>> ending_names = ["ending0", "ending1", "ending2", "ending3"] >>> def preprocess_function(examples): ... first_sentences = [[context] * 4 for context in examples["sent1"]] ... question_headers = examples["sent2"] ... second_sentences = [ ... [f"{header} {examples[end][i]}" for end in ending_names] for i, header in enumerate(question_headers) ... ] ... first_sentences = sum(first_sentences, []) ... second_sentences = sum(second_sentences, []) ... tokenized_examples = tokenizer(first_sentences, second_sentences, truncation=True) ... return {k: [v[i : i + 4] for i in range(0, len(v), 4)] for k, v in tokenized_examples.items()} ``` ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใงใ€`map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ ```py tokenized_swag = swag.map(preprocess_function, batched=True) ``` ๐Ÿค— Transformers ใซใฏๅคš่‚ข้ธๆŠž็”จใฎใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใŒใชใ„ใŸใ‚ใ€[`DataCollatโ€‹โ€‹orWithPadding`] ใ‚’่ชฟๆ•ดใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ๆœ€ๅคง้•ทใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใฎใงใฏใชใใ€็…งๅˆไธญใซใƒใƒƒใƒๅ†…ใฎๆœ€้•ทใฎ้•ทใ•ใพใงๆ–‡ใ‚’ *ๅ‹•็š„ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ* ใ™ใ‚‹ๆ–นใŒๅŠน็އ็š„ใงใ™ใ€‚ `DataCollatโ€‹โ€‹orForMultipleChoice` ใฏใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซๅ…ฅๅŠ›ใ‚’ๅนณๅฆๅŒ–ใ—ใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’้ฉ็”จใ—ใฆใ€็ตๆžœใ‚’้žๅนณๅฆๅŒ–ใ—ใพใ™ใ€‚ <frameworkcontent> <pt> ```py >>> from dataclasses import dataclass >>> from transformers.tokenization_utils_base import PreTrainedTokenizerBase, PaddingStrategy >>> from typing import Optional, Union >>> import torch >>> @dataclass ... class DataCollatorForMultipleChoice: ... """ ... Data collator that will dynamically pad the inputs for multiple choice received. ... """ ... tokenizer: PreTrainedTokenizerBase ... padding: Union[bool, str, PaddingStrategy] = True ... max_length: Optional[int] = None ... pad_to_multiple_of: Optional[int] = None ... def __call__(self, features): ... label_name = "label" if "label" in features[0].keys() else "labels" ... labels = [feature.pop(label_name) for feature in features] ... batch_size = len(features) ... num_choices = len(features[0]["input_ids"]) ... flattened_features = [ ... [{k: v[i] for k, v in feature.items()} for i in range(num_choices)] for feature in features ... ] ... flattened_features = sum(flattened_features, []) ... batch = self.tokenizer.pad( ... flattened_features, ... padding=self.padding, ... max_length=self.max_length, ... pad_to_multiple_of=self.pad_to_multiple_of, ... return_tensors="pt", ... ) ... batch = {k: v.view(batch_size, num_choices, -1) for k, v in batch.items()} ... batch["labels"] = torch.tensor(labels, dtype=torch.int64) ... return batch ``` </pt> <tf> ```py >>> from dataclasses import dataclass >>> from transformers.tokenization_utils_base import PreTrainedTokenizerBase, PaddingStrategy >>> from typing import Optional, Union >>> import tensorflow as tf >>> @dataclass ... class DataCollatorForMultipleChoice: ... """ ... Data collator that will dynamically pad the inputs for multiple choice received. ... """ ... tokenizer: PreTrainedTokenizerBase ... padding: Union[bool, str, PaddingStrategy] = True ... max_length: Optional[int] = None ... pad_to_multiple_of: Optional[int] = None ... def __call__(self, features): ... label_name = "label" if "label" in features[0].keys() else "labels" ... labels = [feature.pop(label_name) for feature in features] ... batch_size = len(features) ... num_choices = len(features[0]["input_ids"]) ... flattened_features = [ ... [{k: v[i] for k, v in feature.items()} for i in range(num_choices)] for feature in features ... ] ... flattened_features = sum(flattened_features, []) ... batch = self.tokenizer.pad( ... flattened_features, ... padding=self.padding, ... max_length=self.max_length, ... pad_to_multiple_of=self.pad_to_multiple_of, ... return_tensors="tf", ... ) ... batch = {k: tf.reshape(v, (batch_size, num_choices, -1)) for k, v in batch.items()} ... batch["labels"] = tf.convert_to_tensor(labels, dtype=tf.int64) ... return batch ``` </tf> </frameworkcontent> ## Evaluate ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ชญใฟ่พผใฟใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„) ) ใƒกใƒˆใƒชใ‚ฏใ‚นใฎ่ชญใฟ่พผใฟใจ่จˆ็ฎ—ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ๆฌกใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„)ใ€‚ ```py >>> import evaluate >>> accuracy = evaluate.load("accuracy") ``` ๆฌกใซใ€ไบˆๆธฌใจใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆ็ฒพๅบฆใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ ```py >>> import numpy as np >>> def compute_metrics(eval_pred): ... predictions, labels = eval_pred ... predictions = np.argmax(predictions, axis=1) ... return accuracy.compute(predictions=predictions, references=labels) ``` ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ ## Train <frameworkcontent> <pt> <Tip> [`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ </Tip> ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForMultipleChoice`] ใ‚’ไฝฟ็”จใ—ใฆ BERT ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ ```py >>> from transformers import AutoModelForMultipleChoice, TrainingArguments, Trainer >>> model = AutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") ``` ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ 1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`Trainer`] ใฏ็ฒพๅบฆใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ 2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ 3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ ```py >>> training_args = TrainingArguments( ... output_dir="my_awesome_swag_model", ... eval_strategy="epoch", ... save_strategy="epoch", ... load_best_model_at_end=True, ... learning_rate=5e-5, ... per_device_train_batch_size=16, ... per_device_eval_batch_size=16, ... num_train_epochs=3, ... weight_decay=0.01, ... push_to_hub=True, ... ) >>> trainer = Trainer( ... model=model, ... args=training_args, ... train_dataset=tokenized_swag["train"], ... eval_dataset=tokenized_swag["validation"], ... tokenizer=tokenizer, ... data_collator=DataCollatorForMultipleChoice(tokenizer=tokenizer), ... compute_metrics=compute_metrics, ... ) >>> trainer.train() ``` ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใพใ™ใ‚ˆใ†ใซใ€‚ ```py >>> trainer.push_to_hub() ``` </pt> <tf> <Tip> Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-a-tensorflow-model-with-keras) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ </Tip> TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ ```py >>> from transformers import create_optimizer >>> batch_size = 16 >>> num_train_epochs = 2 >>> total_train_steps = (len(tokenized_swag["train"]) // batch_size) * num_train_epochs >>> optimizer, schedule = create_optimizer(init_lr=5e-5, num_warmup_steps=0, num_train_steps=total_train_steps) ``` ๆฌกใซใ€[`TFAutoModelForMultipleChoice`] ใ‚’ไฝฟ็”จใ—ใฆ BERT ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ ```py >>> from transformers import TFAutoModelForMultipleChoice >>> model = TFAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") ``` [`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ ```py >>> data_collator = DataCollatorForMultipleChoice(tokenizer=tokenizer) >>> tf_train_set = model.prepare_tf_dataset( ... tokenized_swag["train"], ... shuffle=True, ... batch_size=batch_size, ... collate_fn=data_collator, ... ) >>> tf_validation_set = model.prepare_tf_dataset( ... tokenized_swag["validation"], ... shuffle=False, ... batch_size=batch_size, ... collate_fn=data_collator, ... ) ``` [`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ ```py >>> model.compile(optimizer=optimizer) # No loss argument! ``` ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ๆœ€ๅพŒใฎ 2 ใคใฎใ“ใจใฏใ€ไบˆๆธฌใ‹ใ‚‰็ฒพๅบฆใ‚’่จˆ็ฎ—ใ™ใ‚‹ใ“ใจใจใ€ใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใฉใกใ‚‰ใ‚‚ [Keras ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ](../main_classes/keras_callbacks) ใ‚’ไฝฟ็”จใ—ใฆ่กŒใ‚ใ‚Œใพใ™ใ€‚ `compute_metrics` ้–ขๆ•ฐใ‚’ [`~transformers.KerasMetricCallback`] ใซๆธกใ—ใพใ™ใ€‚ ```py >>> from transformers.keras_callbacks import KerasMetricCallback >>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) ``` [`~transformers.PushToHubCallback`] ใงใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ ```py >>> from transformers.keras_callbacks import PushToHubCallback >>> push_to_hub_callback = PushToHubCallback( ... output_dir="my_awesome_model", ... tokenizer=tokenizer, ... ) ``` ๆฌกใซใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ใพใจใ‚ใฆใƒใƒณใƒ‰ใƒซใ—ใพใ™ใ€‚ ```py >>> callbacks = [metric_callback, push_to_hub_callback] ``` ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ ```py >>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=2, callbacks=callbacks) ``` ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ </tf> </frameworkcontent> <Tip> ่ค‡ๆ•ฐ้ธๆŠž็”จใซใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ [PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb) ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb)ใ€‚ </Tip> # Inference ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ ใ„ใใคใ‹ใฎใƒ†ใ‚ญใ‚นใƒˆใจ 2 ใคใฎๅ›ž็ญ”ๅ€™่ฃœใ‚’่€ƒใˆใฆใใ ใ•ใ„ใ€‚ ```py >>> prompt = "France has a bread law, Le Dรฉcret Pain, with strict rules on what is allowed in a traditional baguette." >>> candidate1 = "The law does not apply to croissants and brioche." >>> candidate2 = "The law applies to baguettes." ``` <frameworkcontent> <pt> ๅ„ใƒ—ใƒญใƒณใƒ—ใƒˆใจๅ›ž็ญ”ๅ€™่ฃœใฎใƒšใ‚ขใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€PyTorch ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ„ใใคใ‹ใฎ`lables`ใ‚‚ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ```py >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_swag_model") >>> inputs = tokenizer([[prompt, candidate1], [prompt, candidate2]], return_tensors="pt", padding=True) >>> labels = torch.tensor(0).unsqueeze(0) ``` ๅ…ฅๅŠ›ใจใƒฉใƒ™ใƒซใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ ```py >>> from transformers import AutoModelForMultipleChoice >>> model = AutoModelForMultipleChoice.from_pretrained("my_awesome_swag_model") >>> outputs = model(**{k: v.unsqueeze(0) for k, v in inputs.items()}, labels=labels) >>> logits = outputs.logits ``` ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ ```py >>> predicted_class = logits.argmax().item() >>> predicted_class '0' ``` </pt> <tf> ๅ„ใƒ—ใƒญใƒณใƒ—ใƒˆใจๅ›ž็ญ”ๅ€™่ฃœใฎใƒšใ‚ขใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€TensorFlow ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ ```py >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_swag_model") >>> inputs = tokenizer([[prompt, candidate1], [prompt, candidate2]], return_tensors="tf", padding=True) ``` ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ ```py >>> from transformers import TFAutoModelForMultipleChoice >>> model = TFAutoModelForMultipleChoice.from_pretrained("my_awesome_swag_model") >>> inputs = {k: tf.expand_dims(v, 0) for k, v in inputs.items()} >>> outputs = model(inputs) >>> logits = outputs.logits ``` ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ ```py >>> predicted_class = int(tf.math.argmax(logits, axis=-1)[0]) >>> predicted_class '0' ``` </tf> </frameworkcontent>
transformers/docs/source/ja/tasks/multiple_choice.md/0
{ "file_path": "transformers/docs/source/ja/tasks/multiple_choice.md", "repo_id": "transformers", "token_count": 7533 }
297
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # XLA Integration for TensorFlow Models [[open-in-colab]] ๅŠ ้€Ÿ็ทšๅฝขไปฃๆ•ฐ๏ผˆAccelerated Linear Algebra๏ผ‰ใ€้€š็งฐXLAใฏใ€TensorFlowใƒขใƒ‡ใƒซใฎใƒฉใƒณใ‚ฟใ‚คใƒ ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใฎใ‚ณใƒณใƒ‘ใ‚คใƒฉใงใ™ใ€‚[ๅ…ฌๅผใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.tensorflow.org/xla)ใซใ‚ˆใ‚Œใฐใ€XLA๏ผˆAccelerated Linear Algebra๏ผ‰ใฏ็ทšๅฝขไปฃๆ•ฐใฎใŸใ‚ใฎใƒ‰ใƒกใ‚คใƒณๅ›บๆœ‰ใฎใ‚ณใƒณใƒ‘ใ‚คใƒฉใงใ€TensorFlowใƒขใƒ‡ใƒซใ‚’ๆฝœๅœจ็š„ใซใ‚ฝใƒผใ‚นใ‚ณใƒผใƒ‰ใฎๅค‰ๆ›ดใชใ—ใง้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ TensorFlowใงXLAใ‚’ไฝฟ็”จใ™ใ‚‹ใฎใฏ็ฐกๅ˜ใงใ™ใ€‚XLAใฏ`tensorflow`ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใซใƒ‘ใƒƒใ‚ฑใƒผใ‚ธๅŒ–ใ•ใ‚ŒใฆใŠใ‚Šใ€[`tf.function`](https://www.tensorflow.org/guide/intro_to_graphs)ใชใฉใฎใ‚ฐใƒฉใƒ•ใ‚’ไฝœๆˆใ™ใ‚‹้–ขๆ•ฐๅ†…ใง`jit_compile`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒชใ‚ฌใƒผใงใใพใ™ใ€‚`fit()`ใ‚„`predict()`ใชใฉใฎKerasใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`model.compile()`ใซ`jit_compile`ๅผ•ๆ•ฐใ‚’ๆธกใ™ใ ใ‘ใงXLAใ‚’ๆœ‰ๅŠนใซใงใใพใ™ใ€‚ใŸใ ใ—ใ€XLAใฏใ“ใ‚Œใ‚‰ใฎใƒกใ‚ฝใƒƒใƒ‰ใซ้™ๅฎšใ•ใ‚Œใฆใ„ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ไปปๆ„ใฎ`tf.function`ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ ๐Ÿค— Transformersๅ†…ใฎใ„ใใคใ‹ใฎTensorFlowใƒกใ‚ฝใƒƒใƒ‰ใฏใ€XLAใจไบ’ๆ›ๆ€งใŒใ‚ใ‚‹ใ‚ˆใ†ใซๆ›ธใ็›ดใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€[GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2)ใ€[T5](https://huggingface.co/docs/transformers/model_doc/t5)ใ€[OPT](https://huggingface.co/docs/transformers/model_doc/opt)ใชใฉใฎใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒขใƒ‡ใƒซใ‚„ใ€[Whisper](https://huggingface.co/docs/transformers/model_doc/whisper)ใชใฉใฎ้Ÿณๅฃฐๅ‡ฆ็†ใƒขใƒ‡ใƒซใ‚‚ๅซใพใ‚Œใพใ™ใ€‚ ้€Ÿๅบฆๅ‘ไธŠใฎๅ…ทไฝ“็š„ใช้‡ใฏใƒขใƒ‡ใƒซใซ้žๅธธใซไพๅญ˜ใ—ใพใ™ใŒใ€๐Ÿค— Transformersๅ†…ใฎTensorFlowใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒขใƒ‡ใƒซใงใฏใ€็ด„100ๅ€ใฎ้€Ÿๅบฆๅ‘ไธŠใ‚’็ขบ่ชใ—ใฆใ„ใพใ™ใ€‚ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใซXLAใ‚’ไฝฟ็”จใ—ใฆๆœ€ๅคงใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ใพใŸใ€ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใจXLA็ตฑๅˆใฎใƒ‡ใ‚ถใ‚คใƒณๅ“ฒๅญฆใซใคใ„ใฆ่ฉณใ—ใๅญฆใณใŸใ„ๅ ดๅˆใฎ่ฟฝๅŠ ใƒชใ‚ฝใƒผใ‚นใธใฎใƒชใƒณใ‚ฏใ‚‚ๆไพ›ใ—ใพใ™ใ€‚ ## Running TF functions with XLA ไปฅไธ‹ใฎTensorFlowใƒขใƒ‡ใƒซใ‚’่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†๏ผš ```py import tensorflow as tf model = tf.keras.Sequential( [tf.keras.layers.Dense(10, input_shape=(10,), activation="relu"), tf.keras.layers.Dense(5, activation="softmax")] ) ``` ไธŠ่จ˜ใฎใƒขใƒ‡ใƒซใฏใ€ๆฌกๅ…ƒใŒ`(10, )`ใฎๅ…ฅๅŠ›ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใพใ™ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใงๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš ```py # Generate random inputs for the model. batch_size = 16 input_vector_dim = 10 random_inputs = tf.random.normal((batch_size, input_vector_dim)) # Run a forward pass. _ = model(random_inputs) ``` XLAใงใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚ŒใŸ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš ```py xla_fn = tf.function(model, jit_compile=True) _ = xla_fn(random_inputs) ``` `model`ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ `call()` ้–ขๆ•ฐใฏXLAใ‚ฐใƒฉใƒ•ใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€XLAใซใ‚ณใƒณใƒ‘ใ‚คใƒซใ—ใŸใ„ไป–ใฎใƒขใƒ‡ใƒซ้–ขๆ•ฐใŒใ‚ใ‚‹ๅ ดๅˆใ€ใใ‚Œใ‚‚ๅฏ่ƒฝใงใ™ใ€‚ไปฅไธ‹ใฏใใฎๆ–นๆณ•ใงใ™๏ผš ```py my_xla_fn = tf.function(model.my_xla_fn, jit_compile=True) ``` ## Running a TF text generation model with XLA from ๐Ÿค— Transformers ๐Ÿค— Transformersๅ†…ใงXLAใงใฎ้ซ˜้€ŸๅŒ–ใ•ใ‚ŒใŸ็”Ÿๆˆใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ๆœ€ๆ–ฐใƒใƒผใ‚ธใƒงใƒณใฎ`transformers`ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใพใ™๏ผš ```bash pip install transformers --upgrade ``` ๆฌกใซใ€ๆฌกใฎใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใงใใพใ™๏ผš ```py import tensorflow as tf from transformers import AutoTokenizer, TFAutoModelForCausalLM # Will error if the minimal version of Transformers is not installed. from transformers.utils import check_min_version check_min_version("4.21.0") tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="</s>") model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2") input_string = ["TensorFlow is"] # One line to create an XLA generation function xla_generate = tf.function(model.generate, jit_compile=True) tokenized_input = tokenizer(input_string, return_tensors="tf") generated_tokens = xla_generate(**tokenized_input, num_beams=2) decoded_text = tokenizer.decode(generated_tokens[0], skip_special_tokens=True) print(f"Generated -- {decoded_text}") # Generated -- TensorFlow is an open-source, open-source, distributed-source application # framework for the ``` `generate()`ใงXLAใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใฎใฏใ€ใŸใฃใŸไธ€่กŒใฎใ‚ณใƒผใƒ‰ใงใ™ใ€‚ใ‚ณใƒผใƒ‰ใฎๆฎ‹ใ‚Š้ƒจๅˆ†ใฏๅค‰ๆ›ดใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ใŸใ ใ—ใ€XLAๅ›บๆœ‰ใฎใ„ใใคใ‹ใฎๆณจๆ„็‚นใŒไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ใ‚นใƒ‹ใƒšใƒƒใƒˆใซใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใซๆณจๆ„ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€XLAใŒใ‚‚ใŸใ‚‰ใ™้€Ÿๅบฆๅ‘ไธŠใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใซใใ‚Œใ‚‰ใ‚’ๆŠŠๆกใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใ“ใ‚Œใ‚‰ใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใ€‚ ## Gotchas to be aware of XLAใ‚’ๆœ‰ๅŠนใซใ—ใŸ้–ขๆ•ฐ๏ผˆไธŠ่จ˜ใฎ`xla_generate()`ใชใฉ๏ผ‰ใ‚’ๅˆใ‚ใฆๅฎŸ่กŒใ™ใ‚‹ใจใ€ๅ†…้ƒจใง่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ใ‚’ๆŽจ่ซ–ใ—ใ‚ˆใ†ใจใ—ใพใ™ใŒใ€ใ“ใ‚Œใฏๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใพใ™ใ€‚ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใฏ["ใƒˆใƒฌใƒผใ‚ทใƒณใ‚ฐ"๏ผˆtracing๏ผ‰](https://www.tensorflow.org/guide/intro_to_graphs#when_is_a_function_tracing)ใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ ็”Ÿๆˆๆ™‚้–“ใŒ้ซ˜้€Ÿใงใฏใชใ„ใ“ใจใซๆฐ—ไป˜ใใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚`xla_generate()`๏ผˆใพใŸใฏไป–ใฎXLAๅฏพๅฟœ้–ขๆ•ฐ๏ผ‰ใฎ้€ฃ็ถšๅ‘ผใณๅ‡บใ—ใงใฏใ€้–ขๆ•ฐใธใฎๅ…ฅๅŠ›ใŒๆœ€ๅˆใซ่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ใŒๆง‹็ฏ‰ใ•ใ‚ŒใŸใจใใจๅŒใ˜ๅฝข็Šถใซๅพ“ใฃใฆใ„ใ‚‹ๅ ดๅˆใ€่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ใ‚’ๆŽจ่ซ–ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ€ๅ…ฅๅŠ›ๅฝข็ŠถใŒๅ›บๅฎšใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ€ใƒชใƒ†ใ‚ฃ๏ผˆไพ‹๏ผš็”ปๅƒ๏ผ‰ใซใฏๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅค‰ๆ•ฐใฎๅ…ฅๅŠ›ๅฝข็Šถใƒขใƒ€ใƒชใƒ†ใ‚ฃ๏ผˆไพ‹๏ผšใƒ†ใ‚ญใ‚นใƒˆ๏ผ‰ใ‚’ๆ‰ฑใ†ๅ ดๅˆใซใฏๆณจๆ„ใŒๅฟ…่ฆใงใ™ใ€‚ `xla_generate()`ใŒๅธธใซๅŒใ˜ๅ…ฅๅŠ›ๅฝข็Šถใงๅ‹•ไฝœใ™ใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใซใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ๅ‘ผใณๅ‡บใ™้š›ใซ`padding`ๅผ•ๆ•ฐใ‚’ๆŒ‡ๅฎšใงใใพใ™ใ€‚ ```py import tensorflow as tf from transformers import AutoTokenizer, TFAutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="</s>") model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2") input_string = ["TensorFlow is"] xla_generate = tf.function(model.generate, jit_compile=True) # Here, we call the tokenizer with padding options. tokenized_input = tokenizer(input_string, pad_to_multiple_of=8, padding=True, return_tensors="tf") generated_tokens = xla_generate(**tokenized_input, num_beams=2) decoded_text = tokenizer.decode(generated_tokens[0], skip_special_tokens=True) print(f"Generated -- {decoded_text}") ``` ใ“ใ‚Œใซใ‚ˆใ‚Šใ€`xla_generate()`ใธใฎๅ…ฅๅŠ›ใŒๅธธใซใƒˆใƒฌใƒผใ‚นใ•ใ‚ŒใŸๅฝข็Šถใฎๅ…ฅๅŠ›ใ‚’ๅ—ใ‘ๅ–ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€็”Ÿๆˆๆ™‚้–“ใฎ้ซ˜้€ŸๅŒ–ใ‚’ๅฎŸ็พใงใใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใงใ“ใ‚Œใ‚’็ขบ่ชใงใใพใ™๏ผš ```py import time import tensorflow as tf from transformers import AutoTokenizer, TFAutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="</s>") model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2") xla_generate = tf.function(model.generate, jit_compile=True) for input_string in ["TensorFlow is", "TensorFlow is a", "TFLite is a"]: tokenized_input = tokenizer(input_string, pad_to_multiple_of=8, padding=True, return_tensors="tf") start = time.time_ns() generated_tokens = xla_generate(**tokenized_input, num_beams=2) end = time.time_ns() print(f"Execution time -- {(end - start) / 1e6:.1f} ms\n") ``` Tesla T4 GPUใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ๆฌกใฎใ‚ˆใ†ใชๅ‡บๅŠ›ใŒๆœŸๅพ…ใ•ใ‚Œใพใ™๏ผš ```bash Execution time -- 30819.6 ms Execution time -- 79.0 ms Execution time -- 78.9 ms ``` ๆœ€ๅˆใฎ`xla_generate()`ๅ‘ผใณๅ‡บใ—ใฏใƒˆใƒฌใƒผใ‚ทใƒณใ‚ฐใฎใŸใ‚ใซๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใพใ™ใŒใ€้€ฃ็ถšใ™ใ‚‹ๅ‘ผใณๅ‡บใ—ใฏๆก้•ใ„ใซ้ซ˜้€Ÿใงใ™ใ€‚็”Ÿๆˆใ‚ชใƒ—ใ‚ทใƒงใƒณใฎใ„ใ‹ใชใ‚‹ๅค‰ๆ›ดใ‚‚ใ€ๅ†ใƒˆใƒฌใƒผใ‚ทใƒณใ‚ฐใ‚’ๅผ•ใ่ตทใ“ใ—ใ€็”Ÿๆˆๆ™‚้–“ใฎ้…ๅปถใ‚’ๅผ•ใ่ตทใ“ใ™ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ€๐Ÿค— TransformersใŒๆไพ›ใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ใ™ในใฆ็ถฒ็พ…ใ—ใฆใ„ใพใ›ใ‚“ใ€‚้ซ˜ๅบฆใชใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซใคใ„ใฆใฏใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ ## Additional Resources ใ“ใ“ใงใฏใ€๐Ÿค— Transformersใจไธ€่ˆฌ็š„ใชXLAใซใคใ„ใฆใ•ใ‚‰ใซ่ฉณใ—ใๅญฆใณใŸใ„ๅ ดๅˆใฎใ„ใใคใ‹ใฎ่ฟฝๅŠ ใƒชใ‚ฝใƒผใ‚นใ‚’ๆไพ›ใ—ใพใ™ใ€‚ * [ใ“ใฎColab Notebook](https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/91_tf_xla_generate.ipynb)ใงใฏใ€XLAๅฏพๅฟœใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผ๏ผˆ[T5](https://huggingface.co/docs/transformers/model_doc/t5)ใชใฉ๏ผ‰ใŠใ‚ˆใณใƒ‡ใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จ๏ผˆ[GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2)ใชใฉ๏ผ‰ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ใŸใ‚ใฎๅฏพ่ฉฑๅž‹ใƒ‡ใƒขใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ * [ใ“ใฎใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://huggingface.co/blog/tf-xla-generate)ใงใฏใ€XLAๅฏพๅฟœใƒขใƒ‡ใƒซใฎๆฏ”่ผƒใƒ™ใƒณใƒใƒžใƒผใ‚ฏใฎๆฆ‚่ฆใจใ€TensorFlowใงใฎXLAใซใคใ„ใฆใฎๅ‹ๅฅฝ็š„ใช็ดนไป‹ใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ * [ใ“ใฎใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://blog.tensorflow.org/2022/11/how-hugging-face-improved-text-generation-performance-with-xla.html)ใงใฏใ€๐Ÿค— TransformersใฎTensorFlowใƒขใƒ‡ใƒซใซXLAใ‚ตใƒใƒผใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹้š›ใฎ่จญ่จˆๅ“ฒๅญฆใซใคใ„ใฆ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ * ไธ€่ˆฌ็š„ใชXLAใจTensorFlowใ‚ฐใƒฉใƒ•ใซใคใ„ใฆ่ฉณใ—ใๅญฆใถใŸใ‚ใฎใŠใ™ใ™ใ‚ใฎๆŠ•็จฟ๏ผš * [XLA: ๆฉŸๆขฐๅญฆ็ฟ’็”จใฎๆœ€้ฉๅŒ–ใ‚ณใƒณใƒ‘ใ‚คใƒฉ](https://www.tensorflow.org/xla) * [ใ‚ฐใƒฉใƒ•ใจ`tf.function`ใฎ็ดนไป‹](https://www.tensorflow.org/guide/intro_to_graphs) * [`tf.function`ใ‚’ไฝฟ็”จใ—ใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๅ‘ไธŠ](https://www.tensorflow.org/guide/function)
transformers/docs/source/ja/tf_xla.md/0
{ "file_path": "transformers/docs/source/ja/tf_xla.md", "repo_id": "transformers", "token_count": 4413 }
298
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # ์ฑ„ํŒ… ๋ชจ๋ธ์„ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ[[templates-for-chat-models]] ## ์†Œ๊ฐœ[[introduction]] ์š”์ฆ˜ LLM์˜ ๊ฐ€์žฅ ํ”ํ•œ ํ™œ์šฉ ์‚ฌ๋ก€ ์ค‘ ํ•˜๋‚˜๋Š” **์ฑ„ํŒ…**์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ…์€ ์ผ๋ฐ˜์ ์ธ ์–ธ์–ด ๋ชจ๋ธ์ฒ˜๋Ÿผ ๋‹จ์ผ ๋ฌธ์ž์—ด์„ ์ด์–ด๊ฐ€๋Š” ๋Œ€์‹  ์—ฌ๋Ÿฌ ๊ฐœ์˜ **๋ฉ”์‹œ์ง€**๋กœ ๊ตฌ์„ฑ๋œ ๋Œ€ํ™”๋ฅผ ์ด์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด ๋Œ€ํ™”์—๋Š” "์‚ฌ์šฉ์ž"๋‚˜ "์–ด์‹œ์Šคํ„ดํŠธ"์™€ ๊ฐ™์€ **์—ญํ• **๊ณผ ๋ฉ”์‹œ์ง€ ํ…์ŠคํŠธ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ํ† ํฐํ™”์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์€ ์ฑ„ํŒ…์— ๋Œ€ํ•ด ๋งค์šฐ ๋‹ค๋ฅธ ์ž…๋ ฅ ํ˜•์‹์„ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์šฐ๋ฆฌ๊ฐ€ **์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ**์„ ๊ธฐ๋Šฅ์œผ๋กœ ์ถ”๊ฐ€ํ•œ ์ด์œ ์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ํ† ํฌ๋‚˜์ด์ €์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ๋Œ€ํ™” ๋ชฉ๋ก์„ ๋ชจ๋ธ์ด ๊ธฐ๋Œ€ํ•˜๋Š” ํ˜•์‹์ธ '๋‹จ์ผ ํ† ํฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด'๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. `BlenderBot` ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ด๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BlenderBot์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ํ…œํ”Œ๋ฆฟ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ฃผ๋กœ ๋Œ€ํ™” ๋ผ์šด๋“œ ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: ```python >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") >>> chat = [ ... {"role": "user", "content": "Hello, how are you?"}, ... {"role": "assistant", "content": "I'm doing great. How can I help you today?"}, ... {"role": "user", "content": "I'd like to show off how chat templating works!"}, ... ] >>> tokenizer.apply_chat_template(chat, tokenize=False) " Hello, how are you? I'm doing great. How can I help you today? I'd like to show off how chat templating works!</s>" ``` ์ „์ฒด ์ฑ„ํŒ…์ด ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์••์ถ•๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ •์ธ `tokenize=True`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ ๋ฌธ์ž์—ด๋„ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ๋” ๋ณต์žกํ•œ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด `mistralai/Mistral-7B-Instruct-v0.1` ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ```python >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") >>> chat = [ ... {"role": "user", "content": "Hello, how are you?"}, ... {"role": "assistant", "content": "I'm doing great. How can I help you today?"}, ... {"role": "user", "content": "I'd like to show off how chat templating works!"}, ... ] >>> tokenizer.apply_chat_template(chat, tokenize=False) "<s>[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today?</s> [INST] I'd like to show off how chat templating works! [/INST]" ``` ์ด๋ฒˆ์—๋Š” ํ† ํฌ๋‚˜์ด์ €๊ฐ€ [INST]์™€ [/INST] ์ œ์–ด ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€์˜ ์‹œ์ž‘๊ณผ ๋์„ ํ‘œ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค(์–ด์‹œ์Šคํ„ดํŠธ ๋ฉ”์‹œ์ง€ ์ œ์™ธ). Mistral-instruct๋Š” ์ด๋Ÿฌํ•œ ํ† ํฐ์œผ๋กœ ํ›ˆ๋ จ๋˜์—ˆ์ง€๋งŒ, BlenderBot์€ ๊ทธ๋ ‡์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ## ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋‚˜์š”?[[how-do-i-use-chat-templates]] ์œ„์˜ ์˜ˆ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. `role`๊ณผ `content` ํ‚ค๊ฐ€ ํฌํ•จ๋œ ๋ฉ”์‹œ์ง€ ๋ชฉ๋ก์„ ์ž‘์„ฑํ•œ ๋‹ค์Œ, [`~PreTrainedTokenizer.apply_chat_template`] ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ถœ๋ ฅ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค! ๋ชจ๋ธ ์ƒ์„ฑ์˜ ์ž…๋ ฅ์œผ๋กœ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•  ๋•Œ, `add_generation_prompt=True`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ](#what-are-generation-prompts)๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ `Zephyr` ์–ด์‹œ์Šคํ„ดํŠธ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ `model.generate()`์˜ ์ž…๋ ฅ์„ ์ค€๋น„ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค: ```python from transformers import AutoModelForCausalLM, AutoTokenizer checkpoint = "HuggingFaceH4/zephyr-7b-beta" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForCausalLM.from_pretrained(checkpoint) # ์—ฌ๊ธฐ์„œ bfloat16 ์‚ฌ์šฉ ๋ฐ/๋˜๋Š” GPU๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. messages = [ { "role": "system", "content": "You are a friendly chatbot who always responds in the style of a pirate", }, {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, ] tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt") print(tokenizer.decode(tokenized_chat[0])) ``` ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Zephyr๊ฐ€ ๊ธฐ๋Œ€ํ•˜๋Š” ์ž…๋ ฅ ํ˜•์‹์˜ ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ```text <|system|> You are a friendly chatbot who always responds in the style of a pirate</s> <|user|> How many helicopters can a human eat in one sitting?</s> <|assistant|> ``` ์ด์ œ ์ž…๋ ฅ์ด Zephyr์— ๋งž๊ฒŒ ํ˜•์‹์ด ์ง€์ •๋˜์—ˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ```python outputs = model.generate(tokenized_chat, max_new_tokens=128) print(tokenizer.decode(outputs[0])) ``` ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค: ```text <|system|> You are a friendly chatbot who always responds in the style of a pirate</s> <|user|> How many helicopters can a human eat in one sitting?</s> <|assistant|> Matey, I'm afraid I must inform ye that humans cannot eat helicopters. Helicopters are not food, they are flying machines. Food is meant to be eaten, like a hearty plate o' grog, a savory bowl o' stew, or a delicious loaf o' bread. But helicopters, they be for transportin' and movin' around, not for eatin'. So, I'd say none, me hearties. None at all. ``` ์ด์ œ ์‰ฌ์›Œ์กŒ์ฃ ! ## ์ฑ„ํŒ…์„ ์œ„ํ•œ ์ž๋™ํ™”๋œ ํŒŒ์ดํ”„๋ผ์ธ์ด ์žˆ๋‚˜์š”?[[is-there-an-automated-pipeline-for-chat]] ๋„ค, ์žˆ์Šต๋‹ˆ๋‹ค! ์šฐ๋ฆฌ์˜ ํ…์ŠคํŠธ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ์€ ์ฑ„ํŒ… ์ž…๋ ฅ์„ ์ง€์›ํ•˜์—ฌ ์ฑ„ํŒ… ๋ชจ๋ธ์„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์—๋Š” "ConversationalPipeline" ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, ์ด์ œ๋Š” ์ด ๊ธฐ๋Šฅ์ด [`TextGenerationPipeline`]์— ํ†ตํ•ฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ `Zephyr` ์˜ˆ์ œ๋ฅผ ๋‹ค์‹œ ์‹œ๋„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ```python from transformers import pipeline pipe = pipeline("text-generation", "HuggingFaceH4/zephyr-7b-beta") messages = [ { "role": "system", "content": "You are a friendly chatbot who always responds in the style of a pirate", }, {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, ] print(pipe(messages, max_new_tokens=128)[0]['generated_text'][-1]) # ์–ด์‹œ์Šคํ„ดํŠธ์˜ ์‘๋‹ต์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ``` ```text {'role': 'assistant', 'content': "Matey, I'm afraid I must inform ye that humans cannot eat helicopters. Helicopters are not food, they are flying machines. Food is meant to be eaten, like a hearty plate o' grog, a savory bowl o' stew, or a delicious loaf o' bread. But helicopters, they be for transportin' and movin' around, not for eatin'. So, I'd say none, me hearties. None at all."} ``` ํŒŒ์ดํ”„๋ผ์ธ์€ ํ† ํฐํ™”์™€ `apply_chat_template` ํ˜ธ์ถœ ์˜ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋ธ์— ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์ด ์žˆ์œผ๋ฉด ํŒŒ์ดํ”„๋ผ์ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ๋ฉ”์‹œ์ง€ ๋ชฉ๋ก์„ ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค! ## "์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ"๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?[[what-are-generation-prompts]] `apply_chat_template` ๋ฉ”์„œ๋“œ์—๋Š” `add_generation_prompt` ์ธ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ˜์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ธ์ˆ˜๋Š” ํ…œํ”Œ๋ฆฟ์— ๋ด‡ ์‘๋‹ต์˜ ์‹œ์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฑ„ํŒ…์„ ๊ณ ๋ คํ•ด ๋ณด์„ธ์š”: ```python messages = [ {"role": "user", "content": "Hi there!"}, {"role": "assistant", "content": "Nice to meet you!"}, {"role": "user", "content": "Can I ask a question?"} ] ``` Zephyr ์˜ˆ์ œ์—์„œ ๋ณด์•˜๋˜ ๊ฒƒ๊ณผ ๊ฐ™์ด, ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ ์—†์ด ChatML ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค: ```python tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False) """<|im_start|>user Hi there!<|im_end|> <|im_start|>assistant Nice to meet you!<|im_end|> <|im_start|>user Can I ask a question?<|im_end|> """ ``` ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๊ฐ€ **์žˆ๋Š”** ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ```python tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) """<|im_start|>user Hi there!<|im_end|> <|im_start|>assistant Nice to meet you!<|im_end|> <|im_start|>user Can I ask a question?<|im_end|> <|im_start|>assistant """ ``` ์ด๋ฒˆ์—๋Š” ๋ด‡ ์‘๋‹ต์˜ ์‹œ์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ† ํฐ์„ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์„ ์ฃผ๋ชฉํ•˜์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ์ด ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ์ž์˜ ๋ฉ”์‹œ์ง€๋ฅผ ๊ณ„์†ํ•˜๋Š” ๋Œ€์‹  ๋ด‡ ์‘๋‹ต์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์–ตํ•˜์„ธ์š”, ์ฑ„ํŒ… ๋ชจ๋ธ์€ ์—ฌ์ „ํžˆ ์–ธ์–ด ๋ชจ๋ธ์ผ ๋ฟ์ด๋ฉฐ, ๊ทธ๋“ค์—๊ฒŒ ์ฑ„ํŒ…์€ ํŠน๋ณ„ํ•œ ์ข…๋ฅ˜์˜ ํ…์ŠคํŠธ์ผ ๋ฟ์ž…๋‹ˆ๋‹ค! ์ ์ ˆํ•œ ์ œ์–ด ํ† ํฐ์œผ๋กœ ์•ˆ๋‚ดํ•ด์•ผ ์ฑ„ํŒ… ๋ชจ๋ธ์ด ๋ฌด์—‡์„ ํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ชจ๋ธ์ด ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. BlenderBot๊ณผ LLaMA ๊ฐ™์€ ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋ด‡ ์‘๋‹ต ์ „์— ํŠน๋ณ„ํ•œ ํ† ํฐ์ด ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ `add_generation_prompt` ์ธ์ˆ˜๋Š” ํšจ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. `add_generation_prompt`์˜ ์ •ํ™•ํ•œ ํšจ๊ณผ๋Š” ์‚ฌ์šฉ ์ค‘์ธ ํ…œํ”Œ๋ฆฟ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ## ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‚˜์š”?[[can-i-use-chat-templates-in-training]] ๋„ค! ์ด ๋ฐฉ๋ฒ•์€ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ๋ชจ๋ธ์ด ํ›ˆ๋ จ ์ค‘์— ๋ณด๋Š” ํ† ํฐ๊ณผ ์ผ์น˜ํ•˜๋„๋ก ํ•˜๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋กœ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„์—๋Š” ๋‹ค๋ฅธ ์–ธ์–ด ๋ชจ๋ธ ํ›ˆ๋ จ ์ž‘์—…๊ณผ ๊ฐ™์ด ๊ณ„์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จํ•  ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ `add_generation_prompt=False`๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด์‹œ์Šคํ„ดํŠธ ์‘๋‹ต์„ ํ”„๋กฌํ”„ํŠธํ•˜๋Š” ์ถ”๊ฐ€ ํ† ํฐ์€ ํ›ˆ๋ จ ์ค‘์—๋Š” ๋„์›€์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ```python from transformers import AutoTokenizer from datasets import Dataset tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta") chat1 = [ {"role": "user", "content": "Which is bigger, the moon or the sun?"}, {"role": "assistant", "content": "The sun."} ] chat2 = [ {"role": "user", "content": "Which is bigger, a virus or a bacterium?"}, {"role": "assistant", "content": "A bacterium."} ] dataset = Dataset.from_dict({"chat": [chat1, chat2]}) dataset = dataset.map(lambda x: {"formatted_chat": tokenizer.apply_chat_template(x["chat"], tokenize=False, add_generation_prompt=False)}) print(dataset['formatted_chat'][0]) ``` ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ```text <|user|> Which is bigger, the moon or the sun?</s> <|assistant|> The sun.</s> ``` ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์ผ๋ฐ˜์ ์ธ ์–ธ์–ด ๋ชจ๋ธ ์ž‘์—…๊ณผ ๊ฐ™์ด `formatted_chat` ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ์„ ๊ณ„์†ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. <Tip> `apply_chat_template(tokenize=False)`๋กœ ํ…์ŠคํŠธ๋ฅผ ํ˜•์‹ํ™”ํ•œ ๋‹ค์Œ ๋ณ„๋„์˜ ๋‹จ๊ณ„์—์„œ ํ† ํฐํ™”ํ•˜๋Š” ๊ฒฝ์šฐ, `add_special_tokens=False` ์ธ์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `apply_chat_template(tokenize=True)`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ด ๋ฌธ์ œ๋ฅผ ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค! ๊ธฐ๋ณธ์ ์œผ๋กœ ์ผ๋ถ€ ํ† ํฌ๋‚˜์ด์ €๋Š” ํ† ํฐํ™”ํ•  ๋•Œ `<bos>` ๋ฐ `<eos>`์™€ ๊ฐ™์€ ํŠน๋ณ„ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ํ•ญ์ƒ ํ•„์š”ํ•œ ๋ชจ๋“  ํŠน๋ณ„ ํ† ํฐ์„ ํฌํ•จํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๊ธฐ๋ณธ `add_special_tokens=True`๋กœ ์ถ”๊ฐ€์ ์ธ ํŠน๋ณ„ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์ž˜๋ชป๋˜๊ฑฐ๋‚˜ ์ค‘๋ณต๋˜๋Š” ํŠน๋ณ„ ํ† ํฐ์„ ์ƒ์„ฑํ•˜์—ฌ ๋ชจ๋ธ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. </Tip> ## ๊ณ ๊ธ‰: ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์— ์ถ”๊ฐ€ ์ž…๋ ฅ ์‚ฌ์šฉ[[advanced-extra-inputs-to-chat-templates]] `apply_chat_template`๊ฐ€ ํ•„์š”ํ•œ ์œ ์ผํ•œ ์ธ์ˆ˜๋Š” `messages`์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ `apply_chat_template`์— ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ํ…œํ”Œ๋ฆฟ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž์œ ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ธ์ˆ˜์˜ ์ด๋ฆ„์ด๋‚˜ ํ˜•์‹์—๋Š” ์ œํ•œ์ด ์—†์–ด ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธด ํ•˜์ง€๋งŒ, ์ด๋Ÿฌํ•œ ์ถ”๊ฐ€ ์ธ์ˆ˜์˜ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€๋กœ 'ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์œ„ํ•œ ๋„๊ตฌ'๋‚˜ '๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ฌธ์„œ'๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ธ์ˆ˜์˜ ์ด๋ฆ„๊ณผ ํ˜•์‹์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ๊ถŒ์žฅ ์‚ฌํ•ญ์ด ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์•„๋ž˜ ์„น์…˜์— ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ ์ž‘์„ฑ์ž์—๊ฒŒ ๋„๊ตฌ ํ˜ธ์ถœ ์ฝ”๋“œ๋ฅผ ๋ชจ๋ธ ๊ฐ„์— ์‰ฝ๊ฒŒ ์ „์†กํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ด ํ˜•์‹๊ณผ ํ˜ธํ™˜๋˜๋„๋ก ๋งŒ๋“ค ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ## ๊ณ ๊ธ‰: ๋„๊ตฌ ์‚ฌ์šฉ / ํ•จ์ˆ˜ ํ˜ธ์ถœ[[advanced-tool-use--function-calling]] "๋„๊ตฌ ์‚ฌ์šฉ" LLM์€ ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜๊ธฐ ์ „์— ์™ธ๋ถ€ ๋„๊ตฌ๋กœ์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์— ๋„๊ตฌ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” ๋‹จ์ˆœํžˆ ํ•จ์ˆ˜ ๋ชฉ๋ก์„ `tools` ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ```python import datetime def current_time(): """ํ˜„์žฌ ํ˜„์ง€ ์‹œ๊ฐ„์„ ๋ฌธ์ž์—ด๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.""" return str(datetime.now()) def multiply(a: float, b: float): """ ๋‘ ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๋Š” ํ•จ์ˆ˜ ์ธ์ˆ˜: a: ๊ณฑํ•  ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž b: ๊ณฑํ•  ๋‘ ๋ฒˆ์งธ ์ˆซ์ž """ return a * b tools = [current_time, multiply] model_input = tokenizer.apply_chat_template( messages, tools=tools ) ``` ์ด๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋ ค๋ฉด ํ•จ์ˆ˜๋ฅผ ์œ„ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ๋„๊ตฌ๋กœ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ๋ฌธ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ ๋‹ค์Œ ๊ทœ์น™์„ ๋”ฐ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค: - ํ•จ์ˆ˜๋Š” ์„ค๋ช…์ ์ธ ์ด๋ฆ„์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. - ๋ชจ๋“  ์ธ์ˆ˜์—๋Š” ํƒ€์ž… ํžŒํŠธ๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - ํ•จ์ˆ˜์—๋Š” ํ‘œ์ค€ Google ์Šคํƒ€์ผ์˜ ๋„ํฌ์ŠคํŠธ๋ง์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(์ฆ‰, ์ดˆ๊ธฐ ํ•จ์ˆ˜ ์„ค๋ช… ๋‹ค์Œ์— ์ธ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š” `Args:` ๋ธ”๋ก์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). - `Args:` ๋ธ”๋ก์—๋Š” ํƒ€์ž…์„ ํฌํ•จํ•˜์ง€ ๋งˆ์„ธ์š”. ์ฆ‰, `a (int): The first number to multiply` ๋Œ€์‹  `a: The first number to multiply`๋ผ๊ณ  ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ž… ํžŒํŠธ๋Š” ํ•จ์ˆ˜ ํ—ค๋”์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - ํ•จ์ˆ˜์—๋Š” ๋ฐ˜ํ™˜ ํƒ€์ž…๊ณผ ๋„ํฌ์ŠคํŠธ๋ง์— `Returns:` ๋ธ”๋ก์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๋ถ€๋ถ„์˜ ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์€ ์ด๋ฅผ ๋ฌด์‹œํ•˜๋ฏ€๋กœ ์ด๋Š” ์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ### ๋„๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๊ธฐ[[passing-tool-results-to-the-model]] ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋ชจ๋ธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ๋ฅผ ๋‚˜์—ดํ•˜๋Š” ๋ฐ ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: 1. ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์„ ํŒŒ์‹ฑํ•˜์—ฌ ๋„๊ตฌ ์ด๋ฆ„๊ณผ ์ธ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 2. ๋ชจ๋ธ์˜ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 3. ํ•ด๋‹น ์ธ์ˆ˜์— ๋Œ€์‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. 4. ๊ฒฐ๊ณผ๋ฅผ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ### ๋„๊ตฌ ์‚ฌ์šฉ ์˜ˆ์ œ[[a-complete-tool-use-example]] ๋„๊ตฌ ์‚ฌ์šฉ ์˜ˆ์ œ๋ฅผ ๋‹จ๊ณ„๋ณ„๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ ์ค‘์—์„œ ์„ฑ๋Šฅ์ด ๊ฐ€์žฅ ์šฐ์ˆ˜ํ•œ 8B `Hermes-2-Pro` ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด, ๋” ํฐ ๋ชจ๋ธ์ธ [Command-R](https://huggingface.co/CohereForAI/c4ai-command-r-v01) ๋˜๋Š” [Mixtral-8x22B](https://huggingface.co/mistralai/Mixtral-8x22B-Instruct-v0.1)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๋ชจ๋ธ ๋ชจ๋‘ ๋„๊ตฌ ์‚ฌ์šฉ์„ ์ง€์›ํ•˜๋ฉฐ ๋” ๊ฐ•๋ ฅํ•œ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer checkpoint = "NousResearch/Hermes-2-Pro-Llama-3-8B" tokenizer = AutoTokenizer.from_pretrained(checkpoint, revision="pr/13") model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto") ``` ๋‹ค์Œ์œผ๋กœ, ๋„๊ตฌ ๋ชฉ๋ก์„ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ```python def get_current_temperature(location: str, unit: str) -> float: """ ํŠน์ • ์œ„์น˜์˜ ํ˜„์žฌ ์˜จ๋„๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ธ์ˆ˜: ์œ„์น˜: ์˜จ๋„๋ฅผ ๊ฐ€์ ธ์˜ฌ ์œ„์น˜, "๋„์‹œ, ๊ตญ๊ฐ€" ํ˜•์‹ ๋‹จ์œ„: ์˜จ๋„ ๋‹จ์œ„ (์„ ํƒ์ง€: ["celsius", "fahrenheit"]) ๋ฐ˜ํ™˜๊ฐ’: ์ง€์ •๋œ ์œ„์น˜์˜ ํ˜„์žฌ ์˜จ๋„๋ฅผ ์ง€์ •๋œ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜, float ํ˜•์‹. """ return 22. # ์ด ํ•จ์ˆ˜๋Š” ์‹ค์ œ๋กœ ์˜จ๋„๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! def get_current_wind_speed(location: str) -> float: """ ์ฃผ์–ด์ง„ ์œ„์น˜์˜ ํ˜„์žฌ ํ’์†์„ km/h ๋‹จ์œ„๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ธ์ˆ˜: ์œ„์น˜(location): ํ’์†์„ ๊ฐ€์ ธ์˜ฌ ์œ„์น˜, "๋„์‹œ, ๊ตญ๊ฐ€" ํ˜•์‹ ๋ฐ˜ํ™˜๊ฐ’: ์ฃผ์–ด์ง„ ์œ„์น˜์˜ ํ˜„์žฌ ํ’์†์„ km/h ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜, float ํ˜•์‹. """ return 6. # ์ด ํ•จ์ˆ˜๋Š” ์‹ค์ œ๋กœ ํ’์†์„ ๊ฐ€์ ธ์™€์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! tools = [get_current_temperature, get_current_wind_speed] ``` ์ด์ œ ๋ด‡์„ ์œ„ํ•œ ๋Œ€ํ™”๋ฅผ ์„ค์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ```python messages = [ {"role": "system", "content": "You are a bot that responds to weather queries. You should reply with the unit used in the queried location."}, {"role": "user", "content": "Hey, what's the temperature in Paris right now?"} ] ``` ์ด์ œ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ ์šฉํ•˜๊ณ  ์‘๋‹ต์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ```python inputs = tokenizer.apply_chat_template(messages, chat_template="tool_use", tools=tools, add_generation_prompt=True, return_dict=True, return_tensors="pt") inputs = {k: v.to(model.device) for k, v in inputs.items()} out = model.generate(**inputs, max_new_tokens=128) print(tokenizer.decode(out[0][len(inputs["input_ids"][0]):])) ``` ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ```text <tool_call> {"arguments": {"location": "Paris, France", "unit": "celsius"}, "name": "get_current_temperature"} </tool_call><|im_end|> ``` ๋ชจ๋ธ์ด ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์œ ํšจํ•œ ์ธ์ˆ˜๋กœ ์ˆ˜ํ–‰ํ–ˆ์œผ๋ฉฐ, ํ•จ์ˆ˜ ๋„ํฌ์ŠคํŠธ๋ง์— ์š”์ฒญ๋œ ํ˜•์‹์œผ๋กœ ํ˜ธ์ถœํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์šฐ๋ฆฌ๊ฐ€ ํ”„๋ž‘์Šค์˜ ํŒŒ๋ฆฌ๋ฅผ ์ง€์นญํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ถ”๋ก ํ–ˆ๊ณ , ํ”„๋ž‘์Šค๊ฐ€ SI ๋‹จ์œ„์˜ ๋ณธ๊ณ ์žฅ์ž„์„ ๊ธฐ์–ตํ•˜์—ฌ ์˜จ๋„๋ฅผ ์„ญ์”จ๋กœ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ž„์˜์˜ `tool_call_id`๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ID๋Š” ๋ชจ๋“  ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์—ฌ๋Ÿฌ ๋„๊ตฌ ํ˜ธ์ถœ์„ ํ•œ ๋ฒˆ์— ๋ฐœํ–‰ํ•˜๊ณ  ๊ฐ ์‘๋‹ต์ด ์–ด๋А ํ˜ธ์ถœ์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์ด ID๋Š” ๋Œ€ํ™” ๋‚ด์—์„œ ๊ณ ์œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ```python tool_call_id = "vAHdf3" # ์ž„์˜์˜ ID, ๊ฐ ๋„๊ตฌ ํ˜ธ์ถœ๋งˆ๋‹ค ๊ณ ์œ ํ•ด์•ผ ํ•จ tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France", "unit": "celsius"}} messages.append({"role": "assistant", "tool_calls": [{"id": tool_call_id, "type": "function", "function": tool_call}]}) ``` ์ด์ œ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ–ˆ์œผ๋ฏ€๋กœ, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ํ•ญ์ƒ 22.0์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋”๋ฏธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๊ฒฐ๊ณผ๋ฅผ ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ•œ ๋ฒˆ, `tool_call_id`๋Š” ๋„๊ตฌ ํ˜ธ์ถœ์— ์‚ฌ์šฉํ–ˆ๋˜ ID์™€ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ```python messages.append({"role": "tool", "tool_call_id": tool_call_id, "name": "get_current_temperature", "content": "22.0"}) ``` ๋งˆ์ง€๋ง‰์œผ๋กœ, ์–ด์‹œ์Šคํ„ดํŠธ๊ฐ€ ํ•จ์ˆ˜ ์ถœ๋ ฅ์„ ์ฝ๊ณ  ์‚ฌ์šฉ์ž์™€ ๊ณ„์† ๋Œ€ํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: ```python inputs = tokenizer.apply_chat_template(messages, chat_template="tool_use", tools=tools, add_generation_prompt=True, return_dict=True, return_tensors="pt") inputs = {k: v.to(model.device) for k, v in inputs.items()} out = model.generate(**inputs, max_new_tokens=128) print(tokenizer.decode(out[0][len(inputs["input_ids"][0]):])) ``` ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ```text The current temperature in Paris, France is 22.0 ยฐ Celsius.<|im_end|> ``` ์ด๊ฒƒ์€ ๋”๋ฏธ ๋„๊ตฌ์™€ ๋‹จ์ผ ํ˜ธ์ถœ์„ ์‚ฌ์šฉํ•œ ๊ฐ„๋‹จํ•œ ๋ฐ๋ชจ์˜€์ง€๋งŒ, ๋™์ผํ•œ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์‹ค์ œ ๋„๊ตฌ์™€ ๋” ๊ธด ๋Œ€ํ™”๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์‹ค์‹œ๊ฐ„ ์ •๋ณด, ๊ณ„์‚ฐ ๋„๊ตฌ ๋˜๋Š” ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜์—ฌ ๋Œ€ํ™”ํ˜• ์—์ด์ „ํŠธ์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <Tip> ์œ„์—์„œ ๋ณด์—ฌ์ค€ ๋„๊ตฌ ํ˜ธ์ถœ ๊ธฐ๋Šฅ์€ ๋ชจ๋“  ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋„๊ตฌ ํ˜ธ์ถœ ID๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ผ๋ถ€๋Š” ํ•จ์ˆ˜ ์ด๋ฆ„๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ์™€ ๋„๊ตฌ ํ˜ธ์ถœ์„ ์ˆœ์„œ์— ๋”ฐ๋ผ ๋งค์นญํ•˜๋ฉฐ, ํ˜ผ๋™์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋„๊ตฌ ํ˜ธ์ถœ๋งŒ ๋ฐœํ–‰ํ•˜๋Š” ๋ชจ๋ธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋งŽ์€ ๋ชจ๋ธ๊ณผ ํ˜ธํ™˜๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์›ํ•œ๋‹ค๋ฉด, ์—ฌ๊ธฐ์— ๋ณด์—ฌ์ค€ ๊ฒƒ์ฒ˜๋Ÿผ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๊ตฌ์„ฑํ•˜๊ณ , ๋ชจ๋ธ์ด ๋ฐœํ–‰ํ•œ ์ˆœ์„œ๋Œ€๋กœ ๋„๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ชจ๋ธ์˜ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์ด ๋‚˜๋จธ์ง€ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. </Tip> ### ๋„๊ตฌ ์Šคํ‚ค๋งˆ ์ดํ•ดํ•˜๊ธฐ[[understanding-tool-schemas]] `apply_chat_template`์˜ `tools` ์ธ์ˆ˜์— ์ „๋‹ฌํ•˜๋Š” ๊ฐ ํ•จ์ˆ˜๋Š” [JSON ์Šคํ‚ค๋งˆ](https://json-schema.org/learn/getting-started-step-by-step)๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์Šคํ‚ค๋งˆ๋Š” ๋ชจ๋ธ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์€ ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ง์ ‘ ๋ณด์ง€ ์•Š์œผ๋ฉฐ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ๋ณด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์ด ๊ด€์‹ฌ์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ **์ •์˜**์™€ **์ธ์ˆ˜**์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์„ ํ•˜๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๊ด€์‹ฌ์ด ์žˆ์„ ๋ฟ, ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์„ ์ฝ๊ณ  ๋ชจ๋ธ์ด ๋„๊ตฌ ์‚ฌ์šฉ์„ ์š”์ฒญํ–ˆ๋Š”์ง€ ๊ฐ์ง€ํ•˜์—ฌ, ์ธ์ˆ˜๋ฅผ ๋„๊ตฌ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ณ  ์ฑ„ํŒ…์—์„œ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ชซ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทœ๊ฒฉ์„ ๋”ฐ๋ฅธ๋‹ค๋ฉด, ํ…œํ”Œ๋ฆฟ์— ์ „๋‹ฌํ•  JSON ์Šคํ‚ค๋งˆ ์ƒ์„ฑ์„ ์ž๋™ํ™”ํ•˜๊ณ  ๋ณด์ด์ง€ ์•Š๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ๋ณ€ํ™˜์„ ๋” ์ œ์–ดํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ˆ˜๋™์œผ๋กœ ๋ณ€ํ™˜์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ˆ˜๋™ ์Šคํ‚ค๋งˆ ๋ณ€ํ™˜ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ```python from transformers.utils import get_json_schema def multiply(a: float, b: float): """ ๋‘ ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๋Š” ํ•จ์ˆ˜ ์ธ์ˆ˜: a: ๊ณฑํ•  ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž b: ๊ณฑํ•  ๋‘ ๋ฒˆ์งธ ์ˆซ์ž """ return a * b schema = get_json_schema(multiply) print(schema) ``` ์ด ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ```json { "type": "function", "function": { "name": "multiply", "description": "A function that multiplies two numbers", "parameters": { "type": "object", "properties": { "a": { "type": "number", "description": "The first number to multiply" }, "b": { "type": "number", "description": "The second number to multiply" } }, "required": ["a", "b"] } } } ``` ์›ํ•œ๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ์Šคํ‚ค๋งˆ๋ฅผ ํŽธ์ง‘ํ•˜๊ฑฐ๋‚˜ `get_json_schema`๋ฅผ ์ „ํ˜€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. JSON ์Šคํ‚ค๋งˆ๋Š” `apply_chat_template`์˜ `tools` ์ธ์ˆ˜์— ์ง์ ‘ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋” ๋ณต์žกํ•œ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ •๋ฐ€ํ•œ ์Šคํ‚ค๋งˆ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์Šคํ‚ค๋งˆ๊ฐ€ ๋ณต์žกํ• ์ˆ˜๋ก ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ํ˜ผ๋ž€์„ ๊ฒช์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค! ๊ฐ€๋Šฅํ•œ ํ•œ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜ ์„œ๋ช…์„ ์œ ์ง€ํ•˜๊ณ , ์ธ์ˆ˜(ํŠนํžˆ ๋ณต์žกํ•˜๊ณ  ์ค‘์ฒฉ๋œ ์ธ์ˆ˜)๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ ์ง์ ‘ ์Šคํ‚ค๋งˆ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ `apply_chat_template`์— ์ „๋‹ฌํ•˜๋Š” ์˜ˆ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: ```python # ์ธ์ˆ˜๋ฅผ ๋ฐ›์ง€ ์•Š๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜ current_time = { "type": "function", "function": { "name": "current_time", "description": "Get the current local time as a string.", "parameters": { 'type': 'object', 'properties': {} } } } # ๋‘ ๊ฐœ์˜ ์ˆซ์ž ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๋” ์™„์ „ํ•œ ํ•จ์ˆ˜ multiply = { 'type': 'function', 'function': { 'name': 'multiply', 'description': 'A function that multiplies two numbers', 'parameters': { 'type': 'object', 'properties': { 'a': { 'type': 'number', 'description': 'The first number to multiply' }, 'b': { 'type': 'number', 'description': 'The second number to multiply' } }, 'required': ['a', 'b'] } } } model_input = tokenizer.apply_chat_template( messages, tools = [current_time, multiply] ) ``` ## ๊ณ ๊ธ‰: ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ[[advanced-retrieval-augmented-generation]] "๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ" ๋˜๋Š” "RAG" LLM์€ ์ฟผ๋ฆฌ์— ์‘๋‹ตํ•˜๊ธฐ ์ „์— ๋ฌธ์„œ์˜ ์ฝ”ํผ์Šค๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์€ ์ œํ•œ๋œ ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ ์ด์ƒ์œผ๋กœ ์ง€์‹ ๊ธฐ๋ฐ˜์„ ํฌ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. RAG ๋ชจ๋ธ์— ๋Œ€ํ•œ ์šฐ๋ฆฌ์˜ ๊ถŒ์žฅ ์‚ฌํ•ญ์€ ํ…œํ”Œ๋ฆฟ์ด `documents` ์ธ์ˆ˜๋ฅผ ํ—ˆ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ธ์ˆ˜๋Š” ๊ฐ "๋ฌธ์„œ"๊ฐ€ `title`๊ณผ `contents` ํ‚ค๋ฅผ ๊ฐ€์ง€๋Š” ๋‹จ์ผ dict์ธ ๋ฌธ์„œ ๋ชฉ๋ก์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜•์‹์€ ๋„๊ตฌ์— ์‚ฌ์šฉ๋˜๋Š” JSON ์Šคํ‚ค๋งˆ๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๋ฏ€๋กœ ๋ณ„๋„์˜ ๋„์šฐ๋ฏธ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ RAG ํ…œํ”Œ๋ฆฟ์ด ์ž‘๋™ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค: ```python document1 = { "title": "The Moon: Our Age-Old Foe", "contents": "Man has always dreamed of destroying the moon. In this essay, I shall..." } document2 = { "title": "The Sun: Our Age-Old Friend", "contents": "Although often underappreciated, the sun provides several notable benefits..." } model_input = tokenizer.apply_chat_template( messages, documents=[document1, document2] ) ``` ## ๊ณ ๊ธ‰: ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋‚˜์š”?[[advanced-how-do-chat-templates-work]] ๋ชจ๋ธ์˜ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ `tokenizer.chat_template` ์†์„ฑ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์ด ์„ค์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ชจ๋ธ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ์ด ๋Œ€์‹  ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. `BlenderBot`์˜ ํ…œํ”Œ๋ฆฟ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ```python >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") >>> tokenizer.chat_template "{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ ' ' }}{% endif %}{% endfor %}{{ eos_token }}" ``` ์•ฝ๊ฐ„ ๋ณต์žกํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์ถ”๊ฐ€ํ•˜๋Š” ์ค„๋ฐ”๊ฟˆ๊ณผ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ํ…œํ”Œ๋ฆฟ ์ถœ๋ ฅ์— ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” [๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋Š”](#trimming-whitespace) ํŒ์ž…๋‹ˆ๋‹ค: ``` {%- for message in messages %} {%- if message['role'] == 'user' %} {{- ' ' }} {%- endif %} {{- message['content'] }} {%- if not loop.last %} {{- ' ' }} {%- endif %} {%- endfor %} {{- eos_token }} ``` ๋งŒ์•ฝ ์ด์™€ ๊ฐ™์€ ํ˜•์‹์„ ์ฒ˜์Œ ๋ณธ๋‹ค๋ฉด, ์ด๊ฒƒ์€ [Jinja ํ…œํ”Œ๋ฆฟ](https://jinja.palletsprojects.com/en/3.1.x/templates/)์ž…๋‹ˆ๋‹ค. Jinja๋Š” ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํ…œํ”Œ๋ฆฟ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ๋งŽ์€ ๋ฉด์—์„œ ์ฝ”๋“œ์™€ ๊ตฌ๋ฌธ์ด ํŒŒ์ด์ฌ๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ˆœ์ˆ˜ ํŒŒ์ด์ฌ์—์„œ๋Š” ์ด ํ…œํ”Œ๋ฆฟ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค: ```python for idx, message in enumerate(messages): if message['role'] == 'user': print(' ') print(message['content']) if not idx == len(messages) - 1: # Check for the last message in the conversation print(' ') print(eos_token) ``` ์ด ํ…œํ”Œ๋ฆฟ์€ ์„ธ ๊ฐ€์ง€ ์ผ์„ ํ•ฉ๋‹ˆ๋‹ค: 1. ๊ฐ ๋ฉ”์‹œ์ง€์— ๋Œ€ํ•ด, ๋ฉ”์‹œ์ง€๊ฐ€ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€์ธ ๊ฒฝ์šฐ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 2. ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 3. ๋ฉ”์‹œ์ง€๊ฐ€ ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๋‘ ๊ฐœ์˜ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€ ํ›„์—๋Š” EOS ํ† ํฐ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ํ…œํ”Œ๋ฆฟ์ž…๋‹ˆ๋‹ค. ์ œ์–ด ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์ดํ›„ ๋Œ€ํ™”์—์„œ ๋ชจ๋ธ์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•ด์•ผ ํ•˜๋Š”์ง€ ์ง€์‹œํ•˜๋Š” "์‹œ์Šคํ…œ" ๋ฉ”์‹œ์ง€๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Jinja๋Š” ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค! LLaMA๊ฐ€ ์ž…๋ ฅ์„ ํ˜•์‹ํ™”ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•œ ํ˜•์‹์˜ Jinja ํ…œํ”Œ๋ฆฟ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค(์‹ค์ œ LLaMA ํ…œํ”Œ๋ฆฟ์€ ๊ธฐ๋ณธ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ์™€ ์ผ๋ฐ˜์ ์ธ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค - ์‹ค์ œ ์ฝ”๋“œ์—์„œ๋Š” ์ด ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”!). ``` {%- for message in messages %} {%- if message['role'] == 'user' %} {{- bos_token + '[INST] ' + message['content'] + ' [/INST]' }} {%- elif message['role'] == 'system' %} {{- '<<SYS>>\\n' + message['content'] + '\\n<</SYS>>\\n\\n' }} {%- elif message['role'] == 'assistant' %} {{- ' ' + message['content'] + ' ' + eos_token }} {%- endif %} {%- endfor %} ``` ์ด ํ…œํ”Œ๋ฆฟ์„ ์ž ์‹œ ์‚ดํŽด๋ณด๋ฉด ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ๊ฐ ๋ฉ”์‹œ์ง€์˜ "role"์— ๋”ฐ๋ผ ํŠน์ • ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ˆ„๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒˆ๋Š”์ง€ ๋ชจ๋ธ์—๊ฒŒ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๋˜ํ•œ ์‚ฌ์šฉ์ž, ์–ด์‹œ์Šคํ„ดํŠธ ๋ฐ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€๋Š” ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ํ† ํฐ์œผ๋กœ ๋ž˜ํ•‘๋˜์–ด ๋ชจ๋ธ์ด ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ## ๊ณ ๊ธ‰: ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ ์ถ”๊ฐ€ ๋ฐ ํŽธ์ง‘[[advanced-adding-and-editing-chat-templates]] ### ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‚˜์š”?[[how-do-i-create-a-chat-template]] ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. Jinja ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•˜๊ณ  `tokenizer.chat_template`์— ์„ค์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ชจ๋ธ์˜ ๊ธฐ์กด ํ…œํ”Œ๋ฆฟ์„ ์‹œ์ž‘์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ํ•„์š”์— ๋งž๊ฒŒ ํŽธ์ง‘ํ•˜๋Š” ๊ฒƒ์ด ๋” ์‰ฌ์šธ ๊ฒƒ ์ž…๋‹ˆ๋‹ค! ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ LLaMA ํ…œํ”Œ๋ฆฟ์„ ๊ฐ€์ ธ์™€ ์–ด์‹œ์Šคํ„ดํŠธ ๋ฉ”์‹œ์ง€์— "[ASST]" ๋ฐ "[/ASST]"๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ``` {%- for message in messages %} {%- if message['role'] == 'user' %} {{- bos_token + '[INST] ' + message['content'].strip() + ' [/INST]' }} {%- elif message['role'] == 'system' %} {{- '<<SYS>>\\n' + message['content'].strip() + '\\n<</SYS>>\\n\\n' }} {%- elif message['role'] == 'assistant' %} {{- '[ASST] ' + message['content'] + ' [/ASST]' + eos_token }} {%- endif %} {%- endfor %} ``` ์ด์ œ `tokenizer.chat_template` ์†์„ฑ์„ ์„ค์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ์— [`~PreTrainedTokenizer.apply_chat_template`]๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ƒˆ๋กญ๊ฒŒ ์„ค์ •ํ•œ ํ…œํ”Œ๋ฆฟ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค! ์ด ์†์„ฑ์€ `tokenizer_config.json` ํŒŒ์ผ์— ์ €์žฅ๋˜๋ฏ€๋กœ, [`~utils.PushToHubMixin.push_to_hub`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ํ…œํ”Œ๋ฆฟ์„ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•˜๊ณ  ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋ธ์— ๋งž๋Š” ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ```python template = tokenizer.chat_template template = template.replace("SYS", "SYSTEM") # ์‹œ์Šคํ…œ ํ† ํฐ ๋ณ€๊ฒฝ tokenizer.chat_template = template # ์ƒˆ ํ…œํ”Œ๋ฆฟ ์„ค์ • tokenizer.push_to_hub("model_name") # ์ƒˆ ํ…œํ”Œ๋ฆฟ์„ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ! ``` ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” [`~PreTrainedTokenizer.apply_chat_template`] ๋ฉ”์†Œ๋“œ๋Š” [`TextGenerationPipeline`] ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ, ์˜ฌ๋ฐ”๋ฅธ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์„ค์ •ํ•˜๋ฉด ๋ชจ๋ธ์ด ์ž๋™์œผ๋กœ [`TextGenerationPipeline`]๊ณผ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค. <Tip> ๋ชจ๋ธ์„ ์ฑ„ํŒ… ์šฉ๋„๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ, ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ ์ƒˆ ์ฑ„ํŒ… ์ œ์–ด ํ† ํฐ์„ ํ† ํฌ๋‚˜์ด์ €์— ํŠน๋ณ„ ํ† ํฐ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํŠน๋ณ„ ํ† ํฐ์€ ์ ˆ๋Œ€๋กœ ๋ถ„ํ• ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์ œ์–ด ํ† ํฐ์ด ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ํ† ํฐํ™”๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ…œํ”Œ๋ฆฟ์—์„œ ์–ด์‹œ์Šคํ„ดํŠธ ์ƒ์„ฑ์˜ ๋์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ† ํฐ์œผ๋กœ ํ† ํฌ๋‚˜์ด์ €์˜ `eos_token` ์†์„ฑ์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ…์ŠคํŠธ ์ƒ์„ฑ ๋„๊ตฌ๊ฐ€ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์–ธ์ œ ์ค‘์ง€ํ•ด์•ผ ํ• ์ง€ ์ •ํ™•ํžˆ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. </Tip> ### ์™œ ์ผ๋ถ€ ๋ชจ๋ธ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…œํ”Œ๋ฆฟ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‚˜์š”?[[why-do-some-models-have-multiple-templates]] ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋‹ค๋ฅธ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ผ๋ฐ˜ ์ฑ„ํŒ…์„ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ๊ณผ ๋„๊ตฌ ์‚ฌ์šฉ ๋˜๋Š” ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ์— ๋Œ€ํ•œ ํ…œํ”Œ๋ฆฟ์„ ๋ณ„๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ `tokenizer.chat_template`๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์•ฝ๊ฐ„์˜ ํ˜ผ๋ž€์„ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ€๋Šฅํ•œ ํ•œ ๋ชจ๋“  ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•ด ๋‹จ์ผ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. `if tools is defined`์™€ ๊ฐ™์€ Jinja ๋ฌธ์žฅ๊ณผ `{% macro %}` ์ •์˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ฝ”๋“œ ๊ฒฝ๋กœ๋ฅผ ๋‹จ์ผ ํ…œํ”Œ๋ฆฟ์— ์‰ฝ๊ฒŒ ๋ž˜ํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฌ๋‚˜์ด์ €์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…œํ”Œ๋ฆฟ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, `tokenizer.chat_template`๋Š” ํ…œํ”Œ๋ฆฟ ์ด๋ฆ„์ด ํ‚ค์ธ `๋”•์…”๋„ˆ๋ฆฌ`์ž…๋‹ˆ๋‹ค. `apply_chat_template` ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ํ…œํ”Œ๋ฆฟ ์ด๋ฆ„์— ๋Œ€ํ•œ ํŠน๋ณ„ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค: ์ผ๋ฐ˜์ ์œผ๋กœ `default`๋ผ๋Š” ํ…œํ”Œ๋ฆฟ์„ ์ฐพ๊ณ , ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ `tools` ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ `tool_use`๋ผ๋Š” ํ…œํ”Œ๋ฆฟ์ด ์กด์žฌํ•˜๋ฉด ๋Œ€์‹  ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ด๋ฆ„์˜ ํ…œํ”Œ๋ฆฟ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด `apply_chat_template()`์˜ `chat_template` ์ธ์ˆ˜์— ์›ํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์ด๋ฆ„์„ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ์•ฝ๊ฐ„์˜ ํ˜ผ๋ž€์„ ์ค„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ…œํ”Œ๋ฆฟ์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€๋Šฅํ•œ ํ•œ ๋‹จ์ผ ํ…œํ”Œ๋ฆฟ์— ๋ชจ๋“  ๊ฒƒ์„ ๋„ฃ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค! ### ์–ด๋–ค ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋‚˜์š”?[[what-template-should-i-use]] ์ด๋ฏธ ์ฑ„ํŒ…์šฉ์œผ๋กœ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์— ํ…œํ”Œ๋ฆฟ์„ ์„ค์ •ํ•  ๋•Œ๋Š” ํ…œํ”Œ๋ฆฟ์ด ํ›ˆ๋ จ ์ค‘ ๋ชจ๋ธ์ด ๋ณธ ๋ฉ”์‹œ์ง€ ํ˜•์‹๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ๊ฒฝํ—˜ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํฝ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์„ ์ถ”๊ฐ€๋กœ ํ›ˆ๋ จํ•  ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ† ํฐ์„ ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ตœ์ƒ์˜ ์„ฑ๋Šฅ์„ ์–ป๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ† ํฐํ™”์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ์ค‘์— ์‚ฌ์šฉ๋œ ํ† ํฐํ™”๋ฅผ ์ •ํ™•ํžˆ ์ผ์น˜์‹œํ‚ฌ ๋•Œ ์ถ”๋ก ์ด๋‚˜ ๋ฏธ์„ธ ์กฐ์ •์—์„œ ์ตœ๊ณ ์˜ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์— ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๊ฑฐ๋‚˜ ์ฑ„ํŒ…์šฉ์œผ๋กœ ๊ธฐ๋ณธ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ, ์ ์ ˆํ•œ ํ…œํ”Œ๋ฆฟ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ์ž์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. LLM์€ ๋‹ค์–‘ํ•œ ์ž…๋ ฅ ํ˜•์‹์„ ์ฒ˜๋ฆฌํ•  ๋งŒํผ ์ถฉ๋ถ„ํžˆ ๋˜‘๋˜‘ํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ธฐ ์žˆ๋Š” ์„ ํƒ ์ค‘ ํ•˜๋‚˜๋Š” `ChatML` ํ˜•์‹์ด๋ฉฐ, ์ด๋Š” ๋งŽ์€ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ์„ ํƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ``` {%- for message in messages %} {{- '<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n' }} {%- endfor %} ``` ์ด ํ…œํ”Œ๋ฆฟ์ด ๋งˆ์Œ์— ๋“ ๋‹ค๋ฉด, ์ฝ”๋“œ์— ๋ฐ”๋กœ ๋ณต์‚ฌํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ์ค„ ๋ฒ„์ „์„ ์ œ๊ณตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ํ•œ ์ค„ ๋ฒ„์ „์€ [์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ](#what-are-generation-prompts)์— ๋Œ€ํ•œ ํŽธ๋ฆฌํ•œ ์ง€์›๋„ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€๋งŒ, BOS๋‚˜ EOS ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”! ๋ชจ๋ธ์ด ํ•ด๋‹น ํ† ํฐ์„ ๊ธฐ๋Œ€ํ•˜๋”๋ผ๋„, `apply_chat_template`์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์ถ”๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํ…์ŠคํŠธ๋Š” `add_special_tokens=False`์— ์˜ํ•ด ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ…œํ”Œ๋ฆฟ๊ณผ `add_special_tokens` ๋…ผ๋ฆฌ ๊ฐ„์˜ ์ž ์žฌ์ ์ธ ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ํŠน๋ณ„ ํ† ํฐ์„ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ฒฝ์šฐ, ํ…œํ”Œ๋ฆฟ์— ์ง์ ‘ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! ```python tokenizer.chat_template = "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}" ``` ์ด ํ…œํ”Œ๋ฆฟ์€ ๊ฐ ๋ฉ”์‹œ์ง€๋ฅผ `<|im_start|>` ์™€ `<|im_end|>`ํ† ํฐ์œผ๋กœ ๊ฐ์‹ธ๊ณ , ์—ญํ• ์„ ๋ฌธ์ž์—ด๋กœ ์ž‘์„ฑํ•˜์—ฌ ํ›ˆ๋ จ ์‹œ ์‚ฌ์šฉํ•˜๋Š” ์—ญํ• ์— ๋Œ€ํ•œ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ```text <|im_start|>system You are a helpful chatbot that will do its best not to say anything so stupid that people tweet about it.<|im_end|> <|im_start|>user How are you?<|im_end|> <|im_start|>assistant I'm doing great!<|im_end|> ``` "์‚ฌ์šฉ์ž", "์‹œ์Šคํ…œ" ๋ฐ "์–ด์‹œ์Šคํ„ดํŠธ" ์—ญํ• ์€ ์ฑ„ํŒ…์˜ ํ‘œ์ค€์ด๋ฉฐ, ๊ฐ€๋Šฅํ•  ๋•Œ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋ชจ๋ธ์ด [`TextGenerationPipeline`]๊ณผ ์ž˜ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๋ ค๋ฉด ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ์—ญํ• ์—๋งŒ ๊ตญํ•œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ…œํ”Œ๋ฆฟ์€ ๋งค์šฐ ์œ ์—ฐํ•˜๋ฉฐ, ์–ด๋–ค ๋ฌธ์ž์—ด์ด๋“  ์—ญํ• ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ### ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค! ์–ด๋–ป๊ฒŒ ์‹œ์ž‘ํ•ด์•ผ ํ•˜๋‚˜์š”?[[i-want-to-add-some-chat-templates-how-should-i-get-started]] ์ฑ„ํŒ… ๋ชจ๋ธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ชจ๋ธ์˜ `tokenizer.chat_template` ์†์„ฑ์„ ์„ค์ •ํ•˜๊ณ  [`~PreTrainedTokenizer.apply_chat_template`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•œ ๋‹ค์Œ ์—…๋ฐ์ดํŠธ๋œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ํ—ˆ๋ธŒ์— ํ‘ธ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ ์†Œ์œ ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋„ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋นˆ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์ด๋‚˜ ์—ฌ์ „ํžˆ ๊ธฐ๋ณธ ํด๋ž˜์Šค ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, [ํ’€ ๋ฆฌํ€˜์ŠคํŠธ](https://huggingface.co/docs/hub/repositories-pull-requests-discussions)๋ฅผ ๋ชจ๋ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ์—ด์–ด ์ด ์†์„ฑ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์„ธ์š”! ์†์„ฑ์„ ์„ค์ •ํ•˜๋ฉด ๋์ž…๋‹ˆ๋‹ค! `tokenizer.apply_chat_template`๊ฐ€ ์ด์ œ ํ•ด๋‹น ๋ชจ๋ธ์— ๋Œ€ํ•ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋ฏ€๋กœ, `TextGenerationPipeline`๊ณผ ๊ฐ™์€ ๊ณณ์—์„œ๋„ ์ž๋™์œผ๋กœ ์ง€์›๋ฉ๋‹ˆ๋‹ค! ๋ชจ๋ธ์— ์ด ์†์„ฑ์„ ์„ค์ •ํ•จ์œผ๋กœ์จ, ์˜คํ”ˆ ์†Œ์Šค ๋ชจ๋ธ์˜ ์ „์ฒด ๊ธฐ๋Šฅ์„ ์ปค๋ฎค๋‹ˆํ‹ฐ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜•์‹ ๋ถˆ์ผ์น˜๋Š” ์ด ๋ถ„์•ผ์—์„œ ์˜ค๋žซ๋™์•ˆ ์„ฑ๋Šฅ์„ ์ €ํ•˜์‹œํ‚ค๋Š” ๋ฌธ์ œ์˜€์œผ๋ฏ€๋กœ, ์ด์ œ ์ด๋ฅผ ๋๋‚ผ ๋•Œ์ž…๋‹ˆ๋‹ค! ## ๊ณ ๊ธ‰: ํ…œํ”Œ๋ฆฟ ์ž‘์„ฑ ํŒ[[advanced-template-writing-tips]] Jinja์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ๋จผ์ € ๋ฉ”์‹œ์ง€๋ฅผ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ˜•์‹ํ™”ํ•˜๋Š” ์งง์€ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•œ ๋‹ค์Œ, ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ…œํ”Œ๋ฆฟ ํ•ธ๋“ค๋Ÿฌ๋Š” `messages`๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ๋Œ€ํ™” ๊ธฐ๋ก์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ…œํ”Œ๋ฆฟ ๋‚ด์˜ `messages`์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, `{% for message in messages %}`๋กœ ๋ฐ˜๋ณตํ•˜๊ฑฐ๋‚˜ `{{ messages[0] }}`์™€ ๊ฐ™์ด ๊ฐœ๋ณ„ ๋ฉ”์‹œ์ง€์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ํŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ Jinja๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: ### ๊ณต๋ฐฑ ์ œ๊ฑฐ[[trimming-whitespace]] ๊ธฐ๋ณธ์ ์œผ๋กœ Jinja๋Š” ๋ธ”๋ก ์ „ํ›„์˜ ๊ณต๋ฐฑ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ณต๋ฐฑ์„ ๋งค์šฐ ์ •ํ™•ํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ณ ์ž ํ•˜๋Š” ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ํ…œํ”Œ๋ฆฟ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: ``` {%- for message in messages %} {{- message['role'] + message['content'] }} {%- endfor %} ``` ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์ง€ ๋งˆ์„ธ์š”: ``` {% for message in messages %} {{ message['role'] + message['content'] }} {% endfor %} ``` `-`๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ธ”๋ก ์ „ํ›„์˜ ๊ณต๋ฐฑ์ด ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ๋Š” ๋ฌดํ•ดํ•ด ๋ณด์ด์ง€๋งŒ, ์ค„๋ฐ”๊ฟˆ๊ณผ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ์ถœ๋ ฅ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฐ๊ณผ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ### ๋ฐ˜๋ณต๋ฌธ[[for-loops]] Jinja์—์„œ ๋ฐ˜๋ณต๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ``` {%- for message in messages %} {{- message['content'] }} {%- endfor %} ``` {{ ํ‘œํ˜„์‹ ๋ธ”๋ก }} ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์ด ์ถœ๋ ฅ์œผ๋กœ ์ธ์‡„๋ฉ๋‹ˆ๋‹ค. `+`์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„์‹ ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ### ์กฐ๊ฑด๋ฌธ[[if-statements]] Jinja์—์„œ ์กฐ๊ฑด๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ``` {%- if message['role'] == 'user' %} {{- message['content'] }} {%- endif %} ``` ํŒŒ์ด์ฌ์ด ๊ณต๋ฐฑ์„ ์‚ฌ์šฉํ•˜์—ฌ `for` ๋ฐ `if` ๋ธ”๋ก์˜ ์‹œ์ž‘๊ณผ ๋์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐ˜๋ฉด, Jinja๋Š” `{% endfor %}` ๋ฐ `{% endif %}`๋กœ ๋ช…์‹œ์ ์œผ๋กœ ๋์„ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ### ํŠน์ˆ˜ ๋ณ€์ˆ˜[[special-variables]] ํ…œํ”Œ๋ฆฟ ๋‚ด๋ถ€์—์„œ๋Š” `messages` ๋ชฉ๋ก์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ ํŠน์ˆ˜ ๋ณ€์ˆ˜์—๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” `bos_token` ๋ฐ `eos_token`๊ณผ ๊ฐ™์€ ํŠน๋ณ„ ํ† ํฐ๊ณผ ์•ž์„œ ๋…ผ์˜ํ•œ `add_generation_prompt` ๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ `loop` ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๋ฐ˜๋ณต์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด `{% if loop.last %}`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋Œ€ํ™”์˜ ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `add_generation_prompt`๊ฐ€ `True`์ธ ๊ฒฝ์šฐ ๋Œ€ํ™” ๋์— ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ``` {%- if loop.last and add_generation_prompt %} {{- bos_token + 'Assistant:\n' }} {%- endif %} ``` ### ๋น„ํŒŒ์ด์ฌ Jinja์™€์˜ ํ˜ธํ™˜์„ฑ[[compatibility-with-non-python-jinja]] Jinja์˜ ์—ฌ๋Ÿฌ ๊ตฌํ˜„์€ ๋‹ค์–‘ํ•œ ์–ธ์–ด๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋™์ผํ•œ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ฃผ์š” ์ฐจ์ด์ ์€ ํŒŒ์ด์ฌ์—์„œ ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•  ๋•Œ ํŒŒ์ด์ฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด์— `.lower()`๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ์— `.items()`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋น„ํŒŒ์ด์ฌ Jinja ๊ตฌํ˜„์—์„œ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ JS์™€ Rust๊ฐ€ ์ธ๊ธฐ ์žˆ๋Š” ๋ฐฐํฌ ํ™˜๊ฒฝ์—์„œ๋Š” ๋น„ํŒŒ์ด์ฌ ๊ตฌํ˜„์ด ํ”ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”! ๋ชจ๋“  Jinja ๊ตฌํ˜„์—์„œ ํ˜ธํ™˜์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ…œํ”Œ๋ฆฟ์„ ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค: - ํŒŒ์ด์ฌ ๋ฉ”์†Œ๋“œ๋ฅผ Jinja ํ•„ํ„ฐ๋กœ ๋Œ€์ฒดํ•˜์„ธ์š”. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง€๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด `string.lower()`๋Š” `string|lower`๋กœ, `dict.items()`๋Š” `dict|items`๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ๋ชฉํ•  ๋งŒํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ `string.strip()`์ด `string|trim`์œผ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ Jinja ๋ฌธ์„œ์˜ [๋‚ด์žฅ ํ•„ํ„ฐ ๋ชฉ๋ก](https://jinja.palletsprojects.com/en/3.1.x/templates/#builtin-filters)์„ ์ฐธ์กฐํ•˜์„ธ์š”. - ํŒŒ์ด์ฌ์— ํŠนํ™”๋œ `True`, `False`, `None`์„ ๊ฐ๊ฐ `true`, `false`, `none`์œผ๋กœ ๋Œ€์ฒดํ•˜์„ธ์š”. - ๋”•์…”๋„ˆ๋ฆฌ๋‚˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง์ ‘ ๋ Œ๋”๋งํ•  ๋•Œ ๋‹ค๋ฅธ ๊ตฌํ˜„์—์„œ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์˜ˆ: ๋ฌธ์ž์—ด ํ•ญ๋ชฉ์ด ๋‹จ์ผ ๋”ฐ์˜ดํ‘œ์—์„œ ์ด์ค‘ ๋”ฐ์˜ดํ‘œ๋กœ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). `tojson` ํ•„ํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.
transformers/docs/source/ko/chat_templating.md/0
{ "file_path": "transformers/docs/source/ko/chat_templating.md", "repo_id": "transformers", "token_count": 29216 }
299