Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#1 from jerrywgz/master
Browse files Browse the repository at this point in the history
Add PaddleMIX code base
  • Loading branch information
jerrywgz authored Jul 5, 2023
2 parents 9f07434 + b8a016f commit ff661de
Show file tree
Hide file tree
Showing 75 changed files with 20,952 additions and 0 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
19 changes: 19 additions & 0 deletions paddlevlp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# copyright (c) 2023 paddlepaddle authors. all rights reserved.
# copyright 2023 the salesforce team authors and the huggingface team. all rights reserved.
#
# licensed under the apache license, version 2.0 (the "license");
# you may not use this file except in compliance with the license.
# you may obtain a copy of the license at
#
# http://www.apache.org/licenses/license-2.0
#
# unless required by applicable law or agreed to in writing, software
# distributed under the license is distributed on an "as is" basis,
# without warranties or conditions of any kind, either express or implied.
# see the license for the specific language governing permissions and
# limitations under the license.

from .datasets import *
from .models import *
from .optimization import *
from .processors import *
174 changes: 174 additions & 0 deletions paddlevlp/activations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import math
from collections import OrderedDict

import paddle
import paddle.nn.functional as F
from paddle import Tensor, nn


class NewGELUActivation(nn.Layer):
"""
Implementation of the GELU activation function currently in Google BERT repo (identical to OpenAI GPT). Also see
the Gaussian Error Linear Units paper: https://arxiv.org/abs/1606.08415
"""

def forward(self, input: Tensor) -> Tensor:
return (
0.5 * input * (1.0 + paddle.tanh(math.sqrt(2.0 / math.pi) * (input + 0.044715 * paddle.pow(input, 3.0))))
)


class GELUActivation(nn.Layer):
"""
Original Implementation of the GELU activation function in Google BERT repo when initially created. For
information: OpenAI GPT's GELU is slightly different (and gives slightly different results): 0.5 * x * (1 +
torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) This is now written in C in nn.functional
Also see the Gaussian Error Linear Units paper: https://arxiv.org/abs/1606.08415
"""

def __init__(self, use_gelu_python: bool = False):
super().__init__()
if use_gelu_python:
self.act = self._gelu_python
else:
self.act = nn.functional.gelu

def _gelu_python(self, input: Tensor) -> Tensor:
return input * 0.5 * (1.0 + paddle.erf(input / math.sqrt(2.0)))

def forward(self, input: Tensor) -> Tensor:
return self.act(input)


class FastGELUActivation(nn.Layer):
"""
Applies GELU approximation that is slower than QuickGELU but more accurate. See: https://github.com/hendrycks/GELUs
"""

def forward(self, input: Tensor) -> Tensor:
return 0.5 * input * (1.0 + paddle.tanh(input * 0.7978845608 * (1.0 + 0.044715 * input * input)))


class QuickGELUActivation(nn.Layer):
"""
Applies GELU approximation that is fast but somewhat inaccurate. See: https://github.com/hendrycks/GELUs
"""

def forward(self, input: Tensor) -> Tensor:
return input * F.sigmoid(1.702 * input)


class ClippedGELUActivation(nn.Layer):
"""
Clip the range of possible GeLU outputs between [min, max]. This is especially useful for quantization purpose, as
it allows mapping negatives values in the GeLU spectrum. For more information on this trick, please refer to
https://arxiv.org/abs/2004.09602.
Gaussian Error Linear Unit. Original Implementation of the gelu activation function in Google Bert repo when
initially created.
For information: OpenAI GPT's gelu is slightly different (and gives slightly different results): 0.5 * x * (1 +
torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))). See https://arxiv.org/abs/1606.08415
"""

def __init__(self, min: float, max: float):
if min > max:
raise ValueError(f"min should be < max (got min: {min}, max: {max})")

super().__init__()
self.min = min
self.max = max

def forward(self, x: Tensor) -> Tensor:
return paddle.clip(gelu(x), self.min, self.max)


class SiLUActivation(nn.Layer):
"""
See Gaussian Error Linear Units (Hendrycks et al., https://arxiv.org/abs/1606.08415) where the SiLU (Sigmoid Linear
Unit) was originally introduced and coined, and see Sigmoid-Weighted Linear Units for Neural Network Function
Approximation in Reinforcement Learning (Elfwing et al., https://arxiv.org/abs/1702.03118) and Swish: a Self-Gated
Activation Function (Ramachandran et al., https://arxiv.org/abs/1710.05941v1) where the SiLU was experimented with
later.
"""

def forward(self, input: Tensor) -> Tensor:
return F.silu(input)


class MishActivation(nn.Layer):
"""
See Mish: A Self-Regularized Non-Monotonic Activation Function (Misra., https://arxiv.org/abs/1908.08681). Also
visit the official repository for the paper: https://github.com/digantamisra98/Mish
"""

def forward(self, input: Tensor) -> Tensor:
return F.mish(input)


