hyprstream_inference/runtime/
tensor_helpers.rs1use anyhow::{anyhow, Result};
2use tch::Kind as DType;
3use tch::{Device, Kind, Tensor};
5
6pub trait ToIntList {
8 type Output;
9 fn to_intlist(&self) -> Self::Output;
10}
11
12impl ToIntList for [usize; 2] {
13 type Output = [i64; 2];
14 fn to_intlist(&self) -> Self::Output {
15 [self[0] as i64, self[1] as i64]
16 }
17}
18
19impl ToIntList for [usize; 3] {
20 type Output = [i64; 3];
21 fn to_intlist(&self) -> Self::Output {
22 [self[0] as i64, self[1] as i64, self[2] as i64]
23 }
24}
25
26impl ToIntList for [usize; 4] {
27 type Output = [i64; 4];
28 fn to_intlist(&self) -> Self::Output {
29 [
30 self[0] as i64,
31 self[1] as i64,
32 self[2] as i64,
33 self[3] as i64,
34 ]
35 }
36}
37
38impl ToIntList for Vec<usize> {
39 type Output = Vec<i64>;
40 fn to_intlist(&self) -> Self::Output {
41 self.iter().map(|&x| x as i64).collect()
42 }
43}
44
45pub fn clone_tensor(tensor: &Tensor) -> Tensor {
47 let out = Tensor::zeros_like(tensor);
48 tensor.clone(&out)
49}
50
51pub fn square_tensor(tensor: &Tensor) -> Result<Tensor> {
53 Ok(tensor.square())
54}
55
56pub fn broadcast_mul(tensor: &Tensor, other: &Tensor) -> Result<Tensor> {
58 Ok(tensor * other)
59}
60
61pub fn broadcast_add(tensor: &Tensor, other: &Tensor) -> Result<Tensor> {
63 Ok(tensor + other)
64}
65
66pub fn broadcast_sub(tensor: &Tensor, other: &Tensor) -> Result<Tensor> {
68 Ok(tensor - other)
69}
70
71pub fn get_dtype(tensor: &Tensor) -> DType {
73 tensor.kind()
74}
75
76pub fn to_dtype(tensor: &Tensor, dtype: DType) -> Result<Tensor> {
78 Ok(tensor.to_dtype(dtype, false, false))
79}
80
81pub fn cat_tensors(tensors: &[Tensor], dim: i64) -> Result<Tensor> {
83 Ok(Tensor::cat(tensors, dim))
84}
85
86pub fn matmul(a: &Tensor, b: &Tensor) -> Tensor {
88 a.matmul(b)
89}
90
91pub fn to_vec1<T: tch::kind::Element>(tensor: &Tensor) -> Result<Vec<T>> {
93 let cpu_tensor = tensor.to_kind(T::KIND).to(Device::Cpu);
95
96 let numel = cpu_tensor.numel();
98 let _result: Vec<T> = Vec::with_capacity(numel);
99
100 Err(anyhow!("Direct tensor to vec conversion not yet implemented for tch. Use tensor operations instead."))
104}
105
106pub fn scalar_tensor(value: f32, device: Device, dtype: Kind) -> Tensor {
108 Tensor::from_slice(&[value])
109 .to_device(device)
110 .to_kind(dtype)
111}
112
113pub fn dims3(tensor: &Tensor) -> Result<(i64, i64, i64)> {
115 let size = tensor.size();
116 if size.len() != 3 {
117 return Err(anyhow!("Expected 3D tensor, got {}D", size.len()));
118 }
119 Ok((size[0], size[1], size[2]))
120}
121
122pub fn dims4(tensor: &Tensor) -> Result<(i64, i64, i64, i64)> {
124 let size = tensor.size();
125 if size.len() != 4 {
126 return Err(anyhow!("Expected 4D tensor, got {}D", size.len()));
127 }
128 Ok((size[0], size[1], size[2], size[3]))
129}