mirror of
https://github.com/hanzoai/ml.git
synced 2026-11-20 22:54:34 +00:00
- declare quantized_lfm2 in models/mod.rs (file existed, module never wired; broke the quantized-lfm2 example on cargo test) - trainer.rs metal arm: anyhow::anyhow! instead of String.into() (E0277 on --features metal) - clippy -D warnings sweep: .clamp() in dsv4_qat, FRAC_1_SQRT_2 in hanzo-3d test, doc-list indents, rename misnamed run_unary_benchmark, cfg-gated allows for rocm-only field/dim reads, drop redundant trim - workspace clippy allows: needless_range_loop + too_many_arguments (quant-kernel idioms) - rust-ci: install cc on the clippy runner (image ships without a C toolchain)
142 lines
4.8 KiB
Rust
142 lines
4.8 KiB
Rust
use hanzo_ml::{test_device, Device, IndexOp, Result, Tensor};
|
|
|
|
fn contiguous(device: &Device) -> Result<()> {
|
|
let tensor = Tensor::arange(0u32, 24u32, device)?.reshape((2, 3, 4))?;
|
|
assert_eq!(
|
|
tensor.to_vec3::<u32>()?,
|
|
&[
|
|
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]],
|
|
[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]
|
|
]
|
|
);
|
|
assert_eq!(
|
|
tensor.t()?.contiguous()?.to_vec3::<u32>()?,
|
|
&[
|
|
[[0, 4, 8], [1, 5, 9], [2, 6, 10], [3, 7, 11]],
|
|
[[12, 16, 20], [13, 17, 21], [14, 18, 22], [15, 19, 23]]
|
|
]
|
|
);
|
|
assert_eq!(
|
|
tensor.transpose(0, 1)?.contiguous()?.to_vec3::<u32>()?,
|
|
&[
|
|
[[0, 1, 2, 3], [12, 13, 14, 15]],
|
|
[[4, 5, 6, 7], [16, 17, 18, 19]],
|
|
[[8, 9, 10, 11], [20, 21, 22, 23]]
|
|
]
|
|
);
|
|
assert_eq!(
|
|
tensor.transpose(0, 1)?.flatten_all()?.to_vec1::<u32>()?,
|
|
&[0, 1, 2, 3, 12, 13, 14, 15, 4, 5, 6, 7, 16, 17, 18, 19, 8, 9, 10, 11, 20, 21, 22, 23]
|
|
);
|
|
assert_eq!(
|
|
tensor
|
|
.i(1..)?
|
|
.transpose(0, 1)?
|
|
.contiguous()?
|
|
.to_vec3::<u32>()?,
|
|
&[[[12, 13, 14, 15]], [[16, 17, 18, 19]], [[20, 21, 22, 23]]]
|
|
);
|
|
assert_eq!(
|
|
tensor.transpose(0, 2)?.contiguous()?.to_vec3::<u32>()?,
|
|
&[
|
|
[[0, 12], [4, 16], [8, 20]],
|
|
[[1, 13], [5, 17], [9, 21]],
|
|
[[2, 14], [6, 18], [10, 22]],
|
|
[[3, 15], [7, 19], [11, 23]]
|
|
]
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
test_device!(contiguous, contiguous_cpu, contiguous_gpu, contiguous_metal);
|
|
|
|
#[test]
|
|
fn strided_blocks() -> Result<()> {
|
|
use hanzo_ml::Device::Cpu;
|
|
let tensor = Tensor::arange(0u32, 24u32, &Cpu)?.reshape((2, 3, 4))?;
|
|
match tensor.strided_blocks() {
|
|
hanzo_ml::StridedBlocks::SingleBlock { start_offset, len } => {
|
|
assert_eq!(start_offset, 0);
|
|
assert_eq!(len, 24);
|
|
}
|
|
other => panic!("unexpected block structure {other:?}"),
|
|
};
|
|
let tensor = Tensor::arange(0u32, 26u32, &Cpu)?
|
|
.i(2..)?
|
|
.reshape((2, 3, 4))?;
|
|
match tensor.strided_blocks() {
|
|
hanzo_ml::StridedBlocks::SingleBlock { start_offset, len } => {
|
|
assert_eq!(start_offset, 2);
|
|
assert_eq!(len, 24);
|
|
}
|
|
other => panic!("unexpected block structure {other:?}"),
|
|
};
|
|
let tensor = Tensor::arange(0u32, 24u32, &Cpu)?.reshape((2, 3, 4))?;
|
|
let tensor = tensor.i(1)?;
|
|
match tensor.strided_blocks() {
|
|
hanzo_ml::StridedBlocks::SingleBlock { start_offset, len } => {
|
|
assert_eq!(start_offset, 12);
|
|
assert_eq!(len, 12);
|
|
}
|
|
other => panic!("unexpected block structure {other:?}"),
|
|
};
|
|
let tensor = Tensor::arange(0u32, 24u32, &Cpu)?.reshape((2, 3, 4))?;
|
|
let tensor = tensor.i((.., 1))?.contiguous()?;
|
|
match tensor.strided_blocks() {
|
|
hanzo_ml::StridedBlocks::SingleBlock { start_offset, len } => {
|
|
assert_eq!(start_offset, 0);
|
|
assert_eq!(len, 8);
|
|
assert_eq!(tensor.to_vec2::<u32>()?, &[[4, 5, 6, 7], [16, 17, 18, 19]]);
|
|
}
|
|
other => panic!("unexpected block structure {other:?}"),
|
|
};
|
|
let tensor = Tensor::arange(0u32, 24u32, &Cpu)?.reshape((2, 3, 4))?;
|
|
let tensor = tensor.i((.., 1))?;
|
|
match tensor.strided_blocks() {
|
|
hanzo_ml::StridedBlocks::UniformBlocks {
|
|
start_offset,
|
|
block_len,
|
|
count,
|
|
src_stride,
|
|
} => {
|
|
assert_eq!(start_offset, 4);
|
|
assert_eq!(block_len, 4);
|
|
assert_eq!(count, 2);
|
|
assert_eq!(src_stride, 12);
|
|
}
|
|
other => panic!("unexpected block structure {other:?}"),
|
|
};
|
|
let tensor = Tensor::arange(0u32, 24u32, &Cpu)?.reshape((2, 3, 4))?;
|
|
match tensor.t()?.strided_blocks() {
|
|
hanzo_ml::StridedBlocks::MultipleBlocks {
|
|
block_start_index,
|
|
block_len,
|
|
} => {
|
|
assert_eq!(block_len, 1);
|
|
assert_eq!(
|
|
block_start_index.collect::<Vec<_>>(),
|
|
&[
|
|
0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11, 12, 16, 20, 13, 17, 21, 14, 18, 22, 15,
|
|
19, 23
|
|
]
|
|
)
|
|
}
|
|
other => panic!("unexpected block structure {other:?}"),
|
|
};
|
|
let tensor = Tensor::arange(0u32, 24u32, &Cpu)?.reshape((2, 3, 4))?;
|
|
match tensor.transpose(0, 1)?.strided_blocks() {
|
|
hanzo_ml::StridedBlocks::MultipleBlocks {
|
|
block_start_index,
|
|
block_len,
|
|
} => {
|
|
assert_eq!(block_len, 4);
|
|
assert_eq!(
|
|
block_start_index.collect::<Vec<_>>(),
|
|
&[0, 12, 4, 16, 8, 20]
|
|
)
|
|
}
|
|
other => panic!("unexpected block structure {other:?}"),
|
|
};
|
|
Ok(())
|
|
}
|