From 4f75f41574ddd6608b94db0239f88a825fd6b30a Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Tue, 6 Oct 2020 00:00:33 -0500 Subject: [PATCH 1/4] make 'name' an optional argument for make_placeholder --- doc/design.rst | 2 ++ pytato/array.py | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/design.rst b/doc/design.rst index a569511..c21422d 100644 --- a/doc/design.rst +++ b/doc/design.rst @@ -121,6 +121,8 @@ Reserved Identifiers - ``_pt_out``: The default name of an unnamed output argument + - ``_pt_in``: The default name of an unnamed placeholder argument + - ``_pt_data``: Used to automatically generate identifiers for names of :class:`~pytato.array.DataWrapper` arguments that are not supplied by the user. diff --git a/pytato/array.py b/pytato/array.py index 1b5406f..608c471 100644 --- a/pytato/array.py +++ b/pytato/array.py @@ -1537,21 +1537,22 @@ def make_dict_of_named_arrays(data: Dict[str, Array]) -> DictOfNamedArrays: def make_placeholder(namespace: Namespace, - name: str, shape: ConvertibleToShape, dtype: Any, + name: Optional[str] = None, tags: Optional[TagsType] = None) -> Placeholder: """Make a :class:`Placeholder` object. :param namespace: namespace of the placeholder array - :param name: name of the placeholder array + :param name: name of the placeholder array, generated automatically + if not given :param shape: shape of the placeholder array :param dtype: dtype of the placeholder array (must be convertible to :class:`numpy.dtype`) :param tags: implementation tags """ if name is None: - raise ValueError("Placeholder instances must have a name") + name = namespace.name_gen('_pt_in') if not name.isidentifier(): raise ValueError(f"'{name}' is not a Python identifier") -- GitLab From fdd4ed220b60d5ba9da14b1e619bfe0fb36c047d Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 7 Oct 2020 17:37:44 -0500 Subject: [PATCH 2/4] fix make_placeholder calls in the tests - "name"'s arg position has been changed --- examples/advection.py | 4 ++-- examples/visualization.py | 2 +- test/test_codegen.py | 6 +++--- test/test_pytato.py | 18 +++++++++--------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/advection.py b/examples/advection.py index a599b05..69b4ebe 100755 --- a/examples/advection.py +++ b/examples/advection.py @@ -149,7 +149,7 @@ def test_advection_convergence(order, flux_type): pt.make_size_param(ns, "nelements") pt.make_size_param(ns, "nnodes") u = pt.make_placeholder(ns, - "u", shape="(nelements, nnodes)", dtype=np.float64) + name="u", shape="(nelements, nnodes)", dtype=np.float64) op = AdvectionOperator(discr, c=1, flux_type=flux_type, dg_ops=DGOps1D(discr, ns)) result = op.apply(u) @@ -182,7 +182,7 @@ def main(): pt.make_size_param(ns, "nelements") pt.make_size_param(ns, "nnodes") u = pt.make_placeholder(ns, - "u", shape="(nelements, nnodes)", dtype=np.float64) + name="u", shape="(nelements, nnodes)", dtype=np.float64) op = AdvectionOperator(discr, c=1, flux_type="central", dg_ops=DGOps1D(discr, ns)) result = op.apply(u) diff --git a/examples/visualization.py b/examples/visualization.py index f41f33b..56a72e6 100755 --- a/examples/visualization.py +++ b/examples/visualization.py @@ -20,7 +20,7 @@ def main(): ns = pt.Namespace() pt.make_size_param(ns, "n") - array = pt.make_placeholder(ns, "array", shape="n", dtype=np.float) + array = pt.make_placeholder(ns, name="array", shape="n", dtype=np.float) stack = pt.stack([array, 2*array, array + 6]) ns.assign("stack", stack) result = stack @ stack.T diff --git a/test/test_codegen.py b/test/test_codegen.py index 4d901b2..53721f2 100755 --- a/test/test_codegen.py +++ b/test/test_codegen.py @@ -69,8 +69,8 @@ def test_size_param(ctx_factory): queue = cl.CommandQueue(ctx) namespace = pt.Namespace() - n = pt.make_size_param(namespace, "n") - pt.make_placeholder(namespace, "x", "(n,)", np.int) + n = pt.make_size_param(namespace, name="n") + pt.make_placeholder(namespace, name="x", shape="(n,)", dtype=np.int) prog = pt.generate_loopy(n, target=pt.PyOpenCLTarget(queue)) x_in = np.array([1, 2, 3, 4, 5]) _, (n_out,) = prog(x=x_in) @@ -157,7 +157,7 @@ def test_roll(ctx_factory, shift, axis): namespace = pt.Namespace() pt.make_size_param(namespace, "n") - x = pt.make_placeholder(namespace, "x", shape=("n", "n"), dtype=np.float) + x = pt.make_placeholder(namespace, name="x", shape=("n", "n"), dtype=np.float) prog = pt.generate_loopy( pt.roll(x, shift=shift, axis=axis), diff --git a/test/test_pytato.py b/test/test_pytato.py index 49444fa..dd48fb6 100755 --- a/test/test_pytato.py +++ b/test/test_pytato.py @@ -33,25 +33,25 @@ import pytato as pt def test_matmul_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10), np.float) - b = pt.make_placeholder(namespace, "b", (20, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float) + b = pt.make_placeholder(namespace, name="b", shape=(20, 10), dtype=np.float) with pytest.raises(ValueError): a @ b - c = pt.make_placeholder(namespace, "c", (), np.float) + c = pt.make_placeholder(namespace, name="c", shape=(), dtype=np.float) with pytest.raises(ValueError): c @ c pt.make_size_param(namespace, "n") - d = pt.make_placeholder(namespace, "d", "(n, n)", np.float) + d = pt.make_placeholder(namespace, name="d", shape="(n, n)", dtype=np.float) d @ d def test_roll_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float) pt.roll(a, 1, axis=0) with pytest.raises(ValueError): @@ -64,7 +64,7 @@ def test_roll_input_validation(): def test_transpose_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float) pt.transpose(a) with pytest.raises(ValueError): @@ -80,7 +80,7 @@ def test_transpose_input_validation(): def test_slice_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10, 10), dtype=np.float) a[0] a[0, 0] @@ -96,8 +96,8 @@ def test_slice_input_validation(): def test_stack_input_validation(): namespace = pt.Namespace() - x = pt.make_placeholder(namespace, "x", (10, 10), np.float) - y = pt.make_placeholder(namespace, "y", (1, 10), np.float) + x = pt.make_placeholder(namespace, name="x", shape=(10, 10), dtype=np.float) + y = pt.make_placeholder(namespace, name="y", shape=(1, 10), dtype=np.float) assert pt.stack((x, x, x), axis=0).shape == (3, 10, 10) -- GitLab From e4358c6f8bbd70a1823d4b0b0035a54c45ca5171 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 7 Oct 2020 19:18:35 -0500 Subject: [PATCH 3/4] add test for make_placeholder with no name --- test/test_pytato.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test_pytato.py b/test/test_pytato.py index dd48fb6..76e11ef 100755 --- a/test/test_pytato.py +++ b/test/test_pytato.py @@ -114,6 +114,17 @@ def test_stack_input_validation(): pt.stack((x, x), axis=3) +def test_make_placeholder_noname(): + ns = pt.Namespace() + x = pt.make_placeholder(ns, shape=(10, 4), dtype=float) + y = 2*x + + knl = pt.generate_loopy(y).program + + assert x.name in knl.arg_dict + assert x.name in knl.get_read_variables() + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) -- GitLab From 5f109526c1e14be66a840245b5e705377231632c Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 7 Oct 2020 19:18:57 -0500 Subject: [PATCH 4/4] [flake8]: use double quotes (the good kind) --- pytato/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytato/array.py b/pytato/array.py index 608c471..33cc478 100644 --- a/pytato/array.py +++ b/pytato/array.py @@ -1552,7 +1552,7 @@ def make_placeholder(namespace: Namespace, :param tags: implementation tags """ if name is None: - name = namespace.name_gen('_pt_in') + name = namespace.name_gen("_pt_in") if not name.isidentifier(): raise ValueError(f"'{name}' is not a Python identifier") -- GitLab