Skip to content

Commit

Permalink
Add module mapper book example (#2621)
Browse files Browse the repository at this point in the history
* Add module mapper example

* Fix handles typo
  • Loading branch information
laggui authored Dec 16, 2024
1 parent 53bc165 commit 8a89293
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions burn-book/src/building-blocks/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ Note that the trait doesn't require all methods to be implemented as they are al
perform no operation. If you're only interested in float tensors (like the majority of use cases),
then you can simply implement `map_float` or `visit_float`.

For example, the `ModuleMapper` trait could be implemented to clamp all parameters into the range
`[min, max]`.

```rust, ignore
/// Clamp parameters into the range `[min, max]`.
pub struct Clamp {
/// Lower-bound of the range.
pub min: f32,
/// Upper-bound of the range.
pub max: f32,
}
// Clamp all floating-point parameter tensors between `[min, max]`.
impl<B: Backend> ModuleMapper<B> for Clamp {
fn map_float<const D: usize>(
&mut self,
_id: burn::module::ParamId,
tensor: burn::prelude::Tensor<B, D>,
) -> burn::prelude::Tensor<B, D> {
tensor.clamp(self.min, self.max)
}
}
// Clamp module mapper into the range `[-0.5, 0.5]`
let mut clamp = Clamp {
min: -0.5,
max: 0.5,
};
let model = model.map(&mut clamp);
```

## Module Display

Burn provides a simple way to display the structure of a module and its configuration at a glance.
Expand Down
2 changes: 1 addition & 1 deletion crates/burn-jit/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<R: JitRuntime, F: FloatElement, I: IntElement, BT: BoolElement> ReprBackend
handle.handle
}

fn quantized_tensor(handles: TensorHandle<Self::Handle>) -> QuantizedTensor<Self> {
fn quantized_tensor(handle: TensorHandle<Self::Handle>) -> QuantizedTensor<Self> {
handle.handle
}

Expand Down

0 comments on commit 8a89293

Please sign in to comment.