# torch_link **Repository Path**: lite_xay/torch_link ## Basic Information - **Project Name**: torch_link - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-03-20 - **Last Updated**: 2024-03-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Torch Link A component to link torch and mindspore_lite. User can utilize it to convert torch.Tensor to mindspore_lite.Tensor or mindspore_lite.Tensor to torch.Tensor without copying of data. > The owner of data is src Tensor, dst Tensor only has the right to use the data. ## How to compile ### Compilation dependencies of Cpp: - [GCC](https://gcc.gnu.org/releases.html) >= 7.3.0 - CMake](https://cmake.org/download/) >= 3.18.3 ### Compilation dependencies of Python: - [Python](https://www.python.org/) >= 3.7.0 - [torch](https://pytorch.org/) >= 2.1.0 - [mindspore-lite](https://www.mindspore.cn/lite/docs/zh-CN/r2.2/use/downloads.html) >= 2.2 ### Instructions for parameters of `build.sh` | Parameter | Parameter Description | | --------- | ------------------------------------------------------------ | | -h | Print usage. | | -v | Control to show the details of compilation. | | -d | Open Debug mode when compiling. Default is Release mode. | | -i | Control if auto-install after compilation. Default only compilation. | | -j | Set the number of threads used to compile. Default is 4 | ### Module build compilation options | Option | Parameter Description | Necessary | | --------------------- | --------------------------------------------------- | --------- | | MSLITE_INSTALLED_PATH | Env of the installation path for mindspore_lite | True | | TORCH_INSTALLED_PATH | Env of the installation path for torch | True | | ENABLE_GITEE_MIRROR | Control the download source of dependency libraries | False | ### Compilation 1. set the necessary ```bash export MSLITE_INSTALL_PATH=xxx export TORCH_INSTALL_PATH=xxx export ENABLE_GITEE_MIRROR=on ``` 2. run `build.sh` ```bash bash build.sh -i ``` ## Usage ### Description of API - Convert torch.Tensor to mindspore_lite.Tensor ```python def torch_tensor_2_mslite_tensor(tensor: torch.Tensor) -> mslite.Tensor ``` - Convert mindspore_lite.Tensor to torch.Tensor ```python def mslite_tensor_2_torch_tensor(tensor: torch.Tensor) -> mslite.Tensor ``` ### Example - Convert torch.Tensor to mindspore_lite.Tensor ```python import torch import torch_link import numpy as np np_data = np.random.uniform(size=[2, 3]).astype(np.float32) torch_tensor = torch.from_numpy(np_data) mslite_tensor = torch_link.torch_tensor_2_mslite_tensor(torch_tensor) ``` - Convert mindspore_lite.Tensor to torch.Tensor ```python import mindspore_lite as mslite import torch_link import numpy as np np_data = np.random.uniform(size=[2, 3]).astype(np.float32) mslite_tensor = mslite.Tensor(np_data) torch_tensor = torch_link.mslite_tensor_2_torch_tensor(mslite_tensor) ```