mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+295
-160
@@ -1,174 +1,309 @@
|
||||
---
|
||||
description: This rule set enforces best practices for developing with the transformers library, covering code organization, performance, security, and testing to promote maintainable and efficient NLP applications.
|
||||
globs: *.py
|
||||
description: Definitive guidelines for writing high-quality, maintainable, and performant code with 🤗 Transformers, ensuring consistency and adherence to 2025 best practices.
|
||||
globs: **/*.py
|
||||
---
|
||||
- **Environment Management:**
|
||||
- Use Conda or UV to create isolated environments for consistent dependencies across projects.
|
||||
- Example (Conda): `conda create -n myenv python=3.12`
|
||||
- Example (UV): `uv venv`
|
||||
- Use `environment.yml` or `requirements.txt` to manage dependencies.
|
||||
- Always use Python 3.12.
|
||||
# transformers Best Practices
|
||||
|
||||
- **Code Organization and Structure:**
|
||||
- **Directory Structure:** Adopt a modular structure for maintainability.
|
||||
- `src/`: Source code.
|
||||
- `models/`: Transformer model definitions.
|
||||
- `data/`: Data loading and preprocessing.
|
||||
- `training/`: Training scripts and utilities.
|
||||
- `utils/`: Utility functions.
|
||||
- `tests/`: Unit and integration tests.
|
||||
- `notebooks/`: Experimentation and exploration notebooks.
|
||||
- `scripts/`: Deployment and utility scripts.
|
||||
- `config/`: Configuration files.
|
||||
- **File Naming Conventions:**
|
||||
- Use descriptive names: `model.py`, `data_loader.py`, `trainer.py`.
|
||||
- Follow PEP 8 guidelines.
|
||||
- **Module Organization:**
|
||||
- Group related functions and classes into modules.
|
||||
- Use clear and concise module names.
|
||||
- **Component Architecture:**
|
||||
- Implement modular components with well-defined interfaces.
|
||||
- Use classes for stateful components, such as data loaders or model wrappers.
|
||||
- **Code Splitting:**
|
||||
- Split large files into smaller, manageable modules.
|
||||
- Use lazy loading for large models or datasets to improve startup time.
|
||||
This guide outlines the definitive best practices for developing with 🤗 Transformers, focusing on reliability, readability, and production readiness. Adhere to these rules to ensure your code integrates seamlessly with the ecosystem and passes all CI checks.
|
||||
|
||||
- **Model Training and Evaluation:**
|
||||
- Implement a structured approach for training and evaluating models.
|
||||
- Use Hugging Face Transformers for easy access to pre-trained models.
|
||||
- Log experiments using MLflow or TensorBoard for tracking model performance and versioning.
|
||||
- Create clear separation between training, validation, and test datasets to prevent data leakage.
|
||||
- Use consistent evaluation metrics.
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
- **Data Handling:**
|
||||
- Preprocess data effectively using libraries like Hugging Face's tokenizers.
|
||||
- Ensure proper tokenization and encoding.
|
||||
- Manage large datasets efficiently with data loaders.
|
||||
- Implement data validation to ensure data quality.
|
||||
Always follow the "Modular Transformers" framework for new models. This reduces boilerplate and promotes reusability.
|
||||
|
||||
- **Code Structure:**
|
||||
- Organize code into reusable modules and functions.
|
||||
- Follow a consistent naming convention and documentation style (PEP 8) for enhanced readability and collaboration.
|
||||
- Always use classes instead of functions where appropriate to encapsulate state and behavior.
|
||||
- Add docstrings to all functions, classes, and modules.
|
||||
### 1.1. Modular Model Definitions
|
||||
Place new model definitions in `src/transformers/models/<model_name>/` using a modular file (e.g., `modular_<model_name>.py`). Inherit from existing base classes and import components from other models to minimize code duplication.
|
||||
|
||||
- **Common Patterns and Anti-patterns:**
|
||||
- **Design Patterns:**
|
||||
- Use the Factory pattern for creating different model architectures.
|
||||
- Use the Strategy pattern for different training strategies.
|
||||
- Use the Decorator pattern for adding functionality to models.
|
||||
- **Recommended Approaches:**
|
||||
- Use pipelines for common tasks like text classification or question answering.
|
||||
- Leverage pre-trained models for transfer learning.
|
||||
- **Anti-patterns:**
|
||||
- Avoid hardcoding configurations.
|
||||
- Avoid global variables.
|
||||
- Avoid deeply nested code.
|
||||
- **State Management:**
|
||||
- Encapsulate state within classes.
|
||||
- Use configuration files to manage hyperparameters.
|
||||
- **Error Handling:**
|
||||
- Implement try-except blocks for error handling.
|
||||
- Log errors and warnings using the `logging` module.
|
||||
- Raise informative exceptions.
|
||||
❌ BAD: Re-implementing common layers or full models from scratch.
|
||||
```python
|
||||
# src/transformers/models/my_model/modeling.py
|
||||
class MyModelAttention(nn.Module):
|
||||
# ... duplicate attention logic ...
|
||||
|
||||
- **Performance Considerations:**
|
||||
- Use appropriate batch sizes to optimize GPU utilization.
|
||||
- Utilize mixed-precision training (FP16) for faster training and reduced memory consumption.
|
||||
- Cache intermediate results to avoid redundant computations.
|
||||
- Profile code using tools like `cProfile` to identify bottlenecks.
|
||||
- Use optimized libraries like `torch.compile` when available.
|
||||
- **Memory Management:**
|
||||
- Release unused memory using `torch.cuda.empty_cache()`.
|
||||
- Use data loaders with `num_workers` to parallelize data loading.
|
||||
- **Bundle Size Optimization:**
|
||||
- Remove unused dependencies.
|
||||
- Use code minification and compression.
|
||||
- **Lazy Loading:**
|
||||
- Load models and datasets only when needed.
|
||||
- Use `torch.jit.script` to compile model for inference.
|
||||
class MyModel(PreTrainedModel):
|
||||
# ... full model re-implementation ...
|
||||
```
|
||||
|
||||
- **Security Best Practices:**
|
||||
- **Common Vulnerabilities:**
|
||||
- Input injection attacks.
|
||||
- Model poisoning attacks.
|
||||
- **Input Validation:**
|
||||
- Validate input data to prevent injection attacks.
|
||||
- Sanitize user input before feeding it to the model.
|
||||
- **Authentication and Authorization:**
|
||||
- Implement authentication and authorization for API endpoints.
|
||||
- Use secure protocols like HTTPS for communication.
|
||||
- **Data Protection:**
|
||||
- Encrypt sensitive data at rest and in transit.
|
||||
- Use appropriate access controls to protect data.
|
||||
- **Secure API Communication:**
|
||||
- Use API keys or tokens for authentication.
|
||||
- Implement rate limiting to prevent abuse.
|
||||
✅ GOOD: Inheriting and reusing components.
|
||||
```python
|
||||
# src/transformers/models/my_model/modular_my_model.py
|
||||
from ..llama.modeling_llama import LlamaAttention, LlamaModel
|
||||
from ..llama.configuration_llama import LlamaConfig
|
||||
|
||||
- **Testing Approaches:**
|
||||
- **Unit Testing:**
|
||||
- Test individual components in isolation.
|
||||
- Use mocking to isolate dependencies.
|
||||
- Cover all code paths with unit tests.
|
||||
- **Integration Testing:**
|
||||
- Test interactions between different components.
|
||||
- Verify that data flows correctly through the system.
|
||||
- **End-to-End Testing:**
|
||||
- Test the entire application from end to end.
|
||||
- Simulate user interactions to verify functionality.
|
||||
- **Test Organization:**
|
||||
- Organize tests into separate directories.
|
||||
- Use descriptive test names.
|
||||
- **Mocking and Stubbing:**
|
||||
- Use mocking frameworks like `unittest.mock` to isolate dependencies.
|
||||
- Stub out external API calls to prevent network access.
|
||||
class MyModelConfig(LlamaConfig):
|
||||
model_type = "my_model"
|
||||
# Add/override specific config parameters
|
||||
|
||||
- **Common Pitfalls and Gotchas:**
|
||||
- **Frequent Mistakes:**
|
||||
- Incorrectly configuring tokenizers.
|
||||
- Using outdated versions of the library.
|
||||
- Not handling edge cases in data preprocessing.
|
||||
- **Edge Cases:**
|
||||
- Handling long sequences.
|
||||
- Dealing with out-of-vocabulary words.
|
||||
- **Version-Specific Issues:**
|
||||
- Check the release notes for breaking changes.
|
||||
- Test code with different versions of the library.
|
||||
- **Compatibility Concerns:**
|
||||
- Ensure compatibility with different hardware and software configurations.
|
||||
- Check compatibility with other libraries.
|
||||
- **Debugging Strategies:**
|
||||
- Use debuggers like `pdb` to step through code.
|
||||
- Use logging to track program execution.
|
||||
class MyModelAttention(LlamaAttention):
|
||||
# Only override specific methods if needed, otherwise inherit directly
|
||||
def forward(self, hidden_states, attention_mask=None, **kwargs):
|
||||
# Custom logic if different from LlamaAttention
|
||||
return super().forward(hidden_states, attention_mask, **kwargs)
|
||||
|
||||
- **Tooling and Environment:**
|
||||
- **Recommended Tools:**
|
||||
- VS Code with Python extension.
|
||||
- PyCharm.
|
||||
- Jupyter Notebook.
|
||||
- Debuggers: pdb, ipdb.
|
||||
- **Build Configuration:**
|
||||
- Use `setup.py` or `pyproject.toml` for build configuration.
|
||||
- Specify dependencies in `requirements.txt` or `environment.yml`.
|
||||
- **Linting and Formatting:**
|
||||
- Use linters like `flake8` and `pylint` to enforce code style.
|
||||
- Use formatters like `black` and `autopep8` to automatically format code.
|
||||
- **Deployment:**
|
||||
- Use Docker to containerize the application.
|
||||
- Deploy to cloud platforms like AWS, Azure, or GCP.
|
||||
- **CI/CD Integration:**
|
||||
- Use CI/CD pipelines to automate testing and deployment.
|
||||
- Integrate with version control systems like Git.
|
||||
class MyModel(LlamaModel):
|
||||
# Override the attention layer if MyModelAttention is different
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
self.attention = MyModelAttention(config) # Use custom attention
|
||||
```
|
||||
After creating the modular file, generate the single-file structure:
|
||||
```bash
|
||||
python utils/modular_model_converter.py my_model
|
||||
```
|
||||
|
||||
- **Additional Recommendations:**
|
||||
- Always document your code thoroughly.
|
||||
- Write clear and concise commit messages.
|
||||
- Use version control (Git) to track changes.
|
||||
- Participate in the transformers community to learn from others.
|
||||
- Regularly update the library to benefit from bug fixes and new features.
|
||||
## 2. Python Style and Formatting
|
||||
|
||||
- **References:**
|
||||
- [Hugging Face Transformers Documentation](https://huggingface.co/transformers/)
|
||||
- [PyTorch Documentation](https://pytorch.org/docs/stable/index.html)
|
||||
- [MLflow Documentation](https://www.mlflow.org/docs/latest/index.html)
|
||||
- [BERT Fine-Tuning Tutorial with PyTorch](http://www.mccormickml.com)
|
||||
Strictly adhere to PEP 8 and the Google Python Style Guide. Automated tools enforce this.
|
||||
|
||||
### 2.1. Automated Formatting
|
||||
Always run `make style` and `make quality` locally before committing. For changes in your current branch, use `make fixup`.
|
||||
|
||||
❌ BAD: Manual formatting, inconsistent spacing, un-sorted imports.
|
||||
```python
|
||||
import os, sys
|
||||
from transformers import AutoModel
|
||||
def my_func(arg1,arg2):
|
||||
if arg1 == True:
|
||||
return arg2
|
||||
```
|
||||
|
||||
✅ GOOD: Consistent, auto-formatted code.
|
||||
```python
|
||||
import os
|
||||
import sys
|
||||
|
||||
from transformers import AutoModel
|
||||
|
||||
|
||||
def my_func(arg1: bool, arg2: str) -> str:
|
||||
if arg1: # Use direct boolean evaluation
|
||||
return arg2
|
||||
return ""
|
||||
```
|
||||
|
||||
### 2.2. Line Length
|
||||
Limit all lines to a maximum of 79 characters. Docstrings and comments should be 72 characters. Use Python's implicit line continuation.
|
||||
|
||||
❌ BAD: Long, unreadable lines.
|
||||
```python
|
||||
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=10, output_attentions=True, output_hidden_states=True, return_dict=True)
|
||||
```
|
||||
|
||||
✅ GOOD: Wrapped lines using parentheses.
|
||||
```python
|
||||
model = AutoModelForSequenceClassification.from_pretrained(
|
||||
"bert-base-uncased",
|
||||
num_labels=10,
|
||||
output_attentions=True,
|
||||
output_hidden_states=True,
|
||||
return_dict=True,
|
||||
)
|
||||
```
|
||||
|
||||
## 3. Type Hints and Docstrings
|
||||
|
||||
Type hints are mandatory for all public methods. Docstrings must follow the Google style.
|
||||
|
||||
### 3.1. Type Annotations
|
||||
Use `from __future__ import annotations` for forward references. Annotate all function arguments, return values, and class attributes.
|
||||
|
||||
❌ BAD: Missing type hints, unclear intent.
|
||||
```python
|
||||
def process_data(data, config):
|
||||
"""Processes input data."""
|
||||
# ...
|
||||
return processed_data
|
||||
```
|
||||
|
||||
✅ GOOD: Clear, explicit type hints.
|
||||
```python
|
||||
from __future__ import annotations
|
||||
from typing import Any, Dict
|
||||
|
||||
def process_data(data: list[str], config: Dict[str, Any]) -> list[int]:
|
||||
"""Processes input data according to the provided configuration.
|
||||
|
||||
Args:
|
||||
data: A list of string inputs to be processed.
|
||||
config: A dictionary containing processing parameters.
|
||||
|
||||
Returns:
|
||||
A list of integers representing the processed data.
|
||||
"""
|
||||
# ...
|
||||
return [len(d) for d in data]
|
||||
```
|
||||
|
||||
### 3.2. Docstring Conventions
|
||||
Follow the Google Python Style Guide for docstrings. Include a concise summary, Args, Returns, and Raises sections where applicable.
|
||||
|
||||
## 4. Performance Considerations
|
||||
|
||||
Prioritize efficiency and leverage built-in optimizations.
|
||||
|
||||
### 4.1. Optimized Training with `Trainer`
|
||||
For training, always use `transformers.Trainer`. It provides out-of-the-box support for:
|
||||
* Mixed precision training (`fp16=True` or `bf16=True`).
|
||||
* `torch.compile` for graph compilation (`torch_compile=True`).
|
||||
* FlashAttention (if available for your model/hardware).
|
||||
|
||||
❌ BAD: Writing custom training loops that re-implement `Trainer` features.
|
||||
```python
|
||||
# Manual training loop
|
||||
optimizer = AdamW(model.parameters(), lr=1e-5)
|
||||
for epoch in range(num_epochs):
|
||||
# ... manual gradient accumulation, mixed precision, etc. ...
|
||||
```
|
||||
|
||||
✅ GOOD: Leveraging `Trainer` for robust and optimized training.
|
||||
```python
|
||||
from transformers import Trainer, TrainingArguments
|
||||
|
||||
training_args = TrainingArguments(
|
||||
output_dir="./results",
|
||||
num_train_epochs=3,
|
||||
per_device_train_batch_size=8,
|
||||
fp16=True, # Enable mixed precision
|
||||
torch_compile=True, # Enable torch.compile
|
||||
# ... other args ...
|
||||
)
|
||||
|
||||
trainer = Trainer(
|
||||
model=model,
|
||||
args=training_args,
|
||||
train_dataset=train_dataset,
|
||||
eval_dataset=eval_dataset,
|
||||
# ...
|
||||
)
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
### 4.2. Efficient Inference with `Pipeline`
|
||||
For common inference tasks, use `transformers.pipeline`. It handles tokenization, model inference, and post-processing efficiently.
|
||||
|
||||
❌ BAD: Manually managing tokenizers, models, and post-processing.
|
||||
```python
|
||||
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
||||
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
|
||||
inputs = tokenizer("Hello, world!", return_tensors="pt")
|
||||
outputs = model(**inputs)
|
||||
predictions = torch.argmax(outputs.logits, dim=-1)
|
||||
# ... manual label mapping ...
|
||||
```
|
||||
|
||||
✅ GOOD: Using `pipeline` for streamlined inference.
|
||||
```python
|
||||
from transformers import pipeline
|
||||
|
||||
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
||||
result = classifier("I love this movie!")
|
||||
# result: [{'label': 'POSITIVE', 'score': 0.99987}]
|
||||
```
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
Thorough testing is non-negotiable. Every new feature or model requires unit tests.
|
||||
|
||||
### 5.1. Unit Tests
|
||||
Write unit tests for every new model, utility, and significant code change. Verify:
|
||||
* **Forward-pass shapes**: Ensure model outputs have expected dimensions.
|
||||
* **Serialization**: Test saving and loading with `safetensors`.
|
||||
* **Edge cases**: Test with various input sizes, empty inputs, etc.
|
||||
|
||||
❌ BAD: No tests, or only manual verification.
|
||||
```python
|
||||
# No test file for new_model.py
|
||||
# Developer manually runs `new_model.py` to check if it works.
|
||||
```
|
||||
|
||||
✅ GOOD: Dedicated test files (`test_new_model.py`) verifying core functionality.
|
||||
```python
|
||||
# tests/test_new_model.py
|
||||
import unittest
|
||||
import torch
|
||||
from transformers import AutoModel, AutoTokenizer
|
||||
from transformers.models.new_model import NewModel, NewModelConfig
|
||||
|
||||
class NewModelTester(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.config = NewModelConfig(vocab_size=100, hidden_size=16)
|
||||
self.model = NewModel(self.config)
|
||||
self.tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") # or a dummy tokenizer
|
||||
|
||||
def test_forward_pass(self):
|
||||
input_ids = torch.randint(0, self.config.vocab_size, (2, 10))
|
||||
outputs = self.model(input_ids)
|
||||
self.assertIn("last_hidden_state", outputs)
|
||||
self.assertEqual(outputs.last_hidden_state.shape, (2, 10, self.config.hidden_size))
|
||||
|
||||
def test_safetensors_serialization(self):
|
||||
# Save and load with safetensors
|
||||
tmpdir = self.create_temporary_dir()
|
||||
self.model.save_pretrained(tmpdir, safe_serialization=True)
|
||||
loaded_model = NewModel.from_pretrained(tmpdir)
|
||||
self.assertTrue(torch.equal(self.model.state_dict()["embeddings.word_embeddings.weight"],
|
||||
loaded_model.state_dict()["embeddings.word_embeddings.weight"]))
|
||||
```
|
||||
|
||||
### 5.2. Local Test Execution
|
||||
Use `utils/tests_fetcher.py` to identify relevant tests and run them with `pytest`.
|
||||
```bash
|
||||
python utils/tests_fetcher.py
|
||||
python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt)
|
||||
```
|
||||
|
||||
## 6. Packaging and Deployment
|
||||
|
||||
Leverage `safetensors` and MLflow for robust model packaging.
|
||||
|
||||
### 6.1. `safetensors` for Model Serialization
|
||||
Always use `safetensors` for saving and loading model weights. It's faster and more secure than traditional PyTorch checkpoints.
|
||||
|
||||
❌ BAD: Using `torch.save` or `model.save_pretrained(safe_serialization=False)`.
|
||||
```python
|
||||
torch.save(model.state_dict(), "model.pt")
|
||||
```
|
||||
|
||||
✅ GOOD: Using `model.save_pretrained` with `safe_serialization=True`.
|
||||
```python
|
||||
model.save_pretrained("./my_model_dir", safe_serialization=True)
|
||||
# To load:
|
||||
loaded_model = AutoModel.from_pretrained("./my_model_dir")
|
||||
```
|
||||
|
||||
### 6.2. MLflow Integration
|
||||
For production deployments, integrate with `mlflow.transformers` to log models, configurations, and prompt templates. This ensures reproducible and traceable deployments.
|
||||
|
||||
```python
|
||||
import mlflow
|
||||
from transformers import pipeline
|
||||
|
||||
# Log a pipeline
|
||||
with mlflow.start_run():
|
||||
text_generator = pipeline("text-generation", model="gpt2")
|
||||
mlflow.transformers.log_model(
|
||||
transformers_model=text_generator,
|
||||
artifact_path="text_generator_pipeline",
|
||||
model_config={"max_new_tokens": 50, "do_sample": True},
|
||||
input_example="Hello, my name is",
|
||||
)
|
||||
|
||||
# Load for inference
|
||||
logged_model = mlflow.pyfunc.load_model("runs:/<run_id>/text_generator_pipeline")
|
||||
result = logged_model.predict(["Hello, my name is"])
|
||||
```
|
||||
|
||||
## 7. Virtual Environments
|
||||
|
||||
Always work within a dedicated virtual environment.
|
||||
|
||||
❌ BAD: Installing dependencies globally.
|
||||
```bash
|
||||
pip install transformers[dev] # Installs into system Python
|
||||
```
|
||||
|
||||
✅ GOOD: Using `venv` or `conda` for isolated environments.
|
||||
```bash
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -e ".[dev]" # Editable install with dev dependencies
|
||||
```
|
||||
Reference in New Issue
Block a user