From 0c188f239c6f0ac026e0090529d3cd91e73f1f16 Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Mon, 13 Oct 2025 11:28:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20`infinicore.Tensor.dat?= =?UTF-8?q?a=5Fptr`=20=E7=9A=84=E8=B0=83=E7=94=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/infinicore/tensor.py | 2 +- src/infinicore/pybind11/tensor.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/infinicore/tensor.py b/python/infinicore/tensor.py index 5095768c5..81e34ebb3 100644 --- a/python/infinicore/tensor.py +++ b/python/infinicore/tensor.py @@ -32,7 +32,7 @@ def ndim(self): return self._underlying.ndim def data_ptr(self): - return self._underlying.data_ptr + return self._underlying.data_ptr() def size(self, dim=None): if dim is None: diff --git a/src/infinicore/pybind11/tensor.hpp b/src/infinicore/pybind11/tensor.hpp index 66fa06678..fc12ffbe2 100644 --- a/src/infinicore/pybind11/tensor.hpp +++ b/src/infinicore/pybind11/tensor.hpp @@ -17,7 +17,7 @@ inline void bind(py::module &m) { .def_property_readonly("dtype", [](const Tensor &tensor) { return tensor->dtype(); }) .def_property_readonly("device", [](const Tensor &tensor) { return tensor->device(); }) - .def("data_ptr", [](const Tensor &tensor) { return tensor->data(); }) + .def("data_ptr", [](const Tensor &tensor) { return reinterpret_cast(tensor->data()); }) .def("size", [](const Tensor &tensor, std::size_t dim) { return tensor->size(dim); }) .def("stride", [](const Tensor &tensor, std::size_t dim) { return tensor->stride(dim); }) .def("numel", [](const Tensor &tensor) { return tensor->numel(); }) From 8855b5fab18bff328f09e98f0cbbfc502899d2b7 Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Mon, 13 Oct 2025 15:14:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8A=A0=E5=85=A5=20`infinicore.use=5Fntop?= =?UTF-8?q?s`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/infinicore/__init__.py | 3 ++ python/infinicore/ntops.py | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 python/infinicore/ntops.py diff --git a/python/infinicore/__init__.py b/python/infinicore/__init__.py index 03ce5da0a..961806b15 100644 --- a/python/infinicore/__init__.py +++ b/python/infinicore/__init__.py @@ -24,6 +24,7 @@ short, uint8, ) +from infinicore.ntops import use_ntops from infinicore.ops.matmul import matmul from infinicore.ops.rearrange import rearrange from infinicore.tensor import ( @@ -62,6 +63,8 @@ "long", "short", "uint8", + # `ntops` integration. + "use_ntops", # Operations. "matmul", "rearrange", diff --git a/python/infinicore/ntops.py b/python/infinicore/ntops.py new file mode 100644 index 000000000..19a600aae --- /dev/null +++ b/python/infinicore/ntops.py @@ -0,0 +1,55 @@ +import sys + +import infinicore + + +def use_ntops(): + import ntops + + return _TemporaryAttributes( + (("ntops.torch.torch", infinicore),) + + tuple( + (f"infinicore.{op_name}", getattr(ntops.torch, op_name)) + for op_name in ntops.torch.__all__ + ) + ) + + +class _TemporaryAttributes: + def __init__(self, attribute_mappings): + self._attribute_mappings = attribute_mappings + + self._original_values = {} + + def __enter__(self): + for attr_path, new_value in self._attribute_mappings: + parent, attr_name = self._resolve_path(attr_path) + + try: + self._original_values[attr_path] = getattr(parent, attr_name) + except AttributeError: + pass + + setattr(parent, attr_name, new_value) + + return self + + def __exit__(self, exc_type, exc_value, traceback): + for attr_path, _ in self._attribute_mappings: + parent, attr_name = self._resolve_path(attr_path) + + if attr_path in self._original_values: + setattr(parent, attr_name, self._original_values[attr_path]) + else: + delattr(parent, attr_name) + + @staticmethod + def _resolve_path(path): + *parent_parts, attr_name = path.split(".") + + curr = sys.modules[parent_parts[0]] + + for part in parent_parts[1:]: + curr = getattr(curr, part) + + return curr, attr_name