Skip to content

Commit

Permalink
Merge pull request #4988 from kamo-naoyuki/six
Browse files Browse the repository at this point in the history
Remove six
  • Loading branch information
mergify[bot] committed Mar 7, 2023
2 parents ab72501 + 6a28a63 commit 89ebca4
Show file tree
Hide file tree
Showing 28 changed files with 111 additions and 160 deletions.
2 changes: 1 addition & 1 deletion ci/test_integration_espnet2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cwd=$(pwd)
gen_dummy_coverage(){
# To avoid a problem when parallel running for `coverage run`.
# Please put this command after cd ./egs2/foo/bar
touch empty.py; ${python} empty.py
touch empty.py; ${python} empty.py
}

#### Make sure chainer-independent ####
Expand Down
5 changes: 2 additions & 3 deletions egs2/TEMPLATE/enh_asr1/scripts/utils/eval_perm_free_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from typing import List

import numpy as np
import six
from scipy.optimize import linear_sum_assignment

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer)
Expand Down Expand Up @@ -166,8 +165,8 @@ def reorder_refs_or_hyps(result_dir, num_spkrs, all_keys, hyp_or_ref=None, perms
def main(args):
# Read results from files
all_results = []
for r in six.moves.range(1, args.num_spkrs + 1):
for h in six.moves.range(1, args.num_spkrs + 1):
for r in range(1, args.num_spkrs + 1):
for h in range(1, args.num_spkrs + 1):
key = f"r{r}h{h}"
result = read_result(
Path(args.results_dir, f"result_{key}.txt"), result_key=key
Expand Down
11 changes: 5 additions & 6 deletions espnet/asr/chainer_backend/asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

# chainer related
import chainer
import six
from chainer import training
from chainer.datasets import TransformDataset
from chainer.training import extensions
Expand Down Expand Up @@ -112,7 +111,7 @@ def train(args):
elif ngpu > 1:
gpu_id = 0
devices = {"main": gpu_id}
for gid in six.moves.xrange(1, ngpu):
for gid in range(1, ngpu):
devices["sub_%d" % gid] = gid
logging.info("multi gpu calculation (#gpus = %d)." % ngpu)
logging.warning(
Expand Down Expand Up @@ -217,7 +216,7 @@ def train(args):
)
# set up minibatches
train_subsets = []
for gid in six.moves.xrange(ngpu):
for gid in range(ngpu):
# make subset
train_json_subset = {
k: v for i, (k, v) in enumerate(train_json.items()) if i % ngpu == gid
Expand All @@ -237,7 +236,7 @@ def train(args):
maxlen = max([len(train_subset) for train_subset in train_subsets])
for train_subset in train_subsets:
if maxlen != len(train_subset):
for i in six.moves.xrange(maxlen - len(train_subset)):
for i in range(maxlen - len(train_subset)):
train_subset += [train_subset[i]]

# hack to make batchsize argument as 1
Expand All @@ -252,7 +251,7 @@ def train(args):
maxtasksperchild=20,
shuffle=not use_sortagrad,
)
for gid in six.moves.xrange(ngpu)
for gid in range(ngpu)
]
else:
train_iters = [
Expand All @@ -261,7 +260,7 @@ def train(args):
batch_size=1,
shuffle=not use_sortagrad,
)
for gid in six.moves.xrange(ngpu)
for gid in range(ngpu)
]

# set up updater
Expand Down
9 changes: 4 additions & 5 deletions espnet/lm/chainer_backend/lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import chainer.functions as F
import chainer.links as L
import numpy as np
import six
from chainer import link, reporter, training
from chainer.dataset import convert

