# Combined Scaling for Zero-shot Transfer Learning

Hieu Pham∗<EMAIL_ADDRESS>
Zihang Dai∗<EMAIL_ADDRESS>
Golnaz Ghiasi∗<EMAIL_ADDRESS>
Kenji Kawaguchi∗<EMAIL_ADDRESS>
Hanxiao Liu<EMAIL_ADDRESS>
Adams Wei Yu<EMAIL_ADDRESS>
Jiahui Yu<EMAIL_ADDRESS>
Yi-Ting Chen<EMAIL_ADDRESS>
Minh-Thang Luong<EMAIL_ADDRESS>
Yonghui Wu<EMAIL_ADDRESS>
Mingxing Tan<EMAIL_ADDRESS>
Quoc V. Le<EMAIL_ADDRESS>
*: Equal contributions.
Corresponding authors<EMAIL_ADDRESS>
###### Abstract

We present a combined scaling method – named BASIC – that achieves 85.7% top-1
accuracy on the ImageNet ILSVRC-2012 validation set without learning from any
labeled ImageNet example. This accuracy surpasses best-published similar
models – CLIP and ALIGN – by 9.3%. Our BASIC model also shows significant
improvements in robustness benchmarks. For instance, on 5 test sets with
natural distribution shifts such as ImageNet-{A,R,V2,Sketch} and ObjectNet,
our model achieves 84.3% top-1 average accuracy, only a small drop from its
original ImageNet accuracy.

To achieve these results, we scale up the contrastive learning framework of
CLIP and ALIGN in three dimensions: data size, model size, and batch size. Our
dataset has 6.6B noisy image-text pairs, which is 4x larger than ALIGN, and
16x larger than CLIP. Our largest model has 3B weights, which is 3.75x larger
in parameters and 8x larger in FLOPs than ALIGN and CLIP. Finally, our batch
size is 65536 which is 2x more than CLIP and 4x more than ALIGN.

We encountered two main challenges with the scaling rules of BASIC. First, the
main challenge with implementing the combined scaling rules of BASIC is the
limited memory of accelerators, such as GPUs and TPUs. To overcome the memory
limit, we propose two simple methods which make use of gradient checkpointing
and model parallelism. Second, while increasing the dataset size and the model
size has been the defacto method to improve the performance of deep learning
models like BASIC, the effect of a large contrastive batch size on such
contrastive-trained image-text models is not well-understood. To shed light on
the benefits of large contrastive batch sizes, we develop a theoretical
framework which shows that larger contrastive batch sizes lead to smaller
generalization gaps for image-text models such as BASIC.

###### Contents

  1. 1 Introduction
  2. 2 Related Work
  3. 3 Background on Image-text Contrastive Learning and Zero-shot Transfer Learning
  4. 4 Batch Size Scaling with Pipelining and Gradient Accumulation
    1. 4.1 The Memory Bottleneck for Image-Text Contrastive Learning
    2. 4.2 Modifying Pipelining and GradAccum for the Contrastive Loss
  5. 5 Batch Size Scaling with the Single-Program Multiple-Data (SPMD) Scheme
    1. 5.1 Weight Sharding
    2. 5.2 Rematerialization
    3. 5.3 Comparison with Pipelining and Gradient Accumulation
  6. 6 Theoretical Insight on the Role of Contrastive Batch Size
  7. 7 Data and Model Scaling
    1. 7.1 Larger image-text dataset
    2. 7.2 Larger Model Architectures
  8. 8 Pretraining and Finetuning
  9. 9 Experiments
    1. 9.1 Training details
    2. 9.2 Results on Image Classification Benchmarks
    3. 9.3 Results on Robustness Benchmarks
  10. 10 Ablation Study
    1. 10.1 The Importance of Batch Size Scaling
    2. 10.2 Data Scaling, Model Scaling, and Pretraining
  11. 11 Limitations
  12. 12 Conclusion
  13. A Model sizes
  14. B Hyperparameters and other implementation details
  15. C Evaluation Datasets Details
  16. D Further Discussion on Robustness
  17. E Computational Cost
  18. F Qualitative Analysis: Successful Classification Examples
  19. G Failure Analysis
    1. G.1 The benchmarks where BASIC fails
    2. G.2 Example failure cases
  20. H Proofs
    1. H.1 General case
      1. H.1.1 Analyzing $\sup_{x\in\mathcal{X}}\mathcal{R}_{B}(\mathcal{H}_{\mathcal{F},\mathcal{G},e}^{x})$ and $\mathcal{R}_{m}(\mathcal{H}_{\mathcal{F},\mathcal{G},\hat{\ell}_{B}})$
      2. H.1.2 Combining all together for the general case
    2. H.2 Bounding $\mathbb{E}_{y,\sigma}\left[\sup_{G\in\mathcal{G}}\frac{1}{B}\left\|\sum_{i=1}^{B}\sigma_{i}G(y_{i})\right\|_{2}\right]$ and $\sum_{k=1}^{D}\left(\mathcal{R}_{m}(\mathcal{F}_{k})+\mathcal{R}_{m}(\mathcal{G}_{k})\right)$ for the special case with deep neural networks
    3. H.3 Combining all together for the special case with deep neural networks

##  1 Introduction

The recent advances in multimodal training approaches such as CLIP (Radford et
al., 2021) and ALIGN (Jia et al., 2021) have the potential to eliminate the
need for collecting labeled training data for every new application. Using
natural language as a weak supervision signal, CLIP and ALIGN achieve the
impressive top-1 accuracy of 76.2% and 76.4% on ImageNet ILSVRC-2012 without
learning from any labeled ImageNet data. In addition to the promising accuracy
on ImageNet, the so-called “zero-shot” models in CLIP and ALIGN demonstrate
two important properties. First, these models are versatile, as they can be
directly deployed on many downstream tasks without task-specific data for
finetuning. Second, CLIP and ALIGN models are more robust than traditional
classifiers. Robustness evaluations on benchmarks with natural distribution
shifts (Hendrycks et al., 2021b, a; Recht et al., 2019; Barbu et al., 2019;
Wang et al., 2019) show that the accuracy of models like CLIP and ALIGN
typically drops less than 10%, while the accuracy of supervised and semi-
supervised models might drop as much as 40% (Taori et al., 2020; Szegedy et
al., 2013).

Despite their versatility and robustness, the best models from CLIP and ALIGN
are still not as competitive as supervised and semi-supervised models when
enough labeled data is available, which can limit their potential
applications. For example, the best CLIP and ALIGN models have an accuracy
around 76% on ImageNet, which is only comparable with a supervised ResNet-50
(He et al., 2015), and significantly worse than the state-of-the-art
supervised training on ImageNet (without extra data: 87.1% (Yuan et al.,
2021), and with extra data: 90.88% (Dai et al., 2021)). Therefore, narrowing
the gap from these models to supervised and semi-supervised models would make
the image-text contrastive learning approach in CLIP and ALIGN a viable
alternative for image classification.

In this paper, we develop significantly better image classifiers that leverage
the image-text contrastive learning approaches like CLIP and ALIGN at a much
larger scale. In particular, we scale up the contrastive learning framework of
CLIP (Radford et al., 2021) and ALIGN (Jia et al., 2021) in 3 dimensions:
dataset size, model size, and batch size. For the data, we expand the ALIGN
dataset (Jia et al., 2021) from 1.7B noisy image-text pairs to 6.6B pairs,
i.e., almost 4x larger. For the models, we choose CoAtNet, an architecture
with higher learning capacity (Dai et al., 2021), and scale it to 3B
parameters, i.e., 3.75x more weights and 8x more FLOPs than the largest models
in CLIP and ALIGN. For the batch size, we use 65536 contrastive learning
examples per minibatch, i.e., 2x more than CLIP and 4x more than ALIGN.

##### Overview of our implementation.

The fundamental bottleneck of training large models at larger batch sizes is
the limited memory of deep learning accelerators such as GPUs and TPUs. We
propose two approaches that allow practitioners to overcome such memory
limits.

Our first approach (Section 4) makes use of micro-batch pipelining (Huang et
al., 2019) and gradient accumulation (GradAccum) (Ott et al., 2018; Zhai et
al., 2021). Our second approach (Section 5) utilizes the model parallelism
scheme of Single-Program Multi-Data (SPMD) (Lepikhin et al., 2020; Xu et al.,
2021) to distribute the weights of certain layers in our networks onto
different devices. While our SPMD approach is faster than our pipelining
approach, and can deliver exact computations, the SPMD approach requires more
manual designs to scale to arbitrarily large contrastive batch sizes, and
hence, is less general than the pipelining approach.

Both our pipelining approach and our SPMD approach make use of gradient
checkpointing (Chen et al., 2016), which is also called rematerialization in
certain literature (Kumar et al., 2019; Jain et al., 2020). The idea behind
rematerialization is to discard certain intermediate values in the forward
pass of a neural network to save memory, and then recompute – i.e.,
rematerialize – these values only when they are needed for gradient
computation in the network’s backward pass.

##### Overview of our theoretical insights.

While the benefits of large datasets and large models for deep learning models
have become established knowledge, the benefits of large batch size are less
well-understood in the context of relatively new image-text contrastive
models. To understand such benefits, we develop a theoretical analysis of the
image-text contrastive learning framework of CLIP and ALIGN. Our analysis
establishes that using a larger contrastive batch size in CLIP and ALIGN’s
framework leads to a smaller generalization gap of the resulting models.

|  ALIGN (Jia et al., 2021) |  CLIP (Radford et al., 2021) |  BASIC (ours)
---|---|---|---
ImageNet | 76.4 | 76.2 | 85.7 | (+9.3)
ImageNet-A | 75.8 | 77.2 | 85.6 | (+8.4)
ImageNet-R | 92.2 | 88.9 | 95.7 | (+3.5)
ImageNet-V2 | 70.1 | 70.1 | 80.6 | (+10.5)
ImageNet-Sketch | 64.8 | 60.2 | 76.1 | (+11.3)
ObjectNet | 72.2 | 72.3 | 82.3 | (+10.1)
Average | 74.5 | 74.2 | 84.3 | (+10.1)

Table 1: Highlights of our key results. Shown are the top-1 accuracy of our
method, BASIC, and similar baselines – CLIP and ALIGN – on ImageNet and other
robustness test sets. None of these models have learned from any labeled
training example in ImageNet. On average, BASIC surpasses the baselines by the
significant 10.1 percentage points.

##### Overview of our empirical results.

Our proposed method, called BASIC, for Batch, Data and Model SIze Combined
Scaling, achieves drastic improvements over CLIP and ALIGN models. For
instance, on ImageNet, the largest BASIC model achieves 85.7% top-1 accuracy,
without learning from any labeled example in the ImageNet training set. This
result surpasses similar models in CLIP and ALIGN 9.3%. This BASIC model also
shows significant improvements on robustness benchmarks. For instance, on 5
test sets with natural distribution shifts such as ImageNet-{A,R,V2,Sketch}
and ObjectNet, the model achieves an average of 83.7% top-1 accuracy, only a
small drop from its original ImageNet accuracy (see Table 1). When tested
against CLIP on the other 17 image classification benchmarks, e.g., CIFAR,
Caltech101, Flowers, etc. BASIC outperforms CLIP on 13 out of these 17
benchmarks.

##  2 Related Work

##### Large-scale pretraining and the contrastive loss.

As computer vision models grow in their size and capacity, many weakly-
supervised and self-supervised pretraining methods have been proposed to learn
good visual representations. On one hand, pretraining with a classification
loss on large weakly-labeled datasets such as Instagram hashtags or JFT can
produce significant gains on downstream tasks such as ImageNet (Joulin et al.,
2016; Mahajan et al., 2018; Kolesnikov et al., 2020; Dosovitskiy et al., 2021;
Sun et al., 2017; Zhai et al., 2021). On the other hand, self-supervised
methods which leverage existing structures in unlabeled data to train models
have been developed. A promising development in self-supervised learning is
the contrastive loss, with representative works like CPC (van den Oord et al.,
2018), SimCLR (Chen et al., 2020a, b) and MoCo (He et al., 2020; Chen et al.,
2020c). In this paper, we scale up the contrastive learning framework, which
we will revisit in detail in Section 3.

##### Contrastive-learned image-text models.

Unlike the single-modal contrastive approaches mentioned in the previous
paragraph, our work leverages data from two modalities: image and text. Using
images with accompanying text is related to the literature on image-captioning
models, such as (Vinyals et al., 2015; Karpathy and Fei-Fei, 2015; Xu et al.,
2015; Joulin et al., 2016; Li et al., 2017; Sariyildiz et al., 2020; Zhang et
al., 2020; Desai and Johnson, 2021). While learning to generate captions from
images can induce good visual representations, it is not the goal of this
paper. Instead, this paper focuses on establishing the ability of models to
classify images based on textual descriptions. This focus makes our work
closely related to the recent work of image-text models such as CLIP (Radford
et al., 2021) and ALIGN (Jia et al., 2021). Similar to CLIP and ALIGN, our
work also learns the mapping between images and texts, which is related to
many previous works, such as (Hironobu et al., 1999; Weston et al., 2010;
Socher and Fei-Fei, 2010; Socher et al., 2013; Hodosh et al., 2013; Frome et
al., 2013; Norouzi et al., 2013; Kiros et al., 2014; Socher et al., 2014;
Akata et al., 2015b, a; Nam et al., 2017; Faghri et al., 2017; Li et al.,
2019; Liu et al., 2019; Lu et al., 2019; Messina et al., 2020; Chen et al.,
2020d; Huang et al., 2020; Chen et al., 2021).

##### Differences between our work and zero-shot learning.

Early works on zero-shot vision models date back to the 2000s, e.g.,
(Larochelle et al., 2008; Zhang et al., 2017; Xian et al., 2016, 2017;
Schönfeld et al., 2019). In these works, the term “zero-shot” refers to the
ability of models to “generalize to classes or tasks for which no training
data are available and only a description of the classes or tasks are
provided”. Under such definition, BASIC models – as well as the recent work
that BASIC is based on such as CLIP (Radford et al., 2021) and ALIGN (Jia et
al., 2021) – are not “zero-shot learned” models. This is because the data
curating procedures of BASIC, CLIP, and ALIGN can exposes certain class names
to their models, albeit not intentionally. For instance, when an image of a
golden retriever dog is crawled from the internet, the image could come from a
file named my_golden_retriever.jpg which was uploaded by a user. If a model in
BASIC, CLIP, or ALIGN learns to associate the content of such an image with
the text sequence “my golden retriever” as parsed from the image’s file name,
and then the model uses the knowledge from such association at test time, then
the model is not zero-shot. Despite being not zero-shot, models from BASIC,
CLIP, and ALIGN retain their claimed benefits on versatility and robustness.

##### Zero-shot transfer learning

Instead of zero-shot learning, CLIP and ALIGN are known to conduct zero-shot
transfer learning (Radford et al., 2021; Jia et al., 2021; Zhai et al., 2022).
Zero-shot transfer learning differs significantly from zero-shot learning.
Unlike zero-shot learning, it permits relevant supervised information during
pretraining, while it allows no supervised examples during the transfer
protocol; i.e., zero-shot transfer learning skips the finetuning stage
completely and performs the downstream task based only on a text description
of the target classes. For example, see (Radford et al., 2021; Jia et al.,
2021; Zhai et al., 2022) for more details on this terminology.

##### Data, model and batch scaling.

Scaling has proven to be a powerful tool to boost the efficacy of vision model
pretraining. There are three dimensions one can scale on. The simplest
dimension is data. Indeed, recent efforts have shown that the more data we
train on, the better the models become (Joulin et al., 2016; Mahajan et al.,
2018; Kolesnikov et al., 2020; Dosovitskiy et al., 2021; Sun et al., 2017).
The second dimension is the model size, with representative works such as
EfficientNet, VITs and related works (Tan and Le, 2019, 2021; Tan et al.,
2020; Dosovitskiy et al., 2021; Zhai et al., 2021; Bello et al., 2021).
Lastly, scaling up batch sizes is also the key for improving the model
effectiveness (Goyal et al., 2017), especially for the contrastive loss (Chen
et al., 2020a; Tian et al., 2020; Jia et al., 2021; Radford et al., 2021). Our
work is inspired by the power of scaling, and pushes the limits in all the
dimensions.

##  3 Background on Image-text Contrastive Learning and Zero-shot Transfer
Learning

In this section, we revisit the contrastive training framework for parallel
image-text data, as introduced by CLIP (Radford et al., 2021) and ALIGN (Jia
et al., 2021). In doing so, we define the notations that will be used
throughout the remaining of this paper.

