Skip to content

Commit

Permalink
added asnumpy where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
nfarabullini authored and havogt committed Nov 20, 2023
1 parent 520da0d commit 1e7b0ed
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_default_arguments(backend):
arg3 = gt_storage.ones(
backend=backend, dtype=np.float64, shape=(3, 3, 3), aligned_index=(0, 0, 0)
)
tmp = np.asarray(arg3)
tmp = arg3.asnumpy()
tmp *= 2

branch_true(arg1, None, arg3, par1=2.0)
Expand All @@ -208,7 +208,7 @@ def test_default_arguments(backend):
arg3 = gt_storage.ones(
backend=backend, dtype=np.float64, shape=(3, 3, 3), aligned_index=(0, 0, 0)
)
tmp = np.asarray(arg3)
tmp = arg3.asnumpy()
tmp *= 2

branch_true(arg1, arg2=None, par1=2.0, par2=5.0, par3=3.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ def testee(a: IJKFloatField, b: IJKFloatField) -> IJKFloatField:
a, b, out = (
cases.allocate(cartesian_case, testee, name)() for name in ("a", "b", cases.RETURN)
)
expected = (1.0 + 3.0 + 5.0 + 7.0) * np.add.accumulate(
np.asarray(a) + 2.0 * np.asarray(b), axis=2
)
expected = (1.0 + 3.0 + 5.0 + 7.0) * np.add.accumulate(a.asnumpy() + 2.0 * b.asnumpy(), axis=2)

cases.verify(cartesian_case, testee, a, b, out=out, ref=expected)

Expand Down Expand Up @@ -210,7 +208,7 @@ def testee(
for name in ("out1", "out2", "out3", "out4")
)

ref = np.add.accumulate(np.asarray(a) + 2 * np.asarray(b), axis=2)
ref = np.add.accumulate(a.asnumpy() + 2 * b.asnumpy(), axis=2)

cases.verify(
cartesian_case,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def testee(a: cases.IKField, offset_field: cases.IKField) -> gtx.Field[[IDim, KD
comparison=lambda out, ref: np.all(out == ref),
)

assert np.allclose(out, ref)
assert np.allclose(out.asnumpy(), ref)


def test_nested_tuple_return(cartesian_case):
Expand Down Expand Up @@ -920,7 +920,7 @@ def fieldop_where_k_offset(
)()
out = cases.allocate(cartesian_case, fieldop_where_k_offset, "inp")()

ref = np.where(np.asarray(k_index) > 0, np.roll(inp, 1, axis=1), 2)
ref = np.where(k_index.asnumpy() > 0, np.roll(inp.asnumpy(), 1, axis=1), 2)

cases.verify(cartesian_case, fieldop_where_k_offset, inp, k_index, out=out, ref=ref)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,9 @@ def conditional_promotion(mask: cases.IBoolField, a: cases.IFloatField) -> cases
)()
a = cases.allocate(cartesian_case, conditional_promotion, "a")()
out = cases.allocate(cartesian_case, conditional_promotion, cases.RETURN)()
ref = np.where(mask.asnumpy(), a.asnumpy(), 10.0)

cases.verify(
cartesian_case, conditional_promotion, mask, a, out=out, ref=np.where(mask, a, 10.0)
)
cases.verify(cartesian_case, conditional_promotion, mask, a, out=out, ref=ref)


def test_conditional_compareop(cartesian_case):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ def test_math_function_builtins_execution(cartesian_case, builtin_name: str, inp

builtin_field_op(*inps, out=out, offset_provider={})

assert np.allclose(np.asarray(out), expected)
assert np.allclose(out.asnumpy(), expected)
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def prog(

cases.run(cartesian_case, prog, a, b, out_a, out_b, offset_provider={})

assert np.allclose((a, b), (out_a, out_b))
assert np.allclose((a.asnumpy(), b.asnumpy()), (out_a.asnumpy(), out_b.asnumpy()))


def test_tuple_program_return_constructed_inside_with_slicing(cartesian_case):
Expand All @@ -178,7 +178,9 @@ def prog(

cases.run(cartesian_case, prog, a, b, out_a, out_b, offset_provider={})

assert np.allclose((a[1:], b[1:]), (out_a[1:], out_b[1:]))
assert np.allclose(
(a[1:].asnumpy(), b[1:].asnumpy()), (out_a[1:].asnumpy(), out_b[1:].asnumpy())
)
assert out_a[0] == 0 and out_b[0] == 0


Expand Down Expand Up @@ -209,7 +211,9 @@ def prog(

cases.run(cartesian_case, prog, a, b, c, out_a, out_b, out_c, offset_provider={})

assert np.allclose((a, b, c), (out_a, out_b, out_c))
assert np.allclose(
(a.asnumpy(), b.asnumpy(), c.asnumpy()), (out_a.asnumpy(), out_b.asnumpy(), out_c.asnumpy())
)


def test_wrong_argument_type(cartesian_case, copy_program_def):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ def if_without_else(
out = cases.allocate(cartesian_case, if_without_else, cases.RETURN)()

ref = {
(True, True): np.asarray(a) + 2,
(True, False): np.asarray(a),
(False, True): np.asarray(b) + 1,
(False, False): np.asarray(b) + 1,
(True, True): a.asnumpy() + 2,
(True, False): a.asnumpy(),
(False, True): b.asnumpy() + 1,
(False, False): b.asnumpy() + 1,
}

cases.verify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_single_argument(program_processor, dom):

run_processor(copy_stencil[dom], program_processor, inp, out=out, offset_provider={})
if validate:
assert np.allclose(inp, out)
assert np.allclose(inp.asnumpy(), out.asnumpy())


def test_2_arguments(program_processor, dom):
Expand All @@ -70,7 +70,7 @@ def fun(inp0, inp1):
run_processor(fun[dom], program_processor, inp0, inp1, out=out, offset_provider={})

if validate:
assert np.allclose(inp0 + inp1, out)
assert np.allclose(inp0.asnumpy() + inp1.asnumpy(), out.asnumpy())


def test_lambda_domain(program_processor):
Expand All @@ -82,4 +82,4 @@ def test_lambda_domain(program_processor):
run_processor(copy_stencil[dom], program_processor, inp, out=out, offset_provider={})

if validate:
assert np.allclose(inp, out)
assert np.allclose(inp.asnumpy(), out.asnumpy())
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def wrapped(inp):
)

if validate:
assert np.allclose(out[:, :-1], reference)
assert np.allclose(out[:, :-1].asnumpy(), reference)
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_trivial(program_processor, lift_mode):
)

if validate:
assert np.allclose(out[:, :, 0], out_s)
assert np.allclose(out[:, :, 0], out_s.asnumpy())


@fundef
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_shifted_arg_to_lift(program_processor, lift_mode):
)

if validate:
assert np.allclose(out, out_s)
assert np.allclose(out, out_s.asnumpy())


@fendef
Expand Down Expand Up @@ -137,7 +137,7 @@ def test_direct_deref(program_processor, lift_mode):
)

if validate:
assert np.allclose(out, out_s)
assert np.allclose(out, out_s.asnumpy())


@fundef
Expand Down Expand Up @@ -167,4 +167,4 @@ def test_vertical_shift_unstructured(program_processor):
)

if validate:
assert np.allclose(inp_s[:, 1:], np.asarray(out_s)[:, :-1])
assert np.allclose(inp_s[:, 1:].asnumpy(), out_s[:, :-1].asnumpy())
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def test_tuple_output(program_processor, stencil):
}
run_processor(stencil[dom], program_processor, inp1, inp2, out=out, offset_provider={})
if validate:
assert np.allclose(inp1, out[0])
assert np.allclose(inp2, out[1])
assert np.allclose(inp1.asnumpy(), out[0].asnumpy())
assert np.allclose(inp2.asnumpy(), out[1].asnumpy())


@fundef
Expand Down Expand Up @@ -144,10 +144,10 @@ def stencil(inp1, inp2, inp3, inp4):
offset_provider={},
)
if validate:
assert np.allclose(inp1, out[0][0])
assert np.allclose(inp2, out[0][1])
assert np.allclose(inp3, out[1][0])
assert np.allclose(inp4, out[1][1])
assert np.allclose(inp1.asnumpy(), out[0][0].asnumpy())
assert np.allclose(inp2.asnumpy(), out[0][1].asnumpy())
assert np.allclose(inp3.asnumpy(), out[1][0].asnumpy())
assert np.allclose(inp4.asnumpy(), out[1][1].asnumpy())


