-
Notifications
You must be signed in to change notification settings - Fork 3
Trig Functions #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Trig Functions #40
Changes from 3 commits
5db987a
4821f59
d76ee8e
4927869
3110588
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -911,6 +911,245 @@ pub fn log_backward(grad: TensorId, saved: &SavedContext, store: &mut TensorStor | |
| } else { vec![None] } | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // sin | ||
| // ========================================================================= | ||
|
|
||
| #[cfg(any(feature = "cpu", feature = "webgpu"))] | ||
| pub fn sin(a: TensorId, store: &mut TensorStore, tape: &mut Tape) -> TensorId { | ||
| let data: Vec<f32> = store.to_host(a).iter().map(|x| x.sin()).collect(); | ||
| let shape = store.shape(a).to_vec(); | ||
| let out = store.from_vec(data, &shape); | ||
| tape.record(TapeEntry { | ||
| op: BackwardOp::Sin, output_id: out, input_ids: smallvec![a], | ||
| saved: SavedContext::Tensor(a), | ||
| }); | ||
| out | ||
| } | ||
|
|
||
| #[cfg(feature = "cuda")] | ||
| pub fn sin(a: TensorId, store: &mut TensorStore, tape: &mut Tape) -> TensorId { | ||
| let shape = store.shape(a).to_vec(); | ||
| let n = shape_size(&shape); | ||
| let out = store.zeros(&shape); | ||
| let out_ptr = store.dev_ptr(out); | ||
| let a_ptr = store.dev_ptr(a); | ||
| let dev = GpuDevice::instance(); | ||
| let func = dev.get_func("sin_f32"); | ||
| unsafe { | ||
| dev.stream.launch_builder(func) | ||
| .arg(&out_ptr) | ||
| .arg(&a_ptr) | ||
| .arg(&(n as i32)) | ||
| .launch(launch_cfg(n as u32)) | ||
| .unwrap(); | ||
| } | ||
|
Comment on lines
+932
to
+946
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid - Fixed in 3110588. |
||
| tape.record(TapeEntry { | ||
| op: BackwardOp::Sin, output_id: out, input_ids: smallvec![a], | ||
| saved: SavedContext::Tensor(a), | ||
| }); | ||
| out | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // sin_backward | ||
| // ========================================================================= | ||
|
|
||
| #[cfg(any(feature = "cpu", feature = "webgpu"))] | ||
| pub fn sin_backward(grad: TensorId, saved: &SavedContext, store: &mut TensorStore) -> Vec<Option<TensorId>> { | ||
| if let SavedContext::Tensor(inp) = saved { | ||
| let inp_data = store.to_host(*inp); | ||
| let grad_data = store.to_host(grad); | ||
| let data: Vec<f32> = grad_data.iter().zip(&inp_data).map(|(g, x)| g * x.cos()).collect(); | ||
| let shape = store.shape(grad).to_vec(); | ||
| vec![Some(store.from_vec(data, &shape))] | ||
| } else { vec![None] } | ||
| } | ||
|
|
||
| #[cfg(feature = "cuda")] | ||
| pub fn sin_backward(grad: TensorId, saved: &SavedContext, store: &mut TensorStore) -> Vec<Option<TensorId>> { | ||
| if let SavedContext::Tensor(inp) = saved { | ||
| let shape = store.shape(grad).to_vec(); | ||
| let n = shape_size(&shape); | ||
| let result = store.zeros(&shape); | ||
| let result_ptr = store.dev_ptr(result); | ||
| let grad_ptr = store.dev_ptr(grad); | ||
| let inp_ptr = store.dev_ptr(*inp); | ||
| let dev = GpuDevice::instance(); | ||
| let func = dev.get_func("sin_backward_f32"); | ||
| unsafe { | ||
| dev.stream.launch_builder(func) | ||
| .arg(&result_ptr) | ||
| .arg(&grad_ptr) | ||
| .arg(&inp_ptr) | ||
| .arg(&(n as i32)) | ||
| .launch(launch_cfg(n as u32)) | ||
| .unwrap(); | ||
| } | ||
| vec![Some(result)] | ||
| } else { vec![None] } | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // cos | ||
| // ========================================================================= | ||
|
|
||
| #[cfg(any(feature = "cpu", feature = "webgpu"))] | ||
| pub fn cos(a: TensorId, store: &mut TensorStore, tape: &mut Tape) -> TensorId { | ||
| let data: Vec<f32> = store.to_host(a).iter().map(|x| x.cos()).collect(); | ||
| let shape = store.shape(a).to_vec(); | ||
| let out = store.from_vec(data, &shape); | ||
| tape.record(TapeEntry { | ||
| op: BackwardOp::Cos, output_id: out, input_ids: smallvec![a], | ||
| saved: SavedContext::Tensor(a), | ||
| }); | ||
| out | ||
| } | ||
|
|
||
| #[cfg(feature = "cuda")] | ||
| pub fn cos(a: TensorId, store: &mut TensorStore, tape: &mut Tape) -> TensorId { | ||
| let shape = store.shape(a).to_vec(); | ||
| let n = shape_size(&shape); | ||
| let out = store.zeros(&shape); | ||
| let out_ptr = store.dev_ptr(out); | ||
| let a_ptr = store.dev_ptr(a); | ||
| let dev = GpuDevice::instance(); | ||
| let func = dev.get_func("cos_f32"); | ||
| unsafe { | ||
| dev.stream.launch_builder(func) | ||
| .arg(&out_ptr) | ||
| .arg(&a_ptr) | ||
| .arg(&(n as i32)) | ||
| .launch(launch_cfg(n as u32)) | ||
| .unwrap(); | ||
| } | ||
| tape.record(TapeEntry { | ||
| op: BackwardOp::Cos, output_id: out, input_ids: smallvec![a], | ||
| saved: SavedContext::Tensor(a), | ||
| }); | ||
| out | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // cos_backward | ||
| // ========================================================================= | ||
|
|
||
| #[cfg(any(feature = "cpu", feature = "webgpu"))] | ||
| pub fn cos_backward(grad: TensorId, saved: &SavedContext, store: &mut TensorStore) -> Vec<Option<TensorId>> { | ||
| if let SavedContext::Tensor(inp) = saved { | ||
| let inp_data = store.to_host(*inp); | ||
| let grad_data = store.to_host(grad); | ||
| let data: Vec<f32> = grad_data.iter().zip(&inp_data).map(|(g, x)| -g * x.sin()).collect(); | ||
| let shape = store.shape(grad).to_vec(); | ||
| vec![Some(store.from_vec(data, &shape))] | ||
| } else { vec![None] } | ||
| } | ||
|
|
||
| #[cfg(feature = "cuda")] | ||
| pub fn cos_backward(grad: TensorId, saved: &SavedContext, store: &mut TensorStore) -> Vec<Option<TensorId>> { | ||
| if let SavedContext::Tensor(inp) = saved { | ||
| let shape = store.shape(grad).to_vec(); | ||
| let n = shape_size(&shape); | ||
| let result = store.zeros(&shape); | ||
| let result_ptr = store.dev_ptr(result); | ||
| let grad_ptr = store.dev_ptr(grad); | ||
| let inp_ptr = store.dev_ptr(*inp); | ||
| let dev = GpuDevice::instance(); | ||
| let func = dev.get_func("cos_backward_f32"); | ||
| unsafe { | ||
| dev.stream.launch_builder(func) | ||
| .arg(&result_ptr) | ||
| .arg(&grad_ptr) | ||
| .arg(&inp_ptr) | ||
| .arg(&(n as i32)) | ||
| .launch(launch_cfg(n as u32)) | ||
| .unwrap(); | ||
| } | ||
| vec![Some(result)] | ||
| } else { vec![None] } | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // sqrt — sqrt(max(x, 0)); gradient masked to 0 where input <= 0 | ||
| // ========================================================================= | ||
|
|
||
| #[cfg(any(feature = "cpu", feature = "webgpu"))] | ||
| pub fn sqrt(a: TensorId, store: &mut TensorStore, tape: &mut Tape) -> TensorId { | ||
| let data: Vec<f32> = store.to_host(a).iter().map(|x| x.max(0.0).sqrt()).collect(); | ||
| let shape = store.shape(a).to_vec(); | ||
|
Comment on lines
+1073
to
+1079
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. intentional design choice, documented inline (sqrt(max(x, 0)) with gradient masked to 0 for x ≤ 0), and its consistent across CPU/CUDA/WGSL |
||
| let out = store.from_vec(data, &shape); | ||
| tape.record(TapeEntry { | ||
| op: BackwardOp::Sqrt, output_id: out, input_ids: smallvec![a], | ||
| saved: SavedContext::Tensor(a), | ||
| }); | ||
| out | ||
| } | ||
|
|
||
| #[cfg(feature = "cuda")] | ||
| pub fn sqrt(a: TensorId, store: &mut TensorStore, tape: &mut Tape) -> TensorId { | ||
| let shape = store.shape(a).to_vec(); | ||
| let n = shape_size(&shape); | ||
| let out = store.zeros(&shape); | ||
| let out_ptr = store.dev_ptr(out); | ||
| let a_ptr = store.dev_ptr(a); | ||
| let dev = GpuDevice::instance(); | ||
| let func = dev.get_func("sqrt_f32"); | ||
| unsafe { | ||
| dev.stream.launch_builder(func) | ||
| .arg(&out_ptr) | ||
| .arg(&a_ptr) | ||
| .arg(&(n as i32)) | ||
| .launch(launch_cfg(n as u32)) | ||
| .unwrap(); | ||
| } | ||
| tape.record(TapeEntry { | ||
| op: BackwardOp::Sqrt, output_id: out, input_ids: smallvec![a], | ||
| saved: SavedContext::Tensor(a), | ||
| }); | ||
| out | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // sqrt_backward | ||
| // ========================================================================= | ||
|
|
||
| #[cfg(any(feature = "cpu", feature = "webgpu"))] | ||
| pub fn sqrt_backward(grad: TensorId, saved: &SavedContext, store: &mut TensorStore) -> Vec<Option<TensorId>> { | ||
| if let SavedContext::Tensor(inp) = saved { | ||
| let inp_data = store.to_host(*inp); | ||
| let grad_data = store.to_host(grad); | ||
| let data: Vec<f32> = grad_data.iter().zip(&inp_data) | ||
| .map(|(g, x)| if *x > 0.0 { g * 0.5 / x.sqrt() } else { 0.0 }) | ||
| .collect(); | ||
| let shape = store.shape(grad).to_vec(); | ||
| vec![Some(store.from_vec(data, &shape))] | ||
| } else { vec![None] } | ||
| } | ||
|
|
||
| #[cfg(feature = "cuda")] | ||
| pub fn sqrt_backward(grad: TensorId, saved: &SavedContext, store: &mut TensorStore) -> Vec<Option<TensorId>> { | ||
| if let SavedContext::Tensor(inp) = saved { | ||
| let shape = store.shape(grad).to_vec(); | ||
| let n = shape_size(&shape); | ||
| let result = store.zeros(&shape); | ||
| let result_ptr = store.dev_ptr(result); | ||
| let grad_ptr = store.dev_ptr(grad); | ||
| let inp_ptr = store.dev_ptr(*inp); | ||
| let dev = GpuDevice::instance(); | ||
| let func = dev.get_func("sqrt_backward_f32"); | ||
| unsafe { | ||
| dev.stream.launch_builder(func) | ||
| .arg(&result_ptr) | ||
| .arg(&grad_ptr) | ||
| .arg(&inp_ptr) | ||
| .arg(&(n as i32)) | ||
| .launch(launch_cfg(n as u32)) | ||
| .unwrap(); | ||
| } | ||
| vec![Some(result)] | ||
| } else { vec![None] } | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // div | ||
| // ========================================================================= | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.