{"id":16271177,"url":"https://github.com/ehsanmok/dlpackrs","last_synced_at":"2025-09-17T18:31:04.963Z","repository":{"id":59521473,"uuid":"537698726","full_name":"ehsanmok/dlpackrs","owner":"ehsanmok","description":"DLPack safe Rust binding","archived":false,"fork":false,"pushed_at":"2022-09-20T16:33:05.000Z","size":28,"stargazers_count":14,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-06T17:14:30.830Z","etag":null,"topics":["deep-learning","dlpack","in-memory","ndarray","rust-bindings","standard","tensor"],"latest_commit_sha":null,"homepage":"https://docs.rs/dlpackrs","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ehsanmok.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-17T04:35:00.000Z","updated_at":"2024-03-24T08:56:23.000Z","dependencies_parsed_at":"2022-09-18T09:01:23.743Z","dependency_job_id":null,"html_url":"https://github.com/ehsanmok/dlpackrs","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fdlpackrs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fdlpackrs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fdlpackrs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fdlpackrs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ehsanmok","download_url":"https://codeload.github.com/ehsanmok/dlpackrs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232810832,"owners_count":18579929,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","dlpack","in-memory","ndarray","rust-bindings","standard","tensor"],"created_at":"2024-10-10T18:12:46.405Z","updated_at":"2025-09-17T18:30:59.682Z","avatar_url":"https://github.com/ehsanmok.png","language":"Rust","readme":"# dlpackrs\n\n[![Build](https://github.com/ehsanmok/dlpackrs/actions/workflows/build.yml/badge.svg)](https://github.com/ehsanmok/dlpackrs/actions/workflows/build.yml)\n[![miri-checked](https://img.shields.io/badge/miri-checked-green)](https://img.shields.io/badge/miri-checked-green)\n[![crates.io](https://img.shields.io/crates/v/dlpackrs.svg)](https://crates.io/crates/dlpackrs)\n[![docs.rs](https://docs.rs/dlpackrs/badge.svg)](https://docs.rs/dlpackrs)\n\nThis crate provides a safe idiomatic Rust binding to [DLPack](https://dmlc.github.io/dlpack/latest/) which is the standard in-memory, (mostly) hardware agnostic data format , recognized by major Deep Learning frameworks such as [PyTorch](https://pytorch.org/docs/stable/dlpack.html), [TensorFlow](https://www.tensorflow.org/api_docs/python/tf/experimental/dlpack/from_dlpack), [MXNet](https://mxnet.apache.org/versions/master/api/python/docs/_modules/mxnet/dlpack.html), [TVM](https://tvm.apache.org/docs/reference/api/python/contrib.html#module-tvm.contrib.dlpack) and major array processing frameworks such as [NumPy](https://numpy.org/doc/stable/release/1.22.0-notes.html#add-nep-47-compatible-dlpack-support) and [CuPy](https://docs.cupy.dev/en/stable/reference/generated/cupy.fromDlpack.html). An important feature of this standard is to provide *zero-cost* tensor conversion across frameworks on a particular supported hardware.\n\nThe Minimum Supported Rust Version (MSRV) is the stable toolchain **1.57.0**.\n\n## Usage\n\nThere are two main cases related to where the owner of the underlying data / storage of a tensor resides and what kind of operations are to be done.\n\n### Memory Managed Tensor\n\nIn this case, `ManagedTensor` is built from `ManagedTensorProxy` which is a safe proxy for the unsafe `ffi::DLManagedTensor`.\n\n### Plain Not-Memory-Managed Tensor\n\nIn this case, the (invariant) Rust wrapper `Tensor` can be used or if needed the unsafe `ffi::DLTensor`.\n\n## Example\n\nWhen ownership is concerned, one can use the `ManagedTensor`. Here is an example on how the bi-directional conversion\n\n\u003cdiv align=\"center\"\u003endarray::ArrayD \u003c---\u003e ManagedTensor\u003c/div\u003e\n\nis done at zero-cost.\n\n```rust\nimpl\u003c'tensor, C\u003e From\u003c\u0026'tensor mut ArrayD\u003cf32\u003e\u003e for ManagedContext\u003c'tensor, C\u003e {\n    fn from(t: \u0026'tensor mut ArrayD\u003cf32\u003e) -\u003e Self {\n        let dlt: Tensor\u003c'tensor\u003e = Tensor::from(t);\n        let inner = DLManagedTensor::new(dlt.0, None);\n        ManagedContext(inner)\n    }\n}\n\nimpl\u003c'tensor, C\u003e From\u003c\u0026mut ManagedContext\u003c'tensor, C\u003e\u003e for ArrayD\u003cf32\u003e {\n    fn from(mt: \u0026mut ManagedContext\u003c'tensor, C\u003e) -\u003e Self {\n        let dlt: DLTensor = mt.0.inner.dl_tensor.into();\n        unsafe {\n            let arr = RawArrayViewMut::from_shape_ptr(dlt.shape().unwrap(), dlt.data() as *mut f32);\n            arr.deref_into_view_mut().into_dyn().to_owned()\n        }\n    }\n}\n```\n\nAnd when ownership is not concerned, one can use `Tensor` as a view. Here is an example on how the bi-directional converion\n\n\u003cdiv align=\"center\"\u003endarray::ArrayD \u003c---\u003e Tensor\u003c/div\u003e\n\nis done at zero-cost.\n\n```rust\nimpl\u003c'tensor\u003e From\u003c\u0026'tensor mut ArrayD\u003cf32\u003e\u003e for Tensor\u003c'tensor\u003e {\n    fn from(arr: \u0026'tensor mut ArrayD\u003cf32\u003e) -\u003e Self {\n        let inner = DLTensor::new(\n            arr.as_mut_ptr() as *mut c_void,\n            Device::default(),\n            arr.ndim() as i32,\n            DataType::f32(),\n            arr.shape().as_ptr() as *const _ as *mut i64,\n            arr.strides().as_ptr() as *const _ as *mut i64,\n            0,\n        );\n        Tensor(inner)\n    }\n}\n\nimpl\u003c'tensor\u003e From\u003c\u0026'tensor mut Tensor\u003c'tensor\u003e\u003e for ArrayD\u003cf32\u003e {\n    fn from(t: \u0026'tensor mut Tensor\u003c'tensor\u003e) -\u003e Self {\n        unsafe {\n            let arr = RawArrayViewMut::from_shape_ptr(t.0.shape().unwrap(), t.0.data() as *mut f32);\n            arr.deref_into_view_mut().into_dyn().to_owned()\n        }\n    }\n}\n```\n\nSee the complete [examples/sample](https://github.com/ehsanmok/dlpackrs/blob/main/examples/sample/src/main.rs) where the above cases have been simulated for the Rust [ndarray](https://docs.rs/ndarray/latest/ndarray/) conversion.\n\n## License\n\nLicensed under either of\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally\nsubmitted for inclusion in the work by you, as defined in the Apache-2.0\nlicense, shall be dual licensed as above, without any additional terms or\nconditions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehsanmok%2Fdlpackrs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fehsanmok%2Fdlpackrs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehsanmok%2Fdlpackrs/lists"}