Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 7635c3a

Browse files
committed
using new x-stuff in python wrappers
1 parent 6467b2b commit 7635c3a

3 files changed

Lines changed: 44 additions & 33 deletions

File tree

ddptensor/__init__.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,34 @@
33
from os import getenv
44
from . import array_api as api
55

6-
__impl_str = getenv("DDPNP_ARRAY", 'numpy')
7-
exec(f"import {__impl_str} as __impl")
6+
#__impl_str = getenv("DDPNP_ARRAY", 'numpy')
7+
#exec(f"import {__impl_str} as __impl")
88

99
for op in api.ew_binary_ops:
10+
OP = op.upper()
1011
exec(
11-
f"{op} = lambda this, other: dtensor(_cdt.ew_binary_op(this._t, '{op}', other._t if isinstance(other, ddptensor) else other, False))"
12+
f"{op} = lambda this, other: dtensor(_cdt.EWBinOp.op(_cdt.{OP}, this._t, other._t))" # if isinstance(other, ddptensor) else other, False))"
1213
)
1314

1415
for op in api.ew_unary_ops:
16+
OP = op.upper()
1517
exec(
16-
f"{op} = lambda this: dtensor(_cdt.ew_unary_op(this._t, '{op}', False))"
18+
f"{op} = lambda this: dtensor(_cdt.EWUnyOp.op(_cdt.{OP}, this._t))"
1719
)
1820

1921
for func in api.creators:
20-
if func in ["empty", "full", "ones", "zeros",]:
22+
FUNC = func.upper()
23+
if func in ["empty", "ones", "zeros",]:
2124
exec(
22-
f"{func} = lambda shape, *args, **kwargs: dtensor(_cdt.create(shape, '{func}', '{__impl_str}', *args, **kwargs))"
25+
f"{func} = lambda shape, dtype: dtensor(_cdt.Creator.create_from_shape(_cdt.{FUNC}, shape, dtype))"
26+
)
27+
elif func == "full":
28+
exec(
29+
f"{func} = lambda shape, val, dtype: dtensor(_cdt.Creator.full(_cdt.{FUNC}, shape, val, dtype))"
2330
)
2431

2532
for func in api.statisticals:
33+
FUNC = func.upper()
2634
exec(
27-
f"{func} = lambda this, **kwargs: dtensor(_cdt.reduce_op(this._t, '{func}', **kwargs))"
35+
f"{func} = lambda this, dim: dtensor(_cdt.ReduceOp.op(_cdt.{FUNC}, this._t, dim))"
2836
)

ddptensor/ddptensor.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,39 @@ def __repr__(self):
2929

3030

3131
for method in api.ew_binary_methods:
32+
METHOD = method.upper()
3233
exec(
33-
f"{method} = lambda self, other: dtensor(_cdt.ew_binary_op(self._t, '{method}', other._t if isinstance(other, dtensor) else other, True))"
34+
f"{method} = lambda self, other: dtensor(_cdt.EWBinOp.op(_cdt.{METHOD}, self._t, other._t))" # if isinstance(other, dtensor) else other, True))"
3435
)
3536

3637
for method in api.ew_binary_methods_inplace:
38+
METHOD = method.upper()
3739
exec(
38-
f"{method} = lambda self, other: (self, _cdt.ew_binary_op_inplace(self._t, '{method}', other._t if isinstance(other, dtensor) else other))[0]"
40+
f"{method} = lambda self, other: (self, _cdt.IEWBinOp.op(_cdt.{METHOD}, self._t, other._t))[0]" # if isinstance(other, dtensor) else other))[0]"
3941
)
4042

4143
for method in api.ew_unary_methods:
44+
METHOD = method.upper()
4245
exec(
43-
f"{method} = lambda self: dtensor(_cdt.ew_unary_op(self._t, '{method}', True))"
46+
f"{method} = lambda self: dtensor(_cdt.EWUnyOp.op(_cdt.{METHOD}, self._t))"
4447
)
48+
49+
# for method in unary_methods:
50+
# exec(
51+
# f"{method} = lambda self: self._t.{method}()"
52+
# )
4553

46-
for method in unary_methods:
47-
exec(
48-
f"{method} = lambda self: self._t.{method}()"
49-
)
50-
51-
for att in t_attributes:
52-
exec(
53-
f"{att} = property(lambda self: self._t.{att})"
54-
)
54+
# for att in t_attributes:
55+
# exec(
56+
# f"{att} = property(lambda self: self._t.{att})"
57+
# )
5558

56-
def __getitem__(self, *args):
57-
x = self._t.__getitem__(*args)
58-
return dtensor(x)
59+
# def __getitem__(self, *args):
60+
# x = self._t.__getitem__(*args)
61+
# return dtensor(x)
5962

60-
def __setitem__(self, key, value):
61-
x = self._t.__setitem__(key, value._t if isinstance(value, dtensor) else value)
63+
# def __setitem__(self, key, value):
64+
# x = self._t.__setitem__(key, value._t if isinstance(value, dtensor) else value)
6265

63-
def get_slice(self, *args):
64-
return self._t.get_slice(*args)
66+
# def get_slice(self, *args):
67+
# return self._t.get_slice(*args)

test/test_x.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import ddptensor._ddptensor as dt
2-
a = dt.Creator.create_from_shape(dt.ONES, [4,4], dt.float64)
3-
b = dt.Creator.create_from_shape(dt.ONES, [4,4], dt.float64)
4-
dt.IEWBinOp.op(dt.__IADD__, a, b)
1+
import ddptensor as dt
2+
a = dt.ones([4,4], dt.float64)
3+
b = dt.ones([4,4], dt.float64)
4+
a += b
55
print(a)
6-
print(dt.EWBinOp.op(dt.EQUAL, a, b))
7-
print(dt.EWUnyOp.op(dt.SQRT, a))
8-
print(dt.ReduceOp.op(dt.SUM, a, [1]))
6+
print(a == b)
7+
print(dt.sqrt(a))
8+
print(dt.sum(a, [1]))
99
dt.fini()

0 commit comments

Comments
 (0)