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:
+198
-163
@@ -1,175 +1,210 @@
|
||||
---
|
||||
description: This rule file provides comprehensive best practices for developing with tinygrad, covering code organization, performance, testing, and security. It aims to improve code quality, maintainability, and prevent common pitfalls when working with tinygrad.
|
||||
globs: *.py
|
||||
description: This guide provides opinionated, actionable best practices for developing with tinygrad, focusing on lazy execution, performance, and code structure.
|
||||
globs: **/*
|
||||
---
|
||||
- **Introduction**
|
||||
This document outlines best practices for developing and maintaining code within the tinygrad library and projects that use tinygrad. Adhering to these guidelines will ensure code readability, maintainability, and optimal performance. It draws from established software engineering principles, AI/ML coding standards, and the specific nuances of the tinygrad library.
|
||||
# tinygrad Best Practices
|
||||
|
||||
- **General Principles**
|
||||
- **Readability is Paramount:** Code should be easy to understand, even for those unfamiliar with the specific problem domain. Use clear, descriptive variable and function names. Comment liberally, explaining the *why* more than the *what*.
|
||||
- **Simplicity First:** Favor simple, straightforward solutions over complex, clever ones. Optimize for readability and maintainability before raw performance. Premature optimization is the root of all evil.
|
||||
- **Test-Driven Development (TDD):** Write tests before writing code. This clarifies requirements and ensures that the code behaves as expected. Tests should be automated and run frequently.
|
||||
- **Version Control:** All code must be managed with Git. Use meaningful commit messages. Branch frequently for new features and bug fixes. Pull requests should be reviewed by at least one other developer.
|
||||
- **Continuous Integration/Continuous Deployment (CI/CD):** Automate the build, test, and deployment process. Use a CI/CD system like GitHub Actions or GitLab CI.
|
||||
- **Documentation:** Document all public APIs. Include examples of how to use the code. Keep documentation up-to-date.
|
||||
- **SOLID Principles:** Attempt to adhere to the SOLID design principles as appropriate for Machine Learning.
|
||||
tinygrad prioritizes minimalism, performance, and a clear computation graph. Adhering to these guidelines ensures your code is efficient, readable, and aligns with the framework's core philosophy.
|
||||
|
||||
- **Code Organization and Structure**
|
||||
- **Directory Structure:** A suggested directory structure for tinygrad projects is:
|
||||
|
||||
project_root/
|
||||
├── tinygrad/
|
||||
│ ├── __init__.py
|
||||
│ ├── tensor.py # Core tensor implementation
|
||||
│ ├── ops.py # Basic operations (add, multiply, etc.)
|
||||
│ ├── device.py # Abstraction for different hardware devices (CPU, GPU)
|
||||
│ ├── nn/ # Neural network modules
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── layers.py
|
||||
│ │ └── optim.py
|
||||
│ ├── mlops.py # Matrix library operations
|
||||
│ ├──codegen/ # Code generation related files
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── linearizer.py
|
||||
│ │ └── opencl.py
|
||||
│ └── ...
|
||||
├── examples/ # Example usage of tinygrad
|
||||
├── tests/ # Unit and integration tests
|
||||
├── README.md
|
||||
├── setup.py
|
||||
└── requirements.txt
|
||||
|
||||
- **File Naming Conventions:**
|
||||
- Python files: Use lowercase with underscores (e.g., `tensor.py`, `nn_layers.py`).
|
||||
- Classes: Use PascalCase (e.g., `Tensor`, `NeuralNetworkLayer`).
|
||||
- Functions and variables: Use lowercase with underscores (e.g., `add`, `learning_rate`).
|
||||
- Constants: Use UPPER_CASE with underscores (e.g., `DEFAULT_DEVICE`, `EPSILON`).
|
||||
- **Module Organization:**
|
||||
- Group related functionality into modules. For example, all neural network layers should be in the `nn` module.
|
||||
- Use `__init__.py` files to define the public API of each module.
|
||||
- Avoid circular dependencies between modules.
|
||||
- **Component Architecture:**
|
||||
- Design components with clear interfaces and responsibilities.
|
||||
- Favor composition over inheritance.
|
||||
- Use dependency injection to decouple components.
|
||||
- **Code Splitting:**
|
||||
- Break down large files into smaller, more manageable ones.
|
||||
- Split code based on functionality or responsibility.
|
||||
- Use lazy loading for modules that are not immediately needed.
|
||||
## 1. Embrace Lazy Evaluation
|
||||
|
||||
- **Common Patterns and Anti-patterns**
|
||||
- **Design Patterns:**
|
||||
- **Factory Pattern:** Use factories to create instances of tensors on different devices.
|
||||
- **Strategy Pattern:** Use the strategy pattern to implement different optimization algorithms.
|
||||
- **Observer Pattern:** Implement an observer pattern for monitoring training progress.
|
||||
- **Recommended Approaches:**
|
||||
- When implementing new operators, consider their performance implications on different hardware backends. Optimize for the common case.
|
||||
- Use NumPy-like syntax where possible to improve code readability for those familiar with NumPy.
|
||||
- **Anti-patterns and Code Smells:**
|
||||
- **God Classes:** Avoid creating classes that do too much. Split them into smaller, more focused classes.
|
||||
- **Long Methods:** Break down long methods into smaller, more manageable ones.
|
||||
- **Duplicated Code:** Extract duplicated code into reusable functions or classes.
|
||||
- **Magic Numbers:** Use named constants instead of hardcoding numerical values.
|
||||
- **Excessive Nesting:** Reduce nesting by using helper functions or early returns.
|
||||
- **State Management:**
|
||||
- Avoid global state. Pass state explicitly to functions and classes.
|
||||
- Use immutable data structures where possible.
|
||||
- Consider using a state management library for complex applications.
|
||||
- **Error Handling:**
|
||||
- Use exceptions to handle errors.
|
||||
- Provide informative error messages.
|
||||
- Catch exceptions at the appropriate level and handle them gracefully.
|
||||
- Avoid swallowing exceptions.
|
||||
tinygrad builds a computation graph that only executes when explicitly requested. This is the single most important concept for correctness and performance.
|
||||
|
||||
- **Performance Considerations**
|
||||
- **Optimization Techniques:**
|
||||
- **Operator Fusion:** Fuse multiple operations into a single kernel to reduce memory transfers.
|
||||
- **Loop Unrolling:** Unroll loops to improve instruction-level parallelism.
|
||||
- **Vectorization:** Use SIMD instructions to perform operations on multiple data elements in parallel.
|
||||
- **Code generation:** Use code generation to tailor the implementation to the specific hardware backend. Utilize libraries like LLVM.
|
||||
- **Memory Management:**
|
||||
- Minimize memory allocations and deallocations.
|
||||
- Use memory pools to reuse memory.
|
||||
- Be aware of memory alignment requirements for different hardware backends.
|
||||
- Optimize data layout for cache efficiency.
|
||||
- **Lazy Loading:**
|
||||
- Only load data when it is needed.
|
||||
- Use generators to process large datasets in chunks.
|
||||
### Explicitly Realize Tensors
|
||||
|
||||
- **Security Best Practices**
|
||||
- **Input Validation:**
|
||||
- Validate all inputs to prevent malicious data from corrupting the system.
|
||||
- Check for valid data types, ranges, and formats.
|
||||
- Sanitize inputs to remove potentially harmful characters.
|
||||
- **Data Protection:**
|
||||
- Encrypt sensitive data at rest and in transit.
|
||||
- Use strong authentication and authorization mechanisms to protect data access.
|
||||
Always call `.realize()` or `.numpy()` to force computation. Forgetting this is the most common pitfall.
|
||||
|
||||
- **Testing Approaches**
|
||||
- **Unit Testing:**
|
||||
- Test individual components in isolation.
|
||||
- Use mocking and stubbing to isolate dependencies.
|
||||
- Write tests for all public APIs.
|
||||
- Aim for high code coverage.
|
||||
- **Integration Testing:**
|
||||
- Test the interactions between different components.
|
||||
- Test the integration with external systems.
|
||||
- **End-to-End Testing:**
|
||||
- Test the entire application from end to end.
|
||||
- Simulate real-world user scenarios.
|
||||
- **Test Organization:**
|
||||
- Organize tests into a directory structure that mirrors the code structure.
|
||||
- Use meaningful test names.
|
||||
- Keep tests independent of each other.
|
||||
- **Mocking and Stubbing:**
|
||||
- Use mocking to replace dependencies with controlled substitutes.
|
||||
- Use stubbing to provide predefined responses to method calls.
|
||||
❌ **BAD: Silent no-op**
|
||||
```python
|
||||
from tinygrad import Tensor
|
||||
a = Tensor.rand(100)
|
||||
b = Tensor.rand(100)
|
||||
c = a + b # This does not compute anything
|
||||
# c is just a graph node, not a computed value
|
||||
```
|
||||
|
||||
- **Common Pitfalls and Gotchas**
|
||||
- **Frequent Mistakes:**
|
||||
- Neglecting to release allocated memory.
|
||||
- Using incorrect data types.
|
||||
- Incorrectly handling exceptions.
|
||||
- Premature optimization.
|
||||
- **Edge Cases:**
|
||||
- Handling empty tensors.
|
||||
- Handling tensors with different shapes.
|
||||
- Handling numerical instability.
|
||||
- **Version-Specific Issues:**
|
||||
- Be aware of breaking changes between different versions of tinygrad.
|
||||
- Consult the release notes for details.
|
||||
- **Compatibility Concerns:**
|
||||
- Be aware of compatibility issues between tinygrad and other libraries.
|
||||
- Test your code with different versions of dependencies.
|
||||
- **Debugging Strategies:**
|
||||
- Use a debugger to step through the code and inspect variables.
|
||||
- Use logging to track the execution flow.
|
||||
- Use assertions to check for unexpected conditions.
|
||||
✅ **GOOD: Force computation**
|
||||
```python
|
||||
from tinygrad import Tensor
|
||||
a = Tensor.rand(100)
|
||||
b = Tensor.rand(100)
|
||||
c = a + b
|
||||
c.realize() # Forces the computation of 'c'
|
||||
print(c.numpy()) # Also forces computation and converts to numpy array
|
||||
```
|
||||
|
||||
- **Tooling and Environment**
|
||||
- **Development Tools:**
|
||||
- PyCharm, VS Code, or other IDE.
|
||||
- Python debugger (pdb).
|
||||
- Profiler (e.g., cProfile).
|
||||
- Memory analyzer (e.g., memory_profiler).
|
||||
- **Build Configuration:**
|
||||
- Use a build system like setuptools.
|
||||
- Define dependencies in `requirements.txt`.
|
||||
- **Linting and Formatting:**
|
||||
- Use a linter like pylint or flake8.
|
||||
- Use a code formatter like black or autopep8.
|
||||
- Configure your IDE to automatically lint and format code.
|
||||
- **Deployment:**
|
||||
- Package your application into a Docker container.
|
||||
- Use a deployment platform like Kubernetes or AWS ECS.
|
||||
- **CI/CD Integration:**
|
||||
- Integrate your build, test, and deployment process into a CI/CD pipeline.
|
||||
- Use a CI/CD system like GitHub Actions or GitLab CI.
|
||||
### Group Realizations for Fusion
|
||||
|
||||
- **Additional Best Practices for Tinygrad:**
|
||||
- **Leverage the low-level nature:** Understand the underlying operations and memory management principles of tinygrad to write efficient code. Don't shy away from customizing operations when necessary.
|
||||
- **Contribute Back:** If you find a bug or implement a useful feature, consider contributing back to the tinygrad project. Help the community grow.
|
||||
- **Stay Updated:** Tinygrad is under active development. Stay up-to-date with the latest releases and best practices.
|
||||
- **Understand the Device Abstraction Layer:** Tinygrad's strength is its ability to target different hardware backends. Familiarize yourself with the device abstraction layer to write portable code.
|
||||
When multiple tensors depend on a shared computation, realize them together to allow tinygrad's scheduler to fuse kernels and avoid redundant memory traffic.
|
||||
|
||||
By following these best practices, you can write high-quality, maintainable, and performant code with tinygrad.
|
||||
❌ **BAD: Sequential realization, missed fusion opportunities**
|
||||
```python
|
||||
from tinygrad import Tensor
|
||||
a = Tensor.rand(100)
|
||||
b = Tensor.rand(100)
|
||||
c = Tensor.rand(100)
|
||||
out1 = a + b + c
|
||||
out2 = a + b - c
|
||||
|
||||
out1.realize() # Computes a+b+c
|
||||
out2.realize() # Recomputes a+b, then computes a+b-c
|
||||
```
|
||||
|
||||
✅ **GOOD: Parallel realization for kernel fusion**
|
||||
```python
|
||||
from tinygrad import Tensor
|
||||
a = Tensor.rand(100)
|
||||
b = Tensor.rand(100)
|
||||
c = Tensor.rand(100)
|
||||
out1 = a + b + c
|
||||
out2 = a + b - c
|
||||
|
||||
Tensor.realize(out1, out2) # Allows tinygrad to fuse (a+b) computation
|
||||
```
|
||||
|
||||
## 2. Optimize Model Structure and Parameters
|
||||
|
||||
Keep models simple and leverage tinygrad's parameter management.
|
||||
|
||||
### Use `get_parameters` for Optimizers
|
||||
|
||||
Always use `tinygrad.nn.state.get_parameters` to collect all trainable parameters for your optimizer. Manually listing parameters is error-prone and not scalable.
|
||||
|
||||
❌ **BAD: Manual parameter listing**
|
||||
```python
|
||||
from tinygrad import Tensor
|
||||
from tinygrad.nn.optim import SGD
|
||||
|
||||
class MyModel:
|
||||
def __init__(self):
|
||||
self.l1_w = Tensor.rand(10, 10, requires_grad=True)
|
||||
self.l1_b = Tensor.zeros(10, requires_grad=True)
|
||||
def __call__(self, x): return x @ self.l1_w + self.l1_b
|
||||
|
||||
net = MyModel()
|
||||
opt = SGD([net.l1_w, net.l1_b], lr=1e-3) # Easy to miss parameters
|
||||
```
|
||||
|
||||
✅ **GOOD: Automatic parameter collection**
|
||||
```python
|
||||
from tinygrad import Tensor
|
||||
from tinygrad.nn.optim import SGD
|
||||
from tinygrad.nn.state import get_parameters
|
||||
|
||||
class MyModel:
|
||||
def __init__(self):
|
||||
self.l1_w = Tensor.rand(10, 10, requires_grad=True)
|
||||
self.l1_b = Tensor.zeros(10, requires_grad=True)
|
||||
def __call__(self, x): return x @ self.l1_w + self.l1_b
|
||||
|
||||
net = MyModel()
|
||||
opt = SGD(get_parameters(net), lr=1e-3) # Robust and scalable
|
||||
```
|
||||
|
||||
### Keep Imports Minimal
|
||||
|
||||
Adhere to tinygrad's minimalist ethos. Only import what you need.
|
||||
|
||||
❌ **BAD: Over-importing**
|
||||
```python
|
||||
import numpy as np
|
||||
import time
|
||||
from tinygrad.helpers import Timing, getenv # getenv is rarely needed
|
||||
from tinygrad.nn import optim, state # Import specific classes
|
||||
from tinygrad import Tensor, dtypes, Device # Device is often implicit
|
||||
```
|
||||
|
||||
✅ **GOOD: Targeted imports**
|
||||
```python
|
||||
import numpy as np # Only if explicitly using numpy
|
||||
from tinygrad import Tensor, dtypes
|
||||
from tinygrad.helpers import Timing
|
||||
from tinygrad.nn.optim import SGD
|
||||
from tinygrad.nn.state import get_parameters
|
||||
```
|
||||
|
||||
## 3. Performance Profiling
|
||||
|
||||
Measure, don't guess. Use tinygrad's built-in tools to identify bottlenecks.
|
||||
|
||||
### Use `Timing` for Benchmarking
|
||||
|
||||
Always wrap performance-critical sections with `Timing` to get accurate execution times, especially when comparing different approaches.
|
||||
|
||||
❌ **BAD: Inaccurate `time.time()` for lazy ops**
|
||||
```python
|
||||
import time
|
||||
from tinygrad import Tensor
|
||||
a = Tensor.rand(1000, 1000)
|
||||
b = Tensor.rand(1000, 1000)
|
||||
start = time.time()
|
||||
c = a @ b # This is lazy, time.time() will be misleadingly fast
|
||||
print(f"Time: {time.time() - start:.6f}s")
|
||||
# Output: Time: 0.000123s (incorrect)
|
||||
```
|
||||
|
||||
✅ **GOOD: Use `Timing` for accurate measurement**
|
||||
```python
|
||||
from tinygrad import Tensor
|
||||
from tinygrad.helpers import Timing
|
||||
|
||||
a = Tensor.rand(1000, 1000)
|
||||
b = Tensor.rand(1000, 1000)
|
||||
|
||||
with Timing("Matrix multiplication"):
|
||||
c = a @ b
|
||||
c.realize() # Ensure computation is part of the timing
|
||||
# Output: Matrix multiplication: 0.012345s (example)
|
||||
```
|
||||
|
||||
## 4. Handle Load/Store Operations
|
||||
|
||||
tinygrad does not natively support load/store ops to simplify backend porting. Implement them using `arange` masks.
|
||||
|
||||
### Implement Custom Load/Store with `arange`
|
||||
|
||||
If you need conditional writes or complex indexing, use `Tensor.arange` to create masks.
|
||||
|
||||
❌ **BAD: Attempting direct conditional assignment (not supported)**
|
||||
```python
|
||||
# This pattern is not directly supported in tinygrad
|
||||
# x[mask] = value
|
||||
```
|
||||
|
||||
✅ **GOOD: Using `where` with `arange` for conditional logic**
|
||||
```python
|
||||
from tinygrad import Tensor, dtypes
|
||||
|
||||
def sparse_update(x: Tensor, indices: Tensor, values: Tensor) -> Tensor:
|
||||
# Example: update specific elements in x with values based on indices
|
||||
# This is a simplified conceptual example, actual implementation
|
||||
# for complex sparse updates would be more involved.
|
||||
# For a real-world example, see sparse_categorical_crossentropy in tinygrad.
|
||||
mask = (Tensor.arange(x.numel(), dtype=dtypes.int32, requires_grad=False) == indices.flatten().unsqueeze(1)).any(axis=1).reshape(x.shape)
|
||||
return mask.where(values, x) # Conditionally replace elements
|
||||
```
|
||||
|
||||
## 5. Reproducibility and Development Workflow
|
||||
|
||||
Maintain a disciplined workflow for robust and reproducible experiments.
|
||||
|
||||
### Commit Before Experiments
|
||||
|
||||
Always commit your code before running experiments. This ensures you can trace results back to a specific Git hash.
|
||||
|
||||
❌ **BAD: Running experiments on uncommitted changes**
|
||||
```bash
|
||||
# You run your training script
|
||||
python train.py --epochs 10 --lr 0.001
|
||||
|
||||
# Later, you can't reproduce the exact code that generated the results.
|
||||
```
|
||||
|
||||
✅ **GOOD: Version control for reproducibility**
|
||||
```bash
|
||||
git add .
|
||||
git commit -m "Experiment: Initial MNIST training with Leaky ReLU"
|
||||
# Get the commit hash: git rev-parse HEAD
|
||||
# Store this hash with your experiment logs and results.
|
||||
python train.py --epochs 10 --lr 0.001 --git_hash $(git rev-parse HEAD)
|
||||
```
|
||||
Reference in New Issue
Block a user