@@ -110,12 +110,10 @@ impl OffloadBuffer {
110110 }
111111 }
112112 #[ cfg( feature = "cuda" ) ]
113- Device :: Cuda ( _) => {
114- unsafe {
115- ptr_host = result:: malloc_host ( size, 1 ) . unwrap ( ) ;
116- std:: ptr:: copy ( src. as_ptr ( ) as * mut core:: ffi:: c_void , ptr_host, size) ;
117- }
118- }
113+ Device :: Cuda ( _) => unsafe {
114+ ptr_host = result:: malloc_host ( size, 1 ) . unwrap ( ) ;
115+ std:: ptr:: copy ( src. as_ptr ( ) as * mut core:: ffi:: c_void , ptr_host, size) ;
116+ } ,
119117 _ => {
120118 crate :: bail!( "offload buffer only for cuda or gcu device tensors" )
121119 }
@@ -134,72 +132,77 @@ impl OffloadBuffer {
134132 #[ cfg( feature = "gcu" ) ]
135133 Device :: Gcu ( dev) => {
136134 let storage = match self . dtype {
137- DType :: BF16 => crate :: Storage :: Gcu ( dev. storage_from_buffer (
135+ DType :: BF16 => crate :: Storage :: Gcu (
136+ dev. storage_from_buffer ( self . ptr_host as * mut bf16 , self . len ) ?,
137+ ) ,
138+ DType :: F16 => crate :: Storage :: Gcu (
139+ dev. storage_from_buffer ( self . ptr_host as * mut f16 , self . len ) ?,
140+ ) ,
141+ DType :: F32 => crate :: Storage :: Gcu (
142+ dev. storage_from_buffer ( self . ptr_host as * mut f32 , self . len ) ?,
143+ ) ,
144+ DType :: U8 => crate :: Storage :: Gcu (
145+ dev. storage_from_buffer ( self . ptr_host as * mut u8 , self . len ) ?,
146+ ) ,
147+ DType :: U32 => crate :: Storage :: Gcu (
148+ dev. storage_from_buffer ( self . ptr_host as * mut u32 , self . len ) ?,
149+ ) ,
150+ DType :: I8 => crate :: Storage :: Gcu (
151+ dev. storage_from_buffer ( self . ptr_host as * mut i8 , self . len ) ?,
152+ ) ,
153+ DType :: I32 => crate :: Storage :: Gcu (
154+ dev. storage_from_buffer ( self . ptr_host as * mut i32 , self . len ) ?,
155+ ) ,
156+ DType :: I64 => crate :: Storage :: Gcu (
157+ dev. storage_from_buffer ( self . ptr_host as * mut i64 , self . len ) ?,
158+ ) ,
159+ DType :: F64 => crate :: Storage :: Gcu (
160+ dev. storage_from_buffer ( self . ptr_host as * mut f64 , self . len ) ?,
161+ ) ,
162+ } ;
163+ Ok ( storage)
164+ }
165+ #[ cfg( feature = "cuda" ) ]
166+ Device :: Cuda ( dev) => {
167+ let storage = match self . dtype {
168+ DType :: BF16 => Storage :: Cuda ( storage_from_buffer (
138169 self . ptr_host as * mut bf16 ,
139170 self . len ,
171+ dev,
140172 ) ?) ,
141- DType :: F16 => crate :: Storage :: Gcu ( dev . storage_from_buffer (
173+ DType :: F16 => Storage :: Cuda ( storage_from_buffer (
142174 self . ptr_host as * mut f16 ,
143175 self . len ,
176+ dev,
144177 ) ?) ,
145- DType :: F32 => crate :: Storage :: Gcu ( dev . storage_from_buffer (
178+ DType :: F32 => Storage :: Cuda ( storage_from_buffer (
146179 self . ptr_host as * mut f32 ,
147180 self . len ,
181+ dev,
148182 ) ?) ,
149- DType :: U8 => crate :: Storage :: Gcu ( dev . storage_from_buffer (
183+ DType :: U8 => Storage :: Cuda ( storage_from_buffer (
150184 self . ptr_host as * mut u8 ,
151185 self . len ,
186+ dev,
152187 ) ?) ,
153- DType :: U32 => crate :: Storage :: Gcu ( dev . storage_from_buffer (
188+ DType :: U32 => Storage :: Cuda ( storage_from_buffer (
154189 self . ptr_host as * mut u32 ,
155190 self . len ,
191+ dev,
156192 ) ?) ,
157- DType :: I8 => crate :: Storage :: Gcu ( dev. storage_from_buffer (
158- self . ptr_host as * mut i8 ,
159- self . len ,
160- ) ?) ,
161- DType :: I32 => crate :: Storage :: Gcu ( dev. storage_from_buffer (
162- self . ptr_host as * mut i32 ,
163- self . len ,
164- ) ?) ,
165- DType :: I64 => crate :: Storage :: Gcu ( dev. storage_from_buffer (
193+ DType :: I64 => Storage :: Cuda ( storage_from_buffer (
166194 self . ptr_host as * mut i64 ,
167195 self . len ,
196+ dev,
168197 ) ?) ,
169- DType :: F64 => crate :: Storage :: Gcu ( dev . storage_from_buffer (
198+ DType :: F64 => Storage :: Cuda ( storage_from_buffer (
170199 self . ptr_host as * mut f64 ,
171200 self . len ,
201+ dev,
172202 ) ?) ,
173203 } ;
174204 Ok ( storage)
175205 }
176- #[ cfg( feature = "cuda" ) ]
177- Device :: Cuda ( dev) => {
178- let storage = match self . dtype {
179- DType :: BF16 => {
180- Storage :: Cuda ( storage_from_buffer ( self . ptr_host as * mut bf16 , self . len , dev) ?)
181- }
182- DType :: F16 => {
183- Storage :: Cuda ( storage_from_buffer ( self . ptr_host as * mut f16 , self . len , dev) ?)
184- }
185- DType :: F32 => {
186- Storage :: Cuda ( storage_from_buffer ( self . ptr_host as * mut f32 , self . len , dev) ?)
187- }
188- DType :: U8 => {
189- Storage :: Cuda ( storage_from_buffer ( self . ptr_host as * mut u8 , self . len , dev) ?)
190- }
191- DType :: U32 => {
192- Storage :: Cuda ( storage_from_buffer ( self . ptr_host as * mut u32 , self . len , dev) ?)
193- }
194- DType :: I64 => {
195- Storage :: Cuda ( storage_from_buffer ( self . ptr_host as * mut i64 , self . len , dev) ?)
196- }
197- DType :: F64 => {
198- Storage :: Cuda ( storage_from_buffer ( self . ptr_host as * mut f64 , self . len , dev) ?)
199- }
200- } ;
201- Ok ( storage)
202- }
203206 _ => crate :: bail!( "not supported device for cpu offloading" ) ,
204207 }
205208 }
@@ -219,11 +222,9 @@ impl Drop for OffloadBuffer {
219222 }
220223 }
221224 #[ cfg( feature = "cuda" ) ]
222- Device :: Cuda ( _) => {
223- unsafe {
224- let _ = result:: free_host ( self . ptr_host ) ;
225- }
226- }
225+ Device :: Cuda ( _) => unsafe {
226+ let _ = result:: free_host ( self . ptr_host ) ;
227+ } ,
227228 _ => { }
228229 }
229230 }
0 commit comments