#serde #types

tch-serde

Serialize/Deserialize tch-rs types with serde

6 releases

new 0.3.1 Jan 10, 2021
0.3.0 Nov 16, 2020
0.2.0 Sep 27, 2020
0.1.2 Jul 21, 2020

#90 in Parser tooling

23 downloads per month

MIT license

17KB
346 lines

tch-serde: Serialize/Deserialize tch-rs types with serde

This crate provides {ser,de}ialization methods for tch-rs common types.

Usage

Add this line to your Cargo.toml to work with this crate.

tch-serde = "0.3"

For example, annotate #[serde(with = "tch_serde::serde_tensor")] attributes to enable serialization on tensor field.

use tch::{Device, Kind, Tensor};

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Example {
    #[serde(with = "tch_serde::serde_tensor")]
    tensor: Tensor,
    #[serde(with = "tch_serde::serde_kind")]
    kind: Kind,
    #[serde(with = "tch_serde::serde_device")]
    device: Device,
}

fn main() {
    let example = Example {
        tensor: Tensor::randn(&[2, 3], (Kind::Float, Device::cuda_if_available())),
        kind: Kind::Float,
        device: Device::Cpu,
    };
    let text = serde_json::to_string_pretty(&example).unwrap();
    println!("{}", text);
}

License

MIT license. See the LICENSE file.

Dependencies

~4.5–6.5MB
~145K SLoC