Files
rustytorch/crates/core/rtx-macros/src/lib.rs
T
osobhandClaude Opus 4.6 02d382d5f6 style: apply rustfmt across all crates and demos
Consistent formatting pass: line wrapping, import sorting, trailing
whitespace removal, let-chain indentation, merged derive attributes,
and unsafe block reformatting.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
2026-04-12 07:01:58 -07:00

119 lines
3.4 KiB
Rust

//! # RustyTorch Derive Macros
//!
//! This crate provides procedural macros for the RustyTorch deep learning framework.
//!
//! ## Available Macros
//!
//! - `#[derive(Module)]` - Automatically implements the `Module` trait for neural network layers
//! - `#[derive(Config)]` - Generates builder pattern configuration structs
//!
//! ## Module Derive
//!
//! The `Module` derive macro automatically generates implementations for:
//! - `parameters()` - Returns all trainable parameters
//! - `parameters_mut()` - Returns mutable references to parameters
//! - `to_device()` - Moves the module to a device
//! - `device()` - Returns the module's device
//! - `train()` / `training()` - Training mode control
//!
//! ### Field Attributes
//!
//! - `#[param]` - Marks a field as a trainable parameter (Tensor)
//! - `#[module]` - Marks a field as a nested module (recursive traversal)
//! - `#[constant]` - Marks a field as a non-trainable constant
//! - `#[device_field]` - Marks the field that stores the Device
//! - `#[training_field]` - Marks the field that stores training mode
//!
//! ### Example
//!
//! ```ignore
//! use rtx_macros::Module;
//!
//! #[derive(Module, Debug)]
//! pub struct MyLayer {
//! #[param]
//! weight: Tensor,
//!
//! #[param]
//! bias: Option<Tensor>,
//!
//! #[module]
//! sublayer: Linear,
//!
//! #[constant]
//! dropout_rate: f32,
//!
//! #[device_field]
//! device: Device,
//!
//! #[training_field]
//! training: bool,
//! }
//! ```
//!
//! ## Config Derive
//!
//! The `Config` derive macro generates:
//! - `new()` constructor with required fields
//! - `with_*()` builder methods for optional fields
//! - `init()` method to create the module
//!
//! ### Field Attributes
//!
//! - `#[config(default = "value")]` - Sets a default value for the field
//!
//! ### Example
//!
//! ```ignore
//! use rtx_macros::Config;
//!
//! #[derive(Config)]
//! pub struct LinearConfig {
//! pub in_features: usize,
//! pub out_features: usize,
//!
//! #[config(default = "true")]
//! pub bias: bool,
//!
//! #[config(default = "0.0")]
//! pub dropout: f64,
//! }
//! ```
use proc_macro::TokenStream;
mod config;
mod module;
mod shared;
/// Derive macro for implementing the `Module` trait.
///
/// This macro automatically generates the boilerplate code needed for neural network modules,
/// including parameter management, device handling, and training mode control.
///
/// See the [crate-level documentation](crate) for detailed usage examples.
#[proc_macro_derive(
Module,
attributes(param, module, constant, device_field, training_field)
)]
pub fn module_derive(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
module::derive_impl(&input)
.unwrap_or_else(|err| err.to_compile_error())
.into()
}
/// Derive macro for creating configuration structs with builder pattern.
///
/// This macro generates constructors and builder methods for configuration structs,
/// making it easy to create module configurations with sensible defaults.
///
/// See the [crate-level documentation](crate) for detailed usage examples.
#[proc_macro_derive(Config, attributes(config))]
pub fn config_derive(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
config::derive_impl(&input)
.unwrap_or_else(|err| err.to_compile_error())
.into()
}