Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support/Enforce dtype for empty() #2294

Merged
merged 18 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration_tests/array_01.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from lpython import i32
from numpy import empty
from numpy import empty, int32

def main0():
Nx: i32 = 600; Ny: i32 = 450
arr: i32[450, 600] = empty([Ny, Nx])
arr: i32[450, 600] = empty([450, 600], dtype=int32)
i: i32
j: i32
for i in range(Ny):
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/array_02.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from lpython import i32
from numpy import empty
from numpy import empty, int32

def main0():
Nx: i32 = 60; Ny: i32 = 45; Nz: i32 = 20
arr: i32[45, 60, 20] = empty([Ny, Nx, Nz])
arr: i32[45, 60, 20] = empty([45, 60, 20], dtype=int32)
i: i32
j: i32
k: i32
Expand Down
14 changes: 7 additions & 7 deletions integration_tests/array_02_decl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lpython import i32, i64, f32, f64, c32, c64
from numpy import empty
from numpy import empty, int32, int64, float32, float64, complex64, complex128

def accept_multidim_i32_array(xi32: i32[:, :]) -> i32:
return xi32[0, 0]
Expand All @@ -14,12 +14,12 @@ def accept_multidim_f64_array(xf64: f64[:, :]) -> f64:
return xf64[0, 1]

def declare_arrays():
ai32: i32[3, 3] = empty([3, 3])
ai64: i64[10, 10, 10] = empty([10, 10, 10])
af32: f32[3] = empty(3)
af64: f64[10, 4] = empty([10, 4])
ac32: c32[3, 5, 99] = empty([3, 5, 99])
ac64: c64[10, 13, 11, 16] = empty([10, 13, 11, 16])
ai32: i32[3, 3] = empty([3, 3], dtype=int32)
ai64: i64[10, 10, 10] = empty([10, 10, 10], dtype=int64)
af32: f32[3] = empty(3, dtype=float32)
af64: f64[10, 4] = empty([10, 4], dtype=float64)
ac32: c32[3, 5, 99] = empty([3, 5, 99], dtype=complex64)
ac64: c64[10, 13, 11, 16] = empty([10, 13, 11, 16], dtype=complex128)
print(accept_multidim_i32_array(ai32))
print(accept_multidim_i64_array(ai64))
print(accept_multidim_f32_array(af32))
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/array_03_decl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Truck:
wheels: i32

def declare_struct_array():
cars: Car[1] = empty(10, dtype=Car)
trucks: Truck[2] = empty(20, dtype=Truck)
cars: Car[1] = empty(1, dtype=Car)
trucks: Truck[2] = empty(2, dtype=Truck)
cars[0] = Car(100000, 800.0)
trucks[0] = Truck(1000000, 8)
trucks[1] = Truck(5000000, 12)
Expand Down
16 changes: 6 additions & 10 deletions integration_tests/array_expr_01.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
from lpython import i32, f32, f64
from lpython import Const, i32, f32, f64
from numpy import empty, reshape, int32, float64

def array_expr_01():
dim1: i32
dim2: i32
dim3: i32
dim1d: i32
dim1: Const[i32] = 10
dim2: Const[i32] = 10
dim3: Const[i32] = 5
dim1d: Const[i32] = dim1 * dim2 * dim3

i: i32
shape1d: i32[1] = empty(1, dtype=int32)
shape3d: i32[3] = empty(3, dtype=int32)
eps: f64
eps = 1e-12

dim1 = 10
dim2 = 10
dim3 = 5
dim1d = dim1 * dim2 * dim3

e: f64[10, 10, 5] = empty((dim1, dim2, dim3), dtype=float64)
f: f64[10, 10, 5] = empty((dim1, dim2, dim3), dtype=float64)
g: f64[500] = empty(dim1d, dtype=float64)
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/array_size_01.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from lpython import i32, f64, c32, c64
from numpy import empty
from numpy import empty, int32, float64, complex64, complex128

