Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- **`Linear`, `Conv2d` and `Embedding` no longer implement `Clone`.**
Breaking. `Param` is a shared handle by design, so deriving `Clone` on a
layer produced a copy that trained together with the original: setting a
weight on one changed the other, which is weight tying by accident and
had no warning anywhere. Each of the three now has
`deep_clone() -> Result<Self>`, which copies the storage and returns an
independent layer, and `Param::detached_copy()` is the same operation one
level down. `Param` itself still clones as a shared handle — optimizers
and `parameters()` depend on that, and it is now documented as
deliberate rather than implied.

Nothing in the workspace cloned these layers, so the break is confined to
out-of-tree callers; the fix at a call site is `.deep_clone()?`.

- **termlens 0.10.1 → 0.11**, with the vendored skill and the report
action's `cli-version:` pins in `ci.yml` and `stress.yml`
(`check-skill-version.sh` holds all three equal to the dependency). 0.11
Expand Down
18 changes: 17 additions & 1 deletion crates/oxmera-nn/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{Module, init};

/// 2-D convolution: input `[n, c_in, h, w]`, output
/// `[n, c_out, h_out, w_out]`.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Conv2d {
weight: Param,
bias: Option<Param>,
Expand Down Expand Up @@ -48,6 +48,22 @@ impl Conv2d {
}
}

/// Create an independent copy of this layer and all of its parameters.
///
/// Parameters are not implicitly cloneable because [`Param::clone`]
/// intentionally preserves parameter sharing for optimizers.
pub fn deep_clone(&self) -> Result<Self> {
Ok(Self {
weight: self.weight.detached_copy()?,
bias: self.bias.as_ref().map(Param::detached_copy).transpose()?,
in_channels: self.in_channels,
out_channels: self.out_channels,
kernel: self.kernel,
stride: self.stride,
padding: self.padding,
})
}

/// The `[c_out, c_in, kh, kw]` weight handle.
pub fn weight(&self) -> &Param {
&self.weight
Expand Down
13 changes: 12 additions & 1 deletion crates/oxmera-nn/src/embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{Module, init};

/// A learnable lookup table: `I64` indices of any shape in, embeddings of
/// shape `indices.shape() + [dim]` out.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Embedding {
weight: Param,
dim: usize,
Expand All @@ -23,6 +23,17 @@ impl Embedding {
}
}

/// Create an independent copy of this layer and its parameter table.
///
/// Parameters are not implicitly cloneable because [`Param::clone`]
/// intentionally preserves parameter sharing for optimizers.
pub fn deep_clone(&self) -> Result<Self> {
Ok(Self {
weight: self.weight.detached_copy()?,
dim: self.dim,
})
}

/// The `[vocab, dim]` weight handle.
pub fn weight(&self) -> &Param {
&self.weight
Expand Down
13 changes: 12 additions & 1 deletion crates/oxmera-nn/src/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{Module, init};

/// `y = x Wᵀ + b`: input `[batch, in_features]`, output
/// `[batch, out_features]`.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Linear {
weight: Param,
bias: Option<Param>,
Expand Down Expand Up @@ -39,6 +39,17 @@ impl Linear {
}
}

/// Create an independent copy of this layer and all of its parameters.
///
/// Parameters are not implicitly cloneable because [`Param::clone`]
/// intentionally preserves parameter sharing for optimizers.
pub fn deep_clone(&self) -> Result<Self> {
Ok(Self {
weight: self.weight.detached_copy()?,
bias: self.bias.as_ref().map(Param::detached_copy).transpose()?,
})
}

