{"id":26772011,"url":"https://github.com/pocketpy/ctensor","last_synced_at":"2025-07-22T09:31:43.226Z","repository":{"id":275256126,"uuid":"925558428","full_name":"pocketpy/cTensor","owner":"pocketpy","description":"Portable Tensor Library with Automatic Differentiation in Modern C","archived":false,"fork":false,"pushed_at":"2025-07-21T12:18:25.000Z","size":268,"stargazers_count":10,"open_issues_count":0,"forks_count":11,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-21T14:24:30.330Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pocketpy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-01T06:32:49.000Z","updated_at":"2025-07-13T15:32:48.000Z","dependencies_parsed_at":"2025-04-15T20:00:53.945Z","dependency_job_id":"5280f932-5735-4d17-a241-ccc19e5070ee","html_url":"https://github.com/pocketpy/cTensor","commit_stats":null,"previous_names":["pocketpy/ctensor"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pocketpy/cTensor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocketpy%2FcTensor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocketpy%2FcTensor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocketpy%2FcTensor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocketpy%2FcTensor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pocketpy","download_url":"https://codeload.github.com/pocketpy/cTensor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocketpy%2FcTensor/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266465104,"owners_count":23933067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-29T00:37:19.620Z","updated_at":"2025-07-22T09:31:43.215Z","avatar_url":"https://github.com/pocketpy.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cTensor\n\nA lightweight neural network library written in C11 for embedded systems.\n\n## Overview\n\ncTensor is a compact tensor computation library designed for small client-side devices, such as mobile phones, microcontrollers. The library implements automatic differentiation and dynamic compute graph functionality, allowing for efficient training and deployment of neural networks on resource-constrained devices.\n\n## Current Status\n\nThis project is under active development. The prototype demonstrates basic tensor operations and neural network functionality using the Iris dataset as an example. Many core mathematical operators and features are still being implemented.\n\n## Features\n\n### Currently Implemented\n\n- **Lightweight C11 Implementation:** Minimal dependencies for wide compatibility\n- **Automatic Differentiation Framework:** Basic gradient computation infrastructure\n- **Dynamic Compute Graph:** Groundwork for efficient computation flow\n- **Basic Tensor Operations:** \n  - Basic arithmetic: add, subtract, multiply, divide, power\n  - Element-wise operations: square, reciprocal\n  - Matrix multiplication\n  - Tensor transpose\n- **Reduction Operations:**\n  - Sum (all elements or along dimension)\n  - Mean (all elements or along dimension)\n  - Max (all elements or along dimension with indices)\n  - Min (all elements or along dimension with indices)\n  - Argmax function\n- **Neural Network Components:**\n  - Linear layer\n  - Activation functions: ReLU, Sigmoid, Softmax\n  - Cross-entropy loss\n  - Softmax cross-entropy (combined operation)\n  - Glorot weight initialization\n- **SGD Optimizer:** Stochastic gradient descent implementation\n- **Memory Management:** Pool-based memory allocation system\n- **Tensor Utilities:**\n  - Element access and manipulation\n  - Tensor detachment\n  - Tensor unsqueeze operation\n  - Broadcasting support for element-wise operations\n  - Dataset normalization and shuffling utilities\n\n### Development Roadmap\n\nThe following features are planned for implementation:\n\n#### Math Operators\n- **Unary Operations:**\n  - Negative (Tensor_neg)\n  - Absolute value (Tensor_abs)\n- **Mathematical Functions:**\n  - Logarithm (nn_log)\n  - Exponential (nn_exp)\n  - Trigonometric functions (nn_sin, nn_cos, nn_tan)\n\n#### Broadcasting System Enhancements\n- Broadcasting for Matmul\n\n#### Activation Functions\n- ELU (Exponential Linear Unit)\n- SELU (Scaled Exponential Linear Unit)\n- Additional activation functions\n\n#### Loss Functions\n- Mean Squared Error (MSE)\n- Mean Absolute Error (MAE)\n- Huber Loss\n- Enhanced multi-class classification losses\n\n#### Advanced Optimizers\n- Adam optimizer\n- RMSProp optimizer\n- AdaGrad optimizer\n- Weight decay implementation\n- Gradient clipping\n\n#### Performance Enhancements\n- Profiling and benchmarking infrastructure\n- Loop unrolling and SIMD optimizations where applicable\n\n## Getting Started\n\n### Prerequisites\n\n- C Compiler with C11 support (GCC, Clang)\n- CMake (3.10+) for build configuration\n- Math library (automatically linked on non-Windows systems)\n\n### Building with CMake\n\n**On Windows:**\n```batch\nbuild.bat\n```\n\n**On Linux/macOS:**\n```bash\nmkdir -p build \u0026\u0026 cd build\ncmake ..\ncmake --build .\ncd ..\n```\n\n### Building with Direct Compilation\n\n**On Linux/macOS:**\n```bash\n./build_g.sh\n```\n\n**On Windows with GCC:**\n```batch\ngcc -std=c11 -Iinclude -O0 -Wfatal-errors -g -DDEBUG -lm src/nn.c src/operator.c src/basic.c src/iris_dataset.c src/context.c src/pool.c src/utils.c src/common/vector.c src/optimizer/sgd.c src2/main.c -o main\n```\nand run `main.exe` from root directory\n\n## Testing the Library\n\ncTensor uses a custom test framework. To run the tests:\n\nFor a more detailed guide, refer to [Testing Documentation](tests/README.md).\n\n```bash\n# Build the test executable with CMake\nmkdir -p build \u0026\u0026 cd build\ncmake ..\ncmake --build .\n\n# Run the tests\n./cten_exe\n```\n\n## Usage Example\n\nThe repository includes a simple example in `src2/main.c` that demonstrates how to train a neural network on the Iris dataset:\n\n```c\n#include \"cten.h\"\n#include \u003cstdio.h\u003e\n\nint main() {\n    // Initialize cTensor library\n    cten_initilize();\n    \n    // Load the Iris dataset\n    const float (*X)[4];\n    const int* y;\n    int num_samples = load_iris_dataset(\u0026X, \u0026y);\n    \n    // Create a simple neural network\n    TensorShape input_shape = {1, 4, 0, 0};  // 4 features\n    TensorShape hidden_shape = {4, 10, 0, 0}; // 10 hidden units\n    TensorShape output_shape = {10, 3, 0, 0}; // 3 classes (iris species)\n    \n    // Initialize network parameters with Glorot initialization\n    Tensor W1 = Glorot_init(hidden_shape, true);\n    Tensor b1 = Tensor_zeros((TensorShape){1, 10, 0, 0}, true);\n    Tensor W2 = Glorot_init(output_shape, true);\n    Tensor b2 = Tensor_zeros((TensorShape){1, 3, 0, 0}, true);\n    \n    // Setup optimizer\n    Tensor params[4] = {W1, b1, W2, b2};\n    optim_sgd* optimizer = optim_sgd_new(4, params);\n    optim_sgd_config(optimizer, 0.01f, 0.9f);\n    \n    // Training loop\n    // ...\n    \n    cten_finalize();\n    return 0;\n}\n```\n\n## API Overview\n\n### Tensor Creation and Management\n\n```c\n// Basic tensor creation\nTensor Tensor_new(TensorShape shape, bool requires_grad);\nTensor Tensor_zeros(TensorShape shape, bool requires_grad);\nTensor Tensor_ones(TensorShape shape, bool requires_grad);\n\n// Tensor manipulation\nTensor Tensor_transpose(Tensor self);\nTensor Tensor_detach(Tensor self);\nTensor Tensor_unsqueeze(Tensor self, int dim);\n\n// Element access\nfloat Tensor_get(Tensor self, int i, int j, int k, int l);\nvoid Tensor_set(Tensor self, int i, int j, int k, int l, float value);\n\n// Backpropagation\nvoid Tensor_backward(Tensor self, Tensor grad);\n```\n\n### Basic Operations\n\n```c\n// Element-wise operations with tensors\nTensor Tensor_add(Tensor self, Tensor other);\nTensor Tensor_sub(Tensor self, Tensor other);\nTensor Tensor_mul(Tensor self, Tensor other);\nTensor Tensor_div(Tensor self, Tensor other);\nTensor Tensor_pow(Tensor self, Tensor other);\n\n// Element-wise operations with scalars\nTensor Tensor_addf(Tensor self, float other);\nTensor Tensor_subf(Tensor self, float other);\nTensor Tensor_mulf(Tensor self, float other);\nTensor Tensor_divf(Tensor self, float other);\nTensor Tensor_powf(Tensor self, float other);\n\n// Matrix operations\nTensor Tensor_matmul(Tensor self, Tensor other);\n\n// Unary operations\nTensor Tensor_square(Tensor self);\nTensor Tensor_reciprocal(Tensor self);\n```\n\n### Reduction Operations\n\n```c\n// Reduction operations (with macro dispatch)\nTensor Tensor_sum(Tensor self);           // Sum all elements\nTensor Tensor_sum(Tensor self, int dim);  // Sum along dimension\n\nTensor Tensor_mean(Tensor self);          // Mean of all elements\nTensor Tensor_mean(Tensor self, int dim); // Mean along dimension\n\nTensor Tensor_max(Tensor self);           // Max of all elements\nTensorMaxMinResult Tensor_max(Tensor self, int dim); // Max along dimension\n\nTensor Tensor_min(Tensor self);           // Min of all elements\nTensorMaxMinResult Tensor_min(Tensor self, int dim); // Min along dimension\n\n// Argmax operation\nvoid Tensor_argmax(Tensor self, int* out);\n```\n\n### Neural Network Functions\n\n```c\n// Neural network layers\nTensor nn_linear(Tensor input, Tensor weight, Tensor bias);\n\n// Activation functions\nTensor nn_relu(Tensor input);\nTensor nn_sigmoid(Tensor input);\nTensor nn_tanh(Tensor input);\nTensor nn_softmax(Tensor input);\n\n// Loss functions\nTensor nn_crossentropy(Tensor y_true, Tensor y_pred);\nTensor nn_softmax_crossentropy(Tensor y_true, Tensor logits);\n\n// Weight initialization\nTensor Glorot_init(TensorShape shape, bool requires_grad);\n```\n\n### Optimizer\n\n```c\n// SGD Optimizer\noptim_sgd* optim_sgd_new(int n_params, Tensor* params);\nvoid optim_sgd_config(optim_sgd* self, float lr, float momentum);\nvoid optim_sgd_zerograd(optim_sgd* self);\nvoid optim_sgd_step(optim_sgd* self);\nvoid optim_sgd_delete(optim_sgd* self);\n```\n\n### Utility Functions\n\n```c\n// TensorShape utilities\nint TensorShape_numel(TensorShape shape);\nint TensorShape_dim(TensorShape shape);\nint TensorShape_asdim(TensorShape shape, int dim);\nint TensorShape_tostring(TensorShape shape, char* buf, int size);\n\n// Dataset utilities\nint load_iris_dataset(const float (**X)[4], const int** y);\nvoid Tensor_normalize_dataset(const float (*X)[4], float (*X_norm)[4], int n_samples, int n_train_samples, int n_features);\nvoid Tensor_shuffle_dataset(const float (*X)[4], const int *y, float (*X_shuffled)[4], int *y_shuffled, int n_samples, int n_features);\n\n// Evaluation mode\nvoid cten_begin_eval();\nbool cten_is_eval();\nvoid cten_end_eval();\n```\n\n## Memory Management\n\ncTensor uses a pool-based memory management system to efficiently handle tensor allocations:\n\n```c\nvoid cten_begin_malloc(PoolId id);\nvoid cten_end_malloc();\nvoid cten_free(PoolId id);\n```\n\n## Project Structure\n\n```\ncTensor/\n├── include/          # Header files defining the API\n├── src/              # Core implementation files\n│   ├── basic.c       # Basic tensor operations\n│   ├── nn.c          # Neural network primitives\n│   ├── operator.c    # Mathematical operators\n│   └── ...\n├── src2/             # Example applications\n│   └── main.c        # Iris dataset example\n└── tests/            # Test suite\n```\n\n## Contributing\n\nContributions to cTensor are welcome! The project needs implementation of various components as outlined in the Development Roadmap section. Key areas for contribution include:\n\n1. **Activation Functions:** Implementing additional activation functions (ELU, SELU) with gradient support\n2. **Loss Functions:** Adding more loss functions (MSE, MAE, Huber) with gradient support\n3. **Advanced Optimizers:** Creating additional optimizers beyond SGD (Adam, RMSProp, AdaGrad)\n4. **Performance Optimization:** Enhancing computational efficiency through benchmarking and optimizations\n5. **Documentation:** Improving examples, tutorials, and API documentation\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpocketpy%2Fctensor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpocketpy%2Fctensor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpocketpy%2Fctensor/lists"}