Skip to content

Commit df9d6a8

Browse files
committed
Avoid building lists unnecessarily
1 parent b3be753 commit df9d6a8

File tree

15 files changed

+43
-43
lines changed

15 files changed

+43
-43
lines changed

pytensor/graph/rewriting/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def is_same_graph_with_merge(var1, var2, givens=None):
114114
# We also need to make sure we replace a Variable if it is present in
115115
# `givens`.
116116
vars_replaced = [givens.get(v, v) for v in fgraph.outputs]
117-
o1, o2 = [v.owner for v in vars_replaced]
117+
o1, o2 = (v.owner for v in vars_replaced)
118118
if o1 is None and o2 is None:
119119
# Comparing two single-Variable graphs: they are equal if they are
120120
# the same Variable.

pytensor/link/c/cmodule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2244,7 +2244,7 @@ def join_options(init_part):
22442244
if len(version) != 3:
22452245
# Unexpected, but should not be a problem
22462246
continue
2247-
mj, mn, patch = [int(vp) for vp in version]
2247+
mj, mn, patch = (int(vp) for vp in version)
22482248
if (
22492249
((mj, mn) == (4, 6) and patch < 4)
22502250
or ((mj, mn) == (4, 7) and patch <= 3)

pytensor/link/numba/dispatch/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def make_slice_from_constant(context, builder, ty, pyval):
209209
default_stop_pos,
210210
default_stop_neg,
211211
default_step,
212-
) = [context.get_constant(types.intp, x) for x in get_defaults(context)]
212+
) = (context.get_constant(types.intp, x) for x in get_defaults(context))
213213

214214
step = pyval.step
215215
if step is None:

pytensor/tensor/blas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ def make_node(self, *inputs):
943943
)
944944
z, a, x, y, b = inputs
945945

946-
zr, xr, yr = [set(view_roots(i)) for i in (z, x, y)]
946+
zr, xr, yr = (set(view_roots(i)) for i in (z, x, y))
947947

948948
# We want the gemm to be inplace. When this op is inplace, it
949949
# declare to be inplace only on z. So to make it safe, we

pytensor/tensor/math.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,7 @@ def make_node(self, *inputs):
19091909
"pytensor.tensor.dot instead."
19101910
)
19111911

1912-
sx, sy = [input.type.shape for input in inputs]
1912+
sx, sy = (input.type.shape for input in inputs)
19131913
if len(sy) == 2:
19141914
sz = sx[:-1] + sy[-1:]
19151915
elif len(sy) == 1:

pytensor/tensor/nlinalg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def make_node(self, x):
250250
def perform(self, node, inputs, outputs):
251251
(x,) = inputs
252252
(w, v) = outputs
253-
w[0], v[0] = [z.astype(x.dtype) for z in self._numop(x)]
253+
w[0], v[0] = (z.astype(x.dtype) for z in self._numop(x))
254254

255255
def infer_shape(self, fgraph, node, shapes):
256256
n = shapes[0][0]

tests/link/jax/test_scan.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ def seir_one_step(ct0, dt0, st0, et0, it0, logp_c, logp_d, beta, gamma, delta):
8787
s0, e0, i0 = 100, 50, 25
8888
logp_c0 = np.array(0.0, dtype=config.floatX)
8989
logp_d0 = np.array(0.0, dtype=config.floatX)
90-
beta_val, gamma_val, delta_val = [
90+
beta_val, gamma_val, delta_val = (
9191
np.array(val, dtype=config.floatX) for val in [0.277792, 0.135330, 0.108753]
92-
]
92+
)
9393
C = np.array([3, 5, 8, 13, 21, 26, 10, 3], dtype=np.int32)
9494
D = np.array([1, 2, 3, 7, 9, 11, 5, 1], dtype=np.int32)
9595

tests/link/numba/test_scan.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ def seir_one_step(ct0, dt0, st0, et0, it0, logp_c, logp_d, beta, gamma, delta):
223223
s0, e0, i0 = 100, 50, 25
224224
logp_c0 = np.array(0.0, dtype=config.floatX)
225225
logp_d0 = np.array(0.0, dtype=config.floatX)
226-
beta_val, gamma_val, delta_val = [
226+
beta_val, gamma_val, delta_val = (
227227
np.array(val, dtype=config.floatX) for val in [0.277792, 0.135330, 0.108753]
228-
]
228+
)
229229
C = np.array([3, 5, 8, 13, 21, 26, 10, 3], dtype=np.int32)
230230
D = np.array([1, 2, 3, 7, 9, 11, 5, 1], dtype=np.int32)
231231