@pytest.mark.parametrize(
Expand Down Expand Up @@ -197,8 +197,8 @@ def fencil(size0, size1, size2, inp1, inp2, out1, out2):
offset_provider={},
)
if validate:
assert np.allclose(inp1, out1)
assert np.allclose(inp2, out2)
assert np.allclose(inp1.asnumpy(), out1.asnumpy())
assert np.allclose(inp2.asnumpy(), out2.asnumpy())


def test_asymetric_nested_tuple_of_field_output_constructed_inside(program_processor):
Expand Down Expand Up @@ -255,9 +255,9 @@ def fencil(size0, size1, size2, inp1, inp2, inp3, out1, out2, out3):
offset_provider={},
)
if validate:
assert np.allclose(inp1, out1)
assert np.allclose(inp2, out2)
assert np.allclose(inp3, out3)
assert np.allclose(inp1.asnumpy(), out1.asnumpy())
assert np.allclose(inp2.asnumpy(), out2.asnumpy())
assert np.allclose(inp3.asnumpy(), out3.asnumpy())


@pytest.mark.xfail(reason="Implement wrapper for extradim as tuple")
Expand Down Expand Up @@ -323,7 +323,7 @@ def test_tuple_field_input(program_processor):
}
run_processor(tuple_input[dom], program_processor, (inp1, inp2), out=out, offset_provider={})
if validate:
assert np.allclose(np.asarray(inp1) + np.asarray(inp2), out)
assert np.allclose(inp1.asnumpy() + inp2.asnumpy(), out.asnumpy())


