Skip to content

Commit

Permalink
Replace OrderedDict with dict in Examples (#463)
Browse files Browse the repository at this point in the history
Summary:
### Description
From Python 3.6 onwards, the standard `dict` type maintains insertion order by default. [link](https://stackoverflow.com/questions/1867861/how-to-keep-keys-values-in-same-order-as-declared)

We can replace `collections.OrderedDict` with built-in python `dict` in AIT Examples and docs

### More info about OrderedDict vs dict
Changed in version python 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6. - [link](https://softwaremaniacs.org/blog/2020/02/05/dicts-ordered/)

Regular python `dict` preserves insertion order - That's a new feature in 3.6 (3.7 is when it's first guaranteed by the language standard) - [link](https://stackoverflow.com/questions/60446154/python-dictionary-insertion-and-deletion)

Pull Request resolved: #463

Reviewed By: tenpercent

Differential Revision: D44293712

Pulled By: aakhundov

fbshipit-source-id: 273c519672300588d8935c2ff1142c5519a09a46
  • Loading branch information
apivovarov authored and facebook-github-bot committed Mar 22, 2023
1 parent b0ca796 commit ad80787
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 11 deletions.
4 changes: 1 addition & 3 deletions docs/source/tutorial/how_to_infer_pt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ We need to import necessary Python modules:

.. code-block:: python
from collections import OrderedDict
import torch
from aitemplate.compiler import compile_model
Expand Down Expand Up @@ -85,7 +83,7 @@ In AIT, all names must follow the C variable naming standard, because the names
def map_pt_params(ait_model, pt_model):
ait_model.name_parameter_tensor()
pt_params = dict(pt_model.named_parameters())
mapped_pt_params = OrderedDict()
mapped_pt_params = {}
for name, _ in ait_model.named_parameters():
ait_name = name.replace(".", "_")
assert name in pt_params
Expand Down
3 changes: 1 addition & 2 deletions examples/03_bert/benchmark_ait.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
#
import os
from collections import OrderedDict

from typing import Dict, List

Expand Down Expand Up @@ -100,7 +99,7 @@ def map_pt_params(
ait_bert, pt_bert, batch_size: int, seq_length: int
) -> Dict[str, torch.Tensor]:
pt_params = dict(pt_bert.named_parameters())
mapped_pt_params = OrderedDict()
mapped_pt_params = {}
for name, _ in ait_bert.named_parameters():
ait_name = name.replace(".", "_")
if name in pt_params:
Expand Down
4 changes: 1 addition & 3 deletions examples/05_stable_diffusion/src/compile_lib/compile_vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from collections import OrderedDict

import numpy as np

import torch
Expand All @@ -27,7 +25,7 @@

def map_vae_params(ait_module, pt_module, batch_size, seq_len):
pt_params = dict(pt_module.named_parameters())
mapped_pt_params = OrderedDict()
mapped_pt_params = {}
for name, _ in ait_module.named_parameters():
ait_name = name.replace(".", "_")
if name in pt_params:
Expand Down
4 changes: 1 addition & 3 deletions examples/07_how_to_run_pt_model/how_to_run_pt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from collections import OrderedDict

import torch

from aitemplate.compiler import compile_model
Expand Down Expand Up @@ -58,7 +56,7 @@ def forward(self, input):
def map_pt_params(ait_model, pt_model):
ait_model.name_parameter_tensor()
pt_params = dict(pt_model.named_parameters())
mapped_pt_params = OrderedDict()
mapped_pt_params = {}
for name, _ in ait_model.named_parameters():
ait_name = name.replace(".", "_")
assert name in pt_params
Expand Down

0 comments on commit ad80787

Please sign in to comment.