/// The `[out, in]` weight handle.
pub fn weight(&self) -> &Param {
&self.weight
Expand Down
14 changes: 13 additions & 1 deletion crates/oxmera-nn/src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

use std::sync::{Arc, RwLock};

use oxmera_core::Result;
use oxmera_tensor::tensor::Tensor;

/// A learnable parameter: a shared, replaceable handle to a
/// gradient-accumulating leaf tensor.
///
/// Modules read the current value each forward pass; optimizers write
/// updated values back through the same handle.
/// updated values back through the same handle. Cloning a `Param` shares
/// that handle intentionally, while [`Param::detached_copy`] creates an
/// independent parameter for explicitly copying a module.
#[derive(Debug, Clone)]
pub struct Param {
inner: Arc<RwLock<Tensor>>,
Expand All @@ -22,6 +25,15 @@ impl Param {
}
}

/// Create an independent parameter with the same current value.
///
/// Unlike [`Clone::clone`], this copies the tensor storage and starts a
/// fresh gradient-accumulating leaf. The copy remains on the same device
/// and keeps the source tensor's dtype and shape.
pub fn detached_copy(&self) -> Result<Self> {
Ok(Self::new(self.value().contiguous_untracked()?))
}

/// The current value (a cheap clone sharing storage and tape state).
pub fn value(&self) -> Tensor {
self.inner.read().expect("param lock poisoned").clone()
Expand Down
74 changes: 74 additions & 0 deletions crates/oxmera-nn/tests/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,77 @@ fn safetensors_round_trip() {
assert_close(&before, &after, 1e-6, "safetensors round trip");
std::fs::remove_file(&path).ok();
}

#[test]
fn deep_clone_detaches_layer_parameters() {
let linear = Linear::new(2, 2, 1);
linear
.weight()
.set(Tensor::from_slice(&[1.0, 2.0, 3.0, 4.0], [2, 2]).unwrap());
linear
.bias()
.unwrap()
.set(Tensor::from_slice(&[5.0, 6.0], [2]).unwrap());
let linear_copy = linear.deep_clone().unwrap();
linear.weight().set(Tensor::zeros([2, 2]));
linear.bias().unwrap().set(Tensor::zeros([2]));
assert_eq!(
linear_copy.weight().value().to_vec_f32().unwrap(),
vec![1.0, 2.0, 3.0, 4.0]
);
assert_eq!(
linear_copy.bias().unwrap().value().to_vec_f32().unwrap(),
vec![5.0, 6.0]
);

let conv = Conv2d::new(1, 1, (2, 2), 1, 0, 2);
conv.weight()
.set(Tensor::from_slice(&[1.0, 2.0, 3.0, 4.0], [1, 1, 2, 2]).unwrap());
let conv_copy = conv.deep_clone().unwrap();
conv.weight().set(Tensor::zeros([1, 1, 2, 2]));
assert_eq!(
conv_copy.weight().value().to_vec_f32().unwrap(),
vec![1.0, 2.0, 3.0, 4.0]
);

let embedding = Embedding::new(2, 2, 3);
embedding
.weight()
.set(Tensor::from_slice(&[1.0, 2.0, 3.0, 4.0], [2, 2]).unwrap());
let embedding_copy = embedding.deep_clone().unwrap();
embedding.weight().set(Tensor::zeros([2, 2]));
assert_eq!(
embedding_copy.weight().value().to_vec_f32().unwrap(),
vec![1.0, 2.0, 3.0, 4.0]
);
}

#[test]
fn a_deep_cloned_layer_is_still_trainable() {
// Value independence is only half of it: a copy that is no longer a
// gradient-accumulating leaf would pass every assertion above and then
// silently never learn. Pin the leaf, and pin that the two layers
// accumulate separately.
let linear = Linear::new(2, 2, 1);
let copy = linear.deep_clone().unwrap();
assert!(
copy.weight().value().requires_grad(),
"a deep-cloned weight must still be a gradient-accumulating leaf"
);

let x = Tensor::from_slice(&[1.0, 1.0], [1, 2]).unwrap();
copy.forward(&x)
.unwrap()
.sum(&[])
.unwrap()
.backward()
.unwrap();
assert!(
copy.weight().grad().is_some(),
"the copy must accumulate its own gradient"
);
assert!(
linear.weight().grad().is_none(),
"the original must not see the copy's gradient"
);
}