mathformer 1.0.0__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. mathformer-1.0.2/MANIFEST.in +4 -0
  2. mathformer-1.0.2/PKG-INFO +124 -0
  3. mathformer-1.0.2/README.md +102 -0
  4. {mathformer-1.0.0 → mathformer-1.0.2}/pyproject.toml +10 -4
  5. {mathformer-1.0.0 → mathformer-1.0.2}/src/mathformer/__init__.py +1 -1
  6. mathformer-1.0.2/src/mathformer/__pycache__/__init__.cpython-311.pyc +0 -0
  7. mathformer-1.0.2/src/mathformer/__pycache__/api.cpython-311.pyc +0 -0
  8. mathformer-1.0.2/src/mathformer/__pycache__/tokenizer.cpython-311.pyc +0 -0
  9. mathformer-1.0.2/src/mathformer/addformer/config.json +33 -0
  10. mathformer-1.0.2/src/mathformer/addformer/generation_config.json +10 -0
  11. mathformer-1.0.2/src/mathformer/addformer/model.safetensors +0 -0
  12. mathformer-1.0.2/src/mathformer/addformer/special_tokens_map.json +6 -0
  13. mathformer-1.0.2/src/mathformer/addformer/tokenizer_config.json +9 -0
  14. mathformer-1.0.2/src/mathformer/addformer/training_args.bin +0 -0
  15. mathformer-1.0.2/src/mathformer/addformer/vocab.json +29 -0
  16. mathformer-1.0.2/src/mathformer/divformer/config.json +33 -0
  17. mathformer-1.0.2/src/mathformer/divformer/generation_config.json +10 -0
  18. mathformer-1.0.2/src/mathformer/divformer/model.safetensors +0 -0
  19. mathformer-1.0.2/src/mathformer/divformer/special_tokens_map.json +6 -0
  20. mathformer-1.0.2/src/mathformer/divformer/tokenizer_config.json +9 -0
  21. mathformer-1.0.2/src/mathformer/divformer/training_args.bin +0 -0
  22. mathformer-1.0.2/src/mathformer/divformer/vocab.json +29 -0
  23. mathformer-1.0.2/src/mathformer/mulformer/config.json +33 -0
  24. mathformer-1.0.2/src/mathformer/mulformer/generation_config.json +10 -0
  25. mathformer-1.0.2/src/mathformer/mulformer/model.safetensors +0 -0
  26. mathformer-1.0.2/src/mathformer/mulformer/special_tokens_map.json +6 -0
  27. mathformer-1.0.2/src/mathformer/mulformer/tokenizer_config.json +9 -0
  28. mathformer-1.0.2/src/mathformer/mulformer/training_args.bin +0 -0
  29. mathformer-1.0.2/src/mathformer/mulformer/vocab.json +29 -0
  30. mathformer-1.0.2/src/mathformer/subformer/config.json +33 -0
  31. mathformer-1.0.2/src/mathformer/subformer/generation_config.json +10 -0
  32. mathformer-1.0.2/src/mathformer/subformer/model.safetensors +0 -0
  33. mathformer-1.0.2/src/mathformer/subformer/special_tokens_map.json +6 -0
  34. mathformer-1.0.2/src/mathformer/subformer/tokenizer_config.json +9 -0
  35. mathformer-1.0.2/src/mathformer/subformer/training_args.bin +0 -0
  36. mathformer-1.0.2/src/mathformer/subformer/vocab.json +29 -0
  37. mathformer-1.0.2/src/mathformer.egg-info/PKG-INFO +124 -0
  38. mathformer-1.0.2/src/mathformer.egg-info/SOURCES.txt +44 -0
  39. mathformer-1.0.0/PKG-INFO +0 -40
  40. mathformer-1.0.0/README.md +0 -18
  41. mathformer-1.0.0/src/mathformer.egg-info/PKG-INFO +0 -40
  42. mathformer-1.0.0/src/mathformer.egg-info/SOURCES.txt +0 -12
  43. {mathformer-1.0.0 → mathformer-1.0.2}/LICENSE +0 -0
  44. {mathformer-1.0.0 → mathformer-1.0.2}/setup.cfg +0 -0
  45. {mathformer-1.0.0 → mathformer-1.0.2}/src/mathformer/api.py +0 -0
  46. {mathformer-1.0.0 → mathformer-1.0.2}/src/mathformer/tokenizer.py +0 -0
  47. {mathformer-1.0.0 → mathformer-1.0.2}/src/mathformer.egg-info/dependency_links.txt +0 -0
  48. {mathformer-1.0.0 → mathformer-1.0.2}/src/mathformer.egg-info/requires.txt +0 -0
  49. {mathformer-1.0.0 → mathformer-1.0.2}/src/mathformer.egg-info/top_level.txt +0 -0
  50. {mathformer-1.0.0 → mathformer-1.0.2}/tests/test_api.py +0 -0