def main0():
x: i32[4, 5, 2] = empty([4, 5, 2])
y: f64[24, 100, 2, 5] = empty([24, 100, 2, 5])
x: i32[4, 5, 2] = empty([4, 5, 2], dtype=int32)
y: f64[24, 100, 2, 5] = empty([24, 100, 2, 5], dtype=float64)
print(x.size)
print(y.size)

assert x.size == 40
assert y.size == 24000

def main1():
a: c32[12] = empty([12])
b: c64[15, 15, 10] = empty([15, 15, 10])
a: c32[12] = empty([12], dtype=complex64)
b: c64[15, 15, 10] = empty([15, 15, 10], dtype=complex128)
print(a.size)
print(b.size)

Expand Down
18 changes: 9 additions & 9 deletions integration_tests/array_size_02.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from lpython import i32, f64, c32, c64, u32
from numpy import empty, size
from lpython import i32, f64, c32, c64, u32, u64
from numpy import empty, size, int32, uint32, uint64, float64, complex64, complex128

def main0():
x: i32[4, 5, 2] = empty([4, 5, 2])
y: f64[24, 100, 2, 5] = empty([24, 100, 2, 5])
x: i32[4, 5, 2] = empty([4, 5, 2], dtype=int32)
y: f64[24, 100, 2, 5] = empty([24, 100, 2, 5], dtype=float64)
z: i32
w: i32
z = 2
Expand All @@ -29,8 +29,8 @@ def main0():
assert size(y, w) == 5

def main1():
a: c32[12] = empty([12])
b: c64[15, 15, 10] = empty([15, 15, 10])
a: c32[12] = empty([12], dtype=complex64)
b: c64[15, 15, 10] = empty([15, 15, 10], dtype=complex128)
c: i32
d: i32
c = 1
Expand All @@ -50,7 +50,7 @@ def main1():
assert size(b, d) == 10

def main2():
a: i32[2, 3] = empty([2, 3])
a: i32[2, 3] = empty([2, 3], dtype=int32)
print(size(a))
print(size(a, 0))
print(size(a, 1))
Expand All @@ -60,8 +60,8 @@ def main2():
assert size(a, 1) == 3

def main3():
a: u32[2, 3, 4] = empty([2, 3, 4])
b: u64[10, 5] = empty([10, 5])
a: u32[2, 3, 4] = empty([2, 3, 4], dtype=uint32)
b: u64[10, 5] = empty([10, 5], dtype=uint64)
c: i32
d: i32
c = 1
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/bindpy_02.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lpython import i32, f64, pythoncall, Const
from numpy import empty
from numpy import empty, int32, float64

@pythoncall(module = "bindpy_02_module")
def get_cpython_version() -> str:
Expand Down Expand Up @@ -28,7 +28,7 @@ def show_array_dot_product(a: i32[:], b: f64[:]):
# Integers:
def test_array_ints():
n: Const[i32] = 5
a: i32[n] = empty([n], dtype=int)
a: i32[n] = empty([n], dtype=int32)

i: i32
for i in range(n):
Expand All @@ -41,7 +41,7 @@ def test_array_ints():
def test_array_floats():
n: Const[i32] = 3
m: Const[i32] = 5
b: f64[n, m] = empty([n, m], dtype=float)
b: f64[n, m] = empty([n, m], dtype=float64)

i: i32
j: i32
Expand All @@ -56,8 +56,8 @@ def test_array_floats():
def test_array_broadcast():
n: Const[i32] = 3
m: Const[i32] = 5
a: i32[n] = empty([n], dtype=int)
b: f64[n, m] = empty([n, m], dtype=float)
a: i32[n] = empty([n], dtype=int32)
b: f64[n, m] = empty([n, m], dtype=float64)

i: i32
j: i32
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/bindpy_04.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lpython import i1, i32, u32, f64, c64, pythoncall, Const, TypeVar
from numpy import empty, uint32, complex64
from numpy import empty, uint32, complex128

