Skip to content

help on safe implementation of XGBoosterGetModelRaw? #4

@jonathanstrong

Description

@jonathanstrong

hey -

I was trying to implement the other side of the load/save from buffer, XGBoosterGetModelRaw, but I'm stuck, and thought maybe you could help.

This is where I am (in Booster impl):

    /// Returns a `Vec<u8>` with model weights.
    pub fn to_vec(&self) -> XGBResult<Vec<u8>> {
        debug!("Writing Booster to_vec");
        let mut out_len = 0; 
        let mut out_dptr = ptr::null_mut();
        xgb_call!(xgboost_sys::XGBoosterGetModelRaw(self.handle, &mut out_len, out_dptr))?;
        // let bytes: &[u8] = unsafe {
        //     let length: u64 = *(out_len as *const _);
        //     std::slice::from_raw_parts(out_dptr as *const _, length as usize)
        // };
        // let mut out: Vec<u8> = vec![0u8; bytes.len()];
        // out[..].copy_from_slice(bytes);
        // Ok(out)
    }

The commented out section is because I can't get past calling the xgboost function.

I have tried a fairly wide variety of various pointer things as it relates to calling XGBoosterGetModelRaw, but I get SIGSEGV no matter what.

The xgboost api is defined as:

/*!
 * \brief save model into binary raw bytes, return header of the array
 * user must copy the result out, before next xgboost call
 * \param handle handle
 * \param out_len the argument to hold the output length
 * \param out_dptr the argument to hold the output data pointer
 * \return 0 when success, -1 when failure happens
 */
XGB_DLL int XGBoosterGetModelRaw(BoosterHandle handle,
                                 bst_ulong *out_len,
                                 const char **out_dptr);

Any ideas? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions