candle icon indicating copy to clipboard operation
candle copied to clipboard

feat: Return Cow on CpuStorage

Open junjunjd opened this issue 1 year ago • 5 comments

Return a Cow from to_cpu_storage to avoid unnecessary copy.

Address one of the issues in https://github.com/huggingface/candle/issues/1699

junjunjd avatar Feb 20 '24 08:02 junjunjd

@LaurentMazare Could I get a review on this small MR?

junjunjd avatar Feb 28 '24 07:02 junjunjd

Would you mind adding some benchmarks to show how much this improves on GPU model loading, ideally with the details of the config this was run on? This would be helpful to check how much of an improvement this would actually bring.

LaurentMazare avatar Feb 28 '24 07:02 LaurentMazare

@LaurentMazare Sure, I will work on that.

junjunjd avatar Feb 28 '24 07:02 junjunjd

cargo build fails for me on

command: cargo build --features metal

error[E0053]: method `to_cpu_storage` has an incompatible type for trait
   --> candle-core/src/metal_backend.rs:327:33
    |
327 |     fn to_cpu_storage(&self) -> Result<CpuStorage> {
    |                                 ^^^^^^^^^^^^^^^^^^
    |                                 |
    |                                 expected `Cow<'_, CpuStorage>`, found `CpuStorage`
    |                                 help: change the output type to match the trait: `std::result::Result<Cow<'_, CpuStorage>, error::Error>`
    |
note: type in trait
   --> candle-core/src/backend.rs:14:33
    |
14  |     fn to_cpu_storage(&self) -> Result<Cow<'_, CpuStorage>>;
    |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: expected signature `fn(&metal_backend::MetalStorage) -> std::result::Result<Cow<'_, CpuStorage, >, _>`
               found signature `fn(&metal_backend::MetalStorage) -> std::result::Result<CpuStorage, _>`

For more information about this error, try `rustc --explain E0053`.
error: could not compile `candle-core` (lib) due to previous error

perhaps the changes need to be also implemented in metal (and cude backends)?

aorticweb avatar Feb 28 '24 17:02 aorticweb

@aorticweb Thanks. This is fixed now.

junjunjd avatar Mar 01 '24 06:03 junjunjd