Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PyTorch at JSC
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Simulation and Data Lab Applied Machine Learning
PyTorch at JSC
Commits
e90d4094
Commit
e90d4094
authored
1 month ago
by
Jan Ebert
Browse files
Options
Downloads
Patches
Plain Diff
Allow using Vision Transformer
parent
48ba71ec
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pytorch-fsdp-example/main.py
+52
-2
52 additions, 2 deletions
pytorch-fsdp-example/main.py
with
52 additions
and
2 deletions
pytorch-fsdp-example/main.py
+
52
−
2
View file @
e90d4094
...
...
@@ -73,6 +73,15 @@ def parse_args():
default
=
0
,
help
=
'
Random number generator initialization value.
'
,
)
parser
.
add_argument
(
'
--model-type
'
,
choices
=
[
'
resnet
'
,
'
vit
'
],
default
=
'
resnet
'
,
help
=
(
'
Which type of model to use
'
'
(
"
resnet
"
= ResNet-50,
"
vit
"
(ViT-B/32))
'
),
)
parser
.
add_argument
(
'
--num-fsdp-replicas
'
,
type
=
int
,
...
...
@@ -187,12 +196,37 @@ def all_reduce_avg(tensor):
return
result
def
build_model
():
def
build_model
(
args
):
"""
Return the model to train.
"""
if
args
.
model_type
==
'
resnet
'
:
model
=
build_resnet
()
elif
args
.
model_type
==
'
vit
'
:
model
=
build_vit
(
args
.
image_edge_size
)
else
:
raise
ValueError
(
f
'
unknown model type
"
{
args
.
model_type
}
"'
)
return
model
def
build_resnet
():
"""
Return a Residual Net model (ResNet-50).
"""
model
=
torchvision
.
models
.
resnet50
(
weights
=
None
)
return
model
def
build_vit
(
image_edge_size
):
"""
Return a Vision Transformer model (ViT-B/32).
"""
hidden_dim
=
768
model
=
torchvision
.
models
.
VisionTransformer
(
image_size
=
image_edge_size
,
patch_size
=
32
,
num_layers
=
12
,
num_heads
=
12
,
hidden_dim
=
hidden_dim
,
mlp_dim
=
hidden_dim
*
4
,
)
return
model
def
distribute_model
(
model
,
args
):
"""
Distribute the model across the different processes using Fully
Sharded Data Parallelism (FSDP).
...
...
@@ -213,6 +247,22 @@ def distribute_model(model, args):
sharding_strategy
=
fsdp
.
ShardingStrategy
.
HYBRID_SHARD
fsdp_mesh
=
device_mesh
.
init_device_mesh
(
"
cuda
"
,
fsdp_mesh_dims
)
if
args
.
model_type
==
'
resnet
'
:
# We could also use the `ModuleWrapPolicy` here, but this way we
# show a method that works with arbitrary models.
auto_wrap_policy
=
functools
.
partial
(
fsdp
.
wrap
.
size_based_auto_wrap_policy
,
# Wrap every 1B parameters.
min_num_params
=
int
(
1e9
),
)
elif
args
.
model_type
==
'
vit
'
:
# Each Transformer block becomes one FSDP unit.
auto_wrap_policy
=
fsdp
.
wrap
.
ModuleWrapPolicy
({
torchvision
.
models
.
vision_transformer
.
EncoderBlock
,
})
else
:
raise
ValueError
(
f
'
unknown model type
"
{
args
.
model_type
}
"'
)
model
=
fsdp
.
FullyShardedDataParallel
(
model
,
device_id
=
local_rank
,
...
...
@@ -330,7 +380,7 @@ def main():
train_dset
,
valid_dset
,
test_dset
=
prepare_datasets
(
args
,
device
)
model
=
build_model
()
model
=
build_model
(
args
)
model
=
distribute_model
(
model
)
loss_func
=
torch
.
nn
.
CrossEntropyLoss
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment