K i",ddlZddlZddlmZmZmZmZddlZddl Z ddl mZm Z m Z m Z mZ ddeee j fdeeeefdefdZ ddeee j fdeeej&fdeeeefddfd Zdd ed edeee j ffd Z ddeeej&fdeee j ffd Z ddeeej.fd edeee j ffdZdeee j fdeeej2ffdZe j6de j8de j:de j<de j>de j@de jBde jDde jFde jHde jJde jLde jNdi Z(e jHe j8e j>e j<e j6e j:e j@e jDe jBe jFe jJe jLd Z)e j6ej6e j8ej8e j:ej:e j<ej>e j>ej>e j@ej@e jBejBe jDejDe jFe#e jHejHe jJejBe jLejBi Z*dede jVfdZ,deee j ffdZ-de j dedefdZ.deee j fdeeeeefffdZ/y) N)AnyDictOptionalUnion)numpy deserialize safe_open serializeserialize_filetensorsmetadatareturncHtt||}t|}|S)a) Saves a dictionary of tensors into raw bytes in safetensors format. Args: tensors (`Dict[str, paddle.Tensor]`): The incoming tensors. Tensors need to be contiguous and dense. metadata (`Dict[str, str]`, *optional*, defaults to `None`): Optional text only metadata you might want to save in your header. For instance it can be useful to specify more about the underlying tensors. This is purely informative and does not affect tensor loading. Returns: `bytes`: The raw bytes representing the format Example: ```python from safetensors.paddle import save import paddle tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))} byte_data = save(tensors) ``` r )r _flattenbytes)r r serializedresults X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/safetensors/paddle.pysaver s$68G,x@J : F Mfilenamec2tt|||y)am Saves a dictionary of tensors into raw bytes in safetensors format. Args: tensors (`Dict[str, paddle.Tensor]`): The incoming tensors. Tensors need to be contiguous and dense. filename (`str`, or `os.PathLike`)): The filename we're saving into. metadata (`Dict[str, str]`, *optional*, defaults to `None`): Optional text only metadata you might want to save in your header. For instance it can be useful to specify more about the underlying tensors. This is purely informative and does not affect tensor loading. Returns: `None` Example: ```python from safetensors.paddle import save_file import paddle tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))} save_file(tensors, "model.safetensors") ``` rN)r r)r rr s r save_filer+s>8G$hBrdatadevicectjdk\rt|}t||St j |}t ||S)a Loads a safetensors file into paddle format from pure bytes. Args: data (`bytes`): The content of a safetensors file Returns: `Dict[str, paddle.Tensor]`: dictionary that contains name as key, value as `paddle.Tensor` on cpu Example: ```python from safetensors.paddle import load file_path = "./my_folder/bert.safetensors" with open(file_path, "rb") as f: data = f.read() loaded = load(data) ``` 3.2.0)paddle __version__r _view2paddlerload _np2paddle)rrflats rr"r"MsD.W$4 D&))zz$$''rci}tjdk\rCt|d|5}|jD]}|j |||< ddd|St j |}t||}|S#1swY|SxYw)a Loads a safetensors file into paddle format. Args: filename (`str`, or `os.PathLike`)): The name of the file which contains the tensors device (`Union[Dict[str, any], str]`, *optional*, defaults to `cpu`): The device where the tensors need to be located after load. available options are all regular paddle device locations Returns: `Dict[str, paddle.Tensor]`: dictionary that contains name as key, value as `paddle.Tensor` Example: ```python from safetensors.paddle import load_file file_path = "./my_folder/bert.safetensors" loaded = load_file(file_path) ``` rr) frameworkrN)rr r offset_keys get_tensorr load_filer#)rrrfkr$s rr)r)ls2F W$ x8F C ,q]]_ ,LLOq  , , Mx(D&) M  , Ms *A<<B numpy_dictcj|jD]\}}tj||||<!|S)Nplace)itemsr to_tensor)r,rr+vs rr#r#s<  ":1((&9 1 : r paddle_dictc|jD]4\}}|jjj||<6|SN)r0detachcpur)r3r+r2s r _paddle2npr8sC!!#21)//1 A2 r) F64F32F16BF16I64I32I16I8U8BOOLF8_E4M3F8_E5M2 dtype_strct|Sr5)_TYPES)rIs r _getdtyperLs ) rci}|D]\}}t|d}t|ddk(r2td|dDsJtj|d|}nUtj j j|d|j|d}|dk7r|j|}tjdk(r5tj|jjd | }|||<|S) Ndtyperrc3&K|] }|dk( yw)rN).0xs r z_view2paddle..s2!qAv2sshape)rNr7bigFinplacer.)rLlenanyremptybasecore frombufferreshapetosys byteorderr1rbyteswap)safeviewrrr+r2rNarrs rr!r!s F 1!G*% qy>Q 2qz22 22,,qz7C++""--ai?GG' SCffVn ==E !""399;#7#7#7#FfUCq   Mrtensornamec|jstd|d|jjs|j }ddl}ddl}t|j|jj}t|j}||z}|j}|dk(ry|j||j|j }|j"j%||f} t&j(dk(r4t*|j} | j-| j/d} | j1S)Nz1You are trying to save a non contiguous tensor: `a` which is not allowed. It either means you are trying to save tensors which are reference of each other in which case it's recommended to save only the full tensors, and reslice at load time, or simply call `.contiguous()` on your tensor to pack it before saving.rrrUFrV) is_contiguous ValueErrorr/ is_cpu_placer7ctypesrintprodrTitem_SIZErNdata_ptrcastPOINTERc_ubyte ctypeslibas_arrayr`raNPDTYPESviewrbtobytes) rerfrknplengthbytes_per_item total_bytesptrnewptrrnpdtypes r_tobytesrs    !?vF& &  << $ $ &&++- .F6<<(N>)K // C ax [[fnnV^^< =F << + 8D }}6<<(yy!**5*9 <<>rc t|tstdt||j D]9\}}t|t j r!td|dt||j Dcic]E\}}|t|jjdd|jt||dGc}}Scc}}w)Nz5Expected a dict of [str, paddle.Tensor] but received zKey `z2` is invalid, expected paddle.Tensor but received .)rNrTr) isinstancedictrityper0rTensorstrrNsplitrTr)r r+r2s rrrs gt $CDM? S   1!V]]+sLTRSWIV MMO   Aq \'',R0WWQN    sA Cr5)r7)0osr`typingrrrrrryr safetensorsrr r r rrrrPathLikerr"r)ndarrayr#arrayr8int64float32int32bfloat16float16int16uint8int8boolfloat64 float8_e4m3fn float8_e5m2 complex64rorKrvrNrLr!rrrPrrrs -- PPMQ #v}}$ %19$sCx.1I F*.C #v}}$ %CC$%CtCH~&C CD(u(c(d3 3E.F(@/4!C$%! #v}} !J6;S"**_%/2 #v}} Dfmm!34c288m9L LL! NNA LL! OOQ NNA LL! LL! KK KK NNA !  a . >> >> >> OO << << << ++ ,, KK##!!   LL"(( NNBJJ LL"(( OORZZ NNBJJ LL"(( LL"(( KK KK NNBJJ "((  $d3 +=&>& V]] # % Fd3 -.4T#s(^8K3Lr