Skip to content

Commit 288baa7

Browse files
authored
Update to PyTorch 1.13 (+ 1.14 compatibility) (#1074)
1 parent f852fbd commit 288baa7

9 files changed

Lines changed: 45 additions & 39 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ Note that Sockeye has checks in place to not translate with an old model that wa
1111

1212
Each version section may have subsections for: _Added_, _Changed_, _Removed_, _Deprecated_, and _Fixed_.
1313

14+
## [3.1.27]
15+
16+
### Changed
17+
18+
- allow torch 1.13 in requirements.txt
19+
- Replaced deprecated `torch.testing.assert_allclose` with `torch.testing.close` for PyTorch 1.14 compatibility.
20+
1421
## [3.1.26]
1522

1623
### Added
@@ -22,8 +29,7 @@ Each version section may have subsections for: _Added_, _Changed_, _Removed_, _D
2229

2330
### Changed
2431

25-
- device.init_device called by train, translate, and score
26-
32+
- `device.init_device()` called by train, translate, and score
2733
- allow torch 1.12 in requirements.txt
2834

2935
## [3.1.25]

requirements/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
torch>=1.10.0,<1.13.0
1+
torch>=1.10.0,<1.14.0
22
pyyaml>=5.1
33
numpy>1.16.0,<2.0.0
44
sacrebleu>=2.3.0

sockeye/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
# express or implied. See the License for the specific language governing
1212
# permissions and limitations under the License.
1313

14-
__version__ = '3.1.26'
14+
__version__ = '3.1.27'

sockeye/output_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def handle(self,
147147
:param t_walltime: Total walltime for translation.
148148
"""
149149
result = "{:.6f}".format(t_output.score)
150-
if hasattr(t_output, 'factor_scores') and t_output.factor_scores:
150+
if hasattr(t_output, 'factor_scores') and t_output.factor_scores is not None:
151151
factor_scores = "\t".join("{:.6f}".format(fs) for fs in t_output.factor_scores)
152152
result = f"{result}\t{factor_scores}"
153153
print(result, file=self.stream, flush=True)

test/unit/test_beam_search.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ def test_length_penalty_default():
2929
lengths = pt.tensor([[1], [2], [3]])
3030
length_penalty = sockeye.beam_search.LengthPenalty(1.0, 0.0)
3131
expected_lp = pt.tensor([[1.0], [2.], [3.]])
32-
pt.testing.assert_allclose(length_penalty(lengths), expected_lp)
32+
pt.testing.assert_close(length_penalty(lengths), expected_lp)
3333

3434

3535
def test_length_penalty():
3636
lengths = pt.tensor([[1], [2], [3]])
3737
length_penalty = sockeye.beam_search.LengthPenalty(.2, 5.0)
3838
expected_lp = pt.tensor([[6 ** 0.2 / 6 ** 0.2], [7 ** 0.2 / 6 ** 0.2], [8 ** 0.2 / 6 ** 0.2]])
39-
pt.testing.assert_allclose(length_penalty(lengths), expected_lp)
39+
pt.testing.assert_close(length_penalty(lengths), expected_lp)
4040

4141

4242
def test_length_penalty_int_input():
@@ -51,15 +51,15 @@ def test_brevity_penalty_default():
5151
ref_lengths = pt.tensor([[2], [3], [2]])
5252
brevity_penalty = sockeye.beam_search.BrevityPenalty(0.0)
5353
expected_bp = pt.tensor([[0], [0], [0]], dtype=pt.long)
54-
pt.testing.assert_allclose(brevity_penalty(hyp_lengths, ref_lengths), expected_bp)
54+
pt.testing.assert_close(brevity_penalty(hyp_lengths, ref_lengths), expected_bp)
5555

5656

5757
def test_brevity_penalty():
5858
hyp_lengths = pt.tensor([[1], [2], [3]])
5959
ref_lengths = pt.tensor([[7], [2], [91]])
6060
brevity_penalty = sockeye.beam_search.BrevityPenalty(3.5)
6161
expected_bp = pt.tensor([[3.5 * (1 - 7 / 1)], [0.0], [3.5 * (1 - 91 / 3)]])
62-
pt.testing.assert_allclose(brevity_penalty(hyp_lengths, ref_lengths), expected_bp)
62+
pt.testing.assert_close(brevity_penalty(hyp_lengths, ref_lengths), expected_bp)
6363

6464

6565
def test_brevity_penalty_int_input():
@@ -82,7 +82,7 @@ def test_candidate_scorer():
8282

8383
scores = scorer(raw_scores, lengths, reference_lengths)
8484
unnormalized_scores = scorer.unnormalize(scores, lengths, reference_lengths)
85-
pt.testing.assert_allclose(unnormalized_scores, raw_scores)
85+
pt.testing.assert_close(unnormalized_scores, raw_scores)
8686

8787
# int/float input
8888
raw_scores = 5.6
@@ -228,7 +228,7 @@ def test_update_scores(use_unk_dist):
228228
pt.tensor(pad_dist), pt.tensor(eos_dist))
229229
scores = scores.detach().numpy()
230230
lengths = lengths
231-
pt.testing.assert_allclose(lengths, pt.tensor([1, 1, 1])) # all lengths but finished updated + 1
231+
pt.testing.assert_close(lengths, pt.tensor([1, 1, 1], dtype=pt.int32)) # all lengths but finished updated + 1
232232
assert (scores[0] == (1. + target_dists[0] + eos_dist)).all() # 1 reached max length, force eos
233233
assert (scores[1] == (1. + pad_dist[0]).tolist()).all() # 2 finished, force pad, keep score
234234
if use_unk_dist:
@@ -341,7 +341,7 @@ def test_beam_search():
341341

342342
print('beam search lengths', r.lengths)
343343
print('internal lengths', inference.states[0])
344-
pt.testing.assert_allclose(r.lengths, inference.states[0].squeeze(1))
344+
pt.testing.assert_close(r.lengths, inference.states[0].squeeze(1))
345345
assert inference.states[1] == max_length
346346

347347

@@ -355,7 +355,7 @@ def test_get_nvs_vocab_slice_ids():
355355
bow, output_vocab_size = sockeye.beam_search._get_nvs_vocab_slice_ids(nvs_thresh=0.5,
356356
nvs_prediction=nvs_prediction)
357357
assert output_vocab_size == expected_bow.shape[0]
358-
pt.testing.assert_allclose(bow, expected_bow)
358+
pt.testing.assert_close(bow, expected_bow)
359359

360360
# Batch size 1
361361
# 0 1 2 3 4 5 6 7 8 9
@@ -364,7 +364,7 @@ def test_get_nvs_vocab_slice_ids():
364364
bow, output_vocab_size = sockeye.beam_search._get_nvs_vocab_slice_ids(nvs_thresh=0.5,
365365
nvs_prediction=nvs_prediction)
366366
assert output_vocab_size == expected_bow.shape[0]
367-
pt.testing.assert_allclose(bow, expected_bow)
367+
pt.testing.assert_close(bow, expected_bow)
368368

369369
# Batch size 1 + higher thresh
370370
# 0 1 2 3 4 5 6 7 8 9
@@ -373,7 +373,7 @@ def test_get_nvs_vocab_slice_ids():
373373
bow, output_vocab_size = sockeye.beam_search._get_nvs_vocab_slice_ids(nvs_thresh=0.9,
374374
nvs_prediction=nvs_prediction)
375375
assert output_vocab_size == expected_bow.shape[0]
376-
pt.testing.assert_allclose(bow, expected_bow)
376+
pt.testing.assert_close(bow, expected_bow)
377377

378378
# Batch size 2 + target prefix
379379
# Note: the first 4 tokens are special tokens (PAD, UNK etc.)
@@ -386,7 +386,7 @@ def test_get_nvs_vocab_slice_ids():
386386
nvs_prediction=nvs_prediction,
387387
target_prefix=target_prefix)
388388
assert output_vocab_size == expected_bow.shape[0]
389-
pt.testing.assert_allclose(bow, expected_bow)
389+
pt.testing.assert_close(bow, expected_bow)
390390

391391
# Batch size 2 + blocking lexicon
392392
# Note: the first 4 tokens are special tokens (PAD, UNK etc.)
@@ -401,7 +401,7 @@ def test_get_nvs_vocab_slice_ids():
401401
nvs_prediction=nvs_prediction,
402402
restrict_lexicon=restrict_lexicon)
403403
assert output_vocab_size == expected_bow.shape[0]
404-
pt.testing.assert_allclose(bow, expected_bow)
404+
pt.testing.assert_close(bow, expected_bow)
405405

406406

407407
def test_get_vocab_slice_ids_blocking():
@@ -419,4 +419,4 @@ def test_get_vocab_slice_ids_blocking():
419419
output_vocab_size=6
420420
)
421421
expected_vocab_slice_ids = pt.tensor([0, 1, 2, 4, 5, C.EOS_ID, C.EOS_ID, C.EOS_ID])
422-
pt.testing.assert_allclose(vocab_slice_ids, expected_vocab_slice_ids)
422+
pt.testing.assert_close(vocab_slice_ids, expected_vocab_slice_ids)

test/unit/test_layers.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ def test_lhuc():
2626
lhuc = sockeye.layers.LHUC(num_hidden=num_hidden)
2727
pt.nn.init.zeros_(lhuc.weight)
2828
out = lhuc(inp)
29-
pt.testing.assert_allclose(inp, out)
29+
pt.testing.assert_close(inp, out)
3030

3131
lhuc = sockeye.layers.LHUC(num_hidden=num_hidden)
3232
pt.nn.init.constant_(lhuc.weight, 20.0)
3333
out = lhuc(inp)
34-
pt.testing.assert_allclose(2 * inp, out)
34+
pt.testing.assert_close(2 * inp, out)
3535

3636

3737
def test_positional_embeddings():
@@ -51,18 +51,18 @@ def test_positional_embeddings():
5151
scale_down_positions=scale_down_positions)
5252
# no steps
5353
out = b(data, None)
54-
pt.testing.assert_allclose(out[0], expected_fixed_embedding)
55-
pt.testing.assert_allclose(out[1], expected_fixed_embedding)
54+
pt.testing.assert_close(out[0], expected_fixed_embedding)
55+
pt.testing.assert_close(out[1], expected_fixed_embedding)
5656

5757
# steps
5858
steps = pt.tensor([2, 3, 1, 1, 1]).unsqueeze(0)
5959
out = b(data, steps)
60-
pt.testing.assert_allclose(out[0, 0], expected_fixed_embedding[2])
61-
pt.testing.assert_allclose(out[1, 0], expected_fixed_embedding[2])
62-
pt.testing.assert_allclose(out[0, 1], expected_fixed_embedding[3])
63-
pt.testing.assert_allclose(out[1, 1], expected_fixed_embedding[3])
64-
pt.testing.assert_allclose(out[0, 2], expected_fixed_embedding[1])
65-
pt.testing.assert_allclose(out[1, 2], expected_fixed_embedding[1])
60+
pt.testing.assert_close(out[0, 0], expected_fixed_embedding[2])
61+
pt.testing.assert_close(out[1, 0], expected_fixed_embedding[2])
62+
pt.testing.assert_close(out[0, 1], expected_fixed_embedding[3])
63+
pt.testing.assert_close(out[1, 1], expected_fixed_embedding[3])
64+
pt.testing.assert_close(out[0, 2], expected_fixed_embedding[1])
65+
pt.testing.assert_close(out[1, 2], expected_fixed_embedding[1])
6666

6767
# learned embeddings
6868
b = sockeye.layers.PositionalEmbeddings(weight_type='learned',
@@ -73,7 +73,7 @@ def test_positional_embeddings():
7373
pt.nn.init.constant_(b.weight, val=1.0)
7474
expected_learned_embeddings = pt.ones(data_len, num_embed)
7575
out = b(data, None)
76-
pt.testing.assert_allclose(out[0], expected_learned_embeddings)
76+
pt.testing.assert_close(out[0], expected_learned_embeddings)
7777

7878

7979
def test_output_layer():
@@ -92,7 +92,7 @@ def test_output_layer():
9292
output_restricted = b(data, vocab_slice_ids)
9393
assert output_restricted.shape == (2, 10, len(vocab_slice_ids))
9494

95-
pt.testing.assert_allclose(output_restricted, reduced_output)
95+
pt.testing.assert_close(output_restricted, reduced_output, equal_nan=True)
9696

9797

9898
@pytest.mark.parametrize('qlen, kvlen, batch_size, hidden, heads',

test/unit/test_loss.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def test_cross_entropy_loss():
9494
expected_loss_value = pt.tensor(
9595
-(math.log(1 / 4) * 3) / num_valid) # 3 valid rows, all uniform, divided by num_valid
9696

97-
pt.testing.assert_allclose(loss_value, expected_loss_value)
98-
pt.testing.assert_allclose(logits.grad, expected_logits_grad)
97+
pt.testing.assert_close(loss_value, expected_loss_value)
98+
pt.testing.assert_close(logits.grad, expected_logits_grad)
9999

100100

101101
def test_label_to_bow():
@@ -109,8 +109,8 @@ def test_label_to_bow():
109109
expected_bow = pt.tensor([
110110
[0, 1, 0, 1],
111111
[1, 0, 0, 0],
112-
])
113-
pt.testing.assert_allclose(bow, expected_bow)
112+
], dtype=pt.float32)
113+
pt.testing.assert_close(bow, expected_bow)
114114

115115

116116
def test_binary_cross_entropy_loss():
@@ -147,9 +147,9 @@ def test_binary_cross_entropy_loss():
147147
loss_value.backward()
148148
assert loss_samples.item() == 1 # this loss returns always 1
149149
expected_loss = -pt.log(pt.sigmoid(pt.tensor(1))) / vocab_size / batch_size
150-
pt.testing.assert_allclose(loss_value, expected_loss)
151-
expected_grad = - 1/ (pt.exp(pt.tensor(1)) + 1) / vocab_size / batch_size
152-
pt.testing.assert_allclose(logits.grad,
150+
pt.testing.assert_close(loss_value, expected_loss)
151+
expected_grad = - 1 / (pt.exp(pt.tensor(1)) + 1) / vocab_size / batch_size
152+
pt.testing.assert_close(logits.grad,
153153
pt.tensor([[0.0000, 0.0000, 0.0000, expected_grad],
154154
[0.0000, 0.0000, 0.0000, 0.0000]])
155155
)

test/unit/test_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_set_parameters():
7777
name = 'output_layer.weight'
7878
model.set_parameters({name: param})
7979

80-
pt.testing.assert_allclose(model_params['output_layer.weight'].data, param.data)
80+
pt.testing.assert_close(model_params['output_layer.weight'].data, param.data)
8181

8282

8383
def test_set_parameters_allow_missing():

test/unit/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_average_tensors():
166166
expected_average += array
167167
expected_average /= 4
168168

169-
pt.testing.assert_allclose(utils.average_tensors(arrays), expected_average)
169+
pt.testing.assert_close(utils.average_tensors(arrays), expected_average)
170170

171171
with pytest.raises(utils.SockeyeError) as e:
172172
other_shape = (12, 13)

0 commit comments

Comments
 (0)