Let ${\bm{x}}\in\mathcal{X}$ be an arbitrary image and
${\bm{y}}\in\mathcal{Y}$ be an arbitrary text sequence. The image-text
contrastive training framework (Radford et al., 2021; Jia et al., 2021) trains
an image encoder $F$ and a text encoder $G$ to map ${\bm{x}}$ and ${\bm{y}}$
into a $D$-dimensional unit sphere, i.e.,
$F({\bm{x}}),G({\bm{y}})\in{\mathbb{S}}^{D}$. The desiderata of these encoders
is that images and text sequences of similar semantics should be mapped to
nearby points in the latent space, while those with different semantics should
be mapped to distant points in the space. To train $F$ and $G$ to achieve such
desiderata, a minibatch gradient training procedure is used. At each step in
this training procedure, $F$ and $G$ receives $B$ image-text pairs, i.e.
$({\bm{x}}_{i},{\bm{y}}_{i})$ for $i=1,2,...,B$. Based on the embeddings
computed by $F$ and $G$, a similarity matrix ${\bm{A}}$ is computed, where
${\bm{A}}_{i,j}=\big{(}F({\bm{x}}_{i})^{\top}G({\bm{y}}_{i})\big{)}/\tau$.
Here, $\tau$ is called the softmax temperature which serves to steepen or
dampen the softmax distributions in the rows and columns of ${\bm{A}}$. From
this similarity matrix ${\bm{A}}$, two softmax-like losses are computed based
on the rows and the columns of ${\bm{A}}$:

$\displaystyle\text{RowLoss}_{B}$
$\displaystyle=-\frac{1}{B}\sum_{i=1}^{B}\log{\frac{{\bm{A}}_{i,j}}{\sum_{k=1}^{B}{\bm{A}}_{i,k}}}$
(1) $\displaystyle\text{ColumnLoss}_{B}$
$\displaystyle=-\frac{1}{B}\sum_{j=1}^{B}\log{\frac{{\bm{A}}_{i,j}}{\sum_{k=1}^{B}{\bm{A}}_{k,j}}}$
(2) $\displaystyle\text{ContrastiveLoss}_{B}$
$\displaystyle=\frac{\text{RowLoss}_{B}+\text{ColumnLoss}_{B}}{2}$ (3)

Minimizing $\text{ContrastiveLoss}_{B}$ encourages the entries on the diagonal
of ${\bm{A}}$ to be large while the entries elsewhere to be small.
Equivalently, images and text sequences from the same pair in the minibatch,
i.e. $x_{i}$ and ${\bm{y}}_{i}$, will be embedded into nearby points, while
those from different pairs, i.e. $x_{i}$ and ${\bm{y}}_{j\neq i}$, will be
embedded into distant points. The resulting encoders $F$ and $G$ thus achieve
the desiderata of the contrastive learning framework.

##  4 Batch Size Scaling with Pipelining and Gradient Accumulation

We start this section by discussing the memory bottleneck in the contrastive
training framework as described in Section 3. We focus on memory because it is
the most crucial bottleneck which hinders two out of three dimensions that we
want to scale, i.e., model size and batch size. We further show that the
vanilla pipelining algorithm (Huang et al., 2019) with gradient accumulation
(GradAccum) (Ott et al., 2018; Zhai et al., 2021) is not directly applicable
to contrastive learning. We then describe our modifications to make GradAccum
work for constrastive learning.

###  4.1 The Memory Bottleneck for Image-Text Contrastive Learning

##### The memory bottleneck.

The consensus among representative work in contrastive learning (Chen et al.,
2020b, c; He et al., 2020; Chen et al., 2020a) is that the larger the networks
trained with a larger contrastive batch size performs better. This observation
is further explained by our theoretical analysis in Section 6, and is
confirmed by empirical results in Section 10. Therefore, we want to enlarge
the networks $F$, $G$, and the batch size $B$. However, this will create a
memory bottleneck. Three well-known techniques to relieve memory burden are
gradient accumulation (GradAccum) (Ott et al., 2018; Zhai et al., 2021), re-
materialization (or gradient checkpointing) (Griewank and Walther, 2000; Chen
et al., 2016) and model parallelism (Shazeer et al., 2018; Huang et al., 2019;
Lepikhin et al., 2020). Note that all three techniques are orthogonal and
complementary to each other. Next in section 4.2, we present an approach based
on pipelining model parallelism and gradient accumulation.

##### Vanilla GradAccum.

Consider training a model weight vector $\theta$ to minimize a loss function
$\mathcal{L}$. For a batch of $B$ examples
$\\{{\mathbf{e}}_{1},{\mathbf{e}}_{2},...,{\mathbf{e}}_{B}\\}$, let $g_{i}$ be
the gradient of $\mathcal{L}$ with respect to $\theta$ computed on example
${\mathbf{e}}_{i}$, i.e.,
$g_{i}=\nabla_{\theta}\mathcal{L}(\theta;{\mathbf{e}}_{i})$. In the standard
minibatch setting, we update $\theta$ with the average batch gradient
$\bar{g}=\big{(}\sum_{i=1}^{B}g_{i}\big{)}/B$. When our accelerator memory can
only hold $M\ll B$ examples, GradAccum splits the batch of $B$ examples into
smaller batches with at most $M$ examples, called microbatches, then computes
the gradients of the microbatches, and averages them.

We now analyze the steps of GradAccum. For simplicity, assume that $M$ evenly
divides $B$, and that microbatch $i$-th consists of examples
${\mathbf{e}}_{j}$’s with $(i-1)M+1\leq j\leq iM$. With this assumption, the
GradAccum procedure first initializes a zero vector $\bar{g}$ of the same size
with $\theta$. Then, sequentially for each microbatch $i$-th, the microbatch
gradient $c_{i}=\big{(}\sum\nolimits_{j=(i-1)M+1}^{iM}g_{j}\big{)}/M$ is added
to $\bar{g}$. In the end, $\bar{g}$ holds the correct minibatch gradient, up
to a normalization constant $K=B/M$.

##### GradAccum cannot be naively applied to contrastive learning.

There are two properties that make GradAccum not applicable to contrastive
learning. First, in order to evaluate the loss $\text{ContrastiveLoss}_{B}$ in
Equation 3, we need all entries of the similarity matrix ${\bm{A}}$. Hence, we
cannot rely only on examples in every microbatch $i$-th to compute the
microbatch gradients $c_{i}$’s. Second, GradAccum must allocate memory for the
cumulative gradient $\bar{g}$.111It is worth noting that this is a common
issue with GradAccum and is not specific to contrastive learning. As $\bar{g}$
has as many elements as $\theta$, its memory grows as we scale up the networks
$F$ and $G$. This growth becomes a more pronounced issue as we scale up our
models. For reference, our largest model has 3B weights, occupying roughly
11GB of accelerator memory. Spending another 11GB on $\bar{g}$, while
possible, defeats the purpose of saving memory in GradAccum. In the remaining
of this subsection, we discuss how to modify GradAccum so that we can use it
to scale up contrastive learning.

###  4.2 Modifying Pipelining and GradAccum for the Contrastive Loss

Inputs |  $\bullet$ Networks $F$, $G$ with a weight vector $\theta=[\theta_{F},\theta_{G}]$, |  |  Memory Analysis
---|---|---|---
|  $\bullet$ A minibatch of $B$ (image, text) pairs $\\{({\bm{x}}_{i},{\bm{y}}_{i})\\}_{i=1}^{B}$, |  $\triangleright$ $B$ is the contrastive batch size.
|  $\bullet$ Microbatch size $M$. Assuming $M$ evenly devices $B$. |  $\triangleright$ $M$ is the largest in-memory batch size.
Yields |  $\bullet$ Gradients $\nabla_{\theta}\text{ContrastiveLoss}$ for $B/M$ |  $\triangleright$ The loss is computed as in Equation 3
|  $\bullet$ microbatches of the minibatch. |
1 | Allocate embedding matrices ${\bm{X}},{\bm{Y}}\in{\mathbb{R}}^{D\times B}$ |  $\triangleright$ $D$ is the embedding size | $\Theta(BD)$
2 |  For $i=1$ to $B/M$ do: |  $\triangleright$ Sequentially compute the embeddings for |
3 |  Let $J\leftarrow\\{j:(i-1)M+1\leq j\leq iM\\}$ |  microbatches of images and text sequences, |
4 |  ${\bm{X}}_{:,J}\leftarrow F({\bm{x}}_{J})$ |  not saving the activations of $F$ and $G$. | $\Theta(M\cdot\text{Mem}(F))$
5 |  ${\bm{Y}}_{:,J}\leftarrow G({\bm{y}}_{J})$ |  | $\Theta(M\cdot\text{Mem}(G))$
6 | ${\bm{A}}\leftarrow\big{(}{\bm{X}}^{\top}\cdot{\bm{Y}}\big{)}/\tau$ |  $\triangleright$ ${\bm{A}}\in{\mathbb{R}}^{B\times B}$ is the similarity matrix |
7 | $\text{RowLoss}_{B}\leftarrow-\frac{1}{B}\sum_{i=1}^{B}\log{\frac{{\bm{A}}_{i,j}}{\sum_{k=1}^{B}{\bm{A}}_{i,k}}}$ |  $\triangleright$ The contrastive loss in Equation 3 | $\Theta(B^{2})$
8 | $\text{ColumnLoss}_{B}\leftarrow-\frac{1}{B}\sum_{j=1}^{B}\log{\frac{{\bm{A}}_{i,j}}{\sum_{k=1}^{B}{\bm{A}}_{k,j}}}$ |  |
9 | $\text{ContrastiveLoss}_{B}\leftarrow\frac{\text{RowLoss}_{B}+\text{ColumnLoss}_{B}}{2}$ |  |
10 | $d{\bm{A}}\leftarrow\text{BackProp}(\text{ContrastiveLoss},{\bm{A}})$ |  $\triangleright$ Back-prop to compute $\nabla_{{\bm{A}}}$ |
11 | $d{\bm{X}}\leftarrow{\bm{Y}}\cdot d{\bm{A}}$ |  $\triangleright$ Because ${\bm{A}}={\bm{X}}^{\top}{\bm{Y}}$ |
12 | $d{\bm{Y}}\leftarrow{\bm{X}}\cdot d{\bm{A}}$ |  |
13 |  For $i=1$ to $B/M$ do: |  $\triangleright$ Repeat a forward pass on $F$, $G$ to |
14 |  Let $J\leftarrow\\{j:(i-1)M+1\leq j\leq iM\\}$ |  back-prop the gradients from $d{\bm{X}}$, $d{\bm{Y}}$ to |
15 |  $d\theta_{F}\leftarrow\text{ForwardAndBackProp}(d{\bm{X}}_{:,J},\theta_{F})$ |  the weights $\theta_{F},\theta_{G}$. | $\Theta(M\cdot\text{Mem}(F))$
16 |  $d\theta_{G}\leftarrow\text{ForwardAndBackProp}(d{\bm{Y}}_{:,J},\theta_{G})$ |  | $\Theta(M\cdot\text{Mem}(G))$
17 |  Yield $d\theta=[d\theta_{F},d\theta_{G}]$ |  |

Algorithm 1 Pseudo code of our gradient accumulation process for the
contrastive loss. Here $\text{Mem}(F)$, $\text{Mem}(G)$ denote the memory
required for a pass for the networks $F$, $G$. As shown in our memory
analysis, at the cost of repeating one forward pass for $F$, $G$ (lines
13-16), our procedure’s peak memory footprint is dominated by
$\Theta(M\cdot\max{\\{\text{Mem}(F),\text{Mem}(G)\\}})$.

##### Microbatching the contrastive loss.

To enable proper GradAccum, a key observation is that while we need the entire
similarity matrix ${\bm{A}}$ to compute $\text{ContrastiveLoss}_{B}$ in
Equation 3, we do not need to store all the intermediate results leading to
the matrix in memory. This observation immediately connects to re-
materialization, which trades computation for memory by dropping some
intermediate hidden states during the forward pass and re-computing them
during back-propagation. Following this insight, we propose to combine re-
materialization with gradient accumulation by microbatching the contrastive
loss and re-materializing each microbatch.

Specifically, we first run a forward pass on the networks $F$, $G$ to compute
the entire similarity matrix ${\bm{A}}$ while discarding all intermediate
hidden states. Then, we use ${\bm{A}}$ to compute $\text{ContrastiveLoss}_{B}$
and the gradient $\nabla_{{\bm{A}}}\text{ContrastiveLoss}_{B}$ and microbatch
this gradient along the batch axis. Finally, for each microbatch, we re-
materialize the hidden states, i.e., rerun the forward computation, and back-
prop and accumulate the corresponding gradient microbatch of
$\nabla_{{\bm{A}}}\mathcal{L}_{\text{c}}$ into the weights of the networks
$F$, $G$.

Algorithm 1 presents this procedure in detail and provides the memory analysis
for each step. As shown, our algorithm can compute the exact microbatch
gradients from an entire batch of $B$ examples, with the peak memory usage of
$\Theta(M\cdot\max{\\{\text{Mem}(F),\text{Mem}(G)\\}})$, instead of
$\Theta(B\cdot(\text{Mem}(F)+\text{Mem}(G)))$. We note that our algorithm can
be flexibly modified to work different microbatch-sizes, i.e., $M$, for the
image network $F$ and the text network $G$. This flexibility allows for more
efficient computations, e.g., when one network is smaller than another and
thus, can operate with larger microbatches.

##### Accumulating the microbatch gradients.

Algorithm 1 yields a stream of microbatch gradients $c_{1},...,c_{B/M}$, which
need to be accumulated, i.e., averaged, into $\bar{g}$ to perform the batch
weight update. As discussed, we want to avoid allocating extra memory for
$\bar{g}$. To do this, we need two assumptions about our training
implementation. Our first assumption is that we use an optimizer which
involves gradient moments (Nesterov, 1983; Tieleman and Hinton, 2012; Kingma
and Ba, 2015; Loshchilov and Hutter, 2019; Shazeer and Stern, 2018). This
assumption motivates our idea to avoid allocating $\bar{g}$: since the
optimizer already allocates the memory for gradient moments, typically called
slots, we will directly accumulate the microbatch gradients $c_{i}$’s into
these slots.

We illustrate this idea with Adam (Kingma and Ba, 2015), a popular optimizer
that involves two gradient moments. At training step $t$, Adam receives the
averaged minibatch gradient $\bar{g}$ and makes the following updates to its
gradient moments $v_{1}$ and $v_{2}$:

$\displaystyle\bar{g}$
$\displaystyle=1/B\cdot\sum\nolimits_{i=1}^{B}g_{i}=1/\underbrace{(B/M)}_{K}\cdot\sum\nolimits_{i=1}^{B/M}c_{i}$
$\displaystyle v_{1}^{(t)}$
$\displaystyle=\beta_{1}v_{1}^{(t-1)}+(1-\beta_{1})\bar{g}$ $\displaystyle
v_{2}^{(t)}$ $\displaystyle=\beta_{2}v_{2}^{(t-1)}+(1-\beta_{2})\bar{g}^{2}$

Accumulating the microbatch gradients $c_{i}$’s to $v_{1}$ is straightforward.
We can simply modify $v_{1}$’s single update with $\bar{g}$ into $K=B/M$
updates as follows:

$\displaystyle v_{1}\leftarrow
k_{i}v_{1}+(1-\beta_{1})c_{i},\leavevmode\nobreak\
\text{where}\leavevmode\nobreak\ k_{i}=\begin{cases}\beta_{1}&\text{if
$i=1$}\\\ 1/K&\text{otherwise}\end{cases}$

Unfortunately, the same approach is not applicable for $v_{2}$, as the square
of the sum is generally different from the sum of the squares, i.e. $(\sum
c_{i})^{2}\neq\sum c_{i}^{2}$. However, the difference between these two
quantities turns out to be:

$\small\underbrace{\frac{1}{K}\sum_{i=1}^{K}c_{i}^{2}}_{\text{sum of
squares}}-\underbrace{\Big{(}\frac{1}{K}\sum_{i=1}^{K}c_{i}\Big{)}^{2}}_{\text{square
of
sum}}=\mathbf{E}\mathopen{}\left[c_{i}^{2}\mathopen{}\right]-\mathbf{E}\mathopen{}\left[c_{i}\mathopen{}\right]^{2}=\mathbf{Var}\mathopen{}\left[c_{i}\mathopen{}\right],$

which we can estimate. Indeed, since each $c_{i}$’s is the mean of $M$ per-
example gradients $g_{j}$’s in the $i$-th microbatch, we can treat $c_{i}$’s
as the population mean of $M$ observed examples drawn from a random variable
${\mathbf{g}}\sim\text{Uniform}\\{g_{1},...,g_{B}\\}$. This treatment allows
us to use the familiar identity:

$\displaystyle\mathbf{Var}\mathopen{}\left[c_{i}\mathopen{}\right]=\mathbf{Var}\mathopen{}\left[\frac{1}{M}\sum\nolimits_{j=(i-1)M+1}^{iM}g_{j}\mathopen{}\right]=\frac{\mathbf{Var}\mathopen{}\left[{\mathbf{g}}\mathopen{}\right]}{M}$
(4)

Therefore, to estimate $\mathbf{Var}\mathopen{}\left[c_{i}\mathopen{}\right]$,
we only need to estimate
$\mathbf{Var}\mathopen{}\left[{\mathbf{g}}\mathopen{}\right]$. For this, we
make the second assumption about our training: that we use a data parallelism
setting with $R$ replicas. Under this assumption, each microbatch gradient
$c_{i}$ is obtained from an all-reduce operation on $R$ replicas, each of
which processes $M/R$ examples. Once again, treating these per-device
gradients $d_{1},...,d_{R}$ as the population mean of $M/R$ observed examples
for ${\mathbf{g}}$, we can apply Identity 4 to obtain:
$\mathbf{Var}\mathopen{}\left[d\mathopen{}\right]=\mathbf{Var}\mathopen{}\left[{\mathbf{g}}\mathopen{}\right]/(M/R)$.
This treatment allows us to perform GradAccum while avoiding to allocate
$\bar{g}$.