@pytest.mark.xfail(reason="Implement wrapper for extradim as tuple")
Expand Down Expand Up @@ -389,7 +389,7 @@ def test_tuple_of_tuple_of_field_input(program_processor):
)
if validate:
assert np.allclose(
(np.asarray(inp1) + np.asarray(inp2) + np.asarray(inp3) + np.asarray(inp4)), out
(inp1.asnumpy() + inp2.asnumpy() + inp3.asnumpy() + inp4.asnumpy()), out.asnumpy()
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_solve_nonhydro_stencil_52_like_z_q(test_setup, fieldview_backend):
offset_provider={"Koff": KDim},
)

assert np.allclose(test_setup.z_q_ref[:, 1:], test_setup.z_q_out[:, 1:])
assert np.allclose(test_setup.z_q_ref[:, 1:], test_setup.z_q_out[:, 1:].asnumpy())


@pytest.mark.uses_tuple_returns
Expand All @@ -250,7 +250,7 @@ def test_solve_nonhydro_stencil_52_like_z_q_tup(test_setup, fieldview_backend):
offset_provider={"Koff": KDim},
)

assert np.allclose(test_setup.z_q_ref[:, 1:], test_setup.z_q_out[:, 1:])
assert np.allclose(test_setup.z_q_ref[:, 1:], test_setup.z_q_out[:, 1:].asnumpy())


@pytest.mark.uses_tuple_returns
Expand All @@ -266,8 +266,8 @@ def test_solve_nonhydro_stencil_52_like(test_setup, fieldview_backend):
offset_provider={"Koff": KDim},
)

assert np.allclose(test_setup.z_q_ref, test_setup.z_q)
assert np.allclose(test_setup.w_ref, test_setup.w)
assert np.allclose(test_setup.z_q_ref, test_setup.z_q.asnumpy())
assert np.allclose(test_setup.w_ref, test_setup.w.asnumpy())


@pytest.mark.uses_tuple_returns
Expand All @@ -285,5 +285,5 @@ def test_solve_nonhydro_stencil_52_like_with_gtfn_tuple_merge(test_setup, fieldv
offset_provider={"Koff": KDim},
)

assert np.allclose(test_setup.z_q_ref, test_setup.z_q)
assert np.allclose(test_setup.w_ref, test_setup.w)
assert np.allclose(test_setup.z_q_ref, test_setup.z_q.asnumpy())
assert np.allclose(test_setup.w_ref, test_setup.w.asnumpy())
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ def test_anton_toy(program_processor, lift_mode):
)

if validate:
assert np.allclose(out, ref)
assert np.allclose(out.asnumpy(), ref)
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_basic_column_stencils(program_processor, lift_mode, basic_stencils):
)

if validate:
assert np.allclose(ref, out)
assert np.allclose(ref, out.asnumpy())


@fundef
Expand Down Expand Up @@ -392,7 +392,7 @@ def test_different_vertical_sizes_with_origin(program_processor):
inp0 = gtx.as_field([KDim], np.arange(0, k_size))
inp1 = gtx.as_field([KDim], np.arange(0, k_size + 1), origin={KDim: 1})
out = gtx.as_field([KDim], np.zeros(k_size, dtype=np.int64))
ref = np.asarray(inp0) + np.asarray(inp1)[:-1]
ref = inp0.asnumpy() + inp1.asnumpy()[:-1]

run_processor(
sum_fencil,
Expand All @@ -405,7 +405,7 @@ def test_different_vertical_sizes_with_origin(program_processor):
)

if validate:
assert np.allclose(ref, out)
assert np.allclose(ref, out.asnumpy())


# TODO(havogt) test tuple_get builtin on a Column
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ def test_hdiff(hdiff_reference, program_processor, lift_mode):
)

if validate:
assert np.allclose(out[:, :, 0], out_s)
assert np.allclose(out[:, :, 0], out_s.asnumpy())
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@ def test_tridiag(fencil, tridiag_reference, program_processor, lift_mode):
)

if validate:
assert np.allclose(x, x_s)
assert np.allclose(x, x_s.asnumpy())
Loading

0 comments on commit 1e7b0ed

Please sign in to comment.