Files
foxhunt/crates/ml/tests/test_var_gradient_flow.rs
jgrusewski dd62f3fcfd refactor: eliminate candle from entire workspace — tests, examples, Cargo.toml
Final cleanup:
- 61 test files + 5 example files: candle imports replaced
- 8 testing/integration files: migrated to cudarc/ml-core types
- 3 services/trading_service test files: migrated
- Root Cargo.toml: candle-core, candle-nn removed from [workspace.dependencies]
- crates/ml/Cargo.toml: candle-nn dependency removed
- testing/e2e/Cargo.toml: candle-core dependency removed

Zero active candle_core/candle_nn/candle_optimisers code references remain.
Zero candle dependency declarations in any Cargo.toml.
Remaining "candle" strings are exclusively in doc comments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 00:53:47 +01:00

151 lines
4.9 KiB
Rust

#![allow(
clippy::assertions_on_constants,
clippy::assertions_on_result_states,
clippy::clone_on_copy,
clippy::decimal_literal_representation,
clippy::doc_markdown,
clippy::empty_line_after_doc_comments,
clippy::field_reassign_with_default,
clippy::get_unwrap,
clippy::identity_op,
clippy::inconsistent_digit_grouping,
clippy::indexing_slicing,
clippy::integer_division,
clippy::len_zero,
clippy::let_underscore_must_use,
clippy::manual_div_ceil,
clippy::manual_let_else,
clippy::manual_range_contains,
clippy::modulo_arithmetic,
clippy::needless_range_loop,
clippy::non_ascii_literal,
clippy::redundant_clone,
clippy::shadow_reuse,
clippy::shadow_same,
clippy::shadow_unrelated,
clippy::single_match_else,
clippy::str_to_string,
clippy::string_slice,
clippy::tests_outside_test_module,
clippy::too_many_lines,
clippy::unnecessary_wraps,
clippy::unseparated_literal_suffix,
clippy::use_debug,
clippy::useless_vec,
clippy::wildcard_enum_match_arm,
clippy::else_if_without_else,
clippy::expect_used,
clippy::missing_const_for_fn,
clippy::similar_names,
clippy::type_complexity,
clippy::collapsible_else_if,
clippy::doc_lazy_continuation,
clippy::items_after_test_module,
clippy::map_clone,
clippy::multiple_unsafe_ops_per_block,
clippy::unwrap_or_default,
clippy::assign_op_pattern,
clippy::needless_borrow,
clippy::println_empty_string,
clippy::unnecessary_cast,
clippy::used_underscore_binding,
clippy::create_dir,
clippy::implicit_saturating_sub,
clippy::exit,
clippy::expect_fun_call,
clippy::too_many_arguments,
clippy::unnecessary_map_or,
clippy::unwrap_used,
dead_code,
unused_imports,
unused_variables,
clippy::cloned_ref_to_slice_refs,
clippy::neg_multiply,
clippy::while_let_loop,
clippy::bool_assert_comparison,
clippy::excessive_precision,
clippy::trivially_copy_pass_by_ref,
clippy::op_ref,
clippy::redundant_closure,
clippy::unnecessary_lazy_evaluations,
clippy::if_then_some_else_none,
clippy::unnecessary_to_owned,
clippy::single_component_path_imports,
)]
//! Minimal test to understand Var vs Tensor gradient flow with scatter_add
// candle eliminated — test uses native APIs
use ml::MLError;
use tracing::info;
#[test]
fn test_scatter_add_tensor_vs_var() -> Result<(), MLError> {
let device = Device::cuda_if_available(0)?;
info!("Testing Tensor::zeros + scatter_add");
{
// Input that should have gradients
let input = Tensor::ones((2, 3), DType::F32, &device)?;
// Base for scatter (using Tensor::zeros)
let base = Tensor::zeros((2, 3), DType::F32, &device)?;
let indices = Tensor::new(&[[0i64, 1i64, 2i64], [0i64, 1i64, 2i64]], &device)?;
// Scatter
let result = base.scatter_add(&indices, &input, 1)?;
// Compute loss and backward
let loss = result.sum_all()?;
let grads = loss.backward()?;
let has_grads = grads.get(&input).is_some();
info!(has_grads, "Tensor::zeros scatter_add gradient check");
}
info!("Testing Var::zeros + scatter_add");
{
// Input that should have gradients
let input = Tensor::ones((2, 3), DType::F32, &device)?;
// Base for scatter (using Var::zeros)
let base_var = Var::zeros((2, 3), DType::F32, &device)?;
let indices = Tensor::new(&[[0i64, 1i64, 2i64], [0i64, 1i64, 2i64]], &device)?;
// Scatter - need to convert Var to Tensor for scatter_add
let base_tensor = base_var.as_tensor();
let result = base_tensor.scatter_add(&indices, &input, 1)?;
// Compute loss and backward
let loss = result.sum_all()?;
let grads = loss.backward()?;
let has_grads = grads.get(&input).is_some();
info!(has_grads, "Var::zeros scatter_add gradient check");
}
info!("Testing Var::from_tensor (input) + scatter_add");
{
// Input wrapped in Var
let input_tensor = Tensor::ones((2, 3), DType::F32, &device)?;
let input_var = Var::from_tensor(&input_tensor)?;
// Base for scatter (regular Tensor)
let base = Tensor::zeros((2, 3), DType::F32, &device)?;
let indices = Tensor::new(&[[0i64, 1i64, 2i64], [0i64, 1i64, 2i64]], &device)?;
// Scatter - use Var as Tensor
let result = base.scatter_add(&indices, &input_var.as_tensor(), 1)?;
// Compute loss and backward
let loss = result.sum_all()?;
let grads = loss.backward()?;
let has_grads_tensor = grads.get(&input_tensor).is_some();
let has_grads_var = grads.get(input_var.as_tensor()).is_some();
info!(has_grads_tensor, "Var(input) gradient check on tensor");
info!(has_grads_var, "Var(input) gradient check on var");
}
Ok(())
}