Skip to content

Commit a408871

Browse files
Add the documentation for package include API, Examples and guides.
1 parent 01198d5 commit a408871

20 files changed

+1395
-0
lines changed

CHANGELOG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Changelog
2+
3+
All notable changes to DiffusionLM will be documented in this file.
4+
5+
## [0.1.6] - 2025-04-05
6+
7+
### Added
8+
- Initial release of DiffusionLM
9+
- Basic transformer-based diffusion model
10+
- Training pipeline with error handling
11+
- Dataset preparation utilities
12+
- Documentation and examples
13+
- Multi-GPU training support
14+
- Mixed precision training
15+
- Custom generation strategies
16+
17+
### Fixed
18+
- Memory optimization for large models
19+
- Import path resolution
20+
- Package structure and dependencies
21+
22+
## [released]
23+
24+
### Added
25+
- Streaming token generation
26+
- Advanced logging configuration
27+
- Performance optimization guides
28+
- Comprehensive documentation
29+
30+
### Changed
31+
- Improved error handling
32+
- Updated package structure
33+
- Enhanced docstrings
34+
35+
### Fixed
36+
- Package import issues
37+
- Memory leaks in training
38+
- Documentation build process

docs/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SPHINXOPTS ?=
2+
SPHINXBUILD ?= sphinx-build
3+
SOURCEDIR = .
4+
BUILDDIR = _build
5+
6+
help:
7+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
8+
9+
.PHONY: help Makefile clean
10+
11+
clean:
12+
rm -rf $(BUILDDIR)/*
13+
14+
%: Makefile
15+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/api/model.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Model API Reference
2+
3+
## DiffusionConfig
4+
5+
Configuration class for the DiffusionLM model.
6+
7+
### Parameters
8+
9+
- `vocab_size` (int, default=50257): Size of the vocabulary
10+
- `hidden_size` (int, default=768): Dimensionality of the hidden layers
11+
- `num_hidden_layers` (int, default=12): Number of transformer layers
12+
- `num_attention_heads` (int, default=12): Number of attention heads per layer
13+
- `intermediate_size` (int, default=3072): Dimensionality of feed-forward layers
14+
- `hidden_dropout_prob` (float, default=0.1): Dropout probability for hidden layers
15+
- `attention_probs_dropout_prob` (float, default=0.1): Dropout probability for attention
16+
- `max_position_embeddings` (int, default=1024): Maximum sequence length
17+
- `num_timesteps` (int, default=100): Number of diffusion timesteps
18+
- `time_embed_dim` (int, default=128): Dimensionality of time embeddings
19+
20+
## DiffusionLLM
21+
22+
Main model class implementing the transformer-based diffusion language model.
23+
24+
### Methods
25+
26+
#### forward()
27+
28+
```python
29+
def forward(
30+
input_ids=None,
31+
attention_mask=None,
32+
timesteps=None,
33+
labels=None,
34+
return_dict=True
35+
)
36+
```
37+
38+
Perform a forward pass through the model.
39+
40+
#### generate()
41+
42+
```python
43+
def generate(
44+
prompt=None,
45+
max_length=100,
46+
num_inference_steps=50,
47+
temperature=1.0,
48+
strategy='random',
49+
top_p=0.9,
50+
top_k=50,
51+
num_beams=5,
52+
return_scores=False,
53+
use_streaming=False,
54+
callback_fn=None
55+
)
56+
```
57+
58+
Generate text using the reverse diffusion process.
59+
60+
## MultiHeadAttention
61+
62+
Multi-head self-attention mechanism implementation.
63+
64+
### Methods
65+
66+
#### forward()
67+
68+
```python
69+
def forward(
70+
hidden_states,
71+
attention_mask=None,
72+
head_mask=None,
73+
output_attentions=False
74+
)
75+
```
76+
77+
Perform multi-head attention computation.
78+
79+
## TimeEmbedding
80+
81+
Embedding layer for diffusion timesteps.
82+
83+
### Methods
84+
85+
#### forward()
86+
87+
```python
88+
def forward(timesteps)
89+
```
90+
91+
Generate time embeddings for the given timesteps.

docs/api/trainer.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Trainer API Reference
2+
3+
## Trainer Function
4+
5+
Main training function for DiffusionLM models.
6+
7+
```python
8+
def trainer(
9+
model: DiffusionLLM,
10+
train_dataset,
11+
val_dataset = None,
12+
batch_size: int = 8,
13+
num_epochs: int = 5,
14+
learning_rate: float = 5e-5,
15+
warmup_steps: int = 1000,
16+
max_grad_norm: float = 1.0,
17+
num_timesteps: int = 100,
18+
save_path: Optional[str] = None,
19+
device: torch.device = None
20+
) -> DiffusionLLM
21+
```
22+
23+
### Parameters
24+
25+
- `model`: The DiffusionLLM model to train
26+
- `train_dataset`: Training dataset
27+
- `val_dataset`: Validation dataset (optional)
28+
- `batch_size`: Batch size for training
29+
- `num_epochs`: Number of training epochs
30+
- `learning_rate`: Learning rate
31+
- `warmup_steps`: Number of warmup steps
32+
- `max_grad_norm`: Maximum gradient norm
33+
- `num_timesteps`: Number of diffusion timesteps
34+
- `save_path`: Path to save checkpoints
35+
- `device`: Device to train on
36+
37+
### Returns
38+
39+
- Trained DiffusionLLM model
40+
41+
## Evaluate Function
42+
43+
```python
44+
def evaluate(
45+
model: DiffusionLLM,
46+
dataloader: DataLoader,
47+
device: torch.device,
48+
num_timesteps: int = 100,
49+
num_eval_steps: int = None
50+
) -> float
51+
```
52+
53+
### Parameters
54+
55+
- `model`: Model to evaluate
56+
- `dataloader`: DataLoader for evaluation data
57+
- `device`: Device to evaluate on
58+
- `num_timesteps`: Number of diffusion timesteps
59+
- `num_eval_steps`: Number of evaluation steps
60+
61+
### Returns
62+
63+
- Average loss on the evaluation set

docs/api/utils.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Utils API Reference
2+
3+
## Dataset Preparation
4+
5+
### prepare_dataset()
6+
7+
```python
8+
def prepare_dataset(
9+
dataset_name: str = "wikitext/wikitext-2-raw-v1",
10+
tokenizer_name: str = "gpt2",
11+
max_length: int = 1024,
12+
cache_dir: Optional[str] = None,
13+
num_proc: int = 4
14+
) -> Tuple[PYTORCH_Dataset, Optional[PYTORCH_Dataset], AutoTokenizer]
15+
```
16+
17+
Prepares datasets for training DiffusionLM models.
18+
19+
### PYTORCH_Dataset
20+
21+
```python
22+
class PYTORCH_Dataset(Dataset):
23+
def __init__(
24+
self,
25+
dataset: Any,
26+
mask_token_id: int,
27+
pad_token_id: int,
28+
)
29+
```
30+
31+
Custom dataset class for DiffusionLM training.
32+
33+
## Error Handling
34+
35+
### DiffusionLMError
36+
37+
```python
38+
class DiffusionLMError(Exception):
39+
"""Base exception class for DiffusionLM package"""
40+
pass
41+
```
42+
43+
### handle_errors()
44+
45+
```python
46+
def handle_errors(
47+
error_class: Type[Exception] = DiffusionLMError,
48+
reraise: bool = True,
49+
logger: logging.Logger = logger
50+
) -> Callable
51+
```
52+
53+
Decorator for handling errors in functions.
54+
55+
## Logging
56+
57+
### setup_logging()
58+
59+
```python
60+
def setup_logging(
61+
log_file: str = None,
62+
level: int = logging.INFO,
63+
format: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
64+
) -> None
65+
```
66+
67+
Sets up logging configuration for the package.

docs/build_docs.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Building the Documentation
2+
3+
## Requirements
4+
5+
First, install the required packages:
6+
7+
```bash
8+
pip install -r docs/requirements.txt
9+
```
10+
11+
## Building HTML Documentation
12+
13+
### On Linux/Mac:
14+
```bash
15+
cd docs
16+
make html
17+
```
18+
19+
### On Windows:
20+
```bash
21+
cd docs
22+
./make.bat html
23+
```
24+
25+
The built documentation will be available in `docs/_build/html/`.
26+
27+
## Development Server
28+
29+
For live preview while writing documentation:
30+
31+
```bash
32+
pip install sphinx-autobuild
33+
sphinx-autobuild docs docs/_build/html
34+
```
35+
36+
Then visit `http://localhost:8000` in your browser.
37+
38+
## Build Options
39+
40+
- `make html` - Build HTML documentation
41+
- `make clean` - Clean build directory
42+
- `make help` - Show all available build options
43+
44+
## Troubleshooting
45+
46+
If you encounter any issues:
47+
48+
1. Ensure all requirements are installed:
49+
```bash
50+
pip install -r docs/requirements.txt
51+
```
52+
53+
2. Clean the build directory:
54+
```bash
55+
make clean
56+
```
57+
58+
3. Check for syntax errors in rst/md files
59+
```bash
60+
sphinx-build -nW -b html docs/ docs/_build/html
61+
```

docs/conf.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import sys
3+
sys.path.insert(0, os.path.abspath('..'))
4+
5+
project = 'DiffusionLM'
6+
copyright = '2024, Dark Coder'
7+
author = 'Dark Coder'
8+
release = '0.1.0'
9+
10+
extensions = [
11+
'sphinx.ext.autodoc',
12+
'sphinx.ext.napoleon',
13+
'sphinx.ext.viewcode',
14+
'sphinx.ext.githubpages',
15+
'myst_parser',
16+
]
17+
18+
templates_path = ['_templates']
19+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
20+
21+
html_theme = 'sphinx_rtd_theme'
22+
html_static_path = ['_static']
23+
24+
autodoc_member_order = 'bysource'
25+
autodoc_typehints = 'description'
26+
napoleon_google_docstring = True
27+
napoleon_numpy_docstring = True

0 commit comments

Comments
 (0)