tests/sparse/test_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3273,7 +3273,7 @@ def test_op_ss(self):
32733273

32743274
tested = f(*data)
32753275

3276-
x, y = [m.toarray() for m in data]
3276+
x, y = (m.toarray() for m in data)
32773277
expected = np.dot(x, y)
32783278

32793279
assert tested.format == format

tests/tensor/rewriting/test_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def test_local_remove_useless_2(self):
490490
fg = FunctionGraph(outputs=[assert_op(x, y, 1)], clone=False)
491491
fg_res = rewrite_graph(fg, include=["canonicalize", "specialize"])
492492
topo = fg_res.toposort()
493-
(assert_node,) = [node for node in topo if isinstance(node.op, CheckAndRaise)]
493+
(assert_node,) = (node for node in topo if isinstance(node.op, CheckAndRaise))
494494
assert assert_node.inputs == [x, y]
495495

496496
def test_local_remove_useless_3(self):
@@ -500,7 +500,7 @@ def test_local_remove_useless_3(self):
500500
fg = FunctionGraph(outputs=[assert_op(x, y, 0)], clone=False)
501501
fg_res = rewrite_graph(fg, include=["canonicalize", "specialize"])
502502
topo = fg_res.toposort()
503-
(assert_node,) = [node for node in topo if isinstance(node.op, CheckAndRaise)]
503+
(assert_node,) = (node for node in topo if isinstance(node.op, CheckAndRaise))
504504
assert assert_node.inputs[:2] == [x, y]
505505
assert assert_node.inputs[-1].data == 0
506506

tests/tensor/rewriting/test_elemwise.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ class TestFusion:
277277
def my_init(dtype="float64", num=0):
278278
return np.zeros((5, 5), dtype=dtype) + num
279279

280-
fw, fx, fy, fz = [
280+
fw, fx, fy, fz = (
281281
tensor(dtype="float32", shape=(None,) * 2, name=n) for n in "wxyz"
282-
]
283-
dw, dx, dy, dz = [
282+
)
283+
dw, dx, dy, dz = (
284284
tensor(dtype="float64", shape=(None,) * 2, name=n) for n in "wxyz"
285-
]
286-
ix, iy, iz = [tensor(dtype="int32", shape=(None,) * 2, name=n) for n in "xyz"]
285+
)
286+
ix, iy, iz = (tensor(dtype="int32", shape=(None,) * 2, name=n) for n in "xyz")
287287
fv = fvector("v")
288288
fs = fscalar("s")
289289
fwv = my_init("float32", 1)

tests/tensor/rewriting/test_math.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,15 +1097,15 @@ def my_init(shp, dtype="float64", num=0):
10971097
ret = np.zeros(shp, dtype=dtype) + num
10981098
return ret
10991099

1100-
fw, fx, fy, fz = [
1100+
fw, fx, fy, fz = (
11011101
tensor(dtype="float32", shape=(None,) * len(shp), name=n) for n in "wxyz"
1102-
]
1103-
dw, dx, dy, dz = [
1102+
)
1103+
dw, dx, dy, dz = (
11041104
tensor(dtype="float64", shape=(None,) * len(shp), name=n) for n in "wxyz"
1105-
]
1106-
ix, iy, iz = [
1105+
)
1106+
ix, iy, iz = (
11071107
tensor(dtype="int32", shape=(None,) * len(shp), name=n) for n in "xyz"
1108-
]
1108+
)
11091109
fv = fvector("v")
11101110
fs = fscalar("s")
11111111

tests/tensor/test_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3656,7 +3656,7 @@ def test_stacklists():
36563656
result = f(1, 2, 3, 4)
36573657
assert result.shape == (2, 2, 1)
36583658