##  5 Batch Size Scaling with the Single-Program Multiple-Data (SPMD) Scheme

In Section 4, we have seen that one circumvent the memory bottleneck of large
models and large batch sizes for image-text contrastive learning by: (1)
“chunk” a large batch of $B$ image-text pairs into arbitrarily smaller
microbatches, (2) compute the gradient for each microbatch, and (3) accumulate
them. While such an approach is generic and can work for any global
contrastive batch size $B$ and any microbatch size $M$, there are two steps in
the approach that makes the resulting gradient inexact. The first inexact
computation comes from the approximations when accumulating the microbatch
gradients. This is a necessary tradeoff to avoid allocating the memory to
accumulate the microbatch gradients. The second inexact computation is more
subtle, and is specific to our modeling choice, and has also been noted by
Huang et al. (2019). Specifically, when our networks $F$ and $G$ depend on the
batch, e.g., via the batch normalization layers in our image encoder $F$, then
the outputs of the networks for multiple microbatches are generally different
from those for one entire global batch. When the microbatch size $M$ is too
small compared to $B$, the discrepancies become very large and can cause the
covariate shift in the image encoder $F$ which was the original motivation for
batch normalization. More recent image encoders can overcome such inexact
computations because they avoid batch normalization by replacing it with layer
normalization like Vision Transformer (Dosovitskiy et al., 2021), or just do
not use any normalization at all like NFNet (Brock et al., 2021). However, the
inexact gradient accumulation remains even for such models.

To overcome these inexact computations, in this section, we discuss an
alternate approach to circumvent the memory bottleneck. This approach is based
on the SPMD programming scheme. We find that not only does our SPMD method
provide exact computations which lead to better results than pipelining and
GradAccum, but our SPMD method also has a better latency per training step.
However, as we shall see in Section 5.1 and Section 5.2, our SPMD method
requires several manual designs, which make it less generic than pipelining
and GradAccum.

###  5.1 Weight Sharding

As model sizes grow, model weights occupy a significant part of accelerator
memory. In modern optimizers for deep learning models, such as Adam (Kingma
and Ba, 2015), RMSprop (Tieleman and Hinton, 2012), and AdamW (Loshchilov and
Hutter, 2019), every weight tensor is additionally accompanied by the first
and second gradient moments, hence tripling its memory footprint. Furthermore,
in the vanilla data parallelism training, all these weights are replicated to
all accelerators. In our experiments with a relatively large model size,
roughly 4GB of accelerator memory is occupied by these weights and their
gradient moments, which is significant for the typical 16GB memory in an
accelerator in 2022, such as a Google TPU core or an Nvidia RTX 3080 GPU.

Here, we split the weight tensors in our encoder networks, i.e. $F$ and $G$ in
Section 3, into multiple accelerator cores, and only combine these tensors
together when the whole tensor is needed to perform certain computations. Note
that upon splitting a weight tensor to multiple cores, we also split its first
and second gradient moments in the similar way. Figure 1 illustrates our
weight sharding strategy on the 2D convolution operation which is prevalent in
image encoder models.

Figure 1: An illustrative example for our model parallelism design. Shown is
2D convolution operation with a 3x3 kernel sharded to 4 cores. Gray cells
represent the tensor values that are mirrored across all cores, while cells of
other colors represent per-core independent tensor values. The convolution’s
input is a tensor of shape $[N,H,W,i]$ which is sharded along its first
dimension so that each core has processes a tensor of shape $[N/4,H,W,i]$. The
convolution’s kernel is a tensor of shape $[3,3,i,o]$, but each core only
stores one shard of the kernel which has size $[3,3,i/4,o]$. Before
convolving, every core receives the kernel shards from all other cores and
concatenate the shares, forming the complete kernel of size $[3,3,i,o]$. After
convolving, the complete kernel is discarded from all cores’ memory.

Our approach is based on the Single-Program Multiple-Data (SPMD) technique,
which has been successfully applied to train large language models in previous
works such as in Xu et al. (2021); Lepikhin et al. (2020). In the SPMD
technique, we define a computational graph which represents our entire
training program. This computational graph is compiled once, and then is
replicated identically to all computational cores to run the training program.
While all of our computational cores run an identical program, they are
allowed to receive different inputs and hence can produce different outputs.
These inputs and outputs can be organized in certain ways to define
arbitrarily complex model parallelism strategies. Next, we describe how we
apply the SPMD technique on our model weights only.

Our training program runs typically on a cluster of 2048 TPUv3 cores. We
partition these 2048 cores into $R$ replicas, each of which uses $2048/R$
cores. The value of $R$ governs how the weights of our image encoder $F$ and
our text encoder $G$ are stored in the memory of our 2048 cores. In
particular, all weight tensors in the networks $F$ and $G$ are split into $R$
equal parts, each lives in one of the $R$ cores in a replica. Note that since
we have $2048/R$ replicas, the weights of our image and text encoders are
still replicated for $2048/R$ times. For instance, our cores $1^{\text{st}}$,
$2^{\text{nd}}$, … $R^{\text{th}}$ can each store $1/R$ of the weight tensors,
and then the cores $R+1^{\text{st}}$, $R+2^{\text{nd}}$, …, $2R^{\text{th}}$
store an identical copy of these tensors. Thus, using fewer replicas and more
cores per replica leads to a better memory utilization, at a higher overhead
for cross-cores communications. We empirically find that using 512 replicas
and 4 cores per replica offers a good balance.

It is important to note that we only apply SPMD on our model weights, and not
on any other steps of our computations. This means that if our training
program receives an input batch of $B$ examples, then these $B$ examples are
distributed equally to all our 2048 cores. In other words, each of our 2048
cores processes $B/2048$ examples, regardless of the value of $R$. We find
that this design choice disentangles our weight sharding strategy from the
rematerialization strategy, as described next in Section 5.2.

###  5.2 Rematerialization

Figure 2: Generic rematerialization map for the blocks in our CoAtNet models.
Left: in the Mobile Inverse Convolution blocks (MBConv), all batch
normalization layers and activation layers, as well as all layers in the
squeeze and excitation steps, are rematerialized. Right: in Transformer
blocks, only the layer normalization layers and activation layers are
rematerialized.

The technique of rematerialization, also widely known as gradient
checkpointing (Chen et al., 2016), preserves the accelerator memory while
training neural networks. It works by not saving certain values from a
network’s forward pass, and recompute them in the backward pass only when
their values are needed for a particular calculation. For instance, if our
image encoder $F$, as discussed in Section 3 has 100 layers, a
rematerialization program can decide that after the forward pass, only the
values of layers $10^{\text{th}}$, $20^{\text{th}}$, …, $90^{\text{th}}$ are
kept in an accelerator’s memory, while the values of other layers are removed.
If all layers of $F$ consumes similar memory, this rematerialization program
has reduced the memory cost by 10 times, at the trade off that the values of
the unsaved layers in the forward pass, such as layer $21^{\text{st}}$ or
layer $72^{\text{nd}}$, have to be recomputed in the backward pass.

We select which layer to rematerialize in our image encoder $F$ and our text
encoder $G$ based on a simple heuristic. Ideally, we want to rematerialize the
layers that are fast to recompute but consumes the more memory. Since we
utilize weight sharding, as described in Section 5.1, the computations that
involve weights are slower than normal because of their overhead time for
cross-core communications. As such, we keep almost all layers that involve
weights, such as convolution, attention, and dense feed-forwards, in our
accelerator’s memory. In contrast, layers that do not involve weights, such as
activation functions, batch normalization, and layer normalization, are all
rematerialized. Figure 2 illustrates our rematerialization strategy for all
three block types in our image and text encoders: the mobile-inverse
convolutional block (Tan and Le, 2019; Dai et al., 2021), the attention block,
and the feed-forward blocks (Vaswani et al., 2017).

We find this design choice beneficial, because in modern encoder
architectures, every layer that involves weights is typically followed by a
normalization layer or an activation layer. As such, our design allows more
than half of the encoder’s activation values to be removed from the
accelerator’s memory after each forward pass, while leaving only the light
computational steps to be repeated in each backward pass. We empirically find
that weight sharding and rematerialization, each of our forward-backward pass
is 1.4 times slower than the vanilla implementation of the same batch size.

##### Exceptions to our heuristics.

Certain parts of our encoders do not follow these general heuristics, as we
find that doing so saves a certain amount of time at the cost of using a
little extra memory. Here, we describe these exceptions:

  1. 1.

All weights in batch normalization and layer normalization in our models,
including the $\beta$’s and $\gamma$’s and the moving average statistics of
batch normalization, are not sharded. Instead, these weights are replicated to
all computational cores to avoid cross-cores communications, because they are
one dimensional vectors which do not occupy much memory.

  2. 2.

All computations the Squeeze-and-Excitation blocks (SE; (Hu et al., 2018)) of
our models are rematerialized, including the convolutions. This is because
these SE blocks only involve 1x1 convolution with reduced internal channels,
making them less costly to recompute. In addition, all the weights of these
1x1 convolutions are replicated to all of our cores, because they have a small
memory footprint but are reused in the backward pass for rematerialization.

###  5.3 Comparison with Pipelining and Gradient Accumulation

Model | Methods | Batch |  Step time (millisecs) | Memory
---|---|---|---|---
Size | ($B$) | FWD | BWD | Total | (GB)
Small | Data parallelism | $2^{16}$ | 28.6 | 81.2 | 128.5 | 4.2
Data parallelism | $2^{17}$ | 65.8 | 144.5 | 230.3 | 6.4
Data parallelism | $2^{18}$ | 131.2 | 282.9 | 428.0 | 9.9
Data parallelism | $2^{19}$ | 261.0 | 559.4 | 842.4 | 14.8
Data parallelism | $2^{20}$ | $-$ | $-$ | $-$ | OOM
Pipeline & GradAccum | $2^{20}$ | 301.7 | 1497.4 | 1983.1 | 11.2
SPMD | $2^{20}$ | 271.8 | 1311.8 | 1631.6 | 15.2
Medium | Data parallelism | $2^{16}$ | 129.5 | 270.8 | 437.6 | 10.7
Data parallelism | $2^{17}$ | 237.7 | 536.4 | 806.1 | 13.8
Data parallelism | $2^{18}$ | $-$ | $-$ | $-$ | OOM
Pipeline & GradAccum | $2^{18}$ | 597.7 | 1677.1 | 2677.3 | 12.6
SPMD | $2^{18}$ | 599.3 | 1407.2 | 2018.6 | 12.1
Pipeline & GradAccum | $2^{19}$ | 1393.1 | 4087.8 | 5601.2 | 12.8
SPMD | $2^{19}$ | 1402.7 | 3141.1 | 4912.3 | 14.1
Pipeline & GradAccum | $2^{20}$ | 3014.3 | 6129.9 | 9781.4 | 12.6
SPMD | $2^{20}$ | 2909.7 | 4912.3 | 8361.1 | 15.4

Table 2: Comparison between our SPMD programs and our Pipelining & GradAccum
programs. Shown are the step times and memory footprints of our small-sized
and medium-sized models at different batch sizes. With the same model size and
batch size, our SPMD design results in a larger device memory footprint and
Pipelining & GradAccum, but SPMD is faster.

We measure and compare the step time and peak memory usage of our SPMD
approach and our Pipelining and GradAccum approach from Section 4. We measure
these pieces of information as the contrastive batch size $B$ becomes larger.
In particular, we start with $B=2^{16}$ and double $B$ until we reach
$B=2^{20}$. When $B$ increases, but our model can still fit into the our
memory only with vanilla data parallelism, we measure the step time and the
peak memory using this setting. These measures serve as the reference to
quantify how much overhead is introduced by pipelining, SPMD, or
rematerialization as a whole. When $B$ grows and our models can no longer
train with merely data parallelism, we experiment with the Pipelining and
GradAccum, and compare the resulting programs with the SPMD programs of the
same model and batch size. Note that for the pipelining approach, we set the
microbatch size to the largest size that vanilla data parallelism can fit in
our accelerator’s memory.

For all settings, we profile a model in 15 seconds. Our profiling tool tracks
the time and memory usage of our models during their forward and backward
passes in each step. Note that our backward pass time includes the time our
models spend on their rematerialized computations (as discussed in Section
5.2), as these computations happen while the models compute their gradients.
Note that other than these forward and backward passes, every step of our
models has some extra overheads for miscellaneous computations that are not
recorded by our profiling tool, e.g., gradient clippings and updating model
parameters.

All of our measurements are reported in Table 2. From the table, it can be
seen that our model parallelism strategy leads to a faster overall step time,
compared to the pipelining approach in the same setting. Breaking down these
step times, it can be seen that the run time of our strategy’s model forward
pass is very close to the run time of pipelining’s forward pass, but our
backward time is often a lot faster. For instance, in our largest setting,
with the medium-sized model and the contrastive batch size $B=2^{20}$, our
backward time is more than 1.2 seconds faster than that of the pipelining
approach, amounting to about 10% of the total step time. Additionally, our
strategy also has a faster total step time, perhaps because do not need to
spend extra time to accumulate the microbatch gradients like the pipelining
approach.

Finally, we note that as $B$ grows larger, the SPMD approach typically
occupies more accelerator memory than does the pipelining approach. This is
because in the pipelining approach, increasing the contrastive batch size $B$
only leads to more microbatches, but does not change the micro batch size, and
so the accelerator’s memory remains constant. As such, the pipelining approach
is still applicable if $B$ grows larger than $2^{20}$, but the SPMD strategy
has to be redesigned, e.g. by deciding to rematerialize a larger portion of
our image and text encoder.

##  6 Theoretical Insight on the Role of Contrastive Batch Size

In this section, we provide a theoretical insight that increasing the
contrastive batch size tends to improve the performance of the final model,
which motivates and partially justifies the design of our new algorithm in the
previous section. Let $\hat{y}_{1},\dots,\hat{y}_{B}$ be the sequence of the
text sentence inputs used in training. Similarly, let
$\bar{y}_{1},\dots,\bar{y}_{M}$ be the sequence of the text sentence inputs
used in testing. We then define the normalized training loss by

$\displaystyle\hat{\ell}_{B}(x,y)=-\frac{B\exp(F(x)^{\top}G(y))}{\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))}=-\frac{\exp(F(x)^{\top}G(y))}{\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))},$

where we multiply $B$ to scale the loss correctly in the regime of
$B\rightarrow\infty$. That is, since the unnormalized loss goes to zero
($\hat{\ell}_{B}(x,y)/B\rightarrow 0$) as the batch size approach infinity
($B\rightarrow\infty$), analyzing the unnormalized version of the loss
$\hat{\ell}_{B}(x,y)/B$ can mistakenly predict benefits of the large
contrastive batch size. We avoid this with the normalization. Similarly, we
define the normalized testing loss by

$\bar{\ell}_{M}(x,y)=-\frac{\exp(F(x)^{\top}G(y))}{\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]}.$

Then, the prediction at testing time for a new input $x$ is given by

$\displaystyle\text{pred}(x)=\operatorname*{argmax}_{j\in\\{1,2,\dots,M\\}}F(x)^{\top}G(\bar{y}_{j})=\operatorname*{argmin}_{j\in\\{1,2,\dots,M\\}}\bar{\ell}_{M}(x,\bar{y}_{j}).$

Therefore, $\hat{\ell}_{B}(\hat{x}_{i},\hat{y}_{i})$ is minimized during
training for training points $(\hat{x}_{i},\hat{y}_{i})$ while we want to
minimize $\bar{\ell}_{M}(x,y)$ to make a prediction at a new point $x$. This
leads to the question of the generalization from training to unseen-data,
which can be studied by analyzing the upper bound on the following quantity:

$\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)],$

where $\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(\hat{x},\hat{y})]$ is the empirical
training loss with a dataset, $S=((\hat{x}_{i},\hat{y}_{i}))_{i=1}^{m}$, of
size $m$. To analyze this in a statistical setting, we define a vector
$v\in\mathbb{R}^{D}$ by
$v_{i}=F(x)_{i}-\frac{\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))G(\hat{y}_{k})_{i}}{\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))}$
for all $i\in\\{1,\dots,D\\}$, and assume that
$\hat{y}_{1},\hat{y}_{2},\dots,\hat{y}_{B}\stackrel{{\scriptstyle
iid}}{{\sim}}p_{y}$,
$\bar{y}_{1},\bar{y}_{2},\dots,\bar{y}_{M}\stackrel{{\scriptstyle
iid}}{{\sim}}p_{y}$, $\exp(F(x)^{\top}G(y))\leq c_{1}$,
$\hat{\ell}_{B}(x,y)\leq c_{2}$, $\|F(x)\|_{2}\leq c_{3}$,
$\frac{\exp(F(x)^{\top}G(y))}{\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))}\leq
c_{4}$, $\|F(x_{i})\|_{2}\leq c_{5}$, $\|v\|_{2}\leq c_{6}$, $\|y\|_{2}\leq
c_{7}$, and $\|x\|_{2}\leq c_{8}$, with probability one. Moreover, by defining
$\gamma(x)=\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(y))]-\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}\allowbreak
G(\hat{y}_{k}))$, we assume that $\gamma$ is $c_{9}$-Lipschitz; i.e.,
$|\gamma(x)-\gamma(x^{\prime})|\leq c_{9}\|x-x^{\prime}\|_{2}$ for all
$x\in\mathcal{X}\subseteq\mathbb{R}^{\kappa}$. To provide a concrete insight,
we consider standard deep neural networks, of the form

