We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Explicit check when training with share_embeddings and not share_vocab
Hey, Whenever I run training with share_embedding flat I get the following error:

```RuntimeError: cuda runtime error (59) : device-side assert triggered at /opt/conda/conda-bld/pytorch_1502009910772/work/torch/lib/THC/THCCachingHostAllocator.cpp:258```

Any idea what can cause this? how can fix this!

Thank.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `onmt/ModelConstructor.py`
Content:
```
1 """
2 This file is for models creation, which consults options
3 and creates each encoder and decoder accordingly.
4 """
5 import torch.nn as nn
6 
7 import onmt
8 import onmt.io
9 import onmt.Models
10 import onmt.modules
11 from onmt.Models import NMTModel, MeanEncoder, RNNEncoder, \
12                         StdRNNDecoder, InputFeedRNNDecoder
13 from onmt.modules import Embeddings, ImageEncoder, CopyGenerator, \
14                          TransformerEncoder, TransformerDecoder, \
15                          CNNEncoder, CNNDecoder, AudioEncoder
16 
17 
18 def make_embeddings(opt, word_dict, feature_dicts, for_encoder=True):
19     """
20     Make an Embeddings instance.
21     Args:
22         opt: the option in current environment.
23         word_dict(Vocab): words dictionary.
24         feature_dicts([Vocab], optional): a list of feature dictionary.
25         for_encoder(bool): make Embeddings for encoder or decoder?
26     """
27     if for_encoder:
28         embedding_dim = opt.src_word_vec_size
29     else:
30         embedding_dim = opt.tgt_word_vec_size
31 
32     word_padding_idx = word_dict.stoi[onmt.io.PAD_WORD]
33     num_word_embeddings = len(word_dict)
34 
35     feats_padding_idx = [feat_dict.stoi[onmt.io.PAD_WORD]
36                          for feat_dict in feature_dicts]
37     num_feat_embeddings = [len(feat_dict) for feat_dict in
38                            feature_dicts]
39 
40     return Embeddings(embedding_dim,
41                       opt.position_encoding,
42                       opt.feat_merge,
43                       opt.feat_vec_exponent,
44                       opt.feat_vec_size,
45                       opt.dropout,
46                       word_padding_idx,
47                       feats_padding_idx,
48                       num_word_embeddings,
49                       num_feat_embeddings)
50 
51 
52 def make_encoder(opt, embeddings):
53     """
54     Various encoder dispatcher function.
55     Args:
56         opt: the option in current environment.
57         embeddings (Embeddings): vocab embeddings for this encoder.
58     """
59     if opt.encoder_type == "transformer":
60         return TransformerEncoder(opt.enc_layers, opt.rnn_size,
61                                   opt.dropout, embeddings)
62     elif opt.encoder_type == "cnn":
63         return CNNEncoder(opt.enc_layers, opt.rnn_size,
64                           opt.cnn_kernel_width,
65                           opt.dropout, embeddings)
66     elif opt.encoder_type == "mean":
67         return MeanEncoder(opt.enc_layers, embeddings)
68     else:
69         # "rnn" or "brnn"
70         return RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
71                           opt.rnn_size, opt.dropout, embeddings)
72 
73 
74 def make_decoder(opt, embeddings):
75     """
76     Various decoder dispatcher function.
77     Args:
78         opt: the option in current environment.
79         embeddings (Embeddings): vocab embeddings for this decoder.
80     """
81     if opt.decoder_type == "transformer":
82         return TransformerDecoder(opt.dec_layers, opt.rnn_size,
83                                   opt.global_attention, opt.copy_attn,
84                                   opt.dropout, embeddings)
85     elif opt.decoder_type == "cnn":
86         return CNNDecoder(opt.dec_layers, opt.rnn_size,
87                           opt.global_attention, opt.copy_attn,
88                           opt.cnn_kernel_width, opt.dropout,
89                           embeddings)
90     elif opt.input_feed:
91         return InputFeedRNNDecoder(opt.rnn_type, opt.brnn,
92                                    opt.dec_layers, opt.rnn_size,
93                                    opt.global_attention,
94                                    opt.coverage_attn,
95                                    opt.context_gate,
96                                    opt.copy_attn,
97                                    opt.dropout,
98                                    embeddings)
99     else:
100         return StdRNNDecoder(opt.rnn_type, opt.brnn,
101                              opt.dec_layers, opt.rnn_size,
102                              opt.global_attention,
103                              opt.coverage_attn,
104                              opt.context_gate,
105                              opt.copy_attn,
106                              opt.dropout,
107                              embeddings)
108 
109 
110 def make_base_model(model_opt, fields, gpu, checkpoint=None):
111     """
112     Args:
113         model_opt: the option loaded from checkpoint.
114         fields: `Field` objects for the model.
115         gpu(bool): whether to use gpu.
116         checkpoint: the model gnerated by train phase, or a resumed snapshot
117                     model from a stopped training.
118     Returns:
119         the NMTModel.
120     """
121     assert model_opt.model_type in ["text", "img", "audio"], \
122         ("Unsupported model type %s" % (model_opt.model_type))
123 
124     # Make encoder.
125     if model_opt.model_type == "text":
126         src_dict = fields["src"].vocab
127         feature_dicts = onmt.io.collect_feature_vocabs(fields, 'src')
128         src_embeddings = make_embeddings(model_opt, src_dict,
129                                          feature_dicts)
130         encoder = make_encoder(model_opt, src_embeddings)
131     elif model_opt.model_type == "img":
132         encoder = ImageEncoder(model_opt.enc_layers,
133                                model_opt.brnn,
134                                model_opt.rnn_size,
135                                model_opt.dropout)
136     elif model_opt.model_type == "audio":
137         encoder = AudioEncoder(model_opt.enc_layers,
138                                model_opt.brnn,
139                                model_opt.rnn_size,
140                                model_opt.dropout,
141                                model_opt.sample_rate,
142                                model_opt.window_size)
143 
144     # Make decoder.
145     tgt_dict = fields["tgt"].vocab
146     # TODO: prepare for a future where tgt features are possible.
147     feature_dicts = onmt.io.collect_feature_vocabs(fields, 'tgt')
148     tgt_embeddings = make_embeddings(model_opt, tgt_dict,
149                                      feature_dicts, for_encoder=False)
150 
151     # Share the embedding matrix - preprocess with share_vocab required
152     if model_opt.share_embeddings:
153         tgt_embeddings.word_lut.weight = src_embeddings.word_lut.weight
154 
155     decoder = make_decoder(model_opt, tgt_embeddings)
156 
157     # Make NMTModel(= encoder + decoder).
158     model = NMTModel(encoder, decoder)
159     model.model_type = model_opt.model_type
160 
161     # Make Generator.
162     if not model_opt.copy_attn:
163         generator = nn.Sequential(
164             nn.Linear(model_opt.rnn_size, len(fields["tgt"].vocab)),
165             nn.LogSoftmax())
166         if model_opt.share_decoder_embeddings:
167             generator[0].weight = decoder.embeddings.word_lut.weight
168     else:
169         generator = CopyGenerator(model_opt, fields["src"].vocab,
170                                   fields["tgt"].vocab)
171 
172     # Load the model states from checkpoint or initialize them.
173     if checkpoint is not None:
174         print('Loading model parameters.')
175         model.load_state_dict(checkpoint['model'])
176         generator.load_state_dict(checkpoint['generator'])
177     else:
178         if model_opt.param_init != 0.0:
179             print('Intializing model parameters.')
180             for p in model.parameters():
181                 p.data.uniform_(-model_opt.param_init, model_opt.param_init)
182             for p in generator.parameters():
183                 p.data.uniform_(-model_opt.param_init, model_opt.param_init)
184         if hasattr(model.encoder, 'embeddings'):
185             model.encoder.embeddings.load_pretrained_vectors(
186                     model_opt.pre_word_vecs_enc, model_opt.fix_word_vecs_enc)
187         if hasattr(model.decoder, 'embeddings'):
188             model.decoder.embeddings.load_pretrained_vectors(
189                     model_opt.pre_word_vecs_dec, model_opt.fix_word_vecs_dec)
190 
191     # Add generator to model (this registers it as parameter of model).
192     model.generator = generator
193 
194     # Make the whole model leverage GPU if indicated to do so.
195     if gpu:
196         model.cuda()
197     else:
198         model.cpu()
199 
200     return model
201
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/onmt/ModelConstructor.py b/onmt/ModelConstructor.py
--- a/onmt/ModelConstructor.py
+++ b/onmt/ModelConstructor.py
@@ -143,13 +143,17 @@
 
     # Make decoder.
     tgt_dict = fields["tgt"].vocab
-    # TODO: prepare for a future where tgt features are possible.
     feature_dicts = onmt.io.collect_feature_vocabs(fields, 'tgt')
     tgt_embeddings = make_embeddings(model_opt, tgt_dict,
                                      feature_dicts, for_encoder=False)
 
-    # Share the embedding matrix - preprocess with share_vocab required
+    # Share the embedding matrix - preprocess with share_vocab required.
     if model_opt.share_embeddings:
+        # src/tgt vocab should be the same if `-share_vocab` is specified.
+        if src_dict != tgt_dict:
+            raise AssertionError('The `-share_vocab` should be set during '
+                                 'preprocess if you use share_embeddings!')
+
         tgt_embeddings.word_lut.weight = src_embeddings.word_lut.weight
 
     decoder = make_decoder(model_opt, tgt_embeddings)

{"golden_diff": "diff --git a/onmt/ModelConstructor.py b/onmt/ModelConstructor.py\n--- a/onmt/ModelConstructor.py\n+++ b/onmt/ModelConstructor.py\n@@ -143,13 +143,17 @@\n \n     # Make decoder.\n     tgt_dict = fields[\"tgt\"].vocab\n-    # TODO: prepare for a future where tgt features are possible.\n     feature_dicts = onmt.io.collect_feature_vocabs(fields, 'tgt')\n     tgt_embeddings = make_embeddings(model_opt, tgt_dict,\n                                      feature_dicts, for_encoder=False)\n \n-    # Share the embedding matrix - preprocess with share_vocab required\n+    # Share the embedding matrix - preprocess with share_vocab required.\n     if model_opt.share_embeddings:\n+        # src/tgt vocab should be the same if `-share_vocab` is specified.\n+        if src_dict != tgt_dict:\n+            raise AssertionError('The `-share_vocab` should be set during '\n+                                 'preprocess if you use share_embeddings!')\n+\n         tgt_embeddings.word_lut.weight = src_embeddings.word_lut.weight\n \n     decoder = make_decoder(model_opt, tgt_embeddings)\n", "issue": "Explicit check when training with share_embeddings and not share_vocab\nHey, Whenever I run training with share_embedding flat I get the following error:\r\n\r\n```RuntimeError: cuda runtime error (59) : device-side assert triggered at /opt/conda/conda-bld/pytorch_1502009910772/work/torch/lib/THC/THCCachingHostAllocator.cpp:258```\r\n\r\nAny idea what can cause this? how can fix this!\r\n\r\nThank.\n", "before_files": [{"content": "\"\"\"\nThis file is for models creation, which consults options\nand creates each encoder and decoder accordingly.\n\"\"\"\nimport torch.nn as nn\n\nimport onmt\nimport onmt.io\nimport onmt.Models\nimport onmt.modules\nfrom onmt.Models import NMTModel, MeanEncoder, RNNEncoder, \\\n                        StdRNNDecoder, InputFeedRNNDecoder\nfrom onmt.modules import Embeddings, ImageEncoder, CopyGenerator, \\\n                         TransformerEncoder, TransformerDecoder, \\\n                         CNNEncoder, CNNDecoder, AudioEncoder\n\n\ndef make_embeddings(opt, word_dict, feature_dicts, for_encoder=True):\n    \"\"\"\n    Make an Embeddings instance.\n    Args:\n        opt: the option in current environment.\n        word_dict(Vocab): words dictionary.\n        feature_dicts([Vocab], optional): a list of feature dictionary.\n        for_encoder(bool): make Embeddings for encoder or decoder?\n    \"\"\"\n    if for_encoder:\n        embedding_dim = opt.src_word_vec_size\n    else:\n        embedding_dim = opt.tgt_word_vec_size\n\n    word_padding_idx = word_dict.stoi[onmt.io.PAD_WORD]\n    num_word_embeddings = len(word_dict)\n\n    feats_padding_idx = [feat_dict.stoi[onmt.io.PAD_WORD]\n                         for feat_dict in feature_dicts]\n    num_feat_embeddings = [len(feat_dict) for feat_dict in\n                           feature_dicts]\n\n    return Embeddings(embedding_dim,\n                      opt.position_encoding,\n                      opt.feat_merge,\n                      opt.feat_vec_exponent,\n                      opt.feat_vec_size,\n                      opt.dropout,\n                      word_padding_idx,\n                      feats_padding_idx,\n                      num_word_embeddings,\n                      num_feat_embeddings)\n\n\ndef make_encoder(opt, embeddings):\n    \"\"\"\n    Various encoder dispatcher function.\n    Args:\n        opt: the option in current environment.\n        embeddings (Embeddings): vocab embeddings for this encoder.\n    \"\"\"\n    if opt.encoder_type == \"transformer\":\n        return TransformerEncoder(opt.enc_layers, opt.rnn_size,\n                                  opt.dropout, embeddings)\n    elif opt.encoder_type == \"cnn\":\n        return CNNEncoder(opt.enc_layers, opt.rnn_size,\n                          opt.cnn_kernel_width,\n                          opt.dropout, embeddings)\n    elif opt.encoder_type == \"mean\":\n        return MeanEncoder(opt.enc_layers, embeddings)\n    else:\n        # \"rnn\" or \"brnn\"\n        return RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,\n                          opt.rnn_size, opt.dropout, embeddings)\n\n\ndef make_decoder(opt, embeddings):\n    \"\"\"\n    Various decoder dispatcher function.\n    Args:\n        opt: the option in current environment.\n        embeddings (Embeddings): vocab embeddings for this decoder.\n    \"\"\"\n    if opt.decoder_type == \"transformer\":\n        return TransformerDecoder(opt.dec_layers, opt.rnn_size,\n                                  opt.global_attention, opt.copy_attn,\n                                  opt.dropout, embeddings)\n    elif opt.decoder_type == \"cnn\":\n        return CNNDecoder(opt.dec_layers, opt.rnn_size,\n                          opt.global_attention, opt.copy_attn,\n                          opt.cnn_kernel_width, opt.dropout,\n                          embeddings)\n    elif opt.input_feed:\n        return InputFeedRNNDecoder(opt.rnn_type, opt.brnn,\n                                   opt.dec_layers, opt.rnn_size,\n                                   opt.global_attention,\n                                   opt.coverage_attn,\n                                   opt.context_gate,\n                                   opt.copy_attn,\n                                   opt.dropout,\n                                   embeddings)\n    else:\n        return StdRNNDecoder(opt.rnn_type, opt.brnn,\n                             opt.dec_layers, opt.rnn_size,\n                             opt.global_attention,\n                             opt.coverage_attn,\n                             opt.context_gate,\n                             opt.copy_attn,\n                             opt.dropout,\n                             embeddings)\n\n\ndef make_base_model(model_opt, fields, gpu, checkpoint=None):\n    \"\"\"\n    Args:\n        model_opt: the option loaded from checkpoint.\n        fields: `Field` objects for the model.\n        gpu(bool): whether to use gpu.\n        checkpoint: the model gnerated by train phase, or a resumed snapshot\n                    model from a stopped training.\n    Returns:\n        the NMTModel.\n    \"\"\"\n    assert model_opt.model_type in [\"text\", \"img\", \"audio\"], \\\n        (\"Unsupported model type %s\" % (model_opt.model_type))\n\n    # Make encoder.\n    if model_opt.model_type == \"text\":\n        src_dict = fields[\"src\"].vocab\n        feature_dicts = onmt.io.collect_feature_vocabs(fields, 'src')\n        src_embeddings = make_embeddings(model_opt, src_dict,\n                                         feature_dicts)\n        encoder = make_encoder(model_opt, src_embeddings)\n    elif model_opt.model_type == \"img\":\n        encoder = ImageEncoder(model_opt.enc_layers,\n                               model_opt.brnn,\n                               model_opt.rnn_size,\n                               model_opt.dropout)\n    elif model_opt.model_type == \"audio\":\n        encoder = AudioEncoder(model_opt.enc_layers,\n                               model_opt.brnn,\n                               model_opt.rnn_size,\n                               model_opt.dropout,\n                               model_opt.sample_rate,\n                               model_opt.window_size)\n\n    # Make decoder.\n    tgt_dict = fields[\"tgt\"].vocab\n    # TODO: prepare for a future where tgt features are possible.\n    feature_dicts = onmt.io.collect_feature_vocabs(fields, 'tgt')\n    tgt_embeddings = make_embeddings(model_opt, tgt_dict,\n                                     feature_dicts, for_encoder=False)\n\n    # Share the embedding matrix - preprocess with share_vocab required\n    if model_opt.share_embeddings:\n        tgt_embeddings.word_lut.weight = src_embeddings.word_lut.weight\n\n    decoder = make_decoder(model_opt, tgt_embeddings)\n\n    # Make NMTModel(= encoder + decoder).\n    model = NMTModel(encoder, decoder)\n    model.model_type = model_opt.model_type\n\n    # Make Generator.\n    if not model_opt.copy_attn:\n        generator = nn.Sequential(\n            nn.Linear(model_opt.rnn_size, len(fields[\"tgt\"].vocab)),\n            nn.LogSoftmax())\n        if model_opt.share_decoder_embeddings:\n            generator[0].weight = decoder.embeddings.word_lut.weight\n    else:\n        generator = CopyGenerator(model_opt, fields[\"src\"].vocab,\n                                  fields[\"tgt\"].vocab)\n\n    # Load the model states from checkpoint or initialize them.\n    if checkpoint is not None:\n        print('Loading model parameters.')\n        model.load_state_dict(checkpoint['model'])\n        generator.load_state_dict(checkpoint['generator'])\n    else:\n        if model_opt.param_init != 0.0:\n            print('Intializing model parameters.')\n            for p in model.parameters():\n                p.data.uniform_(-model_opt.param_init, model_opt.param_init)\n            for p in generator.parameters():\n                p.data.uniform_(-model_opt.param_init, model_opt.param_init)\n        if hasattr(model.encoder, 'embeddings'):\n            model.encoder.embeddings.load_pretrained_vectors(\n                    model_opt.pre_word_vecs_enc, model_opt.fix_word_vecs_enc)\n        if hasattr(model.decoder, 'embeddings'):\n            model.decoder.embeddings.load_pretrained_vectors(\n                    model_opt.pre_word_vecs_dec, model_opt.fix_word_vecs_dec)\n\n    # Add generator to model (this registers it as parameter of model).\n    model.generator = generator\n\n    # Make the whole model leverage GPU if indicated to do so.\n    if gpu:\n        model.cuda()\n    else:\n        model.cpu()\n\n    return model\n", "path": "onmt/ModelConstructor.py"}], "after_files": [{"content": "\"\"\"\nThis file is for models creation, which consults options\nand creates each encoder and decoder accordingly.\n\"\"\"\nimport torch.nn as nn\n\nimport onmt\nimport onmt.io\nimport onmt.Models\nimport onmt.modules\nfrom onmt.Models import NMTModel, MeanEncoder, RNNEncoder, \\\n                        StdRNNDecoder, InputFeedRNNDecoder\nfrom onmt.modules import Embeddings, ImageEncoder, CopyGenerator, \\\n                         TransformerEncoder, TransformerDecoder, \\\n                         CNNEncoder, CNNDecoder, AudioEncoder\n\n\ndef make_embeddings(opt, word_dict, feature_dicts, for_encoder=True):\n    \"\"\"\n    Make an Embeddings instance.\n    Args:\n        opt: the option in current environment.\n        word_dict(Vocab): words dictionary.\n        feature_dicts([Vocab], optional): a list of feature dictionary.\n        for_encoder(bool): make Embeddings for encoder or decoder?\n    \"\"\"\n    if for_encoder:\n        embedding_dim = opt.src_word_vec_size\n    else:\n        embedding_dim = opt.tgt_word_vec_size\n\n    word_padding_idx = word_dict.stoi[onmt.io.PAD_WORD]\n    num_word_embeddings = len(word_dict)\n\n    feats_padding_idx = [feat_dict.stoi[onmt.io.PAD_WORD]\n                         for feat_dict in feature_dicts]\n    num_feat_embeddings = [len(feat_dict) for feat_dict in\n                           feature_dicts]\n\n    return Embeddings(embedding_dim,\n                      opt.position_encoding,\n                      opt.feat_merge,\n                      opt.feat_vec_exponent,\n                      opt.feat_vec_size,\n                      opt.dropout,\n                      word_padding_idx,\n                      feats_padding_idx,\n                      num_word_embeddings,\n                      num_feat_embeddings)\n\n\ndef make_encoder(opt, embeddings):\n    \"\"\"\n    Various encoder dispatcher function.\n    Args:\n        opt: the option in current environment.\n        embeddings (Embeddings): vocab embeddings for this encoder.\n    \"\"\"\n    if opt.encoder_type == \"transformer\":\n        return TransformerEncoder(opt.enc_layers, opt.rnn_size,\n                                  opt.dropout, embeddings)\n    elif opt.encoder_type == \"cnn\":\n        return CNNEncoder(opt.enc_layers, opt.rnn_size,\n                          opt.cnn_kernel_width,\n                          opt.dropout, embeddings)\n    elif opt.encoder_type == \"mean\":\n        return MeanEncoder(opt.enc_layers, embeddings)\n    else:\n        # \"rnn\" or \"brnn\"\n        return RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,\n                          opt.rnn_size, opt.dropout, embeddings)\n\n\ndef make_decoder(opt, embeddings):\n    \"\"\"\n    Various decoder dispatcher function.\n    Args:\n        opt: the option in current environment.\n        embeddings (Embeddings): vocab embeddings for this decoder.\n    \"\"\"\n    if opt.decoder_type == \"transformer\":\n        return TransformerDecoder(opt.dec_layers, opt.rnn_size,\n                                  opt.global_attention, opt.copy_attn,\n                                  opt.dropout, embeddings)\n    elif opt.decoder_type == \"cnn\":\n        return CNNDecoder(opt.dec_layers, opt.rnn_size,\n                          opt.global_attention, opt.copy_attn,\n                          opt.cnn_kernel_width, opt.dropout,\n                          embeddings)\n    elif opt.input_feed:\n        return InputFeedRNNDecoder(opt.rnn_type, opt.brnn,\n                                   opt.dec_layers, opt.rnn_size,\n                                   opt.global_attention,\n                                   opt.coverage_attn,\n                                   opt.context_gate,\n                                   opt.copy_attn,\n                                   opt.dropout,\n                                   embeddings)\n    else:\n        return StdRNNDecoder(opt.rnn_type, opt.brnn,\n                             opt.dec_layers, opt.rnn_size,\n                             opt.global_attention,\n                             opt.coverage_attn,\n                             opt.context_gate,\n                             opt.copy_attn,\n                             opt.dropout,\n                             embeddings)\n\n\ndef make_base_model(model_opt, fields, gpu, checkpoint=None):\n    \"\"\"\n    Args:\n        model_opt: the option loaded from checkpoint.\n        fields: `Field` objects for the model.\n        gpu(bool): whether to use gpu.\n        checkpoint: the model gnerated by train phase, or a resumed snapshot\n                    model from a stopped training.\n    Returns:\n        the NMTModel.\n    \"\"\"\n    assert model_opt.model_type in [\"text\", \"img\", \"audio\"], \\\n        (\"Unsupported model type %s\" % (model_opt.model_type))\n\n    # Make encoder.\n    if model_opt.model_type == \"text\":\n        src_dict = fields[\"src\"].vocab\n        feature_dicts = onmt.io.collect_feature_vocabs(fields, 'src')\n        src_embeddings = make_embeddings(model_opt, src_dict,\n                                         feature_dicts)\n        encoder = make_encoder(model_opt, src_embeddings)\n    elif model_opt.model_type == \"img\":\n        encoder = ImageEncoder(model_opt.enc_layers,\n                               model_opt.brnn,\n                               model_opt.rnn_size,\n                               model_opt.dropout)\n    elif model_opt.model_type == \"audio\":\n        encoder = AudioEncoder(model_opt.enc_layers,\n                               model_opt.brnn,\n                               model_opt.rnn_size,\n                               model_opt.dropout,\n                               model_opt.sample_rate,\n                               model_opt.window_size)\n\n    # Make decoder.\n    tgt_dict = fields[\"tgt\"].vocab\n    feature_dicts = onmt.io.collect_feature_vocabs(fields, 'tgt')\n    tgt_embeddings = make_embeddings(model_opt, tgt_dict,\n                                     feature_dicts, for_encoder=False)\n\n    # Share the embedding matrix - preprocess with share_vocab required.\n    if model_opt.share_embeddings:\n        # src/tgt vocab should be the same if `-share_vocab` is specified.\n        if src_dict != tgt_dict:\n            raise AssertionError('The `-share_vocab` should be set during '\n                                 'preprocess if you use share_embeddings!')\n\n        tgt_embeddings.word_lut.weight = src_embeddings.word_lut.weight\n\n    decoder = make_decoder(model_opt, tgt_embeddings)\n\n    # Make NMTModel(= encoder + decoder).\n    model = NMTModel(encoder, decoder)\n    model.model_type = model_opt.model_type\n\n    # Make Generator.\n    if not model_opt.copy_attn:\n        generator = nn.Sequential(\n            nn.Linear(model_opt.rnn_size, len(fields[\"tgt\"].vocab)),\n            nn.LogSoftmax())\n        if model_opt.share_decoder_embeddings:\n            generator[0].weight = decoder.embeddings.word_lut.weight\n    else:\n        generator = CopyGenerator(model_opt, fields[\"src\"].vocab,\n                                  fields[\"tgt\"].vocab)\n\n    # Load the model states from checkpoint or initialize them.\n    if checkpoint is not None:\n        print('Loading model parameters.')\n        model.load_state_dict(checkpoint['model'])\n        generator.load_state_dict(checkpoint['generator'])\n    else:\n        if model_opt.param_init != 0.0:\n            print('Intializing model parameters.')\n            for p in model.parameters():\n                p.data.uniform_(-model_opt.param_init, model_opt.param_init)\n            for p in generator.parameters():\n                p.data.uniform_(-model_opt.param_init, model_opt.param_init)\n        if hasattr(model.encoder, 'embeddings'):\n            model.encoder.embeddings.load_pretrained_vectors(\n                    model_opt.pre_word_vecs_enc, model_opt.fix_word_vecs_enc)\n        if hasattr(model.decoder, 'embeddings'):\n            model.decoder.embeddings.load_pretrained_vectors(\n                    model_opt.pre_word_vecs_dec, model_opt.fix_word_vecs_dec)\n\n    # Add generator to model (this registers it as parameter of model).\n    model.generator = generator\n\n    # Make the whole model leverage GPU if indicated to do so.\n    if gpu:\n        model.cuda()\n    else:\n        model.cpu()\n\n    return model\n", "path": "onmt/ModelConstructor.py"}]}