Skip to content

Commit d62316d

Browse files
【昇腾AI创新大赛】 poolformer (#1158)
1 parent 23f418f commit d62316d

File tree

9 files changed

+1368
-0
lines changed

9 files changed

+1368
-0
lines changed

mindnlp/transformers/models/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
pegasus,
108108
phi,
109109
phi3,
110+
poolformer,
110111
pop2piano,
111112
qwen2,
112113
qwen2_moe,
@@ -228,6 +229,7 @@
228229
from .pegasus import *
229230
from .phi import *
230231
from .phi3 import *
232+
from .poolformer import *
231233
from .pop2piano import *
232234
from .qwen2 import *
233235
from .qwen2_moe import *
@@ -349,6 +351,7 @@
349351
__all__.extend(pegasus.__all__)
350352
__all__.extend(phi.__all__)
351353
__all__.extend(phi3.__all__)
354+
__all__.extend(poolformer.__all__)
352355
__all__.extend(pop2piano.__all__)
353356
__all__.extend(qwen2.__all__)
354357
__all__.extend(qwen2_moe.__all__)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2024 Huawei Technologies Co., Ltd
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ============================================
15+
"""
16+
PoolFormer Model init
17+
"""
18+
from .import configuration_poolformer, feature_extraction_poolformer, image_processing_poolformer, modeling_poolformer
19+
20+
from .configuration_poolformer import *
21+
from .feature_extraction_poolformer import *
22+
from .image_processing_poolformer import *
23+
from .modeling_poolformer import *
24+
25+
__all__ = []
26+
__all__.extend(configuration_poolformer.__all__)
27+
__all__.extend(feature_extraction_poolformer.__all__)
28+
__all__.extend(image_processing_poolformer.__all__)
29+
__all__.extend(modeling_poolformer.__all__)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Copyright 2024 Huawei Technologies Co., Ltd
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ============================================
15+
"""PoolFormer model configuration"""
16+
17+
from ...configuration_utils import PretrainedConfig
18+
19+
20+
class PoolFormerConfig(PretrainedConfig):
21+
r"""
22+
This is the configuration class to store the configuration of [`PoolFormerModel`]. It is used to instantiate a
23+
PoolFormer model according to the specified arguments, defining the model architecture. Instantiating a
24+
configuration with the defaults will yield a similar configuration to that of the PoolFormer
25+
[sail/poolformer_s12](https://huggingface.co/sail/poolformer_s12) architecture.
26+
27+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
28+
documentation from [`PretrainedConfig`] for more information.
29+
30+
31+
Args:
32+
num_channels (`int`, *optional*, defaults to 3):
33+
The number of channels in the input image.
34+
patch_size (`int`, *optional*, defaults to 16):
35+
The size of the input patch.
36+
stride (`int`, *optional*, defaults to 16):
37+
The stride of the input patch.
38+
pool_size (`int`, *optional*, defaults to 3):
39+
The size of the pooling window.
40+
mlp_ratio (`float`, *optional*, defaults to 4.0):
41+
The ratio of the number of channels in the output of the MLP to the number of channels in the input.
42+
depths (`list`, *optional*, defaults to `[2, 2, 6, 2]`):
43+
The depth of each encoder block.
44+
hidden_sizes (`list`, *optional*, defaults to `[64, 128, 320, 512]`):
45+
The hidden sizes of each encoder block.
46+
patch_sizes (`list`, *optional*, defaults to `[7, 3, 3, 3]`):
47+
The size of the input patch for each encoder block.
48+
strides (`list`, *optional*, defaults to `[4, 2, 2, 2]`):
49+
The stride of the input patch for each encoder block.
50+
padding (`list`, *optional*, defaults to `[2, 1, 1, 1]`):
51+
The padding of the input patch for each encoder block.
52+
num_encoder_blocks (`int`, *optional*, defaults to 4):
53+
The number of encoder blocks.
54+
drop_path_rate (`float`, *optional*, defaults to 0.0):
55+
The dropout rate for the dropout layers.
56+
hidden_act (`str`, *optional*, defaults to `"gelu"`):
57+
The activation function for the hidden layers.
58+
use_layer_scale (`bool`, *optional*, defaults to `True`):
59+
Whether to use layer scale.
60+
layer_scale_init_value (`float`, *optional*, defaults to 1e-05):
61+
The initial value for the layer scale.
62+
initializer_range (`float`, *optional*, defaults to 0.02):
63+
The initializer range for the weights.
64+
65+
Example:
66+
67+
```python
68+
>>> from transformers import PoolFormerConfig, PoolFormerModel
69+
70+
>>> # Initializing a PoolFormer sail/poolformer_s12 style configuration
71+
>>> configuration = PoolFormerConfig()
72+
73+
>>> # Initializing a model (with random weights) from the sail/poolformer_s12 style configuration
74+
>>> model = PoolFormerModel(configuration)
75+
76+
>>> # Accessing the model configuration
77+
>>> configuration = model.config
78+
```
79+
"""
80+
81+
model_type = "poolformer"
82+
83+
def __init__(
84+
self,
85+
num_channels=3,
86+
patch_size=16,
87+
stride=16,
88+
pool_size=3,
89+
mlp_ratio=4.0,
90+
depths=[2, 2, 6, 2],
91+
hidden_sizes=[64, 128, 320, 512],
92+
patch_sizes=[7, 3, 3, 3],
93+
strides=[4, 2, 2, 2],
94+
padding=[2, 1, 1, 1],
95+
num_encoder_blocks=4,
96+
drop_path_rate=0.0,
97+
hidden_act="gelu",
98+
use_layer_scale=True,
99+
layer_scale_init_value=1e-5,
100+
initializer_range=0.02,
101+
**kwargs,
102+
):
103+
self.num_channels = num_channels
104+
self.patch_size = patch_size
105+
self.stride = stride
106+
self.padding = padding
107+
self.pool_size = pool_size
108+
self.hidden_sizes = hidden_sizes
109+
self.mlp_ratio = mlp_ratio
110+
self.depths = depths
111+
self.patch_sizes = patch_sizes
112+
self.strides = strides
113+
self.num_encoder_blocks = num_encoder_blocks
114+
self.drop_path_rate = drop_path_rate
115+
self.hidden_act = hidden_act
116+
self.use_layer_scale = use_layer_scale
117+
self.layer_scale_init_value = layer_scale_init_value
118+
self.initializer_range = initializer_range
119+
super().__init__(**kwargs)
120+
121+
122+
__all__ = [
123+
"PoolFormerConfig",
124+
]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2024 Huawei Technologies Co., Ltd
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ============================================
15+
"""Feature extractor class for PoolFormer."""
16+
17+
import warnings
18+
19+
from .image_processing_poolformer import PoolFormerImageProcessor
20+
21+
22+
class PoolFormerFeatureExtractor(PoolFormerImageProcessor):
23+
def __init__(self, *args, **kwargs) -> None:
24+
warnings.warn(
25+
"The class PoolFormerFeatureExtractor is deprecated and will be removed in version 5 of Transformers."
26+
" Please use PoolFormerImageProcessor instead.",
27+
FutureWarning,
28+
)
29+
super().__init__(*args, **kwargs)
30+
31+
32+
__all__ = [
33+
"PoolFormerFeatureExtractor",
34+
]

0 commit comments

Comments
 (0)