$\displaystyle
G(y)=(\omega_{L}\circ\sigma_{L-1}\circ\omega_{L-1}\circ\sigma_{L-2}\cdots\sigma_{1}\circ\omega_{1})(y),$
$\displaystyle
F(x)=(\omega_{L^{\prime}}^{\prime}\circ\sigma_{L^{\prime}-1}^{\prime}\circ\omega_{L^{\prime}-1}^{\prime}\circ\sigma_{L^{\prime}-2}^{\prime}\cdots\sigma_{1}^{\prime}\circ\omega_{1}^{\prime})(x),$

where $\omega_{l}(q)=W_{l}q$ represents the linear transformation and
$\sigma_{l}$ is an element-wise nonlinear activation function. Similarly,
$\omega_{l}^{\prime}(q)=W_{l}^{\prime}q$ and $\sigma_{l}^{\prime}$ is an
element-wise activation function.

The following theorem provides an insight on the role of the contrastive batch
size to close the accuracy gap from contrastive models to their supervised
counterparts:

###### Theorem 1

Suppose that the activation functions $\sigma$ and $\sigma_{l}^{\prime}$ are
1-Lipschitz and positive homogeneous for all $l\in[L-1]$. Let
$\mathcal{G}=\\{y\mapsto G(y):(\forall l\in[L-1])[\|W_{l}\|_{F}\leq
M_{l}]\wedge\|(W_{L})_{k}\|_{F}\leq M_{L,k}\\}$ and $\mathcal{F}=\\{x\mapsto
F(x):(\forall l\in[L^{\prime}-1])[\|W_{l}^{\prime}\|_{F}\leq
M_{l}^{\prime}]\wedge\|(W_{L^{\prime}}^{\prime})_{k}\|_{F}\leq
M_{L^{\prime},k}^{\prime}\\}$ where $(W_{l})_{k}$ is the $k$-th row of
$W_{l}$. Then, for any $\delta>0$, with probability at least $1-\delta$, the
following holds for all $F\in\mathcal{F}$ and $G\in\mathcal{G}$:

$\displaystyle\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]\leq\frac{Q_{1}}{\sqrt{m}}+\frac{Q_{2}}{\sqrt{2B}}+c_{2}\sqrt{\frac{\ln(2/\delta)}{2m}},$

where
$Q_{1}=2\sqrt{2}c_{4}\sqrt{c_{5}^{2}+c_{6}^{2}}(\tilde{Q}_{1,1}+\tilde{Q}_{1,2})$,
$Q_{2}=c_{1}\mathbb{E}_{x,y}[A(x,y)](\tilde{Q}_{2,1}+\tilde{Q}_{2,2})$,

$\displaystyle\tilde{Q}_{1,1}=c_{7}(\sqrt{2\log(2)L}+1)\left(\prod_{l=1}^{L-1}M_{l}\right)\left(\sum_{k=1}^{D}M_{L,k}\right),$
$\displaystyle\tilde{Q}_{1,2}=c_{8}(\sqrt{2\log(2)L^{\prime}}+1)\left(\prod_{l=1}^{L^{\prime}-1}M_{l}^{\prime}\right)\left(\sum_{k=1}^{D}M^{\prime}_{L^{\prime},k}\right),$
$\displaystyle\tilde{Q}_{2,1}=2\sqrt{2}c_{8}c_{9}+c_{1}\sqrt{\kappa\ln(\sqrt{\kappa
B}/\delta)},$
$\displaystyle\tilde{Q}_{2,2}=2\sqrt{2}c_{3}c_{7}(\sqrt{2\log(2)L}+1)\left(\prod_{l=1}^{L-1}M_{l}\right)\sqrt{\sum_{k=1}^{D}M_{L,k}^{2}},$

and
$A(x,y)=\frac{\exp(F(x)^{\top}G(y))}{\left(\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))\right)\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]}$.

Proof  The proof is presented in Appendix H.
Theorem 1 shows that the generalization gap
$\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]$
approaches zero as the number of samples $m$ and the contrastive batch size
$B$ increase, at the rate of $O(\frac{1}{\sqrt{m}}+\frac{1}{\sqrt{B}})$, with
high probability. This shows the importance of the contrastive batch size $B$
with the week supervision setting without explicit labels: i.e., if $B$ is
small, the generalization gap can be large, even with a lot of training
samples with large $m$. This is different from a classification task with a
standard supervision, where a large $m$ is sufficient to reduce the
generalization gap. In sum, Theorem 1 provides the insight that we should
increase both the number of samples $m$ and the contrastive batch size $B$ to
improve the performance of the final model via the weekly supervised
contrastive learning.

For general models beyond the standard deep neural networks, the following
theorem provides a similar insight on the importance of the contrastive batch
size:

###### Theorem 2

Let $\mathcal{F}$ be a set of maps $x\mapsto F(x)$ and $\mathcal{G}$ be a set
of maps $y\mapsto G(y)$. Then, for any $\delta>0$, with probability at least
$1-\delta$, the following holds for all $F\in\mathcal{F}$ and
$G\in\mathcal{G}$:

$\displaystyle\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]\leq\frac{C_{1}}{\sqrt{2B}}+c_{2}\sqrt{\frac{\ln(2/\delta)}{2m}}+C_{2}\sum_{k=1}^{D}\left(\mathcal{R}_{m}(\mathcal{F}_{k})+\mathcal{R}_{m}(\mathcal{G}_{k})\right)+C_{3}\tilde{\mathcal{R}}_{B}(G),$

where
$\mathcal{R}_{m}(\mathcal{H}):=\mathbb{E}_{S,\xi}[\sup_{h\in\mathcal{H}}\frac{1}{m}\sum_{i=1}^{m}\xi_{i}h(x_{i},y_{i})]$,
$\mathcal{F}_{k}=\\{x\mapsto F(x)_{k}:F\in\mathcal{F}\\}$,
$\mathcal{G}_{k}=\\{y\mapsto G(y)_{k}:G\in\mathcal{G}\\}$,
$\tilde{\mathcal{R}}_{B}(G)=\mathbb{E}_{y,\xi}\left[\sup_{G\in\mathcal{G}}\frac{1}{B}\left\|\sum_{i=1}^{B}\xi_{i}G(y_{i})\right\|_{2}\right]$,
$C_{1}=c_{1}\mathbb{E}_{x,y}[A(x,y)]\tilde{Q}_{2,1}$,
$C_{2}=2\sqrt{2}c_{4}\sqrt{c_{5}^{2}+c_{6}^{2}}$, and
$C_{3}=2c_{1}c_{3}\mathbb{E}_{x,y}[A(x,y)]$. Here, $\xi_{1},\dots,\xi_{m}$ are
independent uniform random variables taking values in $\\{-1,1\\}$.

Proof  The proof is completed in Appendix H.
Similarly to Theorem 1, Theorem 2 shows that the gap
$\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]$
decreases as the number of samples $m$ and the contrastive batch size $B$
increase, at the rate of $O(\frac{1}{\sqrt{m}}+\frac{1}{\sqrt{B}})$, with high
probability, for general models $F$ and $G$. Because we are not specifying the
types of the models $F$ and $G$, we incur the additional terms that capture
the model complexity of $F$ and $G$: i.e.,
$\mathcal{R}_{m}(\mathcal{F}_{k})+\mathcal{R}_{m}(\mathcal{G}_{k})$ and
$\tilde{\mathcal{R}}_{B}(G)$. The values of these model complexity terms
differ for different models and typically scale as
$\mathcal{R}_{m}(\mathcal{F}_{k})+\mathcal{R}_{m}(\mathcal{G}_{k})=O(\frac{1}{\sqrt{m}})$
and $\tilde{\mathcal{R}}_{B}(G)=O(\frac{1}{\sqrt{B}})$ in terms of $m$ and $B$
as illustrated in the proof of Theorem 1 for standard deep neural networks.
Therefore, it is desirable to use a large contrastive batch size, which
motivates our new algorithm in the previous section.

##  7 Data and Model Scaling

###  7.1 Larger image-text dataset

Starting from the ALIGN dataset, which contains 1.7B weakly-aligned image-text
pairs (Jia et al., 2021), we collect 5B more image-text pairs, hence expanding
the dataset size by roughly 4 times. We acquire these 5B image-text pairs from
the JFT dataset. In the JFT dataset, each image is associated with one or
multiple classes. We convert these classes into a text sequence: “{class_1}
and {class_2} and … and {class_k}”. We combine the instances from JFT into
ALIGN, forming our extended dataset, which we denote by ALIGN+JFT.

To tokenize the texts from ALIGN+JFT, we randomly sample 200M sentences and
use them to train a sentence piece model (Kudo and Richardson, 2018) with a
vocabulary size of 32K pieces. Using this tokenizer, we filter and discard the
text sequences which are longer than 64 tokens. In our preliminary
experiments, we find that using a tokenizer directly learned from ALIGN+JFT
and adapting this filtering step can boost our top-1 accuracy on ImageNet
ILSVRC-2012 by more than 1%.

###  7.2 Larger Model Architectures

We find that for the same computational budget, it is more beneficial to
invest in scaling up the image encoder, rather than the text encoder. Thus,
for our image encoder, we use the largest CoatNet architecture (Dai et al.,
2021) due to its proven large learning capacity. This network has convolution
layers followed by attention layers. For our text encoder, we use a simple
transformer (Vaswani et al., 2017). Unlike ALIGN (Jia et al., 2021) which
extracts the final text representations using a [CLS] token similar to BERT
(Devlin et al., 2018), we average the representations across all steps at the
top layer of our transformer.

By experimenting with the scaling benefits for small models and generalizing
these findings to larger models, we choose three model sizes, termed
BASIC-{S,M,L} for Small, Medium, and Large. In Appendix A, we report our
architectures and their computational costs and provide a small-scale study on
the effects of scaling model sizes.

##  8 Pretraining and Finetuning

To further speed up the training of our networks, we make use of pretraining.
In our experiments, we first pretrain the image encoder on a large labeled
dataset using the standard softmax classification loss. After pretraining the
image encoder, we fix all of its weights and just train the text encoder using
contrastive learning. Compared to contrastive learning with GradAccum, the
pretraining-finetuning procedure is much more efficient in terms of peak
memory usage. This is because we never have to compute the gradients of both
the image encoder and the text encoder, which allows automated compiler
optimizations to free up unused memory on-the-fly.

Despite its reduced memory usage, we find that this pretraining-finetuning
scheme has a weakness: it never exposes the image encoder to noisy image-text
data, which makes the image encoder fail on certain tasks. For instance, while
some pretrained-and-finetuned models achieve similar accuracy to their
contrastive counterparts on ImageNet or CIFAR, they completely fail on an
easier task – MNIST. This is because our pretraining labeled dataset, which
mostly consists of natural images, has very few digit images. Meanwhile, our
noisy image-text dataset has plenty instances that can teach a model certain
optical character recognition skills.

As will be shown in Section 9, our best experimental results are achieved
using a hybrid procedure. First, we pretrain the image encoder on a large
labeled dataset, then fix its weights and train the text encoder using the
contrastive loss on our image-text dataset. Finally, we finetune both image
and text encoders, using our GradAccum technique when needed. In Section 10,
we present ablation studies to analyze the effects of pretraining, finetuning,
and other alternative training procedures.

##  9 Experiments

###  9.1 Training details

##### Labeled data for pretraining.

For pretraining (Section 8), we use the JFT dataset. This dataset has been
used in previous publications (Zhai et al., 2021; Dosovitskiy et al., 2021;
Kolesnikov et al., 2020), but it has been constantly expanded. The JFT version
used in our experiments has 5B images, each of which can be associated to one
or multiple labels out of 29K possible classes.

##### Data filtering.

A problem with training on large auto-curated datasets like ALIGN and JFT is
that these datasets might unintentionally contain examples from our test sets.
To avoid such contaminations, we filter all instances in our training data
that has a structural similarity index (SSIM (Wang et al., 2004)) of at least
0.5 with any image from our evaluation benchmarks.

##### Optimizer.

We train our models with our own optimizer called AdaFactorW, adapted from two
existing ones: AdaFactor (Shazeer and Stern, 2018) and AdamW (Loshchilov and
Hutter, 2019). Specifically, we factorize our second gradient moments like
AdaFactor, and decouple the weight decay from all moments like AdamW. To
further save memory, we follow Zhai et al. (2021) and store the first gradient
moments in bfloat16. We observe, however, that while we can store these
moments in bfloat16, we need to convert them into float32 prior to computing
our weight updates to avoid numerical instability.

Datasets |  Birdsnap |  Caltech101 |  CIFAR10 |  CIFAR100 |  DTD |  EuroSAT |  Flowers |  Food101 |  ImageNet |  MNIST |  IIIT-Pets |  PCam |  RESISC45 |  STL10 |  SUN397 |  UCF101 |  VOC2007
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
ResNet-50 | 32.6 | 82.1 | 75.6 | 41.6 | 41.7 | 41.1 | 65.9 | 81.1 | 59.6 | 66.6 | 85.4 | 57.6 | 54.2 | 94.3 | 59.6 | 63.6 | 82.1
BASIC-S | 38.6 | 91.6 | 86.4 | 57.8 | 54.3 | 29.1 | 76.8 | 86.0 | 71.9 | 32.4 | 93.2 | 54.3 | 53.5 | 96.7 | 67.3 | 65.5 | 83.4
(+6.0) | (+9.5) | (+10.8) | (+16.2) | (+12.6) | (-12.0) | (+10.9) | (+4.9) | (+12.3) | (-34.2) | (+7.8) | (-3.3) | (-0.7) | (+2.4) | (+7.7) | (+1.9) | (+1.3)
ViT-B/16 | 39.1 | 89.3 | 91.6 | 68.7 | 46.0 | 54.1 | 70.4 | 89.2 | 68.6 | 56.0 | 88.9 | 48.1 | 65.5 | 98.2 | 65.2 | 69.8 | 83.9
BASIC-M | 49.4 | 94.2 | 94.8 | 72.2 | 60.2 | 39.5 | 86.0 | 92.3 | 81.5 | 33.6 | 95.3 | 58.3 | 65.4 | 99.3 | 72.9 | 77.4 | 84.2
(+10.3) | (+4.9) | (+3.2) | (+3.5) | (+14.2) | (-14.6) | (+15.6) | (+3.1) | (+12.9) | (-22.4) | (+6.4) | (+10.2) | (-0.1) | (+1.1) | (+7.7) | (+7.6) | (+0.3)
ViT-L/14-336 | 49.5 | 92.8 | 95.7 | 77.5 | 55.7 | 59.6 | 78.3 | 93.8 | 76.2 | 88.3 | 93.5 | 63.0 | 71.7 | 99.4 | 68.4 | 76.9 | 84.3
BASIC-L | 59.2 | 94.7 | 97.5 | 82.3 | 64.6 | 51.0 | 91.2 | 95.1 | 85.7 | 40.3 | 97.9 | 59.6 | 72.7 | 99.6 | 76.2 | 84.8 | 84.6
(+9.7) | (+1.9) | (+1.8) | (+4.8) | (+8.9) | (-8.6) | (+13.1) | (+1.3) | (+9.5) | (-48.0) | (+4.4) | (-3.4) | (+1.0) | (+0.2) | (+7.8) | (+7.9) | (+0.3)

Table 3: Performances of BASIC and CLIP models (Radford et al., 2021) on 17
image classification benchmarks. The first two blocks compare models of
similar numbers of weights and FLOPs. The last block compares the largest CLIP
and BASIC models.

##### Other hyperparameters.

For all experiments, we train and evaluate with the image resolution of
224x224. While we can increase this resolution to gain performance (Tan and
Le, 2019, 2021; Touvron et al., 2019; Radford et al., 2021; Jia et al., 2021),
we choose not to do this and instead, reserve our computational resources for
scaling up our model and our batch size. All of our other hyper-parameters can
be found in Appendix B.

###  9.2 Results on Image Classification Benchmarks

We first present the zero-shot transfer performance of our BASIC models. We
compare our models BASIC-{S,M,L} to CLIP models with similar computational
budgets (Radford et al., 2021) on 17 natural image classification datasets.
Details about these datasets can be found in Appendix C.

Zero-shot transfer models require textual prompts, which we take from CLIP
(Radford et al., 2021) for consistent comparison. We suspect that using
prompts which are tuned for our models can further improve our results as
shown in (Lester et al., 2021), because the text sequences in our training
data have a different distribution from the text sequences in CLIP.

