1e41f4b71Sopenharmony_ci# Neural Network Runtime Device Access 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Function Introduction 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciNeural Network Runtime (NNRt) functions as a bridge to connect the upper-layer AI inference framework and underlying acceleration chips, implementing cross-chip inference computing of AI models. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciNNRt opens HDIs for chip vendors to connect dedicated acceleration chips to NNRt to interconnect with the OpenHarmony ecosystem. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci### Basic Concepts 12e41f4b71Sopenharmony_ciBefore you get started, it would be helpful for you to have a basic understanding of the following concepts: 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci- Hardware Device Interface (HDI): defines APIs for cross-process communication between services in OpenHarmony. 15e41f4b71Sopenharmony_ci- Interface Description Language (IDL): defines the HDI language format. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci### Constraints 18e41f4b71Sopenharmony_ci- System version: OpenHarmony trunk version. 19e41f4b71Sopenharmony_ci- Development environment: Ubuntu 18.04 or later. 20e41f4b71Sopenharmony_ci- Access device: a chip with AI computing capabilities. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci### Working Principles 23e41f4b71Sopenharmony_ciNNRt connects to device chips through HDIs, which implement cross-process communication between services. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Figure 1** NNRt architecture 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciThe NNRt architecture consists of three layers: AI applications at the application layer, AI inference framework and NNRt at the system layer, and device services at the chip layer. To use a dedicated acceleration chip model for inference, an AI application needs to call the underlying dedicated AI acceleration chip through the AI inference framework and NNRt. NNRt is responsible for adapting to various underlying dedicated acceleration chips. It opens standard and unified HDIs for various AI acceleration chips to access the OpenHarmony ecosystem. In addition, NNRt opens standard and unified APIs to connect to various upper-layer AI inference frameworks. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciDuring program running, the AI application, AI inference framework, and NNRt reside in the user process, and the underlying AI chip device services reside in the HDI service process. NNRt implements the HDI client and the service side implements HDI Service to fulfill cross-process communication through the Hardware Driver Foundation (HDF) subsystem. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci## How to Develop 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci### Application Scenario 36e41f4b71Sopenharmony_ciSuppose you are connecting an AI acceleration chip, for example, RK3568, to NNRt. The following exemplifies how to connect the RK3568 chip to NNRt through HDI V2.0 to implement AI model inference. The process is similar for HDI V1.0. 37e41f4b71Sopenharmony_ci> **NOTE**<br>In this tutorial, the purpose of connecting the RK3568 chip to NNRt is to utilize the runtime and CPU operators of MindSpore Lite, instead of implementing the CPU driver. Therefore, the RK3568 chip depends on the dynamic libraries and header files of MindSpore Lite. In practice, the RK3568 chip does not depend on any library or header file of MindSpore Lite. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci### Development Flowchart 40e41f4b71Sopenharmony_ciThe following figure shows the process of connecting a dedicated AI acceleration chip to NNRt. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**Figure 2** Process of connecting a dedicated AI acceleration chip to NNRt 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci### Development Procedure 47e41f4b71Sopenharmony_ciThe following uses the RK3568 chip as an example to describe the development procedure. 48e41f4b71Sopenharmony_ci#### Generating the HDI Header File 49e41f4b71Sopenharmony_ciDownload the OpenHarmony source code from the open source community, build the `drivers_interface` component, and generate the HDI header file. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci1. [Download the source code](../get-code/sourcecode-acquire.md). 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci2. Go to the root directory of OpenHarmony source code, and compile the IDL API file of NNRt. 54e41f4b71Sopenharmony_ci ```shell 55e41f4b71Sopenharmony_ci ./build.sh --product-name rk3568 –ccache --build-target drivers_interface_nnrt 56e41f4b71Sopenharmony_ci ``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci After the compilation is complete, an HDI header file of the C++ language is generated in the `out/rk3568/gen/drivers/interface/nnrt/v2_0` directory. To generate a header file of the C language, run the following command to set the language field in the `drivers/interface/nnrt/v2_0/BUILD.gn` file before starting compilation: 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci ```shell 61e41f4b71Sopenharmony_ci language = "c" 62e41f4b71Sopenharmony_ci ``` 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci The directory of the generated header file is as follows: 65e41f4b71Sopenharmony_ci ```text 66e41f4b71Sopenharmony_ci out/rk3568/gen/drivers/interface/nnrt 67e41f4b71Sopenharmony_ci └── v2_0 68e41f4b71Sopenharmony_ci ├── drivers_interface_nnrt__libnnrt_proxy_2.0_external_deps_temp.json 69e41f4b71Sopenharmony_ci ├── drivers_interface_nnrt__libnnrt_stub_2.0_external_deps_temp.json 70e41f4b71Sopenharmony_ci ├── innrt_device.h # Header file of the HDI 71e41f4b71Sopenharmony_ci ├── iprepared_model.h # Header file of the AI model 72e41f4b71Sopenharmony_ci ├── libnnrt_proxy_2.0__notice.d 73e41f4b71Sopenharmony_ci ├── libnnrt_stub_2.0__notice.d 74e41f4b71Sopenharmony_ci ├── model_types.cpp # Implementation file for AI model structure definition 75e41f4b71Sopenharmony_ci ├── model_types.h # Header file for AI model structure definition 76e41f4b71Sopenharmony_ci ├── nnrt_device_driver.cpp # Device driver implementation example 77e41f4b71Sopenharmony_ci ├── nnrt_device_proxy.cpp 78e41f4b71Sopenharmony_ci ├── nnrt_device_proxy.h 79e41f4b71Sopenharmony_ci ├── nnrt_device_service.cpp # Implementation file for device services 80e41f4b71Sopenharmony_ci ├── nnrt_device_service.h # Header file for device services 81e41f4b71Sopenharmony_ci ├── nnrt_device_stub.cpp 82e41f4b71Sopenharmony_ci ├── nnrt_device_stub.h 83e41f4b71Sopenharmony_ci ├── nnrt_types.cpp # Implementation file for data type definition 84e41f4b71Sopenharmony_ci ├── nnrt_types.h # Header file for data type definitions 85e41f4b71Sopenharmony_ci ├── node_attr_types.cpp # Implementation file for AI model operator attribute definition 86e41f4b71Sopenharmony_ci ├── node_attr_types.h # Header file for AI model operator attribute definition 87e41f4b71Sopenharmony_ci ├── prepared_model_proxy.cpp 88e41f4b71Sopenharmony_ci ├── prepared_model_proxy.h 89e41f4b71Sopenharmony_ci ├── prepared_model_service.cpp # Implementation file for AI model services 90e41f4b71Sopenharmony_ci ├── prepared_model_service.h # Header file for AI model services 91e41f4b71Sopenharmony_ci ├── prepared_model_stub.cpp 92e41f4b71Sopenharmony_ci └── prepared_model_stub.h 93e41f4b71Sopenharmony_ci ``` 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci#### Implementing the HDI Service 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci1. Go to the root directory of OpenHarmony source code, and create the `nnrt` folder in the `drivers/peripheral` directory for HDI service development. The directory structure is as follows: 98e41f4b71Sopenharmony_ci ```text 99e41f4b71Sopenharmony_ci drivers/peripheral/nnrt 100e41f4b71Sopenharmony_ci ├── bundle.json 101e41f4b71Sopenharmony_ci ├── v2_0 102e41f4b71Sopenharmony_ci ├── BUILD.gn # Code build script 103e41f4b71Sopenharmony_ci └── hdi_cpu_service # Customized directory 104e41f4b71Sopenharmony_ci ├── BUILD.gn # Code build script 105e41f4b71Sopenharmony_ci ├── include 106e41f4b71Sopenharmony_ci │ ├── nnrt_device_service.h # Header file for device services 107e41f4b71Sopenharmony_ci │ ├── node_functions.h # Optional, depending on the actual implementation 108e41f4b71Sopenharmony_ci │ ├── node_registry.h # Optional, depending on the actual implementation 109e41f4b71Sopenharmony_ci │ └── prepared_model_service.h # Header file for AI model services 110e41f4b71Sopenharmony_ci └── src 111e41f4b71Sopenharmony_ci ├── nnrt_device_driver.cpp # Implementation file for the device driver 112e41f4b71Sopenharmony_ci ├── nnrt_device_service.cpp # Implementation file for device services 113e41f4b71Sopenharmony_ci ├── nnrt_device_stub.cpp # Optional, depending on the actual implementation 114e41f4b71Sopenharmony_ci ├── node_attr_types.cpp # Optional, depending on the actual implementation 115e41f4b71Sopenharmony_ci ├── node_functions.cpp # Optional, depending on the actual implementation 116e41f4b71Sopenharmony_ci ├── node_registry.cpp # Optional, depending on the actual implementation 117e41f4b71Sopenharmony_ci └── prepared_model_service.cpp # Implementation file for AI model services 118e41f4b71Sopenharmony_ci ``` 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci2. Implement the device driver. Unless otherwise required, you can directly use the `nnrt_device_driver.cpp` file generated through IDL file compilation. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci3. Implement service APIs by referring to the `nnrt_device_service.cpp` and `prepared_model_service.cpp` files. For details about the API definitions, see [NNRt HDI Definitions](https://gitee.com/openharmony/drivers_interface/tree/master/nnrt). 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci4. Compile the implementation files for device drivers and services as shared libraries. 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci Create the `BUILD.gn` file with the following content in the `drivers/peripheral/nnrt/v2_0/hdi_cpu_service/` directory. For details about how to set related parameters, see [Compilation and Building](https://gitee.com/openharmony/build). 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci ```shell 129e41f4b71Sopenharmony_ci import("//build/ohos.gni") 130e41f4b71Sopenharmony_ci import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci ohos_shared_library("libnnrt_service_2.0") { 133e41f4b71Sopenharmony_ci include_dirs = [] 134e41f4b71Sopenharmony_ci sources = [ 135e41f4b71Sopenharmony_ci "src/nnrt_device_service.cpp", 136e41f4b71Sopenharmony_ci "src/node_functions.cpp", 137e41f4b71Sopenharmony_ci "src/node_registry.cpp", 138e41f4b71Sopenharmony_ci "src/prepared_model_service.cpp", 139e41f4b71Sopenharmony_ci "src/shared_buffer_parser.cpp", 140e41f4b71Sopenharmony_ci "src/validation.cpp", 141e41f4b71Sopenharmony_ci ] 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci external_deps = [ 144e41f4b71Sopenharmony_ci "c_utils:utils", 145e41f4b71Sopenharmony_ci "drivers_interface_nnrt:libnnrt_stub_2.0", 146e41f4b71Sopenharmony_ci "hdf_core:libhdf_utils", 147e41f4b71Sopenharmony_ci "hilog_native:libhilog", 148e41f4b71Sopenharmony_ci "ipc:ipc_core", 149e41f4b71Sopenharmony_ci ] 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci install_images = [ chipset_base_dir ] 152e41f4b71Sopenharmony_ci subsystem_name = "hdf" 153e41f4b71Sopenharmony_ci part_name = "drivers_peripheral_nnrt" 154e41f4b71Sopenharmony_ci } 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci ohos_shared_library("libnnrt_driver") { 157e41f4b71Sopenharmony_ci include_dirs = [] 158e41f4b71Sopenharmony_ci sources = [ "src/nnr_device_driver.cpp" ] 159e41f4b71Sopenharmony_ci deps = [ ":libnnrt_service_2.0" ] 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci external_deps = [ 162e41f4b71Sopenharmony_ci "c_utils:utils", 163e41f4b71Sopenharmony_ci "drivers_interface_nnrt:libnnrt_stub_2.0", 164e41f4b71Sopenharmony_ci "hdf_core:libhdf_host", 165e41f4b71Sopenharmony_ci "hdf_core:libhdf_ipc_adapter", 166e41f4b71Sopenharmony_ci "hdf_core:libhdf_utils", 167e41f4b71Sopenharmony_ci "hdf_core:libhdi", 168e41f4b71Sopenharmony_ci "hilog_native:libhilog", 169e41f4b71Sopenharmony_ci "ipc:ipc_core", 170e41f4b71Sopenharmony_ci ] 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci install_images = [ chipset_base_dir ] 173e41f4b71Sopenharmony_ci subsystem_name = "hdf" 174e41f4b71Sopenharmony_ci part_name = "drivers_peripheral_nnrt" 175e41f4b71Sopenharmony_ci } 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci group("hdf_nnrt_service") { 178e41f4b71Sopenharmony_ci deps = [ 179e41f4b71Sopenharmony_ci ":libnnrt_driver", 180e41f4b71Sopenharmony_ci ":libnnrt_service_2.0", 181e41f4b71Sopenharmony_ci ] 182e41f4b71Sopenharmony_ci } 183e41f4b71Sopenharmony_ci ``` 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci Add `group("hdf_nnrt_service")` to the `drivers/peripheral/nnrt/v2_0/BUILD.gn` file so that it can be referenced at a higher directory level. 186e41f4b71Sopenharmony_ci ```shell 187e41f4b71Sopenharmony_ci if (defined(ohos_lite)) { 188e41f4b71Sopenharmony_ci group("nnrt_entry") { 189e41f4b71Sopenharmony_ci deps = [] 190e41f4b71Sopenharmony_ci } 191e41f4b71Sopenharmony_ci } else { 192e41f4b71Sopenharmony_ci group("nnrt_entry") { 193e41f4b71Sopenharmony_ci deps = [ "./hdi_cpu_service:hdf_nnrt_service" ] 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci } 196e41f4b71Sopenharmony_ci ``` 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci Create the `drivers/peripheral/nnrt/bundle.json` file to define the new `drivers_peripheral_nnrt` component. 199e41f4b71Sopenharmony_ci ```json 200e41f4b71Sopenharmony_ci { 201e41f4b71Sopenharmony_ci "name": "drivers_peripheral_nnrt", 202e41f4b71Sopenharmony_ci "description": "Neural network runtime device driver", 203e41f4b71Sopenharmony_ci "version": "4.0", 204e41f4b71Sopenharmony_ci "license": "Apache License 2.0", 205e41f4b71Sopenharmony_ci "component": { 206e41f4b71Sopenharmony_ci "name": "drivers_peripheral_nnrt", 207e41f4b71Sopenharmony_ci "subsystem": "hdf", 208e41f4b71Sopenharmony_ci "syscap": [""], 209e41f4b71Sopenharmony_ci "adapter_system_type": ["standard"], 210e41f4b71Sopenharmony_ci "rom": "1024KB", 211e41f4b71Sopenharmony_ci "ram": "2048KB", 212e41f4b71Sopenharmony_ci "deps": { 213e41f4b71Sopenharmony_ci "components": [ 214e41f4b71Sopenharmony_ci "c_utils", 215e41f4b71Sopenharmony_ci "hdf_core", 216e41f4b71Sopenharmony_ci "hilog_native", 217e41f4b71Sopenharmony_ci "ipc" 218e41f4b71Sopenharmony_ci ], 219e41f4b71Sopenharmony_ci "third_part": [ 220e41f4b71Sopenharmony_ci "bounds_checking_function" 221e41f4b71Sopenharmony_ci ] 222e41f4b71Sopenharmony_ci }, 223e41f4b71Sopenharmony_ci "build": { 224e41f4b71Sopenharmony_ci "sub_component": [ 225e41f4b71Sopenharmony_ci "//drivers/peripheral/nnrt/v2_0:nnrt_entry" 226e41f4b71Sopenharmony_ci ], 227e41f4b71Sopenharmony_ci "test": [ 228e41f4b71Sopenharmony_ci ], 229e41f4b71Sopenharmony_ci "inner_kits": [ 230e41f4b71Sopenharmony_ci ] 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci } 233e41f4b71Sopenharmony_ci } 234e41f4b71Sopenharmony_ci ``` 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci#### Declaring the HDI Service 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci In the `uhdf` directory, declare the user-mode driver and services in the `.hcs` file of the corresponding product. For example, for the RK3568 chip, add the following configuration to the `vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs` file: 239e41f4b71Sopenharmony_ci ```text 240e41f4b71Sopenharmony_ci nnrt :: host { 241e41f4b71Sopenharmony_ci hostName = "nnrt_host"; 242e41f4b71Sopenharmony_ci priority = 50; 243e41f4b71Sopenharmony_ci uid = ""; 244e41f4b71Sopenharmony_ci gid = ""; 245e41f4b71Sopenharmony_ci caps = ["DAC_OVERRIDE", "DAC_READ_SEARCH"]; 246e41f4b71Sopenharmony_ci nnrt_device :: device { 247e41f4b71Sopenharmony_ci device0 :: deviceNode { 248e41f4b71Sopenharmony_ci policy = 2; 249e41f4b71Sopenharmony_ci priority = 100; 250e41f4b71Sopenharmony_ci moduleName = "libnnrt_driver.z.so"; 251e41f4b71Sopenharmony_ci serviceName = "nnrt_device_service"; 252e41f4b71Sopenharmony_ci } 253e41f4b71Sopenharmony_ci } 254e41f4b71Sopenharmony_ci } 255e41f4b71Sopenharmony_ci ``` 256e41f4b71Sopenharmony_ci> **NOTE**<br>After modifying the `.hcs` file, you need to delete the `out` folder and compile the file again for the modification to take effect. 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci#### Configuring the User ID and Group ID of the Host Process 259e41f4b71Sopenharmony_ci In the scenario of creating an nnrt_host process, you need to configure the user ID and group ID of the corresponding process. The user ID is configured in the `base/startup/init/services/etc/passwd` file, and the group ID is configured in the `base/startup/init/services/etc/group` file. 260e41f4b71Sopenharmony_ci ```text 261e41f4b71Sopenharmony_ci # Add the user ID in base/startup/init/services/etc/passwd. 262e41f4b71Sopenharmony_ci nnrt_host:x:3311:3311:::/bin/false 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci # Add the group ID in base/startup/init/services/etc/group. 265e41f4b71Sopenharmony_ci nnrt_host:x:3311: 266e41f4b71Sopenharmony_ci ``` 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci#### Configuring SELinux 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ciThe SELinux feature has been enabled for the OpenHarmony. You need to configure SELinux rules for the new processes and services so that they can run the host process to access certain resources, release HDI services, etc. 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci1. Add the following configuration to the `base/security/selinux/sepolicy/ohos_policy/drivers/adapter/public/hdf_service_contexts` file: 273e41f4b71Sopenharmony_ci ```text 274e41f4b71Sopenharmony_ci # Add the security context configuration. 275e41f4b71Sopenharmony_ci nnrt_device_service u:object_r:hdf_nnrt_device_service:s0 276e41f4b71Sopenharmony_ci ``` 277e41f4b71Sopenharmony_ci > **NOTE**<br>`nnrt_host` indicates the process name configured in [Declaring the HDI Service](#declaring-the-hdi-service). 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci2. Add the following configuration to the `base/security/selinux/sepolicy/ohos_policy/drivers/adapter/public/hdf_service.te` file: 280e41f4b71Sopenharmony_ci ```text 281e41f4b71Sopenharmony_ci # Add the security context configuration. 282e41f4b71Sopenharmony_ci type hdf_nnrt_device_service, hdf_service_attr; 283e41f4b71Sopenharmony_ci ``` 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci3. Add the following configuration to the `base/security/selinux/sepolicy/ohos_policy/drivers/adapter/public/hdfdomain.te` file: 286e41f4b71Sopenharmony_ci ```text 287e41f4b71Sopenharmony_ci # Add the security context configuration. 288e41f4b71Sopenharmony_ci neverallow { domain -hdfdomain -sadomain } { hdfdomain -nnrt_host -allocator_host -hdf_public_domain }:binder call; 289e41f4b71Sopenharmony_ci ``` 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci4. Add the following configuration to the `base/security/selinux/sepolicy/ohos_policy/drivers/adapter/public/type.te` file: 292e41f4b71Sopenharmony_ci ```text 293e41f4b71Sopenharmony_ci # Add the security context configuration. 294e41f4b71Sopenharmony_ci type nnrt_host, hdfdomain, domain; 295e41f4b71Sopenharmony_ci ``` 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci5. Add the following configuration to the `base/security/selinux/sepolicy/ohos_policy/drivers/adapter/vendor/hdf_devmgr.te` file: 298e41f4b71Sopenharmony_ci ```text 299e41f4b71Sopenharmony_ci # Add the security context configuration. 300e41f4b71Sopenharmony_ci allow hdf_devmgr nnrt_host:binder { call transfer }; 301e41f4b71Sopenharmony_ci allow hdf_devmgr nnrt_host:dir { search }; 302e41f4b71Sopenharmony_ci allow hdf_devmgr nnrt_host:file { open read write }; 303e41f4b71Sopenharmony_ci allow hdf_devmgr nnrt_host:process { getattr }; 304e41f4b71Sopenharmony_ci ``` 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci6. Add the following configuration to the `base/security/selinux/sepolicy/ohos_policy/drivers/adapter/vendor/init.te` file: 307e41f4b71Sopenharmony_ci ```text 308e41f4b71Sopenharmony_ci # Add the security context configuration. 309e41f4b71Sopenharmony_ci allow init nnrt_host:process { rlimitinh siginh transition }; 310e41f4b71Sopenharmony_ci ``` 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci7. Modify the configuration in the `base/security/selinux/sepolicy/ohos_policy/startup/init/public/chipset_init.te` file. 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci Find the line containing `chipset_init`. 315e41f4b71Sopenharmony_ci ```text 316e41f4b71Sopenharmony_ci allow chipset_init { light_host input_user_host wifi_host camera_host power_host audio_host }:process { rlimitinh siginh transition }; 317e41f4b71Sopenharmony_ci ``` 318e41f4b71Sopenharmony_ci Add `nnrt_host` to the `host` list. 319e41f4b71Sopenharmony_ci ```text 320e41f4b71Sopenharmony_ci allow chipset_init { light_host input_user_host wifi_host camera_host power_host audio_host nnrt_host }:process { rlimitinh siginh transition }; 321e41f4b71Sopenharmony_ci ``` 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci8. Create the `nnrt_host.te` configuration file. 324e41f4b71Sopenharmony_ci ```shell 325e41f4b71Sopenharmony_ci # Create the nnrt folder. 326e41f4b71Sopenharmony_ci mkdir base/security/selinux/sepolicy/ohos_policy/drivers/peripheral/nnrt 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci # Create the vendor folder. 329e41f4b71Sopenharmony_ci mkdir base/security/selinux/sepolicy/ohos_policy/drivers/peripheral/nnrt/vendor 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci # Create the `nnrt_host.te` file. 332e41f4b71Sopenharmony_ci touch base/security/selinux/sepolicy/ohos_policy/drivers/peripheral/nnrt/vendor/nnrt_host.te 333e41f4b71Sopenharmony_ci ``` 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci9. Write the following required permissions to the `nnrt_host.te` file: 336e41f4b71Sopenharmony_ci ```text 337e41f4b71Sopenharmony_ci allow nnrt_host dev_hdf_kevent:chr_file { ioctl }; 338e41f4b71Sopenharmony_ci allow nnrt_host hilog_param:file { read open map }; 339e41f4b71Sopenharmony_ci allow nnrt_host sh:binder { transfer }; 340e41f4b71Sopenharmony_ci allow nnrt_host samgr:binder { call }; 341e41f4b71Sopenharmony_ci allow nnrt_host dev_ashmem_file:chr_file { open }; 342e41f4b71Sopenharmony_ci allow nnrt_host dev_unix_socket:dir { search }; 343e41f4b71Sopenharmony_ci allow nnrt_host hdf_device_manager:hdf_devmgr_class { get }; 344e41f4b71Sopenharmony_ci allow nnrt_host hdf_nnrt_device_service:hdf_devmgr_class { add get }; 345e41f4b71Sopenharmony_ci allow nnrt_host dev_console_file:chr_file { read write }; 346e41f4b71Sopenharmony_ci allow nnrt_host debug_param:file { read open map }; 347e41f4b71Sopenharmony_ci allow nnrt_host sa_device_service_manager:samgr_class { get }; 348e41f4b71Sopenharmony_ci allow nnrt_host hdf_devmgr:binder { call transfer }; 349e41f4b71Sopenharmony_ci allow nnrt_host hdf_nnrt_device_service:binder { call }; 350e41f4b71Sopenharmony_ci allow nnrt_host sysfs_devices_system_cpu:file { read open getattr }; 351e41f4b71Sopenharmony_ci allow sh hdf_nnrt_device_service:hdf_devmgr_class { add get }; 352e41f4b71Sopenharmony_ci allow sh hdf_hci_interface_service:hdf_devmgr_class { get }; 353e41f4b71Sopenharmony_ci allow sh nnrt_host:dir { getattr search }; 354e41f4b71Sopenharmony_ci allow sh nnrt_host:file { open read }; 355e41f4b71Sopenharmony_ci allow sh nnrt_host:process { getattr }; 356e41f4b71Sopenharmony_ci allow sh nnrt_host:binder { call }; 357e41f4b71Sopenharmony_ci allow sh nnrt_host:fd { use }; 358e41f4b71Sopenharmony_ci ``` 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci10. Configure access permissions because SELinux uses the trustlist access permission mechanism. Upon service startup, run the `dmesg` command to view the AVC alarm, 361e41f4b71Sopenharmony_ciwhich provides a list of missing permissions. For details about the SELinux configuration, see [security_selinux](https://gitee.com/openharmony/security_selinux/blob/master/README-en.md). 362e41f4b71Sopenharmony_ci ```shell 363e41f4b71Sopenharmony_ci hdc_std shell 364e41f4b71Sopenharmony_ci dmesg | grep nnrt 365e41f4b71Sopenharmony_ci ``` 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci#### Configuring the Component Build Entry 368e41f4b71Sopenharmony_ciAccess the `chipset_common.json` file. 369e41f4b71Sopenharmony_ci```shell 370e41f4b71Sopenharmony_civim //productdefine/common/inherit/chipset_common.json 371e41f4b71Sopenharmony_ci``` 372e41f4b71Sopenharmony_ciAdd the following configuration to `"subsystems"`, `"subsystem":"hdf"`, and `"components"`: 373e41f4b71Sopenharmony_ci```shell 374e41f4b71Sopenharmony_ci{ 375e41f4b71Sopenharmony_ci "component": "drivers_peripheral_nnrt", 376e41f4b71Sopenharmony_ci "features": [] 377e41f4b71Sopenharmony_ci} 378e41f4b71Sopenharmony_ci``` 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci#### Deleting the out Directory and Building the Entire System 381e41f4b71Sopenharmony_ci```shell 382e41f4b71Sopenharmony_ci# Delete the out directory. 383e41f4b71Sopenharmony_cirm -rf ./out 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci# Build the entire system. 386e41f4b71Sopenharmony_ci./build.sh --product-name rk3568 –ccache --jobs=4 387e41f4b71Sopenharmony_ci``` 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci### Commissioning and Verification 391e41f4b71Sopenharmony_ciOn completion of service development, you can use XTS to verify its basic functions and compatibility. The operation procedure is as follows: 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci1. Compile the **hats** test cases of NNRt in the `test/xts/hats/ai/nnrt/hdi` directory. 394e41f4b71Sopenharmony_ci ```shell 395e41f4b71Sopenharmony_ci # Go to the hats directory. 396e41f4b71Sopenharmony_ci cd test/xts/hats 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci # Compile the `hats` test cases. 399e41f4b71Sopenharmony_ci ./build.sh suite=hats system_size=standard product_name=rk3568 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci # Return to the root directory. 402e41f4b71Sopenharmony_ci cd - 403e41f4b71Sopenharmony_ci ``` 404e41f4b71Sopenharmony_ci The test case executable file `HatsHdfNnrtFunctionTest` is exported to the `out/rk3568/suites/hats/testcases/` directory. 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci2. Push the test case executable file to the `/data/local/tmp/` directory of the RK3568 device. 407e41f4b71Sopenharmony_ci ```shell 408e41f4b71Sopenharmony_ci # Push the test case executable file to the device. In this example, the executable file is HatsHdfNnrtFunctionTest. 409e41f4b71Sopenharmony_ci hdc_std file send out/rk3568/suites/hats/testcases/HartsHdfNnrtFunctionTest /data/local/tmp/ 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_ci # Grant required permissions to the test case executable file. 412e41f4b71Sopenharmony_ci hdc_std shell "chmod +x /data/local/tmp/HatsHdfNnrtFunctionTest" 413e41f4b71Sopenharmony_ci ``` 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci3. Execute the test cases and view the result. 416e41f4b71Sopenharmony_ci ```shell 417e41f4b71Sopenharmony_ci # Execute the test cases. 418e41f4b71Sopenharmony_ci hdc_std shell "/data/local/tmp/HatsHdfNnrtFunctionTest" 419e41f4b71Sopenharmony_ci ``` 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ci According to the following example test report, all of the 47 test cases are successfully executed, indicating the service has passed the compatibility test. 422e41f4b71Sopenharmony_ci ```text 423e41f4b71Sopenharmony_ci ... 424e41f4b71Sopenharmony_ci [----------] Global test environment tear-down 425e41f4b71Sopenharmony_ci Gtest xml output finished 426e41f4b71Sopenharmony_ci [==========] 47 tests from 3 test suites ran. (515 ms total) 427e41f4b71Sopenharmony_ci [ PASSED ] 47 tests. 428e41f4b71Sopenharmony_ci ``` 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci### Development Example 431e41f4b71Sopenharmony_ciFor the complete demo code, see [NNRt Service Implementation Example](https://gitee.com/openharmony/ai_neural_network_runtime/tree/master/example/drivers). 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci1. Go to the root directory of OpenHarmony source code and create the `nnrt` folder in the `drivers/peripheral` directory. Then, copy the `example/driver/nnrt/v2_0` folder from the `foundation/ai/neural_network_runtime` directory of NNRt source code to the created `nnrt` folder. 434e41f4b71Sopenharmony_ci ```shell 435e41f4b71Sopenharmony_ci cp -r example/drivers/nnrt/v2_0 drivers/peripheral/nnrt 436e41f4b71Sopenharmony_ci ``` 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci2. Add the `bundle.json` file to the `drivers/peripheral/nnrt` directory. For details about how to write the `bundle.json` file, see [Implementing the HDI Service](#implementing-the-hdi-service). 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci3. Add the dependency files of MindSpore Lite because the demo depends on the CPU operator of MindSpore Lite. 441e41f4b71Sopenharmony_ci - Run the following command in the root directory of the OpenHarmony source code to build the MindSpore Lite dynamic library: The MindSpore source code is stored in `third_party/mindspore` in the root directory of the OpenHarmony source code. 442e41f4b71Sopenharmony_ci ```shell 443e41f4b71Sopenharmony_ci # Build the MindSpore Lite dynamic libraries. 444e41f4b71Sopenharmony_ci ./build.sh --product-name rk3568 -ccaache --jobs 4 --build-target mindspore_lib 445e41f4b71Sopenharmony_ci ``` 446e41f4b71Sopenharmony_ci - Create the `mindspore` folder in the `drivers/peripheral/nnrt/v2_0` directory to store the dynamic libraries and header files of MindSpore Lite. 447e41f4b71Sopenharmony_ci ```shell 448e41f4b71Sopenharmony_ci mkdir drivers/peripheral/nnrt/v2_0/mindspore 449e41f4b71Sopenharmony_ci ``` 450e41f4b71Sopenharmony_ci - Copy the `mindspore-src/source/include` directory in the MindSpore source code to the `drivers/peripheral/nnrt/v2_0/mindspore` directory. 451e41f4b71Sopenharmony_ci ```shell 452e41f4b71Sopenharmony_ci cp third_party/mindspore/mindspore-src/source/include drivers/peripheral/nnrt/v2_0/mindspore 453e41f4b71Sopenharmony_ci ``` 454e41f4b71Sopenharmony_ci - Create and copy the `schema` file of MindSpore Lite. 455e41f4b71Sopenharmony_ci ```shell 456e41f4b71Sopenharmony_ci # Create the mindspore_schema folder. 457e41f4b71Sopenharmony_ci mkdir drivers/peripheral/nnrt/v2_0/hdi_cpu_service/include/mindspore_schema 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci # Copy the MindSpore schema file from the third_party directory. 460e41f4b71Sopenharmony_ci cp third_party/mindspore/mindspore-src/source/mindspore/lite/schema/* drivers/peripheral/nnrt/v2_0/hdi_cpu_service/include/mindspore_schema/ 461e41f4b71Sopenharmony_ci ``` 462e41f4b71Sopenharmony_ci - Copy the MindSpore Lite dynamic library to the `mindspore` directory. 463e41f4b71Sopenharmony_ci ```shell 464e41f4b71Sopenharmony_ci # Create the `mindspore` folder in the `drivers/peripheral/nnrt/v2_0/mindspore` directory. 465e41f4b71Sopenharmony_ci mkdir drivers/peripheral/nnrt/v2_0/mindspore/mindspore 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci # Copy the MindSpore dynamic libraries from the `out` folder to the `drivers/peripheral/nnrt/v2_0/mindspore/mindspore` directory. 468e41f4b71Sopenharmony_ci cp out/rk3568/package/phone/system/lib/libmindspore-lite.so drivers/peripheral/nnrt/v2_0/mindspore/mindspore/ 469e41f4b71Sopenharmony_ci ``` 470e41f4b71Sopenharmony_ci4. Follow the [development procedure](#development-procedure) to complete other configurations. 471