n = TypeVar("n")
m = TypeVar("m")
Expand Down Expand Up @@ -102,8 +102,8 @@ def test_2D_array_bools():
# Complex
def test_array_complexes():
n: Const[i32] = 5
a: c64[n] = empty([n], dtype=complex64)
b: c64[n] = empty([n], dtype=complex64)
a: c64[n] = empty([n], dtype=complex128)
b: c64[n] = empty([n], dtype=complex128)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. It's confusing, but I think it's less confusing to use c32 and c64 to mean 32bit and 64bit complex numbers (each component), so c32 corresponds to f32 and c64 to f64. NumPy uses the size of the whole complex number (both components together). Now LPython catches the mistake, so that's good.


i: i32
for i in range(n):
Expand All @@ -122,8 +122,8 @@ def test_array_complexes():
def test_2D_array_complexes():
n: Const[i32] = 3
m: Const[i32] = 4
a: c64[n, m] = empty([n, m], dtype=complex64)
b: c64[n, m] = empty([n, m], dtype=complex64)
a: c64[n, m] = empty([n, m], dtype=complex128)
b: c64[n, m] = empty([n, m], dtype=complex128)

i: i32
j: i32
Expand Down
34 changes: 17 additions & 17 deletions integration_tests/elemental_01.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lpython import i32, f64, f32
from numpy import empty, sin, cos, reshape
from numpy import empty, sin, cos, reshape, int32, float32, float64

def verify1d(array: f32[:], result: f32[:], size: i32):
i: i32
Expand Down Expand Up @@ -55,9 +55,9 @@ def elemental_sum():
j: i32
k: i32

array_a: f64[100] = empty(100)
array_b: f64[100] = empty(100)
array_c: f64[100] = empty(100)
array_a: f64[100] = empty(100, dtype=float64)
array_b: f64[100] = empty(100, dtype=float64)
array_c: f64[100] = empty(100, dtype=float64)

for i in range(100):
array_a[i] = float(i)
Expand All @@ -74,9 +74,9 @@ def elemental_mul():
j: i32
k: i32

array_a: f64[100] = empty(100)
array_b: f64[100] = empty(100)
array_c: f64[100] = empty(100)
array_a: f64[100] = empty(100, dtype=float64)
array_b: f64[100] = empty(100, dtype=float64)
array_c: f64[100] = empty(100, dtype=float64)

for i in range(100):
array_a[i] = float(i)
Expand All @@ -93,8 +93,8 @@ def elemental_sin():
j: i32
k: i32

array1d: f32[256] = empty(256)
sin1d: f32[256] = empty(256)
array1d: f32[256] = empty(256, dtype=float32)
sin1d: f32[256] = empty(256, dtype=float32)

for i in range(256):
array1d[i] = f32(i)
Expand All @@ -103,8 +103,8 @@ def elemental_sin():

verify1d(array1d, sin1d, 256)

arraynd: f64[256, 64, 16] = empty((256, 64, 16))
sinnd: f64[256, 64, 16] = empty((256, 64, 16))
arraynd: f64[256, 64, 16] = empty((256, 64, 16), dtype=float64)
sinnd: f64[256, 64, 16] = empty((256, 64, 16), dtype=float64)

for i in range(256):
for j in range(64):
Expand All @@ -119,8 +119,8 @@ def elemental_cos():
i: i32
j: i32

array2d: f64[256, 64] = empty((256, 64))
cos2d: f64[256, 64] = empty((256, 64))
array2d: f64[256, 64] = empty((256, 64), dtype=float64)
cos2d: f64[256, 64] = empty((256, 64), dtype=float64)

for i in range(256):
for j in range(64):
Expand All @@ -138,9 +138,9 @@ def elemental_trig_identity():
eps: f32
eps = f32(1e-6)

arraynd: f32[64, 32, 8, 4] = empty((64, 32, 8, 4))
observed: f32[64, 32, 8, 4] = empty((64, 32, 8, 4))
observed1d: f32[65536] = empty(65536)
arraynd: f32[64, 32, 8, 4] = empty((64, 32, 8, 4), dtype=float32)
observed: f32[64, 32, 8, 4] = empty((64, 32, 8, 4), dtype=float32)
observed1d: f32[65536] = empty(65536, dtype=float32)

for i in range(64):
for j in range(32):
Expand All @@ -150,7 +150,7 @@ def elemental_trig_identity():