Table 3 shows the comparison. From the table, it can be seen that BASIC models
conclusively outperform CLIP models of the same computational budgets.
Specifically, BASIC models demonstrate higher accuracy than CLIP models on 13
out of 17 datasets. On the Oxford IIIT Pets dataset, BASIC-L achieves 97.9%
mean per-class recall which sets a new state-of-the-art, despite having never
seen any training images from the dataset. On ther other hand, BASIC models
have low accuracy on EuroSAT, MNIST, and PCam. MNIST is where BASIC models
perform worst, where the highest accuracy is only 40.3%. We discuss these
failure cases further in Section 11.

###  9.3 Results on Robustness Benchmarks

Despite the convincing accuracy of modern deep learning models on ImageNet,
concerns have been raised about their robustness (Szegedy et al., 2013). These
concerns arise from a common failure mode of ImageNet-trained models: subtle
changes to their input images, which are imperceptible to humans, can wildly
alter their predictions with high confidence, e.g., from “golden retriever”
into “goldfish”.

In CLIP, Radford et al. (2021) have studied certain aspects of this failure
mode. They have not drawn a definitive conclusion whether to attribute such
failures to deep learning, ImageNet, or a combination of them. Instead, they
cautioned against generalizing “too far from [their] initial findings”.

Here we advance CLIP’s study on the robustness of zero-shot models in two
aspects. First, we analyze our BASIC models presented previously in Section
9.2 and reaffirm that zero-shot models are indeed more robust than their
ImageNet-trained counterparts. Second, we perform an experiment which suggests
that ImageNet’s labeled training examples might be responsible for making
ImageNet-trained models less robust. Similar to CLIP’s authors, we caution
readers that our experiment presents a correlation, not a causal analysis. In
other words, we do not attribute the lack of robustness in ImageNet-trained
models to the dataset.

Figure 3: Top-1 accuracy on ImageNet vs. average top-1 accuracy on 5
robustness benchmarks. Zero-shot models (red stars and yellow rhombuses) have
significantly higher effective robustness (Taori et al., 2020) compared to
ImageNet-trained models (blue dots).

##### More accurate zero-shot transfer models are also more robust.

We evaluate BASIC-{S,M,L} models from Section 9.2 on 5 robustness benchmarks
derived from ImageNet: ImageNet-A (Hendrycks et al., 2021b), ImageNet-R
(Hendrycks et al., 2021a), ImageNet-V2 (Recht et al., 2019), ImageNet-Sketch
(Wang et al., 2019), and ObjectNet (Barbu et al., 2019). These benchmarks have
images in all or a subset of the 1000 ImageNet classes, but their inputs are
selected from certain natural distribution shifts, which can cause ImageNet-
trained models to make many more mistakes. Our numerical results are
highlighted in Table 1 from Section 1. To visualize the data trend, in Figure
3, we plot the accuracy of zero-shot models – BASIC, CLIP (Radford et al.,
2021), and ALIGN (Jia et al., 2021) – and of 200 ImageNet-trained models
collected by Taori et al. (2020).

The data points from our BASIC models extend the prediction from CLIP: zero-
shot transfer models have a higher effective robustness (Radford et al., 2021;
Taori et al., 2020), i.e. they have higher robustness than ImageNet-trained
models with the same ImageNet accuracy. To extrapolate from this trend, we fit
a logistic curve (red dashes) to the zero-shot accuracy and robustness of
zero-shot transfer models. The plot shows that this line meets the ideal
robustness line at about 91% on the x-coordinate. In other words, our plot
predicts that a model which achieves about 91% zero-shot accuracy on ImageNet,
i.e., just slightly better than the state-of-the-art ImageNet-trained model
(Dai et al., 2021), will also achieve the ideal robustness.

Figure 4: Top-1 accuracy of BASIC models on ImageNet and on 5 robustness
benchmarks. In all cases, as the BASIC models are trained on more ImageNet
labeled data (1%, 10%, 20%, and 50%), their ImageNet accuracy significantly
increase, but their accuracy on the robustness benchmarks increase much less,
or decrease.

##### ImageNet-finetuned models are less robust.

We now study the effect of ImageNet’s labeled data on our models. We take the
converged BASIC-{S,M,L} checkpoints from Section 9.2 and continue to train
them on 1%, 10%, 20%, and 50% of ImageNet’s labeled examples. Note that we
continue training these checkpoints using the contrastive loss, where the
names of ImageNet classes are utilized as text sequences accompanying their
images. This is different from CLIP’s linear probing approach, which we do not
perform to avoid potential confounding factors from our study, e.g. linear
classifiers might behave differently from our zero-shot transfer classifiers.
We then compare the accuracy of these finetuned models on ImageNet and on the
5 robustness benchmarks. The results are visualized in Figure 4.

The figure shows a clear trend: as our model learns from more labeled ImageNet
data, they become more accurate on ImageNet, but these gains do not carry over
to the robustness benchmarks. Specifically, with the exception of ImageNet-V2,
for which the accuracy of finetuned models stay the same (for BASIC-L) or
slightly increase (for BASIC-M), for all other robustness benchmarks, the
finetuned models suffer from significant performance drops. In the extreme
case, 3% accuracy gain on ImageNet leads to 8.3% accuracy drop for ImageNet-R.

What makes our finetuned models less robust? A quick glance at our results
might lead to the superficial conclusion that our models have overfit, as our
finetuning sets are a lot smaller than ALIGN and JFT. However, this
overfitting theory does not explain the trend observed in Figure 4: training
on more labeled ImageNet data makes our models less robust. We hope our
observation invites further causal analysis on the effects of ImageNet’s
labeled data.

##  10 Ablation Study

###  10.1 The Importance of Batch Size Scaling

To demonstrate the role of large batch sizes, we conduct several controlled
experiments for BASIC-S and BASIC-M on ALIGN. For both BASIC-S and BASIC-M, we
fix all hyperparameters as shown in Table 6, but vary the batch size and the
number of training steps. Models that are trained with larger batch sizes are
trained with fewer steps to guarantee that they “see” the same number of
examples. Table 4 presents the ImageNet top-1 zero-shot accuracy of all models
at the end of their training, and Figure 5 visualizes their entire validation
accuracy curves.

Figure 5: ImageNet held-out validation accuracy curves with different batch
sizes. Models with smaller batch sizes are trained for more steps to ensure a
fair comparison. The comparison shows that despite seeing the same number of
training examples, models with larger batch sizes reach higher performances
than models with more training steps. Image best viewed in color.

Batch size | Steps | BASIC-S | BASIC-M
---|---|---|---
4096 | 800K | 55.6 | 64.8
8192 | 400K | 57.6 | 67.7
16384 | 200K | 58.8 | 69.4
32768 | 100K | 59.3 | 70.1

Table 4: Top-1 ImageNet accuracy at the end of the training for our
BASIC-{S,M} models trained with different batch sizes and numbers of training
steps. All models are trained for the same number of epochs, but models
trained with larger batch sizes has a higher accuracy.

Table 4 and Figure 5 both suggest that training for more steps cannot equalize
the benefit of large batch sizes. This phenomenon is consistent with the
observation from SimCLR (Chen et al., 2020a, b): large batch sizes help
contrastive learning. SimCLR observes that the benefit of large batch sizes
saturate at 8192. In contrast, our results in Table 4 and Figure 5 show that
lager batch sizes continue to benefit our models until 32768, and even until
65536 as in Section 9.2. We suspect that the benefits for large batch sizes do
not saturate because our dataset size and model size are both larger than
those of SimCLR, e.g. ALIGN with 1.7B examples compared to ImageNet with 1M
examples, and BASIC-{S, M} compared to ResNet-{50,101,152}. This comparison
suggests the benefits of our method – combined scaling.

###  10.2 Data Scaling, Model Scaling, and Pretraining

Figure 6: Break-down contributions of data scaling and model scaling for
BASIC-S and BASIC-M. Shown are the ImageNet top-1 accuracy of our BASIC-{S,M}
models under different training settings. Models trained from scratch on
ALIGN+JFT has almost the same performance with models pretrained on JFT and
then finetuned on ALIGN or on ALIGN+JFT. Models that are pretrained and then
have both their image and text encoders finetuned reach the highest accuracy.
Figure best viewed in color.

We now study the benefits of other scaling dimensions, data and model scaling,
on the quality of our models. We also study pretraining as an alternate
training procedure to contrastive learning. We train BASIC-{S,M} models in 6
different settings and plot their final top-1 ImageNet accuracy in Figure 6.
Below, we compare and analyze the settings.

First, BASIC-S and BASIC-M respectively gain 5.3% and 5.8% accuracy when we
expand the contrastive training dataset from ALIGN to ALIGN+JFT. These gains,
albeit large, are smaller than the gain by enlarging the model size, e.g.,
11.7% when going from BASIC-S to BASIC-M.

Next, we study the effects of pretraining image encoders on JFT. As can be
seen from Figure 6, models whose image encoders are pretrained on JFT and
whose text encoders are subsequently trained on ALIGN, i.e., the red bars,
have similar performances with models trained from scratch on ALIGN+JFT, i.e.,
the blue bars. Their similar accuracy suggest that the training losses –
softmax cross-entropy or contrastive – have a much smaller effect than the
datasets. In other words, when given the same dataset, the image encoders in
BASIC models learn to become equally good, regardless of their loss functions.

To our surprise, training the text encoders for JFT-pretrained image encoders
on ALIGN+JFT gains 1% for BASIC-S and 1.8% for BASIC-L, compared to training
these text encoders on ALIGN. We suspect that these gains come from better
representations for the textual prompts, since the models trained on ALIGN+JFT
also sees the textual prompts which consist of clean JFT class names. However,
this speculation needs a more thorough study to understand.

Finally, we find that if we take a converged model whose image encoder is
pretrained on JFT and whose text encoder is trained on ALIGN+JFT, then we
continue to train both its image encoders and text encoders at a small
learning rate. This extra training phase gains us 1.4% ImageNet accuracy for
BASIC-S, 0.6% for BASIC-M, and 0.4% for BASIC-L (not shown in this section).

##  11 Limitations

Despite the strong results of our zero-shot transfer classifier, especially on
natural image classification tasks, they inevitably have their shortcomings.
In this section, we discuss the problems that we find with our BASIC models.

##### Zero-shot transfer models do not perform well on test sets that are
underrepresented in the training datasets.

We emphasize the failures of BASIC on two test sets where BASIC models are
much worse than CLIP models: EuroSAT, MNIST, PatchCamelyon (PCam) (see Table 3
from Section 9.2). Here, we summarize that BASIC models fail on MNIST and PCam
because our training datasets ALIGN and JFT have relatively few images of
handwritten digits and of lymph nodes, which are the domain of these datasets.
Compared to MNIST and PCam, BASIC models do better on EuroSAT which consist of
satellite land images, but their accuracy is lower than that of CLIP models.
This is because the class names for these satellite images are not very
descriptive to BASIC models. More analysis for these failures are in Appendix
G.

##### Zero-shot transfer requires prompt engineering.

In this paper, we use the prompts from CLIP (Radford et al., 2021) to make our
results comparable to previous works. In Appendix G, we present examples which
show that prompts that are badly chosen or adversarially chosen can hurt the
accuracy of zero-shot transfer models by flipping their predictions. These
examples suggest that prompt engineering is an important research topic to
make zero-shot transfer models robust and reliable, but the topic is of out of
the scope of this paper.

##### Combined scaling is expensive.

As reported in Appendix E, the hardware and training time for our models are
not small. Despite the training cost, we can use the models in this paper
without any finetuning, and hence avoid the finetuning cost. We hope that
future research can reduce our models’ training expense, e.g., larger
accelerator memory can save the extra re-materialization steps.

##  12 Conclusion

Zero-shot transfer learning represents a new paradigm where pretrained models
can be used directly for downstream applications without collecting any
application-specific data. However, in order to become practical for real-
world applications, zero-shot transfer models need to bridge the accuracy gap
to supervised and semi-supervised models.

In this paper, we presented combined scaling techniques that significantly
boost the performance of zero-shot transfer models. We show that scaling in
the data size, the model size, and the batch size all improves the final
model’s accuracy and robustness. To overcome the memory limit arising from
combined scaling, we devise a simple gradient accumulation method based on re-
materialization.

