Skip to content

Commit 81e7dff

Browse files
committed
Use fstrings in Alloc.c_code
1 parent 28fc9ac commit 81e7dff

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

pytensor/tensor/basic.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,36 +1494,30 @@ def c_code(self, node, name, inp, out, sub):
14941494

14951495
# Initialize shape
14961496
for i, shp_i in enumerate(inp[1:]):
1497-
code += """
1498-
shape[%(i)s] = ((dtype_%(shp_i)s*) PyArray_DATA(%(shp_i)s))[0];
1499-
""" % dict(
1500-
i=i, shp_i=shp_i
1501-
)
1497+
code += f"""
1498+
shape[{i}] = ((dtype_{shp_i}*) PyArray_DATA({shp_i}))[0];
1499+
"""
15021500

1503-
code += """
1504-
int need_new_out = (NULL == %(zz)s);
1505-
for (int i = 0; i < %(ndim)s; i++)
1506-
need_new_out = (need_new_out
1507-
|| (PyArray_DIMS(%(zz)s)[i] != shape[i]));
1501+
code += f"""
1502+
int need_new_out = (NULL == {zz});
1503+
for (int i = 0; i < {ndim}; i++)
1504+
need_new_out = (need_new_out || (PyArray_DIMS({zz})[i] != shape[i]));
15081505
15091506
if (need_new_out)
1510-
{
1511-
Py_XDECREF(%(zz)s);
1512-
%(zz)s = (PyArrayObject*) PyArray_SimpleNew(%(ndim)s,
1513-
shape, PyArray_TYPE((PyArrayObject*) py_%(vv)s));
1514-
if (!%(zz)s)
1515-
{
1507+
{{
1508+
Py_XDECREF({zz});
1509+
{zz} = (PyArrayObject*) PyArray_SimpleNew({ndim}, shape, PyArray_TYPE((PyArrayObject*) py_{vv}));
1510+
if (!{zz})
1511+
{{
15161512
PyErr_SetString(PyExc_MemoryError, "alloc failed");
1517-
%(fail)s
1518-
}
1519-
}
1513+
{fail}
1514+
}}
1515+
}}
15201516
15211517
// This function takes care of broadcasting
1522-
if (PyArray_CopyInto(%(zz)s, %(vv)s) == -1)
1523-
%(fail)s
1524-
""" % dict(
1525-
vv=vv, ndim=ndim, zz=zz, fail=fail
1526-
)
1518+
if (PyArray_CopyInto({zz}, {vv}) == -1)
1519+
{fail}
1520+
"""
15271521

15281522
return code
15291523

@@ -1568,7 +1562,7 @@ def grad(self, inputs, grads):
15681562
for idx, axis in enumerate(axis_kept):
15691563
new_order[axis] = idx
15701564
gx = gx.dimshuffle(new_order)
1571-
# Dimshuffle to add back the broadcasted dims
1565+
# Dimshuffle to add back the broadcasted dims
15721566
# The *elements* of the output are not connected to
15731567
# the inputs that specify the shape. If you grow the
15741568
# shape by epsilon, the existing elements do not

0 commit comments

Comments
 (0)