Contributing to TorchEBM¶
Thank you for your interest in contributing to TorchEBM! This document provides guidelines and instructions for contributing to the project.
Code of Conduct¶
We expect all contributors to follow our Code of Conduct. Please be respectful and inclusive in your interactions with others.
Ways to Contribute¶
There are many ways to contribute to TorchEBM:
- Report bugs: Report bugs or issues by opening an issue on GitHub
- Suggest features: Suggest new features or improvements
- Improve documentation: Fix typos, clarify explanations, or add examples
- Write code: Implement new features, fix bugs, or improve performance
- Review pull requests: Help review code from other contributors
Development Workflow¶
Setting Up Your Development Environment¶
- Fork the repository on GitHub
- Clone your fork locally:
- Set the original repository as an upstream remote:
- Install development dependencies:
Making Changes¶
- Create a new branch for your changes:
- Make your changes
- Run tests to make sure your changes don't break existing functionality:
- Add and commit your changes using our commit message conventions
Code Style¶
We follow PEP 8 for Python code style with some modifications:
- Line length limit: 88 characters
- Use double quotes for strings
- Follow naming conventions:
- Classes:
CamelCase
- Functions and variables:
snake_case
- Constants:
UPPER_CASE
We use black
for code formatting and isort
for sorting imports.
Commit Message Conventions¶
We follow a specific format for commit messages to make the project history clear and generate meaningful changelogs. Each commit message should have a specific format:
The first line should be max 50-60 chars. Any further details should be in the next lines separated by an empty line.
- ✨ feat: Introduces a new feature
- 🐛 fix: Patches a bug in the codebase
- 📖 docs: Changes related to documentation
- 💎 style: Changes that do not affect the meaning of the code (formatting)
- 📦 refactor: Code changes that neither fix a bug nor add a feature
- 🚀 perf: Improvements to performance
- 🚨 test: Adding or correcting tests
- 👷 build: Changes affecting the build system or external dependencies
- 💻 ci: Changes to Continuous Integration configuration
- 🎫 chore: Miscellaneous changes that don't modify source or test files
- 🔙 revert: Reverts a previous commit
Example:
For version bumping, include one of these tags in your commit message:
- Use #major
for breaking changes
- Use #minor
for new features
- Default is patch level for bug fixes
For releasing to PyPI, include #release
in your commit message.
For more detailed information about our commit message conventions, please see our Commit Message Conventions guide.
Submitting Changes¶
- Push your changes to your fork:
- Create a pull request on GitHub
- In your pull request description, explain the changes and link to any related issues
- Wait for a review and address any feedback
Pull Request Guidelines¶
- Keep pull requests focused on a single task
- Add tests for new features or bug fixes
- Update documentation as needed
- Ensure all tests pass
- Follow the code style guidelines
Issue Guidelines¶
When opening an issue, please provide:
- A clear and descriptive title
- A detailed description of the issue
- Steps to reproduce (for bugs)
- Expected vs. actual behavior
- Version information (TorchEBM, PyTorch, Python, CUDA)
- Any relevant code snippets or error messages
Implementing New Features¶
Samplers¶
When implementing a new sampler:
- Create a new file in
torchebm/samplers/
- Extend the
BaseSampler
class - Implement the required methods:
__init__
: Initialize the sampler with appropriate parametersstep
: Implement the sampling stepsample_chain
: Implement a full sampling chain (or use the default implementation)- Add tests in
tests/samplers/
- Update documentation
Example:
from torchebm.core import BaseSampler
import torch
class MySampler(BaseSampler):
def __init__(self, energy_function, param1, param2, device="cpu"):
super().__init__(energy_function, device)
self.param1 = param1
self.param2 = param2
def sample_chain(self, x, step_idx=None):
# Implement your sampling algorithm here
# x shape: [n_samples, dim]
# Your sampler logic
x_new = ...
# Return updated samples and any diagnostics
return x_new, {"diagnostic1": value1, "diagnostic2": value2}
Energy Functions¶
When implementing a new energy function:
- Create a new class in
torchebm/core/energy_function.py
or a new file intorchebm/core/
- Extend the
EnergyFunction
class - Implement the required methods:
__init__
: Initialize the energy function with appropriate parametersforward
: Compute the energy value for a given input- Add tests in
tests/core/
- Update documentation
Example:
from torchebm.core import EnergyFunction
import torch
class MyEnergyFunction(EnergyFunction):
def __init__(self, param1, param2):
super().__init__()
self.param1 = param1
self.param2 = param2
def forward(self, x):
# Implement your energy function here
# x shape: [batch_size, dimension]
# Return shape: [batch_size]
return torch.sum(self.param1 * x**2 + self.param2 * torch.sin(x), dim=-1)
Loss Functions¶
When implementing a new loss function:
- Create a new class in
torchebm/losses/
- Implement the required methods:
__init__
: Initialize the loss function with appropriate parametersforward
: Compute the loss value- Add tests in
tests/losses/
- Update documentation
Example:
import torch
import torch.nn as nn
class MyLossFunction(nn.Module):
def __init__(self, param1, param2):
super().__init__()
self.param1 = param1
self.param2 = param2
def forward(self, model, data_samples):
# Implement your loss function here
# Return a scalar loss value
return loss
Documentation Guidelines¶
- Use clear, concise language
- Include examples for complex functionality
- Document parameters, return values, and exceptions
- Add docstrings to classes and functions
- Update the roadmap when implementing new features
Getting Help¶
If you need help or have questions:
- Check existing documentation
- Search for similar issues on GitHub
- Ask for help in your pull request or issue
Thank You!¶
Thank you for contributing to TorchEBM! Your help is greatly appreciated and makes the library better for everyone.