## References

  * Abadi et al. (2016) Martín Abadi, Paul Barham, Jianmin Chen, Zhifeng Chen, Andy Davis, Jeffrey Dean, Matthieu Devin, Sanjay Ghemawat, Geoffrey Irving, Michael Isard, et al.  Tensorflow: A system for large-scale machine learning.  In _OSDI_ , 2016.
  * Akata et al. (2015a) Zeynep Akata, Florent Perronnin, Zaid Harchaoui, and Cordelia Schmid.  Label-embedding for image classification.  _IEEE TPAMI_ , 2015a.
  * Akata et al. (2015b) Zeynep Akata, Scott Reed, Daniel Walter, Honglak Lee, and Bernt Schiele.  Evaluation of output embeddings for fine-grained image classification.  In _CVPR_ , 2015b.
  * Barbu et al. (2019) Andrei Barbu, David Mayo, Julian Alverio, William Luo, Christopher Wang, Dan Gutfreund, Josh Tenenbaum, and Boris Katz.  Objectnet: A large-scale bias-controlled dataset for pushing the limits of object recognition models.  In _NeurIPS_ , 2019.
  * Bartlett and Mendelson (2002) Peter L Bartlett and Shahar Mendelson.  Rademacher and gaussian complexities: Risk bounds and structural results.  _Journal of Machine Learning Research_ , 3(Nov):463–482, 2002.
  * Bello et al. (2021) Irwan Bello, William Fedus, Xianzhi Du, Ekin D Cubuk, Aravind Srinivas, Tsung-Yi Lin, Jonathon Shlens, and Barret Zoph.  Revisiting resnets: Improved training and scaling strategies.  In _NeurIPS_ , 2021.
  * Berg et al. (2014) Thomas Berg, Jiongxin Liu, Seung Woo Lee, Michelle L Alexander, David W Jacobs, and Peter N Belhumeur.  Birdsnap: Large-scale fine-grained visual categorization of birds.  _CVPR_ , 2014.
  * Bossard et al. (2014) Lukas Bossard, Matthieu Guillaumin, and Luc Van Gool.  Food-101–mining discriminative components with random forests.  _ECCV_ , 2014.
  * Brock et al. (2021) Andy Brock, Soham De, Samuel L Smith, and Karen Simonyan.  High-performance large-scale image recognition without normalization.  In _International Conference on Machine Learning_ , pages 1059–1071. PMLR, 2021.
  * Chen et al. (2021) Jiacheng Chen, Hexiang Hu, Hao Wu, Yuning Jiang, and Changhu Wang.  Learning the best pooling strategy for visual semantic embedding.  In _CVPR_ , 2021.
  * Chen et al. (2016) Tianqi Chen, Bing Xu, Chiyuan Zhang, and Carlos Guestrin.  Training deep nets with sublinear memory cost.  _Arxiv 1604.06174_ , 2016.
  * Chen et al. (2020a) Ting Chen, Simon Kornblith, Mohammad Norouzi, and Geoffrey E. Hinton.  A simple framework for contrastive learning of visual representations.  In _ICML_ , 2020a.
  * Chen et al. (2020b) Ting Chen, Simon Kornblith, Kevin Swersky, Mohammad Norouzi, and Geoffrey Hinton.  Big self-supervised models are strong semi-supervised learners.  In _NIPS_ , 2020b.
  * Chen et al. (2020c) Xinlei Chen, Haoqi Fan, Ross Girshick, and Kaiming He.  Improved baselines with momentum contrastive learning.  _ArXiv 2003.04297_ , 2020c.
  * Chen et al. (2020d) Yen-Chun Chen, Linjie Li, Licheng Yu, Ahmed El Kholy, Faisal Ahmed, Zhe Gan, Yu Cheng, and Jingjing Liu.  Uniter: Universal image-text representation learning.  In _ECCV_ , 2020d.
  * Cheng et al. (2017) Gong Cheng, Junwei Han, and Xiaoqiang Lu.  Remote sensing image scene classification: Benchmark and state of the art.  _Proceedings of the IEEE_ , 2017.
  * Cimpoi et al. (2014) Mircea Cimpoi, Subhransu Maji, Iasonas Kokkinos, Sammy Mohamed, and Andrea Vedald.  Describing textures in the wild.  In _CVPR_ , 2014.
  * Coates et al. (2011) Adam Coates, Andrew Ng, and Honglak Lee.  An Analysis of Single Layer Networks in Unsupervised Feature Learning.  In _AISTATS_ , 2011.
  * Dai et al. (2021) Zihang Dai, Hanxiao Liu, Quoc V. Le, and Mingxing Tan.  Coatnet: Marrying convolution and attention for all data sizes.  In _NeurIPS_ , 2021.
  * Desai and Johnson (2021) Karan Desai and Justin Johnson.  Virtex: Learning visual representations from textual annotations.  In _CVPR_ , 2021.
  * Devlin et al. (2018) Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova.  Bert: Pre-training of deep bidirectional transformers for language understanding.  In _NAACL_ , 2018.
  * Dosovitskiy et al. (2021) Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, and Neil Houlsby.  An image is worth 16x16 words: Transformers for image recognition at scale.  In _ICLR_ , 2021.
  * (23) M. Everingham, L. Van Gool, C. K. I. Williams, J. Winn, and A. Zisserman.  The PASCAL Visual Object Classes Challenge 2007 (VOC2007) Results.
  * Faghri et al. (2017) Fartash Faghri, David J Fleet, Jamie Ryan Kiros, and Sanja Fidler.  Vse++: Improving visual-semantic embeddings with hard negatives.  _BMVC_ , 2017.
  * Fei-Fei et al. (2004) Li Fei-Fei, Fergus Rob, and Pietro Perona.  Learning generative visual models from few training examples: An incremental bayesian approach tested on 101 object categories.  In _CVPR_ , 2004.
  * Frome et al. (2013) Andrea Frome, Greg Corrado, Jonathon Shlens, Samy Bengio, Jeffrey Dean, Marc’Aurelio Ranzato, and Tomas Mikolov.  Devise: A deep visual-semantic embedding model.  In _Advances in Neural Information Processing Systems_ , 2013.
  * Golowich et al. (2018) Noah Golowich, Alexander Rakhlin, and Ohad Shamir.  Size-independent sample complexity of neural networks.  In _Conference On Learning Theory_ , pages 297–299. PMLR, 2018.
  * Goyal et al. (2017) Priya Goyal, Piotr Dollár, Ross B. Girshick, Pieter Noordhuis, Lukasz Wesolowski, Aapo Kyrola, Andrew Tulloch, Yangqing Jia, and Kaiming He.  Accurate, large minibatch SGD: training imagenet in 1 hour.  _Arxiv 1706.02677_ , 2017.
  * Griewank and Walther (2000) Andreas Griewank and Andrea Walther.  Algorithm 799: revolve: an implementation of checkpointing for the reverse or adjoint mode of computational differentiation.  _ACM Transactions on Mathematical Software (TOMS)_ , 26(1):19–45, 2000.
  * He et al. (2015) Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun.  Deep residual learning for image recognition.  In _CVPR_ , 2015.
  * He et al. (2020) Kaiming He, Haoqi Fan, Yuxin Wu, Saining Xie, and Ross B. Girshick.  Momentum contrast for unsupervised visual representation learning.  In _CVPR_ , 2020.
  * Helber et al. (2018) Patrick Helber, Benjamin Bischke, Andreas Dengel, and Damian Borth.  Introducing eurosat: A novel dataset and deep learning benchmark for land use and land cover classification.  In _IEEE International Geoscience and Remote Sensing Symposium_ , 2018\.
  * Hendrycks et al. (2021a) Dan Hendrycks, Steven Basart, Norman Mu, Saurav Kadavath, Frank Wang, Evan Dorundo, Rahul Desai, Tyler Zhu, Samyak Parajuli, Mike Guo, Dawn Song, Jacob Steinhardt, and Justin Gilmer.  The many faces of robustness: A critical analysis of out-of-distribution generalization.  _ICCV_ , 2021a.
  * Hendrycks et al. (2021b) Dan Hendrycks, Kevin Zhao, Steven Basart, Jacob Steinhardt, and Dawn Song.  Natural adversarial examples.  _CVPR_ , 2021b.
  * Hironobu et al. (1999) Yasuhide Mori Hironobu, Hironobu Takahashi, and Ryuichi Oka.  Image-to-word transformation based on dividing and vector quantizing images with words.  In _Citeseer_ , 1999.
  * Hodosh et al. (2013) Micah Hodosh, Peter Young, and Julia Hockenmaier.  Framing image description as a ranking task: Data, models and evaluation metrics.  _Journal of Artificial Intelligence Research_ , 2013.
  * Hu et al. (2018) Jie Hu, Li Shen, and Gang Sun.  Squeeze-and-excitation networks.  In _Proceedings of the IEEE conference on computer vision and pattern recognition_ , pages 7132–7141, 2018.
  * Huang et al. (2017) Gao Huang, Yu Sun, Zhuang Liu, Daniel Sedra, and Kilian Weinberger.  Deep networks with stochastic depth.  In _BMVC_ , 2017.
  * Huang et al. (2019) Yanping Huang, Youlong Cheng, Ankur Bapna, Orhan Firat, Dehao Chen, Mia Chen, HyoukJoong Lee, Jiquan Ngiam, Quoc V Le, Yonghui Wu, et al.  Gpipe: Efficient training of giant neural networks using pipeline parallelism.  _Advances in neural information processing systems_ , 32:103–112, 2019.
  * Huang et al. (2020) Zhicheng Huang, Zhaoyang Zeng, Bei Liu, Dongmei Fu, and Jianlong Fu.  Pixel-bert: Aligning image pixels with text by deep multi-modal transformers.  _Arxiv 2004.00849_ , 2020.
  * Jain et al. (2020) Paras Jain, Ajay Jain, Aniruddha Nrusimha, Amir Gholami, Pieter Abbeel, Joseph Gonzalez, Kurt Keutzer, and Ion Stoica.  Checkmate: Breaking the memory wall with optimal tensor rematerialization.  _Proceedings of Machine Learning and Systems_ , 2:497–511, 2020.
  * Jia et al. (2021) Chao Jia, Yinfei Yang, Ye Xia, Yi-Ting Chen, Zarana Parekh, Hieu Pham, Quoc V. Le, Yunhsuan Sung, Zhen Li, and Tom Duerig.  Scaling up visual and vision-language representation learning with noisy text supervision.  In _ICML_ , 2021.
  * Joulin et al. (2016) Armand Joulin, Laurens Van Der Maaten, Allan Jabri, and Nicolas Vasilache.  Learning visual features from large weakly supervised data.  In _ECCV_ , 2016.
  * Jouppi et al. (2017) Norman P. Jouppi, Cliff Young, Nishant Patil, David A. Patterson, Gaurav Agrawal, Raminder Bajwa, Sarah Bates, Suresh Bhatia, Nan Boden, Al Borchers, Rick Boyle, Pierre-luc Cantin, Clifford Chao, Chris Clark, Jeremy Coriell, Mike Daley, Matt Dau, Jeffrey Dean, Ben Gelb, Tara Vazir Ghaemmaghami, Rajendra Gottipati, William Gulland, Robert Hagmann, Richard C. Ho, Doug Hogberg, John Hu, Robert Hundt, Dan Hurt, Julian Ibarz, Aaron Jaffey, Alek Jaworski, Alexander Kaplan, Harshit Khaitan, Andy Koch, Naveen Kumar, Steve Lacy, James Laudon, James Law, Diemthu Le, Chris Leary, Zhuyuan Liu, Kyle Lucke, Alan Lundin, Gordon MacKean, Adriana Maggiore, Maire Mahony, Kieran Miller, Rahul Nagarajan, Ravi Narayanaswami, Ray Ni, Kathy Nix, Thomas Norrie, Mark Omernick, Narayana Penukonda, Andy Phelps, Jonathan Ross, Amir Salek, Emad Samadiani, Chris Severn, Gregory Sizikov, Matthew Snelham, Jed Souter, Dan Steinberg, Andy Swing, Mercedes Tan, Gregory Thorson, Bo Tian, Horia Toma, Erick Tuttle, Vijay .  In-datacenter performance analysis of a tensor processing unit.  _Arxiv 1704.04760_ , 2017.
  * Karpathy and Fei-Fei (2015) Andrej Karpathy and Li Fei-Fei.  Deep visual-semantic alignments for generating image descriptions.  In _CVPR_ , 2015.
  * Kingma and Ba (2015) Diederik P. Kingma and Jimmy Ba.  Adam: A method for stochastic optimization.  In _ICLR_ , 2015.
  * Kiros et al. (2014) Ryan Kiros, Ruslan Salakhutdinov, and Richard S Zemel.  Unifying visual-semantic embeddings with multimodal neural language models.  _Arxiv 1411.2539_ , 2014.
  * Kolesnikov et al. (2020) Alexander Kolesnikov, Lucas Beyer, Xiaohua Zhai, Joan Puigcerver, Jessica Yung, Sylvain Gelly, and Neil Houlsby.  Big transfer (bit): General visual representation learning.  In _ECCV_ , 2020.
  * Krizhevsky (2009) Alex Krizhevsky.  Learning multiple layers of features from tiny images.  Technical report, 2009.
  * Kudo and Richardson (2018) Taku Kudo and John Richardson.  Sentencepiece: A simple and language independent subword tokenizer and detokenizer for neural text processing.  In _EMNLP_ , 2018.
  * Kumar et al. (2019) Ravi Kumar, Manish Purohit, Zoya Svitkina, Erik Vee, and Joshua Wang.  Efficient rematerialization for deep networks.  _Advances in Neural Information Processing Systems_ , 32, 2019.
  * Larochelle et al. (2008) Hugo Larochelle, Dumitru Erhan, and Yoshua Bengio.  Zero-data learning of new tasks.  In _AAAI_ , 2008.
  * LeCun et al. (2010) Yann LeCun, Corinna Cortes, and CJ Burges.  Mnist handwritten digit database.  _ATT Labs [Online]. Available: http://yann.lecun.com/exdb/mnist_ , 2, 2010.
  * Lepikhin et al. (2020) Dmitry Lepikhin, HyoukJoong Lee, Yuanzhong Xu, Dehao Chen, Orhan Firat, Yanping Huang, Maxim Krikun, Noam Shazeer, and Zhifeng Chen.  Gshard: Scaling giant models with conditional computation and automatic sharding.  _Arxiv 2006.16668_ , 2020.
  * Lester et al. (2021) Brian Lester, Rami Al-Rfou, and Noah Constant.  The power of scale for parameter-efficient prompt tuning.  In _EMNLP_ , 2021.
  * Li et al. (2017) Ang Li, Allan Jabri, Armand Joulin, and Laurens van der Maaten.  Learning visual n-grams from web data.  In _ICCV_ , 2017.
  * Li et al. (2019) Kunpeng Li, Yulun Zhang, Kai Li, Yuanyuan Li, and Yun Fu.  Visual semantic reasoning for image-text matching.  In _ICCV_ , 2019.
  * Liu et al. (2019) Fenglin Liu, Yuanxin Liu, Xuancheng Ren, Xiaodong He, and Xu Sun.  Aligning visual regions and textual concepts for semantic-grounded image representations.  _Arxiv 1905.06139_ , 2019.
  * Loshchilov and Hutter (2019) Ilya Loshchilov and Frank Hutter.  Decoupled weight decay regularization.  In _ICLR_ , 2019.
  * Lu et al. (2019) Jiasen Lu, Dhruv Batra, Devi Parikh, and Stefan Lee.  Vilbert: Pretraining task-agnostic visiolinguistic representations for vision-and-language tasks.  _NeurIPS_ , 2019.
  * Mahajan et al. (2018) Dhruv Mahajan, Ross B. Girshick, Vignesh Ramanathan, Kaiming He, Manohar Paluri, Yixuan Li, Ashwin Bharambe, and Laurens van der Maaten.  Exploring the limits of weakly supervised pretraining.  In _ECCV_ , 2018.
  * Maurer (2016) Andreas Maurer.  A vector-contraction inequality for rademacher complexities.  In _International Conference on Algorithmic Learning Theory_ , pages 3–17. Springer, 2016.
  * Messina et al. (2020) Nicola Messina, Giuseppe Amato, Andrea Esuli, Fabrizio Falchi, Claudio Gennaro, and Stéphane Marchand-Maillet.  Fine-grained visual textual alignment for cross-modal retrieval using transformer encoders.  _ACM Transactions on Multimedia Computing, Communications, and Applications_ , 2020.
  * Mohri et al. (2012) Mehryar Mohri, Afshin Rostamizadeh, and Ameet Talwalkar.  _Foundations of machine learning_.  MIT press, 2012.
  * Nam et al. (2017) Hyeonseob Nam, Jung-Woo Ha, and Jeonghee Kim.  Dual attention networks for multimodal reasoning and matching.  In _CVPR_ , 2017.
  * Nesterov (1983) Yurii E. Nesterov.  A method for solving the convex programming problem with convergence rate $o(1/k^{2})$.  _Soviet Mathematics Doklady_ , 1983.
  * Nilsback and Zisserman (2008) Maria-Elena Nilsback and Andrew Zisserman.  Automated flower classification over a large number of classes.  In _Indian Conference on Computer Vision, Graphics and Image Processing_ , 2008.
  * Norouzi et al. (2013) Mohammad Norouzi, Tomas Mikolov, Samy Bengio, Yoram Singer, Jonathon Shlens, Andrea Frome, Greg S Corrado, and Jeffrey Dean.  Zero-shot learning by convex combination of semantic embeddings.  _Arxiv 1312.5650_ , 2013.
  * Ott et al. (2018) Myle Ott, Sergey Edunov, David Grangier, and Michael Auli.  Scaling neural machine translation.  In _Workshop in Neural Machine Translation_ , 2018.
  * Parkhi et al. (2012) Omkar M Parkhi, Andrea Vedaldi, Andrew Zisserman, and CV Jawahar.  Cats and dogs.  _CVPR_ , 2012.
  * Pham et al. (2021) Hieu Pham, Zihang Dai, Qizhe Xie, Minh-Thang Luong, and Quoc V. Le.  Meta pseudo labels.  In _CVPR_ , 2021.
  * Radford et al. (2021) Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, and Ilya Sutskever.  Learning transferable visual models from natural language supervision.  In _ICML_ , 2021.
  * Recht et al. (2019) Benjamin Recht, Rebecca Roelofs, Ludwig Schmidt, and Vaishaal Shankar.  Do imagenet classifiers generalize to imagenet?  In _ICML_ , 2019.
  * Russakovsky et al. (2009) Olga Russakovsky, Jia Deng, Hao Su, Jonathan Krause, Sanjeev Satheesh, Sean Ma, Zhiheng Huang, Andrej Karpathy, Aditya Khosla, Michael Bernstein, Alexander C. Berg, and Li Fei-Fei.  ImageNet Large Scale Visual Recognition Challenge.  _IJCV_ , 2009.
  * Sariyildiz et al. (2020) Mert Bulent Sariyildiz, Julien Perez, and Diane Larlus.  Learning visual representations with caption annotations.  In _ECCV_ , 2020.
  * Schönfeld et al. (2019) Edgar Schönfeld, Sayna Ebrahimi, Samarth Sinha, Trevor Darrell, and Zeynep Akata.  Generalized zero- and few-shot learning via aligned variational autoencoders.  In _CVPR_ , 2019.
  * Shalev-Shwartz and Ben-David (2014) Shai Shalev-Shwartz and Shai Ben-David.  _Understanding machine learning: From theory to algorithms_.  Cambridge university press, 2014.
  * Shazeer and Stern (2018) Noam Shazeer and Mitchell Stern.  Adafactor: Adaptive learning rates with sublinear memory cost.  _Arxiv 1804.04235_ , 2018.
  * Shazeer et al. (2018) Noam Shazeer, Youlong Cheng, Niki Parmar, Dustin Tran, Ashish Vaswani, Penporn Koanantakool, Peter Hawkins, HyoukJoong Lee, Mingsheng Hong, Cliff Young, et al.  Mesh-tensorflow: Deep learning for supercomputers.  _Arxiv 1811.02084_ , 2018.
  * Socher and Fei-Fei (2010) Richard Socher and Li Fei-Fei.  Connecting modalities: Semi-supervised segmentation and annotation of images using unaligned text corpora.  In _CVPR_ , 2010.
  * Socher et al. (2013) Richard Socher, Milind Ganjoo, Hamsa Sridhar, Osbert Bastani, Christopher D Manning, and Andrew Y Ng.  Zero-shot learning through cross-modal transfer.  In _Advances in Neural Information Processing Systems_ , 2013.
  * Socher et al. (2014) Richard Socher, Andrej Karpathy, Quoc V. Le, Christopher D. Manning, and Andrew Y. Ng.  Grounded compositional semantics for finding and describing images with sentences.  _TACL_ , 2014.
  * Soomro et al. (2012) Khurram Soomro, Amir Roshan Zamir, and Mubarak Shah.  UCF101: A dataset of 101 human actions classes from videos in the wild.  _Arxiv 1212.0402_ , 2012.
  * Srivastava et al. (2014) Nitish Srivastava, Geoffrey Hinton, Alex Krizhevsky, Ilya Sutskever, and Ruslan Salakhutdinov.  Dropout: A simple way to prevent neural networks from overfitting.  In _JMLR_ , 2014.
  * Sun et al. (2017) Chen Sun, Abhinav Shrivastava, Saurabh Singh, and Abhinav Gupta.  Revisiting unreasonable effectiveness of data in deep learning era.  In _ICCV_ , 2017.
  * Szegedy et al. (2013) Christian Szegedy, Wojciech Zaremba, Ilya Sutskever, Joan Bruna, Dumitru Erhan, Ian Goodfellow, and Rob Fergus.  Intriguing properties of neural networks.  _Arxiv 1312.6199_ , 2013.
  * Tan and Le (2019) Mingxing Tan and Quoc V. Le.  Efficientnet: Rethinking model scaling for convolutional neural networks.  In _ICML_ , 2019.
  * Tan and Le (2021) Mingxing Tan and Quoc V. Le.  Efficientnetv2: Smaller models and faster training.  In _ICML_ , 2021.
  * Tan et al. (2020) Mingxing Tan, Ruoming Pang, and Quoc V. Le.  Efficientdet: Scalable and efficient object detection.  In _CVPR_ , 2020.
  * Taori et al. (2020) Rohan Taori, Achal Dave, Vaishaal Shankar, Nicholas Carlini, Benjamin Recht, and Ludwig Schmidt.  Measuring robustness to natural distribution shifts in image classification.  In _NeurIPS_ , 2020.
  * Tian et al. (2020) Yonglong Tian, Chen Sun, Ben Poole, Dilip Krishnan, Cordelia Schmid, and Phillip Isola.  What makes for good views for contrastive learning?  In _NeurIPS_ , 2020.
  * Tieleman and Hinton (2012) Tijmen Tieleman and Geoffrey Hinton.  RmsProp: Divide the gradient by a running average of its recent magnitude.  COURSERA: Neural Networks for Machine Learning, 2012.
  * Touvron et al. (2019) Hugo Touvron, Andrea Vedaldi, Matthijs Douze, and Hervé Jégou.  Fixing the train-test resolution discrepancy.  In _NeurIPS_ , 2019.
  * van den Oord et al. (2018) Aäron van den Oord, Yazhe Li, and Oriol Vinyals.  Representation learning with contrastive predictive coding.  _Arxiv 1807.03748_ , 2018.
  * Vaswani et al. (2017) Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin.  Attention is all you need.  In _Advances in neural information processing systems_ , 2017.
  * Veeling et al. (2018) Bastiaan S Veeling, Jasper Linmans, Jim Winkens, Taco Cohen, and Max Welling.  Rotation equivariant CNNs for digital pathology.  _Medical Image Computing and Computer Assisted Intervention_ , 2018\.
  * Vinyals et al. (2015) Oriol Vinyals, Alexander Toshev, Samy Bengio, and Dumitru Erhan.  Show and tell: A neural image caption generator.  In _CVPR_ , 2015.
  * Wang et al. (2019) Haohan Wang, Songwei Ge, Zachary Lipton, and Eric P Xing.  Learning robust global representations by penalizing local predictive power.  In _NeurIPS_ , 2019.
  * Wang et al. (2004) Zhou Wang, Alan C Bovik, Hamid R Sheikh, and Eero P Simoncelli.  Image quality assessment: from error visibility to structural similarity.  _IEEE transactions on image processing_ , 13(4):600–612, 2004.
  * Weston et al. (2010) Jason Weston, Samy Bengio, and Nicolas Usunier.  Large scale image annotation: learning to rank with joint word-image embeddings.  _Machine Learning_ , 2010.
  * Xian et al. (2016) Yongqin Xian, Zeynep Akata, Gaurav Sharma, Quynh N. Nguyen, Matthias Hein, and Bernt Schiele.  Latent embeddings for zero-shot classification.  In _CVPR_ , 2016.
  * Xian et al. (2017) Yongqin Xian, Bernt Schiele, and Zeynep Akata.  Zero-shot learning - the good, the bad and the ugly.  In _CVPR_ , 2017.
  * Xiao et al. (2010) Jianxiong Xiao, James Hays, Krista A. Ehinger, Aude Oliva, and Antonio Torralba.  Sun database: Large-scale scene recognition from abbey to zoo.  In _CVPR_ , 2010.
  * Xie et al. (2020) Qizhe Xie, Minh-Thang Luong, Eduard Hovy, and Quoc V Le.  Self-training with noisy student improves imagenet classification.  In _CVPR_ , 2020.
  * Xu et al. (2015) Kelvin Xu, Jimmy Ba, Ryan Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhudinov, Rich Zemel, and Yoshua Bengio.  Show, attend and tell: Neural image caption generation with visual attention.  In _ICML_ , 2015.
  * Xu et al. (2021) Yuanzhong Xu, HyoukJoong Lee, Dehao Chen, Blake Hechtman, Yanping Huang, Rahul Joshi, Maxim Krikun, Dmitry Lepikhin, Andy Ly, Marcello Maggioni, et al.  Gspmd: General and scalable parallelization for ml computation graphs.  _arXiv preprint arXiv:2105.04663_ , 2021.
  * Yuan et al. (2021) Li Yuan, Qibin Hou, Zihang Jiang, Jiashi Feng, and Shuicheng Yan.  Volo: Vision outlooker for visual recognition.  _ArXiv 2106.13112_ , 2021.
  * Zhai et al. (2021) Xiaohua Zhai, Alexander Kolesnikov, Neil Houlsby, and Lucas Beyer.  Scaling vision transformers.  In _Arxiv 2106.04560_ , 2021.
  * Zhai et al. (2022) Xiaohua Zhai, Xiao Wang, Basil Mustafa, Andreas Steiner, Daniel Keysers, Alexander Kolesnikov, and Lucas Beyer.  Lit: Zero-shot transfer with locked-image text tuning.  In _Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition_ , pages 18123–18133, 2022.
  * Zhang et al. (2017) Li Zhang, Tao Xiang, and Shaogang Gong.  Learning a deep embedding model for zero-shot learning.  In _CVPR_ , 2017.
  * Zhang et al. (2020) Yuhao Zhang, Hang Jiang, Yasuhide Miura, Christopher D Manning, and Curtis P Langlotz.  Contrastive learning of medical visual representations from paired images and text.  _Arxiv 2010.00747_ , 2020.