3659-
a, b, c, d = [matrix(x) for x in "abcd"]
3659+
a, b, c, d = (matrix(x) for x in "abcd")
36603660
X = stacklists([[a, b], [c, d]])
36613661
f = function([a, b, c, d], X)
36623662
x = np.ones((4, 4), "float32")

tests/tensor/test_blas.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ def cmp(self, z_, a_, x_, y_, b_):
124124
b = np.asarray(b_, dtype=dtype)
125125

126126
def cmp_linker(z, a, x, y, b, l):
127-
z, a, x, y, b = [np.asarray(p) for p in (z, a, x, y, b)]
127+
z, a, x, y, b = (np.asarray(p) for p in (z, a, x, y, b))
128128
z_orig = z.copy()
129-
tz, ta, tx, ty, tb = [
129+
tz, ta, tx, ty, tb = (
130130
as_tensor_variable(p).type() for p in (z, a, x, y, b)
131-
]
131+
)
132132

133133
f = inplace_func(
134134
[tz, ta, tx, ty, tb],
@@ -309,11 +309,11 @@ def test_transposes(self):
309309
C = rng.random((4, 5))[:, :4]
310310

311311
def t(z, x, y, a=1.0, b=0.0, l="c|py", dt="float64"):
312-
z, a, x, y, b = [_asarray(p, dtype=dt) for p in (z, a, x, y, b)]
312+
z, a, x, y, b = (_asarray(p, dtype=dt) for p in (z, a, x, y, b))
313313
# z_orig = z.copy()
314314
z_after = self._gemm(z, a, x, y, b)
315315

316-
tz, ta, tx, ty, tb = [shared(p) for p in (z, a, x, y, b)]
316+
tz, ta, tx, ty, tb = (shared(p) for p in (z, a, x, y, b))
317317

318318
# f = inplace_func([tz,ta,tx,ty,tb], gemm_inplace(tz,ta,tx,ty,tb),
319319
# mode = Mode(optimizer = None, linker=l))
@@ -368,13 +368,13 @@ def test_non_contiguous(self):
368368
C = rng.random((4, 4, 3))
369369

370370
def t(z, x, y, a=1.0, b=0.0, l="c|py", dt="float64"):
371-
z, a, x, y, b = [_asarray(p, dtype=dt) for p in (z, a, x, y, b)]
371+
z, a, x, y, b = (_asarray(p, dtype=dt) for p in (z, a, x, y, b))
372372
z_orig = z.copy()
373373
z_after = np.zeros_like(z_orig)
374374
for i in range(3):
375375
z_after[:, :, i] = self._gemm(z[:, :, i], a, x[:, :, i], y[:, :, i], b)
376376

377-
tz, ta, tx, ty, tb = [shared(p) for p in (z, a, x, y, b)]
377+
tz, ta, tx, ty, tb = (shared(p) for p in (z, a, x, y, b))
378378
for i in range(3):
379379
f_i = inplace_func(
380380
[],
@@ -1559,7 +1559,7 @@ def get_data(self, x_stride=1, y_stride=1):
15591559
return alpha, beta, a, x, y
15601560

15611561
def test_simple(self):
1562-
alpha, beta, a, x, y = [shared(value) for value in self.get_data()]
1562+
alpha, beta, a, x, y = (shared(value) for value in self.get_data())
15631563
desired_oy = (
15641564
alpha.get_value() * matrixmultiply(a.get_value(), x.get_value())
15651565
+ beta.get_value() * y.get_value()
@@ -1597,7 +1597,7 @@ def test_default_beta_y(self):
15971597
def test_simple_transpose(self):
15981598
vs = self.get_data()
15991599
alpha_v, beta_v, a_v, x_v, y_v = vs
1600-
alpha, beta, a, x, y = [shared(v) for v in vs]
1600+
alpha, beta, a, x, y = (shared(v) for v in vs)
16011601

16021602
desired_oy = alpha_v * matrixmultiply(np.transpose(a_v), x_v) + beta_v * y_v
16031603

@@ -1613,7 +1613,7 @@ def test_simple_transpose(self):
16131613
def test_x_stride(self):
16141614
vs = self.get_data(x_stride=2)
16151615
alpha_v, beta_v, a_v, x_v, y_v = vs
1616-
alpha, beta, a, x, y = [shared(v) for v in vs]
1616+
alpha, beta, a, x, y = (shared(v) for v in vs)
16171617

16181618
desired_oy = alpha_v * matrixmultiply(a_v, x_v[::2]) + beta_v * y_v
16191619

@@ -1629,7 +1629,7 @@ def test_x_stride(self):
16291629
def test_x_stride_transpose(self):
16301630
vs = self.get_data(x_stride=2)
16311631
alpha_v, beta_v, a_v, x_v, y_v = vs
1632-
alpha, beta, a, x, y = [shared(v) for v in vs]
1632+
alpha, beta, a, x, y = (shared(v) for v in vs)
16331633

16341634
desired_oy = (
16351635
alpha_v * matrixmultiply(np.transpose(a_v), x_v[::2]) + beta_v * y_v
@@ -1647,7 +1647,7 @@ def test_x_stride_transpose(self):
16471647
def test_y_stride(self):
16481648
vs = self.get_data(y_stride=2)
16491649
alpha_v, beta_v, a_v, x_v, y_v = vs
1650-
alpha, beta, a, x, y = [shared(v) for v in vs]
1650+
alpha, beta, a, x, y = (shared(v) for v in vs)
16511651

16521652
desired_oy = alpha_v * matrixmultiply(a_v, x_v) + beta_v * y_v[::2]
16531653

@@ -1663,7 +1663,7 @@ def test_y_stride(self):
16631663
def test_y_stride_transpose(self):
16641664
vs = self.get_data(y_stride=2)
16651665
alpha_v, beta_v, a_v, x_v, y_v = vs
1666-
alpha, beta, a, x, y = [shared(v) for v in vs]
1666+
alpha, beta, a, x, y = (shared(v) for v in vs)
16671667

16681668
desired_oy = (
16691669
alpha_v * matrixmultiply(np.transpose(a_v), x_v) + beta_v * y_v[::2]
@@ -1681,7 +1681,7 @@ def test_y_stride_transpose(self):
16811681
def test_a_strides(self):
16821682
vs = self.get_data()
16831683
alpha_v, beta_v, a_v, x_v, y_v = vs
1684-
alpha, beta, a, x, y = [shared(v) for v in vs]
1684+
alpha, beta, a, x, y = (shared(v) for v in vs)
16851685
a_v = a_v[::-1, ::-1]
16861686
a.set_value(
16871687
a.get_value(borrow=True, return_internal_type=True)[::-1, ::-1], borrow=True
@@ -1701,7 +1701,7 @@ def test_a_strides(self):
17011701
def test_a_strides_transpose(self):
17021702
vs = self.get_data()
17031703
alpha_v, beta_v, a_v, x_v, y_v = vs
1704-
alpha, beta, a, x, y = [shared(v) for v in vs]
1704+
alpha, beta, a, x, y = (shared(v) for v in vs)
17051705
a_v = a_v[::-1, ::-1]
17061706
a.set_value(
17071707
a.get_value(borrow=True, return_internal_type=True)[::-1, ::-1], borrow=True

tests/tensor/test_nlinalg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_eval(self):
331331
A = matrix(dtype=self.dtype)
332332
assert [e.eval({A: [[1]]}) for e in self.op(A)] == [[1.0], [[1.0]]]
333333
x = [[0, 1], [1, 0]]
334-
w, v = [e.eval({A: x}) for e in self.op(A)]
334+
w, v = (e.eval({A: x}) for e in self.op(A))
335335
assert_array_almost_equal(np.dot(x, v), w * v)
336336

337337

@@ -341,8 +341,8 @@ class TestEigh(TestEig):
341341
def test_uplo(self):
342342
S = self.S
343343
a = matrix(dtype=self.dtype)
344-
wu, vu = [out.eval({a: S}) for out in self.op(a, "U")]
345-
wl, vl = [out.eval({a: S}) for out in self.op(a, "L")]
344+
wu, vu = (out.eval({a: S}) for out in self.op(a, "U"))
345+
wl, vl = (out.eval({a: S}) for out in self.op(a, "L"))
346346
assert_array_almost_equal(wu, wl)
347347
assert_array_almost_equal(vu * np.sign(vu[0, :]), vl * np.sign(vl[0, :]))
348348

0 commit comments

Comments
 (0)