@@ -0,0 +1,4 @@
1
+ recursive-include src/mathformer/addformer *
2
+ recursive-include src/mathformer/subformer *
3
+ recursive-include src/mathformer/mulformer *
4
+ recursive-include src/mathformer/divformer *
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.4
2
+ Name: mathformer
3
+ Version: 1.0.2
4
+ Summary: A transformer-based math library
5
+ Author-email: JeremySu0818 <xinghong.su0818@gmail.com>
6
+ Project-URL: Homepage, https://github.com/JeremySu0818/MathFormer
7
+ Project-URL: Bug Tracker, https://github.com/JeremySu0818/MathFormer/issues
8
+ Project-URL: Repository, https://github.com/JeremySu0818/MathFormer
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: torch>=2.0.0
16
+ Requires-Dist: transformers>=4.30.0
17
+ Requires-Dist: safetensors>=0.3.0
18
+ Provides-Extra: dev
19
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
20
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
21
+ Dynamic: license-file
22
+
23
+ # MathFormer
24
+
25
+ MathFormer is a Python library that leverages Transformer-based language models to perform mathematical operations. Unlike standard arithmetic libraries, MathFormer uses Llama-architecture models to "predict" the results of arithmetic operations, token by token, demonstrating the capability of small language models to learn arithmetic rules.
26
+
27
+ It supports basic arithmetic operations: **Addition**, **Subtraction**, **Multiplication**, and **Division**.
28
+
29
+ ## Features
30
+
31
+ - **Transformer-Powered Arithmetic**: Uses specialized Llama-based models for each arithmetic operation.
32
+ - **Large Number Support**: Implements recursive logic to handle multi-digit arithmetic using digit-by-digit prediction (similar to manual calculation).
33
+ - **Unified API**: Easy-to-use functions for `add`, `sub`, `mul`, and `div`.
34
+ - **Resource Management**: Supports lazy loading of models to save memory, and manual unloading.
35
+ - **Custom Tokenizer**: Built-in minimalist tokenizer optimized for mathematical expressions.
36
+
37
+ ## Installation
38
+
39
+ You can install MathFormer via pip:
40
+
41
+ ```bash
42
+ pip install mathformer
43
+ ```
44
+
45
+ ## Quick Start
46
+
47
+ The simplest way to use MathFormer is through the top-level convenience functions. These functions automatically handle model loading when needed.
48
+
49
+ ```python
50
+ import mathformer
51
+
52
+ # Addition
53
+ result = mathformer.add(123, 456)
54
+ print(f"123 + 456 = {result}") # Output: 579
55
+
56
+ # Subtraction
57
+ result = mathformer.sub(1000, 250)
58
+ print(f"1000 - 250 = {result}") # Output: 750
59
+
60
+ # Multiplication
61
+ result = mathformer.mul(12, 12)
62
+ print(f"12 * 12 = {result}") # Output: 144
63
+
64
+ # Division (returns Quotient and Remainder if applicable)
65
+ result = mathformer.div(100, 3)
66
+ print(f"100 / 3 = {result}") # Output: Q33R1
67
+ ```
68
+
69
+ You can also pass string expressions:
70
+
71
+ ```python
72
+ print(mathformer.add("100 + 200"))
73
+ print(mathformer.calculate("mul", 50, 4))
74
+ ```
75
+
76
+ ## Advanced Usage
77
+
78
+ For more control over resource usage, you can use the `MathFormerAPI` class directly.
79
+
80
+ ### Managing Resources (Load/Unload)
81
+
82
+ By default, models are lazy-loaded (loaded only when first requested). You can manually load all models at startup or unload them to free GPU/CPU memory.
83
+
84
+ ```python
85
+ from mathformer import MathFormerAPI
86
+
87
+ # Initialize API (lazy_load=False to load everything immediately)
88
+ api = MathFormerAPI(lazy_load=True)
89
+
90
+ # Perform operations
91
+ print(api.add(50, 50))
92
+
93
+ # Unload all models to free memory
94
+ api.unload_all()
95
+ ```
96
+
97
+ ### Context Manager
98
+
99
+ You can use `MathFormerAPI` as a context manager to ensure models are cleaned up after use:
100
+
101
+ ```python
102
+ from mathformer import MathFormerAPI
103
+
104
+ with MathFormerAPI() as api:
105
+ print(api.mul(99, 9))
106
+ # Models are automatically unloaded here
107
+ ```
108
+
109
+ ## How It Works
110
+
111
+ MathFormer isn't just calling Python's `+` or `-` operators. It actually uses a neural network to predict the result!
112
+
113
+ 1. **Single-Step Prediction**: For small single-digit operations (e.g., `5+7`), it queries a Transformer model customized for that operation.
114
+ 2. **Multi-Digit Logic**: For larger numbers (e.g., `123+456`), the library implements the standard grade-school algorithms (carrying, borrowing, partial products) but delegates the fundamental single-digit arithmetic steps to the Transformer model.
115
+
116
+ ## Requirements
117
+
118
+ - Python >= 3.8
119
+ - PyTorch >= 2.0.0
120
+ - Transformers >= 4.30.0
121
+
122
+ ## License
123
+
124
+ This project is licensed under the MIT License.
@@ -0,0 +1,102 @@
1
+ # MathFormer
2
+
3
+ MathFormer is a Python library that leverages Transformer-based language models to perform mathematical operations. Unlike standard arithmetic libraries, MathFormer uses Llama-architecture models to "predict" the results of arithmetic operations, token by token, demonstrating the capability of small language models to learn arithmetic rules.
4
+
5
+ It supports basic arithmetic operations: **Addition**, **Subtraction**, **Multiplication**, and **Division**.
6
+
7
+ ## Features
8
+
9
+ - **Transformer-Powered Arithmetic**: Uses specialized Llama-based models for each arithmetic operation.
10
+ - **Large Number Support**: Implements recursive logic to handle multi-digit arithmetic using digit-by-digit prediction (similar to manual calculation).
11
+ - **Unified API**: Easy-to-use functions for `add`, `sub`, `mul`, and `div`.
12
+ - **Resource Management**: Supports lazy loading of models to save memory, and manual unloading.
13
+ - **Custom Tokenizer**: Built-in minimalist tokenizer optimized for mathematical expressions.
14
+
15
+ ## Installation
16
+
17
+ You can install MathFormer via pip:
18
+
19
+ ```bash
20
+ pip install mathformer
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ The simplest way to use MathFormer is through the top-level convenience functions. These functions automatically handle model loading when needed.
26
+
27
+ ```python
28
+ import mathformer
29
+
30
+ # Addition
31
+ result = mathformer.add(123, 456)
32
+ print(f"123 + 456 = {result}") # Output: 579
33
+
34
+ # Subtraction
35
+ result = mathformer.sub(1000, 250)
36
+ print(f"1000 - 250 = {result}") # Output: 750
37
+
38
+ # Multiplication
39
+ result = mathformer.mul(12, 12)
40
+ print(f"12 * 12 = {result}") # Output: 144
41
+
42
+ # Division (returns Quotient and Remainder if applicable)
43
+ result = mathformer.div(100, 3)
44
+ print(f"100 / 3 = {result}") # Output: Q33R1
45
+ ```
46
+
47
+ You can also pass string expressions:
48
+
49
+ ```python
50
+ print(mathformer.add("100 + 200"))
51
+ print(mathformer.calculate("mul", 50, 4))
52
+ ```
53
+
54
+ ## Advanced Usage
55
+
56
+ For more control over resource usage, you can use the `MathFormerAPI` class directly.
57
+
58
+ ### Managing Resources (Load/Unload)
59
+
60
+ By default, models are lazy-loaded (loaded only when first requested). You can manually load all models at startup or unload them to free GPU/CPU memory.
61
+
62
+ ```python
63
+ from mathformer import MathFormerAPI
64
+
65
+ # Initialize API (lazy_load=False to load everything immediately)
66
+ api = MathFormerAPI(lazy_load=True)
67
+
68
+ # Perform operations
69
+ print(api.add(50, 50))
70
+
71
+ # Unload all models to free memory
72
+ api.unload_all()
73
+ ```
74
+
75
+ ### Context Manager
76
+
77
+ You can use `MathFormerAPI` as a context manager to ensure models are cleaned up after use:
78
+
79
+ ```python
80
+ from mathformer import MathFormerAPI
81
+
82
+ with MathFormerAPI() as api:
83
+ print(api.mul(99, 9))
84
+ # Models are automatically unloaded here
85
+ ```
86
+
87
+ ## How It Works
88
+
89
+ MathFormer isn't just calling Python's `+` or `-` operators. It actually uses a neural network to predict the result!
90
+
91
+ 1. **Single-Step Prediction**: For small single-digit operations (e.g., `5+7`), it queries a Transformer model customized for that operation.
92
+ 2. **Multi-Digit Logic**: For larger numbers (e.g., `123+456`), the library implements the standard grade-school algorithms (carrying, borrowing, partial products) but delegates the fundamental single-digit arithmetic steps to the Transformer model.
93
+
94
+ ## Requirements
95
+
96
+ - Python >= 3.8
97
+ - PyTorch >= 2.0.0
98
+ - Transformers >= 4.30.0
99
+
100
+ ## License
101
+
102
+ This project is licensed under the MIT License.
@@ -5,13 +5,19 @@
5
5
  [tool.setuptools.packages.find]
6
6
  where = ["src"]
7
7
 
8
+ [tool.setuptools]
9
+ include-package-data = true
10
+
11
+ [tool.setuptools.package-data]
12
+ mathformer = ["**"]
13
+
8
14
  [build-system]
9
15
  requires = ["setuptools>=61.0"]
10
16
  build-backend = "setuptools.build_meta"
11
17
 
12
18
  [project]
13
19
  name = "mathformer"
14
- version = "1.0.0"
20
+ version = "1.0.2"
15
21
  description = "A transformer-based math library"
16
22
  readme = "README.md"
17
23
  authors = [
@@ -36,9 +42,9 @@ dev = [
36
42
  ]
37
43
 
38
44
  [project.urls]
39
- "Homepage" = "https://github.com/JeremySu0818/MathFormer-API"
40
- "Bug Tracker" = "https://github.com/JeremySu0818/MathFormer-API/issues"
41
- "Repository" = "https://github.com/JeremySu0818/MathFormer-API"
45
+ "Homepage" = "https://github.com/JeremySu0818/MathFormer"
46
+ "Bug Tracker" = "https://github.com/JeremySu0818/MathFormer/issues"
47
+ "Repository" = "https://github.com/JeremySu0818/MathFormer"
42
48
 
43
49
  [tool.pytest.ini_options]
44
50
  testpaths = ["tests"]
@@ -3,7 +3,7 @@ from .api import MathFormerAPI, MathFormer
3
3
  from .tokenizer import MathTokenizer
4
4
 
5
5
 
6
- __version__ = "1.0.0"
6
+ __version__ = "1.0.2"
7
7
 
8
8
 
9
9
  _default_api = MathFormerAPI()
@@ -0,0 +1,33 @@
1
+ {
2
+ "architectures": [
3
+ "LlamaForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 1,
8
+ "dtype": "float32",
9
+ "eos_token_id": 2,
10
+ "head_dim": 8,
11
+ "hidden_act": "silu",
12
+ "hidden_dropout": 0.0,
13
+ "hidden_size": 64,
14
+ "initializer_range": 0.02,
15
+ "intermediate_size": 256,
16
+ "max_position_embeddings": 64,
17
+ "mlp_bias": false,
18
+ "model_type": "llama",
19
+ "num_attention_heads": 8,
20
+ "num_hidden_layers": 8,
21
+ "num_key_value_heads": 8,
22
+ "pad_token_id": 0,
23
+ "pretraining_tp": 1,
24
+ "rms_norm_eps": 1e-06,
25
+ "rope_parameters": {
26
+ "rope_theta": 10000.0,
27
+ "rope_type": "default"
28
+ },
29
+ "tie_word_embeddings": false,
30
+ "transformers_version": "5.0.0",
31
+ "use_cache": false,
32
+ "vocab_size": 27
33
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "output_attentions": false,
6
+ "output_hidden_states": false,
7
+ "pad_token_id": 0,
8
+ "transformers_version": "5.0.0",
9
+ "use_cache": true
10
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "</s>",
4
+ "unk_token": "<unk>",
5
+ "pad_token": "<pad>"
6
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "tokenizer_class": "MathTokenizer",
3
+ "model_max_length": 64,
4
+ "padding_side": "right",
5
+ "bos_token": "<s>",
6
+ "eos_token": "</s>",
7
+ "unk_token": "<unk>",
8
+ "pad_token": "<pad>"
9
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "<pad>": 0,
3
+ "<s>": 1,
4
+ "</s>": 2,
5
+ "<unk>": 3,
6
+ "0": 4,
7
+ "1": 5,
8
+ "2": 6,
9
+ "3": 7,
10
+ "4": 8,
11
+ "5": 9,
12
+ "6": 10,
13
+ "7": 11,
14
+ "8": 12,
15
+ "9": 13,
16
+ "+": 14,
17
+ "-": 15,
18
+ "*": 16,
19
+ "/": 17,
20
+ "=": 18,
21
+ ".": 19,
22
+ "(": 20,
23
+ ")": 21,
24
+ "^": 22,
25
+ "%": 23,
26
+ " ": 24,
27
+ "Q": 25,
28
+ "R": 26
29
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "architectures": [
3
+ "LlamaForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 1,
8
+ "dtype": "float32",
9
+ "eos_token_id": 2,
10
+ "head_dim": 8,
11
+ "hidden_act": "silu",
12
+ "hidden_dropout": 0.0,
13
+ "hidden_size": 64,
14
+ "initializer_range": 0.02,
15
+ "intermediate_size": 256,
16
+ "max_position_embeddings": 64,
17
+ "mlp_bias": false,
18
+ "model_type": "llama",
19
+ "num_attention_heads": 8,
20
+ "num_hidden_layers": 8,
21
+ "num_key_value_heads": 8,
22
+ "pad_token_id": 0,
23
+ "pretraining_tp": 1,
24
+ "rms_norm_eps": 1e-06,
25
+ "rope_parameters": {
26
+ "rope_theta": 10000.0,
27
+ "rope_type": "default"
28
+ },
29
+ "tie_word_embeddings": false,
30
+ "transformers_version": "5.0.0",
31
+ "use_cache": false,
32
+ "vocab_size": 27
33
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "output_attentions": false,
6
+ "output_hidden_states": false,
7
+ "pad_token_id": 0,
8
+ "transformers_version": "5.0.0",
9
+ "use_cache": true
10
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "</s>",
4
+ "unk_token": "<unk>",
5
+ "pad_token": "<pad>"
6
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "tokenizer_class": "MathTokenizer",
3
+ "model_max_length": 64,
4
+ "padding_side": "right",
5
+ "bos_token": "<s>",
6
+ "eos_token": "</s>",
7
+ "unk_token": "<unk>",
8
+ "pad_token": "<pad>"
9
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "<pad>": 0,
3
+ "<s>": 1,
4
+ "</s>": 2,
5
+ "<unk>": 3,
6
+ "0": 4,
7
+ "1": 5,
8
+ "2": 6,
9
+ "3": 7,
10
+ "4": 8,
11
+ "5": 9,
12
+ "6": 10,
13
+ "7": 11,
14
+ "8": 12,
15
+ "9": 13,
16
+ "+": 14,
17
+ "-": 15,
18
+ "*": 16,
19
+ "/": 17,
20
+ "=": 18,
21
+ ".": 19,
22
+ "(": 20,
23
+ ")": 21,
24
+ "^": 22,
25
+ "%": 23,
26
+ " ": 24,
27
+ "Q": 25,
28
+ "R": 26
29
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "architectures": [
3
+ "LlamaForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 1,
8
+ "dtype": "float32",
9
+ "eos_token_id": 2,
10
+ "head_dim": 8,
11
+ "hidden_act": "silu",
12
+ "hidden_dropout": 0.0,
13
+ "hidden_size": 64,
14
+ "initializer_range": 0.02,
15
+ "intermediate_size": 256,
16
+ "max_position_embeddings": 64,
17
+ "mlp_bias": false,
18
+ "model_type": "llama",
19
+ "num_attention_heads": 8,
20
+ "num_hidden_layers": 8,
21
+ "num_key_value_heads": 8,
22
+ "pad_token_id": 0,
23
+ "pretraining_tp": 1,
24
+ "rms_norm_eps": 1e-06,
25
+ "rope_parameters": {
26
+ "rope_theta": 10000.0,
27
+ "rope_type": "default"
28
+ },
29
+ "tie_word_embeddings": false,
30
+ "transformers_version": "5.0.0",
31
+ "use_cache": false,
32
+ "vocab_size": 27
33
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "output_attentions": false,
6
+ "output_hidden_states": false,
7
+ "pad_token_id": 0,
8
+ "transformers_version": "5.0.0",
9
+ "use_cache": true
10
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "</s>",
4
+ "unk_token": "<unk>",
5
+ "pad_token": "<pad>"
6
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "tokenizer_class": "MathTokenizer",
3
+ "model_max_length": 64,
4
+ "padding_side": "right",
5
+ "bos_token": "<s>",
6
+ "eos_token": "</s>",
7
+ "unk_token": "<unk>",
8
+ "pad_token": "<pad>"
9
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "<pad>": 0,
3
+ "<s>": 1,
4
+ "</s>": 2,
5
+ "<unk>": 3,
6
+ "0": 4,
7
+ "1": 5,
8
+ "2": 6,
9
+ "3": 7,
10
+ "4": 8,
11
+ "5": 9,
12
+ "6": 10,
13
+ "7": 11,
14
+ "8": 12,
15
+ "9": 13,
16
+ "+": 14,
17
+ "-": 15,
18
+ "*": 16,
19
+ "/": 17,
20
+ "=": 18,
21
+ ".": 19,
22
+ "(": 20,
23
+ ")": 21,
24
+ "^": 22,
25
+ "%": 23,
26
+ " ": 24,
27
+ "Q": 25,
28
+ "R": 26
29
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "architectures": [
3
+ "LlamaForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 1,
8
+ "dtype": "float32",
9
+ "eos_token_id": 2,
10
+ "head_dim": 8,
11
+ "hidden_act": "silu",
12
+ "hidden_dropout": 0.0,
13
+ "hidden_size": 64,
14
+ "initializer_range": 0.02,
15
+ "intermediate_size": 256,
16
+ "max_position_embeddings": 64,
17
+ "mlp_bias": false,
18
+ "model_type": "llama",
19
+ "num_attention_heads": 8,
20
+ "num_hidden_layers": 8,
21
+ "num_key_value_heads": 8,
22
+ "pad_token_id": 0,
23
+ "pretraining_tp": 1,
24
+ "rms_norm_eps": 1e-06,
25
+ "rope_parameters": {
26
+ "rope_theta": 10000.0,
27
+ "rope_type": "default"
28
+ },
29
+ "tie_word_embeddings": false,
30
+ "transformers_version": "5.0.0",
31
+ "use_cache": false,
32
+ "vocab_size": 27
33
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "output_attentions": false,
6
+ "output_hidden_states": false,
7
+ "pad_token_id": 0,
8
+ "transformers_version": "5.0.0",
9
+ "use_cache": true
10
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "</s>",
4
+ "unk_token": "<unk>",
5
+ "pad_token": "<pad>"
6
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "tokenizer_class": "MathTokenizer",
3
+ "model_max_length": 64,
4
+ "padding_side": "right",
5
+ "bos_token": "<s>",
6
+ "eos_token": "</s>",
7
+ "unk_token": "<unk>",
8
+ "pad_token": "<pad>"
9
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "<pad>": 0,
3
+ "<s>": 1,
4
+ "</s>": 2,
5
+ "<unk>": 3,
6
+ "0": 4,
7
+ "1": 5,
8
+ "2": 6,
9
+ "3": 7,
10
+ "4": 8,
11
+ "5": 9,
12
+ "6": 10,
13
+ "7": 11,
14
+ "8": 12,
15
+ "9": 13,
16
+ "+": 14,
17
+ "-": 15,
18
+ "*": 16,
19
+ "/": 17,
20
+ "=": 18,
21
+ ".": 19,
22
+ "(": 20,
23
+ ")": 21,
24
+ "^": 22,
25
+ "%": 23,
26
+ " ": 24,
27
+ "Q": 25,
28
+ "R": 26
29
+ }
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.4
2
+ Name: mathformer
3
+ Version: 1.0.2
4
+ Summary: A transformer-based math library
5
+ Author-email: JeremySu0818 <xinghong.su0818@gmail.com>
6
+ Project-URL: Homepage, https://github.com/JeremySu0818/MathFormer
7
+ Project-URL: Bug Tracker, https://github.com/JeremySu0818/MathFormer/issues
8
+ Project-URL: Repository, https://github.com/JeremySu0818/MathFormer
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: torch>=2.0.0
16
+ Requires-Dist: transformers>=4.30.0
17
+ Requires-Dist: safetensors>=0.3.0
18
+ Provides-Extra: dev
19
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
20
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
21
+ Dynamic: license-file
22
+
23
+ # MathFormer
24
+
25
+ MathFormer is a Python library that leverages Transformer-based language models to perform mathematical operations. Unlike standard arithmetic libraries, MathFormer uses Llama-architecture models to "predict" the results of arithmetic operations, token by token, demonstrating the capability of small language models to learn arithmetic rules.
26
+
27
+ It supports basic arithmetic operations: **Addition**, **Subtraction**, **Multiplication**, and **Division**.
28
+
29
+ ## Features
30
+
31
+ - **Transformer-Powered Arithmetic**: Uses specialized Llama-based models for each arithmetic operation.
32
+ - **Large Number Support**: Implements recursive logic to handle multi-digit arithmetic using digit-by-digit prediction (similar to manual calculation).
33
+ - **Unified API**: Easy-to-use functions for `add`, `sub`, `mul`, and `div`.
34
+ - **Resource Management**: Supports lazy loading of models to save memory, and manual unloading.
35
+ - **Custom Tokenizer**: Built-in minimalist tokenizer optimized for mathematical expressions.
36
+
37
+ ## Installation
38
+
39
+ You can install MathFormer via pip:
40
+
41
+ ```bash
42
+ pip install mathformer
43
+ ```
44
+
45
+ ## Quick Start
46
+
47
+ The simplest way to use MathFormer is through the top-level convenience functions. These functions automatically handle model loading when needed.
48
+
49
+ ```python
50
+ import mathformer
51
+
52
+ # Addition
53
+ result = mathformer.add(123, 456)
54
+ print(f"123 + 456 = {result}") # Output: 579
55
+
56
+ # Subtraction
57
+ result = mathformer.sub(1000, 250)
58
+ print(f"1000 - 250 = {result}") # Output: 750
59
+
60
+ # Multiplication
61
+ result = mathformer.mul(12, 12)
62
+ print(f"12 * 12 = {result}") # Output: 144
63
+
64
+ # Division (returns Quotient and Remainder if applicable)
65
+ result = mathformer.div(100, 3)
66
+ print(f"100 / 3 = {result}") # Output: Q33R1
67
+ ```
68
+
69
+ You can also pass string expressions:
70
+
71
+ ```python
72
+ print(mathformer.add("100 + 200"))
73
+ print(mathformer.calculate("mul", 50, 4))
74
+ ```
75
+
76
+ ## Advanced Usage
77
+
78
+ For more control over resource usage, you can use the `MathFormerAPI` class directly.
79
+
80
+ ### Managing Resources (Load/Unload)
81
+
82
+ By default, models are lazy-loaded (loaded only when first requested). You can manually load all models at startup or unload them to free GPU/CPU memory.
83
+
84
+ ```python
85
+ from mathformer import MathFormerAPI
86
+
87
+ # Initialize API (lazy_load=False to load everything immediately)
88
+ api = MathFormerAPI(lazy_load=True)
89
+
90
+ # Perform operations
91
+ print(api.add(50, 50))
92
+
93
+ # Unload all models to free memory
94
+ api.unload_all()
95
+ ```
96
+
97
+ ### Context Manager
98
+
99
+ You can use `MathFormerAPI` as a context manager to ensure models are cleaned up after use:
100
+
101
+ ```python
102
+ from mathformer import MathFormerAPI
103
+
104
+ with MathFormerAPI() as api:
105
+ print(api.mul(99, 9))
106
+ # Models are automatically unloaded here
107
+ ```
108
+
109
+ ## How It Works
110
+
111
+ MathFormer isn't just calling Python's `+` or `-` operators. It actually uses a neural network to predict the result!
112
+
113
+ 1. **Single-Step Prediction**: For small single-digit operations (e.g., `5+7`), it queries a Transformer model customized for that operation.
114
+ 2. **Multi-Digit Logic**: For larger numbers (e.g., `123+456`), the library implements the standard grade-school algorithms (carrying, borrowing, partial products) but delegates the fundamental single-digit arithmetic steps to the Transformer model.
115
+
116
+ ## Requirements
117
+
118
+ - Python >= 3.8
119
+ - PyTorch >= 2.0.0
120
+ - Transformers >= 4.30.0
121
+
122
+ ## License
123
+
124
+ This project is licensed under the MIT License.
@@ -0,0 +1,44 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ src/mathformer/__init__.py
6
+ src/mathformer/api.py
7
+ src/mathformer/tokenizer.py
8
+ src/mathformer.egg-info/PKG-INFO
9
+ src/mathformer.egg-info/SOURCES.txt
10
+ src/mathformer.egg-info/dependency_links.txt
11
+ src/mathformer.egg-info/requires.txt
12
+ src/mathformer.egg-info/top_level.txt
13
+ src/mathformer/__pycache__/__init__.cpython-311.pyc
14
+ src/mathformer/__pycache__/api.cpython-311.pyc
15
+ src/mathformer/__pycache__/tokenizer.cpython-311.pyc
16
+ src/mathformer/addformer/config.json
17
+ src/mathformer/addformer/generation_config.json
18
+ src/mathformer/addformer/model.safetensors
19
+ src/mathformer/addformer/special_tokens_map.json
20
+ src/mathformer/addformer/tokenizer_config.json
21
+ src/mathformer/addformer/training_args.bin
22
+ src/mathformer/addformer/vocab.json
23
+ src/mathformer/divformer/config.json
24
+ src/mathformer/divformer/generation_config.json
25
+ src/mathformer/divformer/model.safetensors
26
+ src/mathformer/divformer/special_tokens_map.json
27
+ src/mathformer/divformer/tokenizer_config.json
28
+ src/mathformer/divformer/training_args.bin
29
+ src/mathformer/divformer/vocab.json
30
+ src/mathformer/mulformer/config.json
31
+ src/mathformer/mulformer/generation_config.json
32
+ src/mathformer/mulformer/model.safetensors
33
+ src/mathformer/mulformer/special_tokens_map.json
34
+ src/mathformer/mulformer/tokenizer_config.json
35
+ src/mathformer/mulformer/training_args.bin
36
+ src/mathformer/mulformer/vocab.json
37
+ src/mathformer/subformer/config.json
38
+ src/mathformer/subformer/generation_config.json
39
+ src/mathformer/subformer/model.safetensors
40
+ src/mathformer/subformer/special_tokens_map.json
41
+ src/mathformer/subformer/tokenizer_config.json
42
+ src/mathformer/subformer/training_args.bin
43
+ src/mathformer/subformer/vocab.json
44
+ tests/test_api.py
mathformer-1.0.0/PKG-INFO DELETED
@@ -1,40 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mathformer
3
- Version: 1.0.0
4
- Summary: A transformer-based math library
5
- Author-email: JeremySu0818 <xinghong.su0818@gmail.com>
6
- Project-URL: Homepage, https://github.com/JeremySu0818/MathFormer-API
7
- Project-URL: Bug Tracker, https://github.com/JeremySu0818/MathFormer-API/issues
8
- Project-URL: Repository, https://github.com/JeremySu0818/MathFormer-API
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.8
13
- Description-Content-Type: text/markdown
14
- License-File: LICENSE
15
- Requires-Dist: torch>=2.0.0
16
- Requires-Dist: transformers>=4.30.0
17
- Requires-Dist: safetensors>=0.3.0
18
- Provides-Extra: dev
19
- Requires-Dist: pytest>=7.0.0; extra == "dev"
20
- Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
21
- Dynamic: license-file
22
-
23
- # MathFormer
24
-
25
- MathFormer is a Python library for mathematical operations using transformer architectures.
26
-
27
- ## Installation
28
-
29
- ```bash
30
- pip install mathformer
31
- ```
32
-
33
- ## Usage
34
-
35
- ```python
36
- import mathformer
37
-
38
- # Example usage
39
- # mathformer.do_something()
40
- ```
@@ -1,18 +0,0 @@
1
- # MathFormer
2
-
3
- MathFormer is a Python library for mathematical operations using transformer architectures.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- pip install mathformer
9
- ```
10
-
11
- ## Usage
12
-
13
- ```python
14
- import mathformer
15
-
16
- # Example usage
17
- # mathformer.do_something()
18
- ```
@@ -1,40 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mathformer
3
- Version: 1.0.0
4
- Summary: A transformer-based math library
5
- Author-email: JeremySu0818 <xinghong.su0818@gmail.com>
6
- Project-URL: Homepage, https://github.com/JeremySu0818/MathFormer-API
7
- Project-URL: Bug Tracker, https://github.com/JeremySu0818/MathFormer-API/issues
8
- Project-URL: Repository, https://github.com/JeremySu0818/MathFormer-API
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.8
13
- Description-Content-Type: text/markdown
14
- License-File: LICENSE
15
- Requires-Dist: torch>=2.0.0
16
- Requires-Dist: transformers>=4.30.0
17
- Requires-Dist: safetensors>=0.3.0
18
- Provides-Extra: dev
19
- Requires-Dist: pytest>=7.0.0; extra == "dev"
20
- Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
21
- Dynamic: license-file
22
-
23
- # MathFormer
24
-
25
- MathFormer is a Python library for mathematical operations using transformer architectures.
26
-
27
- ## Installation
28
-
29
- ```bash
30
- pip install mathformer
31
- ```
32
-
33
- ## Usage
34
-
35
- ```python
36
- import mathformer
37
-
38
- # Example usage
39
- # mathformer.do_something()
40
- ```
@@ -1,12 +0,0 @@
1
- LICENSE
2
- README.md
3
- pyproject.toml
4
- src/mathformer/__init__.py
5
- src/mathformer/api.py
6
- src/mathformer/tokenizer.py
7
- src/mathformer.egg-info/PKG-INFO
8
- src/mathformer.egg-info/SOURCES.txt
9
- src/mathformer.egg-info/dependency_links.txt
10
- src/mathformer.egg-info/requires.txt
11
- src/mathformer.egg-info/top_level.txt
12
- tests/test_api.py
File without changes
File without changes
File without changes