Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions dali/pipeline/operator/builtin/copy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void Copy<CPUBackend>::RunImpl(Workspace &ws) {
} else {
auto &input = ws.Input<GPUBackend>(0);
auto &output = ws.Output<CPUBackend>(0);
DeviceGuard g(input.device_id());
output.Copy(input, ws.output_order());
}
}
Expand Down
57 changes: 55 additions & 2 deletions dali/python/backend_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@ void ExposeTensor(py::module &m) {
.def("layout", [](Tensor<CPUBackend> &t) {
return t.GetLayout().str();
})
.def("set_layout", [](Tensor<CPUBackend> &t, const std::optional<std::string> &layout) {
SetLayout(t, layout);
})
.def("source_info", &Tensor<CPUBackend>::GetSourceInfo,
R"(Gets a string descrbing the source of the data in the tensor, e.g. a name of the file
from which the data was loaded.)")
Expand All @@ -816,6 +819,15 @@ void ExposeTensor(py::module &m) {
},
R"code(Passthrough, since the object is already an instance of `TensorCPU`.)code",
py::return_value_policy::reference_internal)
.def("_make_copy", [](const Tensor<CPUBackend> &t) {
auto dst = std::make_unique<Tensor<CPUBackend>>();
dst->set_device_id(t.device_id());
dst->set_order(t.order());
dst->set_pinned(t.is_pinned());
dst->Copy(t);
return dst;
},
py::return_value_policy::take_ownership)
.def("copy_to_external",
[](Tensor<CPUBackend> &t, py::object p) {
CopyToExternal<mm::memory_kind::host>(ctypes_void_ptr(p), t, AccessOrder::host(), false);
Expand Down Expand Up @@ -961,16 +973,19 @@ void ExposeTensor(py::module &m) {
.def("layout", [](Tensor<GPUBackend> &t) {
return t.GetLayout().str();
})
.def("set_layout", [](Tensor<GPUBackend> &t, const std::optional<std::string> &layout) {
SetLayout(t, layout);
})
.def("source_info", &Tensor<GPUBackend>::GetSourceInfo,
R"(Gets a string descrbing the source of the data in the tensor, e.g. a name of the file
from which the data was loaded.)")
.def("get_property", GetTensorProperty<GPUBackend>)
.def("as_cpu", [](Tensor<GPUBackend> &t) -> Tensor<CPUBackend>* {
DeviceGuard g(t.device_id());
auto ret = std::make_unique<Tensor<CPUBackend>>();
ret->set_pinned(false);
UserStream * us = UserStream::Get();
cudaStream_t s = us->GetStream(t);
DeviceGuard g(t.device_id());
ret->Copy(t, s);
us->Wait(t);
return ret.release();
Expand All @@ -979,6 +994,15 @@ void ExposeTensor(py::module &m) {
Returns a `TensorCPU` object being a copy of this `TensorGPU`.
)code",
py::return_value_policy::take_ownership)
.def("_make_copy", [](const Tensor<GPUBackend> &t) {
DeviceGuard dg(t.device_id());
auto dst = std::make_unique<Tensor<GPUBackend>>();
dst->set_device_id(t.device_id());
dst->set_order(t.order());
dst->Copy(t);
return dst;
},
py::return_value_policy::take_ownership)
.def("squeeze",
[](Tensor<GPUBackend> &t, py::object dim_arg) -> bool {
if (!dim_arg.is_none()) {
Expand Down Expand Up @@ -1301,6 +1325,9 @@ void ExposeTensorListCPU(py::module &m) {
layout : str
Layout of the data
)code")
.def_static("broadcast", [](const Tensor<CPUBackend> &t, int num_samples) {
return std::make_shared<TensorList<CPUBackend>>(t, num_samples);
})
.def("_as_gpu", [](TensorList<CPUBackend> &t) {
auto ret = std::make_shared<TensorList<GPUBackend>>();
int dev = -1;
Expand All @@ -1320,9 +1347,20 @@ void ExposeTensorListCPU(py::module &m) {
return t;
}, R"code(Passthrough, as it is already an instance of `TensorListCPU`.)code",
py::return_value_policy::reference_internal)
.def("_make_copy", [](const TensorList<CPUBackend> &t) {
auto dst = std::make_shared<TensorList<CPUBackend>>();
dst->set_device_id(t.device_id());
dst->set_order(t.order());
dst->set_pinned(t.is_pinned());
dst->Copy(t);
return dst;
})
.def("layout", [](TensorList<CPUBackend> &t) {
return t.GetLayout().str();
})
.def("set_layout", [](TensorList<CPUBackend> &t, const std::optional<std::string> &layout) {
SetLayout(t, layout);
})
.def("shape", &py_shape_list<CPUBackend>,
R"code(
Shape of the tensor list.
Expand Down Expand Up @@ -1567,13 +1605,16 @@ void ExposeTesorListGPU(py::module &m) {
R"code(
List of tensors residing in the GPU memory.
)code")
.def_static("broadcast", [](const Tensor<CPUBackend> &t, int num_samples) {
return std::make_shared<TensorList<CPUBackend>>(t, num_samples);
})
.def("as_cpu", [](TensorList<GPUBackend> &t) {
DeviceGuard g(t.device_id());
auto ret = std::make_shared<TensorList<CPUBackend>>();
ret->set_pinned(false);
ret->SetContiguity(BatchContiguity::Contiguous);
UserStream * us = UserStream::Get();
cudaStream_t s = us->GetStream(t);
DeviceGuard g(t.device_id());
ret->Copy(t, s);
us->Wait(t);
return ret;
Expand All @@ -1582,6 +1623,15 @@ void ExposeTesorListGPU(py::module &m) {
Returns a `TensorListCPU` object being a copy of this `TensorListGPU`.
)code",
py::return_value_policy::take_ownership)
.def("_make_copy", [](const TensorList<GPUBackend> &tl) {
DeviceGuard dg(tl.device_id());
auto dst = std::make_shared<TensorList<GPUBackend>>();
dst->set_device_id(tl.device_id());
dst->set_order(tl.order());
dst->set_pinned(tl.is_pinned());
dst->Copy(tl);
return dst;
})
.def(
"device_id", &TensorList<GPUBackend>::device_id)
.def("shape", &py_shape_list<GPUBackend>,
Expand Down Expand Up @@ -1653,6 +1703,9 @@ void ExposeTesorListGPU(py::module &m) {
.def("layout", [](TensorList<GPUBackend> &t) {
return t.GetLayout().str();
})
.def("set_layout", [](TensorList<GPUBackend> &t, const std::optional<std::string> &layout) {
SetLayout(t, layout);
})
.def("as_reshaped_tensor",
[](TensorList<GPUBackend> &tl, const vector<Index> &new_shape) {
return tl.AsReshapedTensor(new_shape);
Expand Down
8 changes: 8 additions & 0 deletions dali/python/nvidia/dali/experimental/dali2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@
from ._eval_context import * # noqa: F401, F403
from ._type import * # noqa: F401, F403
from ._device import * # noqa: F401, F403
from ._tensor import Tensor, tensor, as_tensor # noqa: F401
from ._batch import Batch, batch, as_batch # noqa: F401

from . import _fn
from . import ops

ops._initialize()
_fn._initialize()
Loading
Loading