class LinearActivation(nn.Layer):
"""
Applies the linear activation function, i.e. forwarding input directly to output.
"""

def forward(self, input: Tensor) -> Tensor:
return input


class ClassInstantier(OrderedDict):
def __getitem__(self, key):
content = super().__getitem__(key)
cls, kwargs = content if isinstance(content, tuple) else (content, {})
return cls(**kwargs)


ACT2CLS = {
"gelu": GELUActivation,
"gelu_10": (ClippedGELUActivation, {"min": -10, "max": 10}),
"gelu_fast": FastGELUActivation,
"gelu_new": NewGELUActivation,
"gelu_python": (GELUActivation, {"use_gelu_python": True}),
"linear": LinearActivation,
"mish": MishActivation,
"quick_gelu": QuickGELUActivation,
"relu": nn.ReLU,
"relu6": nn.ReLU6,
"sigmoid": nn.Sigmoid,
"silu": SiLUActivation,
"swish": SiLUActivation,
"tanh": nn.Tanh,
}
ACT2FN = ClassInstantier(ACT2CLS)


def get_activation(activation_string):
if activation_string in ACT2FN:
return ACT2FN[activation_string]
else:
raise KeyError(f"function {activation_string} not found in ACT2FN mapping {list(ACT2FN.keys())}")


# For backwards compatibility with: from activations import gelu_python
gelu_python = get_activation("gelu_python")
gelu_new = get_activation("gelu_new")
gelu = get_activation("gelu")
gelu_fast = get_activation("gelu_fast")
quick_gelu = get_activation("quick_gelu")
silu = get_activation("silu")
mish = get_activation("mish")
linear_act = get_activation("linear")
17 changes: 17 additions & 0 deletions paddlevlp/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .caption_dataset import *
from .coco_caption import *
from .dataset import *
98 changes: 98 additions & 0 deletions paddlevlp/datasets/caption_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import json
import os

from paddlevlp.utils.env import DATA_HOME
from paddlevlp.utils.log import logger

from .dataset import DatasetBuilder

# from paddle.dataset.common import md5file
# from paddle.utils.download import get_path_from_url


__all__ = ["CaptionDataset"]


class CaptionDataset(DatasetBuilder):
"""
Caption dataset.
"""

URL = "https://bj.bcebos.com/paddlemix/datasets/coco.tar.gz"
META_INFO = collections.namedtuple(
"META_INFO", ("images", "annotations", "images_md5", "annotations_md5")
)
MD5 = ""
SPLITS = {
"train": META_INFO(
os.path.join("coco", "images"),
os.path.join("coco", "annotations/coco_karpathy_train_debug.json"),
"",
"aa31ac474cf6250ebb81d18348a07ed8",
),
"val": META_INFO(
os.path.join("coco", "images"),
os.path.join("coco", "annotations/coco_karpathy_val.json"),
"",
"b273847456ef5580e33713b1f7de52a0",
),
"test": META_INFO(
os.path.join("coco", "images"),
os.path.join("coco", "annotations/coco_karpathy_test.json"),
"",
"3ff34b0ef2db02d01c37399f6a2a6cd1",
),
}

def _get_data(self, mode, **kwargs):
# default_root = '/paddle/wangguanzhong/blip-jinman/PaddleNLP/blip2'
logger.info("default dataset root is {}".format(DATA_HOME))
images, annotations, image_hash, anno_hash = self.SPLITS[mode]
image_fullname = os.path.join(DATA_HOME, images)
anno_fullname = os.path.join(DATA_HOME, annotations)
# if (
# (not os.path.exists(src_fullname) or (src_data_hash and not md5file(src_fullname) == src_data_hash))
# or (not os.path.exists(tgt_fullname) or (tgt_data_hash and not md5file(tgt_fullname) == tgt_data_hash))
# or (not os.path.exists(vocab_fullname) or (vocab_hash and not md5file(vocab_fullname) == vocab_hash))
# ):
# get_path_from_url(self.URL, default_root, self.MD5)

return image_fullname, anno_fullname, mode

def _gen_image_id(self, anno):
img_ids = {}
n = 0
for ann in anno:
img_id = ann["image_id"]
if img_id not in img_ids.keys():
img_ids[img_id] = n
n += 1
return img_ids

def _read(self, filename, *args):
image_root, anno_path, mode = filename
annotations = json.load(open(anno_path, "r"))
image_ids = self._gen_image_id(annotations)

for ann in annotations:
image_path = os.path.join(image_root, ann["image"])
yield_data = {"image": image_path, "image_id": image_ids[ann["image_id"]]}
if mode == "train":
# only train mode has text input
yield_data["text_input"] = ann["caption"]
yield yield_data
17 changes: 17 additions & 0 deletions paddlevlp/datasets/coco_caption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddlevlp.datasets.caption_dataset import CaptionDataset

COCOCaption = CaptionDataset
Loading

0 comments on commit ff661de

Please sign in to comment.