##  Appendix A Model sizes

In our preliminary experiments, we experimented with different model sizes.
Table 5 presents the final, most compute-to-performance efficient model sizes,
which we use throughout the paper.

| Image model | Text model
---|---|---
| Model (Dai et al., 2021) | #Params | #FLOPs | #Layers | HiddenDim | HeadDim | #Params | #FLOPs
BASIC-S | CoAtNet-0 | 25M | 4.2B | 6 | 1024 | 64 | 108M | 10.7B
BASIC-M | CoAtNet-3 | 168M | 34.7B | 12 | 1024 | 128 | 184M | 49.4B
BASIC-L | CoAtNet-7 | 2.4B | 495.8B | 12 | 2048 | 128 | 670M | 212.6B

Table 5: Model sizes. For the image models, all specifications can be found
from the model names in Dai et al. (2021).

##  Appendix B Hyperparameters and other implementation details

Our training and evaluation code will eventually be released. Here, we
summarize a few important details. All of our hyper-parameters are in Table 6.

##### No regularization.

Other than the decoupled weight decay in AdaFactorW, we do not use any other
regularization technique. In fact, we find that with BASIC-S and BASIC-M, if
we add other forms of regularization such as stochastic depth (Huang et al.,
2017) or dropout (Srivastava et al., 2014), our ImageNet top-1 accuracy drops
substantially. This suggests that our datasets are very large and perhaps in
such situation, regularization techniques do more harm than good by causing
optimization difficulty to our models.

Another important effect of not using regularization in our training framework
is to make the re-materialization steps in Section 4.2 consistent. If we apply
random perturbations to our forward passes, e.g. by skipping layers like in
stochastic depth or by setting random values to zeros, then two forward passes
for re-materialization (see Lines 2-5 and 11-14 in Algorithm 1) will compute
two different passes. While we could treat such difference as a form of
regularization noise, our early experiment show that with dropout-like
regularizations, our training loss stays relatively large throughout the
course of training. This observation suggests that the noise causes some
optimization difficulty to our models, so we opt not to use any dropout-like
regularization.

| BASIC-S | BASIC-{M,L}
---|---|---
| Pretraining | Contrastive | Pretraining | Contrastive
Optimizer | AdaFactorW | AdaFactorW | AdaFactorW | AdaFactorW
Batch size | 16384 | 65536 | 16384 | 65536
Training steps | 500K | 500K | 1.2M | 500K
Warm-up steps | 25K | 25K | 25K | 25K
Max learning rate | 1e-3 | 1e-3 | 4e-4 | 2.5e-4
Min learning rate | 1e-5 | 1e-5 | 2e-5 | 1e-5
Learning decay schedule | Cosine | Cosine | Linear | Cosine
Weight decay | 0.005 | 0.0025 | 0.01 | 0.0025

Table 6: Hyperparameters all of our experiments.

##  Appendix C Evaluation Datasets Details

Here, we present the details of the datasets which we use to evaluate our
BASIC models in Section 9.2. It is worth noting that not all these datasets
use the accuracy as the performance metric. This is because these datasets
have a certain level of imbalance between their classes, as well as other
properties that make them accuracy not the best suitable metric for them. For
instance, the dataset Caltech-101 has a class called “Background” which refers
to any image that does not belong to its predefined 101 classes. One certainly
cannot come up with a textual description that describes this “class”. As
such, Caltech-101 is evaluated using mean per-class recall. Details about
other datasets are in Table 7.

Dataset | Reference | Abbreviation in Table 3 | #Classes | Test size | Evaluation metric
---|---|---|---|---|---
ILSVRC-2012, i.e., ImageNet | (Russakovsky et al., 2009) | ImageNet | 1000 | 50000 | accuracy
ImageNet-A | (Hendrycks et al., 2021b) | N/A | 1000 | 7500 | accuracy
ImageNet-R | (Hendrycks et al., 2021a) | N/A | 1000 | 30000 | accuracy
ImageNet-V2 | (Recht et al., 2019) | N/A | 1000 | 10000 | accuracy
ImageNet-Sketch | (Wang et al., 2019) | N/A | 1000 | 50889 | accuracy
ObjectNet | (Barbu et al., 2019) | N/A | 1000 | 18574 | accuracy
CIFAR-10 | (Krizhevsky, 2009) | CIFAR10 | 10 | 10000 | accuracy
CIFAR-100 | (Krizhevsky, 2009) | CIFAR100 | 100 | 10000 | accuracy
Birdsnap | (Berg et al., 2014) | Birdsnap | 500 | 2443 | accuracy
Describable Textures | (Cimpoi et al., 2014) | DTD | 47 | 1880 | accuracy
Oxford Flowers-102 | (Nilsback and Zisserman, 2008) | Flowers | 102 | 6149 | mean per-class recall
Food-101 | (Bossard et al., 2014) | Food101 | 101 | 25250 | accuracy
Caltech101 | (Fei-Fei et al., 2004) | Caltech101 | 102 | 6084 | mean per-class recall
Oxford IIIT-Pets | (Parkhi et al., 2012) | IIIT-Pets | 37 | 3669 | mean per-class recall
MNIST | (LeCun et al., 2010) | MNIST | 10 | 10000 | accuracy
EuroSAT | (Helber et al., 2018) | EuroSAT | 10 | 27000 | accuracy
PatchCamelyon | (Veeling et al., 2018) | PCam | 2 | 32768 | accuracy
RESICS45 | (Cheng et al., 2017) | RESICS45 | 45 | 31500 | accuracy
STL10 | (Coates et al., 2011) | STL10 | 10 | 8000 | accuracy
SUN397 | (Xiao et al., 2010) | SUN397 | 397 | 21750 | accuracy
UCF101 | (Soomro et al., 2012) | UCF101 | 101 | 3783 | accuracy
Pascal VOC 2007 Classification | (Everingham et al., ) | VOC2007 | 20 | 4952 | 11-points mAP

Table 7: Details of the datasets used in this paper to evaluate BASIC models.
The evaluation results are presented in Table 1 and Table 3.

##  Appendix D Further Discussion on Robustness

In Section 9.3, we present a surprising result: finetuning converged BASIC
checkpoints on more ImageNet labeled data leads to worse robustness results.
The metric for robustness in Section 9.3 is the average top-1 accuracy of the
finetuned models on 5 robustness benchmarks derived from ImageNet (Hendrycks
et al., 2021b, a; Recht et al., 2019; Barbu et al., 2019; Wang et al., 2019).
It turns out that each of these benchmarks can demonstrate slightly different
results for the finetuned models. Here, we discuss such benchmarks.

##### ImageNet-V2 (Recht et al., 2019).

This dataset is collected in a process that closely follows the process to
collect and annotate the images in the standard ILSVRC-2012 validation set,
which is typically referred to as “ImageNet” in the literature (and our paper
as well). As such, gains observed on ImageNet often transfer to ImageNet-V2.
Recent works such as EfficientNets (Tan and Le, 2019, 2021) or ViT
(Dosovitskiy et al., 2021) also demonstrate the similar trend. For our
experiment in Section 9.3, BASIC-M’s robustness accuracy improves along with
its ImageNet accuracy, following this trend. However, BASIC-L’s robustness
does not. We suspect this trend is because BASIC-L’s learning capacity is
larger than that of BASIC-M, so BASIC-L picks up more “spurious” patterns from
ImageNet, making it less robust than BASIC-M.

##### ImageNet-R (Wang et al., 2019).

ImageNet-R is a special robustness dataset in our study. Not only of our BASIC
models but also other zero-shot models – CLIP and ALIGN – are more accurate on
ImageNet-R than they are on ImageNet (see Table 1). These data points alone
would suggest that ImageNet-R is somewhat easier than ImageNet, until we look
at the significant accuracy drops for other methods on ImageNet-R. For
instance, Noisy Student (Xie et al., 2020) and Meta Pseudo Labels (Pham et
al., 2021) respectively achieve only 74.9% and 72.7% accuracy on ImageNet-R,
despite their accuracy of 88.4% and 90.2% on ImageNet ILSVRC-2012. The real
reason for such discrepancy in ImageNet-R is that ImageNet-R is collected by
selecting the ImageNet classes from visual art pieces, such as paintings,
cartoons, graffiti, origami, and sculptures. These art pieces are often
displayed in a clean environment, free of noises such as multiple classes per
image, making the images easier to recognize. As such, BASIC, CLIP, and ALIGN,
all perform better on ImageNet-R. However, ImageNet-R images have a
drastically different distribution compared to ImageNet labeled training
images, as they are respectively art images and natural images. This is why
ImageNet-trained models display a much lower accuracy on ImageNet, compared to
zero-shot models.

##### The case of ObjectNet (Barbu et al., 2019).

From Table 1, it can be seen that BASIC model’s improvement over ALIGN and
CLIP on Object is significantly lower than others on other benchmarks, i.e.,
6.6% compared to more than 8% (except for ImageNet-R, for which the accuracy
of all models are saturated at over 90%). We find out the reason is that, even
though ObjectNet has images from the same classes with ImageNet, these objects
turn out to have their own more descriptive names, e.g. the class name
“chairs” in ImageNet could be “chairs by [viewpoint]” or “chairs with
[background]”. As we later show in Section G, using different class names and
prompts can affect our results. This effect has also been observed in CLIP
(Radford et al., 2021). Here, we take the same class names and prompts for
ImageNet and use them for ObjectNet. We suspect that using ObjectNet-specific
class names and prompts can improve our result.

##  Appendix E Computational Cost

All of our models are implemented in TensorFlow (Abadi et al., 2016) and
trained on Tensor Processing Units (TPUs (Jouppi et al., 2017)). Our BASIC-S
and BASIC-M models are all trained on TPUv3 chips, while our BASIC-L models
are trained on TPUv4 chips. These TPUv4 chips in their MegaCore mode can offer
32GB of memory, out of which our BASIC-L models use 30.1GB, which means that
our model essentially saturates the TPU’s memory. We note that oftentimes, a
small portion of TPU memory needs to be reserved for their low-level infra
systems. Therefore, our BASIC-L models essentially saturate the accelerators
with the largest memory currently available. Given this memory usage, we use
Algorithm 1 with the microbatch size $M=8192$ and the batch size $N=65536$ to
train this model. Table 8 summarizes the training cost for each phase of our
models BASIC-{S,M,L} as in Section 9.2.

Model | Pretraining | Text Encoder | Text & Image Encoders
---|---|---|---
Type | Cores$\times$Days | Type | Cores$\times$Days | Type | Cores$\times$Days
BASIC-S | TPUv3 | 0.4K | TPUv3 | 0.9K | TPUv3 | 0.3K
BASIC-M | TPUv3 | 1.7K | TPUv3 | 3.9K | TPUv3 | 1.2K
BASIC-L | TPUv4 | 6.9K | TPUv4 | 1.0K | TPUv4 | 0.8K

Table 8: Computational usages to train our models. Core$\times$Days is the
product of the number of training days and the number of cores used to train
the models. For instance, using 2048 TPUs in 1 day equals to 2.048
Cores$\times$Days. We use this metric because sometimes, our jobs are run on
different numbers of TPUs due to limited availability.

##  Appendix F Qualitative Analysis: Successful Classification Examples

Zero-shot transfer models open the door to versatile applications. This
section is dedicated to demonstrating their versatility. In Figure 7, we
visualize some predictions of our best model, BASIC-L, on instances that are
less expected on traditional image classification benchmarks. We come up with
the text sequences and demonstrate that the model can indeed align images to
the most appropriate sequence.

_Eggs with mixed expressions_. (✓) | $0.944$
---|---
_Emojis with mixed expressions_. | $0.032$
_Happy eggs_. | $0.022$
_Sad eggs_. | $0.002$
_Happy emojis_. | <1e-3
_Sad emojis_. | <1e-3

