Skip to content

Commit

Permalink
Replace python random with torch.rand to enable dynamo.export (#24434)
Browse files Browse the repository at this point in the history
* Replace python random with torch.rand to enable dynamo.export

* revert changes to flax model code

* Remove unused random import

* Fix torch template

* Move torch.manual_seed(0) to right location
  • Loading branch information
BowenBao authored Jun 23, 2023
1 parent c036c81 commit a28325e
Show file tree
Hide file tree
Showing 42 changed files with 71 additions and 104 deletions.
5 changes: 2 additions & 3 deletions src/transformers/models/autoformer/modeling_autoformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
""" PyTorch Autoformer model."""

import math
import random
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

Expand Down Expand Up @@ -1198,7 +1197,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1408,7 +1407,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/bart/modeling_bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
""" PyTorch BART model."""
import copy
import math
import random
import warnings
from typing import List, Optional, Tuple, Union

Expand Down Expand Up @@ -837,7 +836,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1090,7 +1089,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import copy
import math
import random
from typing import List, Optional, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -1933,7 +1932,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -2276,7 +2275,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
3 changes: 1 addition & 2 deletions src/transformers/models/biogpt/modeling_biogpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


import math
import random
from typing import Optional, Tuple, Union

import torch
Expand Down Expand Up @@ -579,7 +578,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/blenderbot/modeling_blenderbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import copy
import math
import os
import random
import warnings
from typing import List, Optional, Tuple, Union

Expand Down Expand Up @@ -767,7 +766,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1019,7 +1018,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import copy
import math
import random
from typing import List, Optional, Tuple, Union

import torch
Expand Down Expand Up @@ -765,7 +764,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1016,7 +1015,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


import math
import random
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple

Expand Down Expand Up @@ -1224,7 +1223,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1378,7 +1377,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue
if idx == 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def forward(
all_hidden_states = all_hidden_states + (hidden_states,)

# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = np.random.uniform(0, 1)
dropout_probability = torch.rand([])

skip_the_layer = True if self.training and (dropout_probability < self.config.layerdrop) else False
if not skip_the_layer or deepspeed_zero3_is_enabled:
Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/detr/modeling_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


import math
import random
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple

Expand Down Expand Up @@ -979,7 +978,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1118,7 +1117,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
3 changes: 1 addition & 2 deletions src/transformers/models/flaubert/modeling_flaubert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import itertools
import math
import random
from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Union

Expand Down Expand Up @@ -580,7 +579,7 @@ def forward(
attentions = () if output_attentions else None
for i in range(self.n_layers):
# LayerDrop
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/fsmt/modeling_fsmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"""PyTorch Fairseq model, ported from https://github.com/pytorch/fairseq/tree/master/examples/wmt19"""

import math
import random
from typing import Any, Dict, List, Optional, Tuple, Union

import torch
Expand Down Expand Up @@ -550,7 +549,7 @@ def forward(
encoder_states += (x,)
x = x.transpose(0, 1) # B x T x C -> T x B x C
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
attn = None
else:
Expand Down Expand Up @@ -794,7 +793,7 @@ def forward(
x = x.transpose(0, 1)
all_hidden_states += (x,)
x = x.transpose(0, 1)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
4 changes: 2 additions & 2 deletions src/transformers/models/hubert/modeling_hubert.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ def forward(
all_hidden_states = all_hidden_states + (hidden_states,)

# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = np.random.uniform(0, 1)
dropout_probability = torch.rand([])

skip_the_layer = True if self.training and (dropout_probability < self.config.layerdrop) else False
if not skip_the_layer or deepspeed_zero3_is_enabled:
Expand Down Expand Up @@ -814,7 +814,7 @@ def forward(
all_hidden_states = all_hidden_states + (hidden_states,)

# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = np.random.uniform(0, 1)
dropout_probability = torch.rand([])

skip_the_layer = True if self.training and (dropout_probability < self.config.layerdrop) else False
if not skip_the_layer or deepspeed_zero3_is_enabled:
Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/informer/modeling_informer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.
""" PyTorch Informer model."""

import random
from typing import List, Optional, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -1205,7 +1204,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1425,7 +1424,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/led/modeling_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


import math
import random
import warnings
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union
Expand Down Expand Up @@ -1871,7 +1870,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])

if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None, None)
Expand Down Expand Up @@ -2135,7 +2134,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/m2m_100/modeling_m2m_100.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


import math
import random
from typing import List, Optional, Tuple, Union

import torch
Expand Down Expand Up @@ -813,7 +812,7 @@ def forward(
encoder_states = encoder_states + (hidden_states,)

# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])

skip_the_layer = True if self.training and (dropout_probability < self.layerdrop) else False
if not skip_the_layer or deepspeed_zero3_is_enabled:
Expand Down Expand Up @@ -1057,7 +1056,7 @@ def forward(
all_hidden_states += (hidden_states,)

# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])

skip_the_layer = True if self.training and (dropout_probability < self.layerdrop) else False
if not skip_the_layer or deepspeed_zero3_is_enabled:
Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/marian/modeling_marian.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import copy
import math
import random
from typing import Dict, List, Optional, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -778,7 +777,7 @@ def forward(
if output_hidden_states:
encoder_states = encoder_states + (hidden_states,)
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop): # skip the layer
layer_outputs = (None, None)
else:
Expand Down Expand Up @@ -1024,7 +1023,7 @@ def forward(
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])
if self.training and (dropout_probability < self.layerdrop):
continue

Expand Down
3 changes: 1 addition & 2 deletions src/transformers/models/mask2former/modeling_mask2former.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
""" PyTorch Mask2Former model."""

import math
import random
import warnings
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple
Expand Down Expand Up @@ -1862,7 +1861,7 @@ def forward(
if output_hidden_states:
all_hidden_states += (hidden_states,)

dropout_probability = random.uniform(0, 1)
dropout_probability = torch.rand([])

if self.training and (dropout_probability < self.layerdrop):
continue
Expand Down
Loading

0 comments on commit a28325e

Please sign in to comment.