Skip to main content

hyprstream_inference/runtime/
tensor_helpers.rs

1use anyhow::{anyhow, Result};
2use tch::Kind as DType;
3/// Helper functions for Tch tensor operations
4use tch::{Device, Kind, Tensor};
5
6/// Helper trait to convert usize arrays to i64 for IntList compatibility
7pub 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
45/// Helper for tensor cloning (Tch requires output tensor)
46pub fn clone_tensor(tensor: &Tensor) -> Tensor {
47    let out = Tensor::zeros_like(tensor);
48    tensor.clone(&out)
49}
50
51/// Helper for squared tensor (sqr -> square)
52pub fn square_tensor(tensor: &Tensor) -> Result<Tensor> {
53    Ok(tensor.square())
54}
55
56/// Helper for broadcast multiplication
57pub fn broadcast_mul(tensor: &Tensor, other: &Tensor) -> Result<Tensor> {
58    Ok(tensor * other)
59}
60
61/// Helper for broadcast addition
62pub fn broadcast_add(tensor: &Tensor, other: &Tensor) -> Result<Tensor> {
63    Ok(tensor + other)
64}
65
66/// Helper for broadcast subtraction
67pub fn broadcast_sub(tensor: &Tensor, other: &Tensor) -> Result<Tensor> {
68    Ok(tensor - other)
69}
70
71/// Helper for getting tensor dtype (Tch uses kind())
72pub fn get_dtype(tensor: &Tensor) -> DType {
73    tensor.kind()
74}
75
76/// Helper for converting to dtype with proper arguments
77pub fn to_dtype(tensor: &Tensor, dtype: DType) -> Result<Tensor> {
78    Ok(tensor.to_dtype(dtype, false, false))
79}
80
81/// Helper for concatenating tensors
82pub fn cat_tensors(tensors: &[Tensor], dim: i64) -> Result<Tensor> {
83    Ok(Tensor::cat(tensors, dim))
84}
85
86/// Helper for matmul operations
87pub fn matmul(a: &Tensor, b: &Tensor) -> Tensor {
88    a.matmul(b)
89}
90
91/// Helper to convert tensor to vec
92pub fn to_vec1<T: tch::kind::Element>(tensor: &Tensor) -> Result<Vec<T>> {
93    // Convert to CPU and correct type
94    let cpu_tensor = tensor.to_kind(T::KIND).to(Device::Cpu);
95
96    // Get the data as a slice and convert to vec
97    let numel = cpu_tensor.numel();
98    let _result: Vec<T> = Vec::with_capacity(numel);
99
100    // Use tensor's data pointer to extract values
101    // This is a placeholder - actual implementation would use tensor.data_ptr() or similar
102    // For now, we'll return an error since direct conversion is not available
103    Err(anyhow!("Direct tensor to vec conversion not yet implemented for tch. Use tensor operations instead."))
104}
105
106/// Create a scalar tensor from a value with specified dtype
107pub 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
113/// Get dimensions as 3-tuple
114pub 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
122/// Get dimensions as 4-tuple  
123pub 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}