_One plus one equals three_. (✓) | $0.468$
---|---
_One plus one equals one_. | $0.264$
_One plus one equals two_. | $0.240$
_One minus one equals three_. | $0.014$
_One minus one equals two_. | <1e-3
_One minus one equals one_. | <1e-3

_Cosplayed pikachu_. (✓) | $0.764$
---|---
_Cosplayed charmander_. | $0.219$
_Real pikachu_. | $0.012$
_Cosplayed eevee_. | $0.006$
_Real charmander_. | <1e-3
_Real eevee_. | <1e-3

_Chemistry equation on a whiteboard_. (✓) | $0.958$
---|---
_Math equation on a whiteboard_. | $0.024$
_Physics equation on a whiteboard_. | $0.012$
_Chemistry equation on a paper_. | $0.005$
_Math equation on a paper_. | <1e-3
_Physics equation on a paper_. | <1e-3

_A shirari dog in cold weather_. (✓) | $0.961$
---|---
_A shirari dog in warm weather_. | $0.038$
_A corgi dog in cold weather_. | <1e-3
_A shiba inu dog in cold weather_. | <1e-3
_A corgi dog in warm weather_. | <1e-3
_A shiba inu dog in warm weather_. | <1e-3

_An alarm clock that reads 7:00 pm_. (✓) | $0.288$
---|---
_An alarm clock that reads 10:00 am_. | $0.183$
_An alarm clock that reads 12:00 pm_. | $0.167$
_An alarm clock that reads 4:00 pm_. | $0.162$
_An alarm clock that reads 7:00 am_. | $0.133$
_An alarm clock that reads 2:00 pm_. | $0.067$

Figure 7: Selected classification examples from BASIC-L over unseen images.

##  Appendix G Failure Analysis

Most machine learning models fail in certain tests. It is important to
identify such failure cases, to understand the failing causes, and if
possible, to come up with fixes. Here, we first look at the test benchmarks in
Table 3 from Section 9.2 where BASIC models perform worse than CLIP models. We
identify the cause of failures for BASIC models and recommend certain fixes
that can improve their performance. Then, in Section G.2, we present some
erroneous behaviors of BASIC models via selected examples. These examples
reveal some weaknesses of zero-shot transfer models, and invite future
research to improve them.

###  G.1 The benchmarks where BASIC fails

From Section 9.2, we see that BASIC models have particularly low performance
on EuroSat (Helber et al., 2018), MNIST (LeCun et al., 2010), and Patch
Camelyon (Veeling et al., 2018). The accuracy of BASIC-L on these datasets are
51.0%, 40.3%, and 59.6% respectively. For what it’s worth, BASIC-L’s accuracy
are better than those of our smaller models, i.e., BASIC-S and BASIC-M, so our
central message in this paper – scaling helps – is not altered. Here, we focus
on analyzing the failures of BASIC-L.

##### Patch Camelyon (PCam).

PCam is perhaps the most sensitive dataset among the three benchmarks where
BASIC-L performs poorly. This dataset consists of images extracted from
histopathologic scans of lymph node sections, and models are asked to make the
binary prediction – whether an input image has a cancerous lymph node or note.
For such an important task, the top-1 accuracy of both BASIC-L (59.6%) and
CLIP (63.0%) are far below the bars for practical deployments. We remark that
PCam is a binary classification task, so the accuracy of BASIC-L and CLIP are
just slightly above random guessing. Their poor performance, however, are
quite understandable: classifying lymph nodes requires much more specific
training, compared to classifying common natural images. As our training data
are weakly crawled and automatically curated from the internet, without any
emphasis on medical images, our BASIC-L model cannot learn enough to perform
well on PCam. We suspect the same speculation also holds for CLIP, as their
data collection and curation process is comparable to ours. Finally, the low
accuracy of CLIP and BASIC models on PCam is an assertion that despite the
benefits of zero-shot transfer models, they are not ready to be deployed to
tasks that require in-domain expertise, e.g. medical knowledge.

##### EuroSAT.

This dataset consists of satellite images taken for certain types of lands.
Models are asked to classify input images into one out of 10 given types of
lands. The land types can be seen in Figure 8. The failure of BASIC-L on
EuroSAT is an example for the importance of prompt engineering in zero-shot
transfer learning for image-text models. In Figure 8, we show that by changing
the dataset’s class names and the model’s set of prompts, into words and
phrases that essentially have the same meaning to humans, we can improve the
accuracy of BASIC-L from 51.0% to 55.7%. We do not further explore the changes
in class names and prompts to improve BASIC-L’s performance on EuroSAT, as
they belong to a different topic from the focus of this paper – combined
scaling. However, our findings on this EuroSAT dataset suggests that
contrastive image-text models do not really “understand” texts. This is
perhaps because of the low quality of the texts in our training data, unlike
the millions of words from books and articles like the training data of NLP
models such as BERT (Devlin et al., 2018).

Prompts from CLIP (Radford et al., 2021) | Prompts we find
---|---
‘a centered satellite photo of {}.’, | ‘a centered satellite photo of {}’
‘a centered satellite photo of a {}.’, | ‘a satellite photo of {}’
‘a centered satellite photo of the {}.’, | ‘a photo of {} taken from a satellite’
| ‘a photo of {} taken from the sky’
| ‘a picture of {} taken from the sky by a satellite’
| ‘a picture of {} taken by a satellite’
| ‘a picture of {} taken by a satellite in space’
| ‘a picture of {} taken by a satellite in its orbit’

Figure 8: Confusion matrices of BASIC-L on the EuroSAT classification dataset
(Helber et al., 2018). Shown are the confusion matrices obtained from zero-
shot transfer learning from BASIC-L, using prompts and class names and CLIP,
compared to the same model using prompts and class names that we tuned. The
zero-shot top-1 accuracy with our prompts and class names are 4.7% higher, and
the confusion matrix illustrates this by showing more concentration on the
diagonal.

##### MNIST.

MNIST is a classical dataset in computer vision for handwritten digit
classification. Simple models can achieve more than 99.5% accuracy, and yet
BASIC-L achieves the humble 40.3% accuracy. Unlike the case of PCam, i.e.
there is not enough training data in our training dataset, for MNIST, we find
that the ALIGN dataset has a fair amount of images that contain digits, either
handwritten or printed. This means that the image encoder of BASIC-L has seen
digit figures, and suggests that the failures might be more attributable to
the text encoder, similar to the case of EuroSAT. In Figure 9, we show the
confusion matrices of BASIC-L models with three sets of class names: using the
digits such as {‘0’, ‘1’, …}, using the the texts such as {‘one’, ‘two‘’, …},
and using both such as {‘0 or zero’, ‘1 or one’, …}. Unfortunately, we cannot
improve BASIC-L’s accuracy on MNIST, like we did for EuroSAT: BASIC-L’s
accuracy is low in all three cases, but the confusion matrices are visibly
different: BASIC-L models ‘thinks’ that many digits look like ‘3’ for the
digit-only class names, but many digits look like ‘1 or one’ in the digit-and-
text class names. Again, humans who understand languages will not make these
mistakes. We think these mistakes constitute a new type or robustness
failures, which we hope will invite further research.

Figure 9: Confusion matrices of BASIC-L’s predictions on MNIST. Digit only: we
use the class names {“0”, “1”, …, “9”}; Text only: {“one”, “two”, …, “nine”};
Digit and Text: {“0 or zero”, “1 or one”, …, “9 or nine”}. The model has
vastly different confusion matrices for different class name, suggesting that
it does not understand the meaning of these strings, but instead, simply
learns to match their embeddings.

###  G.2 Example failure cases

From the confusion matrices of BASIC-L on two benchmarks, EuroSAT (Helber et
al., 2018) and MNIST (LeCun et al., 2010), we observe that the prompts and
class names are crucial for the performance of zero-shot transfor models.
Here, we select and present a few examples to demonstrate the failures of
BASIC-L. Figure 10 visualizes these examples.

_More than 6 kittens in total_. (✗) | $0.472$
---|---
_More than 4 kittens in total_. | $0.342$
_More than 2 kittens in total_. | $0.186$
_More than 6 puppies in total_. | <1e-3
_More than 2 puppies in total_. | <1e-3
_More than 4 puppies in total_. | <1e-3

_No strawberries found in the photo_. (✗) | $0.393$
---|---
_No blueberries found in the photo_. | $0.304$
_No bananas found in the photo_. | $0.297$
_No coconuts found in the photo_. | $0.003$
_No pineapples found in the photo_. | $0.002$
_No oranges found in the photo_. | $0.001$

_Closed road_. (✗) | $0.716$
---|---
_Slippery road_. | $0.170$
_Intersection_. | $0.076$
_Stop_. | $0.034$
_Sharp left_. | $0.003$
_Sharp right_. | $0.002$

_Traffic sign indicating intersection_. (✓) | $0.927$
---|---
_Traffic sign indicating closed road_. | $0.027$
_Traffic sign indicating sharp left_. | $0.027$
_Traffic sign indicating sharp right_. | $0.016$
_Traffic sign indicating slippery road_. | $0.003$
_Traffic sign indicating stop_. | <1e-3

_red light saber_. (✓) | 0.992
---|---
_blue light saber_. | 0.005
_red led light_. | 0.002
_red neon light_. | <1e-3
_blue led light_. | <1e-3
_blue neon light_. | <1e-3

_blue light saber_. (✓) | 0.990
---|---
_red light saber_. | 0.004
_blue led light_. | 0.003
_blue neon light_. | 0.003
_red led light_. | <1e-3
_red neon light_. | <1e-3

_a blue light saber on the left and a red light saber on the right_. (✗) | $0.297$
---|---
_a blue light saber on the right and a red light saber on the left_. | $0.235$
_a red light saber on the left and a blue light saber on the right_. | $0.205$
_a red light saber on the right and a blue light saber on the left_. | $0.173$
_a red light saber to the right of a blue light saber_. | $0.054$
_a red light saber to the left of a blue light saber_. | $0.0342$

Figure 10: Selected failure cases for BASIC-L over unseen images. (1) The
first block indicates that the model is not precise in object counting and
does not well handle negation in the prompts, possibly due the nature of our
training data. (2) The middle block shows two examples to indicate that prompt
engineering can play a critical role in providing the model with sufficient
context to produce the desired output. (3) The last block shows that the model
does not have the sense of left and right, which is a relic of random left-
right flips of images which we apply during training.

##  Appendix H Proofs

In this appendix, we complete the proof of Theorem 1 and Theorem 2 by
gradually analyzing the gap from the general case to the special case.

###  H.1 General case

###### Lemma 3

Let $\mathcal{F}$ be a set of maps $x\mapsto F(x)$ and $\mathcal{G}$ be a set
of maps $y\mapsto G(y)$. Then, for any $\delta>0$, with probability at least
$1-\delta$, the following holds for all $F\in\mathcal{F}$ and
$G\in\mathcal{G}$:

$\displaystyle\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]$
(5) $\displaystyle\leq
c_{1}\mathbb{E}_{x,y}[A(x,y)]\left(\frac{1}{\sqrt{2B}}\left(2\sqrt{2}c_{8}c_{9}+c_{1}\sqrt{\kappa\ln(\sqrt{\kappa
B}/\delta)}\right)+\frac{2}{c_{1}}\sup_{x\in\mathcal{X}}\mathcal{R}_{B}(\mathcal{H}_{\mathcal{F},\mathcal{G},e}^{x})\right)$
$\displaystyle\quad+2\mathcal{R}_{m}(\mathcal{H}_{\mathcal{F},\mathcal{G},\hat{\ell}_{B}})+c_{2}\sqrt{\frac{\ln(2/\delta)}{2m}}.$

where
$\mathcal{H}_{\mathcal{F},\mathcal{G},e}^{x}=\\{y\mapsto\exp(F(x)^{\top}G(y)):F\in\mathcal{F},G\in\mathcal{G}\\}$,
$\mathcal{H}_{\mathcal{F},\mathcal{G},\hat{\ell}_{B}}=\\{(x,y)\mapsto\hat{\ell}_{B}(x,y):F\in\mathcal{F},G\in\mathcal{G}\\}$,
and

$A(x,y)=\frac{\exp(F(x)^{\top}G(y))}{\left(\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))\right)\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]}.$

Here,
$\mathcal{R}_{m}(\mathcal{H}):=\mathbb{E}_{S,\sigma}[\sup_{h\in\mathcal{H}}\frac{1}{m}\sum_{i=1}^{m}\sigma_{i}h(x_{i},y_{i})]$
where $\sigma_{1},\dots,\sigma_{m}$ are independent uniform random variables
taking values in $\\{-1,1\\}$.

Proof  We first decompose the difference as follows:

$\displaystyle\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]$
$\displaystyle=\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)]-\mathbb{E}_{x,y}[\hat{\ell}_{B}(x,y)]+\mathbb{E}_{x,y}[\hat{\ell}_{B}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]$
(6)
$\displaystyle=\mathbb{E}_{x,y}[\bar{\ell}_{M}(x,y)-\hat{\ell}_{B}(x,y)]+(\mathbb{E}_{x,y}[\hat{\ell}_{B}(x,y)]-\hat{\mathbb{E}}_{S}[\hat{\ell}_{B}(x,y)]).$
(7)

For the inside of the expectation in the first term, we can write it as

$\displaystyle\bar{\ell}_{M}(x,y)-\hat{\ell}_{B}(x,y)$ (8)
$\displaystyle=\frac{\exp(F(x)^{\top}G(y))}{\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))}-\frac{\exp(F(x)^{\top}G(y))}{\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]}$
(9)
$\displaystyle=\frac{\exp(F(x)^{\top}G(y))\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]-\exp(F(x)^{\top}G(y))(\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k})))}{(\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k})))\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]}$
(10)
$\displaystyle=\frac{\exp(F(x)^{\top}G(y))\left(\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]-\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))\right)}{\left(\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))\right)\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(\bar{y}))]}$
(11)

Using Lemma 4 (see below) with the assumption that
$\hat{y}_{1},\hat{y}_{2},\dots,\hat{y}_{B}\stackrel{{\scriptstyle
iid}}{{\sim}}p_{y}$ and $\exp(F(x)^{\top}G(y))\leq c_{1}$ with probability
one, we have that for any $\delta>0$ and $x\in\mathcal{X}$, with probability
at least $1-\delta$, the following holds for all $F\in\mathcal{F}$ and
$G\in\mathcal{G}$:

$\displaystyle\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(y))]-\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))\leq
2\sup_{x\in\mathcal{X}}\mathcal{R}_{B}(\mathcal{H}_{\mathcal{F},\mathcal{G},e}^{x})+c_{1}\sqrt{\frac{\ln(1/\delta)}{2B}},$
(12)

where

$\sup_{x\in\mathcal{X}}\mathcal{R}_{B}(\mathcal{H}_{\mathcal{F},\mathcal{G},e}^{x})=\sup_{x\in\mathcal{X}}\mathbb{E}_{y,\sigma}\left[\sup_{h_{x}\in\mathcal{H}_{\mathcal{F},\mathcal{G},e}^{x}}\frac{1}{B}\sum_{i=1}^{B}\sigma_{i}h_{x}(y_{i})\right],$

and

$\mathcal{H}_{\mathcal{F},\mathcal{G},e}^{x}=\\{y\mapsto\exp(F(x)^{\top}G(y)):F\in\mathcal{F},G\in\mathcal{G}\\}.$

Recall that
$\gamma(x)=\mathbb{E}_{\bar{y}}[\exp(F(x)^{\top}G(y))]-\frac{1}{B}\sum_{k=1}^{B}\exp(F(x)^{\top}G(\hat{y}_{k}))$
is $c_{9}$-Lipschitz; i.e., $|\gamma(x)-\gamma(x^{\prime})|\leq
c_{9}\|x-x^{\prime}\|_{2}$ for all $x\in\mathcal{X}$ where $\|x\|_{2}\leq
c_{8}$ for all $\mathcal{X}$. For any metric space $(\mathcal{M},d)$ and
subset $M\subseteq\mathcal{M}$, the (closed) ball of radius $r$ at centered at
$c$ is denoted by
$\mathcal{B}_{(\mathcal{M},d)}[c,r]=\\{x\in\mathcal{M}:d(x,c)\leq r\\}$, and
the $r$-converging number of $M$ with the covering $\mathcal{C}$ is defined by

$\mathcal{N}_{\mathcal{C}}(r,M)=\min\left\\{|\mathcal{C}|:\mathcal{C}\subseteq\mathcal{M},M\subseteq\cup_{c\in\mathcal{C}}\mathcal{B}_{(\mathcal{M},d)}[c,r]\right.\\}.$

Let us choose the metric space $(\mathcal{M},d)$ to be the Euclidian space
$\mathbb{R}^{\kappa}$ with the Euclidian metric. That is, we have the
$\epsilon$-covering of $\mathcal{X}$ with the Euclidean balls of radius $r$,
with the $r$-converging number of

$\mathcal{N}_{\mathcal{C}}(r,\mathcal{X})\leq(2c_{8}\sqrt{\kappa}/r)^{\kappa}.$