|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use base64::{Engine as _, engine::general_purpose}; |
| 6 | +use ndarray::{ArrayBase, Dimension, OwnedRepr}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use crate::block_manager::storage::{ |
| 11 | + StorageError, SystemStorage, nixl::NixlRegisterableStorage, nixl::NixlStorage, |
| 12 | +}; |
| 13 | +use nixl_sys::{Agent as NixlAgent, MemType, RegDescList}; |
| 14 | + |
| 15 | +// Decoded media data (image RGB, video frames pixels, ...) |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct DecodedMediaData { |
| 18 | + pub(crate) data: SystemStorage, |
| 19 | + pub(crate) shape: Vec<usize>, |
| 20 | + pub(crate) dtype: String, |
| 21 | +} |
| 22 | + |
| 23 | +// Decoded media data NIXL descriptor (sent to the next step in the pipeline / NATS) |
| 24 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
| 25 | +pub struct RdmaMediaDataDescriptor { |
| 26 | + // b64 agent metadata |
| 27 | + pub(crate) nixl_metadata: String, |
| 28 | + // tensor descriptor |
| 29 | + pub(crate) nixl_descriptor: NixlStorage, |
| 30 | + pub(crate) shape: Vec<usize>, |
| 31 | + pub(crate) dtype: String, |
| 32 | + // reference to the actual data, kept alive while the rdma descriptor is alive |
| 33 | + #[serde(skip, default)] |
| 34 | + #[allow(dead_code)] |
| 35 | + pub(crate) source_storage: Option<Arc<SystemStorage>>, |
| 36 | +} |
| 37 | + |
| 38 | +impl DecodedMediaData { |
| 39 | + pub fn into_rdma_descriptor(self, nixl_agent: &NixlAgent) -> Result<RdmaMediaDataDescriptor> { |
| 40 | + // get NIXL metadata and descriptor |
| 41 | + let mut source_storage = self.data; |
| 42 | + source_storage.nixl_register(nixl_agent, None)?; |
| 43 | + let nixl_descriptor = unsafe { source_storage.as_nixl_descriptor() } |
| 44 | + .ok_or_else(|| anyhow::anyhow!("Cannot convert storage to NIXL descriptor"))?; |
| 45 | + |
| 46 | + let nixl_metadata = get_nixl_metadata(nixl_agent, &source_storage)?; |
| 47 | + Ok(RdmaMediaDataDescriptor { |
| 48 | + nixl_metadata, |
| 49 | + nixl_descriptor, |
| 50 | + shape: self.shape, |
| 51 | + dtype: self.dtype, |
| 52 | + // do not drop / free the storage yet |
| 53 | + source_storage: Some(Arc::new(source_storage)), |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// convert Array{N}<u8> to DecodedMediaData |
| 59 | +// TODO: Array1<f32> for audio |
| 60 | +impl<D: Dimension> TryFrom<ArrayBase<OwnedRepr<u8>, D>> for DecodedMediaData { |
| 61 | + type Error = StorageError; |
| 62 | + |
| 63 | + fn try_from(array: ArrayBase<OwnedRepr<u8>, D>) -> Result<Self, Self::Error> { |
| 64 | + let shape = array.shape().to_vec(); |
| 65 | + let (data, _) = array.into_raw_vec_and_offset(); |
| 66 | + Ok(Self { |
| 67 | + data: SystemStorage::try_from(data)?, |
| 68 | + shape, |
| 69 | + dtype: "uint8".to_string(), |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Get NIXL metadata for a descriptor |
| 75 | +// Avoids cross-request leak possibility and reduces metadata size |
| 76 | +// TODO: pre-allocate a fixed NIXL-registered RAM pool so metadata can be cached on the target? |
| 77 | +pub fn get_nixl_metadata(agent: &NixlAgent, storage: &SystemStorage) -> Result<String> { |
| 78 | + // WAR: Until https://github.com/ai-dynamo/nixl/pull/970 is merged, can't use get_local_partial_md |
| 79 | + let nixl_md = agent.get_local_md()?; |
| 80 | + // let mut reg_desc_list = RegDescList::new(MemType::Dram)?; |
| 81 | + // reg_desc_list.add_storage_desc(storage)?; |
| 82 | + // let nixl_partial_md = agent.get_local_partial_md(®_desc_list, None)?; |
| 83 | + |
| 84 | + let b64_encoded = general_purpose::STANDARD.encode(&nixl_md); |
| 85 | + Ok(format!("b64:{}", b64_encoded)) |
| 86 | +} |
| 87 | + |
| 88 | +pub fn get_nixl_agent() -> Result<NixlAgent> { |
| 89 | + let uuid = uuid::Uuid::new_v4(); |
| 90 | + let nixl_agent = NixlAgent::new(&format!("media-loader-{}", uuid))?; |
| 91 | + let (_, ucx_params) = nixl_agent.get_plugin_params("UCX")?; |
| 92 | + nixl_agent.create_backend("UCX", &ucx_params)?; |
| 93 | + Ok(nixl_agent) |
| 94 | +} |
0 commit comments