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 data_ptr() on global tensors #10139

Merged
merged 11 commits into from
Apr 17, 2023
4 changes: 3 additions & 1 deletion oneflow/api/python/framework/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,10 @@ static PyObject* PyTensorObject_check_meta_consistency(PyObject* self, PyObject*
static PyObject* PyTensorObject_data_ptr(PyObject* self, PyObject* unused) {
HANDLE_ERRORS
const auto& t = PyTensor_Unpack(self);
const std::shared_ptr<LocalTensor> local_tensor =
t->is_local() ? ASSERT_PTR(t->AsLocalTensor()) : ASSERT_PTR(t->cur_rank_phy_tensor());
return functional::CastToPyObject(
reinterpret_cast<int64_t>(ASSERT(GetTensorDataPtr(ASSERT_PTR(t->AsLocalTensor())))));
reinterpret_cast<int64_t>(ASSERT(GetTensorDataPtr(local_tensor))));
END_HANDLE_ERRORS
}

Expand Down
9 changes: 8 additions & 1 deletion python/oneflow/test/tensor/test_data_ptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import oneflow.unittest


@flow.unittest.skip_unless_1n1d()
class TestDataPtr(unittest.TestCase):
@flow.unittest.skip_unless_1n1d()
def test_equality(test_case):
x = flow.ones(2, 3)
y = flow.ones(2, 3)
Expand All @@ -31,6 +31,13 @@ def test_equality(test_case):
x[:] = 2
test_case.assertEqual(x_ptr, x.data_ptr())

@flow.unittest.skip_unless_1n2d()
def test_global_tensor(test_case):
x = flow.randn(
2, 3, placement=flow.placement.all("cpu"), sbp=flow.sbp.broadcast
)
test_case.assertEqual(x.data_ptr(), x.to_local().data_ptr())


if __name__ == "__main__":
unittest.main()