Expand Down Expand Up @@ -208,7 +207,7 @@ def __call__(self, state, x):
if self.typ == "lstm":
c = [None] * self.n_layers
c[0], h[0] = self.rnn[0](state["c"][0], state["h"][0], F.dropout(emb))
for n in six.moves.range(1, self.n_layers):
for n in range(1, self.n_layers):
c[n], h[n] = self.rnn[n](
state["c"][n], state["h"][n], F.dropout(h[n - 1])
)
Expand All @@ -221,7 +220,7 @@ def __call__(self, state, x):
xp.zeros((emb.shape[0], self.n_units), dtype=emb.dtype)
)
h[0] = self.rnn[0](state["h"][0], F.dropout(emb))
for n in six.moves.range(1, self.n_layers):
for n in range(1, self.n_layers):
if state["h"][n] is None:
xp = self.xp
with chainer.backends.cuda.get_device_from_id(self._device_id):
Expand Down Expand Up @@ -272,7 +271,7 @@ def update_core(self):
loss = 0
state = None
batch_size, sequence_length = x.shape
for i in six.moves.range(sequence_length):
for i in range(sequence_length):
# Compute the loss at this time step and accumulate it
state, loss_batch = optimizer.target(
state, chainer.Variable(x[:, i]), chainer.Variable(t[:, i])
Expand Down Expand Up @@ -313,7 +312,7 @@ def evaluate(self):
x, t = convert.concat_examples(batch, device=self.device, padding=(0, -1))
xp = chainer.backends.cuda.get_array_module(x)
state = None
for i in six.moves.range(len(x[0])):
for i in range(len(x[0])):
state, loss_batch = target(state, x[:, i], t[:, i])
non_zeros = xp.count_nonzero(x[:, i])
loss += loss_batch.data * non_zeros
Expand Down
3 changes: 1 addition & 2 deletions espnet/lm/lm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import chainer
import h5py
import numpy as np
import six
from chainer.training import extension
from tqdm import tqdm

Expand Down Expand Up @@ -152,7 +151,7 @@ def __init__(
# shuffle batches
random.shuffle(self.batch_indices)
else:
self.batch_indices = [np.array([i]) for i in six.moves.range(length)]
self.batch_indices = [np.array([i]) for i in range(length)]

# NOTE: this is not a count of parameter updates. It is just a count of
# calls of ``__next__``.
Expand Down
3 changes: 1 addition & 2 deletions espnet/nets/chainer_backend/deterministic_embed_id.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import chainer
import numpy
import six

# from chainer.functions.connection import embed_id
from chainer import cuda, function_node, link, variable
Expand Down Expand Up @@ -80,7 +79,7 @@ def forward(self, inputs):
if xp is numpy:
# It is equivalent to `numpy.add.at(gW, x, gy)` but ufunc.at is
# too slow.
for ix, igy in six.moves.zip(x.ravel(), gy.reshape(x.size, -1)):
for ix, igy in zip(x.ravel(), gy.reshape(x.size, -1)):
if ix == self.ignore_label:
continue
gW[ix] += igy
Expand Down
5 changes: 2 additions & 3 deletions espnet/nets/chainer_backend/e2e_asr_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import chainer
import chainer.functions as F
import numpy as np
import six
from chainer import reporter

from espnet.nets.chainer_backend.asr_interface import ChainerASRInterface
Expand Down Expand Up @@ -426,7 +425,7 @@ def recognize_beam(self, h, lpz, recog_args, char_list=None, rnnlm=None):
hyps = [hyp]
ended_hyps = []

for i in six.moves.range(maxlen):
for i in range(maxlen):
logging.debug("position " + str(i))

hyps_best_kept = []
Expand Down Expand Up @@ -469,7 +468,7 @@ def recognize_beam(self, h, lpz, recog_args, char_list=None, rnnlm=None):
]
local_best_scores = local_scores[:, local_best_ids]

for j in six.moves.range(beam):
for j in range(beam):
new_hyp = {}
new_hyp["score"] = hyp["score"] + float(local_best_scores[0, j])
new_hyp["yseq"] = [0] * (1 + len(hyp["yseq"]))
Expand Down
21 changes: 10 additions & 11 deletions espnet/nets/chainer_backend/rnn/decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import chainer.functions as F
import chainer.links as L
import numpy as np
import six

import espnet.nets.chainer_backend.deterministic_embed_id as DL
from espnet.nets.ctc_prefix_score import CTCPrefixScore
Expand Down Expand Up @@ -61,7 +60,7 @@ def __init__(
if dtype == "lstm"
else L.StatelessGRU(dunits + eprojs, dunits)
)
for i in six.moves.range(1, dlayers):
for i in range(1, dlayers):
setattr(
self,
"rnn%d" % i,
Expand All @@ -88,7 +87,7 @@ def __init__(
def rnn_forward(self, ey, z_list, c_list, z_prev, c_prev):
if self.dtype == "lstm":
c_list[0], z_list[0] = self.rnn0(c_prev[0], z_prev[0], ey)
for i in six.moves.range(1, self.dlayers):
for i in range(1, self.dlayers):
c_list[i], z_list[i] = self["rnn%d" % i](
c_prev[i], z_prev[i], z_list[i - 1]
)
Expand All @@ -100,7 +99,7 @@ def rnn_forward(self, ey, z_list, c_list, z_prev, c_prev):
xp.zeros((ey.shape[0], self.dunits), dtype=ey.dtype)
)
z_list[0] = self.rnn0(z_prev[0], ey)
for i in six.moves.range(1, self.dlayers):
for i in range(1, self.dlayers):
if z_prev[i] is None:
xp = self.xp
with chainer.backends.cuda.get_device_from_id(self._device_id):
Expand Down Expand Up @@ -156,7 +155,7 @@ def __call__(self, hs, ys):
# initialization
c_list = [None] # list of cell state of each layer
z_list = [None] # list of hidden state of each layer
for _ in six.moves.range(1, self.dlayers):
for _ in range(1, self.dlayers):
c_list.append(None)
z_list.append(None)
att_w = None
Expand All @@ -168,7 +167,7 @@ def __call__(self, hs, ys):
eys = F.separate(eys, axis=1)

# loop for an output sequence
for i in six.moves.range(olength):
for i in range(olength):
att_c, att_w = self.att(hs, z_list[0], att_w)
if i > 0 and random.random() < self.sampling_probability:
logging.info(" scheduled sampling ")
Expand Down Expand Up @@ -234,7 +233,7 @@ def recognize_beam(self, h, lpz, recog_args, char_list, rnnlm=None):
# initialization
c_list = [None] # list of cell state of each layer
z_list = [None] # list of hidden state of each layer
for _ in six.moves.range(1, self.dlayers):
for _ in range(1, self.dlayers):
c_list.append(None)
z_list.append(None)
a = None
Expand Down Expand Up @@ -286,7 +285,7 @@ def recognize_beam(self, h, lpz, recog_args, char_list, rnnlm=None):
hyps = [hyp]
ended_hyps = []

for i in six.moves.range(maxlen):
for i in range(maxlen):
logging.debug("position " + str(i))

hyps_best_kept = []
Expand Down Expand Up @@ -336,7 +335,7 @@ def recognize_beam(self, h, lpz, recog_args, char_list, rnnlm=None):
]
local_best_scores = local_scores[:, local_best_ids]

for j in six.moves.range(beam):
for j in range(beam):
new_hyp = {}
# do not copy {z,c}_list directly
new_hyp["z_prev"] = z_list[:]
Expand Down Expand Up @@ -469,7 +468,7 @@ def calculate_all_attentions(self, hs, ys):
# initialization
c_list = [None] # list of cell state of each layer
z_list = [None] # list of hidden state of each layer
for _ in six.moves.range(1, self.dlayers):
for _ in range(1, self.dlayers):
c_list.append(None)
z_list.append(None)
att_w = None
Expand All @@ -481,7 +480,7 @@ def calculate_all_attentions(self, hs, ys):
eys = F.separate(eys, axis=1)

# loop for an output sequence
for i in six.moves.range(olength):
for i in range(olength):
att_c, att_w = self.att(hs, z_list[0], att_w)
ey = F.hstack((eys[i], att_c)) # utt x (zdim + hdim)
z_list, c_list = self.rnn_forward(ey, z_list, c_list, z_list, c_list)
Expand Down
5 changes: 2 additions & 3 deletions espnet/nets/chainer_backend/rnn/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import chainer.functions as F
import chainer.links as L
import numpy as np
import six
from chainer import cuda

from espnet.nets.chainer_backend.nets_utils import _subsamplex
Expand Down Expand Up @@ -35,7 +34,7 @@ def __init__(self, idim, elayers, cdim, hdim, subsample, dropout, typ="blstm"):
rnn = L.NStepLSTM if "lstm" in typ else L.NStepGRU
rnn_label = "birnn" if bidir else "rnn"
with self.init_scope():
for i in six.moves.range(elayers):
for i in range(elayers):
if i == 0:
inputdim = idim
else:
Expand Down Expand Up @@ -68,7 +67,7 @@ def __call__(self, xs, ilens):
"""
logging.info(self.__class__.__name__ + " input lengths: " + str(ilens))

for layer in six.moves.range(self.elayers):
for layer in range(self.elayers):
if "lstm" in self.typ:
_, _, ys = self[self.rnn_label + str(layer)](None, None, xs)
else:
Expand Down
3 changes: 1 addition & 2 deletions espnet/nets/chainer_backend/rnn/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import math

import numpy as np
import six

# chainer related
from chainer import Variable, cuda, training
Expand Down Expand Up @@ -36,7 +35,7 @@ def sum_sqnorm(arr):
x = x.ravel()
s = x.dot(x)
sq_sum[int(dev)] += s
return sum([float(i) for i in six.itervalues(sq_sum)])
return sum([float(i) for i in sq_sum.values()])


class CustomUpdater(training.StandardUpdater):
Expand Down
3 changes: 1 addition & 2 deletions espnet/nets/chainer_backend/transformer/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import math

import numpy as np
import six
from chainer import cuda
from chainer import functions as F
from chainer import training
Expand Down Expand Up @@ -36,7 +35,7 @@ def sum_sqnorm(arr):
x = x.ravel()
s = x.dot(x)
sq_sum[int(dev)] += s
return sum([float(i) for i in six.itervalues(sq_sum)])
return sum([float(i) for i in sq_sum.values()])


class CustomUpdater(training.StandardUpdater):
Expand Down
9 changes: 4 additions & 5 deletions espnet/nets/ctc_prefix_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)

import numpy as np
import six
import torch


Expand Down Expand Up @@ -263,7 +262,7 @@ def extend_state(self, state):
)
start = max(r_prev.shape[0], 1)
r_prev_new[0:start] = r_prev
for t in six.moves.range(start, self.input_length):
for t in range(start, self.input_length):
r_prev_new[t, 1] = r_prev_new[t - 1, 1] + self.x[0, t, :, self.blank]

return (r_prev_new, s_prev, f_min_prev, f_max_prev)
Expand Down Expand Up @@ -296,7 +295,7 @@ def initial_state(self):
# superscripts n and b (non-blank and blank), respectively.
r = self.xp.full((self.input_length, 2), self.logzero, dtype=np.float32)
r[0, 1] = self.x[0, self.blank]
for i in six.moves.range(1, self.input_length):
for i in range(1, self.input_length):
r[i, 1] = r[i - 1, 1] + self.x[i, self.blank]
return r

Expand Down Expand Up @@ -327,7 +326,7 @@ def __call__(self, y, cs, r_prev):
last = y[-1]
if output_length > 0 and last in cs:
log_phi = self.xp.ndarray((self.input_length, len(cs)), dtype=np.float32)
for i in six.moves.range(len(cs)):
for i in range(len(cs)):
log_phi[:, i] = r_sum if cs[i] != last else r_prev[:, 1]
else:
log_phi = r_sum
Expand All @@ -336,7 +335,7 @@ def __call__(self, y, cs, r_prev):
# and log prefix probabilities log(psi)
start = max(output_length, 1)
log_psi = r[start - 1, 0]
for t in six.moves.range(start, self.input_length):
for t in range(start, self.input_length):
r[t, 0] = self.xp.logaddexp(r[t - 1, 0], log_phi[t - 1]) + xs[t]
r[t, 1] = (
self.xp.logaddexp(r[t - 1, 0], r[t - 1, 1]) + self.x[t, self.blank]
Expand Down

0 comments on commit 89ebca4

Please sign in to comment.