observed = sin(arraynd)**f32(2) + cos(arraynd)**f32(2)

newshape: i32[1] = empty(1, dtype=int)
newshape: i32[1] = empty(1, dtype=int32)
newshape[0] = 65536
observed1d = reshape(observed, newshape)

Expand Down
24 changes: 12 additions & 12 deletions integration_tests/elemental_02.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from lpython import i32, f64, f32
from numpy import empty, tan, sin, cos, reshape
from numpy import empty, tan, sin, cos, reshape, int32, float32, float64

def elemental_tan64():
theta: f64[16, 8, 4, 2, 1] = empty((16, 8, 4, 2, 1))
theta1d: f64[1024] = empty(1024)
tantheta: f64[16, 8, 4, 2, 1] = empty((16, 8, 4, 2, 1))
observed: f64[16, 8, 4, 2, 1] = empty((16, 8, 4, 2, 1))
shapend: i32[5] = empty(5, dtype=int)
theta: f64[16, 8, 4, 2, 1] = empty((16, 8, 4, 2, 1), dtype=float64)
theta1d: f64[1024] = empty(1024, dtype=float64)
tantheta: f64[16, 8, 4, 2, 1] = empty((16, 8, 4, 2, 1), dtype=float64)
observed: f64[16, 8, 4, 2, 1] = empty((16, 8, 4, 2, 1), dtype=float64)
shapend: i32[5] = empty(5, dtype=int32)
i: i32
j: i32
k: i32
Expand All @@ -31,15 +31,15 @@ def elemental_tan64():
assert abs(tantheta[i, j, k, l, 0] - observed[i, j, k, l, 0]) <= eps

def elemental_tan32():
theta: f32[5, 5] = empty((5, 5))
theta1d: f32[25] = empty(25)
tantheta: f32[5, 5] = empty((5, 5))
observed: f32[5, 5] = empty((5, 5))
shapend: i32[2] = empty(2, dtype=int)
theta: f32[5, 5] = empty((5, 5), dtype=float32)
theta1d: f32[25] = empty(25, dtype=float32)
tantheta: f32[5, 5] = empty((5, 5), dtype=float32)
observed: f32[5, 5] = empty((5, 5), dtype=float32)
shapend: i32[2] = empty(2, dtype=int32)
i: i32
j: i32
eps: f32
eps = f32(1e-6)
eps = f32(1e-4)

for i in range(25):
theta1d[i] = f32(i + 1)
Expand Down
20 changes: 10 additions & 10 deletions integration_tests/elemental_03.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from lpython import i32, f32, f64
from numpy import empty, sqrt, reshape
from numpy import empty, sqrt, reshape, int32, float32, float64

def elemental_sqrt64():
array: f64[16, 16, 16] = empty((16, 16, 16))
observed: f64[4096] = empty(4096)
shape: i32[1] = empty(1, dtype=int)
array: f64[16, 16, 16] = empty((16, 16, 16), dtype=float64)
observed: f64[4096] = empty(4096, dtype=float64)
shape: i32[1] = empty(1, dtype=int32)
eps: f64
eps = 1e-12
i: i32
Expand All @@ -26,9 +26,9 @@ def elemental_sqrt64():
assert abs(observed[l]**2.0 - f64(i + j + k)) <= eps

def elemental_sqrt32():
array: f32[16, 16] = empty((16, 16))
observed: f32[256] = empty(256)
shape: i32[1] = empty(1, dtype=int)
array: f32[16, 16] = empty((16, 16), dtype=float32)
observed: f32[256] = empty(256, dtype=float32)
shape: i32[1] = empty(1, dtype=int32)
eps: f32
eps = f32(5e-6)
i: i32
Expand All @@ -48,9 +48,9 @@ def elemental_sqrt32():


def elemental_norm():
array_a: f64[100] = empty(100)
array_b: f64[100] = empty(100)
array_c: f64[100] = empty(100)
array_a: f64[100] = empty(100, dtype=float64)
array_b: f64[100] = empty(100, dtype=float64)
array_c: f64[100] = empty(100, dtype=float64)

i: i32
j: i32
Expand Down
Loading