Skip to content

Commit

Permalink
chore: add rms/layer norm cpu proptest
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarflakstad committed Nov 20, 2024
1 parent d370c4a commit bb33ff0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions crates/ratchet-core/src/ops/norm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ mod tests {
let ln_prg = r#"
import torch
import torch.nn.functional as F
def layer_norm(input, scale, bias):
(input, scale, bias) = (torch.from_numpy(input), torch.from_numpy(scale), torch.from_numpy(bias))
return F.layer_norm(input, (input.shape[-1],), weight=scale, bias=bias).numpy()
Expand All @@ -427,7 +426,6 @@ def manual_rms_norm(input, scale):
input = input * torch.rsqrt(variance + 1e-5)
return (scale * input).numpy()
"#;

let prg = match var {
NormVariant::LayerNorm => ln_prg,
NormVariant::RMSNorm => rms_prg,
Expand Down Expand Up @@ -489,7 +487,7 @@ def manual_rms_norm(input, scale):

#[test]
fn debug_norm() {
let device = Device::request_device(DeviceRequest::GPU).unwrap();
let device = Device::request_device(DeviceRequest::CPU).unwrap();
let prob = NormProblem {
var: NormVariant::LayerNorm,
B: 2,
Expand All @@ -501,9 +499,14 @@ def manual_rms_norm(input, scale):
}

#[proptest(cases = 64)]
fn test_norm(prob: NormProblem) {
fn test_norm_gpu(prob: NormProblem) {
let device = Device::request_device(DeviceRequest::GPU).unwrap();
println!("prob = {:#?}", prob);
run_norm_trial(&device, prob).unwrap();
}

#[proptest(cases = 64)]
fn test_norm_cpu(prob: NormProblem) {
let device = Device::request_device(DeviceRequest::CPU).unwrap();
run_norm_trial(&device, prob).unwrap();
}
}
2 changes: 1 addition & 1 deletion crates/ratchet-core/src/ops/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def {}(a):
}

fn run_unary_trial(prob: UnaryProblem, device: Device) -> anyhow::Result<()> {
let UnaryProblem { op, B, M, N } = prob;
let UnaryProblem { op, B, M, N: _ } = prob;
let a = Tensor::randn::<f32>(shape![B, M], Device::CPU);

let args = match op {
Expand Down

0 comments on commit bb33ff0

Please sign in to comment.